Publish native visual and window type and effect contracts
This commit is contained in:
+72
-2
@@ -3,8 +3,9 @@ use std::sync::{Arc, Mutex};
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use endor_core::{
|
||||
ContextNativeFunction, EndorReal, EndorTypeObject, NativeCallable, NativeContext, NativeResult,
|
||||
ObjectRef, TypeConstructor, Value, Vm, VmError, WeakObjectRef,
|
||||
AnnotationType, CallEffect, CallSignature, ContextNativeFunction, EndorReal, EndorTypeObject,
|
||||
NativeCallable, NativeContext, NativeResult, ObjectRef, TypeConstructor, Value, Vm, VmError,
|
||||
WeakObjectRef,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -320,6 +321,70 @@ impl Operation {
|
||||
}
|
||||
}
|
||||
|
||||
fn method_signature(
|
||||
store: &endor_core::ObjectStore,
|
||||
desktop: ObjectRef,
|
||||
receiver: ObjectRef,
|
||||
operation: Operation,
|
||||
) -> Result<Option<CallSignature>, VmError> {
|
||||
use AnnotationType::{Any, Function, Nominal, Number, String as TextType, Unit};
|
||||
use Operation::*;
|
||||
let nominal = |name| desktop_type(store, name, desktop).map(Nominal);
|
||||
let (arguments, returns, effect) = match operation {
|
||||
Bind => (
|
||||
vec![TextType, Function],
|
||||
nominal("Subscription")?,
|
||||
CallEffect::Mutating,
|
||||
),
|
||||
Mounts => (vec![], nominal("ObjectList")?, CallEffect::ReadOnly),
|
||||
Children => (vec![], Any, CallEffect::ReadOnly),
|
||||
Invalidate => (vec![TextType], Unit, CallEffect::Mutating),
|
||||
SetText | SetColor => (vec![TextType], Unit, CallEffect::Mutating),
|
||||
Append => (vec![nominal("Block")?], Number, CallEffect::Mutating),
|
||||
Insert => (
|
||||
vec![Number, nominal("Block")?],
|
||||
Number,
|
||||
CallEffect::Mutating,
|
||||
),
|
||||
Remove => (vec![Number], Unit, CallEffect::Mutating),
|
||||
Replace => (vec![Number, nominal("Block")?], Unit, CallEffect::Mutating),
|
||||
Move => (vec![Number, Number], Unit, CallEffect::Mutating),
|
||||
Render | Navigate => (vec![nominal("Block")?], Unit, CallEffect::Mutating),
|
||||
OnClick => (vec![Function], Nominal(receiver), CallEffect::Mutating),
|
||||
OnEvent => (vec![nominal("Event")?], Unit, CallEffect::ReadOnly),
|
||||
OnMount | OnUnmount => (vec![nominal("Mount")?], Unit, CallEffect::ReadOnly),
|
||||
Click => (vec![nominal("Mount")?], Unit, CallEffect::Mutating),
|
||||
Dispatch => (
|
||||
vec![nominal("Event")?, nominal("Mount")?],
|
||||
Unit,
|
||||
CallEffect::Mutating,
|
||||
),
|
||||
MoveWindow => (
|
||||
vec![nominal("Window")?],
|
||||
Nominal(receiver),
|
||||
CallEffect::Mutating,
|
||||
),
|
||||
MoveTab => (vec![Number], Nominal(receiver), CallEffect::Mutating),
|
||||
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),
|
||||
_ => return Err(invalid("unsupported native method signature")),
|
||||
};
|
||||
let arity = operation.arity();
|
||||
let mut parameters = vec![Nominal(receiver)];
|
||||
parameters.extend(arguments);
|
||||
Ok(Some(CallSignature {
|
||||
arity: endor_core::CallArity::range(arity.required + 1, arity.maximum + 1),
|
||||
parameters,
|
||||
variadic: None,
|
||||
returns,
|
||||
effect,
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Function {
|
||||
operation: Operation,
|
||||
@@ -332,12 +397,16 @@ struct Function {
|
||||
/// individual Blocks no longer allocate owner-capturing method closures.
|
||||
#[derive(Debug)]
|
||||
struct TypeMethod {
|
||||
signature: Option<CallSignature>,
|
||||
operation: Operation,
|
||||
expected: ObjectRef,
|
||||
session: Shared,
|
||||
}
|
||||
|
||||
impl NativeCallable for TypeMethod {
|
||||
fn signature(&self) -> Option<CallSignature> {
|
||||
self.signature.clone()
|
||||
}
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
let arity = self.operation.arity();
|
||||
Some(endor_core::CallArity::range(
|
||||
@@ -2663,6 +2732,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
|
||||
let method = vm.allocate(ContextNativeFunction::new(
|
||||
name,
|
||||
TypeMethod {
|
||||
signature: method_signature(vm.object_store(), desktop, expected, operation)?,
|
||||
operation,
|
||||
expected,
|
||||
session: session.clone(),
|
||||
|
||||
Reference in New Issue
Block a user