summaryrefslogtreecommitdiff
path: root/src/ltable.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-04-13 07:17:05 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-04-13 08:57:25 +0200
commitd251efeb37778e2ab57a3d2c80b9df621a691796 (patch)
treefe6db792a0e9a018d10983a142724fda6e17071b /src/ltable.js
parenta6dc1dbf5a439a854581927fb36fdad05d0dff4b (diff)
downloadfengari-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
Diffstat (limited to 'src/ltable.js')
-rw-r--r--src/ltable.js16
1 files changed, 9 insertions, 7 deletions
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];
}
}