Skip to content

Commit 2bb5751

Browse files
Make compiler tests independent of VM indexes (#2676)
The compiler unit tests do this: ```rust assert_compiles(Program { source: r#" function main() -> int { let arr = [1, 2, 3]; arr.length() } "#, expected: vec![( "main", vec![ Instruction::LoadConst(0), // Index of the constant in the pool Instruction::LoadConst(1), // Index of the constant in the pool Instruction::LoadConst(2), // Index of the constant in the pool Instruction::AllocArray(3), Instruction::LoadGlobal(GlobalIndex::from_raw(50)), // Index of the function Instruction::LoadVar(1), // Index of the variable Instruction::Call(1), Instruction::Return, ], )], }) ``` Depending on specific object indexes is bad because if we add new builtins they change. Also `LoadConst(0)` or `LoadVar(5)` is not very readable, so instead we want: ```diff assert_compiles(Program { source: r#" function main() -> int { let arr = [1, 2, 3]; arr.length() } "#, expected: vec![( "main", vec![ - Instruction::LoadConst(0), // Index of the constant in the pool + Instruction::LoadConst(Value::Int(1)), - Instruction::LoadConst(1), // Index of the constant in the pool + Instruction::LoadConst(Value::Int(2)), - Instruction::LoadConst(2), // Index of the constant in the pool + Instruction::LoadConst(Value::Int(3)), Instruction::AllocArray(3), - Instruction::LoadGlobal(GlobalIndex::from_raw(50)), // Index of the function + Instruction::LoadGlobal(Value::function("baml.Array.length")), - Instruction::LoadVar(1), // Index of the variable + Instruction::LoadVar("arr".to_string()), Instruction::Call(1), Instruction::Return, ], )], }) ```
1 parent b24d428 commit 2bb5751

23 files changed

+988
-728
lines changed

engine/baml-compiler/tests/arrays.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Compiler tests for array construction.
22
3-
use baml_vm::Instruction;
3+
use baml_vm::test::{Instruction, Value};
44

55
mod common;
66
use common::{assert_compiles, Program};
@@ -17,11 +17,11 @@ fn array_constructor() -> anyhow::Result<()> {
1717
expected: vec![(
1818
"main",
1919
vec![
20-
Instruction::LoadConst(0),
21-
Instruction::LoadConst(1),
22-
Instruction::LoadConst(2),
20+
Instruction::LoadConst(Value::Int(1)),
21+
Instruction::LoadConst(Value::Int(2)),
22+
Instruction::LoadConst(Value::Int(3)),
2323
Instruction::AllocArray(3),
24-
Instruction::LoadVar(1),
24+
Instruction::LoadVar("a".to_string()),
2525
Instruction::Return,
2626
],
2727
)],

engine/baml-compiler/tests/assertions.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Compiler tests for assert statements.
22
3-
use baml_vm::{BinOp, CmpOp, Instruction};
3+
use baml_vm::{
4+
test::{Instruction, Value},
5+
BinOp, CmpOp,
6+
};
47

58
mod common;
69
use common::{assert_compiles, Program};
@@ -17,13 +20,13 @@ fn assert_statement_ok() -> anyhow::Result<()> {
1720
expected: vec![(
1821
"assertOk",
1922
vec![
20-
Instruction::LoadConst(0), // 2
21-
Instruction::LoadConst(1), // 2
23+
Instruction::LoadConst(Value::Int(2)), // 2
24+
Instruction::LoadConst(Value::Int(2)), // 2
2225
Instruction::BinOp(BinOp::Add),
23-
Instruction::LoadConst(2), // 4
26+
Instruction::LoadConst(Value::Int(4)), // 4
2427
Instruction::CmpOp(CmpOp::Eq),
2528
Instruction::Assert,
26-
Instruction::LoadConst(3), // 3
29+
Instruction::LoadConst(Value::Int(3)), // 3
2730
Instruction::Return,
2831
],
2932
)],
@@ -42,11 +45,11 @@ fn assert_statement_not_ok() -> anyhow::Result<()> {
4245
expected: vec![(
4346
"assertNotOk",
4447
vec![
45-
Instruction::LoadConst(0), // 3
46-
Instruction::LoadConst(1), // 1
48+
Instruction::LoadConst(Value::Int(3)), // 3
49+
Instruction::LoadConst(Value::Int(1)), // 1
4750
Instruction::CmpOp(CmpOp::Eq),
4851
Instruction::Assert,
49-
Instruction::LoadConst(2), // 2
52+
Instruction::LoadConst(Value::Int(2)), // 2
5053
Instruction::Return,
5154
],
5255
)],

engine/baml-compiler/tests/builtins.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Compiler tests for built-in method calls.
22
3-
use baml_vm::{GlobalIndex, Instruction, ObjectIndex};
3+
use baml_vm::test::{Instruction, Value};
44

55
mod common;
66
use common::{assert_compiles, Program};
@@ -17,12 +17,12 @@ fn builtin_method_call() -> anyhow::Result<()> {
1717
expected: vec![(
1818
"main",
1919
vec![
20-
Instruction::LoadConst(0),
21-
Instruction::LoadConst(1),
22-
Instruction::LoadConst(2),
20+
Instruction::LoadConst(Value::Int(1)),
21+
Instruction::LoadConst(Value::Int(2)),
22+
Instruction::LoadConst(Value::Int(3)),
2323
Instruction::AllocArray(3),
24-
Instruction::LoadGlobal(GlobalIndex::from_raw(5)),
25-
Instruction::LoadVar(1),
24+
Instruction::LoadGlobal(Value::function("baml.Array.length")),
25+
Instruction::LoadVar("arr".to_string()),
2626
// call with one argument (self)
2727
Instruction::Call(1),
2828
Instruction::Return,
@@ -49,9 +49,9 @@ fn fetch_as() -> anyhow::Result<()> {
4949
expected: vec![(
5050
"main",
5151
vec![
52-
Instruction::LoadGlobal(GlobalIndex::from_raw(51)),
53-
Instruction::LoadConst(0),
54-
Instruction::LoadConst(1),
52+
Instruction::LoadGlobal(Value::function("baml.fetch_as")),
53+
Instruction::LoadConst(Value::string("https://dummyjson.com/todos/1")),
54+
Instruction::LoadConst(Value::class("DummyJsonTodo")),
5555
Instruction::DispatchFuture(2),
5656
Instruction::Await,
5757
Instruction::Return,
@@ -86,25 +86,26 @@ fn fetch_as_with_request_param() -> anyhow::Result<()> {
8686
expected: vec![(
8787
"main",
8888
vec![
89-
Instruction::LoadGlobal(GlobalIndex::from_raw(51)),
90-
Instruction::AllocInstance(ObjectIndex::from_raw(7)),
89+
Instruction::LoadGlobal(Value::function("baml.fetch_as")),
90+
Instruction::AllocInstance(Value::class("baml.HttpRequest")),
9191
Instruction::Copy(0),
92-
Instruction::LoadConst(0),
93-
Instruction::AllocVariant(ObjectIndex::from_raw(10)),
92+
Instruction::LoadConst(Value::Int(1)), // Enum variant index for Post
93+
Instruction::AllocVariant(Value::enm("baml.HttpMethod")),
9494
Instruction::StoreField(1),
9595
Instruction::Copy(0),
96-
Instruction::LoadConst(1),
96+
Instruction::LoadConst(Value::string("https://dummyjson.com/todos/add")),
9797
Instruction::StoreField(0),
9898
Instruction::Copy(0),
99-
Instruction::LoadConst(2),
100-
Instruction::LoadConst(3),
101-
Instruction::LoadConst(4),
102-
Instruction::LoadConst(5),
103-
Instruction::LoadConst(6),
104-
Instruction::LoadConst(7),
99+
// Map values first, then keys
100+
Instruction::LoadConst(Value::string("Buy milk")),
101+
Instruction::LoadConst(Value::Bool(false)),
102+
Instruction::LoadConst(Value::Int(5)),
103+
Instruction::LoadConst(Value::string("todo")),
104+
Instruction::LoadConst(Value::string("completed")),
105+
Instruction::LoadConst(Value::string("userId")),
105106
Instruction::AllocMap(3),
106107
Instruction::StoreField(4),
107-
Instruction::LoadConst(8),
108+
Instruction::LoadConst(Value::class("DummyJsonTodo")),
108109
Instruction::DispatchFuture(2),
109110
Instruction::Await,
110111
Instruction::Return,

0 commit comments

Comments
 (0)