diff --git a/common/space_lua/language_core_test.lua b/common/space_lua/language_core_test.lua index 6519b5b4..979b886c 100644 --- a/common/space_lua/language_core_test.lua +++ b/common/space_lua/language_core_test.lua @@ -542,3 +542,21 @@ assertEqual(tonumber("1010", 1), nil) -- Invalid base assertEqual(tonumber("1010", 37), nil) -- Invalid base assertEqual(tonumber("FF", 10), nil) -- Invalid hex in decimal assertEqual(tonumber("8", 8), nil) -- Invalid octal digit + + +-- select tests +-- Base case +local a, b, c = select(1, 1, 2, 3) +assertEqual(a, 1) +assertEqual(b, 2) +assertEqual(c, 3) +-- One index later +local b, c = select(2, 1, 2, 3) +assertEqual(b, 2) +assertEqual(c, 3) +-- Negative index +local b, c = select(-2, 1, 2, 3) +assertEqual(b, 2) +assertEqual(c, 3) +-- Special "#" case +assertEqual(select("#", 1, 2, 3), 3) \ No newline at end of file diff --git a/common/space_lua/query_collection.ts b/common/space_lua/query_collection.ts index 569da6db..f3801efa 100644 --- a/common/space_lua/query_collection.ts +++ b/common/space_lua/query_collection.ts @@ -102,8 +102,8 @@ export async function applyTransforms( if (query.orderBy) { // Retrieve from config API if not passed if (collation === undefined) { - // const config = sf.threadLocal.get("_GLOBAL").get("config"); // FIXME: Somehow _GLOBAL is empty here - const config = client.config; // HACK: Shouldn't be using client here directly + // @ts-ignore: Hack to access client via the browser + const config = globalThis.client.config; // HACK: Shouldn't be using client here directly collation = config.get("queryCollation", {}); } diff --git a/common/space_lua/stdlib.ts b/common/space_lua/stdlib.ts index 361ce47a..de17bfb9 100644 --- a/common/space_lua/stdlib.ts +++ b/common/space_lua/stdlib.ts @@ -204,6 +204,27 @@ const dofileFunction = new LuaBuiltinFunction(async (sf, filename: string) => { } }); +/** + * From the Lua docs: + * If index is a number, returns all arguments after argument number index; a negative number indexes + * from the end (-1 is the last argument). Otherwise, index must be the string "#", and select returns + * the total number of extra arguments it received. + */ +const selectFunction = new LuaBuiltinFunction( + (_sf, index: number | "#", ...args: LuaValue[]) => { + if (index === "#") { + return args.length; + } else if (typeof index === "number") { + if (index >= 0) { + return new LuaMultiRes(args.slice(index - 1)); + } else { + return new LuaMultiRes(args.slice(args.length + index)); + } + } + }, +); + +// Non-standard, but useful const someFunction = new LuaBuiltinFunction(async (_sf, value: any) => { switch (await luaTypeOf(value)) { case "number": @@ -227,6 +248,7 @@ export function luaBuildStandardEnv() { env.set("tostring", tostringFunction); env.set("tonumber", tonumberFunction); env.set("unpack", unpackFunction); + env.set("select", selectFunction); // Iterators env.set("pairs", pairsFunction); env.set("ipairs", ipairsFunction);