5598 lines
192 KiB
Rust
5598 lines
192 KiB
Rust
//! Compile complete source in a fresh VM or retain a worker-owned Console VM.
|
|
//! Console retains mounted native references and publishes immutable frames after successful inputs.
|
|
use anyhow::{Result, bail};
|
|
use endor_core::{ExecutionLimits, ObjectRef, Value, Vm, VmError};
|
|
use frontend::{
|
|
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,
|
|
};
|
|
|
|
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 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 bridge = ui_bridge::install(&mut candidate.vm, &mut candidate.roots)?;
|
|
bridge.install_globals(&mut candidate.vm, globals)?;
|
|
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)?;
|
|
}
|
|
bridge.refresh_bindings(&mut candidate.vm)?;
|
|
let ui_root = match &plan {
|
|
SourcePlan::Script(_) => bridge
|
|
.rendered(&candidate.vm)?
|
|
.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))?;
|
|
bridge.refresh_bindings(&mut candidate.vm)?;
|
|
let snapshot = bridge
|
|
.rendered(&candidate.vm)
|
|
.and_then(|rendered| match rendered {
|
|
Some(root) => Ok(root),
|
|
None => ui_bridge::snapshot(&candidate.vm, &value),
|
|
});
|
|
candidate.vm.release_value(value)?;
|
|
snapshot?
|
|
}
|
|
};
|
|
let document = RenderDocument {
|
|
windows: std::collections::BTreeMap::new(),
|
|
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}"))),
|
|
}
|
|
}
|
|
|
|
/// Evaluate exactly one expression in a disposable VM; never touch the published page.
|
|
pub fn evaluate(source: &str) -> Result<String> {
|
|
if source.len() > MAX_SOURCE_BYTES {
|
|
bail!("expression exceeds the 1 MiB limit");
|
|
}
|
|
let source = EndorReader::from_text("console expression", source);
|
|
let ast = frontend::parse_source_ast(&source, frontend::ParseMode::Expression)?;
|
|
AstBudget::default().visit_ast_root(&ast)?;
|
|
let compiled = compiler::compile_ast(&ast, &source, &compiler::CompileOptions::default())
|
|
.map_err(|error| source.annotate(error))?;
|
|
let mut candidate = Candidate::new()?;
|
|
let result = (|| {
|
|
let globals = candidate.vm.new_dict()?;
|
|
candidate.globals = Some(globals);
|
|
candidate.roots.push(globals);
|
|
let code = compiled.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))?;
|
|
let rendered = match &value {
|
|
Value::Nil => "nil".to_owned(),
|
|
Value::Bool(v) => v.to_string(),
|
|
Value::Text(v) => format!("{v:?}"),
|
|
Value::Number(v) => v.to_string(),
|
|
Value::Object(v) => candidate.vm.repr(*v)?,
|
|
};
|
|
candidate.vm.release_value(value)?;
|
|
Ok(rendered)
|
|
})();
|
|
let cleanup = candidate.close();
|
|
match (result, cleanup) {
|
|
(Ok(value), Ok(())) => Ok(value),
|
|
(Err(e), _) | (_, Err(e)) => Err(e),
|
|
}
|
|
}
|
|
|
|
fn compile_plan(source: &SourceUnit, plan: &SourcePlan) -> Result<Vec<compiler::CompiledCode>> {
|
|
compile_plan_with_globals(
|
|
source,
|
|
plan,
|
|
std::iter::once("ui")
|
|
.chain(ui_bridge::TYPE_NAMES.iter().copied())
|
|
.chain(ui_bridge::QUERY_NAMES.iter().copied())
|
|
.map(str::to_owned)
|
|
.collect(),
|
|
)
|
|
}
|
|
|
|
fn compile_plan_with_globals(
|
|
source: &SourceUnit,
|
|
plan: &SourcePlan,
|
|
mut globals: BTreeSet<String>,
|
|
) -> Result<Vec<compiler::CompiledCode>> {
|
|
// 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 name = match &decl.kind {
|
|
DeclKind::Function(function) => &function.name,
|
|
DeclKind::Class(class) => &class.name,
|
|
};
|
|
globals.insert(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());
|
|
} else if let StmtKind::Assignment(assignment) = &statement.kind {
|
|
globals.insert(assignment.name.text.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(compiled)
|
|
}
|
|
|
|
/// One real VM, owned only by the existing execution worker. No history replay.
|
|
/// The UI bridge shares the same managed object heap and publishes atomic frames.
|
|
pub struct ReplSession {
|
|
candidate: Candidate,
|
|
bridge: ui_bridge::UiBridge,
|
|
pub id: u64,
|
|
inputs: usize,
|
|
ast_nodes: usize,
|
|
source: String,
|
|
nodes: Vec<CodeNode>,
|
|
faulted: bool,
|
|
program_root: Option<CodeNode>,
|
|
documents: std::collections::BTreeMap<u64, RenderDocument>,
|
|
}
|
|
pub struct ReplResult {
|
|
pub value: String,
|
|
pub document: Option<RenderDocument>,
|
|
pub bindings: usize,
|
|
pub input: usize,
|
|
}
|
|
impl ReplSession {
|
|
/// Compile once into an isolated candidate, then keep its actual heap and
|
|
/// callback environments alive after the first successful publication.
|
|
pub fn load(
|
|
source: &str,
|
|
namespace: &str,
|
|
submission: u64,
|
|
id: u64,
|
|
) -> Result<(Self, RenderDocument)> {
|
|
Self::load_with_viewport(source, namespace, submission, id, None)
|
|
}
|
|
|
|
pub fn load_with_viewport(
|
|
source: &str,
|
|
namespace: &str,
|
|
submission: u64,
|
|
id: u64,
|
|
viewport: Option<crate::layout::Size>,
|
|
) -> Result<(Self, RenderDocument)> {
|
|
if source.len() > MAX_SOURCE_BYTES {
|
|
bail!("source exceeds the 1 MiB limit");
|
|
}
|
|
let unit = EndorReader::from_text(format!("{namespace} [submission {submission}]"), source);
|
|
let plan = endor_script::parse(&unit)?;
|
|
let mut budget = AstBudget::default();
|
|
for ast in plan.roots() {
|
|
budget.visit_ast_root(ast)?;
|
|
}
|
|
let compiled = compile_plan(&unit, &plan)?;
|
|
let mut runtime = Self::new(id)?;
|
|
runtime
|
|
.bridge
|
|
.set_viewport(&mut runtime.candidate.vm, viewport)?;
|
|
let globals = runtime.candidate.globals.expect("program globals");
|
|
let limits = if matches!(plan, SourcePlan::Script(_)) {
|
|
ExecutionLimits {
|
|
max_instructions: LIMITS.max_instructions / compiled.len().max(1),
|
|
..LIMITS
|
|
}
|
|
} else {
|
|
LIMITS
|
|
};
|
|
for template in &compiled {
|
|
let code = template.load(&mut runtime.candidate.vm)?;
|
|
let result = runtime
|
|
.candidate
|
|
.vm
|
|
.execute_with_limits(code, globals, limits)
|
|
.map_err(|error| runtime_error(&runtime.candidate.vm, error));
|
|
runtime.candidate.vm.release(code)?;
|
|
runtime.candidate.vm.release_value(result?)?;
|
|
}
|
|
if matches!(plan, SourcePlan::Module(_)) {
|
|
let main = runtime
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("main".into()))?
|
|
.ok_or_else(|| anyhow::anyhow!("declare a main function"))?;
|
|
let Value::Object(main) = main else {
|
|
bail!("main must be callable");
|
|
};
|
|
let value = runtime
|
|
.candidate
|
|
.vm
|
|
.call_with_limits(main, &[], LIMITS)
|
|
.map_err(|error| runtime_error(&runtime.candidate.vm, error))?;
|
|
let opened = if runtime.bridge.has_page()? {
|
|
Ok(())
|
|
} else {
|
|
runtime.bridge.open_value(&mut runtime.candidate.vm, &value)
|
|
};
|
|
runtime.candidate.vm.release_value(value)?;
|
|
opened?;
|
|
}
|
|
if !runtime.bridge.has_page()? {
|
|
bail!("script must call page.render(root) or ui.open(value)");
|
|
}
|
|
runtime.source = source.into();
|
|
runtime.ast_nodes = budget.nodes;
|
|
runtime.program_root = Some(architecture(&unit, &plan, namespace, submission));
|
|
let document = runtime
|
|
.refresh_document()?
|
|
.ok_or_else(|| anyhow::anyhow!("program did not publish a Page"))?;
|
|
runtime
|
|
.bridge
|
|
.reclaim(&mut runtime.candidate.vm, &mut runtime.candidate.roots)?;
|
|
Ok((runtime, document))
|
|
}
|
|
|
|
pub fn new_with_viewport(id: u64, viewport: Option<crate::layout::Size>) -> Result<Self> {
|
|
let mut runtime = Self::new(id)?;
|
|
runtime
|
|
.bridge
|
|
.set_viewport(&mut runtime.candidate.vm, viewport)?;
|
|
Ok(runtime)
|
|
}
|
|
|
|
pub fn new(id: u64) -> Result<Self> {
|
|
let mut candidate = Candidate::new()?;
|
|
let globals = candidate.vm.new_dict()?;
|
|
candidate.globals = Some(globals);
|
|
candidate.roots.push(globals);
|
|
let bridge = ui_bridge::install(&mut candidate.vm, &mut candidate.roots)?;
|
|
bridge.install_globals(&mut candidate.vm, globals)?;
|
|
Ok(Self {
|
|
candidate,
|
|
bridge,
|
|
id,
|
|
inputs: 0,
|
|
ast_nodes: 0,
|
|
source: String::new(),
|
|
nodes: Vec::new(),
|
|
faulted: false,
|
|
program_root: None,
|
|
documents: std::collections::BTreeMap::new(),
|
|
})
|
|
}
|
|
pub fn execute(&mut self, source: &str, namespace: &str) -> Result<ReplResult> {
|
|
if self.faulted {
|
|
bail!(
|
|
"runtime stopped after a callback or rendering failure; reset the session or replace its program"
|
|
);
|
|
}
|
|
if source.len() > MAX_SOURCE_BYTES {
|
|
bail!("input exceeds 1 MiB");
|
|
}
|
|
if self.inputs >= 256 || self.source.len() + source.len() + 80 > MAX_SOURCE_BYTES {
|
|
bail!("console session exceeds 256 inputs or 1 MiB; reset the session to continue");
|
|
}
|
|
let source = EndorReader::from_text(
|
|
format!("console session {} input {}", self.id, self.inputs + 1),
|
|
source,
|
|
);
|
|
let plan = endor_script::parse(&source)?;
|
|
let mut budget = AstBudget::default();
|
|
for ast in plan.roots() {
|
|
budget.visit_ast_root(ast)?;
|
|
}
|
|
if self.ast_nodes + budget.nodes > MAX_AST_NODES {
|
|
bail!("console session exceeds 10000 AST nodes; reset the session to continue");
|
|
}
|
|
let globals = self.candidate.globals.expect("live console globals");
|
|
let names = self
|
|
.candidate
|
|
.vm
|
|
.dict_items(globals)?
|
|
.iter()
|
|
.filter_map(|(key, _)| {
|
|
if let Value::Text(name) = key {
|
|
Some(name.clone())
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect();
|
|
let compiled = compile_plan_with_globals(&source, &plan, names)?;
|
|
// Parsing/compilation finish before any new statement runs. Successful earlier
|
|
// statements in a runtime-failed input remain live, as in an ordinary REPL.
|
|
self.bridge.discard_pending(&mut self.candidate.vm)?;
|
|
self.bridge
|
|
.reclaim(&mut self.candidate.vm, &mut self.candidate.roots)?;
|
|
self.inputs += 1;
|
|
self.ast_nodes += budget.nodes;
|
|
if let Some(root) = self.program_root.take() {
|
|
self.nodes.push(root);
|
|
}
|
|
if !self.source.is_empty() && !self.source.ends_with('\n') {
|
|
self.source.push('\n');
|
|
}
|
|
let offset = line_starts(&self.source).len();
|
|
self.source
|
|
.push_str(&format!("// Console input {}\n", self.inputs));
|
|
self.source.push_str(source.text());
|
|
if !self.source.ends_with('\n') {
|
|
self.source.push('\n');
|
|
}
|
|
let mut node = architecture(
|
|
&source,
|
|
&plan,
|
|
&format!("{namespace}:console-{}", self.id),
|
|
self.inputs as u64,
|
|
);
|
|
node.name = format!("Input {}", self.inputs);
|
|
shift_node_lines(&mut node, offset);
|
|
self.nodes.push(node);
|
|
let result: Result<ReplResult> = (|| {
|
|
let limits = ExecutionLimits {
|
|
max_instructions: LIMITS.max_instructions / compiled.len().max(1),
|
|
..LIMITS
|
|
};
|
|
let mut rendered_value = "nil".to_owned();
|
|
for (ast, template) in plan.roots().iter().zip(&compiled) {
|
|
let code = template.load(&mut self.candidate.vm)?;
|
|
let executed = self
|
|
.candidate
|
|
.vm
|
|
.execute_with_limits(code, globals, limits)
|
|
.map_err(|error| runtime_error(&self.candidate.vm, error));
|
|
self.candidate.vm.release(code)?;
|
|
let value = executed?;
|
|
// Keep the actual last result accessible as `_`, including unnamed UI objects.
|
|
let display = if let AstRoot::Interactive(input) = ast {
|
|
match &input.statements[0].kind {
|
|
StmtKind::Binding(binding) => self
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text(binding.name.text.clone()))?
|
|
.unwrap_or_else(|| value.clone()),
|
|
StmtKind::Assignment(assignment) => self
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text(assignment.name.text.clone()))?
|
|
.unwrap_or_else(|| value.clone()),
|
|
_ => value.clone(),
|
|
}
|
|
} else {
|
|
value.clone()
|
|
};
|
|
let repr = value_repr(&self.candidate.vm, &display);
|
|
let retained =
|
|
self.candidate
|
|
.vm
|
|
.dict_set(globals, Value::Text("_".into()), display);
|
|
self.candidate.vm.release_value(value)?;
|
|
retained?;
|
|
rendered_value = repr?;
|
|
}
|
|
let document = self.refresh_document()?;
|
|
Ok(ReplResult {
|
|
value: rendered_value,
|
|
document,
|
|
bindings: self
|
|
.candidate
|
|
.vm
|
|
.dict_items(globals)?
|
|
.len()
|
|
.saturating_sub(1),
|
|
input: self.inputs,
|
|
})
|
|
})();
|
|
if result.is_err() {
|
|
self.take_window_commands()?;
|
|
self.bridge.discard_pending(&mut self.candidate.vm)?;
|
|
if let Some(node) = self.nodes.last_mut() {
|
|
node.name
|
|
.push_str(" (error; earlier statements may remain live)");
|
|
}
|
|
}
|
|
self.bridge
|
|
.reclaim(&mut self.candidate.vm, &mut self.candidate.roots)?;
|
|
result
|
|
}
|
|
|
|
fn refresh_document(&mut self) -> Result<Option<RenderDocument>> {
|
|
let mut work = 0;
|
|
let mut changed = false;
|
|
let mut mutated_mounts = false;
|
|
let result = (|| {
|
|
loop {
|
|
if let Err(error) = self.bridge.refresh_bindings(&mut self.candidate.vm) {
|
|
self.faulted = true;
|
|
return Err(error);
|
|
}
|
|
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.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
|
|
}
|
|
|
|
/// Deliver ready work on the owning VM worker, never from the GUI or a
|
|
/// provider thread. A failed callback stops the same shared fault domain.
|
|
pub fn poll_tasks(&mut self) -> Result<Option<RenderDocument>> {
|
|
if self.faulted {
|
|
if self.bridge.cancel_tasks(&mut self.candidate.vm)? > 0 {
|
|
self.bridge
|
|
.reclaim(&mut self.candidate.vm, &mut self.candidate.roots)?;
|
|
}
|
|
return Ok(None);
|
|
}
|
|
let mut completed = false;
|
|
let result = (|| {
|
|
if self.bridge.poll_tasks(&mut self.candidate.vm)? == 0 {
|
|
return Ok(None);
|
|
}
|
|
completed = true;
|
|
self.refresh_document()
|
|
})();
|
|
if result.is_err() {
|
|
self.faulted = true;
|
|
self.bridge.cancel_tasks(&mut self.candidate.vm)?;
|
|
self.take_window_commands()?;
|
|
self.bridge.discard_pending(&mut self.candidate.vm)?;
|
|
}
|
|
if completed || result.is_err() {
|
|
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)
|
|
}
|
|
|
|
pub fn resize(
|
|
&mut self,
|
|
viewport: Option<crate::layout::Size>,
|
|
) -> Result<Option<RenderDocument>> {
|
|
if self.faulted {
|
|
bail!("runtime stopped after a callback or rendering failure");
|
|
}
|
|
if !self.bridge.set_viewport(&mut self.candidate.vm, viewport)? {
|
|
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 retire_window(&mut self, id: u64) -> Result<()> {
|
|
// This path is exclusively host cleanup after the domain has stopped.
|
|
// No language functions, layout, paint or lifecycle hooks may run here.
|
|
self.faulted = true;
|
|
self.bridge.retire_window(&mut self.candidate.vm, id)?;
|
|
self.documents.remove(&id);
|
|
let _ = self.take_window_commands()?;
|
|
self.bridge
|
|
.reclaim(&mut self.candidate.vm, &mut self.candidate.roots)
|
|
}
|
|
|
|
pub fn accepts_tab_drop(&self, input: &crate::events::Input) -> bool {
|
|
self.bridge.accepts_tab_drop(&self.candidate.vm, input)
|
|
}
|
|
|
|
pub fn accepts_window(&self, id: u64) -> bool {
|
|
self.bridge.accepts_window(id)
|
|
}
|
|
|
|
pub fn is_faulted(&self) -> bool {
|
|
self.faulted
|
|
}
|
|
|
|
pub fn dispatch(&mut self, input: &crate::events::Input) -> Result<ReplResult> {
|
|
if self.faulted {
|
|
bail!("runtime stopped after a callback or rendering failure");
|
|
}
|
|
if input.session != self.id {
|
|
bail!("event belongs to another runtime session");
|
|
}
|
|
if !self.accepts_tab_drop(input) {
|
|
bail!("drop targets an obsolete presentation");
|
|
}
|
|
self.bridge.select_window(input.window)?;
|
|
let result = (|| {
|
|
self.bridge.dispatch(&mut self.candidate.vm, input)?;
|
|
let document = self.refresh_document()?;
|
|
Ok(ReplResult {
|
|
value: "nil".into(),
|
|
document,
|
|
bindings: self
|
|
.candidate
|
|
.vm
|
|
.dict_items(self.candidate.globals.expect("live globals"))?
|
|
.len()
|
|
.saturating_sub(1),
|
|
input: self.inputs,
|
|
})
|
|
})();
|
|
self.bridge.select_window(crate::window::MAIN_WINDOW)?;
|
|
if result.is_err() {
|
|
self.take_window_commands()?;
|
|
self.faulted = true;
|
|
self.bridge.discard_pending(&mut self.candidate.vm)?;
|
|
}
|
|
self.bridge
|
|
.reclaim(&mut self.candidate.vm, &mut self.candidate.roots)?;
|
|
result
|
|
}
|
|
}
|
|
fn shift_node_lines(node: &mut CodeNode, offset: usize) {
|
|
node.start_line += offset;
|
|
node.end_line += offset;
|
|
for child in &mut node.children {
|
|
shift_node_lines(child, offset);
|
|
}
|
|
}
|
|
fn value_repr(vm: &Vm, value: &Value) -> Result<String> {
|
|
Ok(match value {
|
|
Value::Nil => "nil".into(),
|
|
Value::Bool(v) => v.to_string(),
|
|
Value::Text(v) => format!("{v:?}"),
|
|
Value::Number(v) => v.to_string(),
|
|
Value::Object(v) => vm.repr(*v)?,
|
|
})
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
pub(crate) 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,
|
|
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 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"
|
|
};
|
|
tree.node(namespace.to_owned(), kind, 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,
|
|
identity: String,
|
|
}
|
|
|
|
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}", self.identity),
|
|
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 {
|
|
match &declaration.kind {
|
|
DeclKind::Function(function) => self.function(function),
|
|
DeclKind::Class(class) => {
|
|
let mut children: Vec<_> = class
|
|
.fields
|
|
.iter()
|
|
.map(|field| self.node(field.name.text.clone(), "field", field.span, vec![]))
|
|
.collect();
|
|
children.extend(class.methods.iter().map(|method| self.function(method)));
|
|
self.node(class.name.text.clone(), "class", class.span, children)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn function(&mut self, function: &frontend::ast::Function) -> CodeNode {
|
|
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::MemberAssignment(assignment) => {
|
|
let object = self.expression(&assignment.object);
|
|
let value = self.expression(&assignment.value);
|
|
self.node(
|
|
assignment.name.text.clone(),
|
|
"member-assign",
|
|
statement.span,
|
|
vec![object, value],
|
|
)
|
|
}
|
|
StmtKind::Binding(binding) => {
|
|
let child = self.expression(&binding.value);
|
|
self.node(
|
|
binding.name.text.clone(),
|
|
"let",
|
|
statement.span,
|
|
vec![child],
|
|
)
|
|
}
|
|
StmtKind::Assignment(assignment) => {
|
|
let child = self.expression(&assignment.value);
|
|
self.node(
|
|
assignment.name.text.clone(),
|
|
"assign",
|
|
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");
|
|
const PAGE: &str = include_str!("../examples/page.endor");
|
|
|
|
#[test]
|
|
fn actual_mount_version_reads_still_bypass_measurement_cache() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
r#"
|
|
class Versioned: RectBlock {
|
|
function measure(self: Versioned,mount: Mount,constraints: Constraints): MeasureResult {
|
|
return MeasureResult(mount.layoutVersion,20)
|
|
}
|
|
}
|
|
rect=Versioned(80,20)
|
|
page=Page()
|
|
page.render(rect)
|
|
"#,
|
|
"app.main",
|
|
1,
|
|
241,
|
|
)
|
|
.unwrap();
|
|
let next = session
|
|
.execute("rect.invalidate(\"paint\")", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(next.frame.layout_version > initial.frame.layout_version);
|
|
assert!(next.frame.commands.iter().any(|command|matches!(command,RenderCommand::RoundedRect{width,..} if *width==next.frame.layout_version as f32)));
|
|
assert_eq!(source_measure_stat(&mut session, "sourceMeasureHits"), 0);
|
|
assert!(source_measure_stat(&mut session, "sourceMeasureMisses") >= 2);
|
|
}
|
|
|
|
#[test]
|
|
fn source_allocations_and_measurement_limits_settle_before_publication() {
|
|
let (mut session, initial) = ReplSession::load_with_viewport(
|
|
include_str!("../examples/layout-negotiation.endor"),
|
|
"app.main",
|
|
1,
|
|
240,
|
|
Some(crate::layout::Size {
|
|
width: 850.0,
|
|
height: 700.0,
|
|
}),
|
|
)
|
|
.unwrap();
|
|
let ids = initial
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.map(|m| m.id)
|
|
.collect::<Vec<_>>();
|
|
let width0 = session
|
|
.execute("paragraph.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value;
|
|
let width1 = session
|
|
.execute("paragraph.mounts().at(1).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value;
|
|
assert_eq!(width0, "280");
|
|
assert_eq!(width1, "360");
|
|
let height = session
|
|
.execute("paragraph.mounts().at(0).bounds().height", "app.main")
|
|
.unwrap()
|
|
.value
|
|
.parse::<f32>()
|
|
.unwrap();
|
|
let y = session
|
|
.execute("tail.mounts().at(0).bounds().y", "app.main")
|
|
.unwrap()
|
|
.value
|
|
.parse::<f32>()
|
|
.unwrap();
|
|
assert!(height > 20.0);
|
|
assert!(y >= height + 12.0);
|
|
assert!(source_measure_stat(&mut session, "layoutPasses") >= 2);
|
|
let changed = session
|
|
.execute("wide.click()", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert_eq!(
|
|
changed
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.map(|m| m.id)
|
|
.collect::<Vec<_>>(),
|
|
ids
|
|
);
|
|
let wider = session
|
|
.execute("paragraph.mounts().at(0).bounds().height", "app.main")
|
|
.unwrap()
|
|
.value
|
|
.parse::<f32>()
|
|
.unwrap();
|
|
let moved = session
|
|
.execute("tail.mounts().at(0).bounds().y", "app.main")
|
|
.unwrap()
|
|
.value
|
|
.parse::<f32>()
|
|
.unwrap();
|
|
assert!(wider < height && moved < y);
|
|
assert_eq!(
|
|
session
|
|
.execute("paragraph.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"560"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("paragraph.mounts().at(1).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"360"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn alternating_source_allocations_are_rejected_as_a_cycle() {
|
|
let source = r#"
|
|
class Alternating: Column {
|
|
function arrange(self: Alternating,mount: Mount,geometry: Bounds): Arrangement {
|
|
result=Column.arrange(self,mount,geometry)
|
|
result.children.at(0).width=geometry.height
|
|
result.children.at(0).height=geometry.width
|
|
return result
|
|
}
|
|
}
|
|
page=Page()
|
|
page.render(Alternating(0,0,RectBlock(120,60)))
|
|
"#;
|
|
let error = compile(source, "app.main", 1).unwrap_err();
|
|
assert!(
|
|
error.to_string().contains("did not converge (cycle)"),
|
|
"{error:#}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn source_text_paint_rejects_incomplete_wrap_bounds() {
|
|
let source = r##"
|
|
class Invalid: TextBlock {
|
|
function paint(self: Invalid,mount: Mount,geometry: Bounds): DisplayList {
|
|
command=PaintCommand()
|
|
command.kind="text"
|
|
command.x=0
|
|
command.y=0
|
|
command.text="invalid"
|
|
command.size=1
|
|
command.color="#000000"
|
|
command.wrapWidth=40
|
|
return DisplayList(ObjectList(command))
|
|
}
|
|
}
|
|
page=Page()
|
|
page.render(Invalid("invalid"))
|
|
"##;
|
|
let error = compile(source, "app.main", 1).unwrap_err();
|
|
assert!(error.to_string().contains("wrapHeight"), "{error:#}");
|
|
}
|
|
|
|
#[test]
|
|
fn wrapped_text_tracks_each_occurrence_width_resize_and_sibling_geometry() {
|
|
let text = "中文段落随着宽度变化而换行,后面的对象跟随段落高度移动。".repeat(8);
|
|
for custom in [false, true] {
|
|
let constructor = if custom { "Wrapped" } else { "TextBlock" };
|
|
let source = format!(
|
|
r#"
|
|
class Wrapped: TextBlock {{
|
|
function measure(self: Wrapped,mount: Mount,constraints: Constraints): MeasureResult {{return TextBlock.measure(self,mount,constraints)}}
|
|
function paint(self: Wrapped,mount: Mount,geometry: Bounds): DisplayList {{return TextBlock.paint(self,mount,geometry)}}
|
|
}}
|
|
paragraph={constructor}("{text}")
|
|
tail=TextBlock("tail")
|
|
page=Page()
|
|
page.render(Column(0,8,Column(0,8,paragraph,tail),Column(50,8,paragraph)))
|
|
"#
|
|
);
|
|
let (mut session, initial) = ReplSession::load_with_viewport(
|
|
&source,
|
|
"app.main",
|
|
1,
|
|
230,
|
|
Some(crate::layout::Size {
|
|
width: 240.0,
|
|
height: 2000.0,
|
|
}),
|
|
)
|
|
.unwrap();
|
|
let ids: Vec<_> = initial.frame.mounts.iter().map(|m| m.id).collect();
|
|
let commands: Vec<_> = initial
|
|
.frame
|
|
.commands
|
|
.iter()
|
|
.filter_map(|command| match command {
|
|
RenderCommand::Text {
|
|
value,
|
|
wrap_bounds: Some(bounds),
|
|
..
|
|
} if value == &text => Some(*bounds),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
assert_eq!(commands.len(), 2);
|
|
assert_eq!(commands[0][0], 240.0);
|
|
assert_eq!(commands[1][0], 140.0);
|
|
assert!(commands[1][1] > commands[0][1]);
|
|
let tail = initial
|
|
.frame
|
|
.commands
|
|
.iter()
|
|
.find_map(|command| match command {
|
|
RenderCommand::Text { value, y, .. } if value == "tail" => Some(*y),
|
|
_ => None,
|
|
})
|
|
.unwrap();
|
|
assert!(tail >= commands[0][1] + 8.0);
|
|
let resized = session
|
|
.resize(Some(crate::layout::Size {
|
|
width: 400.0,
|
|
height: 2000.0,
|
|
}))
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(
|
|
ids,
|
|
resized
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.map(|m| m.id)
|
|
.collect::<Vec<_>>()
|
|
);
|
|
let heights: Vec<_> = resized
|
|
.frame
|
|
.commands
|
|
.iter()
|
|
.filter_map(|command| match command {
|
|
RenderCommand::Text {
|
|
value,
|
|
wrap_bounds: Some(bounds),
|
|
..
|
|
} if value == &text => Some(bounds[1]),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
assert!(heights[0] < commands[0][1]);
|
|
let direct = session
|
|
.execute(
|
|
"paragraph.measure(paragraph.mounts().at(0),Constraints(0,0,80,2000)).height",
|
|
"app.main",
|
|
)
|
|
.unwrap()
|
|
.value
|
|
.parse::<f32>()
|
|
.unwrap();
|
|
assert!(direct > heights[0]);
|
|
session
|
|
.execute("paragraph.setText(\"short\")", "app.main")
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("paragraph.mounts().count", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"2"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("paragraph.mounts().at(0).bounds().height", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"20"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn native_signatures_validate_registration_and_deliver_the_event_argument() {
|
|
let (mut session, _) = ReplSession::load(
|
|
include_str!("../examples/native-signatures.endor"),
|
|
"app.main",
|
|
1,
|
|
220,
|
|
)
|
|
.unwrap();
|
|
for invalid in [
|
|
r#"withEvent.on("click", ui.textOf)"#,
|
|
r#"withEvent.on("click", Page)"#,
|
|
r#"label.bind("text", received.set)"#,
|
|
r#"ui.after(ui,"bad",60000,received.set)"#,
|
|
"count.increment(1)",
|
|
] {
|
|
assert!(session.execute(invalid, "app.main").is_err(), "{invalid}");
|
|
assert!(
|
|
session.documents[&crate::window::MAIN_WINDOW]
|
|
.frame
|
|
.fault
|
|
.is_none()
|
|
);
|
|
}
|
|
let document = session
|
|
.execute("withEvent.click()", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(document.frame.commands.iter().any(
|
|
|command| matches!(command, RenderCommand::Text { value, .. } if value == "click")
|
|
));
|
|
assert_eq!(
|
|
session
|
|
.execute("received.value.kind", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
r#""click""#
|
|
);
|
|
session.execute("withoutEvent.click()", "app.main").unwrap();
|
|
assert_eq!(
|
|
session.execute("count.value", "app.main").unwrap().value,
|
|
"1"
|
|
);
|
|
assert!(session.poll_tasks().unwrap().is_none());
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(received) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("received".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("state")
|
|
};
|
|
let Value::Object(set) = session.candidate.vm.get_attr(received, "set").unwrap() else {
|
|
panic!("set")
|
|
};
|
|
assert_eq!(
|
|
session.candidate.vm.call_arity(set).unwrap(),
|
|
Some(endor_core::CallArity::exact(1))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn checked_fields_reject_native_constructor_side_effects_and_preserve_live_values() {
|
|
let (mut session, _) = ReplSession::load(
|
|
include_str!("../examples/field-contracts.endor"),
|
|
"app.main",
|
|
1,
|
|
210,
|
|
)
|
|
.unwrap();
|
|
for invalid in [
|
|
r#"counter.value = "wrong""#,
|
|
r#"CounterText("bad", "wrong")"#,
|
|
"Model(42)",
|
|
] {
|
|
let error = session.execute(invalid, "app.main").err().unwrap();
|
|
assert!(error.to_string().contains("field"), "{error:#}");
|
|
assert!(
|
|
session.documents[&crate::window::MAIN_WINDOW]
|
|
.frame
|
|
.fault
|
|
.is_none()
|
|
);
|
|
}
|
|
assert_eq!(
|
|
session.execute("counter.value", "app.main").unwrap().value,
|
|
"9"
|
|
);
|
|
session
|
|
.execute("class CheckedPage: Page { count: Number }", "app.main")
|
|
.unwrap();
|
|
let pages = session
|
|
.execute("ui.window().pages().count", "app.main")
|
|
.unwrap()
|
|
.value;
|
|
assert!(
|
|
session
|
|
.execute(r#"CheckedPage("wrong")"#, "app.main")
|
|
.is_err()
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("ui.window().pages().count", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
pages
|
|
);
|
|
let updated = session
|
|
.execute("button.click()", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(
|
|
updated.frame.commands.iter().any(
|
|
|command| matches!(command, RenderCommand::Text { value, .. } if value == "10")
|
|
)
|
|
);
|
|
assert_eq!(
|
|
session.execute("counter.value", "app.main").unwrap().value,
|
|
"10"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn checked_fields_follow_shared_block_forwarding_and_binding_writes() {
|
|
let (mut session, _) = ReplSession::load(
|
|
r#"
|
|
class Numeric: TextBlock { value: Number }
|
|
class Textual: TextBlock { value: String }
|
|
block = Numeric("old", 9)
|
|
alias = block
|
|
page = Page()
|
|
page.render(block)
|
|
"#,
|
|
"app.main",
|
|
1,
|
|
211,
|
|
)
|
|
.unwrap();
|
|
session
|
|
.execute(r#"block = Textual("new", "kept")"#, "app.main")
|
|
.unwrap();
|
|
assert!(session.execute("alias.value = 42", "app.main").is_err());
|
|
assert_eq!(
|
|
session.execute("alias.value", "app.main").unwrap().value,
|
|
r#""kept""#
|
|
);
|
|
session
|
|
.execute(r#"alias.value = "updated""#, "app.main")
|
|
.unwrap();
|
|
assert_eq!(
|
|
session.execute("block.value", "app.main").unwrap().value,
|
|
r#""updated""#
|
|
);
|
|
assert!(
|
|
session
|
|
.execute("alias.bind(\"value\", ui.viewport)", "app.main")
|
|
.is_err()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn positional_insert_preserves_existing_slots_and_mounts_and_rejects_invalid_graphs() {
|
|
let source = r#"head=TextBlock("head")
|
|
tail=TextBlock("tail")
|
|
list=Column(24,8,head,tail)
|
|
page=Page()
|
|
page.render(list)"#;
|
|
let (mut session, initial) = ReplSession::load(source, "app.main", 1, 200).unwrap();
|
|
session
|
|
.execute("held=tail.mounts().at(0)", "app.main")
|
|
.unwrap();
|
|
let id = session
|
|
.execute("held.id", "app.main")
|
|
.unwrap()
|
|
.value
|
|
.parse::<u64>()
|
|
.unwrap();
|
|
let old = initial
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|mount| mount.id == id)
|
|
.unwrap();
|
|
let inserted = session
|
|
.execute("slot=list.insert(1,TextBlock(\"middle\"))", "app.main")
|
|
.unwrap();
|
|
assert_eq!(inserted.value, "2");
|
|
let document = inserted.document.unwrap();
|
|
let new = document
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|mount| mount.id == id)
|
|
.unwrap();
|
|
assert!(new.y > old.y);
|
|
assert_eq!(
|
|
session
|
|
.execute("tail.mounts().at(0).id", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
id.to_string()
|
|
);
|
|
for invalid in [
|
|
"list.insert(4,head)",
|
|
"list.insert(0,list)",
|
|
"list.insert(-1,head)",
|
|
] {
|
|
assert!(session.execute(invalid, "app.main").is_err());
|
|
assert!(!session.is_faulted());
|
|
}
|
|
assert_eq!(
|
|
session.execute("held.mounted", "app.main").unwrap().value,
|
|
"true"
|
|
);
|
|
session.execute("list.remove(slot)", "app.main").unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("tail.mounts().at(0).id", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
id.to_string()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn tab_drop_preserves_page_identity_calls_source_methods_and_rejects_stale_targets() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
include_str!("../examples/tab-placement.endor"),
|
|
"app.main",
|
|
1,
|
|
201,
|
|
)
|
|
.unwrap();
|
|
session.take_window_commands().unwrap();
|
|
let page = initial.frame.tabs[0].id;
|
|
let input = crate::events::Input {
|
|
window: 1,
|
|
session: 201,
|
|
layout_version: initial.frame.layout_version,
|
|
mount: page,
|
|
kind: "tab-drop".into(),
|
|
point: None,
|
|
press_point: None,
|
|
delta: None,
|
|
screen_position: None,
|
|
tab_drop: Some(crate::events::TabDrop {
|
|
window: 1,
|
|
layout_version: initial.frame.layout_version,
|
|
index: 3,
|
|
}),
|
|
};
|
|
session
|
|
.execute("held=list.mounts().at(0)", "app.main")
|
|
.unwrap();
|
|
let mount = session.execute("held.id", "app.main").unwrap().value;
|
|
session.dispatch(&input).unwrap();
|
|
assert_eq!(
|
|
session.execute("calls.value", "app.main").unwrap().value,
|
|
"1"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("list.mounts().at(0).id", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
mount
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("ui.window().pages().at(2)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
session.execute("first", "app.main").unwrap().value
|
|
);
|
|
let input = crate::events::Input {
|
|
layout_version: session.documents[&1].frame.layout_version,
|
|
tab_drop: Some(crate::events::TabDrop {
|
|
window: 2,
|
|
layout_version: session.documents[&2].frame.layout_version,
|
|
index: 0,
|
|
}),
|
|
..input
|
|
};
|
|
let mut stale = input.clone();
|
|
stale.tab_drop.as_mut().unwrap().layout_version += 1;
|
|
assert!(!session.accepts_tab_drop(&stale));
|
|
assert!(session.dispatch(&stale).is_err());
|
|
assert!(!session.is_faulted());
|
|
session.dispatch(&input).unwrap();
|
|
assert_eq!(
|
|
session.execute("calls.value", "app.main").unwrap().value,
|
|
"3"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("first.window().id()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"2"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("other.pages().at(0)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
session.execute("first", "app.main").unwrap().value
|
|
);
|
|
assert_eq!(
|
|
session.execute("held.mounted", "app.main").unwrap().value,
|
|
"false"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn drop_respects_declined_source_moves_and_detach_queues_signed_desktop_position() {
|
|
let source = r#"class Declining: Page {
|
|
calls: Object
|
|
function moveToWindow(self: Declining, window: Window): Page { self.calls.increment() return self }
|
|
}
|
|
calls=ui.state(0)
|
|
page=Declining(calls)
|
|
page.render(TextBlock("source"))
|
|
other=Window()
|
|
target=Page()
|
|
target.render(TextBlock("target"))
|
|
target.open(other)"#;
|
|
let (mut session, initial) = ReplSession::load(source, "app.main", 1, 202).unwrap();
|
|
session.take_window_commands().unwrap();
|
|
let mut input = crate::events::Input {
|
|
window: 1,
|
|
session: 202,
|
|
layout_version: initial.frame.layout_version,
|
|
mount: initial.frame.tabs[0].id,
|
|
kind: "tab-drop".into(),
|
|
point: None,
|
|
press_point: None,
|
|
delta: None,
|
|
screen_position: None,
|
|
tab_drop: Some(crate::events::TabDrop {
|
|
window: 2,
|
|
layout_version: initial.windows[&2].frame.layout_version,
|
|
index: 0,
|
|
}),
|
|
};
|
|
session.dispatch(&input).unwrap();
|
|
assert_eq!(
|
|
session.execute("calls.value", "app.main").unwrap().value,
|
|
"1"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("page.window().id()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"1"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("other.pages().count", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"1"
|
|
);
|
|
input.kind = "tab-detach".into();
|
|
input.tab_drop = None;
|
|
input.screen_position = Some([-400, 120]);
|
|
input.layout_version = session.documents[&1].frame.layout_version;
|
|
session.dispatch(&input).unwrap();
|
|
let commands = session.take_window_commands().unwrap();
|
|
assert!(commands.iter().any(|command| command.window == 3
|
|
&& command.action == crate::window::Action::MoveTo { x: -400, y: 120 }));
|
|
assert_eq!(
|
|
session
|
|
.execute("page.window().id()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"3"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn owned_tasks_replace_cancel_and_update_unmounted_objects() {
|
|
let source = r#"class Action {
|
|
state: Object
|
|
value: Number
|
|
function apply(self: Action): Unit { self.state.value = self.value }
|
|
}
|
|
state = ui.state(0)
|
|
hidden = TextBlock("not mounted")
|
|
first = Action(state, 1)
|
|
latest = Action(state, 2)
|
|
page = Page()
|
|
page.render(ui.textOf(state,"value"))
|
|
old = ui.after(hidden,"load",0,first.apply)
|
|
next = ui.after(hidden,"load",0,latest.apply)"#;
|
|
let (mut session, _) = ReplSession::load(source, "app.main", 1, 190).unwrap();
|
|
assert_eq!(
|
|
session.execute("old.status()", "app.main").unwrap().value,
|
|
"\"cancelled\""
|
|
);
|
|
assert_eq!(
|
|
session.execute("state.value", "app.main").unwrap().value,
|
|
"0"
|
|
);
|
|
assert!(session.poll_tasks().unwrap().is_some());
|
|
assert_eq!(
|
|
session.execute("state.value", "app.main").unwrap().value,
|
|
"2"
|
|
);
|
|
assert_eq!(
|
|
session.execute("next.status()", "app.main").unwrap().value,
|
|
"\"completed\""
|
|
);
|
|
session
|
|
.execute(
|
|
"cancelled=ui.after(ui,\"cancel\",0,first.apply) cancelled.cancel()",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
session.poll_tasks().unwrap();
|
|
assert_eq!(
|
|
session.execute("state.value", "app.main").unwrap().value,
|
|
"2"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("cancelled.active()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"false"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn tab_and_mount_tasks_cancel_while_block_and_app_owners_continue() {
|
|
let source = r#"class Action {
|
|
state: Object
|
|
function apply(self: Action): Unit { self.state.increment() }
|
|
}
|
|
state=ui.state(0)
|
|
action=Action(state)
|
|
leaf=TextBlock("owner")
|
|
page=Page()
|
|
page.render(leaf)"#;
|
|
let (mut session, _) = ReplSession::load(source, "app.main", 1, 191).unwrap();
|
|
session
|
|
.execute(
|
|
r#"held=leaf.mounts().at(0)
|
|
pageTask=ui.after(page,"page",0,action.apply)
|
|
mountTask=ui.after(held,"mount",0,action.apply)
|
|
blockTask=ui.after(leaf,"block",0,action.apply)
|
|
appTask=ui.after(ui,"app",0,action.apply)
|
|
page.close()
|
|
page.open()"#,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
for name in ["pageTask", "mountTask"] {
|
|
assert_eq!(
|
|
session
|
|
.execute(&format!("{name}.status()"), "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"\"cancelled\""
|
|
);
|
|
}
|
|
session.poll_tasks().unwrap();
|
|
assert_eq!(
|
|
session.execute("state.value", "app.main").unwrap().value,
|
|
"2"
|
|
);
|
|
// Moving a Page preserves its scope; the replaced Mount is cancelled.
|
|
session
|
|
.execute(
|
|
r#"moving=ui.after(page,"move",0,action.apply)
|
|
oldMount=leaf.mounts().at(0)
|
|
oldMountTask=ui.after(oldMount,"move",0,action.apply)
|
|
other=page.detachToWindow()"#,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("oldMountTask.status()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"\"cancelled\""
|
|
);
|
|
session.poll_tasks().unwrap();
|
|
assert_eq!(
|
|
session.execute("state.value", "app.main").unwrap().value,
|
|
"3"
|
|
);
|
|
session
|
|
.execute(
|
|
"closing=ui.after(other,\"close\",0,action.apply) other.close()",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
session.poll_tasks().unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("closing.status()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"\"cancelled\""
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn orphan_task_scope_is_collectable_and_callback_failure_stops_remaining_work() {
|
|
let source = r#"class Action {
|
|
state: Object
|
|
function apply(self: Action): Unit { self.state.increment() }
|
|
function fail(self: Action): Unit { ui.window().resize(0,600) }
|
|
}
|
|
function orphan(): Task {
|
|
let owner=TextBlock("temporary")
|
|
return ui.after(owner,"temporary",0,ui.window().focus)
|
|
}
|
|
state=ui.state(0)
|
|
action=Action(state)
|
|
page=Page()
|
|
page.render(TextBlock("preserve"))
|
|
token=orphan()"#;
|
|
let (mut session, _) = ReplSession::load(source, "app.main", 1, 192).unwrap();
|
|
assert_eq!(
|
|
session.execute("token.active()", "app.main").unwrap().value,
|
|
"false"
|
|
);
|
|
session.poll_tasks().unwrap();
|
|
assert_eq!(
|
|
session.execute("token.status()", "app.main").unwrap().value,
|
|
"\"cancelled\""
|
|
);
|
|
session.execute("broken=ui.after(ui,\"fail\",0,action.fail) later=ui.after(ui,\"later\",0,action.apply)","app.main").unwrap();
|
|
assert!(session.poll_tasks().is_err());
|
|
assert!(session.is_faulted());
|
|
let globals = session.candidate.globals.unwrap();
|
|
let state = match session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("state".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
{
|
|
Value::Object(state) => state,
|
|
_ => panic!("state object"),
|
|
};
|
|
assert_eq!(
|
|
session.candidate.vm.get_attr(state, "value").unwrap(),
|
|
Value::Number(0.into())
|
|
);
|
|
assert!(session.poll_tasks().unwrap().is_none());
|
|
assert!(session.execute("42", "app.main").is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn source_annotations_classify_functions_and_reject_before_body() {
|
|
let (mut session, _) = ReplSession::load(
|
|
include_str!("../examples/function-types.endor"),
|
|
"app.main",
|
|
1,
|
|
180,
|
|
)
|
|
.unwrap();
|
|
for expression in [
|
|
"isInstance(identity,Function)",
|
|
"isInstance(ui.rect,Function)",
|
|
"isInstance(actions.read,Function)",
|
|
] {
|
|
assert_eq!(
|
|
session.execute(expression, "app.main").unwrap().value,
|
|
"true"
|
|
);
|
|
}
|
|
assert_eq!(
|
|
session
|
|
.execute("isInstance(RectBlock,Function)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"false"
|
|
);
|
|
session
|
|
.execute(
|
|
"function checked(value: Number): Number { state.increment() return value }",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
let error = session
|
|
.execute("checked(\"wrong\")", "app.main")
|
|
.err()
|
|
.unwrap();
|
|
assert!(
|
|
error.to_string().contains("parameter value expects Number"),
|
|
"{error:#}"
|
|
);
|
|
assert_eq!(
|
|
session.execute("state.value", "app.main").unwrap().value,
|
|
"0"
|
|
);
|
|
assert_eq!(
|
|
session.execute("checked(7)", "app.main").unwrap().value,
|
|
"7"
|
|
);
|
|
session
|
|
.execute("function wrong(): Number { return \"wrong\" }", "app.main")
|
|
.unwrap();
|
|
let error = session.execute("wrong()", "app.main").err().unwrap();
|
|
assert!(
|
|
error.to_string().contains("return expects Number"),
|
|
"{error:#}"
|
|
);
|
|
assert!(
|
|
session
|
|
.execute("callNumber(RectBlock)", "app.main")
|
|
.is_err()
|
|
);
|
|
session
|
|
.execute(
|
|
"class Actions { function read(self: Actions): Number { return 9 } }",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("callNumber(savedFunction)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"1"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("isInstance(actions,Actions)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"false"
|
|
);
|
|
session.execute("button.click()", "app.main").unwrap();
|
|
assert_eq!(
|
|
session.execute("state.value", "app.main").unwrap().value,
|
|
"2"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn stopped_window_teardown_detaches_mounts_without_running_source_hooks() {
|
|
let source = r#"class WatchedPage: Page {
|
|
marker: Object
|
|
function onUnmount(self: WatchedPage, mount: Mount): Unit { self.marker.increment() }
|
|
}
|
|
class Failure {
|
|
function run(self: Failure, event: Event): Unit { ui.window().resize(0,600) }
|
|
}
|
|
marker=ui.state(0)
|
|
mainPage=Page()
|
|
mainPage.render(TextBlock("main"))
|
|
other=Window()
|
|
button=Button("fail")
|
|
failure=Failure()
|
|
connection=button.on("click",failure.run)
|
|
page=WatchedPage(marker)
|
|
page.render(button)
|
|
page.open(other)"#;
|
|
let (mut session, initial) = ReplSession::load(source, "app.main", 1, 140).unwrap();
|
|
session
|
|
.execute("held=button.mounts().at(0)", "app.main")
|
|
.unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let lookup = |session: &ReplSession, name: &str| match session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text(name.into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
{
|
|
Value::Object(object) => object,
|
|
_ => panic!("object"),
|
|
};
|
|
let held = lookup(&session, "held");
|
|
let window = lookup(&session, "other");
|
|
let marker = lookup(&session, "marker");
|
|
let mount = session
|
|
.candidate
|
|
.vm
|
|
.payload::<crate::mount::Mount>(held, "Mount")
|
|
.unwrap()
|
|
.geometry
|
|
.id;
|
|
assert!(
|
|
session
|
|
.dispatch(&crate::events::Input {
|
|
window: 2,
|
|
session: 140,
|
|
layout_version: initial.windows[&2].frame.layout_version,
|
|
mount,
|
|
kind: "click".into(),
|
|
point: None,
|
|
delta: None,
|
|
press_point: None,
|
|
tab_drop: None,
|
|
screen_position: None,
|
|
})
|
|
.is_err()
|
|
);
|
|
assert!(session.is_faulted());
|
|
session.retire_window(2).unwrap();
|
|
assert!(!session.accepts_window(2));
|
|
assert!(
|
|
!session
|
|
.candidate
|
|
.vm
|
|
.payload::<crate::mount::Mount>(held, "Mount")
|
|
.unwrap()
|
|
.mounted
|
|
);
|
|
assert!(
|
|
crate::window::pages(session.candidate.vm.object_store(), window)
|
|
.unwrap()
|
|
.is_empty()
|
|
);
|
|
assert_eq!(
|
|
session.candidate.vm.get_attr(marker, "value").unwrap(),
|
|
Value::Number(0.into())
|
|
);
|
|
assert!(session.execute("42", "app.main").is_err());
|
|
}
|
|
|
|
#[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()
|
|
first.title = "First"
|
|
first.render(TextBlock("one"))
|
|
second = Page()
|
|
second.title = "Second"
|
|
second.render(TextBlock("two"))"#;
|
|
let (mut session, initial) = ReplSession::load(source, "app.main", 120, 1).unwrap();
|
|
assert_eq!(initial.frame.tabs.len(), 2);
|
|
assert_eq!(initial.frame.tabs[0].title, "First");
|
|
assert!(initial.frame.tabs[1].active);
|
|
let input = crate::events::Input {
|
|
window: crate::window::MAIN_WINDOW,
|
|
session: 120,
|
|
layout_version: initial.frame.layout_version,
|
|
mount: initial.frame.tabs[0].id,
|
|
kind: "tab-activate".into(),
|
|
point: None,
|
|
delta: None,
|
|
press_point: None,
|
|
tab_drop: None,
|
|
screen_position: None,
|
|
};
|
|
let selected = session.dispatch(&input).unwrap().document.unwrap();
|
|
assert!(selected.frame.tabs[0].active);
|
|
assert!(selected.frame.layout_version > initial.frame.layout_version);
|
|
// Query the bridge directly: stale transport must be rejected before faulting a runtime.
|
|
assert!(
|
|
session
|
|
.bridge
|
|
.dispatch(&mut session.candidate.vm, &input)
|
|
.is_err()
|
|
);
|
|
let renamed = session
|
|
.execute(r#"second.title = "Renamed""#, "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert_eq!(renamed.frame.tabs[1].title, "Renamed");
|
|
assert_eq!(renamed.frame.tabs[0].id, initial.frame.tabs[0].id);
|
|
let closed = session
|
|
.dispatch(&crate::events::Input {
|
|
window: crate::window::MAIN_WINDOW,
|
|
session: 120,
|
|
layout_version: renamed.frame.layout_version,
|
|
mount: renamed.frame.tabs[1].id,
|
|
kind: "tab-close".into(),
|
|
point: None,
|
|
delta: None,
|
|
press_point: None,
|
|
tab_drop: None,
|
|
screen_position: None,
|
|
})
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert_eq!(closed.frame.tabs.len(), 1);
|
|
assert!(closed.frame.tabs[0].active);
|
|
assert_eq!(
|
|
session
|
|
.execute("second.window()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"nil"
|
|
);
|
|
let empty = session
|
|
.execute("first.close()", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(empty.frame.tabs.is_empty());
|
|
assert!(empty.frame.layout_version > closed.frame.layout_version);
|
|
let reopened = session
|
|
.execute("second.open()", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert_eq!(reopened.frame.tabs.len(), 1);
|
|
assert!(reopened.frame.tabs[0].id > initial.frame.tabs[1].id);
|
|
}
|
|
|
|
#[test]
|
|
fn managed_window_requests_are_coalesced_and_failed_input_discards_pending_requests() {
|
|
let mut session = ReplSession::new(111).unwrap();
|
|
session
|
|
.execute(
|
|
r#"window = ui.window()
|
|
window.resize(900,600)
|
|
window.resize(1000,700)
|
|
window.setTitle("Endor")
|
|
window.focus()"#,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
let requests = session.take_window_commands().unwrap();
|
|
assert_eq!(requests.len(), 3);
|
|
assert!(matches!(
|
|
requests[0].action,
|
|
crate::window::Action::Resize {
|
|
width: 1000.0,
|
|
height: 700.0
|
|
}
|
|
));
|
|
assert!(
|
|
matches!(&requests[1].action,crate::window::Action::SetTitle(title) if title=="Endor")
|
|
);
|
|
assert!(session.take_window_commands().unwrap().is_empty());
|
|
assert!(
|
|
session
|
|
.execute("window.resize(1400,900)\nwindow.resize(0,600)", "app.main")
|
|
.is_err()
|
|
);
|
|
assert!(session.take_window_commands().unwrap().is_empty());
|
|
assert!(session.execute("window.resize(0,600)", "app.main").is_err());
|
|
assert!(session.take_window_commands().unwrap().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn window_page_roots_follow_open_close_and_creation_order() {
|
|
let source = r#"first = Page()
|
|
second = Page()
|
|
first.render(TextBlock("first"))
|
|
second.render(TextBlock("second"))
|
|
window = ui.window()"#;
|
|
let (mut session, _) = ReplSession::load(source, "app.main", 112, 1).unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("window.pages().count", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"2"
|
|
);
|
|
let first = session.execute("first.window()", "app.main").unwrap().value;
|
|
assert_eq!(
|
|
first,
|
|
session
|
|
.execute("second.window()", "app.main")
|
|
.unwrap()
|
|
.value
|
|
);
|
|
session.execute("first.close()", "app.main").unwrap();
|
|
assert_eq!(
|
|
session.execute("first.window()", "app.main").unwrap().value,
|
|
"nil"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("window.pages().count", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"1"
|
|
);
|
|
session.execute("first.open(window)", "app.main").unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("window.pages().count", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"2"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("window.pages().at(0)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
session.execute("second", "app.main").unwrap().value
|
|
);
|
|
assert!(session.execute("first.open(Object())", "app.main").is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn closed_page_is_not_kept_alive_by_window_page_order() {
|
|
let (mut session, _) = ReplSession::load(
|
|
"page = Page()\npage.render(TextBlock(\"page\"))",
|
|
"app.main",
|
|
113,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(page) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("page".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("Page")
|
|
};
|
|
let weak = session.candidate.vm.downgrade(page).unwrap();
|
|
session.execute("page.close()", "app.main").unwrap();
|
|
session
|
|
.candidate
|
|
.vm
|
|
.dict_delete(globals, &Value::Text("page".into()))
|
|
.unwrap();
|
|
session.candidate.vm.collect_cycles();
|
|
assert!(session.candidate.vm.upgrade(weak).unwrap().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn rendering_cannot_enqueue_native_window_side_effects() {
|
|
let source = r##"class BadRect: RectBlock {
|
|
function measure(self: BadRect, mount: Mount, constraints: Constraints): MeasureResult {
|
|
ui.window().resize(900,600)
|
|
return MeasureResult(80,20)
|
|
}
|
|
}
|
|
page = Page()
|
|
page.render(BadRect(80,20,0,"#DDEEFF"))"##;
|
|
let error = ReplSession::load(source, "app.main", 114, 1).err().unwrap();
|
|
assert!(
|
|
error
|
|
.to_string()
|
|
.contains("window operations are forbidden")
|
|
);
|
|
}
|
|
|
|
fn source_measure_stat(session: &mut ReplSession, name: &str) -> i64 {
|
|
let Value::Object(function) = session
|
|
.candidate
|
|
.vm
|
|
.get_attr(session.bridge.ui, "stats")
|
|
.unwrap()
|
|
else {
|
|
panic!("stats function")
|
|
};
|
|
let Value::Object(stats) = session.candidate.vm.call(function, &[]).unwrap() else {
|
|
panic!("stats")
|
|
};
|
|
let Value::Number(value) = session.candidate.vm.get_attr(stats, name).unwrap() else {
|
|
panic!("stat number")
|
|
};
|
|
session.candidate.vm.release(stats).unwrap();
|
|
value.as_i64().unwrap()
|
|
}
|
|
fn click_named(
|
|
session: &mut ReplSession,
|
|
document: &RenderDocument,
|
|
name: &str,
|
|
) -> RenderDocument {
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(block) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text(name.into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("Block")
|
|
};
|
|
let mount = document
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|mount| mount.block_id == block.to_string())
|
|
.unwrap();
|
|
session
|
|
.dispatch(&crate::events::Input {
|
|
window: crate::window::MAIN_WINDOW,
|
|
session: session.id,
|
|
layout_version: document.frame.layout_version,
|
|
mount: mount.id,
|
|
kind: "click".into(),
|
|
point: None,
|
|
delta: None,
|
|
press_point: None,
|
|
tab_drop: None,
|
|
screen_position: None,
|
|
})
|
|
.unwrap()
|
|
.document
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn source_measure_cache_reuses_independent_results_and_invalidates_model_changes() {
|
|
let (mut session, initial) = ReplSession::load_with_viewport(
|
|
include_str!("../examples/layout-cache.endor"),
|
|
"app.main",
|
|
101,
|
|
1,
|
|
Some(crate::layout::Size {
|
|
width: 800.0,
|
|
height: 600.0,
|
|
}),
|
|
)
|
|
.unwrap();
|
|
assert_eq!(source_measure_stat(&mut session, "sourceMeasureMisses"), 2);
|
|
let painted = click_named(&mut session, &initial, "blue");
|
|
assert!(painted.frame.commands.iter().any(
|
|
|command| matches!(command,RenderCommand::RoundedRect {color,..} if color=="#526AC7")
|
|
));
|
|
let passes = source_measure_stat(&mut session, "layoutPasses");
|
|
assert_eq!(
|
|
source_measure_stat(&mut session, "sourceMeasureHits"),
|
|
2 * passes
|
|
);
|
|
assert_eq!(source_measure_stat(&mut session, "measured"), 0);
|
|
let resized = click_named(&mut session, &painted, "expand");
|
|
assert!(
|
|
resized
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.any(|mount| mount.width == 260.0 && mount.height == 60.0)
|
|
);
|
|
let passes = source_measure_stat(&mut session, "layoutPasses");
|
|
assert_eq!(
|
|
source_measure_stat(&mut session, "sourceMeasureHits"),
|
|
2 * passes - 1
|
|
);
|
|
assert_eq!(source_measure_stat(&mut session, "sourceMeasureMisses"), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn viewport_updates_in_a_fresh_console_do_not_replace_the_preserved_page() {
|
|
let mut session = ReplSession::new_with_viewport(
|
|
104,
|
|
Some(crate::layout::Size {
|
|
width: 800.0,
|
|
height: 600.0,
|
|
}),
|
|
)
|
|
.unwrap();
|
|
assert!(
|
|
session
|
|
.execute("42", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.is_none()
|
|
);
|
|
assert!(
|
|
session
|
|
.resize(Some(crate::layout::Size {
|
|
width: 360.0,
|
|
height: 240.0
|
|
}))
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
let result = session.execute("ui.viewport().width", "app.main").unwrap();
|
|
assert_eq!(result.value, "360");
|
|
assert!(result.document.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn source_measure_cache_tracks_fields_of_an_existing_returned_result() {
|
|
let source = r##"class HeldRect: RectBlock {
|
|
result: MeasureResult
|
|
function measure(self: HeldRect, mount: Mount, constraints: Constraints): MeasureResult { return self.result }
|
|
}
|
|
result = MeasureResult(80,20)
|
|
rect = HeldRect(80,20,0,"#DDEEFF",result)
|
|
page = Page()
|
|
page.render(rect)"##;
|
|
let (mut session, initial) = ReplSession::load(source, "app.main", 103, 1).unwrap();
|
|
let id = initial
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|mount| mount.width == 80.0)
|
|
.unwrap()
|
|
.id;
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(result) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("result".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("result")
|
|
};
|
|
session
|
|
.candidate
|
|
.vm
|
|
.set_attr(result, "width", Value::Number(140.into()))
|
|
.unwrap();
|
|
let next = session.refresh_document().unwrap().unwrap();
|
|
assert!(
|
|
next.frame
|
|
.mounts
|
|
.iter()
|
|
.any(|mount| mount.id == id && mount.width == 140.0)
|
|
);
|
|
assert_eq!(source_measure_stat(&mut session, "sourceMeasureMisses"), 1);
|
|
assert!(session.refresh_document().unwrap().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn viewport_resize_changes_constraints_and_preserves_mount_identity_without_source_input() {
|
|
let (mut session, initial) = ReplSession::load_with_viewport(
|
|
include_str!("../examples/layout-cache.endor"),
|
|
"app.main",
|
|
102,
|
|
1,
|
|
Some(crate::layout::Size {
|
|
width: 800.0,
|
|
height: 600.0,
|
|
}),
|
|
)
|
|
.unwrap();
|
|
let ids: Vec<_> = initial.frame.mounts.iter().map(|mount| mount.id).collect();
|
|
let document = session
|
|
.resize(Some(crate::layout::Size {
|
|
width: 360.0,
|
|
height: 240.0,
|
|
}))
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(
|
|
ids,
|
|
document
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.map(|mount| mount.id)
|
|
.collect::<Vec<_>>()
|
|
);
|
|
assert!(
|
|
document
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.any(|mount| mount.width == 312.0 && mount.height == 24.0)
|
|
);
|
|
assert_eq!(session.inputs, 0);
|
|
assert!(
|
|
session
|
|
.resize(Some(crate::layout::Size {
|
|
width: 360.0,
|
|
height: 240.0
|
|
}))
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
assert!(
|
|
session
|
|
.resize(Some(crate::layout::Size {
|
|
width: f32::NAN,
|
|
height: 240.0
|
|
}))
|
|
.is_err()
|
|
);
|
|
assert!(!session.is_faulted());
|
|
}
|
|
|
|
const SOURCE_LAYOUT: &str = include_str!("../examples/source-layout.endor");
|
|
|
|
#[test]
|
|
fn source_layout_uses_initial_mount_links_child_measurements_and_model_dependencies() {
|
|
let (mut session, initial) = ReplSession::load(SOURCE_LAYOUT, "app.main", 91, 1).unwrap();
|
|
let rect = initial
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|mount| mount.width == 200.0 && mount.height == 60.0)
|
|
.unwrap();
|
|
assert_eq!((rect.x, rect.y), (44.0, 24.0));
|
|
let id = rect.id;
|
|
assert!(initial.frame.commands.iter().any(|command| matches!(command,RenderCommand::Text { value,y,.. } if value.starts_with("尺寸来自") && *y == 100.0)));
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(metrics) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("metrics".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("Metrics")
|
|
};
|
|
session
|
|
.candidate
|
|
.vm
|
|
.set_attr(metrics, "width", Value::Number(260.into()))
|
|
.unwrap();
|
|
let next = session.refresh_document().unwrap().unwrap();
|
|
let rect = next
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|mount| mount.id == id)
|
|
.unwrap();
|
|
assert_eq!((rect.x, rect.width), (44.0, 260.0));
|
|
assert!(session.refresh_document().unwrap().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn shared_block_source_measurements_belong_to_each_occurrence() {
|
|
let source = SOURCE_LAYOUT.replace(
|
|
"page.render(root)",
|
|
r#"otherMetrics = Metrics(140, 12)
|
|
other = OffsetColumn(24, 16, rect, otherMetrics)
|
|
page.render(Row(0, 0, root, other))"#,
|
|
);
|
|
let (mut session, initial) = ReplSession::load(&source, "app.main", 92, 1).unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(rect) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("rect".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("Rect")
|
|
};
|
|
let widths: Vec<_> = initial
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.filter(|mount| mount.block_id == rect.to_string())
|
|
.map(|mount| mount.width)
|
|
.collect();
|
|
assert_eq!(widths, vec![200.0, 140.0]);
|
|
assert!(session.refresh_document().unwrap().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn layout_dependencies_changed_by_mount_hook_are_measured_again_before_publication() {
|
|
let source = r##"class Metrics { width: Number }
|
|
class InitialRect: RectBlock {
|
|
metrics: Metrics
|
|
function measure(self: InitialRect, mount: Mount, constraints: Constraints): MeasureResult {
|
|
return MeasureResult(self.metrics.width, 20)
|
|
}
|
|
function onMount(self: InitialRect, mount: Mount): Unit { self.metrics.width = 140 }
|
|
}
|
|
metrics = Metrics(80)
|
|
rect = InitialRect(80,20,0,"#DDEEFF",metrics)
|
|
page = Page()
|
|
page.render(rect)"##;
|
|
let (mut session, document) = ReplSession::load(source, "app.main", 93, 1).unwrap();
|
|
assert!(
|
|
document
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.any(|mount| mount.width == 140.0 && mount.height == 20.0)
|
|
);
|
|
assert!(session.refresh_document().unwrap().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn source_layout_rejects_premature_geometry_writes_wrong_types_and_foreign_slots() {
|
|
for (before, after, message) in [
|
|
(
|
|
"return MeasureResult(parent.block.metrics.width, 60)",
|
|
"return mount.bounds()",
|
|
"geometry is unavailable during measurement",
|
|
),
|
|
(
|
|
"return MeasureResult(parent.block.metrics.width, 60)",
|
|
"parent.block.metrics.width = 260\nreturn MeasureResult(260,60)",
|
|
"read-only",
|
|
),
|
|
(
|
|
"return MeasureResult(parent.block.metrics.width, 60)",
|
|
"return Point(0,0)",
|
|
"wrong result type",
|
|
),
|
|
(
|
|
"positions.children.at(0).x = self.metrics.left",
|
|
"positions.children.at(0).slot = 999999",
|
|
"foreign child slot",
|
|
),
|
|
] {
|
|
let source = SOURCE_LAYOUT.replace(before, after);
|
|
let error = ReplSession::load(&source, "app.main", 94, 1).err().unwrap();
|
|
assert!(error.to_string().contains(message), "{error}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn source_hit_filters_shape_falls_back_and_requires_matching_press_release() {
|
|
for (press, release, expected_hits, expected_background) in [
|
|
([64.0, 64.0], [64.0, 64.0], "1", "0"),
|
|
([144.0, 64.0], [144.0, 64.0], "0", "1"),
|
|
([64.0, 64.0], [144.0, 64.0], "0", "0"),
|
|
([9999.0, 9999.0], [9999.0, 9999.0], "0", "0"),
|
|
] {
|
|
let (mut session, initial) = ReplSession::load(
|
|
include_str!("../examples/source-hit.endor"),
|
|
"app.main",
|
|
81,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
let circle = initial
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|mount| mount.interactive && mount.width == 160.0)
|
|
.unwrap();
|
|
session
|
|
.dispatch(&crate::events::Input {
|
|
window: crate::window::MAIN_WINDOW,
|
|
session: 81,
|
|
layout_version: initial.frame.layout_version,
|
|
mount: circle.id,
|
|
kind: "click".into(),
|
|
point: Some(release),
|
|
delta: None,
|
|
press_point: Some(press),
|
|
tab_drop: None,
|
|
screen_position: None,
|
|
})
|
|
.unwrap();
|
|
assert_eq!(
|
|
session.execute("hits.value", "app.main").unwrap().value,
|
|
expected_hits
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("background.value", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
expected_background
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn source_hit_rejects_wrong_result_and_business_writes_before_callbacks() {
|
|
for (body, message) in [
|
|
("return 1", "hitTest must return Bool"),
|
|
(
|
|
"self.hits.increment()\nreturn point.inCircle(40, 40, 40)",
|
|
"read-only",
|
|
),
|
|
] {
|
|
let source = include_str!("../examples/source-hit.endor")
|
|
.replace("return point.inCircle(40, 40, 40)", body);
|
|
let (mut session, initial) = ReplSession::load(&source, "app.main", 82, 1).unwrap();
|
|
let circle = initial
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|mount| mount.interactive && mount.width == 160.0)
|
|
.unwrap();
|
|
let error = session
|
|
.dispatch(&crate::events::Input {
|
|
window: crate::window::MAIN_WINDOW,
|
|
session: 82,
|
|
layout_version: initial.frame.layout_version,
|
|
mount: circle.id,
|
|
kind: "click".into(),
|
|
point: Some([64.0, 64.0]),
|
|
delta: None,
|
|
press_point: Some([64.0, 64.0]),
|
|
tab_drop: None,
|
|
screen_position: None,
|
|
})
|
|
.err()
|
|
.unwrap();
|
|
assert!(error.to_string().contains(message), "{error}");
|
|
assert!(session.is_faulted());
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(hits) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("hits".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("state")
|
|
};
|
|
assert_eq!(
|
|
session.candidate.vm.get_attr(hits, "value").unwrap(),
|
|
Value::Number(0.into())
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn ordinary_block_subscription_updates_interactivity_after_disconnect() {
|
|
let source = r##"hits = ui.state(0)
|
|
rect = RectBlock(80, 40, 0, "#DDEEFF")
|
|
token = rect.on("click", hits.increment)
|
|
page = Page()
|
|
page.render(rect)"##;
|
|
let (mut session, initial) = ReplSession::load(source, "app.main", 83, 1).unwrap();
|
|
assert!(initial.frame.mounts.iter().any(|mount| mount.interactive));
|
|
let result = session.execute("token.disconnect()", "app.main").unwrap();
|
|
assert!(
|
|
result
|
|
.document
|
|
.unwrap()
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.all(|mount| !mount.interactive)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn source_paint_can_rebind_old_plain_reference_to_new_result_without_mutating_it() {
|
|
let source = r##"class ReboundRect: RectBlock {
|
|
previous: ObjectList
|
|
function paint(self: ReboundRect, mount: Mount, geometry: Bounds): DisplayList {
|
|
drawing = self.previous
|
|
drawing = RectBlock.paint(self, mount, geometry)
|
|
return drawing
|
|
}
|
|
}
|
|
previous = ObjectList()
|
|
rect = ReboundRect(80, 40, 0, "#DDEEFF", previous)
|
|
page = Page()
|
|
page.render(rect)"##;
|
|
let (mut session, document) = ReplSession::load(source, "app.main", 75, 1).unwrap();
|
|
assert_eq!(document.frame.commands.len(), 1);
|
|
assert_eq!(
|
|
session.execute("previous.count", "app.main").unwrap().value,
|
|
"0"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn source_paint_overrides_native_commands_and_reacts_to_heap_field_writes() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
include_str!("../examples/source-paint.endor"),
|
|
"app.main",
|
|
71,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
initial.validate().unwrap();
|
|
assert_eq!(initial.frame.commands.iter().filter(|command| matches!(command, RenderCommand::RoundedRect { color, .. } if color == "#ED6A5A")).count(), 2);
|
|
assert!(initial.frame.commands.iter().any(
|
|
|command| matches!(command, RenderCommand::Text { color, .. } if color == "#FFFFFF")
|
|
));
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(rect) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("rect".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("Block")
|
|
};
|
|
// No Console/global binding writes or focus changes: only the property
|
|
// read by paint changes, proving the paint dependency is sufficient.
|
|
session
|
|
.candidate
|
|
.vm
|
|
.set_attr(rect, "tint", Value::Text("#526AC7".into()))
|
|
.unwrap();
|
|
let updated = session.refresh_document().unwrap().unwrap();
|
|
assert_eq!(updated.frame.commands.iter().filter(|command| matches!(command, RenderCommand::RoundedRect { color, .. } if color == "#526AC7")).count(), 2);
|
|
assert!(session.refresh_document().unwrap().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn source_paint_rejects_business_mutation_before_it_happens_and_restores_scope() {
|
|
let source = r##"class BadPaint: RectBlock {
|
|
tint: String
|
|
function paint(self: BadPaint, mount: Mount, geometry: Bounds): DisplayList {
|
|
self.tint = "#000000"
|
|
return RectBlock.paint(self, mount, geometry)
|
|
}
|
|
}
|
|
rect = BadPaint(100, 40, 0, "#DDEEFF", "#FF0000")
|
|
page = Page()
|
|
page.render(rect)"##;
|
|
let mut session = ReplSession::new(72).unwrap();
|
|
assert!(
|
|
session
|
|
.execute(source, "app.main")
|
|
.err()
|
|
.unwrap()
|
|
.to_string()
|
|
.contains("read-only")
|
|
);
|
|
assert!(session.is_faulted());
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(rect) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("rect".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("Block")
|
|
};
|
|
assert_eq!(
|
|
session.candidate.vm.get_attr(rect, "tint").unwrap(),
|
|
Value::Text("#FF0000".into())
|
|
);
|
|
session
|
|
.candidate
|
|
.vm
|
|
.set_attr(rect, "tint", Value::Text("#526AC7".into()))
|
|
.unwrap();
|
|
let bad_color =
|
|
include_str!("../examples/source-paint.endor").replace("#ED6A5A", "invalid color");
|
|
assert!(ReplSession::load(&bad_color, "app.main", 73, 1).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn source_paint_is_stable_when_mount_notifications_overflow_the_change_batch() {
|
|
let fixture = include_str!("../examples/source-paint.endor");
|
|
let mut source = fixture[..fixture.find("root = Column").unwrap()].to_owned();
|
|
source.push_str("root = Column(0, 0");
|
|
for _ in 0..1100 {
|
|
source.push_str(", rect");
|
|
}
|
|
source.push_str(")\npage = Page()\npage.render(root)");
|
|
let (mut session, document) = ReplSession::load(&source, "app.main", 74, 1).unwrap();
|
|
assert_eq!(document.frame.commands.len(), 1100);
|
|
assert!(session.refresh_document().unwrap().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn constructor_options_and_subclass_fields_have_independent_contracts() {
|
|
let (mut session, document) = ReplSession::load(
|
|
include_str!("../examples/constructor-contracts.endor"),
|
|
"app.main",
|
|
166,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
document.validate().unwrap();
|
|
for (expression, expected) in [
|
|
("rect.width", "180"),
|
|
("rect.radius", "0"),
|
|
("rect.tag", "\"省略 radius/color 后的字段\""),
|
|
("root.tag", "\"可变子项之后的字段\""),
|
|
("event.kind", "\"click\""),
|
|
("items.count", "2"),
|
|
("ui.isInstance(items.at(0), TaggedRect)", "true"),
|
|
] {
|
|
assert_eq!(
|
|
session.execute(expression, "app.main").unwrap().value,
|
|
expected,
|
|
"{expression}"
|
|
);
|
|
}
|
|
let blocks = session
|
|
.execute("ui.stats().blocks", "app.main")
|
|
.unwrap()
|
|
.value;
|
|
for (expression, expected) in [
|
|
("TaggedRect(\"bad\", 60, \"tag\")", "expects Number"),
|
|
("TaggedColumn(0, 0, 7, \"tag\")", "expects Block"),
|
|
("ui.rect(1, 2, \"bad\")", "expects Number"),
|
|
("ui.text(7)", "expects String"),
|
|
("TaggedEvent(7, \"tag\")", "expects String"),
|
|
("ui.isSubtype(7, Block)", "expects Type"),
|
|
] {
|
|
let error = session
|
|
.execute(expression, "app.main")
|
|
.err()
|
|
.expect("invalid typed constructor/API");
|
|
assert!(
|
|
error.to_string().contains(expected),
|
|
"{expression}: {error:#}"
|
|
);
|
|
}
|
|
assert_eq!(
|
|
session
|
|
.execute("ui.stats().blocks", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
blocks
|
|
);
|
|
session.execute("button.click()", "app.main").unwrap();
|
|
assert_eq!(
|
|
session.execute("rect.color", "app.main").unwrap().value,
|
|
"\"#237A45\""
|
|
);
|
|
for name in [
|
|
"open",
|
|
"present",
|
|
"text",
|
|
"rect",
|
|
"spacer",
|
|
"row",
|
|
"column",
|
|
"button",
|
|
"page",
|
|
"state",
|
|
"textOf",
|
|
"stats",
|
|
"viewport",
|
|
"window",
|
|
"isInstance",
|
|
"isSubtype",
|
|
"implement",
|
|
"implements",
|
|
"supports",
|
|
"after",
|
|
] {
|
|
let Value::Object(function) = session
|
|
.candidate
|
|
.vm
|
|
.get_attr(session.bridge.ui, name)
|
|
.unwrap()
|
|
else {
|
|
panic!("native Function")
|
|
};
|
|
let signature = session
|
|
.candidate
|
|
.vm
|
|
.object_store()
|
|
.native_signature(function)
|
|
.unwrap()
|
|
.expect("published UI signature");
|
|
signature
|
|
.validate(session.candidate.vm.object_store())
|
|
.unwrap();
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn source_overrides_accept_wider_inputs_and_more_specific_results() {
|
|
let source = include_str!("../examples/method-contracts.endor");
|
|
let (mut session, document) = ReplSession::load(source, "app.main", 164, 1).unwrap();
|
|
document.validate().unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("rect.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"180"
|
|
);
|
|
session.execute("button.click()", "app.main").unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("rect.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"260"
|
|
);
|
|
assert_eq!(
|
|
session.execute("count.value", "app.main").unwrap().value,
|
|
"1"
|
|
);
|
|
for (source, reason) in [
|
|
(
|
|
source.replace("mount: Object", "mount: Point"),
|
|
"override narrows a parameter type",
|
|
),
|
|
(
|
|
source.replace("): Sized {", "): Object {"),
|
|
"override widens the return type",
|
|
),
|
|
] {
|
|
let error = ReplSession::load(&source, "app.main", 165, 1)
|
|
.err()
|
|
.expect("invalid override");
|
|
assert!(error.to_string().contains(reason), "{error:#}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn nullable_page_methods_union_event_options_and_nominal_mount_bounds_compose() {
|
|
let (mut session, _) = ReplSession::load(
|
|
include_str!("../examples/visual-protocols.endor"),
|
|
"app.main",
|
|
163,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
for (expression, expected) in [
|
|
("context = rect.mounts().at(0)", None),
|
|
("ui.isInstance(context.bounds(), Bounds)", Some("true")),
|
|
("Point(0, 0).inside(context.bounds())", Some("false")),
|
|
(
|
|
"listener = root.on(\"click\", page.close, Point(0, 0).inCircle(0, 0, 1))",
|
|
None,
|
|
),
|
|
("listener.isConnected()", Some("true")),
|
|
("listener.disconnect()", Some("nil")),
|
|
("listener.isConnected()", Some("false")),
|
|
("page.close()", Some("nil")),
|
|
("page.window()", Some("nil")),
|
|
("page.open(page.window())", None),
|
|
("ui.isInstance(page.window(), Window)", Some("true")),
|
|
] {
|
|
let result = session.execute(expression, "app.main").unwrap();
|
|
if let Some(expected) = expected {
|
|
assert_eq!(result.value, expected, "{expression}");
|
|
}
|
|
}
|
|
let error = session
|
|
.execute("root.on(\"click\", page.close, 7)", "app.main")
|
|
.err()
|
|
.expect("invalid union input");
|
|
assert!(error.to_string().contains("Bool | String"), "{error:#}");
|
|
}
|
|
|
|
#[test]
|
|
fn native_contracts_reject_wrong_types_before_geometry_or_window_effects() {
|
|
let (mut session, _) = ReplSession::load(
|
|
include_str!("../examples/visual-protocols.endor"),
|
|
"app.main",
|
|
162,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
session
|
|
.execute("context = rect.mounts().at(0)", "app.main")
|
|
.unwrap();
|
|
for (expression, expected) in [
|
|
("rect.measure(context, Point(0, 0))", "expects Constraints"),
|
|
(
|
|
"rect.measure(Point(0, 0), Constraints(0, 0, 120, 80))",
|
|
"expects Mount",
|
|
),
|
|
("rect.paint(context, Point(0, 0))", "expects Bounds"),
|
|
(
|
|
"Point(0, 0).inside(Constraints(0, 0, 120, 80))",
|
|
"expects Bounds",
|
|
),
|
|
("Point(0, 0).inCircle(0, 0, \"bad\")", "expects Number"),
|
|
("ui.window().resize(\"bad\", 700)", "expects Number"),
|
|
("rect.setColor(7)", "expects String"),
|
|
] {
|
|
let error = session
|
|
.execute(expression, "app.main")
|
|
.err()
|
|
.expect("invalid native call");
|
|
assert!(
|
|
error.to_string().contains(expected),
|
|
"{expression}: {error:#}"
|
|
);
|
|
}
|
|
assert_eq!(
|
|
session.execute("results.color", "app.main").unwrap().value,
|
|
"\"#DDEEFF\""
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute(
|
|
"rect.measure(context, Constraints(0, 0, 120, 80)).width",
|
|
"app.main"
|
|
)
|
|
.unwrap()
|
|
.value,
|
|
"120"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn native_visual_methods_use_managed_values_and_match_real_geometry() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
include_str!("../examples/visual-protocols.endor"),
|
|
"app.main",
|
|
61,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
initial.validate().unwrap();
|
|
for (expression, expected) in [
|
|
("results.width", "120"),
|
|
("results.height", "60"),
|
|
("results.childX", "44"),
|
|
("results.childY", "54"),
|
|
("results.color", "\"#DDEEFF\""),
|
|
] {
|
|
assert_eq!(
|
|
session.execute(expression, "app.main").unwrap().value,
|
|
expected
|
|
);
|
|
}
|
|
assert!(initial.frame.commands.iter().any(|command| matches!(command, RenderCommand::RoundedRect { width: 200.0, height: 60.0, color, .. } if color == "#DDEEFF")));
|
|
session
|
|
.execute("context = rect.mounts().at(0)", "app.main")
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("rect.hitTest(context, Point(1, 1))", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("rect.hitTest(context, Point(-1, 1))", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"false"
|
|
);
|
|
assert!(
|
|
session
|
|
.execute(
|
|
"rect.measure(context, Constraints(200, 0, 100, 100))",
|
|
"app.main"
|
|
)
|
|
.is_err()
|
|
);
|
|
assert!(
|
|
session
|
|
.execute("root.paint(context, Bounds(0, 0, 10, 10))", "app.main")
|
|
.is_err()
|
|
);
|
|
session.execute("page.close()", "app.main").unwrap();
|
|
assert!(
|
|
session
|
|
.execute("rect.paint(context, Bounds(0, 0, 10, 10))", "app.main")
|
|
.is_err()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn explicit_invalidation_publishes_once_preserves_mount_and_remeasures_only_when_requested() {
|
|
let (mut session, _) = ReplSession::load(
|
|
include_str!("../examples/visual-protocols.endor"),
|
|
"app.main",
|
|
62,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
session
|
|
.execute("context = rect.mounts().at(0)", "app.main")
|
|
.unwrap();
|
|
let original = session.execute("context", "app.main").unwrap().value;
|
|
let update = session
|
|
.execute("rect.invalidate(\"paint\")", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
update.validate().unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("ui.stats().measured", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"0"
|
|
);
|
|
assert!(
|
|
session
|
|
.execute("rect.invalidate(\"layout\")", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.is_some()
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("ui.stats().measured", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"1"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("rect.mounts().at(0)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
original
|
|
);
|
|
assert!(
|
|
session
|
|
.execute("results.width", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.is_none()
|
|
);
|
|
assert!(
|
|
session
|
|
.execute("rect.invalidate(\"unknown\")", "app.main")
|
|
.is_err()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn mount_hooks_run_per_occurrence_settle_and_unmount_before_parent() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
include_str!("../examples/mount-lifecycle.endor"),
|
|
"app.main",
|
|
51,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
initial.validate().unwrap();
|
|
for (expression, expected) in [
|
|
("log.mounted.value", "2"),
|
|
("log.parentMounted", "1"),
|
|
("log.childMounted", "3"),
|
|
] {
|
|
assert_eq!(
|
|
session.execute(expression, "app.main").unwrap().value,
|
|
expected
|
|
);
|
|
}
|
|
session
|
|
.execute(
|
|
"saved = child.mounts().at(0)\nchild.setColor(\"#237A45\")",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("log.mounted.value", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"2"
|
|
);
|
|
let closed = session
|
|
.execute("page.close()", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(closed.frame.mounts.is_empty());
|
|
assert_eq!(
|
|
session
|
|
.execute("log.unmounted.value", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"2"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("log.childUnmounted", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"5"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("log.parentUnmounted", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"6"
|
|
);
|
|
assert_eq!(
|
|
session.execute("saved.mounted", "app.main").unwrap().value,
|
|
"false"
|
|
);
|
|
assert!(session.execute("saved.bounds()", "app.main").is_err());
|
|
session.execute("page.open()", "app.main").unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("log.mounted.value", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"4"
|
|
);
|
|
assert_eq!(
|
|
session.execute("saved.mounted", "app.main").unwrap().value,
|
|
"false"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn page_switch_replaces_mount_context_even_for_shared_root() {
|
|
let (mut session, _) = ReplSession::load(
|
|
include_str!("../examples/mount-lifecycle.endor"),
|
|
"app.main",
|
|
52,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
session
|
|
.execute(
|
|
"saved = child.mounts().at(0)\nsecond = Page()\nsecond.render(root)",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("log.mounted.value", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"4"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("log.unmounted.value", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"2"
|
|
);
|
|
assert_eq!(
|
|
session.execute("saved.mounted", "app.main").unwrap().value,
|
|
"false"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("child.mounts().at(0).page()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
session.execute("second", "app.main").unwrap().value
|
|
);
|
|
session.execute("page.activate()", "app.main").unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("log.mounted.value", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"6"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn lifecycle_hook_can_edit_container_without_session_lock_and_failure_stops_domain() {
|
|
let source = r#"class GrowingColumn: Column {
|
|
function onMount(self: GrowingColumn, mount: Mount): Unit { self.append(TextBlock("from hook")) }
|
|
}
|
|
root = GrowingColumn(0, 0)
|
|
page = Page()
|
|
page.render(root)"#;
|
|
let (_, document) = ReplSession::load(source, "app.main", 53, 1).unwrap();
|
|
assert!(document.frame.commands.iter().any(
|
|
|command| matches!(command, RenderCommand::Text { value, .. } if value == "from hook")
|
|
));
|
|
let mut session = ReplSession::new(54).unwrap();
|
|
let source = r#"class BrokenText: TextBlock {
|
|
function onMount(self: BrokenText, mount: Mount): Unit { self.setText(1) }
|
|
}
|
|
page = Page()
|
|
page.render(BrokenText("x"))"#;
|
|
assert!(session.execute(source, "app.main").is_err());
|
|
assert!(session.is_faulted());
|
|
}
|
|
|
|
fn event_session(extra: &str) -> (ReplSession, RenderDocument) {
|
|
let source = format!("{}\n{}", include_str!("../examples/events.endor"), extra);
|
|
ReplSession::load(&source, "app.main", 41, 1).unwrap()
|
|
}
|
|
|
|
fn click_document(session: &mut ReplSession, document: &RenderDocument) -> RenderDocument {
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(button) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("button".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("Button")
|
|
};
|
|
let mount = document
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|mount| mount.interactive && mount.block_id == button.to_string())
|
|
.unwrap();
|
|
session
|
|
.dispatch(&crate::events::Input {
|
|
window: crate::window::MAIN_WINDOW,
|
|
session: 41,
|
|
layout_version: document.frame.layout_version,
|
|
mount: mount.id,
|
|
kind: "click".into(),
|
|
point: None,
|
|
delta: None,
|
|
press_point: None,
|
|
tab_drop: None,
|
|
screen_position: None,
|
|
})
|
|
.unwrap()
|
|
.document
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn managed_event_route_orders_capture_method_listeners_and_bubble() {
|
|
let (mut session, initial) = event_session("");
|
|
click_document(&mut session, &initial).validate().unwrap();
|
|
for (field, expected) in [
|
|
("captureStep", "1"),
|
|
("methodStep", "2"),
|
|
("targetStep", "3"),
|
|
("bubbleStep", "4"),
|
|
] {
|
|
assert_eq!(
|
|
session
|
|
.execute(&format!("model.{field}"), "app.main")
|
|
.unwrap()
|
|
.value,
|
|
expected
|
|
);
|
|
}
|
|
assert_eq!(
|
|
session.execute("model.phase", "app.main").unwrap().value,
|
|
"\"bubble\""
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("target.isConnected()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn disconnect_during_capture_skips_pending_listener_and_stop_preserves_current_group() {
|
|
let (mut session, initial) = event_session(
|
|
"function cancel(event: Event): Unit { target.disconnect() }\nconnection = root.on(\"click\", cancel, \"capture\")",
|
|
);
|
|
click_document(&mut session, &initial);
|
|
assert_eq!(
|
|
session
|
|
.execute("model.targetStep", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"0"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("model.bubbleStep", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"3"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("target.isConnected()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"false"
|
|
);
|
|
let (mut session, initial) = event_session(
|
|
"function stop(event: Event): Unit { event.stopPropagation() }\nconnection = button.on(\"click\", stop, \"capture\")",
|
|
);
|
|
click_document(&mut session, &initial);
|
|
assert_eq!(
|
|
session
|
|
.execute("model.targetStep", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"3"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("model.bubbleStep", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"0"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn immediate_stop_skips_target_method_and_code_dispatch_returns_cancelled_event() {
|
|
let (mut session, initial) = event_session(
|
|
"function stop(event: Event): Unit { event.stopImmediatePropagation() }\nconnection = button.on(\"click\", stop, \"capture\")",
|
|
);
|
|
click_document(&mut session, &initial);
|
|
assert_eq!(
|
|
session
|
|
.execute("model.count.value", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"1"
|
|
);
|
|
let (mut session, _) = event_session(
|
|
"function prevent(event: Event): Unit { event.preventDefault() }\nconnection = button.on(\"custom\", prevent)\nresult = button.dispatch(Event(\"custom\"))",
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("result.isDefaultPrevented()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
assert_eq!(
|
|
session.execute("result.phase", "app.main").unwrap().value,
|
|
"\"none\""
|
|
);
|
|
// Reusing a completed event is legal; its previous dispatch has no live guard.
|
|
session
|
|
.execute("button.dispatch(result)", "app.main")
|
|
.unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn event_failure_releases_dispatch_guard_and_token_does_not_retain_target() {
|
|
let (mut session, _) = event_session("");
|
|
session.execute("function fail(event: Event): Unit { page.render(1) }\nfailing = button.on(\"failure\", fail)\nfailedEvent = Event(\"failure\")", "app.main").unwrap();
|
|
assert!(
|
|
session
|
|
.execute("button.dispatch(failedEvent)", "app.main")
|
|
.is_err()
|
|
);
|
|
session
|
|
.execute(
|
|
"failing.disconnect()\nbutton.dispatch(failedEvent)",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
session
|
|
.execute(
|
|
"orphan = Button(\"unmounted\")\ntoken = orphan.on(\"click\", model.target)",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(orphan) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("orphan".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("Block expected")
|
|
};
|
|
let weak = session.candidate.vm.downgrade(orphan).unwrap();
|
|
session
|
|
.candidate
|
|
.vm
|
|
.dict_delete(globals, &Value::Text("orphan".into()))
|
|
.unwrap();
|
|
session
|
|
.bridge
|
|
.reclaim(&mut session.candidate.vm, &mut session.candidate.roots)
|
|
.unwrap();
|
|
assert!(session.candidate.vm.upgrade(weak).unwrap().is_none());
|
|
assert_eq!(
|
|
session
|
|
.execute("token.isConnected()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"false"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn source_native_subclasses_share_live_fields_and_release_replaced_storage() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
include_str!("../examples/native-subclasses.endor"),
|
|
"app.main",
|
|
31,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
initial.validate().unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("isInstance(label, TextBlock)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("isInstance(page, Page)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
session.execute("alias = label", "app.main").unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(label) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("label".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("Block expected")
|
|
};
|
|
let path = session
|
|
.candidate
|
|
.vm
|
|
.object_store()
|
|
.attribute_path(label)
|
|
.unwrap();
|
|
let previous = session
|
|
.candidate
|
|
.vm
|
|
.downgrade(*path.last().unwrap())
|
|
.unwrap();
|
|
let updated = session
|
|
.execute(
|
|
r##"label = CustomStatus("replacement", 2, "#242422", "#237A45", "new field")"##,
|
|
"app.main",
|
|
)
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(updated.frame.commands.iter().any(
|
|
|command| matches!(command, RenderCommand::Text { value, .. } if value == "new field")
|
|
));
|
|
session
|
|
.bridge
|
|
.reclaim(&mut session.candidate.vm, &mut session.candidate.roots)
|
|
.unwrap();
|
|
assert!(session.candidate.vm.upgrade(previous).unwrap().is_none());
|
|
assert_eq!(
|
|
session.execute("alias.message", "app.main").unwrap().value,
|
|
"\"new field\""
|
|
);
|
|
let updated = session
|
|
.execute("button.click()", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert_eq!(updated.frame.commands.iter().filter(|command| matches!(command, RenderCommand::Text { value, .. } if value == "new field")).count(), 3);
|
|
let updated = session
|
|
.execute("alias.message = \"changed field\"", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(updated.frame.commands.iter().any(|command| matches!(command, RenderCommand::Text { value, .. } if value == "changed field")));
|
|
}
|
|
|
|
#[test]
|
|
fn loaded_program_console_and_native_events_share_unique_source_ids() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
include_str!("../examples/managed-types.endor"),
|
|
"app.main",
|
|
1,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
initial.validate().unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("isInstance(page, Block)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
let mount = initial
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|mount| mount.interactive)
|
|
.unwrap();
|
|
let event = crate::events::Input {
|
|
window: crate::window::MAIN_WINDOW,
|
|
session: 1,
|
|
layout_version: initial.frame.layout_version,
|
|
mount: mount.id,
|
|
kind: "click".into(),
|
|
point: None,
|
|
delta: None,
|
|
press_point: None,
|
|
tab_drop: None,
|
|
screen_position: None,
|
|
};
|
|
let updated = session.dispatch(&event).unwrap().document.unwrap();
|
|
updated.validate().unwrap();
|
|
assert_eq!(
|
|
updated
|
|
.frame
|
|
.commands
|
|
.iter()
|
|
.filter(|command| matches!(command,
|
|
RenderCommand::Text { value, .. } if value == "10"
|
|
))
|
|
.count(),
|
|
2
|
|
);
|
|
assert!(!session.is_faulted());
|
|
}
|
|
|
|
#[test]
|
|
fn scroll_occurrences_are_independent_and_clipped_pointer_input_is_ignored() {
|
|
let (mut session, initial) =
|
|
ReplSession::load(include_str!("../examples/scroll.endor"), "app.main", 1, 240)
|
|
.unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(middle) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("middle".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("expected Block");
|
|
};
|
|
let target = initial
|
|
.frame
|
|
.mounts
|
|
.iter()
|
|
.find(|m| m.block_id == middle.to_string())
|
|
.unwrap();
|
|
let point = [
|
|
target.x + target.width / 2.0,
|
|
target.y + target.height / 2.0,
|
|
];
|
|
assert!(!target.contains(crate::geometry::Point::new(point[0], point[1])));
|
|
session
|
|
.dispatch(&crate::events::Input {
|
|
window: 1,
|
|
session: 240,
|
|
layout_version: initial.frame.layout_version,
|
|
mount: target.id,
|
|
kind: "click".into(),
|
|
point: Some(point),
|
|
press_point: Some(point),
|
|
tab_drop: None,
|
|
screen_position: None,
|
|
delta: None,
|
|
})
|
|
.unwrap();
|
|
assert_eq!(
|
|
session.execute("count.value", "app.main").unwrap().value,
|
|
"0"
|
|
);
|
|
session.execute("saved = scroller.mounts().at(0)\nsaved.scrollTo(0,100)\nsaved.scrollBy(0,20)\nsaved.scrollBy(0,30)","app.main").unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("saved.scrollPosition().y", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"150"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("scroller.mounts().at(1).scrollPosition().y", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"0"
|
|
);
|
|
session
|
|
.execute("last.mounts().at(0).scrollIntoView()", "app.main")
|
|
.unwrap();
|
|
let Value::Object(last) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("last".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("expected Block");
|
|
};
|
|
let frame = &session.documents[&crate::window::MAIN_WINDOW].frame;
|
|
let target = frame
|
|
.mounts
|
|
.iter()
|
|
.find(|m| m.block_id == last.to_string())
|
|
.unwrap();
|
|
assert!(target.contains(crate::geometry::Point::new(
|
|
target.x + target.width / 2.0,
|
|
target.y + target.height / 2.0
|
|
)));
|
|
assert_eq!(frame.command_clips.len(), frame.commands.len());
|
|
session
|
|
.execute("scroller.height = 1000", "app.main")
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("saved.scrollPosition().y", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"0"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("saved.scrollPosition().maxY", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"0"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn nested_flex_refreshes_natural_height_when_cross_width_changes() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
r#"
|
|
class Narrow: Row {
|
|
function measure(self: Narrow, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(240, 400) }
|
|
}
|
|
paragraph = TextBlock("Changing the parent's width must refresh the natural height of this wrapping paragraph. The first wide measurement must not freeze a single line of text. Changing the parent's width must refresh the natural height of this wrapping paragraph.")
|
|
paragraph.flexShrink = 1
|
|
column = Column(0, 0, paragraph)
|
|
column.flex = 1
|
|
page = Page()
|
|
page.render(Narrow(10, 0, column))
|
|
"#,
|
|
"app.main",
|
|
268,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
initial.validate().unwrap();
|
|
let height: f32 = session
|
|
.execute("paragraph.mounts().at(0).bounds().height", "app.main")
|
|
.unwrap()
|
|
.value
|
|
.parse()
|
|
.unwrap();
|
|
assert!(height > 20.0);
|
|
session
|
|
.execute("paragraph.setText(\"short\")", "app.main")
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("paragraph.mounts().at(0).bounds().height", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"20"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn bounded_flex_redistributes_growth_and_scaled_shrink() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
r#"
|
|
class Wide: Row {
|
|
function measure(self: Wide, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(340, 60) }
|
|
}
|
|
first = RectBlock(20, 20)
|
|
second = RectBlock(40, 20)
|
|
fixed = RectBlock(60, 20)
|
|
first.flex = 1
|
|
second.flex = 3
|
|
first.flexBasis = 100
|
|
second.flexBasis = 100
|
|
first.flexMax = 105
|
|
row = Wide(10, 10, first, second, fixed)
|
|
page = Page()
|
|
page.render(row)
|
|
"#,
|
|
"app.main",
|
|
267,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
initial.validate().unwrap();
|
|
let width = |session: &mut ReplSession, name| {
|
|
session
|
|
.execute(&format!("{name}.mounts().at(0).bounds().width"), "app.main")
|
|
.unwrap()
|
|
.value
|
|
};
|
|
assert_eq!(width(&mut session, "first"), "105");
|
|
assert_eq!(width(&mut session, "second"), "135");
|
|
session.execute(
|
|
"first.flex = 0\nsecond.flex = 0\nfirst.flexMax = 300\nfirst.flexMin = 150\nfirst.flexBasis = 200\nsecond.flexBasis = 200\nfirst.flexShrink = 1\nsecond.flexShrink = 1",
|
|
"app.main",
|
|
).unwrap();
|
|
assert_eq!(width(&mut session, "first"), "150");
|
|
assert_eq!(width(&mut session, "second"), "90");
|
|
assert_eq!(width(&mut session, "fixed"), "60");
|
|
session.execute(
|
|
"first.flexMin = 0\nfirst.flexMax = 1000000\nfirst.flexBasis = \"auto\"\nsecond.flexBasis = \"auto\"",
|
|
"app.main",
|
|
).unwrap();
|
|
assert_eq!(width(&mut session, "first"), "20");
|
|
assert_eq!(width(&mut session, "second"), "40");
|
|
}
|
|
|
|
#[test]
|
|
fn native_flex_reflows_shared_text_per_occurrence() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
r#"
|
|
paragraph = TextBlock("Shared text has one object identity and separate layout constraints in each occurrence. Changing the sibling weight reflows only the affected occurrence.")
|
|
paragraph.flex = 1
|
|
filler = RectBlock(20, 20)
|
|
filler.flex = 1
|
|
first = Row(10, 10, paragraph, filler)
|
|
second = Row(10, 10, paragraph, RectBlock(300, 20))
|
|
page = Page()
|
|
page.render(Column(10, 10, first, second))
|
|
"#,
|
|
"app.main",
|
|
266,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
initial.validate().unwrap();
|
|
let height = |session: &mut ReplSession, index| -> f32 {
|
|
session
|
|
.execute(
|
|
&format!("paragraph.mounts().at({index}).bounds().height"),
|
|
"app.main",
|
|
)
|
|
.unwrap()
|
|
.value
|
|
.parse()
|
|
.unwrap()
|
|
};
|
|
let before = height(&mut session, 0);
|
|
let other = height(&mut session, 1);
|
|
assert!(before > other);
|
|
session.execute("filler.flex = 3", "app.main").unwrap();
|
|
assert!(height(&mut session, 0) > before);
|
|
assert_eq!(height(&mut session, 1), other);
|
|
}
|
|
|
|
#[test]
|
|
fn weighted_flex_remeasures_children_and_updates_live_weights() {
|
|
let (mut session, initial) = ReplSession::load(r#"
|
|
class Wide: Row {
|
|
function measure(self: Wide, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(340, 40) }
|
|
}
|
|
first = RectBlock(20, 20)
|
|
fixed = RectBlock(60, 20)
|
|
last = RectBlock(40, 20)
|
|
first.flex = 1
|
|
last.flex = 2
|
|
row = Wide(10, 10, first, fixed, last)
|
|
page = Page()
|
|
page.render(row)
|
|
"#, "app.main", 265, 1).unwrap();
|
|
initial.validate().unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("first.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"80"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("last.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"160"
|
|
);
|
|
session
|
|
.execute("first.flex = 3\nlast.flex = 1", "app.main")
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("first.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"180"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("last.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"60"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn main_alignment_updates_live_geometry_without_changing_sizes() {
|
|
let (mut session, initial) = ReplSession::load(r#"
|
|
class Wide: Row {
|
|
function measure(self: Wide, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(300, 60) }
|
|
}
|
|
first = RectBlock(20, 20)
|
|
second = RectBlock(40, 40)
|
|
row = Wide(10, 10, first, second)
|
|
page = Page()
|
|
page.render(row)
|
|
"#, "app.main", 264, 1).unwrap();
|
|
initial.validate().unwrap();
|
|
for (mode, first, second) in [
|
|
("start", "10", "40"),
|
|
("center", "115", "145"),
|
|
("end", "220", "250"),
|
|
("space-between", "10", "250"),
|
|
] {
|
|
session
|
|
.execute(&format!("row.mainAlign = \"{mode}\""), "app.main")
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("first.mounts().at(0).bounds().x", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
first
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("second.mounts().at(0).bounds().x", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
second
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("first.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"20"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn scroll_axes_wrap_shared_text_and_clamp_only_the_enabled_offsets() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
include_str!("../examples/scroll-axis.endor"),
|
|
"app.main",
|
|
260,
|
|
1,
|
|
)
|
|
.unwrap();
|
|
initial.validate().unwrap();
|
|
for expression in [
|
|
"vertical.mounts().at(0).scrollPosition().maxX",
|
|
"horizontal.mounts().at(0).scrollPosition().maxY",
|
|
] {
|
|
assert_eq!(session.execute(expression, "app.main").unwrap().value, "0");
|
|
}
|
|
session
|
|
.execute(
|
|
"vertical.mounts().at(0).scrollTo(100, 60)\nhorizontal.mounts().at(0).scrollTo(60, 100)",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
for (expression, expected) in [
|
|
("vertical.mounts().at(0).scrollPosition().x", "0"),
|
|
("vertical.mounts().at(0).scrollPosition().y", "60"),
|
|
("horizontal.mounts().at(0).scrollPosition().x", "60"),
|
|
("horizontal.mounts().at(0).scrollPosition().y", "0"),
|
|
] {
|
|
assert_eq!(
|
|
session.execute(expression, "app.main").unwrap().value,
|
|
expected
|
|
);
|
|
}
|
|
let vertical_width: f32 = session
|
|
.execute("paragraph.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value
|
|
.parse()
|
|
.unwrap();
|
|
let horizontal_width: f32 = session
|
|
.execute("paragraph.mounts().at(1).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value
|
|
.parse()
|
|
.unwrap();
|
|
assert!(vertical_width <= 244.0);
|
|
assert!(horizontal_width > vertical_width);
|
|
session
|
|
.execute("horizontal.axis = \"vertical\"", "app.main")
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("horizontal.mounts().at(0).scrollPosition().x", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"0"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn scroll_axis_defaults_to_both_and_rejects_unknown_policy() {
|
|
let mut session = ReplSession::new(262).unwrap();
|
|
session
|
|
.execute(
|
|
"scroller = ScrollBlock(20, 10, TextBlock(\"default\"))",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
session.execute("scroller.axis", "app.main").unwrap().value,
|
|
"\"both\""
|
|
);
|
|
let error = ReplSession::load(
|
|
r#"
|
|
scroller = ScrollBlock(20, 10, TextBlock("bad"))
|
|
scroller.axis = "diagonal"
|
|
page = Page()
|
|
page.render(scroller)
|
|
"#,
|
|
"app.main",
|
|
263,
|
|
1,
|
|
)
|
|
.err()
|
|
.expect("invalid axis");
|
|
assert!(
|
|
error
|
|
.to_string()
|
|
.contains("ScrollBlock.axis expects both, vertical or horizontal"),
|
|
"{error:#}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn scroll_axis_uses_source_allocated_viewport_instead_of_constructor_width() {
|
|
let source = r#"
|
|
class Allocator: Column {
|
|
function arrange(self: Allocator, mount: Mount, geometry: Bounds): Arrangement {
|
|
result = Column.arrange(self, mount, geometry)
|
|
result.children.at(0).width = 120
|
|
return result
|
|
}
|
|
}
|
|
text = TextBlock("This text must wrap inside the allocated viewport, not the original constructor width. This text must wrap inside the allocated viewport.")
|
|
scroller = ScrollBlock(300, 80, text)
|
|
scroller.padding = 10
|
|
scroller.axis = "vertical"
|
|
page = Page()
|
|
page.render(Allocator(0, 0, scroller))
|
|
"#;
|
|
let (mut session, initial) = ReplSession::load(source, "app.main", 261, 1).unwrap();
|
|
initial.validate().unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("scroller.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"120"
|
|
);
|
|
let width: f32 = session
|
|
.execute("text.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value
|
|
.parse()
|
|
.unwrap();
|
|
assert!(width <= 100.0);
|
|
assert_eq!(
|
|
session
|
|
.execute("scroller.mounts().at(0).scrollPosition().maxX", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"0"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn nested_scroll_into_view_and_source_paint_share_clipped_geometry() {
|
|
let mut session = ReplSession::new(241).unwrap();
|
|
let result = session
|
|
.execute(
|
|
r#"
|
|
class Painted: RectBlock {
|
|
function paint(self: Painted, mount: Mount, geometry: Bounds): DisplayList {
|
|
return RectBlock.paint(self,mount,geometry)
|
|
}
|
|
}
|
|
last = Button("last")
|
|
inner = ScrollBlock(160,100,Column(0,8,Painted(200,180),last))
|
|
outer = ScrollBlock(180,140,Column(0,8,RectBlock(180,180),inner))
|
|
page = Page()
|
|
page.render(outer)
|
|
"#,
|
|
"app.main",
|
|
)
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(result.frame.command_clips.iter().any(Option::is_some));
|
|
session
|
|
.execute("last.mounts().at(0).scrollIntoView()", "app.main")
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("inner.mounts().at(0).scrollPosition().y", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"128"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("outer.mounts().at(0).scrollPosition().y", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"148"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("last.mounts().at(0).hitTest(10,110)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn click_and_dispatch_return_the_actual_delivered_event() {
|
|
let mut session = ReplSession::new(237).unwrap();
|
|
session
|
|
.execute(
|
|
r#"button = Button("event")
|
|
clicked = button.click()
|
|
dispatched = Event("click")
|
|
returned = button.dispatch(dispatched)"#,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("ui.isInstance(clicked, Event)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
assert_eq!(
|
|
session.execute("clicked.kind", "app.main").unwrap().value,
|
|
"\"click\""
|
|
);
|
|
let globals = session.candidate.globals.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("returned".into()))
|
|
.unwrap(),
|
|
session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("dispatched".into()))
|
|
.unwrap()
|
|
);
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct VisualRedirect {
|
|
target: ObjectRef,
|
|
signature: endor_core::CallSignature,
|
|
}
|
|
impl endor_core::NativeCallable for VisualRedirect {
|
|
fn signature(&self) -> Option<endor_core::CallSignature> {
|
|
Some(self.signature.clone())
|
|
}
|
|
fn invoke(
|
|
&self,
|
|
_: &mut endor_core::NativeContext<'_>,
|
|
arguments: &[Value],
|
|
) -> std::result::Result<endor_core::NativeResult, VmError> {
|
|
Ok(endor_core::NativeResult::Invoke {
|
|
function: self.target,
|
|
arguments: arguments.to_vec(),
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn native_visual_identity_accepts_shared_window_registries_only() {
|
|
let mut session = ReplSession::new(238).unwrap();
|
|
session.execute("second = Window()", "app.main").unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(window) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("second".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("window")
|
|
};
|
|
let vm = &mut session.candidate.vm;
|
|
let desktop = vm
|
|
.payload::<crate::window::ManagedWindow>(window, "Window")
|
|
.unwrap()
|
|
.desktop;
|
|
let block = crate::ui_bridge::desktop_type(vm.object_store(), "Block", desktop).unwrap();
|
|
let function = vm
|
|
.object_store()
|
|
.lookup_method(block, "measure")
|
|
.unwrap()
|
|
.unwrap();
|
|
assert!(
|
|
crate::native_visual::is_intrinsic(vm.object_store(), function, "measure", desktop)
|
|
.unwrap()
|
|
);
|
|
assert!(
|
|
!crate::native_visual::is_intrinsic(vm.object_store(), function, "paint", desktop)
|
|
.unwrap()
|
|
);
|
|
let foreign = vm.new_object().unwrap();
|
|
let registry = vm.new_dict().unwrap();
|
|
vm.set_attr(foreign, "types", Value::Object(registry))
|
|
.unwrap();
|
|
assert!(
|
|
!crate::native_visual::is_intrinsic(vm.object_store(), function, "measure", foreign)
|
|
.unwrap()
|
|
);
|
|
vm.release(foreign).unwrap();
|
|
vm.release(registry).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn replacing_base_visual_methods_changes_layout_and_same_identity_paint() {
|
|
let (mut session, _) = ReplSession::load(r#"
|
|
class Hooks {
|
|
function measure(self: Object, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(140, 70) }
|
|
function paint(self: Object, mount: Mount, geometry: Bounds): DisplayList { return DisplayList(ObjectList()) }
|
|
}
|
|
base = Block
|
|
rect = RectBlock(20, 10)
|
|
page = Page()
|
|
page.render(rect)
|
|
"#, "app.main", 235, 1).unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let get = |name: &str| match session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text(name.into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
{
|
|
Value::Object(value) => value,
|
|
_ => panic!("object"),
|
|
};
|
|
let base = get("base");
|
|
let hooks = get("Hooks");
|
|
let Value::Object(measure) = session.candidate.vm.get_attr(hooks, "measure").unwrap()
|
|
else {
|
|
panic!("measure")
|
|
};
|
|
let Value::Object(paint) = session.candidate.vm.get_attr(hooks, "paint").unwrap() else {
|
|
panic!("paint")
|
|
};
|
|
session
|
|
.candidate
|
|
.vm
|
|
.define_method(base, "measure", measure)
|
|
.unwrap();
|
|
session.refresh_document().unwrap().unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("rect.mounts().at(0).bounds().width", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"140"
|
|
);
|
|
let original = session
|
|
.candidate
|
|
.vm
|
|
.object_store()
|
|
.lookup_method(base, "paint")
|
|
.unwrap()
|
|
.unwrap();
|
|
let signature = session
|
|
.candidate
|
|
.vm
|
|
.object_store()
|
|
.native_signature(original)
|
|
.unwrap()
|
|
.unwrap();
|
|
session
|
|
.candidate
|
|
.vm
|
|
.replace_payload(
|
|
original,
|
|
endor_core::ContextNativeFunction::new(
|
|
"redirect paint",
|
|
VisualRedirect {
|
|
target: paint,
|
|
signature,
|
|
},
|
|
vec![paint],
|
|
),
|
|
)
|
|
.unwrap();
|
|
let updated = session.refresh_document().unwrap().unwrap();
|
|
assert!(updated.frame.commands.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn same_identity_event_payload_changes_invalidate_interactivity_cache() {
|
|
let mut session = ReplSession::new(236).unwrap();
|
|
session
|
|
.execute(
|
|
r#"
|
|
class Hooks { function onEvent(self: Object, event: Event): Unit {} }
|
|
base = Block
|
|
rect = RectBlock(20, 10)
|
|
"#,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let get = |name: &str| match session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text(name.into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
{
|
|
Value::Object(value) => value,
|
|
_ => panic!("object"),
|
|
};
|
|
let base = get("base");
|
|
let hooks = get("Hooks");
|
|
let rect = get("rect");
|
|
let Value::Object(target) = session.candidate.vm.get_attr(hooks, "onEvent").unwrap() else {
|
|
panic!("onEvent")
|
|
};
|
|
let vm = &mut session.candidate.vm;
|
|
let function = vm
|
|
.object_store()
|
|
.lookup_method(base, "onEvent")
|
|
.unwrap()
|
|
.unwrap();
|
|
let mut signature = vm
|
|
.object_store()
|
|
.native_signature(function)
|
|
.unwrap()
|
|
.unwrap();
|
|
signature.effect = endor_core::CallEffect::Mutating;
|
|
let mut cache = crate::snapshot::RenderCache::default();
|
|
let initial = cache.snapshot(vm.object_store(), rect).unwrap();
|
|
assert!(!initial.interactive);
|
|
vm.take_changes();
|
|
vm.replace_payload(
|
|
function,
|
|
endor_core::ContextNativeFunction::new(
|
|
"redirect event",
|
|
VisualRedirect { target, signature },
|
|
vec![target],
|
|
),
|
|
)
|
|
.unwrap();
|
|
cache.invalidate(vm.take_changes());
|
|
let updated = cache.snapshot(vm.object_store(), rect).unwrap();
|
|
assert!(updated.interactive);
|
|
assert_ne!(initial.protocol_key, updated.protocol_key);
|
|
assert_eq!(
|
|
session
|
|
.execute("ui.implements(base, EventTarget)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn source_effect_queries_use_compiled_instructions_without_running_functions() {
|
|
let mut session = ReplSession::new(234).unwrap();
|
|
session
|
|
.execute(
|
|
r#"
|
|
function simple(value: Number): Number { return value }
|
|
function forward(value: Number): Number { return simple(value) }
|
|
function unknown(callback: Function): Object { return callback() }
|
|
function recursive(value: Number): Number { return recursive(value) }
|
|
marker = ui.state(0)
|
|
function impure(value: Number): Number { marker.increment() return value }
|
|
original = simple
|
|
class ReadOnlyModel {
|
|
function helper(self: ReadOnlyModel, value: Number): Number { return value }
|
|
function check(self: ReadOnlyModel, value: Number): Number { return self.helper(value) }
|
|
}
|
|
model = ReadOnlyModel()
|
|
"#,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
for (name, expected) in [
|
|
("simple", Some(endor_core::CallEffect::ReadOnly)),
|
|
("forward", Some(endor_core::CallEffect::ReadOnly)),
|
|
("recursive", Some(endor_core::CallEffect::ReadOnly)),
|
|
("unknown", None),
|
|
("impure", None),
|
|
] {
|
|
let Value::Object(function) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text(name.into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("function")
|
|
};
|
|
assert_eq!(
|
|
session
|
|
.candidate
|
|
.vm
|
|
.object_store()
|
|
.call_effect(function)
|
|
.unwrap(),
|
|
expected
|
|
);
|
|
}
|
|
let Value::Object(model) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("model".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("model")
|
|
};
|
|
let Value::Object(bound) = session.candidate.vm.bind_attr(model, "check").unwrap() else {
|
|
panic!("bound")
|
|
};
|
|
assert_eq!(
|
|
session
|
|
.candidate
|
|
.vm
|
|
.object_store()
|
|
.call_effect(bound)
|
|
.unwrap(),
|
|
Some(endor_core::CallEffect::ReadOnly)
|
|
);
|
|
session.candidate.vm.release(bound).unwrap();
|
|
let Value::Object(unbound) = session.candidate.vm.get_attr(model, "check").unwrap() else {
|
|
panic!("method")
|
|
};
|
|
assert_eq!(session.candidate.vm.call_effect(unbound).unwrap(), None);
|
|
let Value::Object(forward) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("forward".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("forward")
|
|
};
|
|
session.execute("simple = impure", "app.main").unwrap();
|
|
assert_eq!(session.candidate.vm.call_effect(forward).unwrap(), None);
|
|
session.execute("simple = original", "app.main").unwrap();
|
|
assert_eq!(
|
|
session.candidate.vm.call_effect(forward).unwrap(),
|
|
Some(endor_core::CallEffect::ReadOnly)
|
|
);
|
|
assert_eq!(
|
|
session.execute("marker.value", "app.main").unwrap().value,
|
|
"0"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn typed_method_shadows_invalidate_cached_ancestors_and_can_be_removed() {
|
|
let mut session = ReplSession::new(233).unwrap();
|
|
session
|
|
.execute(
|
|
"rect = RectBlock(40, 20)\nroot = ColumnBlock(0, 0, rect)",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let lookup = |name: &str| match session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text(name.into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
{
|
|
Value::Object(value) => value,
|
|
_ => panic!("object"),
|
|
};
|
|
let rect = lookup("rect");
|
|
let root = lookup("root");
|
|
for name in ["paint", "measure", "onEvent"] {
|
|
let error = session
|
|
.execute(&format!("rect.{name} = 1"), "app.main")
|
|
.err()
|
|
.expect("typed method cannot be shadowed");
|
|
assert!(
|
|
error.to_string().contains("instance attribute shadows"),
|
|
"{error:#}"
|
|
);
|
|
}
|
|
let vm = &mut session.candidate.vm;
|
|
let storage = *vm
|
|
.object_store()
|
|
.attribute_path(rect)
|
|
.unwrap()
|
|
.last()
|
|
.unwrap();
|
|
let mut cache = crate::snapshot::RenderCache::default();
|
|
cache.snapshot(vm.object_store(), root).unwrap();
|
|
vm.take_changes();
|
|
cache.snapshot(vm.object_store(), root).unwrap();
|
|
assert_eq!(cache.visited, 0);
|
|
vm.set_attr(storage, "paint", Value::Nil).unwrap();
|
|
cache.invalidate(vm.take_changes());
|
|
assert!(matches!(
|
|
cache.snapshot(vm.object_store(), root),
|
|
Err(endor_core::VmError::TraitViolation { .. })
|
|
));
|
|
vm.del_attr(storage, "paint").unwrap();
|
|
cache.invalidate(vm.take_changes());
|
|
cache.snapshot(vm.object_store(), root).unwrap();
|
|
assert!(cache.visited > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn presentable_type_contracts_reject_before_registration_or_body_effects() {
|
|
let mut session = ReplSession::new(232).unwrap();
|
|
session.execute("marker = ui.state(0)", "app.main").unwrap();
|
|
for (name, method) in [
|
|
(
|
|
"WrongParameter",
|
|
"function present(self: WrongParameter, window: Point): Block { marker.increment() return TextBlock(\"bad\") }",
|
|
),
|
|
(
|
|
"WrongResult",
|
|
"function present(self: WrongResult, window: Window): Number { marker.increment() return 7 }",
|
|
),
|
|
] {
|
|
let input = format!("class {name} {{ {method} }} ui.implement({name}, Presentable)");
|
|
let error = session
|
|
.execute(&input, "app.main")
|
|
.err()
|
|
.expect("incompatible typed capability");
|
|
assert!(
|
|
error
|
|
.to_string()
|
|
.contains("trait Presentable method present"),
|
|
"{error:#}"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute(&format!("ui.implements({name}, Presentable)"), "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"false"
|
|
);
|
|
assert_eq!(
|
|
session.execute("marker.value", "app.main").unwrap().value,
|
|
"0"
|
|
);
|
|
}
|
|
session.execute(r#"
|
|
class Good {
|
|
function present(self: Good, window: Object): TextBlock { return TextBlock("typed") }
|
|
}
|
|
ui.implement(Good, Presentable)
|
|
view = ui.present(Good())
|
|
"#, "app.main").unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("ui.isInstance(view, TextBlock)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
assert!(!session.is_faulted());
|
|
}
|
|
|
|
#[test]
|
|
fn presentable_dispatch_preserves_source_identity_and_reactive_display() {
|
|
let (mut session, initial) = ReplSession::load(
|
|
include_str!("../examples/presentable.endor"),
|
|
"app.main",
|
|
1,
|
|
230,
|
|
)
|
|
.unwrap();
|
|
assert!(initial.frame.commands.iter().any(|command| matches!(command,RenderCommand::Text { value, .. } if value == "普通对象的显式展示能力")));
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(product) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("product".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("expected Product");
|
|
};
|
|
let Value::Object(view) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("view".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("expected Block");
|
|
};
|
|
assert_eq!(
|
|
session.candidate.vm.get_attr(view, "source").unwrap(),
|
|
Value::Object(product)
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("ui.supports(product,Presentable)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"true"
|
|
);
|
|
let changed = session
|
|
.execute("product.name = \"updated\"", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(changed.frame.commands.iter().any(
|
|
|command| matches!(command,RenderCommand::Text { value, .. } if value == "updated")
|
|
));
|
|
assert!(session.execute("class Bad: RectBlock { function paint(self: Bad): DisplayList { return DisplayList(ObjectList()) } }","app.main").err().unwrap().to_string().contains("trait Visual method paint"));
|
|
assert!(!session.is_faulted());
|
|
}
|
|
|
|
#[test]
|
|
fn matching_present_method_does_not_implicitly_declare_presentable() {
|
|
let mut session = ReplSession::new(231).unwrap();
|
|
session
|
|
.execute(
|
|
r#"
|
|
class Data {
|
|
marker: Object
|
|
function present(self: Data, window: Window): Block {
|
|
self.marker.increment()
|
|
return TextBlock("custom")
|
|
}
|
|
}
|
|
marker = ui.state(0)
|
|
data = Data(marker)
|
|
fallback = ui.present(data)
|
|
"#,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
session.execute("marker.value", "app.main").unwrap().value,
|
|
"0"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("ui.supports(data,Presentable)", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"false"
|
|
);
|
|
session
|
|
.execute(
|
|
"ui.implement(Data,Presentable)\ncustom = ui.open(data)",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
session.execute("marker.value", "app.main").unwrap().value,
|
|
"1"
|
|
);
|
|
let before = session.documents.clone();
|
|
assert!(
|
|
session
|
|
.execute(
|
|
"class Missing {}\nui.implement(Missing,Presentable)",
|
|
"app.main"
|
|
)
|
|
.is_err()
|
|
);
|
|
assert_eq!(
|
|
session.documents[&crate::window::MAIN_WINDOW]
|
|
.frame
|
|
.layout_version,
|
|
before[&crate::window::MAIN_WINDOW].frame.layout_version
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn managed_property_bindings_update_hidden_objects_and_shared_window_geometry() {
|
|
let mut session = ReplSession::new(220).unwrap();
|
|
session
|
|
.execute(
|
|
r##"
|
|
class Model {
|
|
width: Number
|
|
function read(self: Model): Number { return self.width }
|
|
}
|
|
model = Model(80)
|
|
rect = RectBlock(1,20,0,"#DDEEFF")
|
|
token = rect.bind("width",model.read)
|
|
"##,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
session.execute("rect.width", "app.main").unwrap().value,
|
|
"80"
|
|
);
|
|
session.execute("model.width = 140", "app.main").unwrap();
|
|
assert_eq!(
|
|
session.execute("rect.width", "app.main").unwrap().value,
|
|
"140"
|
|
);
|
|
session.execute("first = Page()\nfirst.render(rect)\nother = Window()\nsecond = Page()\nsecond.open(other)\nsecond.render(rect)", "app.main").unwrap();
|
|
let updated = session
|
|
.execute("model.width = 260", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
for frame in std::iter::once(&updated.frame)
|
|
.chain(updated.windows.values().map(|document| &document.frame))
|
|
{
|
|
assert!(frame.mounts.iter().any(|mount| mount.width == 260.0));
|
|
}
|
|
session
|
|
.execute("token.disconnect()\nmodel.width = 360", "app.main")
|
|
.unwrap();
|
|
assert_eq!(
|
|
session.execute("rect.width", "app.main").unwrap().value,
|
|
"260"
|
|
);
|
|
let direct = session
|
|
.execute("rect.width = 180", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(direct.frame.mounts.iter().any(|mount| mount.width == 180.0));
|
|
}
|
|
|
|
#[test]
|
|
fn multiple_object_outputs_on_one_target_do_not_invalidate_each_other() {
|
|
let mut session = ReplSession::new(222).unwrap();
|
|
session
|
|
.execute(
|
|
r##"
|
|
class Record { value: Number }
|
|
function makeRecord(): Object { return Record(7) }
|
|
rect = RectBlock(1,20)
|
|
rect.first = Record(0)
|
|
rect.second = Record(0)
|
|
first = rect.bind("first",makeRecord)
|
|
second = rect.bind("second",makeRecord)
|
|
"##,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
session
|
|
.execute("rect.first.value", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"7"
|
|
);
|
|
assert_eq!(
|
|
session
|
|
.execute("rect.second.value", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"7"
|
|
);
|
|
assert!(!session.is_faulted());
|
|
}
|
|
|
|
#[test]
|
|
fn binding_tokens_do_not_retain_hidden_targets_and_compute_is_read_only() {
|
|
let mut session = ReplSession::new(221).unwrap();
|
|
session
|
|
.execute(
|
|
r##"
|
|
class Model {
|
|
width: Number
|
|
function read(self: Model): Number { return self.width }
|
|
function bad(self: Model): Number {
|
|
self.width = 999
|
|
return self.width
|
|
}
|
|
}
|
|
model = Model(80)
|
|
rect = RectBlock(1,20,0,"#DDEEFF")
|
|
token = rect.bind("width",model.read)
|
|
"##,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(target) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("rect".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("expected Block");
|
|
};
|
|
let weak = session
|
|
.candidate
|
|
.vm
|
|
.object_store()
|
|
.downgrade(target)
|
|
.unwrap();
|
|
session
|
|
.candidate
|
|
.vm
|
|
.dict_delete(globals, &Value::Text("rect".into()))
|
|
.unwrap();
|
|
session.candidate.vm.collect_cycles();
|
|
assert!(session.candidate.vm.upgrade(weak).unwrap().is_none());
|
|
assert_eq!(
|
|
session
|
|
.execute("token.isConnected()", "app.main")
|
|
.unwrap()
|
|
.value,
|
|
"false"
|
|
);
|
|
assert!(
|
|
session
|
|
.execute(
|
|
"other = RectBlock(1,20)\nbadToken = other.bind(\"width\",model.bad)",
|
|
"app.main"
|
|
)
|
|
.is_err()
|
|
);
|
|
assert!(session.is_faulted());
|
|
let Value::Object(model) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("model".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("expected model");
|
|
};
|
|
assert_eq!(
|
|
session.candidate.vm.get_attr(model, "width").unwrap(),
|
|
Value::Number(80.into())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn native_method_callbacks_update_two_explicit_bindings() {
|
|
let mut session = ReplSession::new(21).unwrap();
|
|
session.execute("counter = ui.state(9)\nfirst = ui.textOf(counter, \"value\")\nsecond = ui.textOf(counter, \"value\")\nbutton = ui.button(\"increment\")\nfunction increment(): Unit { counter.increment() }\nbutton.onClick(increment)\npage = ui.page()\npage.render(ui.row(0, 8, first, second, button))", "app.main").unwrap();
|
|
let updated = session
|
|
.execute("button.click()", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
let texts: Vec<_> = updated
|
|
.frame
|
|
.commands
|
|
.iter()
|
|
.filter_map(|command| match command {
|
|
RenderCommand::Text { value, .. } => Some(value.as_str()),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
assert_eq!(&texts[..2], &["10", "10"]);
|
|
}
|
|
|
|
#[test]
|
|
fn closing_a_page_releases_its_root_without_retaining_it_through_shared_content() {
|
|
let mut session = ReplSession::new(22).unwrap();
|
|
session
|
|
.execute(
|
|
"card = ui.rect(10, 20)\npage = ui.page()\npage.render(card)",
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
let globals = session.candidate.globals.unwrap();
|
|
let Value::Object(page) = session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("page".into()))
|
|
.unwrap()
|
|
.unwrap()
|
|
else {
|
|
panic!("expected Page")
|
|
};
|
|
let weak = session.candidate.vm.downgrade(page).unwrap();
|
|
session.execute("page.close()", "app.main").unwrap();
|
|
session
|
|
.execute("card.setColor(\"#FFEEDD\")", "app.main")
|
|
.unwrap();
|
|
assert!(
|
|
session
|
|
.execute("page.open()", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.is_some()
|
|
);
|
|
session.execute("page.close()", "app.main").unwrap();
|
|
session
|
|
.candidate
|
|
.vm
|
|
.dict_delete(globals, &Value::Text("page".into()))
|
|
.unwrap();
|
|
session
|
|
.bridge
|
|
.reclaim(&mut session.candidate.vm, &mut session.candidate.roots)
|
|
.unwrap();
|
|
assert!(session.candidate.vm.upgrade(weak).unwrap().is_none());
|
|
assert!(
|
|
session
|
|
.candidate
|
|
.vm
|
|
.dict_get(globals, &Value::Text("card".into()))
|
|
.unwrap()
|
|
.is_some()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn mounted_aliases_update_nested_positions_unmount_and_reject_cycles() {
|
|
let mut session = ReplSession::new(10).unwrap();
|
|
session.execute("card = ui.rect(10, 20)\nother = card\ncontainer = ui.column(0, 0, other)\npage = ui.page()\npage.render(container)", "app.main").unwrap();
|
|
let identity = session.execute("other", "app.main").unwrap().value;
|
|
let updated = session
|
|
.execute("card = ui.rect(640, 200, 32)", "app.main")
|
|
.unwrap();
|
|
assert_eq!(updated.value, identity);
|
|
assert_eq!(
|
|
session.execute("other", "app.main").unwrap().value,
|
|
identity
|
|
);
|
|
let frame = updated.document.unwrap().frame;
|
|
assert!(matches!(
|
|
frame.commands[0],
|
|
RenderCommand::RoundedRect {
|
|
width: 640.0,
|
|
height: 200.0,
|
|
..
|
|
}
|
|
));
|
|
assert!(
|
|
session
|
|
.execute("card = ui.column(0, 0, other)", "app.main")
|
|
.is_err()
|
|
);
|
|
assert!(
|
|
session
|
|
.execute("other", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.is_none()
|
|
);
|
|
let unmounted = session
|
|
.execute("page.unmount()", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(unmounted.frame.commands.is_empty());
|
|
assert!(
|
|
session
|
|
.execute("card = ui.rect(80, 50)", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.is_none()
|
|
);
|
|
let remounted = session
|
|
.execute("page.render(other)", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(matches!(
|
|
remounted.frame.commands[0],
|
|
RenderCommand::RoundedRect {
|
|
width: 80.0,
|
|
height: 50.0,
|
|
..
|
|
}
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn live_session_retains_objects_and_updates_only_after_mounting() {
|
|
let mut session = ReplSession::new(7).unwrap();
|
|
let created = session
|
|
.execute(
|
|
r##"title = ui.text("你好,Tessera", 2, "#243047")"##,
|
|
"app.main",
|
|
)
|
|
.unwrap();
|
|
assert!(created.document.is_none());
|
|
assert_eq!(
|
|
session.execute("title", "app.main").unwrap().value,
|
|
created.value
|
|
);
|
|
assert_eq!(
|
|
session.execute("_", "app.main").unwrap().value,
|
|
created.value
|
|
);
|
|
let rendered = session
|
|
.execute("page = ui.page()\npage.render(title)", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(
|
|
matches!(&rendered.frame.commands[0], RenderCommand::Text { value, .. } if value == "你好,Tessera")
|
|
);
|
|
assert_eq!(rendered.root.as_ref().unwrap().kind, "repl-session");
|
|
assert!(
|
|
session
|
|
.execute(r#"title = ui.text("changed")"#, "app.main")
|
|
.unwrap()
|
|
.document
|
|
.is_some()
|
|
);
|
|
let updated = session
|
|
.execute("page.render(title)", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.unwrap();
|
|
assert!(
|
|
matches!(&updated.frame.commands[0], RenderCommand::Text { value, .. } if value == "changed")
|
|
);
|
|
assert!(updated.source.contains("// Console input 6"));
|
|
assert!(
|
|
session
|
|
.execute("title", "app.main")
|
|
.unwrap()
|
|
.document
|
|
.is_none()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn live_session_compiles_before_execution_and_discards_failed_render() {
|
|
let mut session = ReplSession::new(8).unwrap();
|
|
session
|
|
.execute("value = 1\npage = ui.page()", "app.main")
|
|
.unwrap();
|
|
assert!(session.execute("value = 2\nmissing", "app.main").is_err());
|
|
assert_eq!(session.execute("value", "app.main").unwrap().value, "1");
|
|
assert!(
|
|
session
|
|
.execute(
|
|
"value = 3\npage.render(ui.text(\"discarded\"))\nui.rect(-1, 1, 0)",
|
|
"app.main"
|
|
)
|
|
.is_err()
|
|
);
|
|
let retained = session.execute("value", "app.main").unwrap();
|
|
assert_eq!(retained.value, "3");
|
|
assert!(retained.document.is_none());
|
|
session.execute("function local(value: Number): Number { result = value result = 42 return result }", "app.main").unwrap();
|
|
assert_eq!(
|
|
session.execute("local(40)", "app.main").unwrap().value,
|
|
"42"
|
|
);
|
|
}
|
|
|
|
#[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: 76.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.commands).unwrap(),
|
|
serde_json::to_value(&wrapped.frame.commands).unwrap()
|
|
);
|
|
// Separate VM instances render the same commands with independent
|
|
// managed object identities and presentation contexts.
|
|
assert_ne!(
|
|
document.frame.mounts[0].block_id,
|
|
wrapped.frame.mounts[0].block_id
|
|
);
|
|
}
|
|
|
|
#[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 {
|
|
submission: 1,
|
|
..NamespaceState::default()
|
|
};
|
|
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_live_object_budget() {
|
|
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::default();
|
|
tools.open = true;
|
|
tools.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() {
|
|
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: 76.0,
|
|
width: 240.0,
|
|
height: 80.0,
|
|
..
|
|
}
|
|
));
|
|
let first_width = crate::text::measure("Aster VM", crate::text::TextStyle::ui(1)).width;
|
|
assert!(matches!(
|
|
&document.frame.commands[3],
|
|
RenderCommand::Text { x, y: 168.0, .. } if (*x - (24.0 + first_width + 12.0 + 8.0 + 12.0)).abs() < 0.01
|
|
));
|
|
// 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, .. }
|
|
));
|
|
let width = crate::text::measure("shared", crate::text::TextStyle::ui(1)).width;
|
|
assert!(matches!(
|
|
&document.frame.commands[1],
|
|
RenderCommand::Text { x, y: 4.0, .. } if (*x - (4.0 + width + 8.0)).abs() < 0.01
|
|
));
|
|
}
|
|
|
|
#[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 Block graph"),
|
|
"{error:#}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn managed_ui_depth_is_not_truncated_at_64_levels() {
|
|
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 document = compile(&source, "app.main", 1).unwrap();
|
|
assert_eq!(document.frame.commands.len(), 1);
|
|
}
|
|
|
|
#[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)
|
|
);
|
|
}
|
|
}
|