163 lines
6.4 KiB
Rust
163 lines
6.4 KiB
Rust
//! Canonical contracts for native Block methods and constructors.
|
|
use super::*;
|
|
|
|
pub(super) fn method_signature(
|
|
store: &endor_core::ObjectStore,
|
|
desktop: ObjectRef,
|
|
receiver: ObjectRef,
|
|
operation: Operation,
|
|
) -> 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);
|
|
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::External),
|
|
OnMount | OnUnmount => (vec![nominal("Mount")?], Unit, CallEffect::External),
|
|
Click => (
|
|
vec![nominal("Mount")?],
|
|
nominal("Event")?,
|
|
CallEffect::External,
|
|
),
|
|
Dispatch => (
|
|
vec![nominal("Event")?, nominal("Mount")?],
|
|
nominal("Event")?,
|
|
CallEffect::External,
|
|
),
|
|
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),
|
|
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);
|
|
let mut signature = CallSignature {
|
|
arity: endor_core::CallArity::range(arity.required + 1, arity.maximum + 1),
|
|
parameters: parameters.into_iter().map(CallType::from).collect(),
|
|
variadic: None,
|
|
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)
|
|
}
|
|
|
|
pub(super) fn operation_signature(
|
|
store: &endor_core::ObjectStore,
|
|
desktop: ObjectRef,
|
|
operation: Operation,
|
|
) -> Result<CallSignature, VmError> {
|
|
use AnnotationType::{Any, Bool, Number, String as TextType, Trait, Type, Unit};
|
|
use Operation::*;
|
|
let nominal = |name| desktop_type(store, name, desktop).map(AnnotationType::Nominal);
|
|
let (parameters, variadic, returns, effect) = match operation {
|
|
IsInstance => (vec![Any, Type], None, Bool, CallEffect::ReadOnly),
|
|
IsSubtype => (vec![Type, Type], None, Bool, CallEffect::ReadOnly),
|
|
Implement => (vec![Type, Trait], None, Unit, CallEffect::Mutating),
|
|
Implements => (vec![Type, Trait], None, Bool, CallEffect::ReadOnly),
|
|
Supports => (vec![Any, Trait], None, Bool, CallEffect::ReadOnly),
|
|
OpenValue => (vec![Any], None, nominal("Page")?, CallEffect::Mutating),
|
|
Present => (vec![Any], None, nominal("Block")?, CallEffect::Delegated),
|
|
State => (vec![Any], None, Any, CallEffect::ReadOnly),
|
|
StateSet => (vec![Any], None, Unit, CallEffect::Mutating),
|
|
Increment => (vec![], None, Unit, CallEffect::Mutating),
|
|
Stats => (vec![], None, Any, CallEffect::External),
|
|
Viewport => (
|
|
vec![],
|
|
None,
|
|
nominal("MeasureResult")?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
Window => (vec![], None, nominal("Window")?, CallEffect::ReadOnly),
|
|
NewWindow => (vec![], None, nominal("Window")?, CallEffect::Mutating),
|
|
TextOf => (
|
|
vec![Any, TextType, Number, TextType],
|
|
None,
|
|
nominal("TextBlock")?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
Text => (
|
|
vec![TextType, Number, TextType],
|
|
None,
|
|
nominal("TextBlock")?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
Rect => (
|
|
vec![Number, Number, Number, TextType],
|
|
None,
|
|
nominal("RectBlock")?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
Spacer => (vec![Number], None, nominal("Spacer")?, CallEffect::ReadOnly),
|
|
Scroll => (
|
|
vec![Number, Number, nominal("Block")?],
|
|
None,
|
|
nominal("ScrollBlock")?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
Row | Column => (
|
|
vec![Number, Number],
|
|
Some(nominal("Block")?),
|
|
nominal(if matches!(operation, Row) {
|
|
"Row"
|
|
} else {
|
|
"Column"
|
|
})?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
Button => (
|
|
vec![TextType],
|
|
None,
|
|
nominal("Button")?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
Page => (vec![], None, nominal("Page")?, CallEffect::ReadOnly),
|
|
_ => return Err(invalid("unsupported UI function signature")),
|
|
};
|
|
Ok(CallSignature {
|
|
arity: operation.arity(),
|
|
parameters: parameters.into_iter().map(CallType::from).collect(),
|
|
variadic: variadic.map(CallType::from),
|
|
returns: returns.into(),
|
|
effect,
|
|
})
|
|
}
|