This commit is contained in:
2026-04-27 20:38:49 -05:00
parent 7b39a40a32
commit 960cc76005
5 changed files with 90 additions and 18 deletions

View File

@@ -47,17 +47,12 @@ TEST(Bytecode, Constants)
bp.constants.emplace_back(42);
bp.constants.emplace_back("HELLO");
Bytecode bc(bp);
auto binary = bc.data();
ByteArray ba;
ba.append_int(42);
std::vector<uint8_t> expected = {
// header
0x38, 0xc1, 0xb3, 0x74, // magic
0x01, 0x00, 0x00, 0x00, // version
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// index
0x30, 0x00, 0x00, 0x00,
@@ -74,10 +69,47 @@ TEST(Bytecode, Constants)
0x01, 0x00, 0x00, 0x00,
// constant values
0x54, 'H', 'e', 'l', 'l', 'o', 0x00
0x54, 'H', 'E', 'L', 'L', 'O', 0x00
};
ASSERT_EQ(binary, expected);
Bytecode bc(bp);
ASSERT_EQ(bc.data(), expected);
}
TEST(Bytecode, Code)
{
BytecodePrototype bp;
auto& f = bp.functions.emplace_back(0, 0);
f.code.append_byte(0x68);
f.code.append_int(42);
f = bp.functions.emplace_back(2, 1);
f.code.append_byte(0x42);
std::vector<uint8_t> expected = {
// header
0x38, 0xc1, 0xb3, 0x74, // magic
0x01, 0x00, 0x00, 0x00, // version
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// index
0x00, 0x00, 0x00, 0x00,
0x30, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// function definitions
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,
// code
0x68, 0x54, 0x42,
};
}
int main(int argc, char** argv)