merge: resolve manuscript conflicts with latest main comments
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//! 给源协议返回的对象列表提供受控封装。列表内容通过托管引用被追踪,读方法只暴露检查后的条目。
|
||||
//! 用于儿童组成、布局结果和几何输入等需要保留对象身份的场景;不是窗口线程共享的可变容器。
|
||||
//! 用于子节点组成、布局结果和几何输入等需要保留对象身份的场景;不是窗口线程共享的可变容器。
|
||||
//!
|
||||
//! Managed indexed host results; values and the at method are traced edges.
|
||||
use endor_core::{
|
||||
@@ -46,16 +46,17 @@ impl NativeCallable for At {
|
||||
endor_core::CallEffect::ReadOnly,
|
||||
))
|
||||
}
|
||||
/// 声明宿主可调用对象接受的参数个数,供 VM 在执行前检查。
|
||||
/// 绑定列表接收者已捕获,只接受一个显式索引。
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::exact(1))
|
||||
}
|
||||
|
||||
/// VM 调入本宿主能力的入口;按所属操作解码参数并返回托管值或后续调用。
|
||||
/// 要求索引可精确转为非负平台整数,检查列表范围后返回对应托管成员,不复制成员对象。
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let [Value::Number(index)] = args else {
|
||||
return Err(VmError::InvalidArgument("object list at expects an index"));
|
||||
};
|
||||
// 先要求可精确表示为 i64 的整数,再转为 usize,拒绝小数、负数及目标平台越界值。
|
||||
let index = index
|
||||
.as_i64()
|
||||
.and_then(|v| usize::try_from(v).ok())
|
||||
@@ -145,7 +146,8 @@ pub fn collection(
|
||||
build(cx, mounts, desktop, None)
|
||||
}
|
||||
|
||||
/// 根据当前输入构造本模块的派生结果,具体的验证与资源边界见函数内步骤。
|
||||
/// 建立列表 payload 和 first/count/at 属性;definition 指定子类时保留该名义类型。
|
||||
/// 最多接受 10000 个成员;空列表的 first 为 Nil,at 越界仍报错而不返回 Nil。
|
||||
fn build(
|
||||
cx: &mut NativeContext<'_>,
|
||||
mounts: Vec<ObjectRef>,
|
||||
@@ -157,6 +159,7 @@ fn build(
|
||||
"object list exceeds 10000 members",
|
||||
));
|
||||
}
|
||||
// 在移动 Vec 之前提取元信息;成员对象的拥有边由 ObjectList::trace 声明。
|
||||
let first = mounts
|
||||
.first()
|
||||
.copied()
|
||||
@@ -171,6 +174,7 @@ fn build(
|
||||
}
|
||||
cx.set_attr(set, "first", first)?;
|
||||
cx.set_attr(set, "count", Value::Number((count as i64).into()))?;
|
||||
// at 捕获列表,列表属性又指向 at,形成托管环;显式 captures 让 GC 能追踪和回收此环。
|
||||
let at = cx.allocate(ContextNativeFunction::new("at", At(set), vec![set]))?;
|
||||
crate::ui_bridge::attach_desktop_type(cx, at, "Function", desktop)?;
|
||||
cx.set_attr(set, "at", Value::Object(at))?;
|
||||
@@ -213,21 +217,23 @@ impl NativeCallable for Constructor {
|
||||
fn signature(&self) -> Option<endor_core::CallSignature> {
|
||||
self.signature.clone()
|
||||
}
|
||||
/// 声明宿主可调用对象接受的参数个数,供 VM 在执行前检查。
|
||||
/// 至少包含请求 Type,之后为可变成员对象与源子类字段,具体分段在 invoke 校验。
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::range(1, usize::MAX))
|
||||
}
|
||||
|
||||
/// VM 调入本宿主能力的入口;按所属操作解码参数并返回托管值或后续调用。
|
||||
/// 按请求类型字段数切分成员/字段,禁止覆盖列表协议保留属性,验证后分配列表并初始化源字段。
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let [Value::Object(definition), values @ ..] = args else {
|
||||
return Err(VmError::InvalidArgument("ObjectList type argument"));
|
||||
};
|
||||
// 首参是要构造的名义类型,余下实参分成“成员对象前缀”和“子类字段后缀”。
|
||||
let fields = cx
|
||||
.store()
|
||||
.payload::<endor_core::EndorTypeObject>(*definition, "Type")?
|
||||
.fields()
|
||||
.to_vec();
|
||||
// 列表查询和修改方法均由原生协议保留,子类字段不得覆盖这些名称。
|
||||
if fields.iter().any(|field| {
|
||||
matches!(
|
||||
field.name.as_str(),
|
||||
@@ -236,6 +242,7 @@ impl NativeCallable for Constructor {
|
||||
}) {
|
||||
return Err(VmError::InvalidArgument("reserved ObjectList field"));
|
||||
}
|
||||
// 字段按类型定义顺序占据最后 N 项;checked_sub 在实参不足时返回错误,避免下标下溢。
|
||||
let count = values
|
||||
.len()
|
||||
.checked_sub(fields.len())
|
||||
@@ -244,6 +251,7 @@ impl NativeCallable for Constructor {
|
||||
))?;
|
||||
cx.store()
|
||||
.check_class_field_arguments(*definition, &values[count..])?;
|
||||
// 字段值先按声明检查;成员前缀另行要求全部是对象,禁止把标量装入宿主对象列表。
|
||||
let members = values[..count]
|
||||
.iter()
|
||||
.map(|value| {
|
||||
|
||||
Reference in New Issue
Block a user