From beb5d0abd707c112953719a8ac077dff88a5ecf7 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 23 May 2017 14:54:41 +1000 Subject: src/ltable.js: Convert float keys that fit in an integer to integers --- src/ltable.js | 11 +++++++++-- 1 file 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) { -- cgit v1.2.3-70-g09d2