From 3b0c38d72d85821536afc0e4eea8041a0a5f9799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Jan=20Fialka?= Date: Mon, 10 Nov 2025 13:23:32 +0100 Subject: [PATCH] Space Lua: Perf improvements (#1666) * Add result-or-promise (RP) exec model to interpreter (async-first). * Add centralized table property access with fast misses. * Update table string keys to be object map. * Update error contexts to be more consistent and precise. * Update evaluator to return either values or _Promises_. * Update func calls, unary and bin operations, and expression lists. * Update _stdlib_ to the changes and improve errors. * Add tests to validate async control flow and error contexts. * Add dedicated benches that isolate the _runtime_ from parsing. --- client/space_lua/ast_narrow.ts | 81 ++ client/space_lua/break_async.test.ts | 179 ++++ client/space_lua/context_errors.test.ts | 146 ++++ client/space_lua/eval.ts | 1041 +++++++++++++++++------ client/space_lua/query_collection.ts | 2 +- client/space_lua/rp.bench.ts | 270 ++++++ client/space_lua/rp.ts | 38 + client/space_lua/runtime.ts | 404 ++++++--- client/space_lua/stdlib.ts | 74 +- client/space_lua/stdlib/table.ts | 2 +- 10 files changed, 1791 insertions(+), 446 deletions(-) create mode 100644 client/space_lua/ast_narrow.ts create mode 100644 client/space_lua/break_async.test.ts create mode 100644 client/space_lua/context_errors.test.ts create mode 100644 client/space_lua/rp.bench.ts create mode 100644 client/space_lua/rp.ts diff --git a/client/space_lua/ast_narrow.ts b/client/space_lua/ast_narrow.ts new file mode 100644 index 00000000..336330aa --- /dev/null +++ b/client/space_lua/ast_narrow.ts @@ -0,0 +1,81 @@ +// Typed narrowers for AST nodes to avoid `(node as any)` in consumers. + +import type { ASTCtx, LuaExpression, LuaLValue, LuaStatement } from "./ast.ts"; + +// Extract by `type` discriminant +type NarrowByType = + Extract; + +// Expressions +export const asStringExpr = (e: LuaExpression) => + e as NarrowByType; +export const asNumberExpr = (e: LuaExpression) => + e as NarrowByType; +export const asBooleanExpr = (e: LuaExpression) => + e as NarrowByType; +export const asNilExpr = (e: LuaExpression) => + e as NarrowByType; +export const asUnary = (e: LuaExpression) => + e as NarrowByType; +export const asBinary = (e: LuaExpression) => + e as NarrowByType; +export const asVariable = (e: LuaExpression) => + e as NarrowByType; +export const asFunctionCall = (e: LuaExpression) => + e as NarrowByType; +export const asTableAccess = (e: LuaExpression) => + e as NarrowByType; +export const asPropertyAccess = (e: LuaExpression) => + e as NarrowByType; +export const asParenthesized = (e: LuaExpression) => + e as NarrowByType; +export const asTableConstructor = (e: LuaExpression) => + e as NarrowByType; +export const asFunctionDef = (e: LuaExpression) => + e as NarrowByType; +export const asQueryExpr = (e: LuaExpression) => + e as NarrowByType; + +// L-values +export const asLValueVariable = (l: LuaLValue) => + l as Extract; +export const asLValueTableAccess = (l: LuaLValue) => + l as Extract; +export const asLValuePropertyAccess = (l: LuaLValue) => + l as Extract; + +// Statements +export const asAssignment = (s: LuaStatement) => + s as NarrowByType; +export const asLocal = (s: LuaStatement) => + s as NarrowByType; +export const asBlock = (s: LuaStatement) => + s as NarrowByType; +export const asIf = (s: LuaStatement) => s as NarrowByType; +export const asWhile = (s: LuaStatement) => + s as NarrowByType; +export const asRepeat = (s: LuaStatement) => + s as NarrowByType; +export const asBreak = (s: LuaStatement) => + s as NarrowByType; +export const asFunctionStmt = (s: LuaStatement) => + s as NarrowByType; +export const asLocalFunction = (s: LuaStatement) => + s as NarrowByType; +export const asFunctionCallStmt = (s: LuaStatement) => + s as NarrowByType; +export const asReturn = (s: LuaStatement) => + s as NarrowByType; +export const asFor = (s: LuaStatement) => + s as NarrowByType; +export const asForIn = (s: LuaStatement) => + s as NarrowByType; +export const asLabel = (s: LuaStatement) => + s as NarrowByType; +export const asGoto = (s: LuaStatement) => + s as NarrowByType; +export const asSemicolon = (s: LuaStatement) => + s as NarrowByType; + +// Pull ctx with a single shape +export const ctxOf = (node: { ctx: ASTCtx }): ASTCtx => node.ctx; diff --git a/client/space_lua/break_async.test.ts b/client/space_lua/break_async.test.ts new file mode 100644 index 00000000..6488de48 --- /dev/null +++ b/client/space_lua/break_async.test.ts @@ -0,0 +1,179 @@ +import { assertEquals } from "@std/assert/equals"; +import { parse } from "./parse.ts"; +import { evalStatement } from "./eval.ts"; +import { LuaEnv, LuaNativeJSFunction, LuaStackFrame } from "./runtime.ts"; +import { luaBuildStandardEnv } from "./stdlib.ts"; + +async function evalBlock(code: string, env?: LuaEnv) { + const ast = parse(code); + // Use the same global env for both the base env's parent and the StackFrame's _GLOBAL + const G = luaBuildStandardEnv(); + const base = env ?? new LuaEnv(G); + const sf = LuaStackFrame.createWithGlobalEnv(G, ast.ctx); + const r = evalStatement(ast, base, sf, false); + if (r instanceof Promise) await r; + return base; +} + +Deno.test("Nested for loops: inner break (sync body)", async () => { + const env = new LuaEnv(luaBuildStandardEnv()); + await evalBlock( + ` + local t = {1,2,3} + found = 0 + for i = 1, #t do + for j = i + 1, #t do + found = found + 1 + break + error("BUG! Execution continued after break!") + end + end + `, + env, + ); + assertEquals(env.get("found"), 2); +}); + +Deno.test("Nested for loops: inner break with async call in body (Promise path)", async () => { + const env = new LuaEnv(luaBuildStandardEnv()); + env.set( + "asyncOne", + new LuaNativeJSFunction(() => Promise.resolve(1)), + ); + await evalBlock( + ` + local t = {1,2,3} + found = 0 + for i = 1, #t do + for j = i + 1, #t do + asyncOne() -- forces Promise in body + found = found + 1 -- still must execute before break + break -- breaks only inner loop + error("BUG! Execution continued after break!") + end + end + `, + env, + ); + assertEquals(env.get("found"), 2); +}); + +Deno.test("While: Promise condition, break inside body", async () => { + const env = new LuaEnv(luaBuildStandardEnv()); + // asyncTrue returns a truthy value via Promise to trigger Promise-based while path + env.set( + "asyncTrue", + new LuaNativeJSFunction(() => Promise.resolve(1)), + ); + await evalBlock( + ` + cnt = 0 + local first = true + local function cond() + if first then + first = false + return asyncTrue() + end + return nil + end + while cond() do + cnt = cnt + 1 + break + error("BUG! Execution continued after break!") + end + `, + env, + ); + assertEquals(env.get("cnt"), 1); +}); + +Deno.test("Repeat-until: body goes Promise once, break handled locally", async () => { + const env = new LuaEnv(luaBuildStandardEnv()); + env.set( + "asyncOne", + new LuaNativeJSFunction(() => Promise.resolve(1)), + ); + await evalBlock( + ` + n = 0 + repeat + asyncOne() -- body Promise + n = n + 1 + break + error("BUG! Execution continued after break!") + until true + `, + env, + ); + assertEquals(env.get("n"), 1); +}); + +Deno.test("For-in: custom iterator, Promise in body before break", async () => { + const env = new LuaEnv(luaBuildStandardEnv()); + env.set( + "asyncOne", + new LuaNativeJSFunction(() => Promise.resolve(1)), + ); + await evalBlock( + ` + function gen(n) + local i = 0 + return function() + i = i + 1 + if i <= n then return i end + end + end + + hits = 0 + for v in gen(3) do + asyncOne() -- force Promise during body + hits = hits + 1 + break + error("BUG! Execution continued after break!") + end + `, + env, + ); + assertEquals(env.get("hits"), 1); +}); + +Deno.test("Jaro-like inner-window matching: inner break with occasional Promise", async () => { + const env = new LuaEnv(luaBuildStandardEnv()); + env.set( + "tick", + new LuaNativeJSFunction(() => Promise.resolve(true)), + ); + await evalBlock( + ` + local s1 = "#aa" + local s2 = "#ab" + local l1, l2 = #s1, #s2 + local md = (l1 > l2 and l1 or l2) // 2 - 1 + if md < 0 then md = 0 end + + s1m = {} + s2m = {} + mc = 0 + + for i = 1, l1 do + local start = (1 > i - md) and 1 or (i - md) + local finish = ((i + md) < l2) and (i + md) or l2 + + for j = start, finish do + tick() -- Promise in inner loop body to exercise Promise path + if not s2m[j] and s1:byte(i) == s2:byte(j) then + s1m[i] = true + s2m[j] = true + mc = mc + 1 + break + error("BUG! Execution continued after break!") + end + end + end + + ok = (mc >= 1) + `, + env, + ); + assertEquals(env.get("ok"), true); +}); diff --git a/client/space_lua/context_errors.test.ts b/client/space_lua/context_errors.test.ts new file mode 100644 index 00000000..d2a30678 --- /dev/null +++ b/client/space_lua/context_errors.test.ts @@ -0,0 +1,146 @@ +import { parse } from "./parse.ts"; +import { evalStatement } from "./eval.ts"; +import { LuaEnv, LuaRuntimeError, LuaStackFrame } from "./runtime.ts"; +import { luaBuildStandardEnv } from "./stdlib.ts"; +import { assertInstanceOf, assertStringIncludes } from "@std/assert"; + +async function runAndCatch(code: string, ref = "ctx_test.lua") { + const ast = parse(code, { ref }); + const global = luaBuildStandardEnv(); + const env = new LuaEnv(global); + const sf = LuaStackFrame.createWithGlobalEnv(global, ast.ctx); + try { + const r = evalStatement(ast, env, sf, false); + if (r instanceof Promise) await r; + throw new Error("Expected LuaRuntimeError but evaluation succeeded"); + } catch (e: any) { + return { e, code, ref }; + } +} + +function assertCtxErrorContains( + e: unknown, + code: string, + ref: string, + msgIncludes: string, +) { + assertInstanceOf(e, LuaRuntimeError); + const err = e as LuaRuntimeError; + assertStringIncludes(err.message, msgIncludes); + const pretty = err.toPrettyString(code); + assertStringIncludes(pretty, msgIncludes); + assertStringIncludes(pretty, ref); + // caret presence sanity check + assertStringIncludes(pretty, "^"); +} + +Deno.test("Context error: indexing nil value includes message and ref", async () => { + const { e, code, ref } = await runAndCatch( + ` + local t = nil + local x = t[1] + `, + "index_nil.lua", + ); + assertCtxErrorContains(e, code, ref, "attempt to index a nil value"); +}); + +Deno.test("Context error: indexing with nil key includes message and ref", async () => { + const { e, code, ref } = await runAndCatch( + ` + local t = {} + local k = nil + local x = t[k] + `, + "nil_key.lua", + ); + assertCtxErrorContains(e, code, ref, "attempt to index with a nil key"); +}); + +Deno.test("Context error: calling nil includes message and ref", async () => { + const { e, code, ref } = await runAndCatch( + ` + local f = nil + f() + `, + "call_nil.lua", + ); + assertCtxErrorContains(e, code, ref, "attempt to call a nil value"); +}); + +Deno.test("Context error: modulo by zero includes message and ref", async () => { + const { e, code, ref } = await runAndCatch( + ` + local a = 1 + local b = 0 + local c = a % b + `, + "mod_zero.lua", + ); + assertCtxErrorContains(e, code, ref, "attempt to perform modulo by zero"); +}); + +Deno.test("Context error: type mismatch in comparison includes message and ref", async () => { + const { e, code, ref } = await runAndCatch( + ` + local x = "a" < 1 + `, + "compare_type.lua", + ); + assertCtxErrorContains(e, code, ref, "attempt to compare string with number"); +}); + +/** + * Additional generic context-error coverage for lazy dispatch: + * - __index metamethod wrong type (neither function nor table) + * - __call metamethod wrong type (non-callable) + * - length operator on unsupported type + */ +Deno.test("Context error: __index metamethod must be function or table", async () => { + const { e, code, ref } = await runAndCatch( + ` + local t = {} + setmetatable(t, { __index = 5 }) + local x = t.foo + `, + "meta_index_wrong.lua", + ); + assertCtxErrorContains( + e, + code, + ref, + "Meta table __index must be a function or table", + ); +}); + +Deno.test("Context error: __call metamethod must be a function", async () => { + const { e, code, ref } = await runAndCatch( + ` + local t = {} + setmetatable(t, { __call = 5 }) + t() + `, + "meta_call_wrong.lua", + ); + assertCtxErrorContains( + e, + code, + ref, + "Meta table __call must be a function", + ); +}); + +Deno.test("Context error: length operator wrong type (number)", async () => { + const { e, code, ref } = await runAndCatch( + ` + local x = #1 + `, + "len_number.lua", + ); + assertCtxErrorContains( + e, + code, + ref, + "attempt to get length of a number value", + ); +}); diff --git a/client/space_lua/eval.ts b/client/space_lua/eval.ts index 944234d1..215d6bbb 100644 --- a/client/space_lua/eval.ts +++ b/client/space_lua/eval.ts @@ -7,6 +7,7 @@ import type { } from "./ast.ts"; import { evalPromiseValues } from "./util.ts"; import { + getMetatable, type ILuaFunction, type ILuaGettable, type ILuaSettable, @@ -42,6 +43,46 @@ import { coerceNumericPair, type OpHints, } from "./numeric.ts"; +import { isPromise, rpAll, rpThen } from "./rp.ts"; +import { + asAssignment, + asBinary, + asBlock, + asFor, + asForIn, + asFunctionCall, + asFunctionCallStmt, + asFunctionDef, + asFunctionStmt, + asIf, + asLocal, + asLocalFunction, + asLValuePropertyAccess, + asLValueTableAccess, + asLValueVariable, + asParenthesized, + asPropertyAccess, + asQueryExpr, + asRepeat, + asReturn, + asTableAccess, + asTableConstructor, + asUnary, + asVariable, + asWhile, +} from "./ast_narrow.ts"; + +// Queryable guard to avoid `(collection as any).query` usage +type Queryable = { + query: ( + q: LuaCollectionQuery, + env: LuaEnv, + sf: LuaStackFrame, + ) => Promise; +}; +function isQueryable(x: unknown): x is Queryable { + return !!x && typeof (x as any).query === "function"; +} function luaFloorDiv( a: unknown, @@ -230,36 +271,38 @@ export function evalExpression( return null; } case "Binary": { - if (e.operator === "or") { + const b = asBinary(e); + if (b.operator === "or") { // Special case: eagerly evaluate left before even attempting right - return evalLogical("or", e.left, e.right, env, sf); - } else if (e.operator === "and") { + return evalLogical("or", b.left, b.right, env, sf); + } else if (b.operator === "and") { // Special case: eagerly evaluate left before even attempting right - return evalLogical("and", e.left, e.right, env, sf); + return evalLogical("and", b.left, b.right, env, sf); } // Enforce left-to-right evaluation - const hints: OpHints = opHintsFromBinary(e); + const hints: OpHints = opHintsFromBinary(b); return evalBinaryWithLR( - e.operator, - e.left, - e.right, - e.ctx, + b.operator, + b.left, + b.right, + b.ctx, env, sf, hints, ); } case "Unary": { - const value = evalExpression(e.argument, env, sf); - if (value instanceof Promise) { + const u = asUnary(e); + const value = evalExpression(u.argument, env, sf); + if (isPromise(value)) { return value.then((value) => { - switch (e.operator) { + switch (u.operator) { case "-": { const arg = singleResult(value); return unaryWithMeta( arg, "__unm", - e.ctx, + u.ctx, sf, () => luaUnaryMinus(arg), ); @@ -268,37 +311,37 @@ export function evalExpression( return +singleResult(value); } case "not": { - return !luaTruthy(singleResult(value)); + return !luaTruthy(value); } case "~": { const arg = singleResult(value); return unaryWithMeta( arg, "__bnot", - e.ctx, + u.ctx, sf, - () => ~exactInt(arg, e.ctx, sf), + () => ~exactInt(arg, u.ctx, sf), ); } case "#": { - return luaLengthOp(singleResult(value), e.ctx, sf); + return luaLengthOp(singleResult(value), u.ctx, sf); } default: { throw new LuaRuntimeError( - `Unknown unary operator ${e.operator}`, - sf.withCtx(e.ctx), + `Unknown unary operator ${u.operator}`, + sf.withCtx(u.ctx), ); } } }); } else { - switch (e.operator) { + switch (u.operator) { case "-": { const arg = singleResult(value); return unaryWithMeta( arg, "__unm", - e.ctx, + u.ctx, sf, () => luaUnaryMinus(arg), ); @@ -307,25 +350,25 @@ export function evalExpression( return +singleResult(value); } case "not": { - return !luaTruthy(singleResult(value)); + return !luaTruthy(value); } case "~": { const arg = singleResult(value); return unaryWithMeta( arg, "__bnot", - e.ctx, + u.ctx, sf, - () => ~exactInt(arg, e.ctx, sf), + () => ~exactInt(arg, u.ctx, sf), ); } case "#": { - return luaLengthOp(singleResult(value), e.ctx, sf); + return luaLengthOp(singleResult(value), u.ctx, sf); } default: { throw new LuaRuntimeError( - `Unknown unary operator ${e.operator}`, - sf.withCtx(e.ctx), + `Unknown unary operator ${u.operator}`, + sf.withCtx(u.ctx), ); } } @@ -339,10 +382,11 @@ export function evalExpression( return evalPrefixExpression(e, env, sf); } case "TableConstructor": { + const tc = asTableConstructor(e); return Promise.resolve().then(async () => { const table = new LuaTable(); - for (const field of e.fields) { + for (const field of tc.fields) { await handleTableFieldSync(table, field, env, sf); } @@ -350,15 +394,16 @@ export function evalExpression( }); } case "FunctionDefinition": { - return new LuaFunction(e.body, env); + const fd = asFunctionDef(e); + return new LuaFunction(fd.body, env); } case "Query": { - // console.log("Query", e); - const findFromClause = e.clauses.find((c) => c.type === "From"); + const q = asQueryExpr(e); + const findFromClause = q.clauses.find((c) => c.type === "From"); if (!findFromClause) { throw new LuaRuntimeError( "No from clause found", - sf.withCtx(e.ctx), + sf.withCtx(q.ctx), ); } const objectVariable = findFromClause.name; @@ -368,18 +413,18 @@ export function evalExpression( if (!collection) { throw new LuaRuntimeError( "Collection is nil", - sf.withCtx(e.ctx), + sf.withCtx(q.ctx), ); } collection = luaValueToJS(collection, sf); // Check if collection is a queryable collection - if (!collection.query) { + if (!isQueryable(collection)) { // If not, try to convert it to JS and see if it's an array collection = await luaValueToJS(collection, sf); if (!Array.isArray(collection)) { throw new LuaRuntimeError( "Collection does not support query", - sf.withCtx(e.ctx), + sf.withCtx(q.ctx), ); } collection = new ArrayQueryCollection(collection); @@ -391,7 +436,7 @@ export function evalExpression( }; // Map clauses to query parameters - for (const clause of e.clauses) { + for (const clause of q.clauses) { switch (clause.type) { case "Where": { query.where = clause.expression; @@ -424,7 +469,9 @@ export function evalExpression( } } - return collection.query(query, env, sf).then(jsToLuaValue); + return (collection as Queryable).query(query, env, sf).then( + jsToLuaValue, + ); }, ); } @@ -451,7 +498,8 @@ function evalPrefixExpression( ): Promise | LuaValue { switch (e.type) { case "Variable": { - const value = env.get(e.name); + const v = asVariable(e); + const value = env.get(v.name); if (value === undefined) { return null; } else { @@ -459,76 +507,116 @@ function evalPrefixExpression( } } case "Parenthesized": { - return evalExpression(e.expression, env, sf); + const p = asParenthesized(e); + return evalExpression(p.expression, env, sf); } // <>[<>] case "TableAccess": { - const values = evalPromiseValues([ - evalPrefixExpression(e.object, env, sf), - evalExpression(e.key, env, sf), - ]); - if (values instanceof Promise) { - return values.then(([table, key]) => { - table = singleResult(table); - key = singleResult(key); + const ta = asTableAccess(e); + // Sync-first: evaluate object and key without allocating Promise when both are sync. + const objV = evalPrefixExpression(ta.object, env, sf); + const keyV = evalExpression(ta.key, env, sf); - return luaGet(table, key, sf.withCtx(e.ctx)); - }); - } else { - const table = singleResult(values[0]); - const key = singleResult(values[1]); - return luaGet(table, singleResult(key), sf.withCtx(e.ctx)); + if (!isPromise(objV) && !isPromise(keyV)) { + const table = singleResult(objV); + const key = singleResult(keyV); + return luaGet(table, key, ta.ctx, sf); } + + return rpThen( + objV, + (obj) => + rpThen( + keyV, + (key) => luaGet(singleResult(obj), singleResult(key), ta.ctx, sf), + ), + ); } // .property case "PropertyAccess": { - const obj = evalPrefixExpression(e.object, env, sf); - if (obj instanceof Promise) { - return obj.then((obj) => { - return luaGet(obj, e.property, sf.withCtx(e.ctx)); - }); - } else { - return luaGet(obj, e.property, sf.withCtx(e.ctx)); + const pa = asPropertyAccess(e); + // Sync-first: evaluate object; avoid Promise when object is sync. + const objV = evalPrefixExpression(pa.object, env, sf); + if (!isPromise(objV)) { + return luaGet(objV, pa.property, pa.ctx, sf); } + return rpThen(objV, (obj) => luaGet(obj, pa.property, pa.ctx, sf)); } case "FunctionCall": { - const prefixValue = evalPrefixExpression(e.prefix, env, sf); + const fc = asFunctionCall(e); + const prefixValue = evalPrefixExpression(fc.prefix, env, sf); if (prefixValue === null || prefixValue === undefined) { + const nilMsg = fc.prefix.type === "Variable" + ? `attempt to call a nil value (global '${ + asVariable(fc.prefix).name + }')` + : `attempt to call a nil value`; throw new LuaRuntimeError( - `Attempting to call nil as a function`, - sf.withCtx(e.prefix.ctx), + nilMsg, + sf.withCtx(fc.prefix.ctx), ); } let selfArgs: LuaValue[] = []; const handleFunctionCall = ( - prefixValue: LuaValue, + calleeVal: LuaValue, ): LuaValue | Promise => { // Normal argument handling for hello:there(a, b, c) type calls - if (e.name) { - selfArgs = [prefixValue]; - prefixValue = luaIndexValue(prefixValue, e.name, sf); + if (fc.name) { + selfArgs = [calleeVal]; + calleeVal = luaIndexValue(calleeVal, fc.name, sf); - if (prefixValue instanceof Promise) { - return prefixValue.then(handleFunctionCall); + if (isPromise(calleeVal)) { + return (calleeVal as Promise).then(handleFunctionCall); } } // Unsure if part of the spec, but it seems to be common for lua implementations // to evaluate all args before evaluating the callee - const args = evalExpressions(e.args, env, sf); + const parts = fc.args.map((arg: LuaExpression) => + evalExpression(arg, env, sf) + ); + const argsVal = rpAll(parts); - if (args instanceof Promise) { - return args.then((args) => - luaCall(prefixValue, [...selfArgs, ...args], e.ctx, sf) - ); + const thenCall = (args: LuaValue[]) => + luaCall(calleeVal, [...selfArgs, ...args], fc.ctx, sf); + + if (isPromise(argsVal)) { + return (argsVal as Promise).then((argsResolved: any[]) => { + if (argsResolved.length === 0) return thenCall([]); + const out: LuaValue[] = []; + // All but last expression produce a single value + for (let i = 0; i < argsResolved.length - 1; i++) { + out.push(singleResult(argsResolved[i])); + } + // Last expression preserves multiple results + const last = argsResolved[argsResolved.length - 1]; + if (last instanceof LuaMultiRes) { + out.push(...last.flatten().values); + } else { + out.push(singleResult(last)); + } + return thenCall(out); + }); } else { - return luaCall(prefixValue, [...selfArgs, ...args], e.ctx, sf); + const argsResolved = argsVal as LuaValue[]; + if (argsResolved.length === 0) return thenCall([]); + const out: LuaValue[] = []; + for (let i = 0; i < argsResolved.length - 1; i++) { + out.push(singleResult(argsResolved[i])); + } + const last = argsResolved[argsResolved.length - 1]; + if (last instanceof LuaMultiRes) { + out.push(...last.flatten().values); + } else { + out.push(singleResult(last)); + } + return thenCall(out); } }; - if (prefixValue instanceof Promise) { - return prefixValue.then(handleFunctionCall); + if (isPromise(prefixValue)) { + return (prefixValue as Promise).then(handleFunctionCall); } else { return handleFunctionCall(prefixValue); } @@ -586,7 +674,9 @@ function unaryWithMeta( const mm = evalUnaryMetamethod(arg, meta, ctx, sf); if (mm !== undefined) { - return mm instanceof Promise ? mm.then(singleResult) : singleResult(mm); + return isPromise(mm) + ? (mm as Promise).then(singleResult) + : singleResult(mm); } return fallback(); } @@ -607,18 +697,22 @@ function evalLogical( return singleResult(lv); } const rv = evalExpression(rightExpr, env, sf); - return rv instanceof Promise ? rv.then(singleResult) : singleResult(rv); + return isPromise(rv) + ? (rv as Promise).then(singleResult) + : singleResult(rv); } else { if (!luaTruthy(lv)) { return singleResult(lv); } const rv = evalExpression(rightExpr, env, sf); - return rv instanceof Promise ? rv.then(singleResult) : singleResult(rv); + return isPromise(rv) + ? (rv as Promise).then(singleResult) + : singleResult(rv); } }; - if (left instanceof Promise) { - return left.then(decide); + if (isPromise(left)) { + return (left as Promise).then(decide); } else { return decide(left); } @@ -644,36 +738,51 @@ function evalBinaryWithLR( ): any { const leftVal = evalExpression(leftExpr, env, sf); - const evalRightAndOp = (lv: any) => { + if (!isPromise(leftVal)) { const rightVal = evalExpression(rightExpr, env, sf); - if (rightVal instanceof Promise) { - return rightVal.then((rv) => - luaOp( - op, - singleResult(lv), - singleResult(rv), - ctx, - sf, - hints, - ) - ); - } else { + if (!isPromise(rightVal)) { return luaOp( op, - singleResult(leftVal instanceof Promise ? lv : leftVal), + singleResult(leftVal), singleResult(rightVal), ctx, sf, hints, ); } - }; - - if (leftVal instanceof Promise) { - return leftVal.then(evalRightAndOp); - } else { - return evalRightAndOp(leftVal); + return rpThen(rightVal, (rv) => + luaOp( + op, + singleResult(leftVal), + singleResult(rv), + ctx, + sf, + hints, + )); } + + return rpThen(leftVal, (lv) => { + const rightVal = evalExpression(rightExpr, env, sf); + if (!isPromise(rightVal)) { + return luaOp( + op, + singleResult(lv), + singleResult(rightVal), + ctx, + sf, + hints, + ); + } + return rpThen(rightVal, (rv) => + luaOp( + op, + singleResult(lv), + singleResult(rv), + ctx, + sf, + hints, + )); + }); } // Relational comparison "prelude" @@ -694,38 +803,6 @@ function luaRelOperands( return { ta, tb, av, bv }; } -export function getMetatable( - value: LuaValue, - sf?: LuaStackFrame, -): LuaValue | null { - if (value === null || value === undefined) { - return null; - } - if (typeof value === "string") { - // Add a metatable to the string value on the fly - if (!sf) { - console.warn( - "metatable lookup with string value but no stack frame, returning nil", - ); - return null; - } - if (!sf.threadLocal.get("_GLOBAL")) { - console.warn( - "metatable lookup with string value but no _GLOBAL, returning nil", - ); - return null; - } - const stringMetatable = new LuaTable(); - stringMetatable.set("__index", sf.threadLocal.get("_GLOBAL").get("string")); - return stringMetatable; - } - if (value.metatable) { - return value.metatable; - } else { - return null; - } -} - // Simplified operator definitions const operatorsMetaMethods: Record | LuaValue[] { - const argsVal = evalPromiseValues( - es.map((arg) => evalExpression(arg, env, sf)), - ); + // Evaluate all arguments first (sync-first); do not allocate a Promise if all are sync. + const parts = es.map((arg) => evalExpression(arg, env, sf)); + const argsVal = rpAll(parts); // In Lua multi-returns propagate only in tail position of an expression // list. @@ -988,11 +1065,9 @@ function evalExpressions( return out; }; - if (argsVal instanceof Promise) { - return argsVal.then(finalize); - } else { - return finalize(argsVal as any[]); - } + return isPromise(argsVal) + ? (argsVal as Promise).then(finalize) + : finalize(argsVal as LuaValue[]); } /** @@ -1000,44 +1075,87 @@ function evalExpressions( * 1. with `returnOnReturn` set to `true` will return the value of a return statement * 2. with `returnOnReturn` set to `false` will throw a LuaReturn exception if a return statement is encountered */ -export async function evalStatement( +export function evalStatement( s: LuaStatement, env: LuaEnv, sf: LuaStackFrame, returnOnReturn = false, -): Promise { +): void | LuaValue[] | Promise { switch (s.type) { case "Assignment": { - const values = await evalExpressions(s.expressions, env, sf); - const lvalues = await evalPromiseValues(s.variables + const a = asAssignment(s); + const valuesRP = evalExpressions(a.expressions, env, sf); + const lvaluesRP = evalPromiseValues(a.variables .map((lval) => evalLValue(lval, env, sf))); - for (let i = 0; i < lvalues.length; i++) { - await luaSet( - lvalues[i].env, - lvalues[i].key, - values[i], - sf.withCtx(s.ctx), + const apply = (values: LuaValue[], lvalues: { env: any; key: any }[]) => { + const ps: Promise[] = []; + for (let i = 0; i < lvalues.length; i++) { + const r = luaSet( + lvalues[i].env, + lvalues[i].key, + values[i], + sf.withCtx(a.ctx), + ); + if (isPromise(r)) { + ps.push(r); + } + } + if (ps.length) { + return Promise.all(ps).then(() => undefined); + } + return; + }; + + if (!isPromise(valuesRP) && !isPromise(lvaluesRP)) { + return apply( + valuesRP as LuaValue[], + lvaluesRP as LuaLValueContainer[], + ); + } else if ( + isPromise(valuesRP) && !isPromise(lvaluesRP) + ) { + return (valuesRP as Promise).then((values: LuaValue[]) => + apply(values, lvaluesRP as LuaLValueContainer[]) + ); + } else if ( + !isPromise(valuesRP) && isPromise(lvaluesRP) + ) { + return (lvaluesRP as Promise).then((lvalues: any[]) => + apply(valuesRP as LuaValue[], lvalues) + ); + } else { + return (valuesRP as Promise).then((values: LuaValue[]) => + (lvaluesRP as Promise).then((lvalues: any[]) => + apply(values, lvalues) + ) ); } - - break; } case "Local": { - if (s.expressions) { - const values = await evalExpressions(s.expressions, env, sf); - for (let i = 0; i < s.names.length; i++) { - env.setLocal(s.names[i].name, values[i]); + const l = asLocal(s); + if (l.expressions) { + const valuesRP = evalExpressions(l.expressions, env, sf); + const setAll = (values: LuaValue[]) => { + for (let i = 0; i < l.names.length; i++) { + env.setLocal(l.names[i].name, values[i]); + } + return; + }; + if (isPromise(valuesRP)) { + return (valuesRP as Promise).then(setAll); + } else { + return setAll(valuesRP); } } else { - for (let i = 0; i < s.names.length; i++) { - env.setLocal(s.names[i].name, null); + for (let i = 0; i < l.names.length; i++) { + env.setLocal(l.names[i].name, null); } + return; } - break; } case "Semicolon": { - break; + return; } case "Label": case "Goto": { @@ -1047,38 +1165,159 @@ export async function evalStatement( ); } case "Block": { + const b = asBlock(s); const newEnv = new LuaEnv(env); - for (const statement of s.statements) { - const result = await evalStatement( - statement, - newEnv, - sf, - returnOnReturn, - ); - // Will only happen with `return` statement - if (result !== undefined) { - return result; + const stmts = b.statements; + + // Sync-first execution: iterate statements in a simple loop; if + // a statement returns a Promise, immediately switch to async by + // returning a continuation that resumes execution from the next + // statement (`i + 1`). + const processFrom = ( + idx: number, + ): void | LuaValue[] | Promise => { + for (let i = idx; i < stmts.length; i++) { + const result = evalStatement( + stmts[i], + newEnv, + sf, + returnOnReturn, + ); + if (isPromise(result)) { + return (result as Promise).then((res) => { + if (res !== undefined) { + return res; + } + return processFrom(i + 1); + }); + } + // Will only happen with `return` statement + if (result !== undefined) { + return result; + } } - } - break; + return; + }; + + return processFrom(0); } case "If": { - for (const cond of s.conditions) { - if (luaTruthy(await evalExpression(cond.condition, env, sf))) { - await evalStatement(cond.block, env, sf); + const iff = asIf(s); + // Evaluate conditions in order; avoid awaiting when not necessary + const conds = iff.conditions; + + const runFrom = ( + i: number, + ): void | LuaValue[] | Promise => { + if (i >= conds.length) { + if (iff.elseBlock) { + return evalStatement(iff.elseBlock, env, sf, returnOnReturn); + } return; } - } - if (s.elseBlock) { - await evalStatement(s.elseBlock, env, sf); - return; - } - break; + const cv = evalExpression(conds[i].condition, env, sf); + if (isPromise(cv)) { + return (cv as Promise).then((val) => { + if (luaTruthy(val)) { + const r = evalStatement(conds[i].block, env, sf, returnOnReturn); + return isPromise(r) ? r : r; + } + return runFrom(i + 1); + }); + } else { + if (luaTruthy(cv)) { + return evalStatement(conds[i].block, env, sf, returnOnReturn); + } + return runFrom(i + 1); + } + }; + + return runFrom(0); } case "While": { - while (luaTruthy(await evalExpression(s.condition, env, sf))) { + const w = asWhile(s); + const runAsync = async () => { + while (true) { + const c = await evalExpression(w.condition, env, sf); + if (!luaTruthy(c)) { + break; + } + try { + const r = evalStatement(w.block, env, sf, returnOnReturn); + if (isPromise(r)) { + const res = await r; + if (res !== undefined) { + return res; + } + } else if (r !== undefined) { + return r; + } + } catch (e: any) { + if (e instanceof LuaBreak) { + break; + } else { + throw e; + } + } + } + return; + }; + + while (true) { + const c = evalExpression(w.condition, env, sf); + if (isPromise(c)) { + return (c as Promise).then((cv) => { + if (!luaTruthy(cv)) { + return; + } + // Run body once for this already true condition, then + // continue async. + try { + const r = evalStatement(w.block, env, sf, returnOnReturn); + if (isPromise(r)) { + return (r as Promise).then((res) => { + if (res !== undefined) { + return res; + } + return runAsync(); + }).catch((e: any) => { + if (e instanceof LuaBreak) { + return; + } + throw e; + }); + } else if (r !== undefined) { + return r; + } + return runAsync(); + } catch (e: any) { + if (e instanceof LuaBreak) { + return; + } + throw e; + } + }); + } + if (!luaTruthy(c)) { + break; + } try { - await evalStatement(s.block, env, sf); + const r = evalStatement(w.block, env, sf, returnOnReturn); + if (isPromise(r)) { + return (r as Promise).then((res) => { + if (res !== undefined) { + return res; + } + return runAsync(); + }).catch((e: any) => { + if (e instanceof LuaBreak) { + return; + } + throw e; + }); + } else if (r !== undefined) { + return r; + } } catch (e: any) { if (e instanceof LuaBreak) { break; @@ -1087,12 +1326,55 @@ export async function evalStatement( } } } - break; + return; } case "Repeat": { - do { + const r = asRepeat(s); + const runAsync = async () => { + while (true) { + try { + const rr = evalStatement(r.block, env, sf, returnOnReturn); + if (isPromise(rr)) { + const res = await rr; + if (res !== undefined) { + return res; + } + } else if (rr !== undefined) { + return rr; + } + } catch (e: any) { + if (e instanceof LuaBreak) { + break; + } else { + throw e; + } + } + const c = await evalExpression(r.condition, env, sf); + if (luaTruthy(c)) { + break; + } + } + return; + }; + + while (true) { try { - await evalStatement(s.block, env, sf); + const rr = evalStatement(r.block, env, sf, returnOnReturn); + if (isPromise(rr)) { + return (rr as Promise).then((res) => { + if (res !== undefined) { + return res; + } + return runAsync(); + }).catch((e: any) => { + if (e instanceof LuaBreak) { + return; + } + throw e; + }); + } else if (rr !== undefined) { + return rr; + } } catch (e: any) { if (e instanceof LuaBreak) { break; @@ -1100,133 +1382,325 @@ export async function evalStatement( throw e; } } - } while (!luaTruthy(await evalExpression(s.condition, env, sf))); - break; + const c = evalExpression(r.condition, env, sf); + if (isPromise(c)) { + return (c as Promise).then(( + cv, + ) => (luaTruthy(cv) ? undefined : runAsync())); + } else { + if (luaTruthy(c)) { + break; + } + } + } + return; } case "Break": { throw new LuaBreak(); } case "FunctionCallStatement": { - await evalExpression(s.call, env, sf); + const fcs = asFunctionCallStmt(s); + const r = evalExpression(fcs.call, env, sf); + if (isPromise(r)) { + return (r as Promise).then(() => undefined); + } return; } case "Function": { - let body = s.body; - let propNames = s.name.propNames; - if (s.name.colonName) { + const fn = asFunctionStmt(s); + let body = fn.body; + let propNames = fn.name.propNames; + if (fn.name.colonName) { // function hello:there() -> function hello.there(self) transformation body = { - ...s.body, - parameters: ["self", ...s.body.parameters], + ...(fn.body), + parameters: ["self", ...fn.body.parameters], }; - propNames = [...s.name.propNames, s.name.colonName]; + propNames = [...fn.name.propNames, fn.name.colonName]; } let settable: ILuaSettable & ILuaGettable = env; for (let i = 0; i < propNames.length - 1; i++) { - settable = settable.get(propNames[i]); + settable = (settable as any).get(propNames[i]); if (!settable) { throw new LuaRuntimeError( `Cannot find property ${propNames[i]}`, - sf.withCtx(s.name.ctx), + sf.withCtx(fn.name.ctx), ); } } - settable.set( + (settable as any).set( propNames[propNames.length - 1], new LuaFunction(body, env), ); - break; + return; } case "LocalFunction": { + const lf = asLocalFunction(s); env.setLocal( - s.name, - new LuaFunction(s.body, env), + lf.name, + new LuaFunction(lf.body, env), ); - break; + return; } case "Return": { + const ret = asReturn(s); + // Sync-first collection of return expressions, no extra Promise + // if all are sync. + const parts = ret.expressions.map((value: LuaExpression) => + evalExpression(value, env, sf) + ); + const valuesRP = rpAll(parts); if (returnOnReturn) { - return await evalPromiseValues( - s.expressions.map((value) => evalExpression(value, env, sf)), - ); + return isPromise(valuesRP) ? valuesRP : valuesRP; } else { - throw new LuaReturn( - await evalPromiseValues( - s.expressions.map((value) => evalExpression(value, env, sf)), - ), - ); + if (isPromise(valuesRP)) { + return (valuesRP as Promise).then((vals) => { + throw new LuaReturn(vals); + }); + } else { + throw new LuaReturn(valuesRP as LuaValue[]); + } } } case "For": { - const start = await evalExpression(s.start, env, sf); - const end = await evalExpression(s.end, env, sf); - const step = s.step ? await evalExpression(s.step, env, sf) : 1; - for ( - let i = start; - step > 0 ? i <= end : i >= end; - i += step - ) { - const localEnv = new LuaEnv(env); - localEnv.setLocal(s.name, i); - try { - await evalStatement(s.block, localEnv, sf); - } catch (e: any) { - if (e instanceof LuaBreak) { - break; - } else { - throw e; + const fr = asFor(s); + // Evaluate bounds and step once (sync-first) + const startV = evalExpression(fr.start, env, sf); + const endV = evalExpression(fr.end, env, sf); + const stepV = fr.step ? evalExpression(fr.step, env, sf) : 1; + + const runAsync = async (start: any, end: any, step: any) => { + for ( + let i = start; + step > 0 ? i <= end : i >= end; + i += step + ) { + const localEnv = new LuaEnv(env); + localEnv.setLocal(fr.name, i); + try { + const r = evalStatement(fr.block, localEnv, sf, returnOnReturn); + if (isPromise(r)) { + const res = await r; + if (res !== undefined) { + return res; + } + } else if (r !== undefined) { + return r; + } + } catch (e: any) { + if (e instanceof LuaBreak) { + break; + } else { + throw e; + } } } + return; + }; + + const runSyncFirst = ( + start: any, + end: any, + step: any, + ): void | LuaValue[] | Promise => { + for ( + let i = start; + step > 0 ? i <= end : i >= end; + i += step + ) { + const localEnv = new LuaEnv(env); + localEnv.setLocal(fr.name, i); + try { + const r = evalStatement(fr.block, localEnv, sf, returnOnReturn); + if (isPromise(r)) { + return (r as Promise).then((res) => { + if (res !== undefined) { + return res; + } + return runAsync(i + step, end, step); + }).catch((e: any) => { + if (e instanceof LuaBreak) { + return; + } + throw e; + }); + } else if (r !== undefined) { + return r; + } + } catch (e: any) { + if (e instanceof LuaBreak) { + break; + } else { + throw e; + } + } + } + return; + }; + + if ( + !isPromise(startV) && + !isPromise(endV) && + !isPromise(stepV) + ) { + return runSyncFirst(startV, endV, (stepV as number) ?? 1); + } else { + return Promise.all([ + isPromise(startV) ? startV : Promise.resolve(startV), + isPromise(endV) ? endV : Promise.resolve(endV), + isPromise(stepV) ? stepV : Promise.resolve(stepV), + ]).then(([start, end, step]) => runSyncFirst(start, end, step ?? 1)); } - break; } case "ForIn": { - const iteratorMultiRes = new LuaMultiRes( - await evalPromiseValues( - s.expressions.map((e) => evalExpression(e, env, sf)), - ), - ).flatten(); - let iteratorValue: ILuaFunction | any = iteratorMultiRes.values[0]; - // Handle the case where the iterator is a table and we need to call the each function - if (Array.isArray(iteratorValue) || iteratorValue instanceof LuaTable) { - iteratorValue = env.get("each").call(sf, iteratorValue); - } + const fi = asForIn(s); + const exprVals = rpAll( + fi.expressions.map((e: LuaExpression) => evalExpression(e, env, sf)), + ); - if (!iteratorValue?.call) { - console.error("Cannot iterate over", iteratorMultiRes.values[0]); - throw new LuaRuntimeError( - `Cannot iterate over ${iteratorMultiRes.values[0]}`, - sf.withCtx(s.ctx), - ); - } - - const state: LuaValue = iteratorMultiRes.values[1] || null; - const control: LuaValue = iteratorMultiRes.values[2] || null; - - while (true) { - const iterResult = new LuaMultiRes( - await luaCall(iteratorValue, [state, control], s.ctx, sf), - ).flatten(); - if ( - iterResult.values[0] === null || iterResult.values[0] === undefined - ) { - break; + const afterExprs = (resolved: any[]) => { + const iteratorMultiRes = new LuaMultiRes(resolved).flatten(); + let iteratorValue: ILuaFunction | any = iteratorMultiRes.values[0]; + // Handle the case where the iterator is a table and we need + // to call the `each` function. + if (Array.isArray(iteratorValue) || iteratorValue instanceof LuaTable) { + iteratorValue = (env.get("each") as ILuaFunction).call( + sf, + iteratorValue, + ); } - const localEnv = new LuaEnv(env); - for (let i = 0; i < s.names.length; i++) { - localEnv.setLocal(s.names[i], iterResult.values[i]); + + if (!iteratorValue?.call) { + console.error("Cannot iterate over", iteratorMultiRes.values[0]); + throw new LuaRuntimeError( + `Cannot iterate over ${iteratorMultiRes.values[0]}`, + sf.withCtx(fi.ctx), + ); } - try { - await evalStatement(s.block, localEnv, sf); - } catch (e: any) { - if (e instanceof LuaBreak) { + + const state: LuaValue = iteratorMultiRes.values[1] || null; + const control: LuaValue = iteratorMultiRes.values[2] || null; + + const runAsync = async () => { + while (true) { + const callRes = luaCall( + iteratorValue, + [state, control], + fi.ctx, + sf, + ); + const iterResult = new LuaMultiRes( + isPromise(callRes) ? await callRes : callRes, + ).flatten(); + if ( + iterResult.values[0] === null || + iterResult.values[0] === undefined + ) { + break; + } + const localEnv = new LuaEnv(env); + for (let i = 0; i < fi.names.length; i++) { + localEnv.setLocal(fi.names[i], iterResult.values[i]); + } + try { + const r = evalStatement(fi.block, localEnv, sf, returnOnReturn); + if (isPromise(r)) { + const res = await r; + if (res !== undefined) { + return res; + } + } else if (r !== undefined) { + return r; + } + } catch (e: any) { + if (e instanceof LuaBreak) { + break; + } else { + throw e; + } + } + } + return; + }; + + while (true) { + const iterCall = luaCall(iteratorValue, [state, control], fi.ctx, sf); + if (isPromise(iterCall)) { + return (iterCall as Promise).then((itv) => { + const iterResult = new LuaMultiRes(itv).flatten(); + if ( + iterResult.values[0] === null || + iterResult.values[0] === undefined + ) { + return; + } + const localEnv = new LuaEnv(env); + for (let i = 0; i < fi.names.length; i++) { + localEnv.setLocal(fi.names[i], iterResult.values[i]); + } + const r = evalStatement(fi.block, localEnv, sf, returnOnReturn); + if (isPromise(r)) { + return (r as Promise).then((res) => { + if (res !== undefined) { + return res; + } + return runAsync(); + }).catch((e: any) => { + if (e instanceof LuaBreak) { + return; + } + throw e; + }); + } else if (r !== undefined) { + return r; + } else { + return runAsync(); + } + }); + } + const iterResult = new LuaMultiRes(iterCall).flatten(); + if ( + iterResult.values[0] === null || iterResult.values[0] === undefined + ) { break; - } else { - throw e; + } + const localEnv = new LuaEnv(env); + for (let i = 0; i < fi.names.length; i++) { + localEnv.setLocal(fi.names[i], iterResult.values[i]); + } + try { + const r = evalStatement(fi.block, localEnv, sf, returnOnReturn); + if (isPromise(r)) { + return (r as Promise).then((res) => { + if (res !== undefined) { + return res; + } + return runAsync(); + }).catch((e: any) => { + if (e instanceof LuaBreak) { + return; + } + throw e; + }); + } else if (r !== undefined) { + return r; + } + } catch (e: any) { + if (e instanceof LuaBreak) { + break; + } else { + throw e; + } } } + return; + }; + + if (isPromise(exprVals)) { + return (exprVals as Promise).then(afterExprs); + } else { + return afterExprs(exprVals as any[]); } - break; } } } @@ -1238,25 +1712,27 @@ function evalLValue( ): LuaLValueContainer | Promise { switch (lval.type) { case "Variable": { + const v = asLValueVariable(lval); return { env, - key: lval.name, + key: v.name, }; } case "TableAccess": { + const ta = asLValueTableAccess(lval); const objValue = evalExpression( - lval.object, + ta.object, env, sf, ); - const keyValue = evalExpression(lval.key, env, sf); + const keyValue = evalExpression(ta.key, env, sf); if ( - objValue instanceof Promise || - keyValue instanceof Promise + isPromise(objValue) || + isPromise(keyValue) ) { return Promise.all([ - objValue instanceof Promise ? objValue : Promise.resolve(objValue), - keyValue instanceof Promise ? keyValue : Promise.resolve(keyValue), + isPromise(objValue) ? objValue : Promise.resolve(objValue), + isPromise(keyValue) ? keyValue : Promise.resolve(keyValue), ]).then(([objValue, keyValue]) => ({ env: singleResult(objValue), key: singleResult(keyValue), @@ -1269,22 +1745,23 @@ function evalLValue( } } case "PropertyAccess": { + const pa = asLValuePropertyAccess(lval); const objValue = evalExpression( - lval.object, + pa.object, env, sf, ); - if (objValue instanceof Promise) { - return objValue.then((objValue) => { + if (isPromise(objValue)) { + return (objValue as Promise).then((ov) => { return { - env: objValue, - key: lval.property, + env: ov, + key: pa.property, }; }); } else { return { env: objValue, - key: lval.property, + key: pa.property, }; } } diff --git a/client/space_lua/query_collection.ts b/client/space_lua/query_collection.ts index 07e3284d..995ac826 100644 --- a/client/space_lua/query_collection.ts +++ b/client/space_lua/query_collection.ts @@ -19,7 +19,7 @@ export function buildItemEnv( if (!objectVariable) { // Inject all item keys as variables for (const key of luaKeys(item)) { - itemEnv.setLocal(key, luaGet(item, key, sf)); + itemEnv.setLocal(key, luaGet(item, key, sf.astCtx ?? null, sf)); } // As well as _ itemEnv.setLocal("_", item); diff --git a/client/space_lua/rp.bench.ts b/client/space_lua/rp.bench.ts new file mode 100644 index 00000000..f254a77d --- /dev/null +++ b/client/space_lua/rp.bench.ts @@ -0,0 +1,270 @@ +// Benchmark suite for Space Lua RP (Result-or-Promise) optimizations +// that exercises hot synchronous paths (binary ops, loops, function +// calls, argument lists, table get/set, concatenation). +// +// # NOTES +// +// * Parsing cost is excluded from measured time by compiling each +// snippet once. +// +// * Each bench creates a fresh Lua environment to isolate state. +// +// * Minimal global environment (`_GLOBAL`) is installed with included: +// +// * `string.format` (simple `%s` formatter) and +// * `type`. +// +// * To add benches that need more stdlib, extend makeEnv() accordingly. + +import { evalStatement } from "./eval.ts"; +import { + LuaBuiltinFunction, + LuaEnv, + LuaRuntimeError, + LuaStackFrame, + LuaTable, + luaTypeOf, +} from "./runtime.ts"; +import { parse as parseLua } from "./parse.ts"; + +const LOOP = 100000; +const SMALL = 20000; + +function makeEnv(): { global: LuaEnv; sf: LuaStackFrame } { + const global = new LuaEnv(); + + const stringLib = new LuaTable({ + format: (fmt: string, ...args: any[]) => { + let i = 0; + return String(fmt).replace(/%s/g, () => String(args[i++])); + }, + }); + + global.setLocal("string", stringLib); + + global.setLocal( + "type", + new LuaBuiltinFunction((_sf, v) => luaTypeOf(v)), + ); + + const sf = LuaStackFrame.createWithGlobalEnv(global); + + return { global, sf }; +} + +async function run(ast: any) { + const { global, sf } = makeEnv(); + try { + const r = evalStatement(ast, global, sf, false); + if (r instanceof Promise) { + await r; + } + } catch (e) { + if (e instanceof LuaRuntimeError) { + throw e; + } + throw new Error(`Lua execution error: ${e && (e as any).message || e}`); + } +} + +// Snippets + +const luaWhileSync = ` + local i = 0 + local s = 0 + while i < ${LOOP} do + i = i + 1 + s = s + i + end +`; + +const luaForNumeric = ` + local s = 0 + for i = 1, ${LOOP} do + s = s + i + end +`; + +const luaWhileFuncCondTruthy = ` + local n = 0 + local function next_or_nil() + n = n + 1 + if n <= 2 then + return 0 -- 0 is truthy; should loop exactly twice + end + return nil + end + local count = 0 + while next_or_nil() do + count = count + 1 + end +`; + +const luaFuncCallArgs = ` + local s = 0 + local function f(a, b, c) return a + b + c end + for i = 1, ${LOOP}, 3 do + s = s + f(i, i + 1, i + 2) + end +`; + +const luaTableGetSet = ` + local t = { a = 1 } + for i = 1, ${LOOP} do + t.a = t.a + 1 + end +`; + +const luaTableIndexNumeric = ` + local t = {} + for i = 1, ${LOOP} do + t[i] = i + end + local s = 0 + for i = 1, ${LOOP} do + s = s + t[i] + end +`; + +const luaConcatStrings = ` + local s = "" + for i = 1, ${SMALL} do + s = s .. "x" + end +`; + +const luaArithmeticBinary = ` + local a, b, c = 1, 2, 3 + local s = 0 + for i = 1, ${LOOP} do + s = s + (a * b) - (c / a) + (a ^ 2) % 5 + end +`; + +const luaWhileTruthinessMix = ` + local items = { 0, "", {}, 1, -0.0, "x" } + local idx = 0 + local function next_item() + idx = idx + 1 + local v = items[idx] + if v ~= nil then return v end + return nil + end + local cnt = 0 + while next_item() do + cnt = cnt + 1 + end +`; + +const luaTableDeepDotGetSet = ` + local t = { a = { b = { c = 1 } } } + for i = 1, ${LOOP} do + t.a.b.c = t.a.b.c + 1 + end +`; + +const luaTableDotMissRead = ` + local t = { a = 1 } + local s = 0 + for i = 1, ${LOOP} do + if t.m == nil then + s = s + 1 + end + end +`; + +// The truthiness_test.lua uses the `string.format`. +const truthinessPath = + new URL("./truthiness_test.lua", import.meta.url).pathname; +const truthinessCode = await Deno.readTextFile(truthinessPath); + +const astWhileSync = parseLua(luaWhileSync); +const astForNumeric = parseLua(luaForNumeric); +const astWhileFuncCondTruthy = parseLua(luaWhileFuncCondTruthy); +const astFuncCallArgs = parseLua(luaFuncCallArgs); +const astTableGetSet = parseLua(luaTableGetSet); +const astTableIndexNumeric = parseLua(luaTableIndexNumeric); +const astConcatStrings = parseLua(luaConcatStrings); +const astArithmeticBinary = parseLua(luaArithmeticBinary); +const astWhileTruthinessMix = parseLua(luaWhileTruthinessMix); +const astTableDeepDotGetSet = parseLua(luaTableDeepDotGetSet); +const astTableDotMissRead = parseLua(luaTableDotMissRead); +const astTruthiness = parseLua(truthinessCode); + +Deno.bench( + { name: "RP: while (sync cond) numeric sum", group: "space-lua" }, + async () => { + await run(astWhileSync); + }, +); + +Deno.bench({ name: "RP: for (numeric) sum", group: "space-lua" }, async () => { + await run(astForNumeric); +}); + +Deno.bench({ + name: "RP: while (function cond -> truthy then nil)", + group: "space-lua", +}, async () => { + await run(astWhileFuncCondTruthy); +}); + +Deno.bench( + { name: "RP: function calls + arg eval", group: "space-lua" }, + async () => { + await run(astFuncCallArgs); + }, +); + +Deno.bench({ name: "RP: table dot get/set", group: "space-lua" }, async () => { + await run(astTableGetSet); +}); + +Deno.bench( + { name: "RP: table numeric index get/set", group: "space-lua" }, + async () => { + await run(astTableIndexNumeric); + }, +); + +Deno.bench( + { name: "RP: table deep dot get/set (3 levels)", group: "space-lua" }, + async () => { + await run(astTableDeepDotGetSet); + }, +); + +Deno.bench( + { name: "RP: table dot miss (nil reads, no metatable)", group: "space-lua" }, + async () => { + await run(astTableDotMissRead); + }, +); + +Deno.bench( + { name: "RP: string concatenation (..)", group: "space-lua" }, + async () => { + await run(astConcatStrings); + }, +); + +Deno.bench( + { name: "RP: arithmetic (binary ops)", group: "space-lua" }, + async () => { + await run(astArithmeticBinary); + }, +); + +Deno.bench({ + name: "RP: while truthiness mix (0,'' ,{},...)", + group: "space-lua", +}, async () => { + await run(astWhileTruthinessMix); +}); + +Deno.bench( + { name: "RP: truthiness_test.lua (end-to-end)", group: "space-lua" }, + async () => { + await run(astTruthiness); + }, +); diff --git a/client/space_lua/rp.ts b/client/space_lua/rp.ts new file mode 100644 index 00000000..a6ed1dc6 --- /dev/null +++ b/client/space_lua/rp.ts @@ -0,0 +1,38 @@ +// Result-or-Promise helpers + +export type RP = T | Promise; + +// Returns true when v is a Promise or a has a then function. +export function isPromise(v: RP): v is Promise { + return v !== null && typeof v === "object" && + typeof (v as any).then === "function"; +} + +export function rpThen(v: RP, f: (a: A) => RP): RP { + return isPromise(v) ? (v as Promise).then(f) : f(v as A); +} + +/** + * Collect an array of Result-or-Promise values into a single array + * avoiding Promise allocation when all inputs are synchronous. + */ +export function rpAll(arr: RP[]): RP { + let hasPromise = false; + for (let i = 0; i < arr.length; i++) { + if (isPromise(arr[i])) { + hasPromise = true; + break; + } + } + if (!hasPromise) { + // All sync values: return as-is + return arr as T[]; + } + + // At least one Promise: allocate a new array + const out = new Array>(arr.length); + for (let i = 0; i < arr.length; i++) { + out[i] = arr[i] as any; + } + return Promise.all(out as Promise[]); +} diff --git a/client/space_lua/runtime.ts b/client/space_lua/runtime.ts index c5749cea..e2d5e53a 100644 --- a/client/space_lua/runtime.ts +++ b/client/space_lua/runtime.ts @@ -1,6 +1,7 @@ import type { ASTCtx, LuaFunctionBody } from "./ast.ts"; -import { evalStatement, getMetatable } from "./eval.ts"; -import { asyncQuickSort, evalPromiseValues } from "./util.ts"; +import { evalStatement } from "./eval.ts"; +import { asyncQuickSort } from "./util.ts"; +import { isPromise, rpAll } from "./rp.ts"; export type LuaType = | "nil" @@ -30,6 +31,32 @@ export interface ILuaGettable { get(key: LuaValue, sf?: LuaStackFrame): LuaValue | undefined; } +// Small helpers for type safety/readability +export function isILuaFunction(v: unknown): v is ILuaFunction { + return !!v && typeof (v as any).call === "function"; +} + +export function isLuaTable(v: unknown): v is LuaTable { + return v instanceof LuaTable; +} + +export function toNumKey(key: unknown): string | number { + if (key instanceof Number) { + return Number(key); + } + if (typeof key === "number" || typeof key === "string") { + return key; + } + return key as unknown as string | number; +} + +export function ctxOrNull(sf?: LuaStackFrame): ASTCtx | null { + return sf?.astCtx ?? null; +} + +// Reuse a single empty context to avoid allocating `{}` in hot paths +const EMPTY_CTX = {} as ASTCtx; + export class LuaEnv implements ILuaSettable, ILuaGettable { variables = new Map(); @@ -165,7 +192,7 @@ export class LuaFunction implements ILuaFunction { this.capturedEnv = closure; } - async call(sf: LuaStackFrame, ...args: LuaValue[]): Promise { + call(sf: LuaStackFrame, ...args: LuaValue[]): Promise | LuaValue { // Create a new environment that chains to the captured environment const env = new LuaEnv(this.capturedEnv); if (!sf) { @@ -174,25 +201,54 @@ export class LuaFunction implements ILuaFunction { // Set _CTX to the thread local environment from the stack frame env.setLocal("_CTX", sf.threadLocal); - // Evaluate all arguments - const resolvedArgs = await evalPromiseValues(args); - - // Assign parameter values to variable names in env - let varargs: LuaValue[] = []; - for (let i = 0; i < this.body.parameters.length; i++) { - const paramName = this.body.parameters[i]; - if (paramName === "...") { - // Vararg parameter, let's collect the remainder of the resolved args into the varargs array - varargs = resolvedArgs.slice(i); - // Done, break out of this loop - break; + // Resolve args (sync-first) + const argsRP = rpAll(args as any[]); + const resolveArgs = (resolvedArgs: any[]) => { + // Assign parameter values to variable names in env + let varargs: LuaValue[] = []; + for (let i = 0; i < this.body.parameters.length; i++) { + const paramName = this.body.parameters[i]; + if (paramName === "...") { + // Vararg parameter, let's collect the remainder of the resolved args into the varargs array + varargs = resolvedArgs.slice(i); + // Done, break out of this loop + break; + } + env.setLocal(paramName, resolvedArgs[i] ?? null); } - env.setLocal(paramName, resolvedArgs[i] ?? null); + env.setLocal("...", new LuaMultiRes(varargs)); + + // Evaluate the function body with returnOnReturn set to true + try { + const r = evalStatement(this.body.block, env, sf, true); + const map = (val: any) => { + if (val !== undefined) { + return mapFunctionReturnValue(val); + } + }; + if (isPromise(r)) { + return r.then(map).catch((e: any) => { + if (e instanceof LuaReturn) { + return mapFunctionReturnValue(e.values); + } + throw e; + }); + } else { + return map(r); + } + } catch (e: any) { + if (e instanceof LuaReturn) { + return mapFunctionReturnValue(e.values); + } + throw e; + } + }; + + if (isPromise(argsRP)) { + return argsRP.then(resolveArgs); + } else { + return resolveArgs(argsRP); } - - env.setLocal("...", new LuaMultiRes(varargs)); - - return this.evalBody(env, sf); } asString(): string { @@ -202,25 +258,6 @@ export class LuaFunction implements ILuaFunction { toString(): string { return this.asString(); } - - private async evalBody( - env: LuaEnv, - sf: LuaStackFrame, - ): Promise { - try { - // Evaluate the function body with returnOnReturn set to true - const result = await evalStatement(this.body.block, env, sf, true); - if (result !== undefined) { - return mapFunctionReturnValue(result); - } - } catch (e: any) { - if (e instanceof LuaReturn) { - return mapFunctionReturnValue(e.values); - } else { - throw e; - } - } - } } function mapFunctionReturnValue(values: any[]): any { @@ -239,13 +276,12 @@ export class LuaNativeJSFunction implements ILuaFunction { // Performs automatic conversion between Lua and JS values for arguments, but not for return values call(sf: LuaStackFrame, ...args: LuaValue[]): Promise | LuaValue { - const evaluatedArgs = evalPromiseValues( - args.map((v) => luaValueToJS(v, sf)), - ); - if (evaluatedArgs instanceof Promise) { - return evaluatedArgs.then((args) => this.fn(...args)); + const jsArgsRP = args.map((v) => luaValueToJS(v, sf)); + const resolved = rpAll(jsArgsRP); + if (isPromise(resolved)) { + return resolved.then((jsArgs) => this.fn(...jsArgs)); } else { - return this.fn(...evaluatedArgs); + return this.fn(...resolved); } } @@ -281,6 +317,7 @@ export class LuaBuiltinFunction implements ILuaFunction { export class LuaTable implements ILuaSettable, ILuaGettable { // To optimize the table implementation we use a combination of different data structures public metatable: LuaTable | null; + // When tables are used as maps, the common case is that they are string keys, so we use a simple object for that private stringKeys: Record; // Other keys we can support using a Map as a fallback @@ -288,10 +325,23 @@ export class LuaTable implements ILuaSettable, ILuaGettable { // When tables are used as arrays, we use a native JavaScript array for that private arrayPart: any[]; + // Metamethod presence flags + hasIndexMM = false; + hasNewIndexMM = false; + hasLenMM = false; + constructor(init?: any[] | Record) { // For efficiency and performance reasons we pre-allocate these (modern JS engines are very good at optimizing this) this.arrayPart = Array.isArray(init) ? init : []; this.stringKeys = init && !Array.isArray(init) ? init : {}; + + if (init && !Array.isArray(init)) { + for (const k in init) { + if (Object.prototype.hasOwnProperty.call(init, k)) { + this.stringKeys[k] = (init as any)[k]; + } + } + } this.otherKeys = null; // Only create this when needed this.metatable = null; } @@ -308,16 +358,13 @@ export class LuaTable implements ILuaSettable, ILuaGettable { return this.arrayPart.length; } - empty(): boolean { - return ( - Object.keys(this.stringKeys).length === 0 && - this.arrayPart.length === 0 && - (this.otherKeys === null || this.otherKeys.size === 0) - ); - } - keys(): any[] { - const keys: any[] = Object.keys(this.stringKeys); + const keys: any[] = []; + for (const k in this.stringKeys) { + if (Object.prototype.hasOwnProperty.call(this.stringKeys, k)) { + keys.push(k); + } + } for (let i = 0; i < this.arrayPart.length; i++) { keys.push(i + 1); } @@ -329,6 +376,21 @@ export class LuaTable implements ILuaSettable, ILuaGettable { return keys; } + empty(): boolean { + for (const k in this.stringKeys) { + if (Object.prototype.hasOwnProperty.call(this.stringKeys, k)) { + return false; + } + } + if (this.arrayPart.length !== 0) { + return false; + } + if (this.otherKeys && this.otherKeys.size !== 0) { + return false; + } + return true; + } + has(key: LuaValue) { if (typeof key === "string") { return this.stringKeys[key] !== undefined; @@ -341,11 +403,11 @@ export class LuaTable implements ILuaSettable, ILuaGettable { } rawSet(key: LuaValue, value: LuaValue): void | Promise { - if (key instanceof Promise) { + if (isPromise(key)) { return key.then((key) => this.rawSet(key, value)); } - if (value instanceof Promise) { - return value.then(() => this.rawSet(key, value)); + if (isPromise(value)) { + return value.then((v) => this.rawSet(key, v)); } if (typeof key === "string") { if (value === null || value === undefined) { @@ -353,17 +415,19 @@ export class LuaTable implements ILuaSettable, ILuaGettable { } else { this.stringKeys[key] = value; } - } else if (LuaTable.isIntegerKey(key)) { + return; + } + if (LuaTable.isIntegerKey(key)) { this.arrayPart[LuaTable.toIndex(key)] = value; + return; + } + if (!this.otherKeys) { + this.otherKeys = new Map(); + } + if (value === null || value === undefined) { + this.otherKeys.delete(key); } else { - if (!this.otherKeys) { - this.otherKeys = new Map(); - } - if (value === null || value === undefined) { - this.otherKeys.delete(key); - } else { - this.otherKeys.set(key, value); - } + this.otherKeys.set(key, value); } } @@ -372,17 +436,36 @@ export class LuaTable implements ILuaSettable, ILuaGettable { value: LuaValue, sf?: LuaStackFrame, ): Promise | void { - const metatable = getMetatable(this, sf); - if (metatable && metatable.has("__newindex") && !this.has(key)) { - // Invoke the meta table! - const metaValue = metatable.get("__newindex", sf); - if (metaValue.then) { + const mt = this.metatable; + if (!mt) { + return this.rawSet(key, value); + } + // Refresh flags when used (fix: treat both undefined and null as absent) + const newIndexMM = mt.rawGet("__newindex"); + this.hasNewIndexMM = !(newIndexMM === undefined || newIndexMM === null); + + if (this.hasNewIndexMM && !this.has(key)) { + // Invoke the meta table + const metaValue = mt.get("__newindex", sf); + // Ensure we pass a non-null ASTCtx to luaCall + const callCtx: ASTCtx = sf?.astCtx ?? EMPTY_CTX; + if (isPromise(metaValue)) { // This is a promise, we need to wait for it - return metaValue.then((metaValue: any) => { - return luaCall(metaValue, [this, key, value], metaValue.ctx, sf); + return (metaValue as Promise).then((mv: any) => { + return luaCall( + mv, + [this, key, value], + callCtx, + sf, + ); }); } else { - return luaCall(metaValue, [this, key, value], metaValue.ctx, sf); + return luaCall( + metaValue, + [this, key, value], + callCtx, + sf, + ); } } @@ -493,42 +576,53 @@ export function luaIndexValue( if (rawValue !== undefined) { return rawValue; } + // Skip metatable lookup fast path + if (value.metatable === null) { + return null; + } } // If not, let's see if the value has a metatable and if it has a __index metamethod const metatable = getMetatable(value, sf); if (metatable && metatable.has("__index")) { // Invoke the meta table const metaValue = metatable.get("__index", sf); - if (metaValue.then) { + if (isPromise(metaValue)) { // Got a promise, we need to wait for it - return metaValue.then((metaValue: any) => { - if (metaValue.call) { - return metaValue.call(sf, value, key); - } else if (metaValue instanceof LuaTable) { - return metaValue.get(key, sf); + return (metaValue as Promise).then((mv: any) => { + if (mv?.call) { + return luaCall(mv, [value, key], sf?.astCtx ?? EMPTY_CTX, sf); + } else if (mv instanceof LuaTable) { + return mv.get(key, sf); } else { - throw new Error("Meta table __index must be a function or table"); + throw new LuaRuntimeError( + "Meta table __index must be a function or table", + sf || LuaStackFrame.lostFrame, + ); } }); } else { - if (metaValue.call) { - return metaValue.call(sf, value, key); - } else if (metaValue instanceof LuaTable) { - return metaValue.get(key, sf); + const mv = metaValue as any; + if (mv?.call) { + return luaCall(mv, [value, key], sf?.astCtx ?? EMPTY_CTX, sf); + } else if (mv instanceof LuaTable) { + return mv.get(key, sf); } else { - throw new Error("Meta table __index must be a function or table"); + throw new LuaRuntimeError( + "Meta table __index must be a function or table", + sf || LuaStackFrame.lostFrame, + ); } } } // If not, perhaps let's assume this is a plain JavaScript object and we just index into it - const idxKey = key instanceof Number ? Number(key) : key; + const k = toNumKey(key); if ( typeof value === "object" && Array.isArray(value) && - typeof idxKey === "number" + typeof k === "number" ) { - return (value as any)[idxKey - 1]; + return (value as any[])[k - 1]; } - const objValue = (value as any)[idxKey]; + const objValue = (value as Record)[k]; if (objValue === undefined || objValue === null) { return null; } else { @@ -554,26 +648,29 @@ export async function luaSet( if (obj instanceof LuaTable || obj instanceof LuaEnv) { await obj.set(key, value, sf); } else { - const k = key instanceof Number ? Number(key) : key; - obj[k] = value; + const k = toNumKey(key); + (obj as Record)[k] = value; } } export function luaGet( obj: any, key: any, + ctx: ASTCtx | null, sf: LuaStackFrame, ): Promise | any { + const errSf = ctx ? sf.withCtx(ctx) : sf; + if (obj === null || obj === undefined) { throw new LuaRuntimeError( - `Attempting to index a nil value`, - sf, + `attempt to index a nil value`, + errSf, ); } if (key === null || key === undefined) { throw new LuaRuntimeError( - `Attempting to index with a nil key`, - sf, + `attempt to index with a nil key`, + errSf, ); } @@ -581,11 +678,11 @@ export function luaGet( return obj.get(key, sf); } else if (typeof key === "number" || key instanceof Number) { const idx = key instanceof Number ? Number(key) : key; - return obj[idx - 1]; + return (obj as any[])[idx - 1]; } else { // Native JS object - const k = key instanceof Number ? Number(key) : key; - const val = obj[k]; + const k = toNumKey(key); + const val = (obj as Record)[k]; if (typeof val === "function") { // Automatically bind the function to the object return val.bind(obj); @@ -623,20 +720,22 @@ export function luaCall( ): any { if (!callee) { throw new LuaRuntimeError( - `Attempting to call a nil value`, + `attempt to call a nil value`, (sf || LuaStackFrame.lostFrame).withCtx(ctx), ); } if (typeof callee === "function") { - const jsArgs = evalPromiseValues( + const jsArgs = rpAll( args.map((v) => luaValueToJS(v, sf || LuaStackFrame.lostFrame)), ); - if (jsArgs instanceof Promise) { - return jsArgs.then((jsArgs) => callee(...jsArgs)); + if (isPromise(jsArgs)) { + return jsArgs.then((resolved) => + (callee as (...a: any[]) => any)(...resolved) + ); } else { - return callee(...jsArgs); + return (callee as (...a: any[]) => any)(...jsArgs); } } else if (callee instanceof LuaTable) { const metatable = getMetatable(callee, sf); @@ -646,27 +745,33 @@ export function luaCall( const metaValue = metatable.get("__call", sf); const callMetaValue = (value: any) => { - if (metaValue.call) { + if (value && value.call) { return luaCall(value, [callee, ...args], ctx, sf); } else { - throw new Error("Meta table __call must be a function"); + throw new LuaRuntimeError( + "Meta table __call must be a function", + (sf || LuaStackFrame.lostFrame).withCtx(ctx), + ); } }; - if (metaValue.then) { + if (isPromise(metaValue)) { // Got a promise, we need to wait for it - return metaValue.then((value: any) => { - callMetaValue(value); + return (metaValue as Promise).then((value: any) => { + return callMetaValue(value); }); } else { return callMetaValue(metaValue); } } - } else if (callee.call) { - return callee.call((sf || LuaStackFrame.lostFrame).withCtx(ctx), ...args); + } else if (isILuaFunction(callee)) { + return callee.call( + (sf || LuaStackFrame.lostFrame).withCtx(ctx), + ...args, + ); } else { throw new LuaRuntimeError( - `Attempting to call a non-callable value of type: ${luaTypeOf(callee)}`, + `attempt to call a non-callable value of type: ${luaTypeOf(callee)}`, (sf || LuaStackFrame.lostFrame).withCtx(ctx), ); } @@ -695,8 +800,8 @@ export function luaTypeOf(val: any): LuaType | Promise { if (val === null || val === undefined) { return "nil"; } - if (val instanceof Promise) { - return val.then((v) => luaTypeOf(v)); + if (isPromise(val)) { + return (val as Promise).then((v) => luaTypeOf(v)); } else if (typeof val === "boolean") { return "boolean"; } else if (typeof val === "number" || val instanceof Number) { @@ -707,9 +812,9 @@ export function luaTypeOf(val: any): LuaType | Promise { return "table"; } else if (Array.isArray(val)) { return "table"; - } else if (typeof val === "function" || val.call) { + } else if (typeof val === "function" || isILuaFunction(val)) { return "function"; - } else if (typeof val === "object" && val.constructor === Object) { + } else if (typeof val === "object" && (val as any).constructor === Object) { return "table"; } else { return "userdata"; @@ -790,7 +895,7 @@ export function luaTruthy(value: any): boolean { return false; } - if (value instanceof LuaMultiRes) { + if (typeof value === "object" && value instanceof LuaMultiRes) { // for multi-return values, only the first result determines truthiness const first = value.unwrap(); return !(first === null || first === undefined || first === false); @@ -807,21 +912,21 @@ export function luaToString( if (value === null || value === undefined) { return "nil"; } - if (value instanceof Promise) { - return value.then((v) => luaToString(v, visited)); + if (isPromise(value)) { + return (value as Promise).then((v) => luaToString(v, visited)); } // Check for circular references if (typeof value === "object" && visited.has(value)) { return ""; } - if (value.toStringAsync) { + if ((value as any).toStringAsync) { // Add to visited before recursing visited.add(value); - return value.toStringAsync(); + return (value as any).toStringAsync(); } - if (value.asString) { + if ((value as any).asString) { visited.add(value); - return value.asString(); + return (value as any).asString(); } if (value instanceof LuaFunction) { // Don't recurse into the function body, just show the function signature @@ -873,9 +978,44 @@ export function luaToString( return String(value); } +export function getMetatable( + value: LuaValue, + sf?: LuaStackFrame, +): LuaValue | null { + if (value === null || value === undefined) { + return null; + } + if (typeof value === "string") { + // Prefer per-thread cached string metatable if `_GLOBAL` available + const thread = sf?.threadLocal; + const globalEnv = thread?.get("_GLOBAL") as LuaEnv | null | undefined; + + if (thread && globalEnv instanceof LuaEnv) { + const cached = thread.get("_STRING_MT") as LuaTable | undefined; + if (cached instanceof LuaTable) { + return cached; + } + + const stringMetatable = new LuaTable(); + stringMetatable.set("__index", (globalEnv as any).get("string")); + thread.setLocal("_STRING_MT", stringMetatable); + + return stringMetatable; + } + + return null; + } + + if ((value as any).metatable) { + return (value as any).metatable; + } else { + return null; + } +} + export function jsToLuaValue(value: any): any { - if (value instanceof Promise) { - return value.then(jsToLuaValue); + if (isPromise(value)) { + return (value as Promise).then(jsToLuaValue); } if (value instanceof LuaTable) { return value; @@ -901,7 +1041,7 @@ export function jsToLuaValue(value: any): any { } else if (typeof value === "object") { const table = new LuaTable(); for (const key in value) { - table.set(key, jsToLuaValue(value[key])); + table.set(key, jsToLuaValue((value as any)[key])); } return table; } else if (typeof value === "function") { @@ -913,8 +1053,8 @@ export function jsToLuaValue(value: any): any { // Inverse of jsToLuaValue export function luaValueToJS(value: any, sf: LuaStackFrame): any { - if (value instanceof Promise) { - return value.then((v) => luaValueToJS(v, sf)); + if (isPromise(value)) { + return (value as Promise).then((v) => luaValueToJS(v, sf)); } if (value instanceof LuaTable) { return value.toJS(sf); @@ -923,16 +1063,16 @@ export function luaValueToJS(value: any, sf: LuaStackFrame): any { value instanceof LuaBuiltinFunction ) { return (...args: any[]) => { - const jsArgs = evalPromiseValues( + const jsArgs = rpAll( args.map((v) => luaValueToJS(v, sf)), ); - if (jsArgs instanceof Promise) { + if (isPromise(jsArgs)) { return luaValueToJS( - jsArgs.then((jsArgs) => value.call(sf, ...jsArgs)), + jsArgs.then((jsArgs) => (value as ILuaFunction).call(sf, ...jsArgs)), sf, ); } else { - return luaValueToJS(value.call(sf, ...jsArgs), sf); + return luaValueToJS((value as ILuaFunction).call(sf, ...jsArgs), sf); } }; } else if (value instanceof Number) { diff --git a/client/space_lua/stdlib.ts b/client/space_lua/stdlib.ts index 24ae9208..dc80c79f 100644 --- a/client/space_lua/stdlib.ts +++ b/client/space_lua/stdlib.ts @@ -1,5 +1,6 @@ import { type ILuaFunction, + isLuaTable, LuaBuiltinFunction, luaCall, LuaEnv, @@ -48,7 +49,10 @@ const ipairsFunction = new LuaBuiltinFunction((sf, ar: LuaTable | any[]) => { if (i > ar.length) { return; } - const result = new LuaMultiRes([i, await luaGet(ar, i, sf)]); + const result = new LuaMultiRes([ + i, + await luaGet(ar, i, sf.astCtx ?? null, sf), + ]); i++; return result; }; @@ -59,7 +63,7 @@ const pairsFunction = new LuaBuiltinFunction( let keys: (string | number)[]; if (Array.isArray(t)) { keys = Array.from({ length: t.length }, (_, i) => i + 1); // For arrays, generate 1-based indices - } else if (t.keys) { + } else if (isLuaTable(t)) { keys = t.keys(); } else { // For plain JavaScript objects case, note: this will also include keys from the prototype @@ -76,7 +80,7 @@ const pairsFunction = new LuaBuiltinFunction( } const key = keys[i]; i++; - const value = await luaGet(t, key, sf); + const value = await luaGet(t, key, sf.astCtx ?? null, sf); return new LuaMultiRes([key, value]); }; }, @@ -85,12 +89,12 @@ const pairsFunction = new LuaBuiltinFunction( export const eachFunction = new LuaBuiltinFunction( (sf, ar: LuaTable | any[]) => { let i = 1; - const length = ar.length; + const length = (ar as any).length; return async () => { if (i > length) { return; } - const result = await luaGet(ar, i, sf); + const result = await luaGet(ar, i, sf.astCtx ?? null, sf); i++; return result; }; @@ -99,8 +103,8 @@ export const eachFunction = new LuaBuiltinFunction( const unpackFunction = new LuaBuiltinFunction(async (sf, t: LuaTable) => { const values: LuaValue[] = []; - for (let i = 1; i <= t.length; i++) { - values.push(await luaGet(t, i, sf)); + for (let i = 1; i <= (t as any).length; i++) { + values.push(await luaGet(t, i, sf.astCtx ?? null, sf)); } return new LuaMultiRes(values); }); @@ -170,23 +174,19 @@ const rawlenFunction = new LuaBuiltinFunction( const rawsetFunction = new LuaBuiltinFunction( (_sf, table: LuaTable, key: LuaValue, value: LuaValue) => { - return table.rawSet(key, value); + return (table as any).rawSet(key, value); }, ); const rawgetFunction = new LuaBuiltinFunction( (_sf, table: any, key: LuaValue) => { - const isLuaTable = typeof table === "object" && - table !== null && - typeof (table as any).rawGet === "function"; - const isArray = Array.isArray(table); const isPlainObj = typeof table === "object" && table !== null && - table.constructor === Object; + (table as any).constructor === Object; - if (!isLuaTable && !isArray && !isPlainObj) { + if (!isLuaTable(table) && !isArray && !isPlainObj) { let typeName = "userdata"; if (table === null || table === undefined) { typeName = "nil"; @@ -210,8 +210,8 @@ const rawgetFunction = new LuaBuiltinFunction( ); } - if (isLuaTable) { - const v = (table as any).rawGet(key); + if (isLuaTable(table)) { + const v = table.rawGet(key); return v === undefined ? null : v; } @@ -219,15 +219,15 @@ const rawgetFunction = new LuaBuiltinFunction( if (isArray) { if (typeof k === "number") { - const v = (table as any)[k - 1]; + const v = (table as any[])[k - 1]; return v === undefined ? null : v; } else { - const v = (table as any)[k]; + const v = (table as Record)[k]; return v === undefined ? null : v; } } - const v = (table as any)[k]; + const v = (table as Record)[k as any]; return v === undefined ? null : v; }, ); @@ -241,13 +241,13 @@ const rawequalFunction = new LuaBuiltinFunction( ); const getmetatableFunction = new LuaBuiltinFunction((_sf, table: LuaTable) => { - return table.metatable; + return (table as any).metatable; }); const dofileFunction = new LuaBuiltinFunction(async (sf, filename: string) => { - const global = sf.threadLocal.get("_GLOBAL"); + const global = sf.threadLocal.get("_GLOBAL") as LuaEnv; const file = await luaCall( - global.get("space").get("readFile"), + (global.get("space") as any).get("readFile"), [filename], sf.astCtx!, sf, @@ -267,9 +267,11 @@ 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. + * + * 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[]) => { @@ -287,11 +289,23 @@ const selectFunction = new LuaBuiltinFunction( /** * From the Lua docs: - * Allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index in this table. A call to next returns the next index of the table and its associated value. When called with nil as its second argument, next returns an initial index and its associated value. When called with the last index, or with nil in an empty table, next returns nil. If the second argument is absent, then it is interpreted as nil. In particular, you can use next(t) to check whether a table is empty. * - * The order in which the indices are enumerated is not specified, even for numeric indices. (To traverse a table in numerical order, use a numerical for.) + * Allows a program to traverse all fields of a table. Its first + * argument is a table and its second argument is an index in this + * table. A call to next returns the next index of the table and its + * associated value. When called with nil as its second argument, next + * returns an initial index and its associated value. When called with + * the last index, or with nil in an empty table, next returns nil. If + * the second argument is absent, then it is interpreted as nil. In + * particular, you can use next(t) to check whether a table is empty. * - * You should not assign any value to a non-existent field in a table during its traversal. You may however modify existing fields. In particular, you may set existing fields to nil. + * The order in which the indices are enumerated is not specified, even + * for numeric indices. (To traverse a table in numerical order, use + * a numerical for.) + * + * You should not assign any value to a non-existent field in a table + * during its traversal. You may however modify existing fields. In + * particular, you may set existing fields to nil. */ const nextFunction = new LuaBuiltinFunction( (sf, table: LuaTable | Record, index: number | null = null) => { @@ -309,7 +323,7 @@ const nextFunction = new LuaBuiltinFunction( if (index === null) { // Return the first key, value const key = keys[0]; - return new LuaMultiRes([key, luaGet(table, key, sf)]); + return new LuaMultiRes([key, luaGet(table, key, sf.astCtx ?? null, sf)]); } else { // Find index in the key list const idx = keys.indexOf(index); @@ -321,7 +335,7 @@ const nextFunction = new LuaBuiltinFunction( // When called with the last key, should return nil return null; } - return new LuaMultiRes([key, luaGet(table, key, sf)]); + return new LuaMultiRes([key, luaGet(table, key, sf.astCtx ?? null, sf)]); } }, ); diff --git a/client/space_lua/stdlib/table.ts b/client/space_lua/stdlib/table.ts index 029d54ae..27085d6a 100644 --- a/client/space_lua/stdlib/table.ts +++ b/client/space_lua/stdlib/table.ts @@ -185,7 +185,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); + const val = await luaGet(tbl, i, sf.astCtx ?? null, sf); if (await luaCall(criteriaFn, [val], sf.astCtx!, sf)) { return new LuaMultiRes([i, val]); }