summaryrefslogtreecommitdiff
path: root/src/lundump.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/lundump.js')
-rw-r--r--src/lundump.js117
1 files changed, 73 insertions, 44 deletions
diff --git a/src/lundump.js b/src/lundump.js
index e49c376..cf3fafa 100644
--- a/src/lundump.js
+++ b/src/lundump.js
@@ -1,16 +1,46 @@
"use strict";
-const assert = require('assert');
-
-const defs = require('./defs.js');
+const {
+ LUA_SIGNATURE,
+ constant_types: {
+ LUA_TBOOLEAN,
+ LUA_TLNGSTR,
+ LUA_TNIL,
+ LUA_TNUMFLT,
+ LUA_TNUMINT,
+ LUA_TSHRSTR
+ },
+ thread_status: { LUA_ERRSYNTAX },
+ is_luastring,
+ luastring_eq,
+ to_luastring
+} = require('./defs.js');
const ldo = require('./ldo.js');
const lfunc = require('./lfunc.js');
const lobject = require('./lobject.js');
-const lopcodes = require('./lopcodes.js');
-const lstring = require('./lstring.js');
-const lzio = require('./lzio.js');
-
-let LUAC_DATA = [0x19, 0x93, defs.char["\r"], defs.char["\n"], 0x1a, defs.char["\n"]];
+const {
+ MAXARG_sBx,
+ POS_A,
+ POS_Ax,
+ POS_B,
+ POS_Bx,
+ POS_C,
+ POS_OP,
+ SIZE_A,
+ SIZE_Ax,
+ SIZE_B,
+ SIZE_Bx,
+ SIZE_C,
+ SIZE_OP
+} = require('./lopcodes.js');
+const { lua_assert } = require("./llimits.js");
+const { luaS_bless } = require('./lstring.js');
+const {
+ luaZ_read,
+ ZIO
+} = require('./lzio.js');
+
+let LUAC_DATA = [0x19, 0x93, 13, 10, 0x1a, 10];
class BytecodeParser {
@@ -21,13 +51,13 @@ class BytecodeParser {
this.integerSize = 4;
this.numberSize = 8;
- assert(Z instanceof lzio.ZIO, "BytecodeParser only operates on a ZIO");
- assert(defs.is_luastring(name));
+ lua_assert(Z instanceof ZIO, "BytecodeParser only operates on a ZIO");
+ lua_assert(is_luastring(name));
- if (name[0] == defs.char["@"] || name[0] == defs.char["="])
+ if (name[0] === 64 /* ('@').charCodeAt(0) */ || name[0] === 61 /* ('=').charCodeAt(0) */)
this.name = name.subarray(1);
- else if (name[0] == defs.LUA_SIGNATURE.charCodeAt(0))
- this.name = defs.to_luastring("binary string", true);
+ else if (name[0] == LUA_SIGNATURE[0])
+ this.name = to_luastring("binary string", true);
else
this.name = name;
@@ -44,19 +74,19 @@ class BytecodeParser {
read(size) {
let u8 = new Uint8Array(size);
- if(lzio.luaZ_read(this.Z, u8, 0, size) !== 0)
+ if(luaZ_read(this.Z, u8, 0, size) !== 0)
this.error("truncated");
return u8;
}
readByte() {
- if (lzio.luaZ_read(this.Z, this.u8, 0, 1) !== 0)
+ if (luaZ_read(this.Z, this.u8, 0, 1) !== 0)
this.error("truncated");
return this.u8[0];
}
readInteger() {
- if (lzio.luaZ_read(this.Z, this.u8, 0, this.integerSize) !== 0)
+ if (luaZ_read(this.Z, this.u8, 0, this.integerSize) !== 0)
this.error("truncated");
return this.dv.getInt32(0, true);
}
@@ -66,13 +96,13 @@ class BytecodeParser {
}
readInt() {
- if (lzio.luaZ_read(this.Z, this.u8, 0, this.intSize) !== 0)
+ if (luaZ_read(this.Z, this.u8, 0, this.intSize) !== 0)
this.error("truncated");
return this.dv.getInt32(0, true);
}
readNumber() {
- if (lzio.luaZ_read(this.Z, this.u8, 0, this.numberSize) !== 0)
+ if (luaZ_read(this.Z, this.u8, 0, this.numberSize) !== 0)
this.error("truncated");
return this.dv.getFloat64(0, true);
}
@@ -87,7 +117,7 @@ class BytecodeParser {
return null;
}
- return lstring.luaS_bless(this.L, this.read(size));
+ return luaS_bless(this.L, this.read(size));
}
/* creates a mask with 'n' 1 bits at position 'p' */
@@ -101,27 +131,26 @@ class BytecodeParser {
}
readInstruction() {
- if (lzio.luaZ_read(this.Z, this.u8, 0, this.instructionSize) !== 0)
+ if (luaZ_read(this.Z, this.u8, 0, this.instructionSize) !== 0)
this.error("truncated");
return this.dv.getUint32(0, true);
}
readCode(f) {
let n = this.readInt();
- let o = lopcodes;
let p = BytecodeParser;
for (let i = 0; i < n; i++) {
let ins = this.readInstruction();
f.code[i] = {
code: ins,
- opcode: (ins >> o.POS_OP) & p.MASK1(o.SIZE_OP, 0),
- A: (ins >> o.POS_A) & p.MASK1(o.SIZE_A, 0),
- B: (ins >> o.POS_B) & p.MASK1(o.SIZE_B, 0),
- C: (ins >> o.POS_C) & p.MASK1(o.SIZE_C, 0),
- Bx: (ins >> o.POS_Bx) & p.MASK1(o.SIZE_Bx, 0),
- Ax: (ins >> o.POS_Ax) & p.MASK1(o.SIZE_Ax, 0),
- sBx: ((ins >> o.POS_Bx) & p.MASK1(o.SIZE_Bx, 0)) - o.MAXARG_sBx
+ opcode: (ins >> POS_OP) & p.MASK1(SIZE_OP, 0),
+ A: (ins >> POS_A) & p.MASK1(SIZE_A, 0),
+ B: (ins >> POS_B) & p.MASK1(SIZE_B, 0),
+ C: (ins >> POS_C) & p.MASK1(SIZE_C, 0),
+ Bx: (ins >> POS_Bx) & p.MASK1(SIZE_Bx, 0),
+ Ax: (ins >> POS_Ax) & p.MASK1(SIZE_Ax, 0),
+ sBx: ((ins >> POS_Bx) & p.MASK1(SIZE_Bx, 0)) - MAXARG_sBx
};
}
}
@@ -145,21 +174,21 @@ class BytecodeParser {
let t = this.readByte();
switch (t) {
- case defs.CT.LUA_TNIL:
- f.k.push(new lobject.TValue(defs.CT.LUA_TNIL, null));
+ case LUA_TNIL:
+ f.k.push(new lobject.TValue(LUA_TNIL, null));
break;
- case defs.CT.LUA_TBOOLEAN:
- f.k.push(new lobject.TValue(defs.CT.LUA_TBOOLEAN, this.readByte() !== 0));
+ case LUA_TBOOLEAN:
+ f.k.push(new lobject.TValue(LUA_TBOOLEAN, this.readByte() !== 0));
break;
- case defs.CT.LUA_TNUMFLT:
- f.k.push(new lobject.TValue(defs.CT.LUA_TNUMFLT, this.readNumber()));
+ case LUA_TNUMFLT:
+ f.k.push(new lobject.TValue(LUA_TNUMFLT, this.readNumber()));
break;
- case defs.CT.LUA_TNUMINT:
- f.k.push(new lobject.TValue(defs.CT.LUA_TNUMINT, this.readInteger()));
+ case LUA_TNUMINT:
+ f.k.push(new lobject.TValue(LUA_TNUMINT, this.readInteger()));
break;
- case defs.CT.LUA_TSHRSTR:
- case defs.CT.LUA_TLNGSTR:
- f.k.push(new lobject.TValue(defs.CT.LUA_TLNGSTR, this.readString()));
+ case LUA_TSHRSTR:
+ case LUA_TLNGSTR:
+ f.k.push(new lobject.TValue(LUA_TLNGSTR, this.readString()));
break;
default:
this.error(`unrecognized constant '${t}'`);
@@ -214,12 +243,12 @@ class BytecodeParser {
checkliteral(s, msg) {
let buff = this.read(s.length);
- if (!defs.luastring_cmp(buff, s))
+ if (!luastring_eq(buff, s))
this.error(msg);
}
checkHeader() {
- this.checkliteral(defs.to_luastring(defs.LUA_SIGNATURE.substring(1)), "not a"); /* 1st char already checked */
+ this.checkliteral(LUA_SIGNATURE.subarray(1), "not a"); /* 1st char already checked */
if (this.readByte() !== 0x53)
this.error("version mismatch in");
@@ -250,8 +279,8 @@ class BytecodeParser {
}
error(why) {
- lobject.luaO_pushfstring(this.L, defs.to_luastring("%s: %s precompiled chunk"), this.name, defs.to_luastring(why));
- ldo.luaD_throw(this.L, defs.thread_status.LUA_ERRSYNTAX);
+ lobject.luaO_pushfstring(this.L, to_luastring("%s: %s precompiled chunk"), this.name, to_luastring(why));
+ ldo.luaD_throw(this.L, LUA_ERRSYNTAX);
}
checksize(byte, size, tname) {
@@ -268,7 +297,7 @@ const luaU_undump = function(L, Z, name) {
L.stack[L.top-1].setclLvalue(cl);
cl.p = new lfunc.Proto(L);
S.readFunction(cl.p, null);
- assert(cl.nupvalues === cl.p.upvalues.length);
+ lua_assert(cl.nupvalues === cl.p.upvalues.length);
/* luai_verifycode */
return cl;
};