Publish native visual and window type and effect contracts

This commit is contained in:
2026-10-22 13:52:01 +08:00
parent 1eb3b87050
commit 543c909d53
8 changed files with 217 additions and 20 deletions
+49 -8
View File
@@ -8,8 +8,8 @@ use crate::{
ui_bridge::{MAX_DIMENSION, attach_desktop_type},
};
use endor_core::{
ContextNativeFunction, NativeCallable, NativeContext, NativeResult, ObjectRef, Value, Vm,
VmError,
AnnotationType, CallEffect, CallSignature, ContextNativeFunction, NativeCallable,
NativeContext, NativeResult, ObjectRef, Value, Vm, VmError,
};
fn invalid(message: &'static str) -> VmError {
@@ -85,8 +85,12 @@ enum Query {
struct Method {
query: Query,
desktop: ObjectRef,
signature: CallSignature,
}
impl NativeCallable for Method {
fn signature(&self) -> Option<CallSignature> {
Some(self.signature.clone())
}
fn arity(&self) -> Option<endor_core::CallArity> {
Some(endor_core::CallArity::exact(3))
}
@@ -278,8 +282,12 @@ impl NativeCallable for Method {
struct PointMethod {
expected: ObjectRef,
circle: bool,
signature: CallSignature,
}
impl NativeCallable for PointMethod {
fn signature(&self) -> Option<CallSignature> {
Some(self.signature.clone())
}
fn arity(&self) -> Option<endor_core::CallArity> {
Some(endor_core::CallArity::exact(if self.circle {
4
@@ -345,15 +353,34 @@ pub fn install(
block_type: ObjectRef,
function_type: ObjectRef,
) -> Result<(), VmError> {
for (name, query) in [
("measure", Query::Measure),
("arrange", Query::Arrange),
("paint", Query::Paint),
("hitTest", Query::HitTest),
let nominal = |name| {
crate::ui_bridge::desktop_type(vm.object_store(), name, desktop)
.map(AnnotationType::Nominal)
};
let mount = nominal("Mount")?;
let constraints = nominal("Constraints")?;
let bounds = nominal("Bounds")?;
let point = nominal("Point")?;
let measure = nominal("MeasureResult")?;
let arrangement = nominal("Arrangement")?;
let display = nominal("DisplayList")?;
for (name, query, input, returns) in [
("measure", Query::Measure, constraints, measure),
("arrange", Query::Arrange, bounds, arrangement),
("paint", Query::Paint, bounds, display),
("hitTest", Query::HitTest, point, AnnotationType::Bool),
] {
let function = vm.allocate(ContextNativeFunction::new(
name,
Method { query, desktop },
Method {
query,
desktop,
signature: CallSignature::exact(
vec![AnnotationType::Nominal(block_type), mount, input],
returns,
CallEffect::ReadOnly,
),
},
vec![desktop],
))?;
roots.push(function);
@@ -371,6 +398,20 @@ pub fn install(
PointMethod {
expected: point_type,
circle,
signature: CallSignature::exact(
if circle {
vec![
point,
AnnotationType::Number,
AnnotationType::Number,
AnnotationType::Number,
]
} else {
vec![point, bounds]
},
AnnotationType::Bool,
CallEffect::ReadOnly,
),
},
vec![point_type],
))?;