Complete nullable page and typed lifecycle resource method contracts

This commit is contained in:
2026-10-22 14:01:01 +08:00
parent 543c909d53
commit d9fd3f2f65
10 changed files with 193 additions and 24 deletions
+35 -1
View File
@@ -225,6 +225,20 @@ pub fn new_event(
Ok(event)
}
#[derive(Debug)]
struct TypedMethod {
method: Method,
signature: endor_core::CallSignature,
}
impl NativeCallable for TypedMethod {
fn signature(&self) -> Option<endor_core::CallSignature> {
Some(self.signature.clone())
}
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
self.method.invoke(cx, args)
}
}
pub fn install(
vm: &mut Vm,
roots: &mut Vec<ObjectRef>,
@@ -244,7 +258,27 @@ pub fn install(
(token_type, "disconnect", Method::Disconnect),
(token_type, "isConnected", Method::Connected),
] {
let function = vm.allocate(ContextNativeFunction::new(name, method, vec![]))?;
let query = matches!(method, Method::Prevented | Method::Connected);
let function = vm.allocate(ContextNativeFunction::new(
name,
TypedMethod {
method,
signature: endor_core::CallSignature::exact(
vec![endor_core::AnnotationType::Nominal(definition)],
if query {
endor_core::AnnotationType::Bool
} else {
endor_core::AnnotationType::Unit
},
if query {
endor_core::CallEffect::ReadOnly
} else {
endor_core::CallEffect::Mutating
},
),
},
vec![],
))?;
roots.push(function);
vm.attach_type(function, function_type)?;
vm.define_method(definition, name, function)?;