feat: expose native visual protocols and explicit invalidation
Verify Tessera GUI / verify (push) Failing after 7m23s

This commit is contained in:
2026-10-21 02:42:16 +08:00
parent d2f167e71f
commit dc61f5e33c
13 changed files with 691 additions and 140 deletions
+244
View File
@@ -0,0 +1,244 @@
//! Native visual protocol entry points. Values are ordinary managed Endor
//! objects; geometry is scoped to a live occurrence, never stored on a Block.
use crate::{
block,
document::RenderCommand,
layout::{self, Size},
mount::Mount,
ui_bridge::{MAX_DIMENSION, attach_desktop_type},
};
use endor_core::{
ContextNativeFunction, NativeCallable, NativeContext, NativeResult, ObjectRef, Value, Vm,
VmError,
};
fn invalid(message: &'static str) -> VmError {
VmError::InvalidArgument(message)
}
fn object(value: &Value) -> Result<ObjectRef, VmError> {
if let Value::Object(object) = value {
Ok(*object)
} else {
Err(invalid("visual protocol requires objects"))
}
}
fn number(
cx: &NativeContext<'_>,
object: ObjectRef,
name: &str,
signed: bool,
) -> Result<f32, VmError> {
let Value::Number(value) = cx.store().get_attr(object, name)? else {
return Err(invalid("visual field must be a Number"));
};
let value = value
.to_string()
.parse::<f32>()
.map_err(|_| invalid("invalid visual number"))?;
if !value.is_finite() || value.abs() > MAX_DIMENSION as f32 || (!signed && value < 0.0) {
return Err(invalid("visual number exceeds bounds"));
}
Ok(value)
}
fn value(value: f32) -> Value {
Value::Number((value as f64).into())
}
fn record(
cx: &mut NativeContext<'_>,
desktop: ObjectRef,
kind: &str,
fields: Vec<(&str, Value)>,
) -> Result<ObjectRef, VmError> {
let object = cx.new_object()?;
attach_desktop_type(cx, object, kind, desktop)?;
for (key, value) in fields {
cx.set_attr(object, key, value)?;
}
Ok(object)
}
fn size(node: &crate::ui_bridge::UiNode) -> Result<Size, VmError> {
let frame =
layout::frame(node).map_err(|_| invalid("native measurement exceeds layout limits"))?;
let root = frame
.mounts
.first()
.ok_or(invalid("empty native measurement"))?;
Ok(Size {
width: root.width,
height: root.height,
})
}
#[derive(Debug, Clone, Copy)]
enum Query {
Measure,
Arrange,
Paint,
HitTest,
}
#[derive(Debug)]
struct Method {
query: Query,
desktop: ObjectRef,
}
impl NativeCallable for Method {
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
let [owner, context, input] = args else {
return Err(invalid("visual method expects a Mount and an input value"));
};
let owner = object(owner)?;
let mount = cx.store().payload::<Mount>(object(context)?, "Mount")?;
if !mount.mounted || mount.block != owner {
return Err(invalid(
"visual context is detached or belongs to another Block",
));
}
let geometry = mount.geometry.clone();
let input = object(input)?;
let node = block::snapshot(cx.store(), owner)?;
let result = match self.query {
Query::Measure => {
let min_w = number(cx, input, "minWidth", false)?;
let min_h = number(cx, input, "minHeight", false)?;
let max_w = number(cx, input, "maxWidth", false)?;
let max_h = number(cx, input, "maxHeight", false)?;
if min_w > max_w || min_h > max_h {
return Err(invalid("minimum exceeds maximum constraint"));
}
let size = size(&node)?;
Value::Object(record(
cx,
self.desktop,
"MeasureResult",
vec![
("width", value(size.width.clamp(min_w, max_w))),
("height", value(size.height.clamp(min_h, max_h))),
],
)?)
}
Query::Arrange => {
let x = number(cx, input, "x", false)?;
let y = number(cx, input, "y", false)?;
let width = number(cx, input, "width", false)?;
let height = number(cx, input, "height", false)?;
if x + width > MAX_DIMENSION as f32 || y + height > MAX_DIMENSION as f32 {
return Err(invalid("visual bounds exceed limits"));
}
let children: Vec<_> = node
.children()
.iter()
.map(|child| size(child))
.collect::<Result<_, _>>()?;
let points = layout::arrange_native(&node, x, y, &children)
.map_err(|_| invalid("native arrangement exceeds limits"))?;
let mut result = Vec::new();
for ((slot, size), (x, y)) in node.slots.iter().zip(children).zip(points) {
let bounds = record(
cx,
self.desktop,
"Bounds",
vec![
("x", value(x)),
("y", value(y)),
("width", value(size.width)),
("height", value(size.height)),
],
)?;
cx.set_attr(bounds, "slot", Value::Number((*slot as i64).into()))?;
result.push(bounds);
}
let result = crate::object_list::collection(cx, result, self.desktop)?;
Value::Object(record(
cx,
self.desktop,
"Arrangement",
vec![("children", Value::Object(result))],
)?)
}
Query::Paint => {
let x = number(cx, input, "x", false)?;
let y = number(cx, input, "y", false)?;
let width = number(cx, input, "width", false)?;
let height = number(cx, input, "height", false)?;
if x + width > MAX_DIMENSION as f32 || y + height > MAX_DIMENSION as f32 {
return Err(invalid("visual bounds exceed limits"));
}
let mut commands = Vec::new();
if let Some(command) = layout::paint_native(&node, x, y, Size { width, height }) {
let fields = match command {
RenderCommand::Text {
x,
y,
value: text,
size,
color,
} => vec![
("kind", Value::Text("text".into())),
("x", value(x)),
("y", value(y)),
("text", Value::Text(text)),
("size", Value::Number((size as i64).into())),
("color", Value::Text(color)),
],
RenderCommand::RoundedRect {
x,
y,
width,
height,
radius,
color,
} => vec![
("kind", Value::Text("roundedRect".into())),
("x", value(x)),
("y", value(y)),
("width", value(width)),
("height", value(height)),
("radius", value(radius)),
("color", Value::Text(color)),
],
_ => return Err(invalid("unsupported native paint command")),
};
let command = record(cx, self.desktop, "PaintCommand", fields)?;
commands.push(command);
}
let commands = crate::object_list::collection(cx, commands, self.desktop)?;
Value::Object(record(
cx,
self.desktop,
"DisplayList",
vec![("commands", Value::Object(commands))],
)?)
}
Query::HitTest => {
let x = number(cx, input, "x", true)?;
let y = number(cx, input, "y", true)?;
Value::Bool(x >= 0.0 && y >= 0.0 && x < geometry.width && y < geometry.height)
}
};
Ok(NativeResult::Return(result))
}
}
pub fn install(
vm: &mut Vm,
roots: &mut Vec<ObjectRef>,
desktop: ObjectRef,
block_type: ObjectRef,
function_type: ObjectRef,
) -> Result<(), VmError> {
for (name, query) in [
("measure", Query::Measure),
("arrange", Query::Arrange),
("paint", Query::Paint),
("hitTest", Query::HitTest),
] {
let function = vm.allocate(ContextNativeFunction::new(
name,
Method { query, desktop },
vec![desktop],
))?;
roots.push(function);
vm.attach_type(function, function_type)?;
vm.define_method(block_type, name, function)?;
}
Ok(())
}