diff options
author | daurnimator <quae@daurnimator.com> | 2017-04-19 18:12:07 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-04-19 18:12:07 +1000 |
commit | 662381f6669f28f90a1d50c89647d265ca3804a4 (patch) | |
tree | 5ae3bce5dcf2a83302618280163d3088e5d090f8 /src/ltable.js | |
parent | 4d596650dff0417660874727964a32dae23dc9ea (diff) | |
parent | 6be8db07196c407cd321a7b04f5022939c4ffce3 (diff) | |
download | fengari-662381f6669f28f90a1d50c89647d265ca3804a4.tar.gz fengari-662381f6669f28f90a1d50c89647d265ca3804a4.tar.bz2 fengari-662381f6669f28f90a1d50c89647d265ca3804a4.zip |
Merge branch 'master' into cli
Diffstat (limited to 'src/ltable.js')
-rw-r--r-- | src/ltable.js | 16 |
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]; } } |