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
+24 -13
View File
@@ -3,9 +3,9 @@ use std::sync::{Arc, Mutex};
use anyhow::{Result, bail};
use endor_core::{
AnnotationType, CallEffect, CallSignature, ContextNativeFunction, EndorReal, EndorTypeObject,
NativeCallable, NativeContext, NativeResult, ObjectRef, TypeConstructor, Value, Vm, VmError,
WeakObjectRef,
AnnotationType, CallEffect, CallSignature, CallType, ContextNativeFunction, EndorReal,
EndorTypeObject, NativeCallable, NativeContext, NativeResult, ObjectRef, TypeConstructor,
Value, Vm, VmError, WeakObjectRef,
};
use crate::{
@@ -326,7 +326,7 @@ fn method_signature(
desktop: ObjectRef,
receiver: ObjectRef,
operation: Operation,
) -> Result<Option<CallSignature>, VmError> {
) -> Result<CallSignature, VmError> {
use AnnotationType::{Any, Function, Nominal, Number, String as TextType, Unit};
use Operation::*;
let nominal = |name| desktop_type(store, name, desktop).map(Nominal);
@@ -368,21 +368,32 @@ fn method_signature(
Activate => (vec![], Nominal(receiver), CallEffect::Mutating),
Detach => (vec![], nominal("Window")?, CallEffect::Mutating),
Unmount | Close | CloseWindow | Back | Forward => (vec![], Unit, CallEffect::Mutating),
// These accept nullable/union values not yet expressible in AnnotationType.
// Keep absent metadata explicit instead of narrowing existing calls.
Open | PageWindow | On => return Ok(None),
Open => (vec![Any], Nominal(receiver), CallEffect::Mutating),
PageWindow => (vec![], Any, CallEffect::ReadOnly),
On => (
vec![TextType, Function, Any],
nominal("Subscription")?,
CallEffect::Mutating,
),
_ => return Err(invalid("unsupported native method signature")),
};
let arity = operation.arity();
let mut parameters = vec![Nominal(receiver)];
parameters.extend(arguments);
Ok(Some(CallSignature {
let mut signature = CallSignature {
arity: endor_core::CallArity::range(arity.required + 1, arity.maximum + 1),
parameters,
parameters: parameters.into_iter().map(CallType::from).collect(),
variadic: None,
returns,
returns: returns.into(),
effect,
}))
};
match operation {
Open => signature.parameters[1] = CallType::nullable(nominal("Window")?),
PageWindow => signature.returns = CallType::nullable(nominal("Window")?),
On => signature.parameters[3] = CallType::Union(vec![AnnotationType::Bool, TextType]),
_ => {}
}
Ok(signature)
}
#[derive(Debug)]
@@ -397,7 +408,7 @@ struct Function {
/// individual Blocks no longer allocate owner-capturing method closures.
#[derive(Debug)]
struct TypeMethod {
signature: Option<CallSignature>,
signature: CallSignature,
operation: Operation,
expected: ObjectRef,
session: Shared,
@@ -405,7 +416,7 @@ struct TypeMethod {
impl NativeCallable for TypeMethod {
fn signature(&self) -> Option<CallSignature> {
self.signature.clone()
Some(self.signature.clone())
}
fn arity(&self) -> Option<endor_core::CallArity> {
let arity = self.operation.arity();