summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lapi.js6
-rw-r--r--src/lobject.js13
-rw-r--r--src/lstate.js4
-rw-r--r--src/ltable.js16
-rw-r--r--src/ltablib.js4
5 files changed, 18 insertions, 25 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);
}
};