feat: support bare name assignments in interactive and function scopes
This commit is contained in:
@@ -368,3 +368,81 @@ fn compiling_ast_rejects_invalid_source_ranges() {
|
||||
}
|
||||
assert!(compile_ast(&root, &source, &CompileOptions::default()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bare_assignments_create_and_replace_real_interactive_globals() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
for text in ["value = \"first\"", "value = \"second\"", "value"] {
|
||||
let options = CompileOptions {
|
||||
globals: vm
|
||||
.dict_items(globals)
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter_map(|(key, _)| match key {
|
||||
Value::Text(name) => Some(name.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
let compiled = compile_source(
|
||||
&EndorReader::from_text("<session>", text),
|
||||
ParseMode::Interactive,
|
||||
&options,
|
||||
)
|
||||
.unwrap();
|
||||
let code = compiled.load(&mut vm).unwrap();
|
||||
let result = vm.execute(code, globals).unwrap();
|
||||
if text == "value" {
|
||||
assert_eq!(result, Value::Text("second".into()));
|
||||
}
|
||||
vm.release_value(result).unwrap();
|
||||
vm.release(code).unwrap();
|
||||
}
|
||||
assert_eq!(
|
||||
vm.dict_get(globals, &Value::Text("value".into())).unwrap(),
|
||||
Some(Value::Text("second".into()))
|
||||
);
|
||||
vm.dict_clear(globals).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
vm.collect_cycles();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn assignments_update_parameters_and_allocate_one_local_slot_per_target() {
|
||||
let compiled = compile(
|
||||
"function main(value: String): String { copy = value copy = \"updated\" value = copy return value }",
|
||||
ParseMode::Module,
|
||||
);
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let (code, globals) = module(&mut vm, &compiled);
|
||||
let result = vm
|
||||
.call(
|
||||
function(&vm, globals, "main"),
|
||||
&[Value::Text("original".into())],
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result, Value::Text("updated".into()));
|
||||
vm.release_value(result).unwrap();
|
||||
cleanup(&mut vm, code, globals);
|
||||
assert!(
|
||||
compile_source(
|
||||
&EndorReader::from_text("<uninitialized>", "function f(): T { x = x return x }"),
|
||||
ParseMode::Module,
|
||||
&CompileOptions::default()
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
compile_source(
|
||||
&EndorReader::from_text(
|
||||
"<duplicate>",
|
||||
"function f(): T { let x = 1 let x = 2 return x }"
|
||||
),
|
||||
ParseMode::Module,
|
||||
&CompileOptions::default()
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user