Skip to content

Commit

Permalink
compiler: Add source code length to bytecode buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMrBonnie committed Nov 1, 2022
1 parent 8f33d81 commit 8a13c0b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
11 changes: 6 additions & 5 deletions compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool Compiler::CompileModule(const std::string& fileName, bool compileDependenci
}

// Write the bytecode to file
std::vector<uint8_t> bytecodeResult = CreateBytecodeBuffer(cache->data, cache->length);
std::vector<uint8_t> bytecodeResult = CreateBytecodeBuffer(cache->data, cache->length, sourceCode.size());
bool writeResult = package->WriteFile(fileName, (void*)bytecodeResult.data(), bytecodeResult.size());
if(!writeResult)
{
Expand Down Expand Up @@ -101,21 +101,22 @@ bool Compiler::IsBytecodeFile(void* buffer, size_t size)
return true;
}

std::vector<uint8_t> Compiler::CreateBytecodeBuffer(const uint8_t* buffer, int length)
std::vector<uint8_t> Compiler::CreateBytecodeBuffer(const uint8_t* buffer, int length, int sourceLength)
{
// Make necessary changes to the bytecode
FixBytecode(buffer);

// Create our own custom bytecode buffer by appending our magic bytes
// at the front, and then the bytecode itself at the end
std::vector<uint8_t> buf;
size_t bufSize = magicBytes.size() + length;
size_t bufSize = magicBytes.size() + sizeof(int) + length;
buf.resize(bufSize);

memcpy(buf.data(), magicBytes.data(), magicBytes.size());
memcpy(buf.data() + magicBytes.size(), buffer, length);
memcpy(buf.data() + magicBytes.size(), &sourceLength, sizeof(int));
memcpy(buf.data() + magicBytes.size() + sizeof(int), buffer, length);

return std::move(buf);
return buf;
}

// Hash for empty module ("")
Expand Down
2 changes: 1 addition & 1 deletion compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace BytecodeCompiler
bool IsBytecodeFile(void* buffer, size_t size);

private:
std::vector<uint8_t> CreateBytecodeBuffer(const uint8_t* buffer, int length);
std::vector<uint8_t> CreateBytecodeBuffer(const uint8_t* buffer, int length, int sourceLength);

static void FixBytecode(const uint8_t* buffer);
};
Expand Down
4 changes: 3 additions & 1 deletion docs/internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ so that the module is working again.
## Format
The bytecode we send to the client has exactly *5 bytes* of magic bytes at the front, these bytes are `ALTBC` to identify the data as alt:V bytecode, when we write it to file.
To read this bytecode, we need to check for these 5 magic bytes at the front, if they match, remove them from the buffer,
After that there are *4 bytes* that form a 4-byte integer that corresponds to the size of the original source code (this is needed because V8 sometimes uses the source code buffer,
when compiling functions, and if the buffer is too small, it crashes) that will be used to create a string to pass to the script compiler with the length of the original source code.
To read this bytecode, we need to check for these 5 magic bytes at the front, if they match, remove them and the 4 bytes from the source code length from the buffer,
and the remaining buffer is the full bytecode generated by V8, which we can then use to instantiate the script.

0 comments on commit 8a13c0b

Please sign in to comment.