aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-05-23 14:54:41 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-23 15:06:36 +1000
commitbeb5d0abd707c112953719a8ac077dff88a5ecf7 (patch)
treea8be2dfdf82a0b5a02da52cb08175c2b1f3698b9 /src
parentfe6129f7edaa3a9dee990cd90f765e7196d992b6 (diff)
downloadfengari-beb5d0abd707c112953719a8ac077dff88a5ecf7.tar.gz
fengari-beb5d0abd707c112953719a8ac077dff88a5ecf7.tar.bz2
fengari-beb5d0abd707c112953719a8ac077dff88a5ecf7.zip
src/ltable.js: Convert float keys that fit in an integer to integers
Diffstat (limited to 'src')
-rw-r--r--src/ltable.js11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/ltable.js b/src/ltable.js
index 88b09e3..b408cd1 100644
--- a/src/ltable.js
+++ b/src/ltable.js
@@ -29,8 +29,8 @@ const table_hash = function(L, key) {
if (isNaN(key.value))
return ldebug.luaG_runerror(L, defs.to_luastring("table index is NaN", true));
/* fall through */
+ case CT.LUA_TNUMINT: /* takes advantage of floats and integers being same in JS */
case CT.LUA_TBOOLEAN:
- case CT.LUA_TNUMINT:
case CT.LUA_TTABLE:
case CT.LUA_TLCL:
case CT.LUA_TLCF:
@@ -167,6 +167,13 @@ const setgeneric = function(t, hash, key) {
if (v)
return v.value;
+ let kv = key.value;
+ if ((key.ttisfloat() && (kv|0) === kv)) { /* does index fit in an integer? */
+ /* insert it as an integer */
+ key = new lobject.TValue(CT.LUA_TNUMINT, kv);
+ } else {
+ key = new lobject.TValue(key.type, kv);
+ }
let tv = new lobject.TValue(CT.LUA_TNIL, null);
add(t, hash, key, tv);
return tv;
@@ -193,7 +200,7 @@ const luaH_setint = function(t, key, value) {
const luaH_set = function(L, t, key) {
assert(key instanceof lobject.TValue);
let hash = table_hash(L, key);
- return setgeneric(t, hash, new lobject.TValue(key.type, key.value));
+ return setgeneric(t, hash, key);
};
const luaH_delete = function(L, t, key) {