feat: execute Tessera UI source with embedded Aster
Verify Tessera GUI / verify (push) Successful in 7m38s

This commit is contained in:
2026-10-20 03:35:46 +08:00
parent 3e1cce78f8
commit 8cb2bf8607
14 changed files with 1482 additions and 778 deletions
+470
View File
@@ -0,0 +1,470 @@
//! One source submission uses a fresh Aster VM and publishes an owned snapshot.
use anyhow::{Result, bail};
use endor_core::{ExecutionLimits, ObjectRef, Value, Vm, VmError};
use frontend::{
EndorReader, ParseMode, SourceUnit, Span,
ast::{AstRoot, Decl, DeclKind, Expr, ExprKind, Spanned, Stmt, StmtKind, visitor::Visitor},
};
use crate::{
document::{CodeNode, RenderDocument},
layout, ui_bridge,
};
pub const MAX_SOURCE_BYTES: usize = 1_048_576;
const LIMITS: ExecutionLimits = ExecutionLimits {
max_frames: 128,
max_instructions: 1_000_000,
};
const MAX_AST_NODES: usize = 10_000;
const MAX_AST_DEPTH: usize = 64;
pub fn compile(source: &str, namespace: &str, submission: u64) -> Result<RenderDocument> {
if source.len() > MAX_SOURCE_BYTES {
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 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)?;
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");
};
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),
frame: layout::frame(&ui_root)?,
ui_root: Some(ui_root),
};
document.validate()?;
Ok(document)
})();
let cleanup = candidate.close();
match (result, cleanup) {
(Ok(document), Ok(())) => Ok(document),
(Err(error), Ok(())) | (Ok(_), Err(error)) => Err(error),
(Err(error), Err(cleanup)) => Err(error.context(format!("candidate cleanup: {cleanup}"))),
}
}
struct Candidate {
vm: Vm,
roots: Vec<ObjectRef>,
globals: Option<ObjectRef>,
}
impl Candidate {
fn new() -> Result<Self> {
Ok(Self {
vm: Vm::new()?,
roots: Vec::new(),
globals: None,
})
}
fn close(&mut self) -> Result<()> {
if let Some(globals) = self.globals.take() {
self.vm.dict_clear(globals)?;
}
while let Some(root) = self.roots.pop() {
self.vm.release(root)?;
}
self.vm.collect_cycles();
if self.vm.object_count() != 0 {
bail!("candidate VM retained objects after cleanup");
}
Ok(())
}
}
impl Drop for Candidate {
fn drop(&mut self) {
// Explicit close reports accounting errors; dropping the fresh VM is
// also the final cleanup boundary on every early return or unwind.
let _ = self.close();
}
}
fn runtime_error(vm: &Vm, error: VmError) -> anyhow::Error {
let locations = vm
.last_traceback()
.iter()
.map(|frame| {
format!(
"{}:{} in {} (instruction {})",
frame.filename, frame.first_line, frame.function, frame.instruction_offset
)
})
.collect::<Vec<_>>()
.join("\n");
if locations.is_empty() {
error.into()
} else {
anyhow::anyhow!("{error}\n{locations}")
}
}
#[derive(Default)]
struct AstBudget {
depth: usize,
nodes: usize,
}
impl Visitor for AstBudget {
type Error = anyhow::Error;
fn enter_node(&mut self, _span: Span) -> Result<()> {
self.depth += 1;
self.nodes += 1;
if self.depth > MAX_AST_DEPTH || self.nodes > MAX_AST_NODES {
bail!("Aster AST exceeds the 64-level or 10000-node host budget");
}
Ok(())
}
fn leave_node(&mut self) {
self.depth -= 1;
}
}
fn architecture(source: &SourceUnit, ast: &AstRoot, namespace: &str) -> CodeNode {
let mut tree = Architecture {
line_starts: line_starts(source.text()),
next_id: 0,
};
let AstRoot::Module(program) = ast else {
unreachable!("module parse mode");
};
let children = program
.declarations
.iter()
.map(|decl| tree.declaration(decl))
.collect();
tree.node(namespace.to_owned(), "module", ast.span(), children)
}
fn line_starts(source: &str) -> Vec<usize> {
let bytes = source.as_bytes();
let mut starts = vec![0];
for (offset, byte) in bytes.iter().enumerate() {
if *byte == b'\n' || (*byte == b'\r' && bytes.get(offset + 1) != Some(&b'\n')) {
starts.push(offset + 1);
}
}
starts
}
struct Architecture {
line_starts: Vec<usize>,
next_id: usize,
}
impl Architecture {
fn node(&mut self, name: String, kind: &str, span: Span, children: Vec<CodeNode>) -> CodeNode {
let id = self.next_id;
self.next_id += 1;
let line = |offset| self.line_starts.partition_point(|start| *start <= offset);
CodeNode {
id: format!("ast-{id}"),
name,
kind: kind.into(),
start_line: line(span.start),
end_line: line(span.end.saturating_sub(1).max(span.start)),
children,
}
}
fn declaration(&mut self, declaration: &Decl) -> CodeNode {
let DeclKind::Function(function) = &declaration.kind;
let children = function
.body
.statements
.iter()
.map(|stmt| self.statement(stmt))
.collect();
self.node(
function.name.text.clone(),
"function",
function.span,
children,
)
}
fn statement(&mut self, statement: &Stmt) -> CodeNode {
match &statement.kind {
StmtKind::Binding(binding) => {
let child = self.expression(&binding.value);
self.node(
binding.name.text.clone(),
"let",
statement.span,
vec![child],
)
}
StmtKind::Return(returned) => {
let children = returned
.value
.iter()
.map(|expr| self.expression(expr))
.collect();
self.node("return".into(), "return", statement.span, children)
}
StmtKind::Expression(expr) => self.expression(expr),
StmtKind::Declaration(decl) => self.declaration(decl),
}
}
fn expression(&mut self, expr: &Expr) -> CodeNode {
let (name, kind, children) = match &expr.kind {
ExprKind::Name(name) => (name.clone(), "name", vec![]),
ExprKind::Number(value) => (value.clone(), "number", vec![]),
ExprKind::String(value) => (value.chars().take(40).collect(), "string", vec![]),
ExprKind::Symbol(name) => (name.text.clone(), "symbol", vec![]),
ExprKind::Unary { operand, .. } => {
("negate".into(), "unary", vec![self.expression(operand)])
}
ExprKind::Binary { left, right, .. } => (
"binary".into(),
"binary",
vec![self.expression(left), self.expression(right)],
),
ExprKind::Member { object, name } => {
(name.text.clone(), "member", vec![self.expression(object)])
}
ExprKind::Call { callee, arguments } => {
let mut children = vec![self.expression(callee)];
children.extend(arguments.iter().map(|expr| self.expression(expr)));
("call".into(), "call", children)
}
};
self.node(name, kind, expr.span, children)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::document::{NamespaceState, RenderCommand};
const EXAMPLE: &str = include_str!("../examples/hello.endor");
#[test]
fn executes_aster_functions_and_renders_after_vm_cleanup() {
let document = compile(EXAMPLE, "app.main", 1).unwrap();
assert_eq!(document.source, EXAMPLE);
let tree = document.root.as_ref().unwrap();
assert_eq!(tree.kind, "module");
assert_eq!(tree.name, "app.main");
assert_eq!(tree.children[0].name, "heading");
assert_eq!(tree.children[1].name, "main");
assert_eq!(tree.children[1].start_line, 5);
assert_eq!(document.frame.commands.len(), 4);
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,
..
}
));
assert!(matches!(
&document.frame.commands[3],
RenderCommand::Text {
x: 120.0,
y: 144.0,
..
}
));
// Candidate::close already proved zero remaining VM objects. Both
// redraws must still work from native data at different display scales.
assert_eq!(document.display_list(1.0).unwrap().commands().len(), 5);
assert_eq!(document.display_list(2.0).unwrap().commands().len(), 5);
}
#[test]
fn borrowed_ui_handles_survive_variables_helpers_and_reused_children() {
let source = r#"function wrap(child: View): View { return ui.row(4, 8, child, child) }
function main(): View { let child = ui.text("shared") return wrap(child) }"#;
let document = compile(source, "arbitrary.namespace", 3).unwrap();
assert_eq!(document.frame.commands.len(), 2);
assert!(matches!(
&document.frame.commands[0],
RenderCommand::Text { x: 4.0, y: 4.0, .. }
));
assert!(matches!(
&document.frame.commands[1],
RenderCommand::Text {
x: 60.0,
y: 4.0,
..
}
));
}
#[test]
fn architecture_ranges_follow_crlf_and_cr_source_snapshots() {
for ending in ["\r\n", "\r"] {
let source = EXAMPLE.replace('\n', ending);
let document = compile(&source, "app.main", 1).unwrap();
assert_eq!(document.source, source);
assert_eq!(document.root.unwrap().children[1].start_line, 5);
}
}
#[test]
fn rejects_invalid_entry_objects_arguments_and_old_syntax() {
for source in [
"namespace app.main export scene Main { render { Text(\"old\") } }",
"function helper(): View { return ui.text(\"missing main\") }",
"function main(value: View): View { return value }",
"function main(): View { return \"wrong root\" }",
"function main(): View { return main }",
"function main(): View { return ui.text(1) }",
"function main(): View { return ui.text(\"x\", 1.0) }",
"function main(): View { return ui.text(\"x\", 9) }",
"function main(): View { return ui.rect(-1, 1) }",
"function main(): View { return ui.rect(1000001, 1) }",
"function main(): View { return ui.rect(1, 1, 0, \"#中中\") }",
"function main(): View { return ui.column(0, 0, ui.text) }",
"function main(): View { return unknown(1) }",
"function main(): View { return ui.missing() }",
] {
assert!(
compile(source, "app.main", 1).is_err(),
"accepted: {source}"
);
}
}
#[test]
fn source_diagnostics_keep_unicode_locations_and_submission_origin() {
let error = compile(
"function main(): View {\n let title = ui.text(\"中文\")\n return missing\n}",
"app.main",
17,
)
.unwrap_err();
let error = error.downcast_ref::<frontend::SourceDiagnostic>().unwrap();
assert_eq!(error.location.line, 3);
assert!(error.origin.to_string().contains("submission 17"));
}
#[test]
fn recursive_program_stops_at_the_vm_frame_limit() {
let error = compile("function main(): View { return main() }", "app.main", 1).unwrap_err();
assert!(
error
.to_string()
.contains("execution limit reached: frames")
);
}
fn nested_functions(levels: usize, shared: bool) -> String {
let mut source = "function node0(): View { return ui.text(\"x\") }\n".to_owned();
for index in 1..=levels {
let previous = index - 1;
if shared {
source.push_str(&format!("function node{index}(): View {{ let child = node{previous}() return ui.column(0, 0, child, child) }}\n"));
} else {
source.push_str(&format!("function node{index}(): View {{ return ui.column(0, 0, node{previous}(), node{previous}()) }}\n"));
}
}
source.push_str(&format!(
"function main(): View {{ return node{levels}() }}"
));
source
}
#[test]
fn construction_and_expanded_layout_have_separate_budgets() {
let error = compile(&nested_functions(12, false), "app.main", 1).unwrap_err();
assert!(error.to_string().contains("4096 node budget"), "{error:#}");
let error = compile(&nested_functions(14, true), "app.main", 2).unwrap_err();
assert!(
error.to_string().contains("expanded UI layout"),
"{error:#}"
);
}
#[test]
fn ui_depth_is_bounded_independently_of_call_frames() {
let mut source = "function node0(): View { return ui.text(\"x\") }\n".to_owned();
for index in 1..=64 {
source.push_str(&format!(
"function node{index}(): View {{ return ui.column(0, 0, node{}()) }}\n",
index - 1
));
}
source.push_str("function main(): View { return node64() }");
let error = compile(&source, "app.main", 1).unwrap_err();
assert!(error.to_string().contains("64 levels"), "{error:#}");
}
#[test]
fn failed_updates_preserve_success_and_delete_invalidates_inflight_work() {
let mut state = NamespaceState::default();
state.submission = state.next_submission().unwrap();
assert_eq!(
state
.commit(state.submission, compile(EXAMPLE, "app.main", 1).unwrap())
.unwrap(),
Some(1)
);
state.submission = state.next_submission().unwrap();
assert!(compile("invalid", "app.main", state.submission).is_err());
assert_eq!(state.revision, 1);
assert_eq!(state.document.source, EXAMPLE);
let stale = state.submission;
let candidate = compile(EXAMPLE, "app.main", stale).unwrap();
state.submission = state.next_submission().unwrap();
assert!(state.commit(stale, candidate.clone()).unwrap().is_none());
let deleted = state.clear().unwrap();
assert!(state.commit(stale, candidate).unwrap().is_none());
assert_eq!(state.revision, deleted);
assert!(state.document.source.is_empty());
state.submission = state.next_submission().unwrap();
let candidate = compile(EXAMPLE, "app.main", state.submission).unwrap();
assert_eq!(
state.commit(state.submission, candidate).unwrap(),
Some(deleted + 1)
);
}
}