feat: add managed types, source classes and scoped native calls
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
use compiler::{CompileOptions, compile_source};
|
||||
use endor_core::{ObjectRef, Value, Vm};
|
||||
use frontend::{EndorReader, ParseMode};
|
||||
|
||||
fn execute(vm: &mut Vm, globals: ObjectRef, source: &str, mode: ParseMode) -> Value {
|
||||
let names = vm
|
||||
.dict_items(globals)
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter_map(|(name, _)| {
|
||||
if let Value::Text(name) = name {
|
||||
Some(name.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let source = EndorReader::from_text("<classes>", source);
|
||||
let template = compile_source(&source, mode, &CompileOptions { globals: names }).unwrap();
|
||||
let code = template.load(vm).unwrap();
|
||||
let result = vm.execute(code, globals).unwrap();
|
||||
vm.release(code).unwrap();
|
||||
result
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_inheritance_binding_and_dynamic_dispatch_keep_the_real_receiver_alive() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let roots = vm.bootstrap_types().unwrap();
|
||||
for (name, object) in [("Object", roots.object), ("Type", roots.type_type)] {
|
||||
vm.dict_set(globals, Value::Text(name.into()), Value::Object(object))
|
||||
.unwrap();
|
||||
vm.release(object).unwrap();
|
||||
}
|
||||
let source = r#"
|
||||
class Base {
|
||||
value: Number
|
||||
function read(self: Base): Number { return self.value }
|
||||
function via(self: Base): Number { return self.read() }
|
||||
function withEvent(self: Base, event: Object): Object { return event }
|
||||
}
|
||||
class Child: Base {
|
||||
label: String
|
||||
function read(self: Child): Number { return 12 }
|
||||
}
|
||||
"#;
|
||||
assert_eq!(
|
||||
execute(&mut vm, globals, source, ParseMode::Module),
|
||||
Value::Nil
|
||||
);
|
||||
let callback = execute(
|
||||
&mut vm,
|
||||
globals,
|
||||
"Child(9, \"ok\").via",
|
||||
ParseMode::Expression,
|
||||
);
|
||||
let Value::Object(callback) = callback else {
|
||||
panic!("bound callback")
|
||||
};
|
||||
assert_eq!(
|
||||
vm.object_store()
|
||||
.positional_parameter_count(callback)
|
||||
.unwrap(),
|
||||
Some(0)
|
||||
);
|
||||
let with_event = execute(
|
||||
&mut vm,
|
||||
globals,
|
||||
"Child(9, \"ok\").withEvent",
|
||||
ParseMode::Expression,
|
||||
);
|
||||
let Value::Object(with_event) = with_event else {
|
||||
panic!("bound callback")
|
||||
};
|
||||
assert_eq!(
|
||||
vm.object_store()
|
||||
.positional_parameter_count(with_event)
|
||||
.unwrap(),
|
||||
Some(1)
|
||||
);
|
||||
vm.release(with_event).unwrap();
|
||||
vm.dict_clear(globals).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
vm.collect_cycles();
|
||||
// No global name or external instance root remains. The bound callback
|
||||
// alone retains its receiver and can still dispatch to Child.read.
|
||||
assert_eq!(
|
||||
vm.call(callback, &[]).unwrap(),
|
||||
Value::Number(12_i64.into())
|
||||
);
|
||||
vm.release(callback).unwrap();
|
||||
vm.collect_cycles();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_members_and_missing_receivers_fail_before_execution() {
|
||||
for text in [
|
||||
"class Bad { value: Number value: String }",
|
||||
"class Bad { function read(): Number { return 1 } }",
|
||||
] {
|
||||
let source = EndorReader::from_text("<invalid-class>", text);
|
||||
assert!(
|
||||
compile_source(
|
||||
&source,
|
||||
ParseMode::Module,
|
||||
&CompileOptions {
|
||||
globals: vec!["Object".into()]
|
||||
}
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user