This commit is contained in:
2026-04-28 09:42:27 -05:00
parent 8ff66ac1e2
commit 44a51acad1
6 changed files with 107 additions and 32 deletions

View File

@@ -44,8 +44,10 @@ Bytecode::Bytecode(BytecodePrototype const& bp)
byte_array_.append_bytearray(constant_array);
// constant index
if (!constant_table.empty())
if (!constant_table.empty()) {
byte_array_.set_uint32(HEADER_SZ, HEADER_SZ + INDEX_SZ);
byte_array_.set_uint16(HEADER_SZ + 4, constant_table.size());
}
// functions
size_t functions_start = idx + (constant_table.size() * 4) + byte_array_.size();
@@ -62,31 +64,53 @@ Bytecode::Bytecode(BytecodePrototype const& bp)
byte_array_.append_bytearray(f.code);
// function index
if (!functions.empty())
byte_array_.set_uint32(HEADER_SZ + 4, functions_start);
if (!functions.empty()) {
byte_array_.set_uint32(HEADER_SZ + 6, functions_start);
byte_array_.set_uint16(HEADER_SZ + 6 + 4, functions.size());
}
}
Bytecode::Bytecode(std::vector<uint8_t> data)
: byte_array_(std::move(data))
{
// check file size
if (byte_array_.size() < (HEADER_SZ + INDEX_SZ))
throw BytecodeParsingError("Invalid bytecode format (file too short)");
// check magic number and version
if (byte_array_.get_uint32(0) != MAGIC)
throw BytecodeParsingError("Invalid bytecode format (magic number not matching)");
if (byte_array_.get_uint32(4) != VERSION)
throw BytecodeParsingError("Unexpected bytecode format version");
// load cache
cache_.constants_addr = byte_array_.get_uint32(HEADER_SZ);
cache_.n_constants = byte_array_.get_uint16(HEADER_SZ + 4);
cache_.functions_addr = byte_array_.get_uint32(HEADER_SZ + 6);
cache_.n_functions = byte_array_.get_uint16(HEADER_SZ + 10);
}
uint32_t Bytecode::n_constants() const
{
return 0;
return cache_.n_constants;
}
uint32_t Bytecode::n_functions() const
{
return 0;
return cache_.n_functions;
}
int32_t Bytecode::get_constant_int(uint32_t addr) const
int32_t Bytecode::get_constant_int(uint32_t idx) const
{
return 0;
}
float Bytecode::get_constant_float(uint32_t addr) const
float Bytecode::get_constant_float(uint32_t idx) const
{
return 0;
}
std::string Bytecode::get_constant_string(uint32_t addr) const
std::string Bytecode::get_constant_string(uint32_t idx) const
{
return std::string();
}