diff --git a/src/README.md b/src/README.md index 53946a8..8d14812 100644 --- a/src/README.md +++ b/src/README.md @@ -167,3 +167,6 @@ Viewport refresh now advances through ReplSession's saved refresh state on worke Runtime mutation entry points reject direct calls while refresh work is pending, before executing source or changing metadata; task polling defers. The API worker drains the saved refresh before accepting those mutations, keeping the runtime usable. Incremental refresh keeps the prior complete document map until a stable pass, including across window traversal boundaries where no VM read-only frame is currently active. + + +Mount preparation now has an explicit checkpoint that roots the previous dictionary, Mount payloads and metadata values. It can restore geometry/relations/registry after cancelling an unpublished layout, or release its roots after completion. Task cancellation moves from speculative preparation to immediately before the actual onUnmount callback, so cancelling preparation does not destroy the old occurrence's tasks. diff --git a/src/mount.rs b/src/mount.rs index 0623ccc..889026c 100644 --- a/src/mount.rs +++ b/src/mount.rs @@ -302,13 +302,17 @@ struct LifecycleCall { function: ObjectRef, block: ObjectRef, mount: ObjectRef, + unmount: bool, } impl NativeCallable for LifecycleCall { fn arity(&self) -> Option { Some(endor_core::CallArity::exact(0)) } - fn invoke(&self, _: &mut NativeContext<'_>, _: &[Value]) -> Result { + fn invoke(&self, cx: &mut NativeContext<'_>, _: &[Value]) -> Result { + if self.unmount { + crate::tasks::cancel_owner(cx, self.mount)?; + } Ok(NativeResult::Invoke { function: self.function, arguments: vec![Value::Object(self.block), Value::Object(self.mount)], @@ -336,6 +340,7 @@ fn lifecycle_call( function, block, mount, + unmount: name == "onUnmount", }, vec![function, block, mount], )) @@ -491,7 +496,6 @@ impl NativeCallable for Sync { && objects.get(id) != Some(&object) { let mut mount = cx.store().payload::(object, "Mount")?.clone(); - crate::tasks::cancel_owner(cx, object)?; mount.mounted = false; mount.layout = None; mount.parent = None; @@ -530,7 +534,138 @@ impl NativeCallable for Sync { } } +/// Explicitly owned roots protect the old presentation while preparation +/// temporarily replaces its desktop dictionary and Mount payloads. +pub struct Checkpoint { + table: MountTable, + dictionary: Option, + states: Vec<(ObjectRef, Mount, Option, Option)>, + roots: Vec, +} + +impl Checkpoint { + pub fn release(self, vm: &mut Vm) -> Result<(), VmError> { + let mut result = Ok(()); + for root in self.roots.into_iter().rev() { + if let Err(error) = vm.release(root) { + result = Err(error); + } + } + result + } + + pub fn restore( + self, + vm: &mut Vm, + desktop: ObjectRef, + table: &mut MountTable, + ) -> Result<(), VmError> { + let result = (|| { + let old: std::collections::HashSet<_> = + self.states.iter().map(|state| state.0).collect(); + for weak in table.ordered() { + if let Some(object) = vm.upgrade(weak)? { + let result = (|| { + if !old.contains(&object) { + let mut mount = vm.payload::(object, "Mount")?.clone(); + mount.mounted = false; + mount.layout = None; + mount.parent = None; + mount.first_child = None; + mount.next_sibling = None; + vm.replace_payload(object, mount)?; + vm.set_attr(object, "mounted", Value::Bool(false))?; + } + Ok::<_, VmError>(()) + })(); + vm.release(object)?; + result?; + } + } + for (object, mount, mounted, version) in &self.states { + vm.replace_payload(*object, mount.clone())?; + for (name, value) in [("mounted", mounted), ("layoutVersion", version)] { + if let Some(value) = value { + vm.set_attr(*object, name, value.clone())?; + } else { + match vm.del_attr(*object, name) { + Ok(()) | Err(VmError::AttributeNotFound(_)) => {} + Err(error) => return Err(error), + } + } + } + } + if let Some(dictionary) = self.dictionary { + vm.set_attr(desktop, "mounts", Value::Object(dictionary))?; + } else { + match vm.del_attr(desktop, "mounts") { + Ok(()) | Err(VmError::AttributeNotFound(_)) => {} + Err(error) => return Err(error), + } + } + *table = self.table.clone(); + Ok(()) + })(); + let released = self.release(vm); + result?; + released + } +} + impl MountTable { + pub fn checkpoint(&self, vm: &mut Vm, desktop: ObjectRef) -> Result { + let dictionary = match vm.get_attr(desktop, "mounts") { + Ok(Value::Object(dictionary)) => Some(dictionary), + Err(VmError::AttributeNotFound(_)) => None, + Ok(_) => return Err(VmError::InvalidArgument("invalid mount dictionary")), + Err(error) => return Err(error), + }; + let mut roots = Vec::new(); + if let Some(dictionary) = dictionary { + vm.retain(dictionary)?; + roots.push(dictionary); + } + let mut checkpoint = Checkpoint { + table: self.clone(), + dictionary, + states: Vec::new(), + roots, + }; + let result = (|| { + for weak in self.ordered() { + if let Some(object) = vm.upgrade(weak)? { + checkpoint.roots.push(object); + let mount = vm.payload::(object, "Mount")?.clone(); + let mut attributes = Vec::new(); + for name in ["mounted", "layoutVersion"] { + let value = match vm.get_attr(object, name) { + Ok(value) => Some(value), + Err(VmError::AttributeNotFound(_)) => None, + Err(error) => return Err(error), + }; + if let Some(Value::Object(object)) = &value { + vm.retain(*object)?; + checkpoint.roots.push(*object); + } + attributes.push(value); + } + checkpoint.states.push(( + object, + mount, + attributes.remove(0), + attributes.remove(0), + )); + } + } + Ok::<_, VmError>(()) + })(); + if let Err(error) = result { + checkpoint.release(vm)?; + return Err(error); + } + Ok(checkpoint) + } + /// A faulted domain cannot run user onUnmount callbacks. Still detach every /// context and release its owning dictionary through the normal heap barrier. pub fn retire(&mut self, vm: &mut Vm, desktop: ObjectRef) -> Result<(), VmError> {