diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-05-11 11:19:55 +0200 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-05-11 11:19:55 +0200 |
commit | 992c58899b32c919d3c5c1c60c14c7c073c5d54c (patch) | |
tree | fdf156cf211355f383c2d6c44041531decca95f7 /src | |
parent | 4d4782d890830d7730f0f9ada5638f9443e76047 (diff) | |
parent | 57ca9b375d262d98645d219bbdd5969e0c0c85aa (diff) | |
download | fengari-992c58899b32c919d3c5c1c60c14c7c073c5d54c.tar.gz fengari-992c58899b32c919d3c5c1c60c14c7c073c5d54c.tar.bz2 fengari-992c58899b32c919d3c5c1c60c14c7c073c5d54c.zip |
Merge branch 'feature/dead-keys'
Diffstat (limited to 'src')
-rw-r--r-- | src/lobject.js | 12 | ||||
-rw-r--r-- | src/ltable.js | 23 |
2 files changed, 32 insertions, 3 deletions
diff --git a/src/lobject.js b/src/lobject.js index e4ef380..f926e85 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -13,6 +13,8 @@ const llimit = require('./llimit.js'); const CT = defs.constant_types; const char = defs.char; +const LUA_TPROTO = CT.LUA_NUMTAGS; +const LUA_TDEADKEY = CT.LUA_NUMTAGS+1; class TValue { @@ -108,7 +110,7 @@ class TValue { } ttisdeadkey() { - return this.checktag(CT.LUA_TDEADKEY); + return this.checktag(LUA_TDEADKEY); } l_isfalse() { @@ -150,6 +152,11 @@ class TValue { this.value = x; } + setdeadvalue() { + this.type = LUA_TDEADKEY; + this.value = null; + } + setfrom(tv) { /* in lua C source setobj2t is often used for this */ this.type = tv.type; this.value = tv.value; @@ -175,6 +182,7 @@ class TValue { } const luaO_nilobject = new TValue(CT.LUA_TNIL, null); +Object.freeze(luaO_nilobject); module.exports.luaO_nilobject = luaO_nilobject; class LClosure { @@ -539,6 +547,8 @@ const numarith = function(L, op, v1, v2) { } }; +module.exports.LUA_TPROTO = LUA_TPROTO; +module.exports.LUA_TDEADKEY = LUA_TDEADKEY; module.exports.CClosure = CClosure; module.exports.LClosure = LClosure; module.exports.LocVar = LocVar; diff --git a/src/ltable.js b/src/ltable.js index ce3ba77..0619efe 100644 --- a/src/ltable.js +++ b/src/ltable.js @@ -34,10 +34,17 @@ class Table { constructor(L) { this.id = L.l_G.id_counter++; this.strong = new Map(); + this.dead_hashes = []; this.metatable = null; } } +const clean_dead_keys = function(t) { + for (let i=0; i<t.dead_hashes.length; i++) { + t.strong.delete(t.dead_hashes[i]); + } +}; + const luaH_new = function(L) { return new Table(L); }; @@ -69,6 +76,7 @@ const setgeneric = function(t, hash, key) { if (v) return v.value; + clean_dead_keys(t); let tv = new lobject.TValue(CT.LUA_TNIL, null); t.strong.set(hash, { key: key, @@ -81,7 +89,7 @@ const luaH_setint = function(t, key, value) { assert(typeof key == "number" && (key|0) === key && value instanceof lobject.TValue); let hash = key; /* table_hash known result */ if (value.ttisnil()) { - t.strong.delete(hash); + delete_hash(t, hash); return; } let v = t.strong.get(hash); @@ -89,6 +97,7 @@ const luaH_setint = function(t, key, value) { let tv = v.value; tv.setfrom(value); } else { + clean_dead_keys(t); t.strong.set(hash, { key: new lobject.TValue(CT.LUA_TNUMINT, key), value: new lobject.TValue(value.type, value.value) @@ -102,10 +111,20 @@ const luaH_set = function(t, key) { return setgeneric(t, hash, new lobject.TValue(key.type, key.value)); }; +/* Can't remove from table immediately due to next() */ +const delete_hash = function(t, hash) { + let e = t.strong.get(hash); + if (e) { + e.key.setdeadvalue(); + e.value = new lobject.TValue(CT.LUA_TNIL, null); + t.dead_hashes.push(hash); + } +}; + const luaH_delete = function(t, key) { assert(key instanceof lobject.TValue); let hash = table_hash(key); - t.strong.delete(hash); + return delete_hash(t, hash); }; /* |