feat: execute source control flow and exact real arithmetic
This commit is contained in:
@@ -262,11 +262,22 @@ fn declared_field_checks_cover_construction_inheritance_writes_and_gc() {
|
||||
r#"
|
||||
class Base { count: Number }
|
||||
class Child: Base { label: String }
|
||||
value = Child(9, "ok")
|
||||
class Holder { child: Base }
|
||||
holder = Holder(value)
|
||||
class Unknown { value: Missing }
|
||||
"#,
|
||||
ParseMode::Module,
|
||||
);
|
||||
// Interactive intentionally accepts one complete statement per source unit.
|
||||
execute(
|
||||
&mut vm,
|
||||
globals,
|
||||
r#"value = Child(9, "ok")"#,
|
||||
ParseMode::Interactive,
|
||||
);
|
||||
execute(
|
||||
&mut vm,
|
||||
globals,
|
||||
"holder = Holder(value)",
|
||||
ParseMode::Interactive,
|
||||
);
|
||||
let Value::Object(value) = vm
|
||||
@@ -334,7 +345,7 @@ fn checked_fields_keep_declaring_self_type_and_can_be_opted_out() {
|
||||
class Link { next: Link }
|
||||
class Seed: Link {}
|
||||
"#,
|
||||
ParseMode::Interactive,
|
||||
ParseMode::Module,
|
||||
);
|
||||
// A recursive nonnullable field can be initialized by the trusted host in two steps.
|
||||
let Value::Object(link) = vm
|
||||
|
||||
@@ -345,18 +345,10 @@ fn extended_operands_work_for_large_local_and_constant_tables() {
|
||||
|
||||
#[test]
|
||||
fn unsupported_execution_features_are_compile_errors_instead_of_fake_results() {
|
||||
for (text, mode) in [
|
||||
("1 + 2", ParseMode::Expression),
|
||||
(".title", ParseMode::Expression),
|
||||
(
|
||||
"function f(value: Number): Number { return -value }",
|
||||
ParseMode::Module,
|
||||
),
|
||||
] {
|
||||
let source = EndorReader::from_text("<unsupported>", text);
|
||||
let error = compile_source(&source, mode, &CompileOptions::default()).unwrap_err();
|
||||
assert_eq!(error.diagnostic.kind, DiagnosticKind::Compilation);
|
||||
}
|
||||
let source = EndorReader::from_text("<unsupported>", ".title");
|
||||
let error =
|
||||
compile_source(&source, ParseMode::Expression, &CompileOptions::default()).unwrap_err();
|
||||
assert_eq!(error.diagnostic.kind, DiagnosticKind::Compilation);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -482,3 +474,79 @@ fn source_signatures_survive_template_load_and_enable_runtime_checks() {
|
||||
assert_eq!(vm.object_count(), baseline);
|
||||
cleanup(&mut vm, code, globals);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_control_flow_and_exact_arithmetic_execute_real_bytecode() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let text = "function main(): Number { i=0 total=0 while i<20 { if i<10 {total=total+i} else {total=total-i} i=i+1 } return total }";
|
||||
let (code, globals) = module(&mut vm, &compile(text, ParseMode::Module));
|
||||
let main = function(&vm, globals, "main");
|
||||
let result = vm.call(main, &[]).unwrap();
|
||||
assert_eq!(result, Value::Number((-100_i64).into()));
|
||||
vm.release_value(result).unwrap();
|
||||
cleanup(&mut vm, code, globals);
|
||||
for (text, expected) in [
|
||||
("0.1 + 0.2 == 0.3", Value::Bool(true)),
|
||||
(
|
||||
"100000000000000000000 + 1 > 100000000000000000000",
|
||||
Value::Bool(true),
|
||||
),
|
||||
("1 / 3 + 1 / 3 + 1 / 3", Value::Number(1_i64.into())),
|
||||
("-(2 + 3)", Value::Number((-5_i64).into())),
|
||||
] {
|
||||
let code = compile(text, ParseMode::Expression).load(&mut vm).unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let result = vm.execute(code, globals).unwrap();
|
||||
assert_eq!(result, expected);
|
||||
vm.release_value(result).unwrap();
|
||||
vm.release(code).unwrap();
|
||||
vm.release(globals).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn control_flow_definite_initialization_large_jumps_and_loop_budgets() {
|
||||
let mut body = String::from("function main(): Number { i=0 while i<2 { if i==0 {");
|
||||
for _ in 0..300 {
|
||||
body.push_str("i=i+0 ");
|
||||
}
|
||||
body.push_str("} i=i+1 } return i }");
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let (code, globals) = module(&mut vm, &compile(&body, ParseMode::Module));
|
||||
let result = vm.call(function(&vm, globals, "main"), &[]).unwrap();
|
||||
assert_eq!(result, Value::Number(2_i64.into()));
|
||||
vm.release_value(result).unwrap();
|
||||
cleanup(&mut vm, code, globals);
|
||||
for text in [
|
||||
"function f(): Number { if 1 {x=2} return x }",
|
||||
"function f(): Number {while 0 {x=2} return x}",
|
||||
] {
|
||||
assert!(
|
||||
compile_source(
|
||||
&EndorReader::from_text("<test>", text),
|
||||
ParseMode::Module,
|
||||
&CompileOptions::default()
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
let (code, globals) = module(
|
||||
&mut vm,
|
||||
&compile(
|
||||
"function main(): Unit { while 1 { value=2 } }",
|
||||
ParseMode::Module,
|
||||
),
|
||||
);
|
||||
assert!(matches!(
|
||||
vm.call_with_limits(
|
||||
function(&vm, globals, "main"),
|
||||
&[],
|
||||
ExecutionLimits {
|
||||
max_frames: 10,
|
||||
max_instructions: 100
|
||||
}
|
||||
),
|
||||
Err(VmError::ExecutionLimit("instructions"))
|
||||
));
|
||||
cleanup(&mut vm, code, globals);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user