feat: retain managed visual objects and dispatch native input through Endor
Verify Tessera GUI / verify (push) Successful in 9m28s
Verify Tessera GUI / verify (push) Successful in 9m28s
This commit is contained in:
+251
@@ -0,0 +1,251 @@
|
||||
//! Live visual objects. All semantic references are owned by Aster's heap;
|
||||
//! native Arcs are reserved for immutable rendering snapshots.
|
||||
use std::sync::Arc;
|
||||
|
||||
use endor_core::{NativeContext, ObjectPayload, ObjectRef, ObjectStore, Value, VmError};
|
||||
|
||||
use crate::ui_bridge::UiNode;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Child {
|
||||
pub slot: u64,
|
||||
pub block: ObjectRef,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Kind {
|
||||
Text {
|
||||
value: String,
|
||||
size: u32,
|
||||
color: String,
|
||||
},
|
||||
Rect {
|
||||
width: f32,
|
||||
height: f32,
|
||||
radius: f32,
|
||||
color: String,
|
||||
},
|
||||
Spacer(f32),
|
||||
Row,
|
||||
Column,
|
||||
Button,
|
||||
Page,
|
||||
}
|
||||
|
||||
/// The VM owns the Block -> Cell -> Data relationship. Shared assignment
|
||||
/// changes the Cell to point at another Block, retaining the original identity.
|
||||
#[derive(Debug)]
|
||||
pub struct Block {
|
||||
pub state: ObjectRef,
|
||||
pub page: bool,
|
||||
}
|
||||
|
||||
impl ObjectPayload for Block {
|
||||
fn type_target(&self, store: &ObjectStore) -> Result<Option<ObjectRef>, VmError> {
|
||||
let Value::Object(value) = store.cell_get(self.state)? else {
|
||||
return Err(VmError::InvalidArgument("Block state is not an object"));
|
||||
};
|
||||
if store.object(value)?.payload().as_any().is::<Block>() {
|
||||
Ok(Some(value))
|
||||
} else {
|
||||
store.payload::<Data>(value, "Block data")?;
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"tessera-block"
|
||||
}
|
||||
fn allows_attributes(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
visit(self.state);
|
||||
}
|
||||
fn assignment_cell(
|
||||
&self,
|
||||
source: ObjectRef,
|
||||
store: &ObjectStore,
|
||||
) -> Result<Option<ObjectRef>, VmError> {
|
||||
if !store.object(source)?.payload().as_any().is::<Self>() {
|
||||
return Ok(None);
|
||||
}
|
||||
if store.payload::<Self>(source, "Block")?.page != self.page {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"Page assignment requires another Page",
|
||||
));
|
||||
}
|
||||
validate(store, source, Some(self.state))?;
|
||||
Ok(Some(self.state))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Data {
|
||||
pub kind: Kind,
|
||||
pub children: Vec<Child>,
|
||||
pub padding: f32,
|
||||
pub gap: f32,
|
||||
pub next_slot: u64,
|
||||
pub history: Vec<ObjectRef>,
|
||||
pub history_index: usize,
|
||||
pub text_binding: Option<(ObjectRef, String)>,
|
||||
pub click: Option<ObjectRef>,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
pub fn new(kind: Kind, children: Vec<ObjectRef>, padding: f32, gap: f32) -> Self {
|
||||
let next_slot = children.len() as u64;
|
||||
Self {
|
||||
kind,
|
||||
children: children
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(slot, block)| Child {
|
||||
slot: slot as u64,
|
||||
block,
|
||||
})
|
||||
.collect(),
|
||||
padding,
|
||||
gap,
|
||||
next_slot,
|
||||
history: vec![],
|
||||
history_index: 0,
|
||||
text_binding: None,
|
||||
click: None,
|
||||
}
|
||||
}
|
||||
pub fn is_container(&self) -> bool {
|
||||
matches!(
|
||||
self.kind,
|
||||
Kind::Row | Kind::Column | Kind::Button | Kind::Page
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectPayload for Data {
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"tessera-block-data"
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
for child in &self.children {
|
||||
visit(child.block);
|
||||
}
|
||||
for item in &self.history {
|
||||
visit(*item);
|
||||
}
|
||||
if let Some((source, _)) = &self.text_binding {
|
||||
visit(*source);
|
||||
}
|
||||
if let Some(handler) = self.click {
|
||||
visit(handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn allocate(cx: &mut NativeContext<'_>, data: Data) -> Result<ObjectRef, VmError> {
|
||||
let page = matches!(data.kind, Kind::Page);
|
||||
for child in &data.children {
|
||||
validate(cx.store(), child.block, None)?;
|
||||
}
|
||||
let data = cx.allocate(data)?;
|
||||
let state = cx.new_cell(Some(Value::Object(data)))?;
|
||||
cx.allocate(Block { state, page })
|
||||
}
|
||||
|
||||
/// Borrowed final data; forwarding is part of the managed graph, never an Arc
|
||||
/// ownership graph hidden from the VM's cycle collector.
|
||||
pub fn resolve(store: &ObjectStore, block: ObjectRef) -> Result<(ObjectRef, &Data), VmError> {
|
||||
let (reference, data, _) = resolve_tracked(store, block)?;
|
||||
Ok((reference, data))
|
||||
}
|
||||
|
||||
pub fn resolve_tracked(
|
||||
store: &ObjectStore,
|
||||
block: ObjectRef,
|
||||
) -> Result<(ObjectRef, &Data, Vec<ObjectRef>), VmError> {
|
||||
let mut current = block;
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut dependencies = Vec::new();
|
||||
loop {
|
||||
if !seen.insert(current) {
|
||||
return Err(VmError::InvalidArgument("cyclic Block reference"));
|
||||
}
|
||||
let state = store.payload::<Block>(current, "Block")?.state;
|
||||
dependencies.extend([current, state]);
|
||||
let Value::Object(value) = store.cell_get(state)? else {
|
||||
return Err(VmError::InvalidArgument("Block state is not an object"));
|
||||
};
|
||||
if store.object(value)?.payload().as_any().is::<Data>() {
|
||||
dependencies.push(value);
|
||||
return Ok((
|
||||
value,
|
||||
store.payload::<Data>(value, "Block data")?,
|
||||
dependencies,
|
||||
));
|
||||
}
|
||||
current = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// Validate the entire finite expansion before establishing a new composition
|
||||
/// edge. The node count is an explicit rejected resource quota, not truncated UI.
|
||||
pub fn validate(
|
||||
store: &ObjectStore,
|
||||
root: ObjectRef,
|
||||
forbidden_state: Option<ObjectRef>,
|
||||
) -> Result<(), VmError> {
|
||||
enum Step {
|
||||
Enter(ObjectRef),
|
||||
Exit(ObjectRef),
|
||||
}
|
||||
let mut work = vec![Step::Enter(root)];
|
||||
let mut path = std::collections::HashSet::new();
|
||||
let mut visits = 0;
|
||||
while let Some(step) = work.pop() {
|
||||
match step {
|
||||
Step::Exit(state) => {
|
||||
path.remove(&state);
|
||||
}
|
||||
Step::Enter(block) => {
|
||||
visits += 1;
|
||||
if visits > 10_000 {
|
||||
return Err(VmError::ExecutionLimit("expanded Block graph"));
|
||||
}
|
||||
let state = store.payload::<Block>(block, "Block")?.state;
|
||||
if forbidden_state == Some(state) || !path.insert(state) {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"Block composition would create a cycle",
|
||||
));
|
||||
}
|
||||
work.push(Step::Exit(state));
|
||||
let Value::Object(value) = store.cell_get(state)? else {
|
||||
return Err(VmError::InvalidArgument("invalid Block state"));
|
||||
};
|
||||
if forbidden_state == Some(value) {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"Block composition would create a cycle",
|
||||
));
|
||||
}
|
||||
if let Ok(data) = store.payload::<Data>(value, "Block data") {
|
||||
for child in data.children.iter().rev() {
|
||||
work.push(Step::Enter(child.block));
|
||||
}
|
||||
} else {
|
||||
work.push(Step::Enter(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn snapshot(store: &ObjectStore, root: ObjectRef) -> Result<Arc<UiNode>, VmError> {
|
||||
crate::snapshot::RenderCache::default().snapshot(store, root)
|
||||
}
|
||||
Reference in New Issue
Block a user