diff --git a/compiler/compiler.cpp b/compiler/compiler.cpp index 058bccb..3fabf32 100644 --- a/compiler/compiler.cpp +++ b/compiler/compiler.cpp @@ -49,7 +49,7 @@ bool Compiler::CompileModule(const std::string& fileName, bool compileDependenci } // Write the bytecode to file - std::vector bytecodeResult = CreateBytecodeBuffer(cache->data, cache->length); + std::vector bytecodeResult = CreateBytecodeBuffer(cache->data, cache->length, sourceCode.size()); bool writeResult = package->WriteFile(fileName, (void*)bytecodeResult.data(), bytecodeResult.size()); if(!writeResult) { @@ -101,7 +101,7 @@ bool Compiler::IsBytecodeFile(void* buffer, size_t size) return true; } -std::vector Compiler::CreateBytecodeBuffer(const uint8_t* buffer, int length) +std::vector Compiler::CreateBytecodeBuffer(const uint8_t* buffer, int length, int sourceLength) { // Make necessary changes to the bytecode FixBytecode(buffer); @@ -109,13 +109,14 @@ std::vector Compiler::CreateBytecodeBuffer(const uint8_t* buffer, int l // Create our own custom bytecode buffer by appending our magic bytes // at the front, and then the bytecode itself at the end std::vector 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 ("") diff --git a/compiler/compiler.h b/compiler/compiler.h index 9001773..8f169e6 100644 --- a/compiler/compiler.h +++ b/compiler/compiler.h @@ -73,7 +73,7 @@ namespace BytecodeCompiler bool IsBytecodeFile(void* buffer, size_t size); private: - std::vector CreateBytecodeBuffer(const uint8_t* buffer, int length); + std::vector CreateBytecodeBuffer(const uint8_t* buffer, int length, int sourceLength); static void FixBytecode(const uint8_t* buffer); }; diff --git a/docs/internal.md b/docs/internal.md index c167ec0..5c280eb 100644 --- a/docs/internal.md +++ b/docs/internal.md @@ -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.