Support shared-VM native windows and Page detachment
Verify Tessera GUI / verify (push) Failing after 7m35s

This commit is contained in:
2026-10-21 18:11:58 +08:00
parent f8173168fd
commit e3e46068eb
13 changed files with 993 additions and 145 deletions
+348 -57
View File
@@ -173,6 +173,10 @@ struct Session {
nodes: Vec<WeakObjectRef>,
presentations: std::collections::BTreeMap<u64, crate::presentation::Presentation>,
current: u64,
master: ObjectRef,
next_window: u64,
next_mount: u64,
retired_commands: Vec<crate::window::Command>,
}
impl std::ops::Deref for Session {
@@ -202,6 +206,10 @@ enum Operation {
Viewport,
Window,
PageWindow,
NewWindow,
CloseWindow,
Detach,
MoveWindow,
State,
StateSet,
Increment,
@@ -445,14 +453,116 @@ fn new_typed_block(
Ok(block)
}
fn show(cx: &mut NativeContext<'_>, page: ObjectRef, session: &Shared) -> Result<(), VmError> {
let mut state = session.lock().map_err(|_| invalid("UI session poisoned"))?;
let window = object(Some(&cx.store().get_attr(state.desktop, "window")?))?;
crate::window::register_page(cx, window, page)?;
cx.set_attr(state.desktop, "pending", Value::Object(page))?;
state.pending = Some(Some(page));
fn create_window(
cx: &mut NativeContext<'_>,
state: &mut Session,
definition: ObjectRef,
) -> Result<ObjectRef, VmError> {
if cx.store().is_read_only() {
return Err(invalid(
"window creation is forbidden during read-only rendering",
));
}
if state.presentations.len() >= 16 {
return Err(VmError::ExecutionLimit("16 window budget"));
}
let id = state
.next_window
.checked_add(1)
.filter(|id| *id <= i64::MAX as u64)
.ok_or(invalid("window identities exhausted"))?;
let (window, desktop, tabs) = crate::window::create(cx, state.master, id, definition)?;
let mut presentation = crate::presentation::Presentation::new(desktop, tabs);
presentation.force_frame = true;
state.presentations.insert(id, presentation);
state.next_window = id;
Ok(window)
}
fn page_window(
store: &endor_core::ObjectStore,
state: &Session,
page: ObjectRef,
) -> Result<Option<u64>, VmError> {
for (id, view) in &state.presentations {
let window = object(Some(&store.get_attr(view.desktop, "window")?))?;
if crate::window::pages(store, window)?.contains(&page) {
return Ok(Some(*id));
}
}
Ok(None)
}
fn remove_page(
cx: &mut NativeContext<'_>,
view: &mut crate::presentation::Presentation,
page: ObjectRef,
) -> Result<(), VmError> {
let window = object(Some(&cx.store().get_attr(view.desktop, "window")?))?;
crate::window::unregister_page(cx, window, page)?;
if view.pending.unwrap_or(view.active) == Some(page) {
let next = crate::window::pages(cx.store(), window)?.first().copied();
cx.set_attr(
view.desktop,
"pending",
next.map(Value::Object).unwrap_or(Value::Nil),
)?;
view.pending = Some(next);
}
Ok(())
}
fn show_in(
cx: &mut NativeContext<'_>,
page: ObjectRef,
session: &Shared,
target: Option<ObjectRef>,
) -> Result<(), VmError> {
let mut state = session.lock().map_err(|_| invalid("UI session poisoned"))?;
let old = page_window(cx.store(), &state, page)?;
let id = match target {
Some(window) => {
let data = cx
.store()
.payload::<crate::window::ManagedWindow>(window, "Window")?;
if data.closed {
return Err(invalid("native window is closed"));
}
let id = data.id;
if state
.presentations
.get(&id)
.is_none_or(|view| view.desktop != data.desktop)
{
return Err(invalid("window belongs to another runtime"));
}
id
}
None => old.unwrap_or(state.current),
};
if let Some(old) = old
&& old != id
{
remove_page(
cx,
state
.presentations
.get_mut(&old)
.ok_or(invalid("missing source window"))?,
page,
)?;
}
let view = state
.presentations
.get_mut(&id)
.ok_or(invalid("missing target window"))?;
let window = object(Some(&cx.store().get_attr(view.desktop, "window")?))?;
crate::window::register_page(cx, window, page)?;
cx.set_attr(view.desktop, "pending", Value::Object(page))?;
view.pending = Some(Some(page));
Ok(())
}
fn show(cx: &mut NativeContext<'_>, page: ObjectRef, session: &Shared) -> Result<(), VmError> {
show_in(cx, page, session, None)
}
fn present_value(
cx: &mut NativeContext<'_>,
@@ -506,12 +616,13 @@ impl NativeCallable for Function {
TextOf => (2, 4),
Text => (1, 3),
Rect => (2, 4),
OpenValue | Spacer | SetText | SetColor | Append | Remove | Render | Navigate
| OnClick | Present => (1, 1),
MoveWindow | OpenValue | Spacer | SetText | SetColor | Append | Remove | Render
| Navigate | OnClick | Present => (1, 1),
Row | Column => (2, MAX_UI_NODES + 2),
Button => (1, 1),
Replace | Move => (2, 2),
Page | Unmount | Close | Activate | Back | Forward | Children => (0, 0),
Page | Unmount | Close | Activate | Back | Forward | Children | NewWindow
| CloseWindow | Detach => (0, 0),
Open => (0, 1),
Click | Invalidate => (0, 1),
On => (2, 3),
@@ -594,6 +705,38 @@ impl NativeCallable for Function {
)?;
return Ok(NativeResult::Return(Value::Object(set)));
}
if matches!(self.operation, NewWindow) {
let mut state = self
.session
.lock()
.map_err(|_| invalid("UI session poisoned"))?;
let definition = self
.requested_type
.ok_or(invalid("Window constructor requires a type"))?;
let window = create_window(cx, &mut state, definition)?;
return Ok(NativeResult::Return(Value::Object(window)));
}
if matches!(self.operation, CloseWindow) {
let window = self
.owner
.ok_or(invalid("window close requires a receiver"))?;
let id = cx
.store()
.payload::<crate::window::ManagedWindow>(window, "Window")?
.id;
crate::window::close(cx, window)?;
let mut state = self
.session
.lock()
.map_err(|_| invalid("UI session poisoned"))?;
if let Some(view) = state.presentations.get_mut(&id) {
cx.set_attr(view.desktop, "pending", Value::Nil)?;
view.pending = Some(None);
view.force_frame = true;
view.closing = true;
}
return Ok(NativeResult::Return(Value::Nil));
}
if matches!(self.operation, Window) {
let desktop = self
.session
@@ -935,55 +1078,61 @@ impl NativeCallable for Function {
}];
show(cx, owner, &self.session)?;
}
Detach => {
let window = {
let mut state = self
.session
.lock()
.map_err(|_| invalid("UI session poisoned"))?;
let definition = desktop_type(cx.store(), "Window", state.master)?;
create_window(cx, &mut state, definition)?
};
show_in(cx, owner, &self.session, Some(window))?;
return Ok(NativeResult::Return(Value::Object(window)));
}
PageWindow => {
let desktop = self
let state = self
.session
.lock()
.map_err(|_| invalid("UI session poisoned"))?
.desktop;
let window = object(Some(&cx.store().get_attr(desktop, "window")?))?;
let value = if crate::window::pages(cx.store(), window)?.contains(&owner) {
Value::Object(window)
.map_err(|_| invalid("UI session poisoned"))?;
let value = if let Some(id) = page_window(cx.store(), &state, owner)? {
cx.store()
.get_attr(state.presentations[&id].desktop, "window")?
} else {
Value::Nil
};
return Ok(NativeResult::Return(value));
}
Open | Activate => {
if let Some(requested) = args.first()
&& !matches!(requested, Value::Nil)
Open | Activate | MoveWindow => {
if matches!(self.operation, MoveWindow)
&& matches!(args.first(), None | Some(Value::Nil))
{
let desktop = self
.session
.lock()
.map_err(|_| invalid("UI session poisoned"))?
.desktop;
let window = cx.store().get_attr(desktop, "window")?;
if requested != &window {
return Err(invalid("Page.open requires this runtime's native Window"));
}
return Err(invalid("Page.moveToWindow requires a Window"));
}
show(cx, owner, &self.session)?;
let target = match args.first() {
None | Some(Value::Nil) => None,
Some(value) => Some(object(Some(value))?),
};
show_in(cx, owner, &self.session, target)?;
return Ok(NativeResult::Return(Value::Object(owner)));
}
Unmount => {
data.children.clear();
}
Close => {
let mut session = self
let mut state = self
.session
.lock()
.map_err(|_| invalid("UI session poisoned"))?;
let window = object(Some(&cx.store().get_attr(session.desktop, "window")?))?;
crate::window::unregister_page(cx, window, owner)?;
if session.pending.unwrap_or(session.active) == Some(owner) {
let next = crate::window::pages(cx.store(), window)?.first().copied();
cx.set_attr(
session.desktop,
"pending",
next.map(Value::Object).unwrap_or(Value::Nil),
if let Some(id) = page_window(cx.store(), &state, owner)? {
remove_page(
cx,
state
.presentations
.get_mut(&id)
.ok_or(invalid("missing window"))?,
owner,
)?;
session.pending = Some(next);
}
return Ok(NativeResult::Return(Value::Nil));
}
@@ -1103,16 +1252,84 @@ fn project_tabs(
}
impl UiBridge {
pub fn take_window_commands(&self, vm: &Vm) -> Result<Vec<crate::window::Command>> {
let desktop = self
pub fn window_ids(&self) -> Result<Vec<u64>> {
let state = self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?
.desktop;
let Value::Object(window) = vm.get_attr(desktop, "window")? else {
bail!("native Window is unavailable");
};
Ok(crate::window::take_commands(vm, window)?)
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
let mut ids: Vec<_> = state.presentations.keys().copied().collect();
ids.sort_by_key(|id| {
let view = &state.presentations[id];
(
if view.active.is_some()
&& view.pending.is_some()
&& view.pending.flatten() != view.active
{
0
} else {
1
},
*id,
)
});
Ok(ids)
}
pub fn accepts_window(&self, id: u64) -> bool {
self.session.lock().is_ok_and(|state| {
state
.presentations
.get(&id)
.is_some_and(|view| !view.closing)
})
}
pub fn select_window(&self, id: u64) -> Result<()> {
let mut state = self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
if !state.presentations.contains_key(&id) {
bail!("window is unavailable");
}
state.current = id;
Ok(())
}
pub fn finish_closed(&self, vm: &mut Vm) -> Result<()> {
let mut state = self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
let ids: Vec<_> = state
.presentations
.iter()
.filter(|(id, view)| **id != crate::window::MAIN_WINDOW && view.closing)
.map(|(id, _)| *id)
.collect();
let windows = object(Some(&vm.get_attr(state.master, "windows")?))?;
for id in ids {
let desktop = state.presentations[&id].desktop;
let window = object(Some(&vm.get_attr(desktop, "window")?))?;
state
.retired_commands
.extend(crate::window::take_commands(vm, window)?);
vm.dict_delete(windows, &Value::Text(id.to_string()))?;
state.presentations.remove(&id);
}
state.current = crate::window::MAIN_WINDOW;
Ok(())
}
pub fn take_window_commands(&self, vm: &Vm) -> Result<Vec<crate::window::Command>> {
let mut state = self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
let mut commands = std::mem::take(&mut state.retired_commands);
for view in state.presentations.values() {
let window = object(Some(&vm.get_attr(view.desktop, "window")?))?;
commands.extend(crate::window::take_commands(vm, window)?);
}
commands.sort_by_key(|command| (command.window, command.sequence));
Ok(commands)
}
pub fn set_viewport(&self, vm: &mut Vm, viewport: Option<crate::layout::Size>) -> Result<bool> {
@@ -1163,9 +1380,9 @@ impl UiBridge {
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?
.pending
.flatten()
.is_some())
.presentations
.values()
.any(|view| view.pending.flatten().is_some() || view.active.is_some()))
}
pub fn open_value(&self, vm: &mut Vm, value: &Value) -> Result<()> {
let function = object(Some(&vm.get_attr(self.ui, "open")?))?;
@@ -1195,10 +1412,10 @@ impl UiBridge {
.find(|page| vm.object_store().downgrade(*page).ok() == Some(weak))
.ok_or_else(|| anyhow::anyhow!("Page is no longer open"))?
};
let method = if input.kind == "tab-close" {
"close"
} else {
"activate"
let method = match input.kind.as_str() {
"tab-close" => "close",
"tab-detach" => "detachToWindow",
_ => "activate",
};
let bound = vm.bind_attr(page, method)?;
let result = match &bound {
@@ -1230,7 +1447,33 @@ impl UiBridge {
{
bail!("event belongs to another window");
}
if matches!(input.kind.as_str(), "tab-activate" | "tab-close") {
if input.kind == "window-close" {
let desktop = self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?
.desktop;
let window = object(Some(&vm.get_attr(desktop, "window")?))?;
let bound = vm.bind_attr(window, "close")?;
let result = match &bound {
Value::Object(function) => vm.call_with_limits(
*function,
&[],
endor_core::ExecutionLimits {
max_frames: 128,
max_instructions: 1_000_000,
},
),
_ => Err(invalid("Window.close is not callable")),
};
vm.release_value(bound)?;
vm.release_value(result?)?;
return Ok(());
}
if matches!(
input.kind.as_str(),
"tab-activate" | "tab-close" | "tab-detach"
) {
return self.dispatch_tab(vm, input);
}
let (weak, desktop, candidates, viewport) = {
@@ -1332,8 +1575,33 @@ impl UiBridge {
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
for presentation in state.presentations.values_mut() {
presentation.pending = None;
vm.set_attr(presentation.desktop, "pending", Value::Nil)?;
let window = object(Some(&vm.get_attr(presentation.desktop, "window")?))?;
let pages = crate::window::pages(vm.object_store(), window)?;
let next = if presentation.closing {
None
} else {
presentation
.active
.filter(|page| pages.contains(page))
.or_else(|| pages.first().copied())
};
// Earlier successful statements in a failed REPL input remain live.
// Repair ownership changes before the next publication so a closed or
// moved Page cannot survive in an old context's mount dictionary.
presentation.pending = if next != presentation.active || presentation.closing {
Some(next)
} else {
None
};
vm.set_attr(
presentation.desktop,
"pending",
presentation
.pending
.flatten()
.map(Value::Object)
.unwrap_or(Value::Nil),
)?;
if presentation
.active
.is_some_and(|page| vm.object(page).is_err())
@@ -1530,7 +1798,22 @@ impl UiBridge {
if let Some(size) = &mut layout.viewport {
size.height = (size.height - crate::tabs::height(&tabs)).max(0.0);
}
{
let state = self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
layout.reserve_identity_floor(state.next_mount);
}
let mut frame = layout.structure(&root)?;
{
let mut state = self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
state.next_mount = state.next_mount.max(layout.identity_high_watermark());
frame.window = state.current;
}
let (hooks, mounts) = {
let mut state = self
.session
@@ -1632,6 +1915,10 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
crate::presentation::Presentation::new(desktop, tabs),
)]),
current: crate::window::MAIN_WINDOW,
master: desktop,
next_window: crate::window::MAIN_WINDOW,
next_mount: 0,
retired_commands: Vec::new(),
}));
for (name, operation) in [
("isInstance", Operation::IsInstance),
@@ -1683,7 +1970,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
("Column", "ContainerBlock", Some("column")),
("Button", "ContainerBlock", Some("button")),
("Page", "ContainerBlock", Some("page")),
("Window", "Object", None),
("Window", "Object", Some("window-create")),
("Mount", "Object", None),
("Event", "Object", Some("event")),
("Subscription", "Object", None),
@@ -1722,6 +2009,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
"column" => Operation::Column,
"button" => Operation::Button,
"page" => Operation::Page,
"window-create" => Operation::NewWindow,
_ => return Err(invalid("unknown native type allocator").into()),
};
let factory = vm.allocate(ContextNativeFunction::new(
@@ -1828,6 +2116,9 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
("ContainerBlock", "replace", Operation::Replace),
("ContainerBlock", "move", Operation::Move),
("ContainerBlock", "children", Operation::Children),
("Window", "close", Operation::CloseWindow),
("Page", "moveToWindow", Operation::MoveWindow),
("Page", "detachToWindow", Operation::Detach),
("Page", "render", Operation::Render),
("Page", "unmount", Operation::Unmount),
("Page", "open", Operation::Open),