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
+263 -34
View File
@@ -86,6 +86,7 @@ pub fn compile(source: &str, namespace: &str, submission: u64) -> Result<RenderD
}
};
let document = RenderDocument {
windows: std::collections::BTreeMap::new(),
source: source.text().to_owned(),
root: Some(root),
frame: layout::frame(&ui_root)?,
@@ -207,6 +208,7 @@ pub struct ReplSession {
nodes: Vec<CodeNode>,
faulted: bool,
program_root: Option<CodeNode>,
documents: std::collections::BTreeMap<u64, RenderDocument>,
}
pub struct ReplResult {
pub value: String,
@@ -328,6 +330,7 @@ impl ReplSession {
nodes: Vec::new(),
faulted: false,
program_root: None,
documents: std::collections::BTreeMap::new(),
})
}
pub fn execute(&mut self, source: &str, namespace: &str) -> Result<ReplResult> {
@@ -467,50 +470,134 @@ impl ReplSession {
}
fn refresh_document(&mut self) -> Result<Option<RenderDocument>> {
let mut last = None;
let mut work = 0;
let mut changed = false;
let mut mutated_mounts = false;
let result = (|| loop {
let Some(root) = self.bridge.prepare_frame(&mut self.candidate.vm)? else {
return Ok(last);
};
mutated_mounts = true;
let frame = self
.bridge
.layout_frame(&mut self.candidate.vm, root.clone())?;
let mut document = RenderDocument {
source: self.source.clone(),
root: Some(self.program_root.clone().unwrap_or_else(|| CodeNode {
id: format!("console-session-{}", self.id),
name: format!("Console session {}", self.id),
kind: "repl-session".into(),
start_line: 1,
end_line: self.source.lines().count(),
children: self.nodes.clone(),
})),
frame,
ui_root: root.clone(),
};
document.frame.session = self.id;
document.validate()?;
work += document.frame.mounts.len().max(1);
if work > 200_000 {
bail!("layout dependencies did not stabilize within the rendering work budget");
let result = (|| {
loop {
let mut pass_changed = false;
for id in self.bridge.window_ids()? {
self.bridge.select_window(id)?;
while let Some(root) = self.bridge.prepare_frame(&mut self.candidate.vm)? {
mutated_mounts = true;
let frame = self
.bridge
.layout_frame(&mut self.candidate.vm, root.clone())?;
let mut document = RenderDocument {
windows: std::collections::BTreeMap::new(),
source: self.source.clone(),
root: Some(self.program_root.clone().unwrap_or_else(|| CodeNode {
id: format!("console-session-{}", self.id),
name: format!("Console session {}", self.id),
kind: "repl-session".into(),
start_line: 1,
end_line: self.source.lines().count(),
children: self.nodes.clone(),
})),
frame,
ui_root: root.clone(),
};
document.frame.session = self.id;
document.validate()?;
work += document.frame.mounts.len().max(1);
if work > 200_000 {
bail!(
"layout dependencies did not stabilize within the rendering work budget"
);
}
self.bridge.paint_frame(
&mut self.candidate.vm,
root.as_deref(),
&mut document.frame,
)?;
document.validate()?;
self.documents.insert(id, document);
pass_changed = true;
changed = true;
}
}
if !pass_changed {
break;
}
}
self.bridge.paint_frame(
&mut self.candidate.vm,
root.as_deref(),
&mut document.frame,
)?;
document.validate()?;
last = Some(document);
self.bridge.finish_closed(&mut self.candidate.vm)?;
let ids = self.bridge.window_ids()?;
self.documents.retain(|id, _| ids.contains(id));
if !changed {
return Ok(None);
}
let mut main = self
.documents
.get(&crate::window::MAIN_WINDOW)
.cloned()
.unwrap_or_else(|| RenderDocument {
source: self.source.clone(),
frame: crate::document::Frame {
session: self.id,
..crate::document::Frame::default()
},
..RenderDocument::default()
});
main.source = self.source.clone();
main.root = self
.documents
.values()
.find(|document| document.source == self.source)
.and_then(|document| document.root.clone())
.or(main.root);
main.windows = self
.documents
.iter()
.filter(|(id, _)| **id != crate::window::MAIN_WINDOW)
.map(|(id, document)| (*id, document.clone()))
.collect();
main.validate()?;
Ok(Some(main))
})();
self.bridge.select_window(crate::window::MAIN_WINDOW)?;
if result.is_err() && mutated_mounts {
self.faulted = true;
}
result
}
pub fn resize_windows(
&mut self,
viewports: &std::collections::BTreeMap<u64, crate::layout::Size>,
) -> Result<Option<RenderDocument>> {
if self.faulted {
bail!("runtime stopped after a callback or rendering failure");
}
let ids = self.bridge.window_ids()?;
if ids.len() == 1
&& let Some(viewport) = viewports.get(&crate::window::MAIN_WINDOW)
{
self.bridge.select_window(crate::window::MAIN_WINDOW)?;
return self.resize(Some(*viewport));
}
let mut changed = false;
for (id, viewport) in viewports {
if ids.contains(id) {
self.bridge.select_window(*id)?;
changed |= self
.bridge
.set_viewport(&mut self.candidate.vm, Some(*viewport))?;
}
}
self.bridge.select_window(crate::window::MAIN_WINDOW)?;
if !changed {
return Ok(None);
}
let result = self.refresh_document();
if result.is_err() {
self.take_window_commands()?;
self.bridge.discard_pending(&mut self.candidate.vm)?;
}
self.bridge
.reclaim(&mut self.candidate.vm, &mut self.candidate.roots)?;
result
}
pub fn take_window_commands(&self) -> Result<Vec<crate::window::Command>> {
self.bridge.take_window_commands(&self.candidate.vm)
}
@@ -535,6 +622,10 @@ impl ReplSession {
result
}
pub fn accepts_window(&self, id: u64) -> bool {
self.bridge.accepts_window(id)
}
pub fn is_faulted(&self) -> bool {
self.faulted
}
@@ -546,6 +637,7 @@ impl ReplSession {
if input.session != self.id {
bail!("event belongs to another runtime session");
}
self.bridge.select_window(input.window)?;
let result = (|| {
self.bridge.dispatch(&mut self.candidate.vm, input)?;
let document = self.refresh_document()?;
@@ -561,6 +653,7 @@ impl ReplSession {
input: self.inputs,
})
})();
self.bridge.select_window(crate::window::MAIN_WINDOW)?;
if result.is_err() {
self.take_window_commands()?;
self.faulted = true;
@@ -845,6 +938,142 @@ mod tests {
const EXAMPLE: &str = include_str!("../examples/hello.endor");
const PAGE: &str = include_str!("../examples/page.endor");
#[test]
fn native_windows_share_business_state_move_pages_and_release_closed_contexts() {
let (mut session, initial) = ReplSession::load(
include_str!("../examples/windows.endor"),
"app.main",
1,
130,
)
.unwrap();
assert_eq!(initial.windows.len(), 1);
let main_ids: std::collections::BTreeSet<_> =
initial.frame.mounts.iter().map(|mount| mount.id).collect();
assert!(
initial.windows[&2]
.frame
.mounts
.iter()
.all(|mount| !main_ids.contains(&mount.id))
);
assert_eq!(initial.windows[&2].frame.session, initial.frame.session);
let changed = session
.execute("count.increment()", "app.main")
.unwrap()
.document
.unwrap();
let has_one = |frame: &crate::document::Frame| {
frame
.commands
.iter()
.any(|command| matches!(command,RenderCommand::Text{value,..} if value=="1"))
};
assert!(has_one(&changed.frame) && has_one(&changed.windows[&2].frame));
session
.execute("oldRight = rightButton.mounts().at(0)", "app.main")
.unwrap();
let page = session.execute("right", "app.main").unwrap().value;
let moved = session
.execute("right.moveToWindow(ui.window())", "app.main")
.unwrap()
.document
.unwrap();
assert!(moved.windows[&2].frame.tabs.is_empty());
assert_eq!(session.execute("right", "app.main").unwrap().value, page);
assert_eq!(
session
.execute("right.window().id()", "app.main")
.unwrap()
.value,
"1"
);
assert!(session.execute("oldRight.bounds()", "app.main").is_err());
let globals = session.candidate.globals.unwrap();
let Value::Object(window) = session
.candidate
.vm
.dict_get(globals, &Value::Text("other".into()))
.unwrap()
.unwrap()
else {
panic!("Window");
};
let weak = session.candidate.vm.downgrade(window).unwrap();
let closed = session
.execute("other.close()", "app.main")
.unwrap()
.document
.unwrap();
assert!(closed.windows.is_empty());
assert!(session.execute("right.open(other)", "app.main").is_err());
session
.candidate
.vm
.dict_delete(globals, &Value::Text("other".into()))
.unwrap();
session.execute("nil", "app.main").unwrap();
session.candidate.vm.collect_cycles();
assert!(session.candidate.vm.upgrade(weak).unwrap().is_none());
// An externally retained old leaf Mount must not retain the window context.
let detached = session
.execute("detached = right.detachToWindow()", "app.main")
.unwrap()
.document
.unwrap();
assert!(detached.windows.contains_key(&3));
session.execute("detached.close()", "app.main").unwrap();
assert_eq!(
session.execute("right.window()", "app.main").unwrap().value,
"nil"
);
assert_eq!(
session.execute("count.value", "app.main").unwrap().value,
"1"
);
session
.execute("right.open(ui.window())", "app.main")
.unwrap();
assert_eq!(session.execute("right", "app.main").unwrap().value, page);
}
#[test]
fn failed_input_after_window_close_cannot_leave_owned_mounts_in_closed_context() {
let (mut session, _) = ReplSession::load(
include_str!("../examples/windows.endor"),
"app.main",
1,
131,
)
.unwrap();
assert!(
session
.execute("other.close()\nui.window().resize(0,600)", "app.main")
.is_err()
);
assert!(!session.accepts_window(2));
let repaired = session.execute("right.window()", "app.main").unwrap();
assert_eq!(repaired.value, "nil");
assert!(repaired.document.unwrap().windows.is_empty());
assert_eq!(
session
.execute("right.mounts().count", "app.main")
.unwrap()
.value,
"0"
);
session
.execute("right.open(ui.window())", "app.main")
.unwrap();
assert_eq!(
session
.execute("right.window().id()", "app.main")
.unwrap()
.value,
"1"
);
}
#[test]
fn tab_projection_uses_page_methods_and_rejects_old_presentations() {
let source = r#"first = Page()