feat: construct native visual subclasses from Endor source
Verify Tessera GUI / verify (push) Successful in 9m53s
Verify Tessera GUI / verify (push) Successful in 9m53s
This commit is contained in:
+104
-11
@@ -3,8 +3,8 @@ use std::sync::{Arc, Mutex};
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use endor_core::{
|
||||
ContextNativeFunction, EndorReal, NativeCallable, NativeContext, NativeResult, ObjectRef,
|
||||
TypeConstructor, Value, Vm, VmError, WeakObjectRef,
|
||||
ContextNativeFunction, EndorReal, EndorTypeObject, NativeCallable, NativeContext, NativeResult,
|
||||
ObjectRef, TypeConstructor, Value, Vm, VmError, WeakObjectRef,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -215,6 +215,7 @@ struct Function {
|
||||
operation: Operation,
|
||||
owner: Option<ObjectRef>,
|
||||
session: Shared,
|
||||
requested_type: Option<ObjectRef>,
|
||||
}
|
||||
|
||||
/// One shared implementation on the nominal type. The VM binds receivers;
|
||||
@@ -236,11 +237,55 @@ impl NativeCallable for TypeMethod {
|
||||
operation: self.operation,
|
||||
owner: Some(owner),
|
||||
session: self.session.clone(),
|
||||
requested_type: None,
|
||||
}
|
||||
.invoke(cx, &args[1..])
|
||||
}
|
||||
}
|
||||
|
||||
/// The Type dispatcher passes the requested subclass explicitly. Native
|
||||
/// constructor options precede the subclass's inherited/own field arguments.
|
||||
#[derive(Debug)]
|
||||
struct NativeFactory {
|
||||
operation: Operation,
|
||||
session: Shared,
|
||||
}
|
||||
|
||||
impl NativeCallable for NativeFactory {
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let requested = object(args.first())?;
|
||||
let fields = cx
|
||||
.store()
|
||||
.payload::<EndorTypeObject>(requested, "Type")?
|
||||
.fields()
|
||||
.to_vec();
|
||||
let values = &args[1..];
|
||||
let native_count = values
|
||||
.len()
|
||||
.checked_sub(fields.len())
|
||||
.ok_or(invalid("missing subclass field arguments"))?;
|
||||
let result = Function {
|
||||
operation: self.operation,
|
||||
owner: None,
|
||||
session: self.session.clone(),
|
||||
requested_type: Some(requested),
|
||||
}
|
||||
.invoke(cx, &values[..native_count])?;
|
||||
let NativeResult::Return(Value::Object(instance)) = result else {
|
||||
return Err(invalid("native constructor did not return an instance"));
|
||||
};
|
||||
if cx.store().type_of(instance)? != Some(requested) {
|
||||
return Err(invalid(
|
||||
"native constructor returned an incorrect nominal type",
|
||||
));
|
||||
}
|
||||
for (field, value) in fields.iter().zip(&values[native_count..]) {
|
||||
cx.set_attr(instance, &field.name, value.clone())?;
|
||||
}
|
||||
Ok(NativeResult::Return(Value::Object(instance)))
|
||||
}
|
||||
}
|
||||
|
||||
fn invalid(message: &'static str) -> VmError {
|
||||
VmError::InvalidArgument(message)
|
||||
}
|
||||
@@ -305,6 +350,7 @@ fn bind(
|
||||
operation,
|
||||
owner: Some(owner),
|
||||
session: session.clone(),
|
||||
requested_type: None,
|
||||
},
|
||||
vec![owner, desktop],
|
||||
))?;
|
||||
@@ -316,6 +362,15 @@ fn new_block(
|
||||
cx: &mut NativeContext<'_>,
|
||||
data: Data,
|
||||
session: &Shared,
|
||||
) -> Result<ObjectRef, VmError> {
|
||||
new_typed_block(cx, data, session, None)
|
||||
}
|
||||
|
||||
fn new_typed_block(
|
||||
cx: &mut NativeContext<'_>,
|
||||
data: Data,
|
||||
session: &Shared,
|
||||
requested: Option<ObjectRef>,
|
||||
) -> Result<ObjectRef, VmError> {
|
||||
let type_name = match data.kind {
|
||||
Kind::Text { .. } => "TextBlock",
|
||||
@@ -327,8 +382,17 @@ fn new_block(
|
||||
Kind::Page => "Page",
|
||||
};
|
||||
let page = matches!(data.kind, Kind::Page);
|
||||
let desktop = session
|
||||
.lock()
|
||||
.map_err(|_| invalid("UI session poisoned"))?
|
||||
.desktop;
|
||||
let expected = desktop_type(cx.store(), type_name, desktop)?;
|
||||
let type_object = requested.unwrap_or(expected);
|
||||
if !cx.store().is_subtype(type_object, expected)? {
|
||||
return Err(invalid("native constructor cannot allocate this subclass"));
|
||||
}
|
||||
let block = block::allocate(cx, data)?;
|
||||
attach_named_type(cx, block, type_name, session)?;
|
||||
cx.attach_type(block, type_object)?;
|
||||
{
|
||||
let mut state = session.lock().map_err(|_| invalid("UI session poisoned"))?;
|
||||
if state.nodes.len() >= MAX_UI_NODES {
|
||||
@@ -626,10 +690,11 @@ impl NativeCallable for Function {
|
||||
{
|
||||
return Err(invalid("text exceeds size limits"));
|
||||
}
|
||||
return Ok(NativeResult::Return(Value::Object(new_block(
|
||||
return Ok(NativeResult::Return(Value::Object(new_typed_block(
|
||||
cx,
|
||||
data,
|
||||
&self.session,
|
||||
self.requested_type,
|
||||
)?)));
|
||||
}
|
||||
if matches!(self.operation, OpenValue) {
|
||||
@@ -1083,6 +1148,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
|
||||
operation,
|
||||
owner: None,
|
||||
session: session.clone(),
|
||||
requested_type: None,
|
||||
},
|
||||
vec![desktop],
|
||||
))?;
|
||||
@@ -1113,7 +1179,30 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
|
||||
] {
|
||||
let base = object(Some(&vm.get_attr(ui, base_name)?))?;
|
||||
let constructor = match constructor_name {
|
||||
Some(name) => TypeConstructor::Native(object(Some(&vm.get_attr(ui, name)?))?),
|
||||
Some(constructor_name) => {
|
||||
let operation = match constructor_name {
|
||||
"text" => Operation::Text,
|
||||
"rect" => Operation::Rect,
|
||||
"spacer" => Operation::Spacer,
|
||||
"row" => Operation::Row,
|
||||
"column" => Operation::Column,
|
||||
"button" => Operation::Button,
|
||||
"page" => Operation::Page,
|
||||
_ => return Err(invalid("unknown native type allocator").into()),
|
||||
};
|
||||
let factory = vm.allocate(ContextNativeFunction::new(
|
||||
constructor_name,
|
||||
NativeFactory {
|
||||
operation,
|
||||
session: session.clone(),
|
||||
},
|
||||
vec![desktop],
|
||||
))?;
|
||||
roots.push(factory);
|
||||
let function_type = object(Some(&vm.get_attr(ui, "Function")?))?;
|
||||
vm.attach_type(factory, function_type)?;
|
||||
TypeConstructor::NativeTyped(factory)
|
||||
}
|
||||
None => TypeConstructor::Abstract,
|
||||
};
|
||||
let definition = vm.new_type(name, base, constructor)?;
|
||||
@@ -1208,15 +1297,19 @@ pub fn attach_desktop_type(
|
||||
name: &str,
|
||||
desktop: ObjectRef,
|
||||
) -> Result<(), VmError> {
|
||||
let types = object(Some(&cx.store().get_attr(desktop, "types")?))?;
|
||||
let definition = object(
|
||||
cx.store()
|
||||
.dict_get(types, &Value::Text(name.into()))?
|
||||
.as_ref(),
|
||||
)?;
|
||||
let definition = desktop_type(cx.store(), name, desktop)?;
|
||||
cx.attach_type(instance, definition)
|
||||
}
|
||||
|
||||
fn desktop_type(
|
||||
store: &endor_core::ObjectStore,
|
||||
name: &str,
|
||||
desktop: ObjectRef,
|
||||
) -> Result<ObjectRef, VmError> {
|
||||
let types = object(Some(&store.get_attr(desktop, "types")?))?;
|
||||
object(store.dict_get(types, &Value::Text(name.into()))?.as_ref())
|
||||
}
|
||||
|
||||
pub fn snapshot(vm: &Vm, value: &Value) -> Result<Arc<UiNode>> {
|
||||
let Value::Object(root) = value else {
|
||||
bail!("main must return a Block");
|
||||
|
||||
Reference in New Issue
Block a user