feat: render top-level Endor scripts through embedded Aster
Verify Tessera GUI / verify (push) Failing after 6m52s

This commit is contained in:
2026-10-20 13:47:29 +08:00
parent 8cb2bf8607
commit 47ab2aa3a6
14 changed files with 695 additions and 89 deletions
+11 -5
View File
@@ -4,8 +4,9 @@
| --- | --- |
| main.rs | Native window event loop, namespace state and API startup |
| api.rs | Code CRUD, eight-entry execution queue, worker, submission ordering and diagnostics |
| endor_host.rs | Aster Reader/Parser/Compiler, fresh candidate VM, limited initialization/main execution, root cleanup and AST developer tree |
| ui_bridge.rs | Tessera UI constructors and payloads, bounded preallocated node pool and native snapshot extraction |
| endor_host.rs | Aster compilation, fresh candidate VM, bounded script/module execution, root cleanup and AST developer tree |
| endor_script.rs | Batch complete inputs for Aster's Interactive parser/compiler, retain Module entry, and rebase Aster spans to the original snapshot |
| ui_bridge.rs | Tessera UI/page payloads and bound render methods, preallocated node/page pools and native snapshot extraction |
| layout.rs | Native UI tree to Frame; expanded-tree and coordinate budgets |
| document.rs | Published source/AST/UI snapshot, revisions, invalidation, frame validation and display commands |
| app.rs | Window events, redraw and developer-tools input |
@@ -21,14 +22,19 @@ compilation, bytecode and VM execution come from its libraries. Tessera implemen
only host UI behavior and layout. No CLI, HTTP server or Docker process is needed
to execute Endor in the application.
An execution candidate owns all external VM roots, including the 4096-node pool.
An execution candidate owns all external VM roots, including the 4096-node pool
and 64 pages with bound render callbacks. Script units share one globals dictionary
inside the candidate; no variables or VM handles persist across successful PUTs.
Callbacks return borrowed, preallocated real VM objects. Their mutable slots hold
immutable native UI descriptions; child edges contain owned native Arcs, never
managed VM references. Once extraction finishes, release the return value, clear
managed VM references. Page rendering stores native root data in the build session;
the final render selects the snapshot after all script units succeed. Once
extraction finishes, release temporary results, clear
globals, release roots, collect cycles and require an empty VM. The window keeps
only source, developer metadata, frame commands and native UI data.
Tests in api.rs execute real HTTP CRUD and a controlled queued-PUT/DELETE race
without opening a window. Tests in endor_host.rs cover lifecycle, normal Endor calls, malformed input,
without opening a window. Tests in endor_host.rs cover lifecycle, direct script and
module entry, page methods, multiline inputs, original source ranges, malformed input,
entry checks, budgets, failure preservation and stale-result invalidation. They
run only in remote CI. See the [project guide](../README.md) for public behavior.
+17 -10
View File
@@ -299,9 +299,10 @@ fn agent_guide(namespace: &NamespaceState) -> String {
Current namespace: `{name}`
Current revision: `{revision}`
Submit ordinary Endor functions. Tessera embeds Aster's frontend, compiler and VM.
The namespace comes from the request path. Declare a zero-argument `main` returning
a UI node. Type annotations are currently syntax only in Aster.
Submit an ordinary Endor script. Tessera embeds Aster's frontend, compiler and VM.
The namespace comes from the request path. Top-level bindings share the candidate
VM's globals; `ui.page().render(root)` selects the visible UI. No main function is
required. Typed helper functions and the older main-returning-View form also work.
## Code CRUD
@@ -322,11 +323,17 @@ Positional arguments only:
- `ui.rect(width, height, radius = 0, color = "#DCE8FF")`.
- `ui.column(padding, gap, child...)` and `ui.row(padding, gap, child...)`.
- `ui.spacer(size)`.
- `ui.page()` creates a page; `page.render(root)` sets its root. The last render
in a successful submission selects the page shown in this namespace.
Defaults above describe the host API; do not write keyword arguments in Endor.
Colors use #RRGGBB or #RRGGBBAA. Dimensions are finite numbers in 0..1000000.
Limits: 1 MiB source, 4096 constructed nodes, 64 UI levels, 10000 expanded layout
nodes, 16 KiB per text, 128 VM frames and 1000000 instructions per execution.
Use newlines between complete top-level statements. Multiline calls, function
bodies and expression continuations are passed to Aster as complete inputs.
Limits: 1 MiB source, 4096 constructed nodes, 64 pages, 64 UI levels, 10000 expanded
layout nodes, 16 KiB per text, 128 VM frames. Script statements each receive an
equal share of a total 1000000-instruction allowance. Older module initialization
and main execution each retain their own 1000000-instruction allowance.
PUT waits for its worker result. Errors return 422 with diagnostics and preserve
the last successful revision. A newer accepted PUT or DELETE supersedes older
@@ -339,7 +346,7 @@ future work. It is not an untrusted-code sandbox.
name = namespace.name,
revision = namespace.revision,
address = API_ADDRESS,
example = include_str!("../examples/hello.endor"),
example = include_str!("../examples/page.endor"),
)
}
@@ -466,10 +473,10 @@ mod tests {
api: Some(api),
};
let route = "/v1/namespaces/app.main/code";
let example = include_str!("../examples/hello.endor");
let example = include_str!("../examples/page.endor");
let (status, _, guide) = client.request("GET", "/agent", "");
assert_eq!(status, 200);
assert!(guide.contains("function main(): View"));
assert!(guide.contains("let myui = ui.page()"));
let (status, _, body) = client.request("PUT", route, example);
assert_eq!(status, 200, "{body}");
assert_eq!(
@@ -540,7 +547,7 @@ mod tests {
let release = Release(release_tx);
let client = &client;
let route = "/v1/namespaces/app.main/code";
let example = include_str!("../examples/hello.endor");
let example = include_str!("../examples/page.endor");
let first = scope.spawn(move || client.request("PUT", route, example));
entered_rx.recv_timeout(Duration::from_secs(10)).unwrap();
let pending = scope.spawn(move || client.request("PUT", route, example));
@@ -573,7 +580,7 @@ mod tests {
fn guide_contains_only_code_crud_routes() {
let guide = agent_guide(&NamespaceState::default());
assert!(guide.contains("Code CRUD"));
assert!(guide.contains("function main(): View"));
assert!(guide.contains("let myui = ui.page()"));
assert!(!guide.contains("export scene"));
assert!(guide.contains("PUT http://127.0.0.1:4737"));
assert!(!guide.contains("display_list"));
+296 -44
View File
@@ -2,12 +2,14 @@
use anyhow::{Result, bail};
use endor_core::{ExecutionLimits, ObjectRef, Value, Vm, VmError};
use frontend::{
EndorReader, ParseMode, SourceUnit, Span,
EndorReader, SourceUnit, Span,
ast::{AstRoot, Decl, DeclKind, Expr, ExprKind, Spanned, Stmt, StmtKind, visitor::Visitor},
};
use std::collections::BTreeSet;
use crate::{
document::{CodeNode, RenderDocument},
endor_script::{self, SourcePlan},
layout, ui_bridge,
};
@@ -24,47 +26,64 @@ pub fn compile(source: &str, namespace: &str, submission: u64) -> Result<RenderD
bail!("source exceeds the 1 MiB limit");
}
let source = EndorReader::from_text(format!("{namespace} [submission {submission}]"), source);
let ast = frontend::parse_source_ast(&source, ParseMode::Module)?;
AstBudget::default().visit_ast_root(&ast)?;
let compiled = compiler::compile_ast(
&ast,
&source,
&compiler::CompileOptions {
globals: vec!["ui".into()],
},
)
.map_err(|error| source.annotate(error))?;
let root = architecture(&source, &ast, namespace);
let plan = endor_script::parse(&source)?;
let mut budget = AstBudget::default();
for ast in plan.roots() {
budget.visit_ast_root(ast)?;
}
let compiled = compile_plan(&source, &plan)?;
let root = architecture(&source, &plan, namespace, submission);
let mut candidate = Candidate::new()?;
let result: Result<RenderDocument> = (|| {
let globals = candidate.vm.new_dict()?;
candidate.globals = Some(globals);
candidate.roots.push(globals);
let ui = ui_bridge::install(&mut candidate.vm, &mut candidate.roots)?;
let bridge = ui_bridge::install(&mut candidate.vm, &mut candidate.roots)?;
candidate
.vm
.dict_set(globals, Value::Text("ui".into()), Value::Object(ui))?;
let code = compiled.load(&mut candidate.vm)?;
candidate.roots.push(code);
let initialized = candidate
.vm
.execute_with_limits(code, globals, LIMITS)
.map_err(|error| runtime_error(&candidate.vm, error))?;
candidate.vm.release_value(initialized)?;
let entry = candidate
.vm
.dict_get(globals, &Value::Text("main".into()))?
.ok_or_else(|| anyhow::anyhow!("source must declare function main(): View"))?;
let Value::Object(entry) = entry else {
bail!("main must be a callable function");
.dict_set(globals, Value::Text("ui".into()), Value::Object(bridge.ui))?;
let limits = match &plan {
SourcePlan::Module(_) => LIMITS,
SourcePlan::Script(_) => ExecutionLimits {
max_instructions: LIMITS.max_instructions / compiled.len().max(1),
..LIMITS
},
};
for template in &compiled {
let code = template.load(&mut candidate.vm)?;
candidate.roots.push(code);
let value = candidate
.vm
.execute_with_limits(code, globals, limits)
.map_err(|error| runtime_error(&candidate.vm, error))?;
candidate.vm.release_value(value)?;
}
let ui_root = match &plan {
SourcePlan::Script(_) => bridge
.rendered()?
.ok_or_else(|| anyhow::anyhow!("script must call ui.page().render(root)"))?,
SourcePlan::Module(_) => {
let entry = candidate
.vm
.dict_get(globals, &Value::Text("main".into()))?
.ok_or_else(|| {
anyhow::anyhow!("declare main or render a page from a top-level script")
})?;
let Value::Object(entry) = entry else {
bail!("main must be a callable function");
};
let value = candidate
.vm
.call_with_limits(entry, &[], LIMITS)
.map_err(|error| runtime_error(&candidate.vm, error))?;
let snapshot = bridge.rendered().and_then(|rendered| match rendered {
Some(root) => Ok(root),
None => ui_bridge::snapshot(&candidate.vm, &value),
});
candidate.vm.release_value(value)?;
snapshot?
}
};
let returned = candidate
.vm
.call_with_limits(entry, &[], LIMITS)
.map_err(|error| runtime_error(&candidate.vm, error))?;
let snapshot = ui_bridge::snapshot(&candidate.vm, &returned);
candidate.vm.release_value(returned)?;
let ui_root = snapshot?;
let document = RenderDocument {
source: source.text().to_owned(),
root: Some(root),
@@ -82,6 +101,41 @@ pub fn compile(source: &str, namespace: &str, submission: u64) -> Result<RenderD
}
}
fn compile_plan(source: &SourceUnit, plan: &SourcePlan) -> Result<Vec<compiler::CompiledCode>> {
let mut globals = BTreeSet::from(["ui".to_owned()]);
// Match Aster module behavior for function forward references. Bindings are
// added in source order; every executable statement still uses Aster's own
// single-input Interactive compiler and the candidate's shared globals.
for root in plan.roots() {
if let AstRoot::Interactive(input) = root {
for statement in &input.statements {
if let StmtKind::Declaration(decl) = &statement.kind {
let DeclKind::Function(function) = &decl.kind;
globals.insert(function.name.text.clone());
}
}
}
}
let mut compiled = Vec::with_capacity(plan.roots().len());
for root in plan.roots() {
let options = compiler::CompileOptions {
globals: globals.iter().cloned().collect(),
};
compiled.push(
compiler::compile_ast(root, source, &options)
.map_err(|error| source.annotate(error))?,
);
if let AstRoot::Interactive(input) = root {
for statement in &input.statements {
if let StmtKind::Binding(binding) = &statement.kind {
globals.insert(binding.name.text.clone());
}
}
}
}
Ok(compiled)
}
struct Candidate {
vm: Vm,
roots: Vec<ObjectRef>,
@@ -160,20 +214,43 @@ impl Visitor for AstBudget {
}
}
fn architecture(source: &SourceUnit, ast: &AstRoot, namespace: &str) -> CodeNode {
fn architecture(
source: &SourceUnit,
plan: &SourcePlan,
namespace: &str,
submission: u64,
) -> CodeNode {
let mut tree = Architecture {
line_starts: line_starts(source.text()),
next_id: 0,
identity: format!("{namespace}@{submission}"),
};
let AstRoot::Module(program) = ast else {
unreachable!("module parse mode");
let mut children = Vec::new();
for root in plan.roots() {
match root {
AstRoot::Module(program) => children.extend(
program
.declarations
.iter()
.map(|decl| tree.declaration(decl)),
),
AstRoot::Interactive(input) => {
children.extend(input.statements.iter().map(|stmt| tree.statement(stmt)))
}
AstRoot::Expression(expression) => children.push(tree.expression(expression)),
}
}
let roots = plan.roots();
let span = Span::new(
roots.first().map_or(0, |root| root.span().start),
roots.last().map_or(0, |root| root.span().end),
);
let kind = if matches!(plan, SourcePlan::Module(_)) {
"module"
} else {
"script"
};
let children = program
.declarations
.iter()
.map(|decl| tree.declaration(decl))
.collect();
tree.node(namespace.to_owned(), "module", ast.span(), children)
tree.node(namespace.to_owned(), kind, span, children)
}
fn line_starts(source: &str) -> Vec<usize> {
@@ -190,6 +267,7 @@ fn line_starts(source: &str) -> Vec<usize> {
struct Architecture {
line_starts: Vec<usize>,
next_id: usize,
identity: String,
}
impl Architecture {
@@ -198,7 +276,7 @@ impl Architecture {
self.next_id += 1;
let line = |offset| self.line_starts.partition_point(|start| *start <= offset);
CodeNode {
id: format!("ast-{id}"),
id: format!("{}:ast-{id}", self.identity),
name,
kind: kind.into(),
start_line: line(span.start),
@@ -280,6 +358,180 @@ mod tests {
use crate::document::{NamespaceState, RenderCommand};
const EXAMPLE: &str = include_str!("../examples/hello.endor");
const PAGE: &str = include_str!("../examples/page.endor");
#[test]
fn top_level_goal_script_renders_without_a_main_or_synthetic_wrapper() {
let document = compile(PAGE, "app.main", 9).unwrap();
assert_eq!(document.source, PAGE);
let tree = document.root.as_ref().unwrap();
assert_eq!(tree.kind, "script");
assert_eq!(tree.children.len(), 4);
assert_eq!(tree.children[0].name, "heading");
assert_eq!(tree.children[1].name, "rectangle");
assert_eq!(tree.children[2].name, "myui");
assert_eq!(tree.children[2].start_line, 3);
assert_eq!(tree.children[3].start_line, 4);
assert!(tree.id.contains("app.main@9"));
assert_eq!(document.frame.commands.len(), 2);
assert!(
matches!(&document.frame.commands[0], RenderCommand::Text { value, size: 2, x: 24.0, y: 24.0, .. } if value == "Hello Endor")
);
assert!(
matches!(&document.frame.commands[1], RenderCommand::RoundedRect { x: 24.0, y: 52.0, width: 240.0, height: 80.0, radius: 12.0, color } if color == "#DCE8FF")
);
assert_eq!(document.display_list(2.0).unwrap().commands().len(), 3);
let wrapped = compile(include_str!("../examples/main.endor"), "app.main", 10).unwrap();
assert_eq!(
serde_json::to_value(&document.frame).unwrap(),
serde_json::to_value(&wrapped.frame).unwrap()
);
}
#[test]
fn script_helpers_multiline_bindings_calls_and_member_continuations_use_aster() {
let source = r##"let color = "#242422" // comment with ( and )
function greeting(): Text {
return "Hello Endor"
}
function heading(value: Text): View {
return ui.text(value, 2, color)
}
let title =
heading(greeting())
let rectangle = ui.rect(
240, 80, 12,
"#DCE8FF"
)
let page = ui
.page()
page
.render(ui.column(24, 12, title, rectangle))
"##;
let document = compile(source, "app.main", 2).unwrap();
assert_eq!(document.frame.commands.len(), 2);
let tree = document.root.unwrap();
assert_eq!(tree.children[1].name, "greeting");
assert_eq!(tree.children[1].start_line, 2);
assert_eq!(tree.children[2].name, "heading");
assert_eq!(tree.children[2].start_line, 5);
assert_eq!(tree.children[3].start_line, 8);
assert_eq!(tree.children[3].end_line, 9);
assert_eq!(tree.children.last().unwrap().end_line, 17);
}
#[test]
fn script_diagnostics_refer_to_original_unicode_source_and_byte_ranges() {
for ending in ["\n", "\r\n", "\r"] {
let source = format!(
"let title = ui.text(\"中文\"){ending}// comment{ending}let broken = missing{ending}ui.page().render(title)"
);
let error = compile(&source, "app.main", 27).unwrap_err();
let diagnostic = error.downcast_ref::<frontend::SourceDiagnostic>().unwrap();
assert_eq!(diagnostic.location.line, 3);
assert_eq!(diagnostic.location.column, 14);
assert_eq!(diagnostic.diagnostic.span.slice(&source), Some("missing"));
assert!(diagnostic.origin.to_string().contains("submission 27"));
}
let error = compile("let page = ui.page()\npage.render(,)", "app.main", 1).unwrap_err();
let diagnostic = error.downcast_ref::<frontend::SourceDiagnostic>().unwrap();
assert_eq!(diagnostic.location.line, 2);
}
#[test]
fn pages_bind_render_methods_and_last_render_selects_the_snapshot() {
let source = r#"let first = ui.page()
let second = ui.page()
first.render(ui.text("first"))
second.render(ui.text("second"))
let paint = first.render
paint(ui.text("last"))"#;
let document = compile(source, "app.main", 1).unwrap();
assert_eq!(document.frame.commands.len(), 1);
assert!(
matches!(&document.frame.commands[0], RenderCommand::Text { value, .. } if value == "last")
);
}
#[test]
fn page_api_errors_and_late_failures_do_not_publish_partial_snapshots() {
let mut state = NamespaceState::default();
state.submission = 1;
state
.commit(1, compile(PAGE, "app.main", 1).unwrap())
.unwrap();
for source in [
"ui.page()",
"let title = ui.text(\"no render\")",
"let page = ui.page(1)\npage.render(ui.text(\"x\"))",
"let page = ui.page()\npage.render()",
"let page = ui.page()\npage.render(\"not a node\")",
"let page = ui.page()\npage.render(ui.text(\"partial\"))\nui.missing()",
] {
state.submission = state.next_submission().unwrap();
assert!(
compile(source, "app.main", state.submission).is_err(),
"{source}"
);
assert_eq!(state.document.source, PAGE);
assert_eq!(state.revision, 1);
}
}
#[test]
fn page_creation_has_a_separate_bounded_pool() {
let mut source = String::new();
for index in 0..=ui_bridge::MAX_PAGES {
source.push_str(&format!("let page{index} = ui.page()\n"));
}
source.push_str("page0.render(ui.text(\"x\"))");
let error = compile(&source, "app.main", 1).unwrap_err();
assert!(error.to_string().contains("64 page budget"), "{error:#}");
}
#[test]
fn script_execution_is_limited_even_without_creating_ui_nodes() {
let mut source = "function text0(): Text { return \"Hello Endor\" }\n".to_owned();
for index in 1..=16 {
source.push_str(&format!(
"function text{index}(): Text {{ let unused = text{}() return text{}() }}\n",
index - 1,
index - 1
));
}
source.push_str("let title = ui.text(text16())\nui.page().render(title)");
let error = compile(&source, "app.main", 1).unwrap_err();
assert!(
error
.to_string()
.contains("execution limit reached: instructions"),
"{error:#}"
);
}
#[test]
fn literal_string_return_from_aster_can_be_displayed_by_the_host() {
let source = "function main(): Text { return \"Hello Endor\" }\nlet title = ui.text(main())\nui.page().render(title)";
let document = compile(source, "app.main", 1).unwrap();
assert!(
matches!(&document.frame.commands[0], RenderCommand::Text { value, .. } if value == "Hello Endor")
);
}
#[test]
fn developer_selection_resets_to_the_new_source_snapshot() {
let first = compile(PAGE, "app.main", 1).unwrap();
let second = compile(PAGE, "app.main", 2).unwrap();
let mut tools = crate::developer_tools::DeveloperToolsState {
open: true,
selected_node_id: Some(first.root.as_ref().unwrap().children[0].id.clone()),
};
tools.document_updated(&second);
assert_eq!(
tools.selected_node_id.as_deref(),
Some(second.root.as_ref().unwrap().id.as_str())
);
}
#[test]
fn executes_aster_functions_and_renders_after_vm_cleanup() {
+150
View File
@@ -0,0 +1,150 @@
//! Feed a source snapshot to Aster's existing module or interactive entry points.
//! Statement syntax, tokens and ASTs all remain Aster-owned. The host only batches
//! complete interactive inputs, just as a REPL feeds consecutive inputs to a VM.
use anyhow::Result;
use frontend::{
ParseMode, SourceUnit, Span, TokenKind,
ast::{AstRoot, Decl, DeclKind, Expr, ExprKind, InteractiveInput, Stmt, StmtKind},
};
pub enum SourcePlan {
Module(AstRoot),
Script(Vec<AstRoot>),
}
impl SourcePlan {
pub fn roots(&self) -> &[AstRoot] {
match self {
Self::Module(root) => std::slice::from_ref(root),
Self::Script(roots) => roots,
}
}
}
pub fn parse(source: &SourceUnit) -> Result<SourcePlan> {
if let Ok(module) = frontend::parse_source_ast(source, ParseMode::Module) {
return Ok(SourcePlan::Module(module));
}
let tokens = frontend::lex_source(source)?;
let mut roots = Vec::new();
let mut start = 0;
let mut depth = 0_i64;
for (index, token) in tokens.iter().enumerate() {
match token.kind {
TokenKind::LeftParen | TokenKind::LeftBrace => depth += 1,
TokenKind::RightParen | TokenKind::RightBrace => depth -= 1,
_ => {}
}
let end = token.span.end;
let boundary = tokens.get(index + 1).is_none_or(|next| {
depth == 0
&& source.text()[end..next.span.start].contains(['\r', '\n'])
&& !matches!(
next.kind,
TokenKind::Dot
| TokenKind::LeftParen
| TokenKind::Plus
| TokenKind::Minus
| TokenKind::Star
| TokenKind::Slash
)
});
if !boundary {
continue;
}
match frontend::parse_ast(&source.text()[start..end], ParseMode::Interactive) {
Ok(mut root) => {
let AstRoot::Interactive(input) = &mut root else {
unreachable!("interactive mode");
};
shift_input(input, start);
roots.push(root);
start = end;
}
// An incomplete interactive input can continue on the next line.
// Only Aster decides whether its combined syntax is valid.
Err(diagnostic) if index + 1 < tokens.len() && diagnostic.span.start == end - start => {
}
Err(mut diagnostic) => {
shift_span(&mut diagnostic.span, start);
return Err(source.annotate(diagnostic).into());
}
}
}
if roots.is_empty() {
// Produce Aster's original empty-input diagnostic and provenance.
frontend::parse_source_ast(source, ParseMode::Interactive)?;
}
Ok(SourcePlan::Script(roots))
}
fn shift_span(span: &mut Span, offset: usize) {
span.start += offset;
span.end += offset;
}
fn shift_input(input: &mut InteractiveInput, offset: usize) {
shift_span(&mut input.span, offset);
for statement in &mut input.statements {
shift_statement(statement, offset);
}
}
fn shift_statement(statement: &mut Stmt, offset: usize) {
shift_span(&mut statement.span, offset);
match &mut statement.kind {
StmtKind::Binding(binding) => {
shift_span(&mut binding.span, offset);
shift_span(&mut binding.name.span, offset);
shift_expression(&mut binding.value, offset);
}
StmtKind::Return(returned) => {
shift_span(&mut returned.span, offset);
if let Some(expression) = &mut returned.value {
shift_expression(expression, offset);
}
}
StmtKind::Expression(expression) => shift_expression(expression, offset),
StmtKind::Declaration(declaration) => shift_declaration(declaration, offset),
}
}
fn shift_declaration(declaration: &mut Decl, offset: usize) {
shift_span(&mut declaration.span, offset);
let DeclKind::Function(function) = &mut declaration.kind;
shift_span(&mut function.span, offset);
shift_span(&mut function.name.span, offset);
shift_span(&mut function.return_type.span, offset);
for parameter in &mut function.parameters {
shift_span(&mut parameter.span, offset);
shift_span(&mut parameter.name.span, offset);
shift_span(&mut parameter.type_name.span, offset);
}
shift_span(&mut function.body.span, offset);
for statement in &mut function.body.statements {
shift_statement(statement, offset);
}
}
fn shift_expression(expression: &mut Expr, offset: usize) {
shift_span(&mut expression.span, offset);
match &mut expression.kind {
ExprKind::Name(_) | ExprKind::Number(_) | ExprKind::String(_) => {}
ExprKind::Symbol(name) => shift_span(&mut name.span, offset),
ExprKind::Unary { operand, .. } => shift_expression(operand, offset),
ExprKind::Binary { left, right, .. } => {
shift_expression(left, offset);
shift_expression(right, offset);
}
ExprKind::Member { object, name } => {
shift_expression(object, offset);
shift_span(&mut name.span, offset);
}
ExprKind::Call { callee, arguments } => {
shift_expression(callee, offset);
for argument in arguments {
shift_expression(argument, offset);
}
}
}
}
+1
View File
@@ -4,6 +4,7 @@ mod developer_tools;
mod display_list;
mod document;
mod endor_host;
mod endor_script;
mod geometry;
mod layout;
mod scene;
+105 -2
View File
@@ -12,6 +12,7 @@ use crate::document::parse_color;
pub const MAX_UI_NODES: usize = 4096;
pub const MAX_UI_DEPTH: usize = 64;
pub const MAX_PAGES: usize = 64;
pub const MAX_DIMENSION: f64 = 1_000_000.0;
#[derive(Debug)]
@@ -68,9 +69,22 @@ struct BuildSession {
pool: Vec<(ObjectRef, UiSlot)>,
indexes: HashMap<ObjectRef, usize>,
used: usize,
pages: Vec<ObjectRef>,
pages_used: usize,
rendered: Option<(usize, Arc<UiNode>)>,
}
impl BuildSession {
fn page(&mut self) -> Result<Value, VmError> {
let page = self
.pages
.get(self.pages_used)
.ok_or(VmError::InvalidArgument(
"UI build exceeds the 64 page budget",
))?;
self.pages_used += 1;
Ok(Value::Object(*page))
}
fn child(&self, value: &Value) -> Result<Arc<UiNode>, VmError> {
let Value::Object(reference) = value else {
return Err(VmError::InvalidArgument("UI children must be UI nodes"));
@@ -113,6 +127,7 @@ enum Constructor {
Column,
Row,
Spacer,
Page,
}
impl Constructor {
@@ -123,6 +138,7 @@ impl Constructor {
Self::Column => "column",
Self::Row => "row",
Self::Spacer => "spacer",
Self::Page => "page",
}
}
}
@@ -148,6 +164,7 @@ impl ObjectPayload for UiConstructor {
Constructor::Rect => (2, 4),
Constructor::Column | Constructor::Row => (2, MAX_UI_NODES + 2),
Constructor::Spacer => (1, 1),
Constructor::Page => (0, 0),
};
if !(required..=maximum).contains(&arguments.len()) {
return Err(VmError::ArgumentCount {
@@ -218,11 +235,78 @@ impl ObjectPayload for UiConstructor {
}
}
Constructor::Spacer => UiKind::Spacer(dimension(&arguments[0])?),
Constructor::Page => return session.page(),
};
session.emit(kind)
}
}
#[derive(Debug)]
struct PagePayload;
impl ObjectPayload for PagePayload {
fn as_any(&self) -> &dyn Any {
self
}
fn kind_name(&self) -> &'static str {
"tessera-page"
}
fn allows_attributes(&self) -> bool {
true
}
}
#[derive(Debug)]
struct PageRender {
page: usize,
session: Arc<Mutex<BuildSession>>,
}
impl ObjectPayload for PageRender {
fn as_any(&self) -> &dyn Any {
self
}
fn kind_name(&self) -> &'static str {
"tessera-page-render"
}
fn call(&self, arguments: &[Value]) -> Result<Value, VmError> {
if arguments.len() != 1 {
return Err(VmError::ArgumentCount {
name: "page.render".into(),
required: 1,
maximum: 1,
provided: arguments.len(),
});
}
let mut session = self
.session
.lock()
.map_err(|_| VmError::InvalidArgument("UI build session is poisoned"))?;
if self.page >= session.pages_used {
return Err(VmError::InvalidArgument("page is not initialized"));
}
let root = session.child(&arguments[0])?;
session.rendered = Some((self.page, root));
Ok(Value::Nil)
}
}
pub struct UiBridge {
/// Borrowed from the candidate's external roots; never crosses VM lifetime.
pub ui: ObjectRef,
session: Arc<Mutex<BuildSession>>,
}
impl UiBridge {
pub fn rendered(&self) -> Result<Option<Arc<UiNode>>> {
let session = self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI build session is poisoned"))?;
Ok(session.rendered.as_ref().map(|(_, root)| root.clone()))
}
}
fn color(value: Option<&Value>, default: &str) -> Result<String, VmError> {
let value = match value {
Some(Value::Text(value)) => value.as_str(),
@@ -256,7 +340,7 @@ fn dimension(value: &Value) -> Result<f32, VmError> {
}
/// All allocation roots go to the candidate, including partial installation.
pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<ObjectRef> {
pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
let mut pool = Vec::with_capacity(MAX_UI_NODES);
let mut indexes = HashMap::with_capacity(MAX_UI_NODES);
for index in 0..MAX_UI_NODES {
@@ -270,7 +354,25 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<ObjectRef> {
pool,
indexes,
used: 0,
pages: Vec::with_capacity(MAX_PAGES),
pages_used: 0,
rendered: None,
}));
for page in 0..MAX_PAGES {
let reference = vm.allocate(PagePayload)?;
roots.push(reference);
let render = vm.allocate(PageRender {
page,
session: session.clone(),
})?;
roots.push(render);
vm.set_attr(reference, "render", Value::Object(render))?;
session
.lock()
.map_err(|_| anyhow::anyhow!("UI build session is poisoned"))?
.pages
.push(reference);
}
let ui = vm.new_object()?;
roots.push(ui);
for constructor in [
@@ -279,6 +381,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<ObjectRef> {
Constructor::Column,
Constructor::Row,
Constructor::Spacer,
Constructor::Page,
] {
let function = vm.allocate(UiConstructor {
constructor,
@@ -287,7 +390,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<ObjectRef> {
roots.push(function);
vm.set_attr(ui, constructor.name(), Value::Object(function))?;
}
Ok(ui)
Ok(UiBridge { ui, session })
}
pub fn snapshot(vm: &Vm, result: &Value) -> Result<Arc<UiNode>> {