summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-03-24 16:40:29 +0100
committerBenoit Giannangeli <giann008@gmail.com>2017-03-24 16:40:29 +0100
commit7af8347dbb7a8c3a09d2aebccd5f96384a7c55c3 (patch)
tree174de728b42de05b68f8ad10d1b0efddf6c188e0
parent12944ea4d52a330b8b015af296dbbdee3fb4abbd (diff)
downloadfengari-7af8347dbb7a8c3a09d2aebccd5f96384a7c55c3.tar.gz
fengari-7af8347dbb7a8c3a09d2aebccd5f96384a7c55c3.tar.bz2
fengari-7af8347dbb7a8c3a09d2aebccd5f96384a7c55c3.zip
Proper use of luaO_nilobject
-rw-r--r--src/lapi.js24
-rw-r--r--src/lbaselib.js15
-rw-r--r--src/ldebug.js2
-rw-r--r--src/ldo.js15
-rw-r--r--src/lobject.js9
-rw-r--r--src/lstate.js6
-rw-r--r--src/ltm.js4
-rw-r--r--src/lvm.js6
8 files changed, 34 insertions, 47 deletions
diff --git a/src/lapi.js b/src/lapi.js
index d1ca02f..8cabf12 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -20,6 +20,10 @@ const TS = lua.thread_status;
const TValue = lobject.TValue;
const CClosure = lobject.CClosure;
+const isvalid = function(o) {
+ return o !== lobject.luaO_nilobject;
+};
+
const lua_version = function(L) {
if (L === null) return lua.LUA_VERSION_NUM;
else return L.l_G.version;
@@ -37,7 +41,7 @@ const index2addr = function(L, idx) {
if (idx > 0) {
let o = ci.funcOff + idx;
assert(idx <= ci.top - (ci.funcOff + 1), "unacceptable index");
- if (o >= L.top) return ldo.nil;
+ if (o >= L.top) return lobject.luaO_nilobject;
else return L.stack[o];
} else if (idx > lua.LUA_REGISTRYINDEX) {
assert(idx !== 0 && -idx <= L.top, "invalid index");
@@ -48,9 +52,9 @@ const index2addr = function(L, idx) {
idx = lua.LUA_REGISTRYINDEX - idx;
assert(idx <= MAXUPVAL + 1, "upvalue index too large");
if (ci.func.ttislcf()) /* light C function? */
- return ldo.nil; /* it has no upvalues */
+ return lobject.luaO_nilobject; /* it has no upvalues */
else {
- return idx <= ci.func.nupvalues ? ci.func.upvalue[idx - 1] : ldo.nil;
+ return idx <= ci.func.nupvalues ? ci.func.upvalue[idx - 1] : lobject.luaO_nilobject;
}
}
};
@@ -129,7 +133,7 @@ const lua_settop = function(L, idx) {
let func = L.ci.funcOff;
if (idx >= 0) {
while (L.top < func + 1 + idx)
- L.stack[L.top++] = ldo.nil;
+ L.stack[L.top++] = new TValue(CT.LUA_TNIL, null);
L.top = func + 1 + idx;
} else {
assert(-(idx + 1) <= L.top - (func + 1), "invalid new top");
@@ -158,7 +162,7 @@ const lua_rotate = function(L, idx, n) {
let p = index2addr(L, idx);
let pIdx = index2addr_(L, idx);
- assert(/*!p.ttisnil() && */idx > lua.LUA_REGISTRYINDEX, "index not in the stack");
+ assert(p !== lobject.luaO_nilobject && idx > lua.LUA_REGISTRYINDEX, "index not in the stack");
assert((n >= 0 ? n : -n) <= (L.top - idx), "invalid 'n'");
let m = n >= 0 ? L.top - 1 - n : pIdx - n - 1; /* end of prefix */
@@ -193,7 +197,7 @@ const lua_replace = function(L, idx) {
*/
const lua_pushnil = function(L) {
- L.stack[L.top++] = ldo.nil;
+ L.stack[L.top++] = new TValue(CT.LUA_TNIL, null);
assert(L.top <= L.ci.top, "stack overflow");
};
@@ -228,7 +232,7 @@ const lua_pushlstring = function(L, s, len) { // TODO: embedded \0
const lua_pushstring = function (L, s) {
if (typeof s !== "string")
- L.stack[L.top] = ldo.nil;
+ L.stack[L.top] = new TValue(CT.LUA_TNIL, null);
else {
let ts = L.l_G.intern(lua.to_luastring(s));
L.stack[L.top] = ts;
@@ -631,7 +635,7 @@ const lua_compare = function(L, index1, index2, op) {
const lua_compare_ = function(L, o1, o2, op) {
let i = 0;
- if (!o1.ttisnil() && !o2.ttisnil()) {
+ if (isvalid(o1) && isvalid(o2)) {
switch (op) {
case lua.LUA_OPEQ: i = lvm.luaV_equalobj(L, o1, o2); break;
case lua.LUA_OPLT: i = lvm.luaV_lessthan(L, o1, o2); break;
@@ -661,7 +665,7 @@ const f_call = function(L, ud) {
const lua_type = function(L, idx) {
let o = index2addr(L, idx);
- return o.ttnov(); // TODO: isvalid ? luaO_nilobject !== nil tvalue ?
+ return isvalid(o) ? o.ttnov() : CT.LUA_TNONE;
};
const lua_typename = function(L, t) {
@@ -706,7 +710,7 @@ const lua_isuserdata = function(L, idx) {
const lua_rawequal = function(L, index1, index2) {
let o1 = index2addr(L, index1);
let o2 = index2addr(L, index2);
- return lvm.luaV_equalobj(null, o1, o2); // TODO: isvalid ?
+ return isvalid(o1) && isvalid(o2) ? lvm.luaV_equalobj(null, o1, o2) : 0; // TODO: isvalid ?
};
/*
diff --git a/src/lbaselib.js b/src/lbaselib.js
index 762838c..11906db 100644
--- a/src/lbaselib.js
+++ b/src/lbaselib.js
@@ -341,21 +341,6 @@ if (typeof require === "function") {
} catch (e) {}
if (fs) {
- const load_aux = function(L, status, envidx) {
- if (status === TS.LUA_OK) {
- if (envidx !== 0) { /* 'env' parameter? */
- lapi.lua_pushvalue(L, envidx); /* environment for loaded function */
- if (!lapi.lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
- lapi.lua_pop(L, 1); /* remove 'env' if not used by previous call */
- }
- return 1;
- } else { /* error (message is on top of the stack) */
- lapi.lua_pushnil(L);
- lapi.lua_insert(L, -2); /* put before error message */
- return 2; /* return nil plus error message */
- }
- };
-
const luaB_loadfile = function(L) {
let fname = lauxlib.luaL_optstring(L, 1, null);
let mode = lauxlib.luaL_optstring(L, 2, null);
diff --git a/src/ldebug.js b/src/ldebug.js
index aa57aef..7c892a5 100644
--- a/src/ldebug.js
+++ b/src/ldebug.js
@@ -78,7 +78,7 @@ const funcinfo = function(ar, cl) {
const collectvalidlines = function(L, f) {
if (f === null || f.c.type === CT.LUA_TCCL) {
- L.stack[L.top++] = ldo.nil;
+ L.stack[L.top++] = new TValue(CT.LUA_TNIL, null);
assert(L.top <= L.ci.top, "stack overflow");
} else {
let lineinfo = f.l.p.lineinfo;
diff --git a/src/ldo.js b/src/ldo.js
index a9f442a..50d455e 100644
--- a/src/ldo.js
+++ b/src/ldo.js
@@ -20,8 +20,6 @@ const TS = lua.thread_status;
const LUA_MULTRET = lua.LUA_MULTRET;
const TValue = lobject.TValue;
-const nil = new TValue(CT.LUA_TNIL, null);
-
const seterrorobj = function(L, errcode, oldtop) {
switch (errcode) {
case TS.LUA_ERRMEM: {
@@ -94,7 +92,7 @@ const luaD_precall = function(L, off, nresults) {
base = adjust_varargs(L, p, n);
} else {
for (; n < p.numparams; n++)
- L.stack[L.top++] = nil; // complete missing arguments
+ L.stack[L.top++] = new TValue(CT.LUA_TNIL, null); // complete missing arguments
base = off + 1;
}
@@ -143,7 +141,7 @@ const moveresults = function(L, firstResult, res, nres, wanted) {
break;
case 1: {
if (nres === 0)
- L.stack[firstResult] = nil;
+ L.stack[firstResult] = lobject.luaO_nilobject;
L.stack[res] = L.stack[firstResult];
break;
}
@@ -163,7 +161,7 @@ const moveresults = function(L, firstResult, res, nres, wanted) {
for (i = 0; i < nres; i++)
L.stack[res + i] = L.stack[firstResult + i];
for (; i < wanted; i++)
- L.stack[res + i] = nil;
+ L.stack[res + i] = new TValue(CT.LUA_TNIL, null);
}
break;
}
@@ -182,11 +180,11 @@ const adjust_varargs = function(L, p, actual) {
let i;
for (i = 0; i < nfixargs && i < actual; i++) {
L.stack[L.top++] = L.stack[fixed + i];
- L.stack[fixed + i] = nil;
+ L.stack[fixed + i] = new TValue(CT.LUA_TNIL, null);
}
for (; i < nfixargs; i++)
- L.stack[L.top++] = nil;
+ L.stack[L.top++] = new TValue(CT.LUA_TNIL, null);
return base;
};
@@ -579,6 +577,5 @@ module.exports.lua_resume = lua_resume;
module.exports.lua_yield = lua_yield;
module.exports.lua_yieldk = lua_yieldk;
module.exports.moveresults = moveresults;
-module.exports.nil = nil;
module.exports.stackerror = stackerror;
-module.exports.tryfuncTM = tryfuncTM; \ No newline at end of file
+module.exports.tryfuncTM = tryfuncTM;
diff --git a/src/lobject.js b/src/lobject.js
index 32a7573..58e2d21 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -127,6 +127,9 @@ class TValue {
}
+const luaO_nilobject = new TValue(CT.LUA_TNIL, null);
+module.exports.luaO_nilobject = luaO_nilobject;
+
const jsstring = function(value, from, to) {
let u0, u1, u2, u3, u4, u5;
let idx = 0;
@@ -169,8 +172,6 @@ const jsstring = function(value, from, to) {
return str;
};
-const nil = new TValue(CT.LUA_TNIL, null);
-
class Table extends TValue {
constructor(array, hash) {
@@ -210,14 +211,14 @@ class Table extends TValue {
__index(table, key) {
key = Table.keyValue(key);
- let v = nil;
+ let v = luaO_nilobject;
if (typeof key === 'number' && key > 0) {
v = table.value.get(key - 1); // Lua array starts at 1
} else {
v = table.value.get(key);
}
- return v ? v : nil;
+ return v ? v : luaO_nilobject;
}
__len(table) {
diff --git a/src/lstate.js b/src/lstate.js
index 80dcc21..74c908d 100644
--- a/src/lstate.js
+++ b/src/lstate.js
@@ -68,7 +68,7 @@ class global_State {
constructor(L) {
this.mainthread = L;
this.strt = new Map();
- this.l_registry = ldo.nil;
+ this.l_registry = new lobject.TValue(CT.LUA_TNIL, null);
this.panic = null;
this.version = null;
this.twups = [];
@@ -98,7 +98,7 @@ const stack_init = function(L1, L) {
ci.callstatus = 0;
ci.func = L1.stack[L1.top];
ci.funcOff = L1.top;
- L1.stack[L1.top++] = ldo.nil;
+ L1.stack[L1.top++] = new lobject.TValue(CT.LUA_TNIL, null);
ci.top = L1.top + lua.LUA_MINSTACK;
L1.ci = ci;
};
@@ -183,4 +183,4 @@ module.exports.CIST_HOOKYIELD = (1<<6); /* last hook called yielded */
module.exports.CIST_LEQ = (1<<7); /* using __lt for __le */
module.exports.CIST_FIN = (1<<8); /* call is running a finalizer */
module.exports.lua_newstate = lua_newstate;
-module.exports.lua_newthread = lua_newthread; \ No newline at end of file
+module.exports.lua_newthread = lua_newthread;
diff --git a/src/ltm.js b/src/ltm.js
index e48ff23..7ef772f 100644
--- a/src/ltm.js
+++ b/src/ltm.js
@@ -179,7 +179,7 @@ const luaT_gettmbyobj = function(L, o, event) {
mt = L.l_G.mt[o.ttnov()];
}
- return mt ? mt.__index(mt, event) : ldo.nil;
+ return mt ? mt.__index(mt, event) : lobject.luaO_nilobject;
};
module.exports.TMS = TMS;
@@ -190,4 +190,4 @@ module.exports.luaT_callorderTM = luaT_callorderTM;
module.exports.luaT_gettmbyobj = luaT_gettmbyobj;
module.exports.luaT_init = luaT_init;
module.exports.luaT_objtypename = luaT_objtypename;
-module.exports.ttypename = ttypename; \ No newline at end of file
+module.exports.ttypename = ttypename;
diff --git a/src/lvm.js b/src/lvm.js
index c5628cf..53e1584 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -154,7 +154,7 @@ const luaV_execute = function(L) {
}
case "OP_LOADNIL": {
for (let j = 0; j <= i.B; j++)
- L.stack[ra + j] = ldo.nil;
+ L.stack[ra + j] = new TValue(CT.LUA_TNIL, null);
break;
}
case "OP_GETUPVAL": {
@@ -694,7 +694,7 @@ const luaV_execute = function(L) {
L.stack[ra + j] = L.stack[base - n + j];
for (; j < b; j++) /* complete required results with nil */
- L.stack[ra + j] = ldo.nil;
+ L.stack[ra + j] = new TValue(CT.LUA_TNIL, null);
break;
}
case "OP_EXTRAARG": {
@@ -1068,7 +1068,7 @@ const luaV_finishget = function(L, t, key, val, slot, recur) {
assert(slot.ttisnil());
tm = ltm.luaT_gettmbyobj(L, t, ltm.TMS.TM_INDEX); // TODO: fasttm
if (tm.ttisnil()) {
- L.stack[val] = ldo.nil;
+ L.stack[val] = new TValue(CT.LUA_TNIL, null);
return;
}
}