summaryrefslogtreecommitdiff
path: root/src/ltable.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/ltable.js')
-rw-r--r--src/ltable.js53
1 files changed, 50 insertions, 3 deletions
diff --git a/src/ltable.js b/src/ltable.js
index c8bf7a2..b408cd1 100644
--- a/src/ltable.js
+++ b/src/ltable.js
@@ -7,8 +7,20 @@ const defs = require('./defs.js');
const ldebug = require('./ldebug.js');
const lobject = require('./lobject.js');
const lstring = require('./lstring.js');
+const lstate = require('./lstate.js');
const CT = defs.constant_types;
+/* used to prevent conflicts with lightuserdata keys */
+let lightuserdata_hashes = new WeakMap();
+const get_lightuserdata_hash = function(v) {
+ let hash = lightuserdata_hashes.get(v);
+ if (!hash) {
+ hash = Symbol("lightuserdata");
+ lightuserdata_hashes.set(v, hash);
+ }
+ return hash;
+};
+
const table_hash = function(L, key) {
switch(key.type) {
case CT.LUA_TNIL:
@@ -17,9 +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_TLIGHTUSERDATA: /* XXX: if user pushes conflicting lightuserdata then the table will do odd things */
- case CT.LUA_TNUMINT:
case CT.LUA_TTABLE:
case CT.LUA_TLCL:
case CT.LUA_TLCF:
@@ -30,6 +41,35 @@ const table_hash = function(L, key) {
case CT.LUA_TSHRSTR:
case CT.LUA_TLNGSTR:
return lstring.luaS_hashlongstr(key.tsvalue());
+ case CT.LUA_TLIGHTUSERDATA:
+ let v = key.value;
+ switch(typeof v) {
+ case "string":
+ /* possible conflict with LUA_TSTRING.
+ prefix this string with "*" so they don't clash */
+ return "*" + v;
+ case "number":
+ /* possible conflict with LUA_TNUMBER.
+ turn into string and prefix with "#" to avoid clash with other strings */
+ return "#" + v;
+ case "boolean":
+ /* possible conflict with LUA_TBOOLEAN. use strings ?true and ?false instead */
+ return v?"?true":"?false";
+ case "function":
+ /* possible conflict with LUA_TLCF.
+ indirect via a weakmap */
+ return get_lightuserdata_hash(v);
+ case "object":
+ /* v shouldn't be a CClosure, LClosure, Table or Userdata from this state as they're never exposed
+ the only exposed internal type is a lua_State */
+ if (v instanceof lstate.lua_State && v.l_G === L.l_G) {
+ /* indirect via a weakmap */
+ return get_lightuserdata_hash(v);
+ }
+ /* fall through */
+ default:
+ return v;
+ }
default:
throw new Error("unknown key type: " + key.type);
}
@@ -127,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;
@@ -153,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) {