summaryrefslogtreecommitdiff
path: root/src/ltable.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-05-22 16:24:46 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-22 16:24:46 +1000
commit13ba814ab51f43718c1f07680012e6ee7815b16b (patch)
tree68e1e3fe1a00d5b0235b7aa0d543cc14506d3367 /src/ltable.js
parentedf9e47120f2098458519628335a7e90b2569b70 (diff)
downloadfengari-13ba814ab51f43718c1f07680012e6ee7815b16b.tar.gz
fengari-13ba814ab51f43718c1f07680012e6ee7815b16b.tar.bz2
fengari-13ba814ab51f43718c1f07680012e6ee7815b16b.zip
Checks for nil and NaN table keys
Diffstat (limited to 'src/ltable.js')
-rw-r--r--src/ltable.js25
1 files changed, 15 insertions, 10 deletions
diff --git a/src/ltable.js b/src/ltable.js
index 0ed56ab..c8bf7a2 100644
--- a/src/ltable.js
+++ b/src/ltable.js
@@ -9,11 +9,16 @@ const lobject = require('./lobject.js');
const lstring = require('./lstring.js');
const CT = defs.constant_types;
-const table_hash = function(key) {
+const table_hash = function(L, key) {
switch(key.type) {
+ case CT.LUA_TNIL:
+ return ldebug.luaG_runerror(L, defs.to_luastring("table index is nil", true));
+ case CT.LUA_TNUMFLT:
+ if (isNaN(key.value))
+ return ldebug.luaG_runerror(L, defs.to_luastring("table index is NaN", true));
+ /* fall through */
case CT.LUA_TBOOLEAN:
case CT.LUA_TLIGHTUSERDATA: /* XXX: if user pushes conflicting lightuserdata then the table will do odd things */
- case CT.LUA_TNUMFLT:
case CT.LUA_TNUMINT:
case CT.LUA_TTABLE:
case CT.LUA_TLCL:
@@ -110,11 +115,11 @@ const luaH_getstr = function(t, key) {
return getgeneric(t, lstring.luaS_hashlongstr(key));
};
-const luaH_get = function(t, key) {
+const luaH_get = function(L, t, key) {
assert(key instanceof lobject.TValue);
- if (key.ttisnil())
+ if (key.ttisnil() || (key.ttisfloat() && isNaN(key.value)))
return lobject.luaO_nilobject;
- return getgeneric(t, table_hash(key));
+ return getgeneric(t, table_hash(L, key));
};
const setgeneric = function(t, hash, key) {
@@ -145,15 +150,15 @@ const luaH_setint = function(t, key, value) {
}
};
-const luaH_set = function(t, key) {
+const luaH_set = function(L, t, key) {
assert(key instanceof lobject.TValue);
- let hash = table_hash(key);
+ let hash = table_hash(L, key);
return setgeneric(t, hash, new lobject.TValue(key.type, key.value));
};
-const luaH_delete = function(t, key) {
+const luaH_delete = function(L, t, key) {
assert(key instanceof lobject.TValue);
- let hash = table_hash(key);
+ let hash = table_hash(L, key);
return mark_dead(t, hash);
};
@@ -183,7 +188,7 @@ const luaH_next = function(L, table, keyI) {
return false;
} else {
/* First find current key */
- let hash = table_hash(keyO);
+ let hash = table_hash(L, keyO);
/* Look in main part of table */
entry = table.strong.get(hash);
if (entry) {