From fe15ff909f9edbbc485ae66d54d6415bf0843a6d Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 24 Apr 2017 15:52:27 +0200 Subject: lun/dump.js use 32bit integers --- src/ldo.js | 2 +- src/ldump.js | 8 ++++---- src/lundump.js | 25 ++++++++++++++++++++++--- tests/loadfile-test.bc | Bin 142 -> 138 bytes 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/ldo.js b/src/ldo.js index 3bdd5b5..4105722 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -565,7 +565,7 @@ const f_parser = function(L, p) { let c = p.z.getc(); /* read first character */ if (c === lua.LUA_SIGNATURE.charCodeAt(0)) { checkmode(L, p.mode, lua.to_luastring("binary", true)); - cl = new BytecodeParser(L, p.z.buffer).luaU_undump(); + cl = new BytecodeParser(L, p.z.buffer, p.name).luaU_undump(); } else { checkmode(L, p.mode, lua.to_luastring("text", true)); cl = lparser.luaY_parser(L, p.z, p.buff, p.dyd, p.name, c); diff --git a/src/ldump.js b/src/ldump.js index 98c48ef..e35d3be 100644 --- a/src/ldump.js +++ b/src/ldump.js @@ -48,10 +48,10 @@ const DumpInt = function(x, D) { }; const DumpInteger = function(x, D) { - let dv = new DataView(new ArrayBuffer(8)); + let dv = new DataView(new ArrayBuffer(4)); dv.setInt32(0, x, true); let t = []; - for (let i = 0; i < 8; i++) + for (let i = 0; i < 4; i++) t.push(dv.getUint8(i, true)); DumpBlock(t, 8, D); }; @@ -176,7 +176,7 @@ const DumpHeader = function(D) { DumpByte(4, D); // intSize DumpByte(8, D); // size_tSize DumpByte(4, D); // instructionSize - DumpByte(8, D); // integerSize + DumpByte(4, D); // integerSize DumpByte(8, D); // numberSize DumpInteger(LUAC_INT, D); DumpNumber(LUAC_NUM, D); @@ -198,4 +198,4 @@ const luaU_dump = function(L, f, w, data, strip) { return D.status; }; -module.exports.luaU_dump = luaU_dump; \ No newline at end of file +module.exports.luaU_dump = luaU_dump; diff --git a/src/lundump.js b/src/lundump.js index 36fd674..b229926 100644 --- a/src/lundump.js +++ b/src/lundump.js @@ -13,16 +13,17 @@ const LUAI_MAXSHORTLEN = 40; class BytecodeParser { - constructor(L, dataView) { + constructor(L, dataView, name) { this.L = L; this.intSize = 4; this.size_tSize = 8; this.instructionSize = 4; - this.integerSize = 8; + this.integerSize = 4; this.numberSize = 8; this.dataView = dataView; this.offset = 0; + this.name = name; } peekByte() { @@ -36,7 +37,7 @@ class BytecodeParser { } peekInteger() { - return this.dataView.getInt32(this.offset, true); // TODO: 64b ? + return this.dataView.getInt32(this.offset, true); } readInteger() { @@ -251,6 +252,12 @@ class BytecodeParser { this.integerSize = this.readByte(); this.numberSize = this.readByte(); + this.checksize(this.intSize, 4, "int"); + this.checksize(this.size_tSize, 8, "size_t"); + this.checksize(this.instructionSize, 4, "instruction"); + this.checksize(this.integerSize, 4, "integer"); + this.checksize(this.numberSize, 8, "number"); + if (this.readInteger() !== 0x5678) throw new Error("endianness mismatch"); @@ -259,6 +266,18 @@ class BytecodeParser { } + error(why) { + const lapi = require('./lapi.js'); + const ldo = require('./ldo.js'); + lapi.lua_pushstring(this.L, lua.to_luastring(`${this.name}: ${why} precompiled chunk`, true)); + ldo.luaD_throw(this.L, lua.thread_status.LUA_ERRSYNTAX); + } + + checksize(byte, size, tname) { + if (byte !== size) + this.error(`${tname} size mismatch in`); + } + luaU_undump() { this.checkHeader(); diff --git a/tests/loadfile-test.bc b/tests/loadfile-test.bc index 502b0ce..db0ac05 100644 Binary files a/tests/loadfile-test.bc and b/tests/loadfile-test.bc differ -- cgit v1.2.3-70-g09d2