diff --git a/src/bytecode/bytecode.cc b/src/bytecode/bytecode.cc index 099a735..c1232fe 100644 --- a/src/bytecode/bytecode.cc +++ b/src/bytecode/bytecode.cc @@ -90,8 +90,8 @@ ByteArray Bytecode::generate(BytecodePrototype const& bp) std::vector> functions; ByteArray code; for (auto const& f: bp.functions) { - functions.emplace_back(std::make_pair(FunctionDef { f.n_pars, f.n_locals }, code.size())); code.append_bytearray(f.code); + functions.emplace_back(std::make_pair(FunctionDef { f.n_pars, f.n_locals }, code.size())); } // @@ -117,13 +117,13 @@ ByteArray Bytecode::generate(BytecodePrototype const& bp) } // functions - size_t functions_start = idx + (constant_table.size() * 4) + ba.size(); - idx += functions_start; + size_t functions_start = idx + (constant_table.size() * 4); + idx = functions_start; uint32_t code_idx = 0; for (auto const& f: functions) { ba.set_uint32(idx, code_idx); ba.set_uint16(idx + 4, f.first.n_params); - ba.set_uint16(idx + 6, f.first.n_params); + ba.set_uint16(idx + 6, f.first.locals); idx += 8; code_idx += f.second; } diff --git a/src/bytecode/tests.cc b/src/bytecode/tests.cc index 8ad0bc5..009f525 100644 --- a/src/bytecode/tests.cc +++ b/src/bytecode/tests.cc @@ -117,16 +117,14 @@ TEST(Bytecode, Code) // function definitions 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, // code 0x68, 0x54, 0x42, }; - /* ByteArray ba = Bytecode::generate(bp); ASSERT_EQ(ba.data(), expected); - */ } TEST(Bytecode, Parsing)