summaryrefslogtreecommitdiff
path: root/src/lcode.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/lcode.js')
-rw-r--r--src/lcode.js113
1 files changed, 67 insertions, 46 deletions
diff --git a/src/lcode.js b/src/lcode.js
index e7259f2..4d9689a 100644
--- a/src/lcode.js
+++ b/src/lcode.js
@@ -1,8 +1,30 @@
"use strict";
-const assert = require('assert');
-
-const defs = require('./defs.js');
+const {
+ LUA_MULTRET,
+ LUA_OPADD,
+ LUA_OPBAND,
+ LUA_OPBNOT,
+ LUA_OPBOR,
+ LUA_OPBXOR,
+ LUA_OPDIV,
+ LUA_OPIDIV,
+ LUA_OPMOD,
+ LUA_OPSHL,
+ LUA_OPSHR,
+ LUA_OPUNM,
+ constant_types: {
+ LUA_TBOOLEAN,
+ LUA_TLIGHTUSERDATA,
+ LUA_TLNGSTR,
+ LUA_TNIL,
+ LUA_TNUMFLT,
+ LUA_TNUMINT,
+ LUA_TTABLE
+ },
+ to_luastring
+} = require('./defs.js');
+const { lua_assert } = require("./llimits.js");
const llex = require('./llex.js');
const lobject = require('./lobject.js');
const lopcodes = require('./lopcodes.js');
@@ -10,7 +32,6 @@ const lparser = require('./lparser.js');
const ltable = require('./ltable.js');
const lvm = require('./lvm.js');
-const CT = defs.CT;
const OpCodesI = lopcodes.OpCodesI;
const TValue = lobject.TValue;
@@ -71,12 +92,12 @@ const tonumeral = function(e, make_tvalue) {
switch (e.k) {
case ek.VKINT:
if (make_tvalue) {
- return new TValue(CT.LUA_TNUMINT, e.u.ival);
+ return new TValue(LUA_TNUMINT, e.u.ival);
}
return true;
case ek.VKFLT:
if (make_tvalue) {
- return new TValue(CT.LUA_TNUMFLT, e.u.nval);
+ return new TValue(LUA_TNUMFLT, e.u.nval);
}
return true;
default: return false;
@@ -133,9 +154,9 @@ const getjump = function(fs, pc) {
const fixjump = function(fs, pc, dest) {
let jmp = fs.f.code[pc];
let offset = dest - (pc + 1);
- assert(dest !== NO_JUMP);
+ lua_assert(dest !== NO_JUMP);
if (Math.abs(offset) > lopcodes.MAXARG_sBx)
- llex.luaX_syntaxerror(fs.ls, defs.to_luastring("control structure too long", true));
+ llex.luaX_syntaxerror(fs.ls, to_luastring("control structure too long", true));
lopcodes.SETARG_sBx(jmp, offset);
};
@@ -291,7 +312,7 @@ const luaK_patchlist = function(fs, list, target) {
if (target === fs.pc) /* 'target' is current position? */
luaK_patchtohere(fs, list); /* add list to pending jumps */
else {
- assert(target < fs.pc);
+ lua_assert(target < fs.pc);
patchlistaux(fs, list, target, lopcodes.NO_REG, target);
}
};
@@ -305,7 +326,7 @@ const luaK_patchclose = function(fs, list, level) {
level++; /* argument is +1 to reserve 0 as non-op */
for (; list !== NO_JUMP; list = getjump(fs, list)) {
let ins = fs.f.code[list];
- assert(ins.opcode === OpCodesI.OP_JMP && (ins.A === 0 || ins.A >= level));
+ lua_assert(ins.opcode === OpCodesI.OP_JMP && (ins.A === 0 || ins.A >= level));
lopcodes.SETARG_A(ins, level);
}
};
@@ -328,10 +349,10 @@ const luaK_code = function(fs, i) {
** of parameters versus opcode.)
*/
const luaK_codeABC = function(fs, o, a, b, c) {
- assert(lopcodes.getOpMode(o) === lopcodes.iABC);
- assert(lopcodes.getBMode(o) !== lopcodes.OpArgN || b === 0);
- assert(lopcodes.getCMode(o) !== lopcodes.OpArgN || c === 0);
- assert(a <= lopcodes.MAXARG_A && b <= lopcodes.MAXARG_B && c <= lopcodes.MAXARG_C);
+ lua_assert(lopcodes.getOpMode(o) === lopcodes.iABC);
+ lua_assert(lopcodes.getBMode(o) !== lopcodes.OpArgN || b === 0);
+ lua_assert(lopcodes.getCMode(o) !== lopcodes.OpArgN || c === 0);
+ lua_assert(a <= lopcodes.MAXARG_A && b <= lopcodes.MAXARG_B && c <= lopcodes.MAXARG_C);
return luaK_code(fs, lopcodes.CREATE_ABC(o, a, b, c));
};
@@ -339,9 +360,9 @@ const luaK_codeABC = function(fs, o, a, b, c) {
** Format and emit an 'iABx' instruction.
*/
const luaK_codeABx = function(fs, o, a, bc) {
- assert(lopcodes.getOpMode(o) === lopcodes.iABx || lopcodes.getOpMode(o) === lopcodes.iAsBx);
- assert(lopcodes.getCMode(o) === lopcodes.OpArgN);
- assert(a <= lopcodes.MAXARG_A && bc <= lopcodes.MAXARG_Bx);
+ lua_assert(lopcodes.getOpMode(o) === lopcodes.iABx || lopcodes.getOpMode(o) === lopcodes.iAsBx);
+ lua_assert(lopcodes.getCMode(o) === lopcodes.OpArgN);
+ lua_assert(a <= lopcodes.MAXARG_A && bc <= lopcodes.MAXARG_Bx);
return luaK_code(fs, lopcodes.CREATE_ABx(o, a, bc));
};
@@ -353,7 +374,7 @@ const luaK_codeAsBx = function(fs,o,A,sBx) {
** Emit an "extra argument" instruction (format 'iAx')
*/
const codeextraarg = function(fs, a) {
- assert(a <= lopcodes.MAXARG_Ax);
+ lua_assert(a <= lopcodes.MAXARG_Ax);
return luaK_code(fs, lopcodes.CREATE_Ax(OpCodesI.OP_EXTRAARG, a));
};
@@ -380,7 +401,7 @@ const luaK_checkstack = function(fs, n) {
let newstack = fs.freereg + n;
if (newstack > fs.f.maxstacksize) {
if (newstack >= MAXREGS)
- llex.luaX_syntaxerror(fs.ls, defs.to_luastring("function or expression needs too many registers", true));
+ llex.luaX_syntaxerror(fs.ls, to_luastring("function or expression needs too many registers", true));
fs.f.maxstacksize = newstack;
}
};
@@ -400,7 +421,7 @@ const luaK_reserveregs = function(fs, n) {
const freereg = function(fs, reg) {
if (!lopcodes.ISK(reg) && reg >= fs.nactvar) {
fs.freereg--;
- assert(reg === fs.freereg);
+ lua_assert(reg === fs.freereg);
}
};
@@ -458,7 +479,7 @@ const addk = function(fs, key, v) {
** Add a string to list of constants and return its index.
*/
const luaK_stringK = function(fs, s) {
- let o = new TValue(CT.LUA_TLNGSTR, s);
+ let o = new TValue(LUA_TLNGSTR, s);
return addk(fs, o, o); /* use string itself as key */
};
@@ -469,8 +490,8 @@ const luaK_stringK = function(fs, s) {
** same value.
*/
const luaK_intK = function(fs, n) {
- let k = new TValue(CT.LUA_TLIGHTUSERDATA, n);
- let o = new TValue(CT.LUA_TNUMINT, n);
+ let k = new TValue(LUA_TLIGHTUSERDATA, n);
+ let o = new TValue(LUA_TNUMINT, n);
return addk(fs, k, o);
};
@@ -478,7 +499,7 @@ const luaK_intK = function(fs, n) {
** Add a float to list of constants and return its index.
*/
const luaK_numberK = function(fs, r) {
- let o = new TValue(CT.LUA_TNUMFLT, r);
+ let o = new TValue(LUA_TNUMFLT, r);
return addk(fs, o, o); /* use number itself as key */
};
@@ -487,7 +508,7 @@ const luaK_numberK = function(fs, r) {
** Add a boolean to list of constants and return its index.
*/
const boolK = function(fs, b) {
- let o = new TValue(CT.LUA_TBOOLEAN, b);
+ let o = new TValue(LUA_TBOOLEAN, b);
return addk(fs, o, o); /* use boolean itself as key */
};
@@ -496,8 +517,8 @@ const boolK = function(fs, b) {
** Add nil to list of constants and return its index.
*/
const nilK = function(fs) {
- let v = new TValue(CT.LUA_TNIL, null);
- let k = new TValue(CT.LUA_TTABLE, fs.ls.h);
+ let v = new TValue(LUA_TNIL, null);
+ let k = new TValue(LUA_TTABLE, fs.ls.h);
/* cannot use nil as key; instead use table itself to represent nil */
return addk(fs, k, v);
};
@@ -518,11 +539,11 @@ const luaK_setreturns = function(fs, e, nresults) {
lopcodes.SETARG_A(pc, fs.freereg);
luaK_reserveregs(fs, 1);
}
- else assert(nresults === defs.LUA_MULTRET);
+ else lua_assert(nresults === LUA_MULTRET);
};
const luaK_setmultret = function(fs, e) {
- luaK_setreturns(fs, e, defs.LUA_MULTRET);
+ luaK_setreturns(fs, e, LUA_MULTRET);
};
/*
@@ -539,7 +560,7 @@ const luaK_setoneret = function(fs, e) {
let ek = lparser.expkind;
if (e.k === ek.VCALL) { /* expression is an open function call? */
/* already returns 1 value */
- assert(getinstruction(fs, e).C === 2);
+ lua_assert(getinstruction(fs, e).C === 2);
e.k = ek.VNONRELOC; /* result has fixed position */
e.u.info = getinstruction(fs, e).A;
} else if (e.k === ek.VVARARG) {
@@ -571,7 +592,7 @@ const luaK_dischargevars = function(fs, e) {
freereg(fs, e.u.ind.t);
op = OpCodesI.OP_GETTABLE;
} else {
- assert(e.u.ind.vt === ek.VUPVAL);
+ lua_assert(e.u.ind.vt === ek.VUPVAL);
op = OpCodesI.OP_GETTABUP; /* 't' is in an upvalue */
}
e.u.info = luaK_codeABC(fs, op, 0, e.u.ind.t, e.u.ind.idx);
@@ -630,7 +651,7 @@ const discharge2reg = function(fs, e, reg) {
break;
}
default: {
- assert(e.k === ek.VJMP);
+ lua_assert(e.k === ek.VJMP);
return; /* nothing to do... */
}
}
@@ -817,7 +838,7 @@ const luaK_self = function(fs, e, key) {
*/
const negatecondition = function(fs, e) {
let pc = getjumpcontrol(fs, e.u.info);
- assert(lopcodes.testTMode(pc.opcode) && pc.opcode !== OpCodesI.OP_TESTSET && pc.opcode !== OpCodesI.OP_TEST);
+ lua_assert(lopcodes.testTMode(pc.opcode) && pc.opcode !== OpCodesI.OP_TESTSET && pc.opcode !== OpCodesI.OP_TEST);
lopcodes.SETARG_A(pc, !(pc.A));
};
@@ -934,7 +955,7 @@ const codenot = function(fs, e) {
*/
const luaK_indexed = function(fs, t, k) {
let ek = lparser.expkind;
- assert(!hasjumps(t) && (lparser.vkisinreg(t.k) || t.k === ek.VUPVAL));
+ lua_assert(!hasjumps(t) && (lparser.vkisinreg(t.k) || t.k === ek.VUPVAL));
t.u.ind.t = t.u.info; /* register or upvalue index */
t.u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */
t.u.ind.vt = (t.k === ek.VUPVAL) ? ek.VUPVAL : ek.VLOCAL;
@@ -948,11 +969,11 @@ const luaK_indexed = function(fs, t, k) {
*/
const validop = function(op, v1, v2) {
switch (op) {
- case defs.LUA_OPBAND: case defs.LUA_OPBOR: case defs.LUA_OPBXOR:
- case defs.LUA_OPSHL: case defs.LUA_OPSHR: case defs.LUA_OPBNOT: { /* conversion errors */
+ case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
+ case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
return (lvm.tointeger(v1) !== false && lvm.tointeger(v2) !== false);
}
- case defs.LUA_OPDIV: case defs.LUA_OPIDIV: case defs.LUA_OPMOD: /* division by 0 */
+ case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
return (v2.value !== 0);
default: return 1; /* everything else is valid */
}
@@ -1026,7 +1047,7 @@ const codecomp = function(fs, opr, e1, e2) {
if (e1.k === ek.VK)
rk1 = lopcodes.RKASK(e1.u.info);
else {
- assert(e1.k === ek.VNONRELOC);
+ lua_assert(e1.k === ek.VNONRELOC);
rk1 = e1.u.info;
}
@@ -1063,7 +1084,7 @@ const luaK_prefix = function(fs, op, e, line) {
ef.f = NO_JUMP;
switch (op) {
case UnOpr.OPR_MINUS: case UnOpr.OPR_BNOT: /* use 'ef' as fake 2nd operand */
- if (constfolding(op + defs.LUA_OPUNM, e, ef))
+ if (constfolding(op + LUA_OPUNM, e, ef))
break;
/* FALLTHROUGH */
case UnOpr.OPR_LEN:
@@ -1118,14 +1139,14 @@ const luaK_posfix = function(fs, op, e1, e2, line) {
let ek = lparser.expkind;
switch (op) {
case BinOpr.OPR_AND: {
- assert(e1.t === NO_JUMP); /* list closed by 'luK_infix' */
+ lua_assert(e1.t === NO_JUMP); /* list closed by 'luK_infix' */
luaK_dischargevars(fs, e2);
e2.f = luaK_concat(fs, e2.f, e1.f);
e1.to(e2);
break;
}
case BinOpr.OPR_OR: {
- assert(e1.f === NO_JUMP); /* list closed by 'luK_infix' */
+ lua_assert(e1.f === NO_JUMP); /* list closed by 'luK_infix' */
luaK_dischargevars(fs, e2);
e2.t = luaK_concat(fs, e2.t, e1.t);
e1.to(e2);
@@ -1135,7 +1156,7 @@ const luaK_posfix = function(fs, op, e1, e2, line) {
luaK_exp2val(fs, e2);
let ins = getinstruction(fs, e2);
if (e2.k === ek.VRELOCABLE && ins.opcode === OpCodesI.OP_CONCAT) {
- assert(e1.u.info === ins.B - 1);
+ lua_assert(e1.u.info === ins.B - 1);
freeexp(fs, e1);
lopcodes.SETARG_B(ins, e1.u.info);
e1.k = ek.VRELOCABLE; e1.u.info = e2.u.info;
@@ -1150,7 +1171,7 @@ const luaK_posfix = function(fs, op, e1, e2, line) {
case BinOpr.OPR_IDIV: case BinOpr.OPR_MOD: case BinOpr.OPR_POW:
case BinOpr.OPR_BAND: case BinOpr.OPR_BOR: case BinOpr.OPR_BXOR:
case BinOpr.OPR_SHL: case BinOpr.OPR_SHR: {
- if (!constfolding(op + defs.LUA_OPADD, e1, e2))
+ if (!constfolding(op + LUA_OPADD, e1, e2))
codebinexpval(fs, op + OpCodesI.OP_ADD, e1, e2, line);
break;
}
@@ -1180,8 +1201,8 @@ const luaK_fixline = function(fs, line) {
*/
const luaK_setlist = function(fs, base, nelems, tostore) {
let c = (nelems - 1)/lopcodes.LFIELDS_PER_FLUSH + 1;
- let b = (tostore === defs.LUA_MULTRET) ? 0 : tostore;
- assert(tostore !== 0 && tostore <= lopcodes.LFIELDS_PER_FLUSH);
+ let b = (tostore === LUA_MULTRET) ? 0 : tostore;
+ lua_assert(tostore !== 0 && tostore <= lopcodes.LFIELDS_PER_FLUSH);
if (c <= lopcodes.MAXARG_C)
luaK_codeABC(fs, OpCodesI.OP_SETLIST, base, b, c);
else if (c <= lopcodes.MAXARG_Ax) {
@@ -1189,7 +1210,7 @@ const luaK_setlist = function(fs, base, nelems, tostore) {
codeextraarg(fs, c);
}
else
- llex.luaX_syntaxerror(fs.ls, defs.to_luastring("constructor too long", true));
+ llex.luaX_syntaxerror(fs.ls, to_luastring("constructor too long", true));
fs.freereg = base + 1; /* free registers with list values */
};