From 47ab2aa3a6c6b6f1882540036e091e7843fd5da1 Mon Sep 17 00:00:00 2001 From: Codex Bot Date: Tue, 20 Oct 2026 13:47:29 +0800 Subject: [PATCH] feat: render top-level Endor scripts through embedded Aster --- .gitea/workflows/README.md | 4 + .gitea/workflows/ci.yml | 1 + README.md | 72 +++++--- examples/README.md | 12 +- examples/main.endor | 5 + examples/page.endor | 4 + scripts/README.md | 14 ++ scripts/build-macos-app.sh | 31 ++++ src/README.md | 16 +- src/api.rs | 27 +-- src/endor_host.rs | 340 ++++++++++++++++++++++++++++++++----- src/endor_script.rs | 150 ++++++++++++++++ src/main.rs | 1 + src/ui_bridge.rs | 107 +++++++++++- 14 files changed, 695 insertions(+), 89 deletions(-) create mode 100644 examples/main.endor create mode 100644 examples/page.endor create mode 100644 scripts/README.md create mode 100755 scripts/build-macos-app.sh create mode 100644 src/endor_script.rs diff --git a/.gitea/workflows/README.md b/.gitea/workflows/README.md index a7c721f..ae5b4af 100644 --- a/.gitea/workflows/README.md +++ b/.gitea/workflows/README.md @@ -8,4 +8,8 @@ The container includes the Endor examples used by the live guide and tests. It validates the actual Aster-backed source-to-UI path, snapshot lifetime, layout and execution budgets, failed-update preservation and stale-result invalidation, in addition to the existing rendering checks and Clippy. Cargo uses official sources. +The primary example now executes top-level let statements and ui.page().render +through Aster's Interactive interface. Function-entry compatibility, original +source positions, page methods and page budgets are also checked. The workflow +checks the macOS packaging script's syntax without running it on Linux. Docker is used only in this remote workflow; local compilation is native. diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 9023380..781ef08 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -43,6 +43,7 @@ jobs: - name: Verify portable GUI code run: | + sh -n scripts/build-macos-app.sh image="endorphin-tessera-gui-ci:${{ gitea.sha }}" docker build -f Dockerfile.ci -t "$image" . docker run --rm --init "$image" diff --git a/README.md b/README.md index 96f2d67..5e2dabf 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Tessera Tessera is a native desktop UI host embedding [Aster](vendor/aster/README.md). -Agents submit ordinary Endor source through a local namespace API. Aster parses, -compiles and executes it; `main` returns a Tessera UI object. Tessera extracts a +Agents submit ordinary Endor scripts through a local namespace API. Aster parses, +compiles and executes them; `ui.page().render(root)` selects a Tessera UI tree. Tessera extracts a native tree, lays it out and draws it using the existing software renderer. The initial page is empty. @@ -33,8 +33,15 @@ cargo build --release --locked Cargo compiles the required Aster libraries together with Tessera. No separate Aster build, service or deployment is required. Local builds download dependencies from official crates.io; do not substitute a mirror. Docker and automated tests -are not used locally. On macOS this produces a native executable, not an `.app` -bundle or installer. +are not used locally. The command above produces a native executable. To create a +Finder-launchable development app on macOS, run: + +```bash +sh scripts/build-macos-app.sh +``` + +Open `target/release/Tessera.app`. See [packaging](scripts/README.md); this local +bundle does not include distribution signing, notarization or an installer. The API listens only on `127.0.0.1:4737`. Start integrations with its live guide: @@ -52,30 +59,39 @@ PUT /v1/namespaces/app.main/code DELETE /v1/namespaces/app.main/code ``` -Submit the [complete example](examples/hello.endor): +Submit the [complete example](examples/page.endor): ```bash curl --request PUT --header 'Content-Type: text/plain; charset=utf-8' \ - --data-binary @examples/hello.endor \ + --data-binary @examples/page.endor \ http://127.0.0.1:4737/v1/namespaces/app.main/code ``` A minimal program is: ```endor -function main(): View { - let title = ui.text("Hello Endor", 2, "#242422") - let rectangle = ui.rect(240, 80, 12, "#DCE8FF") - return ui.column(24, 12, title, rectangle) -} +let heading = ui.text("Hello Endor", 2, "#242422") +let rectangle = ui.rect(240, 80, 12, "#DCE8FF") +let myui = ui.page() +myui.render(ui.column(24, 12, heading, rectangle)) ``` The namespace comes from the request path. `ui` is a host global declared to -Aster's compiler and injected into the candidate VM. Function declarations, -helper calls, local variables, member reads and positional arguments use Aster's -existing language implementation. `View` is currently a syntactic annotation; -Tessera validates the actual returned object. This does not add syntax to Endor. -The sample file is client-side material; the API itself stores source by namespace. +Aster's compiler and injected into the candidate VM. Top-level variables share +that VM's globals within this source submission. No `main` wrapper is generated. +Complete top-level inputs are separated by newlines; multiline calls and function +bodies are grouped before being handed to Aster's existing Interactive parser and +compiler. Expression continuations such as a member read on the following line +remain in the same input. Aster still owns every token, syntax rule, AST and +bytecode operation; the host only batches inputs and restores original AST offsets. + +The older [main-returning-View example](examples/main.endor) and the richer +[function example](examples/hello.endor) remain supported. A source consisting +entirely of function declarations uses Aster's Module entry and calls zero-argument +`main`. Mixed function declarations, bindings and expression statements use +Interactive inputs in source order. Type annotations remain syntactic in Aster; +Tessera checks actual UI values. The sample files are client-side material; the +API itself stores source by namespace. ## UI host API @@ -89,29 +105,37 @@ not Endor keyword argument syntax. | `ui.column` | Padding, gap, followed by zero or more UI children | | `ui.row` | Padding, gap, followed by zero or more UI children | | `ui.spacer` | Size | +| `ui.page` | No arguments; creates a page with a bound `render(root)` member | Colors are `#RRGGBB` or `#RRGGBBAA`. Dimensions are finite numbers in 0..1000000. +A namespace has one displayed page. If a script creates several pages or renders +more than once, its final successful render selects the visible root. Rendering +does not publish partial execution: a later error rejects the entire submission. + Aster keeps its exact numeric representation; conversion occurs only at the UI boundary. Text uses the current bitmap renderer, with Chinese fonts still pending. ## Candidate execution and publication -A dedicated worker performs source parsing, AST budget checks, compilation, -module initialization and zero-argument `main` execution in a fresh VM. It checks -the returned UI node, extracts native data, builds and validates the frame, then +A dedicated worker performs source parsing, AST budget checks and compilation in +a fresh candidate VM. It executes each script input with shared globals (or module +initialization and zero-argument `main` for function-only source), checks the +selected UI root, extracts native data, builds and validates the frame, then cleans up all VM roots. The window receives no VM handles. It relays out the owned native tree during redraw and display scale changes without rerunning Endor. The existing callback interface has no allocator access. Tessera therefore -preallocates at most 4096 real UI objects per candidate. Each is initialized once. +preallocates 4096 real UI node objects plus a separate 64-page pool with bound +render callbacks per candidate. Each constructed node is initialized once. Constructors return those borrowed handles; Aster retains call results under its existing rules. Native child data owns its own lifetime and contains no managed VM edges. This bounded pool is for snapshot construction, not a persistent dynamic UI. Limits are 1 MiB source, 10000 visited AST nodes with a 64-level visitor depth, -4096 constructed UI nodes, 64 UI levels, 10000 expanded layout visits and 16 KiB -per text. VM initialization and entry execution each have a 128-frame and 1000000- -instruction budget. Repeated children count again during layout, preventing a +4096 constructed UI nodes, 64 pages, 64 UI levels, 10000 expanded layout visits +and 16 KiB per text. Every execution has a 128-frame limit. Script inputs each +receive an equal share of a total 1000000-instruction allowance; function-only +module initialization and main execution each retain a 1000000-instruction budget. Repeated children count again during layout, preventing a small shared tree from expanding without bound. The build queue holds eight pending requests. @@ -144,7 +168,7 @@ maintain a second language AST. Local native builds and window runs are allowed; do not run local automated tests or Docker. [Remote CI](.gitea/README.md) on ces-control runs the existing renderer -checks and the new Aster execution, UI layout, source-range, lifetime, budget, +checks and the new Aster script/module execution, page rendering, UI layout, source-range, lifetime, budget, failed-update and deletion-invalidation regressions, plus Clippy. The API regressions use actual HTTP requests, including DELETE while the worker is occupied and a queued PUT is superseded. diff --git a/examples/README.md b/examples/README.md index d9ea032..ceb62c1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,11 +1,15 @@ # Endor UI examples -[hello.endor](hello.endor) returns a native UI tree using an ordinary helper -function and `main`. It covers text, a rounded rectangle, row/column layout and -spacing. Submit it as the UTF-8 body of a namespace PUT request; no namespace, +| File | Purpose | +| --- | --- | +| [page.endor](page.endor) | Four top-level statements construct text, a rectangle and a page, then call page.render; no main wrapper. | +| [main.endor](main.endor) | The earlier function-entry form, producing the same frame as page.endor. | +| [hello.endor](hello.endor) | Richer function-entry example covering a helper, Row/Column and Spacer. | + +Submit a file as the UTF-8 body of a namespace PUT request. No namespace, scene, render block or imports belong in the source. -The live `/agent` guide embeds this exact example. Remote CI executes it through +The live `/agent` guide embeds page.endor exactly. Remote CI executes the examples through Aster and checks the resulting coordinates and source ranges after VM cleanup. The file is client-side sample material; Tessera's API stores source by namespace. diff --git a/examples/main.endor b/examples/main.endor new file mode 100644 index 0000000..6c17a7d --- /dev/null +++ b/examples/main.endor @@ -0,0 +1,5 @@ +function main(): View { + let heading = ui.text("Hello Endor", 2, "#242422") + let rectangle = ui.rect(240, 80, 12, "#DCE8FF") + return ui.column(24, 12, heading, rectangle) +} diff --git a/examples/page.endor b/examples/page.endor new file mode 100644 index 0000000..89607e7 --- /dev/null +++ b/examples/page.endor @@ -0,0 +1,4 @@ +let heading = ui.text("Hello Endor", 2, "#242422") +let rectangle = ui.rect(240, 80, 12, "#DCE8FF") +let myui = ui.page() +myui.render(ui.column(24, 12, heading, rectangle)) diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 0000000..285b9c0 --- /dev/null +++ b/scripts/README.md @@ -0,0 +1,14 @@ +# Native packaging + +`build-macos-app.sh` builds the release executable with the normal Cargo lockfile +and creates `target/release/Tessera.app`. It uses the local Rust toolchain and +official Cargo sources; no mirror or Docker configuration is installed, and no +automated tests run. `CARGO_NET_OFFLINE=true` can reuse an already populated +official dependency cache. + +The local bundle has identifier `com.entroia.tessera` and can be opened from Finder. +It is a development app, without a distribution signature or notarization. +The CI workflow only checks this script's shell syntax on Linux; native bundle +construction and window presentation are verified on macOS. + +Return to the [project guide](../README.md). diff --git a/scripts/build-macos-app.sh b/scripts/build-macos-app.sh new file mode 100755 index 0000000..08c2876 --- /dev/null +++ b/scripts/build-macos-app.sh @@ -0,0 +1,31 @@ +#!/bin/sh +# Native macOS build and app bundle. No Docker, mirror configuration, or tests. +set -eu +if [ "$(uname -s)" != Darwin ]; then + echo "Build this app bundle on macOS." >&2 + exit 1 +fi +cd "$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)" +cargo build --release --locked +version=$(awk -F '"' '$1 ~ /^version[[:space:]]*=/ { print $2; exit }' Cargo.toml) +test -n "$version" +bundle="$(pwd)/target/release/Tessera.app" +mkdir -p "$bundle/Contents/MacOS" +cp target/release/tessera "$bundle/Contents/MacOS/tessera.new" +mv -f "$bundle/Contents/MacOS/tessera.new" "$bundle/Contents/MacOS/tessera" +cat > "$bundle/Contents/Info.plist.new" < + + + CFBundleIdentifiercom.entroia.tessera + CFBundleNameTessera + CFBundleDisplayNameTessera + CFBundleExecutabletessera + CFBundlePackageTypeAPPL + CFBundleShortVersionString$version + CFBundleVersion$version + NSHighResolutionCapable + +PLIST +mv -f "$bundle/Contents/Info.plist.new" "$bundle/Contents/Info.plist" +printf '%s\n' "$bundle" diff --git a/src/README.md b/src/README.md index fee4f6d..389d3e3 100644 --- a/src/README.md +++ b/src/README.md @@ -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. diff --git a/src/api.rs b/src/api.rs index 1728fb5..e3e4eab 100644 --- a/src/api.rs +++ b/src/api.rs @@ -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")); diff --git a/src/endor_host.rs b/src/endor_host.rs index 2376113..18bd3ac 100644 --- a/src/endor_host.rs +++ b/src/endor_host.rs @@ -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 = (|| { 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 Result> { + 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, @@ -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 { @@ -190,6 +267,7 @@ fn line_starts(source: &str) -> Vec { struct Architecture { line_starts: Vec, 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::().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::().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() { diff --git a/src/endor_script.rs b/src/endor_script.rs new file mode 100644 index 0000000..8bbe1ef --- /dev/null +++ b/src/endor_script.rs @@ -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), +} + +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 { + 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); + } + } + } +} diff --git a/src/main.rs b/src/main.rs index bc6ec7f..50037f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod developer_tools; mod display_list; mod document; mod endor_host; +mod endor_script; mod geometry; mod layout; mod scene; diff --git a/src/ui_bridge.rs b/src/ui_bridge.rs index e12b941..9468d28 100644 --- a/src/ui_bridge.rs +++ b/src/ui_bridge.rs @@ -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, used: usize, + pages: Vec, + pages_used: usize, + rendered: Option<(usize, Arc)>, } impl BuildSession { + fn page(&mut self) -> Result { + 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, 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>, +} + +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 { + 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>, +} + +impl UiBridge { + pub fn rendered(&self) -> Result>> { + 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 { let value = match value { Some(Value::Text(value)) => value.as_str(), @@ -256,7 +340,7 @@ fn dimension(value: &Value) -> Result { } /// All allocation roots go to the candidate, including partial installation. -pub fn install(vm: &mut Vm, roots: &mut Vec) -> Result { +pub fn install(vm: &mut Vm, roots: &mut Vec) -> Result { 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) -> Result { 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) -> Result { 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) -> Result { 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> {