diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-04-13 07:17:05 +0200 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-04-13 08:57:25 +0200 |
commit | d251efeb37778e2ab57a3d2c80b9df621a691796 (patch) | |
tree | fe6db792a0e9a018d10983a142724fda6e17071b | |
parent | a6dc1dbf5a439a854581927fb36fdad05d0dff4b (diff) | |
download | fengari-d251efeb37778e2ab57a3d2c80b9df621a691796.tar.gz fengari-d251efeb37778e2ab57a3d2c80b9df621a691796.tar.bz2 fengari-d251efeb37778e2ab57a3d2c80b9df621a691796.zip |
Table indexes are also starting at 1 internally
Since we use a Map we don't have to care about indexes starting at 0
-rw-r--r-- | src/lapi.js | 6 | ||||
-rw-r--r-- | src/lobject.js | 13 | ||||
-rw-r--r-- | src/lstate.js | 4 | ||||
-rw-r--r-- | src/ltable.js | 16 | ||||
-rw-r--r-- | src/ltablib.js | 4 | ||||
-rw-r--r-- | tests/lbaselib.js | 1 | ||||
-rw-r--r-- | tests/lexparse.js | 4 | ||||
-rw-r--r-- | tests/ltablib.js | 2 | ||||
-rw-r--r-- | tests/lvm.js | 4 |
9 files changed, 23 insertions, 31 deletions
diff --git a/src/lapi.js b/src/lapi.js index 894b5bb..048a6b7 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -346,7 +346,7 @@ const auxsetstr = function(L, t, k) { }; const lua_setglobal = function(L, name) { - auxsetstr(L, L.l_G.l_registry.value.get(lua.LUA_RIDX_GLOBALS - 1), name); + auxsetstr(L, L.l_G.l_registry.value.get(lua.LUA_RIDX_GLOBALS), name); }; const lua_setmetatable = function(L, objindex) { @@ -562,7 +562,7 @@ const lua_geti = function(L, idx, n) { }; const lua_getglobal = function(L, name) { - return auxgetstr(L, L.l_G.l_registry.value.get(lua.LUA_RIDX_GLOBALS - 1), name); + return auxgetstr(L, L.l_G.l_registry.value.get(lua.LUA_RIDX_GLOBALS), name); }; /* @@ -773,7 +773,7 @@ const lua_load = function(L, reader, data, chunckname, mode) { if (f.nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ let reg = L.l_G.l_registry; - let gt = reg.value.get(lua.LUA_RIDX_GLOBALS - 1); + let gt = reg.value.get(lua.LUA_RIDX_GLOBALS); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ f.upvals[0].u.value = gt; } diff --git a/src/lobject.js b/src/lobject.js index 618e801..cf3a52e 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -207,22 +207,13 @@ class Table extends TValue { __newindex(table, key, value) { key = Table.keyValue(key); - if (typeof key === 'number' && key > 0) { - table.value.set(key - 1, value); // Lua array starts at 1 - } else { - table.value.set(key, value); - } + table.value.set(key, value); } __index(table, key) { key = Table.keyValue(key); - 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); - } + let v = table.value.get(key); return v ? v : luaO_nilobject; } diff --git a/src/lstate.js b/src/lstate.js index 74c908d..e18e7aa 100644 --- a/src/lstate.js +++ b/src/lstate.js @@ -109,8 +109,8 @@ const stack_init = function(L1, L) { const init_registry = function(L, g) { let registry = new lobject.Table(); g.l_registry = registry; - registry.value.set(lua.LUA_RIDX_MAINTHREAD - 1, L); - registry.value.set(lua.LUA_RIDX_GLOBALS - 1, new lobject.Table()); + registry.value.set(lua.LUA_RIDX_MAINTHREAD, L); + registry.value.set(lua.LUA_RIDX_GLOBALS, new lobject.Table()); }; /* diff --git a/src/ltable.js b/src/ltable.js index 039dd6d..6ba8236 100644 --- a/src/ltable.js +++ b/src/ltable.js @@ -13,7 +13,7 @@ const TValue = lobject.TValue; Table.prototype.ordered_intindexes = function() { return [...this.value.keys()] - .filter(e => typeof e === 'number' && e % 1 === 0) // Only integer indexes + .filter(e => typeof e === 'number' && e % 1 === 0 && e > 0) // Only integer indexes .sort(function (a, b) { return a > b ? 1 : -1; }); @@ -22,8 +22,8 @@ Table.prototype.ordered_intindexes = function() { Table.prototype.ordered_indexes = function() { return [...this.value.keys()] .sort(function(a, b) { - if (typeof a !== "number") return 1; - if (typeof b !== "number") return -1; + if (typeof a !== "number" || a <= 0) return 1; + if (typeof b !== "number" || b <= 0) return -1; return a > b ? 1 : -1; }); }; @@ -33,16 +33,18 @@ Table.prototype.ordered_indexes = function() { ** 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() { - // TODO: is this costly ? let indexes = this.ordered_intindexes(); let len = indexes.length; + // If first index != 1, length is 0 + if (indexes[0] !== 1) return 0; + for (let i = 0; i < len; i++) { let key = indexes[i]; - if (!this.__index(this, key).ttisnil() // t[i] is non-nil - && (i === len - 1 || this.__index(this, indexes[i + 1]).ttisnil())) { // t[i+1] is nil or is the last integer indexed element - return indexes[i] + 1; + 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 + return indexes[i]; } } diff --git a/src/ltablib.js b/src/ltablib.js index eaaf24d..210a119 100644 --- a/src/ltablib.js +++ b/src/ltablib.js @@ -192,7 +192,7 @@ const auxsort = function(L) { else if (typeof b[0] !== 'number') return -1; return lapi.lua_compare_(L, a[1], b[1], lua.LUA_OPLT) === 1 ? -1 : 1; /* a < b */ }) - .forEach((e, i) => typeof e[0] === 'number' ? t.value.set(i, e[1]) : true); + .forEach((e, i) => typeof e[0] === 'number' ? t.value.set(i + 1, e[1]) : true); } else { [...t.value.entries()] .sort(function (a, b) { @@ -207,7 +207,7 @@ const auxsort = function(L) { lapi.lua_pop(L, 1); /* pop result */ return res ? -1 : 1; }) - .forEach((e, i) => typeof e[0] === 'number' ? t.value.set(i, e[1]) : true); + .forEach((e, i) => typeof e[0] === 'number' ? t.value.set(i + 1, e[1]) : true); } }; diff --git a/tests/lbaselib.js b/tests/lbaselib.js index dd91449..5f39991 100644 --- a/tests/lbaselib.js +++ b/tests/lbaselib.js @@ -15,7 +15,6 @@ const lua = require('../src/lua.js'); const linit = require('../src/linit.js'); const CT = lua.constant_types; - test('print', function (t) { let luaCode = ` print("hello", "world", 123) diff --git a/tests/lexparse.js b/tests/lexparse.js index 92901b1..96c8fca 100644 --- a/tests/lexparse.js +++ b/tests/lexparse.js @@ -742,7 +742,7 @@ test('SETTABLE, GETTABLE', function (t) { }, "Lua program ran without error"); t.strictEqual( - lapi.lua_topointer(L, -1).get(0).jsstring(), + lapi.lua_topointer(L, -1).get(1).jsstring(), "hello", "Program output is correct" ); @@ -835,7 +835,7 @@ test('SETTABUP, GETTABUP', function (t) { }, "Lua program ran without error"); t.strictEqual( - lapi.lua_topointer(L, -1).get(0).jsstring(), + lapi.lua_topointer(L, -1).get(1).jsstring(), "hello", "Program output is correct" ); diff --git a/tests/ltablib.js b/tests/ltablib.js index 81ffcbd..60f9abc 100644 --- a/tests/ltablib.js +++ b/tests/ltablib.js @@ -21,7 +21,7 @@ const inttable2array = function(t) { t.forEach(function (v, k) { if (typeof k === 'number') - a[k] = v; + a[k - 1] = v; }); return a.map(e => e.value); diff --git a/tests/lvm.js b/tests/lvm.js index bda4c4f..0f91d81 100644 --- a/tests/lvm.js +++ b/tests/lvm.js @@ -496,7 +496,7 @@ test('SETTABLE, GETTABLE', function (t) { console.log(L.stack[L.top - 1]); t.deepEqual( - L.stack[L.top - 1].value.get(0).jsstring(), + L.stack[L.top - 1].value.get(1).jsstring(), "hello", "Program output is correct" ); @@ -559,7 +559,7 @@ test('SETTABUP, GETTABUP', function (t) { }, "Program executed without errors"); t.deepEqual( - L.stack[L.top - 1].value.get(0).jsstring(), + L.stack[L.top - 1].value.get(1).jsstring(), "hello", // "hello" "Program output is correct" ); |