Register typed GUI capabilities and reject incompatible presenters early
This commit is contained in:
+42
-13
@@ -1,5 +1,8 @@
|
||||
//! Host capability definitions over Aster's explicit managed trait relations.
|
||||
use endor_core::{MethodRequirement, ObjectRef, Value, Vm, VmError};
|
||||
use endor_core::{
|
||||
AnnotationType, CallArity, CallEffect, CallSignature, ObjectRef, TypedMethodRequirement, Value,
|
||||
Vm, VmError,
|
||||
};
|
||||
|
||||
pub fn install(
|
||||
vm: &mut Vm,
|
||||
@@ -38,22 +41,48 @@ pub fn install(
|
||||
),
|
||||
("Presentable", vec![("present", 1)], None),
|
||||
] {
|
||||
let requirements = methods
|
||||
.into_iter()
|
||||
.map(|(name, parameters)| MethodRequirement {
|
||||
name: name.into(),
|
||||
parameters,
|
||||
})
|
||||
.collect();
|
||||
let capability = vm.new_trait(name, requirements)?;
|
||||
let owner_definition = if let Some(owner) = owner {
|
||||
let Value::Object(definition) = vm.get_attr(ui, owner)? else {
|
||||
return Err(VmError::InvalidArgument("missing protocol owner"));
|
||||
};
|
||||
Some(definition)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut requirements = Vec::new();
|
||||
for (method, parameters) in methods {
|
||||
let mut signature = if let Some(owner) = owner_definition {
|
||||
let function = vm
|
||||
.object_store()
|
||||
.lookup_method(owner, method)?
|
||||
.ok_or(VmError::InvalidArgument("missing protocol method"))?;
|
||||
vm.object_store()
|
||||
.native_signature(function)?
|
||||
.ok_or(VmError::InvalidArgument(
|
||||
"protocol method lacks native types",
|
||||
))?
|
||||
} else {
|
||||
let window = crate::ui_bridge::desktop_type(vm.object_store(), "Window", desktop)?;
|
||||
let block = crate::ui_bridge::desktop_type(vm.object_store(), "Block", desktop)?;
|
||||
CallSignature::exact(
|
||||
vec![AnnotationType::Any, AnnotationType::Nominal(window)],
|
||||
AnnotationType::Nominal(block),
|
||||
CallEffect::External,
|
||||
)
|
||||
};
|
||||
signature.arity = CallArity::exact(parameters + 1);
|
||||
signature.parameters[0] = AnnotationType::Any.into();
|
||||
requirements.push(TypedMethodRequirement {
|
||||
name: method.into(),
|
||||
signature,
|
||||
});
|
||||
}
|
||||
let capability = vm.new_typed_trait(name, requirements)?;
|
||||
roots.push(capability);
|
||||
vm.attach_type(capability, trait_type)?;
|
||||
vm.set_attr(ui, name, Value::Object(capability))?;
|
||||
vm.dict_set(types, Value::Text(name.into()), Value::Object(capability))?;
|
||||
if let Some(owner) = owner {
|
||||
let Value::Object(owner) = vm.get_attr(ui, owner)? else {
|
||||
return Err(VmError::InvalidArgument("missing protocol owner"));
|
||||
};
|
||||
if let Some(owner) = owner_definition {
|
||||
vm.implement_trait(owner, capability)?;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user