diff --git a/client/space_lua/runtime.ts b/client/space_lua/runtime.ts index dff9adb7..df9f45b3 100644 --- a/client/space_lua/runtime.ts +++ b/client/space_lua/runtime.ts @@ -340,14 +340,22 @@ export class LuaTable implements ILuaSettable, ILuaGettable { return value.then(() => this.rawSet(key, value)); } if (typeof key === "string") { - this.stringKeys[key] = value; + if (value === null || value === undefined) { + delete this.stringKeys[key]; + } else { + this.stringKeys[key] = value; + } } else if (Number.isInteger(key) && key >= 1) { this.arrayPart[key - 1] = value; } else { if (!this.otherKeys) { this.otherKeys = new Map(); } - this.otherKeys.set(key, value); + if (value === null || value === undefined) { + this.otherKeys.delete(key); + } else { + this.otherKeys.set(key, value); + } } } diff --git a/client/space_lua/stdlib/table.ts b/client/space_lua/stdlib/table.ts index 107496cc..d4bcf56b 100644 --- a/client/space_lua/stdlib/table.ts +++ b/client/space_lua/stdlib/table.ts @@ -1,6 +1,7 @@ import { type ILuaFunction, LuaBuiltinFunction, + luaCall, type LuaEnv, luaEquals, luaGet, @@ -175,7 +176,7 @@ export const tableApi = new LuaTable({ const startIndex = fromIndex < 1 ? 1 : fromIndex; for (let i = startIndex; i <= tbl.length; i++) { const val = await luaGet(tbl, i, sf); - if (await criteriaFn.call(sf, val)) { + if (await luaCall(criteriaFn, [val], sf.astCtx!, sf)) { return new LuaMultiRes([i, val]); } } diff --git a/client/space_lua/stdlib/table_test.lua b/client/space_lua/stdlib/table_test.lua index a7d55eda..7b5e680a 100644 --- a/client/space_lua/stdlib/table_test.lua +++ b/client/space_lua/stdlib/table_test.lua @@ -126,6 +126,13 @@ assert(table.includes(t, "three")) assert(table.includes(t, true)) assert(not table.includes(t, "missing")) +-- Test using table as set +local s = {} +s["something"] = true +assertEqual(#table.keys(s), 1) +-- Remove from the map +s["something"] = nil +assertEqual(#table.keys(s), 0) -- Error cases local success, error = pcall(function() @@ -147,4 +154,4 @@ local t = { 0, 1, 2, 3 } local idx, two = table.find(t, function(v) return v == 2 end) assertEqual(idx, 3) assertEqual(two, 2) -assertEqual(table.find(t, function(v) return v == 4 end), nil) \ No newline at end of file +assertEqual(table.find(t, function(v) return v == 4 end), nil)