MCPcopy
hub / github.com/bytenode/bytenode / fixBytecode

Function fixBytecode

lib/index.js:324–354  ·  view source on GitHub ↗
(bytecodeBuffer)

Source from the content-addressed store, hash-verified

322
323// TODO: rewrite this function
324const fixBytecode = function (bytecodeBuffer) {
325 if (!isBuffer(bytecodeBuffer)) {
326 throw new Error('bytecodeBuffer must be a buffer object.');
327 }
328
329 const dummyBytecode = compileCode('"ಠ_ಠ"');
330 const version = parseFloat(process.version.slice(1, 5));
331
332 if (process.version.startsWith('v8.8') || process.version.startsWith('v8.9')) {
333 // Node is v8.8.x or v8.9.x
334 if (DEBUG) console.error('[bytenode] fixBytecode branch: legacy v8.8/v8.9 (patch 16-20, 20-24)');
335 dummyBytecode.subarray(16, 20).copy(bytecodeBuffer, 16);
336 dummyBytecode.subarray(20, 24).copy(bytecodeBuffer, 20);
337 } else if (version >= 12) {
338 // Node >= 12 (modern V8 layout). Only the flag hash (offset 12) is patched.
339 //
340 // The previous upper bound (`version <= 23`) accidentally pushed Node 24+ into the
341 // legacy branch below, which ALSO overwrites bytes 16-20 (the read-only snapshot
342 // checksum) with the runtime dummy's value. On Electron 42 / V8 14.8 that masks a
343 // real checksum mismatch and makes V8 hard-abort (SIGTRAP) inside vm.Script instead
344 // of cleanly reporting cachedDataRejected. Patching only the flag hash is the safe,
345 // version-stable behavior shared with Node 12-23.
346 if (DEBUG) console.error('[bytenode] fixBytecode branch: modern node>=12 (patch flag hash 12-16 only)');
347 dummyBytecode.subarray(12, 16).copy(bytecodeBuffer, 12);
348 } else {
349 // Node 9/10/11 (older header layout).
350 if (DEBUG) console.error('[bytenode] fixBytecode branch: node 9/10/11 (patch 12-16, 16-20)');
351 dummyBytecode.subarray(12, 16).copy(bytecodeBuffer, 12);
352 dummyBytecode.subarray(16, 20).copy(bytecodeBuffer, 16);
353 }
354};
355
356// TODO: rewrite this function
357const readSourceHash = function (bytecodeBuffer) {

Callers 1

generateScriptFunction · 0.85

Calls 2

isBufferFunction · 0.85
compileCodeFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…