feat: support opt-in shared reference assignment for host objects
This commit is contained in:
+27
-5
@@ -220,6 +220,20 @@ impl Vm {
|
||||
Self::stack_value(frame, frame.stack.len() - 1)
|
||||
}
|
||||
|
||||
fn assignment_value(&self, previous: Option<Value>, incoming: Value) -> Result<Value, VmError> {
|
||||
if let (Some(Value::Object(old)), Value::Object(new)) = (&previous, &incoming) {
|
||||
if old == new
|
||||
|| self
|
||||
.object(*old)?
|
||||
.payload()
|
||||
.assign_from(self.object(*new)?.payload())?
|
||||
{
|
||||
return Ok(Value::Object(*old));
|
||||
}
|
||||
}
|
||||
Ok(incoming)
|
||||
}
|
||||
|
||||
fn push_borrowed(&mut self, frame: &mut EndorFrame, value: Value) -> Result<(), VmError> {
|
||||
self.retain_value(&value)?;
|
||||
frame.stack.push(StackEntry::Value(value));
|
||||
@@ -372,9 +386,12 @@ impl Vm {
|
||||
self.push_borrowed(frame, value)?;
|
||||
}
|
||||
Opcode::StoreFast => {
|
||||
let value = Self::top_value(frame)?;
|
||||
// The stack root is moved into the local slot.
|
||||
frame.stack.pop();
|
||||
let value = self.assignment_value(
|
||||
frame.localsplus[argument].clone(),
|
||||
Self::top_value(frame)?,
|
||||
)?;
|
||||
self.retain_value(&value)?;
|
||||
self.discard(frame, 1);
|
||||
if let Some(old) = frame.localsplus[argument].replace(value) {
|
||||
self.drop_root(old);
|
||||
}
|
||||
@@ -398,8 +415,11 @@ impl Vm {
|
||||
self.push_borrowed(frame, value)?;
|
||||
}
|
||||
Opcode::StoreGlobal => {
|
||||
let value = Self::top_value(frame)?;
|
||||
let key = Value::Text(frame.executable.layout().names[argument].clone());
|
||||
let value = self.assignment_value(
|
||||
self.dict_get(frame.globals, &key)?,
|
||||
Self::top_value(frame)?,
|
||||
)?;
|
||||
self.dict_set(frame.globals, key, value)?;
|
||||
self.discard(frame, 1);
|
||||
}
|
||||
@@ -426,7 +446,9 @@ impl Vm {
|
||||
}
|
||||
Opcode::StoreDeref => {
|
||||
let cell = self.cell_slot(frame, argument)?;
|
||||
self.cell_set(cell, Self::top_value(frame)?)?;
|
||||
let value =
|
||||
self.assignment_value(self.cell_get(cell).ok(), Self::top_value(frame)?)?;
|
||||
self.cell_set(cell, value)?;
|
||||
self.discard(frame, 1);
|
||||
}
|
||||
Opcode::DeleteDeref => {
|
||||
|
||||
@@ -9,3 +9,5 @@
|
||||
| [execution.rs](execution.rs) | wordcode 与校验、控制流、参数/默认值、嵌套帧、字节码函数创建/默认值/环境捕获、闭包、返回值存活、失败清理、限额 |
|
||||
|
||||
核心对象现由 objects 定义,本目录保留既有 endor_core 导入与 Vm 方法的兼容性检查。独立对象存储检查见 [objects/tests](../../objects/tests/README.md)。堆内部伪造句柄与计数溢出检查在 objects 的 src/heap.rs,公开示例由 src/lib.rs rustdoc 提供。文件职责见 [src](../src/README.md),检查方式和结果边界见 [验证](../../../docs/operations/validation.md)。本地不执行测试;存在测试文件不代表某次提交已通过。返回 [core](../README.md)。
|
||||
|
||||
execution.rs 同时覆盖宿主可选 assign_from 的全局/局部赋值、别名身份、拒绝替换与执行根清理;不在本地运行。
|
||||
|
||||
@@ -35,6 +35,130 @@ fn return_constant(vm: &mut Vm, value: Value) -> ObjectRef {
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct SharedReference(AtomicUsize);
|
||||
impl ObjectPayload for SharedReference {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"shared-reference"
|
||||
}
|
||||
fn assign_from(&self, source: &dyn ObjectPayload) -> Result<bool, VmError> {
|
||||
let Some(source) = source.as_any().downcast_ref::<Self>() else {
|
||||
return Ok(false);
|
||||
};
|
||||
let value = source.0.load(Ordering::SeqCst);
|
||||
if value == 99 {
|
||||
return Err(VmError::InvalidArgument("rejected replacement"));
|
||||
}
|
||||
self.0.store(value, Ordering::SeqCst);
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opt_in_assignment_keeps_alias_identity_ordinary_values_and_root_accounting() {
|
||||
for local in [false, true] {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let old = vm.allocate(SharedReference(AtomicUsize::new(1))).unwrap();
|
||||
let new = vm.allocate(SharedReference(AtomicUsize::new(2))).unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
vm.dict_set(globals, text("alias"), Value::Object(old))
|
||||
.unwrap();
|
||||
let (store, load) = if local {
|
||||
(Opcode::StoreFast, Opcode::LoadFast)
|
||||
} else {
|
||||
(Opcode::StoreGlobal, Opcode::LoadGlobal)
|
||||
};
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(store, 0),
|
||||
op(Opcode::LoadConst, 1),
|
||||
op(store, 0),
|
||||
op(load, 0),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Object(old), Value::Object(new)],
|
||||
if local {
|
||||
CodeLayout {
|
||||
locals: vec!["value".into()],
|
||||
..CodeLayout::default()
|
||||
}
|
||||
} else {
|
||||
CodeLayout {
|
||||
names: vec!["value".into()],
|
||||
..CodeLayout::default()
|
||||
}
|
||||
},
|
||||
);
|
||||
let value = vm.execute(code, globals).unwrap();
|
||||
assert_eq!(value, Value::Object(old));
|
||||
assert_eq!(
|
||||
vm.dict_get(globals, &text("alias")).unwrap(),
|
||||
Some(Value::Object(old))
|
||||
);
|
||||
assert_eq!(
|
||||
vm.payload::<SharedReference>(old, "shared-reference")
|
||||
.unwrap()
|
||||
.0
|
||||
.load(Ordering::SeqCst),
|
||||
2
|
||||
);
|
||||
vm.release_value(value).unwrap();
|
||||
vm.release(code).unwrap();
|
||||
vm.dict_clear(globals).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
vm.release(old).unwrap();
|
||||
vm.release(new).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_opt_in_assignment_preserves_target_and_cleans_execution_roots() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let old = vm.allocate(SharedReference(AtomicUsize::new(1))).unwrap();
|
||||
let rejected = vm.allocate(SharedReference(AtomicUsize::new(99))).unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
vm.dict_set(globals, text("value"), Value::Object(old))
|
||||
.unwrap();
|
||||
let code = make_code(
|
||||
&mut vm,
|
||||
&[
|
||||
op(Opcode::LoadConst, 0),
|
||||
op(Opcode::StoreGlobal, 0),
|
||||
op(Opcode::LoadConst, 1),
|
||||
simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Object(rejected), Value::Nil],
|
||||
CodeLayout {
|
||||
names: vec!["value".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
);
|
||||
assert!(vm.execute(code, globals).is_err());
|
||||
assert_eq!(
|
||||
vm.payload::<SharedReference>(old, "shared-reference")
|
||||
.unwrap()
|
||||
.0
|
||||
.load(Ordering::SeqCst),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
vm.dict_get(globals, &text("value")).unwrap(),
|
||||
Some(Value::Object(old))
|
||||
);
|
||||
vm.release(code).unwrap();
|
||||
vm.dict_clear(globals).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
vm.release(old).unwrap();
|
||||
vm.release(rejected).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wordcode_extended_operands_decode_and_execute_without_truncation() {
|
||||
let encoded = Instruction::encode_all(&[op(Opcode::LoadConst, 0x01020304)]).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user