221 lines
8.4 KiB
Rust
221 lines
8.4 KiB
Rust
//! 声明宿主方法在 Endor 类型系统中的参数和返回契约。
|
|
//! 签名用于构造/调用检查,应与 calls 和各 NativeCallable 实现保持一致;这里不执行实际业务操作。
|
|
//!
|
|
//! Canonical contracts for native Block methods and constructors.
|
|
use super::*;
|
|
|
|
/// 根据宿主方法类别提供 Endor 方法的签名,供类型与调用检查使用。
|
|
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 {
|
|
Translate3D | Rotate3D => (vec![Number, Number, Number], Unit, CallEffect::Mutating),
|
|
CameraControl => (
|
|
vec![nominal("Object")?, nominal("CameraGesture")?],
|
|
Unit,
|
|
CallEffect::Mutating,
|
|
),
|
|
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)
|
|
}
|
|
|
|
/// 将具体宿主 Operation 映射到参数/返回类型契约。
|
|
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 | Overlay => (
|
|
vec![Number, Number],
|
|
Some(nominal("Block")?),
|
|
nominal(if matches!(operation, Row) {
|
|
"Row"
|
|
} else if matches!(operation, Overlay) {
|
|
"Overlay"
|
|
} else {
|
|
"Column"
|
|
})?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
Button => (
|
|
vec![TextType],
|
|
None,
|
|
nominal("Button")?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
Page => (vec![], None, nominal("Page")?, CallEffect::ReadOnly),
|
|
Viewport3D => (
|
|
vec![Number, Number, nominal("Scene3D")?],
|
|
None,
|
|
nominal("Viewport3D")?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
SceneObject(kind) => {
|
|
let parameters = kind
|
|
.parameters()
|
|
.iter()
|
|
.map(|name| match *name {
|
|
"Number" => Ok(Number),
|
|
"String" => Ok(TextType),
|
|
name => nominal(name),
|
|
})
|
|
.collect::<Result<Vec<_>, VmError>>()?;
|
|
(
|
|
parameters,
|
|
if kind.variadic() {
|
|
Some(nominal("Object")?)
|
|
} else {
|
|
None
|
|
},
|
|
nominal(kind.name())?,
|
|
CallEffect::ReadOnly,
|
|
)
|
|
}
|
|
Scene2D => (
|
|
vec![],
|
|
Some(nominal("Block")?),
|
|
nominal("Scene2D")?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
CanvasViewport => (
|
|
vec![Number, Number, nominal("Scene2D")?],
|
|
None,
|
|
nominal("CanvasViewport")?,
|
|
CallEffect::ReadOnly,
|
|
),
|
|
Camera2D => (
|
|
vec![Number, Number, Number],
|
|
None,
|
|
nominal("Camera2D")?,
|
|
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,
|
|
})
|
|
}
|