From c31a8135a7b094732f2129b247c982fc58ee52f6 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 18 Apr 2017 12:08:21 +0200 Subject: luaH_next and luaH_getn are not Table member functions --- src/lapi.js | 11 ++++++----- src/lbaselib.js | 2 +- src/lobject.js | 4 ---- src/ltable.js | 25 ++++++++++++++----------- src/lvm.js | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/lapi.js b/src/lapi.js index 72c35f0..c4e1029 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -14,6 +14,7 @@ const lua = require('./lua.js'); const luaconf = require('./luaconf.js'); const lundump = require('./lundump.js'); const lvm = require('./lvm.js'); +const ltable = require('./ltable.js'); const MAXUPVAL = lfunc.MAXUPVAL; const CT = lua.constant_types; const TS = lua.thread_status; @@ -509,8 +510,8 @@ const lua_getupvalue = function(L, funcindex, n) { let name = up.name; let val = up.val; if (name) - L.stack[L.top++] = new TValue(name.type, name.value); - return name; + L.stack[L.top++] = new TValue(val.type, val.value); + return name.value; }; const lua_setupvalue = function(L, funcindex, n) { @@ -525,7 +526,7 @@ const lua_setupvalue = function(L, funcindex, n) { val.type = L.stack[L.top].type; val.value = L.stack[L.top].value; } - return name; + return name.value; }; const lua_newtable = function(L) { @@ -645,7 +646,7 @@ const lua_rawlen = function(L, idx) { case CT.LUA_TUSERDATA: return o.len; case CT.LUA_TTABLE: - return o.luaH_getn(); + return ltable.luaH_getn(o); default: return 0; } @@ -923,7 +924,7 @@ const lua_error = function(L) { const lua_next = function(L, idx) { let t = index2addr(L, idx); assert(t.ttistable(), "table expected"); - let more = t.luaH_next(L, L.top - 1); + let more = ltable.luaH_next(L, t, L.top - 1); if (more) { L.top++; assert(L.top <= L.ci.top, "stack overflow"); diff --git a/src/lbaselib.js b/src/lbaselib.js index af7c3cc..57173b6 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -314,7 +314,7 @@ const luaB_load = function(L) { }; const base_funcs = { - "collectgarbage": function () {}, + "collectgarbage": function () { return 0; }, "assert": luaB_assert, "error": luaB_error, "getmetatable": luaB_getmetatable, diff --git a/src/lobject.js b/src/lobject.js index 52ca385..adec0ba 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -218,10 +218,6 @@ class Table extends TValue { return v ? v : luaO_nilobject; } - __len(table) { - return this.luaH_getn(); - } - } class LClosure extends TValue { diff --git a/src/ltable.js b/src/ltable.js index 6ba8236..d3009a5 100644 --- a/src/ltable.js +++ b/src/ltable.js @@ -11,16 +11,16 @@ const Table = lobject.Table; const TValue = lobject.TValue; -Table.prototype.ordered_intindexes = function() { - return [...this.value.keys()] +const ordered_intindexes = function(table) { + return [...table.value.keys()] .filter(e => typeof e === 'number' && e % 1 === 0 && e > 0) // Only integer indexes .sort(function (a, b) { return a > b ? 1 : -1; }); }; -Table.prototype.ordered_indexes = function() { - return [...this.value.keys()] +const ordered_indexes = function(table) { + return [...table.value.keys()] .sort(function(a, b) { if (typeof a !== "number" || a <= 0) return 1; if (typeof b !== "number" || b <= 0) return -1; @@ -32,8 +32,8 @@ Table.prototype.ordered_indexes = function() { ** Try to find a boundary in table 't'. A 'boundary' is an integer index ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). */ -Table.prototype.luaH_getn = function() { - let indexes = this.ordered_intindexes(); +const luaH_getn = function(table) { + let indexes = ordered_intindexes(table); let len = indexes.length; // If first index != 1, length is 0 @@ -42,8 +42,8 @@ Table.prototype.luaH_getn = function() { for (let i = 0; i < len; i++) { let key = indexes[i]; - if (!this.__index(this, key).ttisnil() // t[key] is non-nil - && (indexes[i + 1] - key > 1 || this.__index(this, indexes[i + 1]).ttisnil())) { // gap with next key or next value is nil + if (!table.__index(table, key).ttisnil() // t[key] is non-nil + && (indexes[i + 1] - key > 1 || table.__index(table, indexes[i + 1]).ttisnil())) { // gap with next key or next value is nil return indexes[i]; } } @@ -51,10 +51,10 @@ Table.prototype.luaH_getn = function() { return 0; }; -Table.prototype.luaH_next = function(L, keyI) { +const luaH_next = function(L, table, keyI) { let keyO = L.stack[keyI]; let key = Table.keyValue(keyO); - let indexes = this.ordered_indexes(); + let indexes = ordered_indexes(table); if (indexes.length === 0) return 0; @@ -72,9 +72,12 @@ Table.prototype.luaH_next = function(L, keyI) { else L.stack[keyI] = indexes[i + 1]; - L.stack[keyI + 1] = this.value.get(indexes[i + 1]); + L.stack[keyI + 1] = table.value.get(indexes[i + 1]); return 1; } return 0; }; + +module.exports.luaH_next = luaH_next; +module.exports.luaH_getn = luaH_getn; diff --git a/src/lvm.js b/src/lvm.js index 1d131cf..55daa91 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -978,7 +978,7 @@ const luaV_objlen = function(L, ra, rb) { case CT.LUA_TTABLE: { tm = ltm.luaT_gettmbyobj(L, rb, ltm.TMS.TM_LEN); if (!tm.ttisnil()) break; - L.stack[ra] = new TValue(CT.LUA_TNUMINT, rb.luaH_getn()); + L.stack[ra] = new TValue(CT.LUA_TNUMINT, ltable.luaH_getn(rb)); return; } case CT.LUA_TSHRSTR: -- cgit v1.2.3-70-g09d2