feat: notify Endor objects of mount lifecycle transitions
Verify Tessera GUI / verify (push) Failing after 7m23s

This commit is contained in:
2026-10-21 02:29:46 +08:00
parent 8886bdbd2d
commit d2f167e71f
10 changed files with 373 additions and 49 deletions
+89 -9
View File
@@ -58,7 +58,7 @@ struct Method {
impl NativeCallable for Method {
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
let mount = cx.store().payload::<Mount>(self.owner, "Mount")?.clone();
if !mount.mounted {
if !mount.mounted && matches!(self.query, Query::Bounds | Query::Focus | Query::HitTest) {
return Err(VmError::InvalidArgument("Mount is no longer attached"));
}
let arity = if matches!(self.query, Query::HitTest) {
@@ -150,6 +150,7 @@ impl NativeCallable for Method {
#[derive(Debug, Default, Clone)]
pub struct MountTable {
pub active: HashMap<u64, WeakObjectRef>,
order: Vec<u64>,
}
#[derive(Debug)]
@@ -158,10 +159,51 @@ struct Sync {
page: Option<ObjectRef>,
nodes: Vec<WeakObjectRef>,
old: HashMap<u64, WeakObjectRef>,
old_order: Vec<u64>,
frame: Frame,
output: Arc<Mutex<HashMap<u64, WeakObjectRef>>>,
}
#[derive(Debug)]
struct LifecycleCall {
function: ObjectRef,
block: ObjectRef,
mount: ObjectRef,
}
impl NativeCallable for LifecycleCall {
fn invoke(&self, _: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
Ok(NativeResult::Invoke {
function: self.function,
arguments: vec![Value::Object(self.block), Value::Object(self.mount)],
})
}
}
fn lifecycle_call(
cx: &mut NativeContext<'_>,
mount: ObjectRef,
name: &'static str,
) -> Result<ObjectRef, VmError> {
let block = cx.store().payload::<Mount>(mount, "Mount")?.block;
let definition = cx
.store()
.type_of(block)?
.ok_or(VmError::InvalidArgument("untyped mounted Block"))?;
let function = cx
.store()
.lookup_method(definition, name)?
.ok_or(VmError::InvalidArgument("Block has no lifecycle method"))?;
cx.allocate(ContextNativeFunction::new(
name,
LifecycleCall {
function,
block,
mount,
},
vec![function, block, mount],
))
}
impl NativeCallable for Sync {
fn invoke(&self, cx: &mut NativeContext<'_>, _args: &[Value]) -> Result<NativeResult, VmError> {
let mut blocks = HashMap::new();
@@ -175,6 +217,7 @@ impl NativeCallable for Sync {
.map(|page| cx.store().downgrade(page))
.transpose()?;
let mut objects = HashMap::new();
let mut created = Vec::new();
for geometry in &self.frame.mounts {
let block = *blocks
.get(&geometry.block_id)
@@ -187,7 +230,13 @@ impl NativeCallable for Sync {
.map(|weak| cx.upgrade(*weak))
.transpose()?
.flatten();
let object = if let Some(old) = old {
let reusable = if let Some(old) = old {
let mount = cx.store().payload::<Mount>(old, "Mount")?;
(mount.mounted && mount.page == page && mount.block == block).then_some(old)
} else {
None
};
let object = if let Some(old) = reusable {
old
} else {
let object = cx.allocate(Mount {
@@ -223,6 +272,7 @@ impl NativeCallable for Sync {
}
cx.set_attr(object, "id", Value::Number((geometry.id as i64).into()))?;
cx.set_attr(object, "block", Value::Object(block))?;
created.push(object);
object
};
objects.insert(geometry.id, object);
@@ -245,6 +295,7 @@ impl NativeCallable for Sync {
};
let old = cx.store().payload::<Mount>(object, "Mount")?;
if !old.mounted
|| old.version != mount.version
|| old.geometry != mount.geometry
|| old.page != mount.page
|| old.parent != mount.parent
@@ -269,9 +320,13 @@ impl NativeCallable for Sync {
Value::Object(object),
)?;
}
for (id, weak) in &self.old {
if !objects.contains_key(id)
&& let Some(object) = cx.upgrade(*weak)?
let mut hooks = Vec::new();
// Layout records are preorder; reversing guarantees children unmount
// before their parents. A Page change creates fresh Mount contexts.
for id in self.old_order.iter().rev() {
let weak = self.old[id];
if let Some(object) = cx.upgrade(weak)?
&& objects.get(id) != Some(&object)
{
let mut mount = cx.store().payload::<Mount>(object, "Mount")?.clone();
mount.mounted = false;
@@ -280,8 +335,22 @@ impl NativeCallable for Sync {
mount.next_sibling = None;
cx.replace_payload(object, mount)?;
cx.set_attr(object, "mounted", Value::Bool(false))?;
hooks.push(lifecycle_call(cx, object, "onUnmount")?);
}
}
for object in created {
hooks.push(lifecycle_call(cx, object, "onMount")?);
}
let driver = if hooks.is_empty() {
None
} else {
Some(crate::vm_sequence::build(
cx,
hooks,
"<mount lifecycle>",
self.desktop,
)?)
};
let weak = objects
.into_iter()
.map(|(id, object)| Ok((id, cx.store().downgrade(object)?)))
@@ -291,7 +360,9 @@ impl NativeCallable for Sync {
.output
.lock()
.map_err(|_| VmError::InvalidArgument("Mount registry poisoned"))? = weak;
Ok(NativeResult::Return(Value::Nil))
Ok(NativeResult::Return(
driver.map(Value::Object).unwrap_or(Value::Nil),
))
}
}
@@ -303,7 +374,7 @@ impl MountTable {
page: Option<ObjectRef>,
nodes: Vec<WeakObjectRef>,
frame: &Frame,
) -> Result<(), VmError> {
) -> Result<Option<ObjectRef>, VmError> {
let output = Arc::new(Mutex::new(HashMap::new()));
let mut captures = vec![desktop];
captures.extend(page);
@@ -314,6 +385,7 @@ impl MountTable {
page,
nodes,
old: self.active.clone(),
old_order: self.order.clone(),
frame: frame.clone(),
output: output.clone(),
},
@@ -321,13 +393,21 @@ impl MountTable {
))?;
let result = vm.call(function, &[]);
vm.release(function)?;
vm.release_value(result?)?;
let result = result?;
self.active = std::mem::take(
&mut *output
.lock()
.map_err(|_| VmError::InvalidArgument("Mount registry poisoned"))?,
);
Ok(())
self.order = frame.mounts.iter().map(|mount| mount.id).collect();
match result {
Value::Object(hooks) => Ok(Some(hooks)),
Value::Nil => Ok(None),
value => {
vm.release_value(value)?;
Err(VmError::InvalidArgument("invalid lifecycle batch"))
}
}
}
}