feat: inherit native allocators and forward managed field storage

This commit is contained in:
2026-10-21 02:05:52 +08:00
parent a970fd7707
commit be9a032647
13 changed files with 275 additions and 23 deletions
+108
View File
@@ -1,7 +1,115 @@
use compiler::{CompileOptions, compile_source};
use endor_core::{
ContextNativeFunction, EndorTypeObject, NativeCallable, NativeContext, NativeResult,
ObjectPayload, TypeConstructor, VmError,
};
use endor_core::{ObjectRef, Value, Vm};
use frontend::{EndorReader, ParseMode};
#[derive(Debug)]
struct NativeWidget(String);
impl ObjectPayload for NativeWidget {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn kind_name(&self) -> &'static str {
"widget"
}
fn allows_attributes(&self) -> bool {
true
}
}
#[derive(Debug)]
struct WidgetFactory;
impl NativeCallable for WidgetFactory {
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
let [
Value::Object(requested),
Value::Text(native_label),
values @ ..,
] = args
else {
return Err(VmError::InvalidArgument("widget constructor arguments"));
};
let fields = cx
.store()
.payload::<EndorTypeObject>(*requested, "Type")?
.fields()
.to_vec();
if values.len() != fields.len() {
return Err(VmError::InvalidArgument("widget field count"));
}
let instance = cx.allocate(NativeWidget(native_label.clone()))?;
cx.attach_type(instance, *requested)?;
for (field, value) in fields.iter().zip(values) {
cx.set_attr(instance, &field.name, value.clone())?;
}
Ok(NativeResult::Return(Value::Object(instance)))
}
}
#[test]
fn source_subclasses_preserve_native_allocation_fields_and_nominal_identity() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let roots = vm.bootstrap_types().unwrap();
let factory = vm
.allocate(ContextNativeFunction::new("widget", WidgetFactory, vec![]))
.unwrap();
let widget = vm
.new_type(
"Widget",
roots.object,
TypeConstructor::NativeTyped(factory),
)
.unwrap();
for (name, object) in [
("Object", roots.object),
("Type", roots.type_type),
("Widget", widget),
] {
vm.dict_set(globals, Value::Text(name.into()), Value::Object(object))
.unwrap();
vm.release(object).unwrap();
}
vm.release(factory).unwrap();
let source = "class Child: Widget { value: Number }\nclass Grand: Child { label: String }";
assert_eq!(
execute(&mut vm, globals, source, ParseMode::Module),
Value::Nil
);
let value = execute(
&mut vm,
globals,
"Grand(\"native\", 9, \"extra\")",
ParseMode::Expression,
);
let Value::Object(instance) = value else {
panic!("native instance")
};
assert_eq!(
vm.payload::<NativeWidget>(instance, "widget").unwrap().0,
"native"
);
assert!(vm.is_instance(instance, widget).unwrap());
assert_eq!(
vm.get_attr(instance, "value").unwrap(),
Value::Number(9_i64.into())
);
assert_eq!(
vm.get_attr(instance, "label").unwrap(),
Value::Text("extra".into())
);
vm.dict_clear(globals).unwrap();
vm.release(globals).unwrap();
vm.collect_cycles();
assert!(vm.is_instance(instance, widget).unwrap());
vm.release(instance).unwrap();
vm.collect_cycles();
assert_eq!(vm.object_count(), 0);
}
fn execute(vm: &mut Vm, globals: ObjectRef, source: &str, mode: ParseMode) -> Value {
let names = vm
.dict_items(globals)