docs: 逐模块细化中文逻辑注释并覆盖测试与示例
This commit is contained in:
@@ -14,14 +14,18 @@ pub fn install(
|
||||
desktop: ObjectRef,
|
||||
ui: ObjectRef,
|
||||
) -> Result<(), VmError> {
|
||||
// 安装前要求桥接已经创建 Trait 类型与 desktop.types 注册表;缺失时立即失败,不能安装半定义能力。
|
||||
let Value::Object(trait_type) = vm.get_attr(ui, "Trait")? else {
|
||||
return Err(VmError::InvalidArgument("missing Trait type"));
|
||||
};
|
||||
let Value::Object(types) = vm.get_attr(desktop, "types")? else {
|
||||
return Err(VmError::InvalidArgument("missing type registry"));
|
||||
};
|
||||
// 每组声明能力名、方法及显式参数数目、提供默认签名的原生类型。
|
||||
// 签名还含接收者参数,下面会把总 arity 调整为 parameters+1。
|
||||
for (name, methods, owner) in [
|
||||
(
|
||||
// 测量/布局/绘制/命中能力,签名取自 Block 的原生协议实现,保证效果约束与具体方法一致。
|
||||
"Visual",
|
||||
vec![("measure", 2), ("arrange", 2), ("paint", 2), ("hitTest", 2)],
|
||||
Some("Block"),
|
||||
@@ -32,6 +36,7 @@ pub fn install(
|
||||
Some("Block"),
|
||||
),
|
||||
(
|
||||
// 组成编辑能力属于 ContainerBlock,普通叶 Block 不因此获得追加或重排孩子的方法。
|
||||
"Children",
|
||||
vec![
|
||||
("append", 1),
|
||||
@@ -45,6 +50,7 @@ pub fn install(
|
||||
),
|
||||
("Presentable", vec![("present", 1)], None),
|
||||
] {
|
||||
// 从当前 ui 模块查名义类型身份,不靠字符串名称把另一个运行时的类型误接进来。
|
||||
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"));
|
||||
@@ -55,6 +61,7 @@ pub fn install(
|
||||
};
|
||||
let mut requirements = Vec::new();
|
||||
for (method, parameters) in methods {
|
||||
// 优先复用原生方法参数/返回注解和调用效果,缺少原生签名元数据时拒绝安装。
|
||||
let mut signature = if let Some(owner) = owner_definition {
|
||||
let function = vm
|
||||
.object_store()
|
||||
@@ -66,6 +73,7 @@ pub fn install(
|
||||
"protocol method lacks native types",
|
||||
))?
|
||||
} else {
|
||||
// Presentable 没有默认 owner,手工声明 present(receiver, Window) -> Block,调用效果为 External。
|
||||
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(
|
||||
@@ -75,6 +83,7 @@ pub fn install(
|
||||
)
|
||||
};
|
||||
signature.arity = CallArity::exact(parameters + 1);
|
||||
// 接收者类型改为 Any,使能力可由其他符合方法契约的名义类型实现,不把契约绑定死在 Block。
|
||||
signature.parameters[0] = AnnotationType::Any.into();
|
||||
requirements.push(TypedMethodRequirement {
|
||||
name: method.into(),
|
||||
@@ -82,11 +91,13 @@ pub fn install(
|
||||
});
|
||||
}
|
||||
let capability = vm.new_typed_trait(name, requirements)?;
|
||||
// 新能力放入宿主 roots 管理生命周期,再同时暴露于 ui 属性与 desktop 类型注册表。
|
||||
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_definition {
|
||||
// 有默认提供者时显式登记 implements 关系;Presentable 留待具体类型声明,不自动给所有 Block 安装。
|
||||
vm.implement_trait(owner, capability)?;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user