Lua runtime fixes

main
Zef Hemel 2025-01-09 10:27:41 +01:00
parent 821dddff5e
commit b6f1977cec
3 changed files with 72 additions and 63 deletions

View File

@ -160,7 +160,8 @@ Deno.test("Test directive parser", () => {
});
Deno.test("Test lua directive parser", () => {
const simpleExample = `Simple \${{a=}}`;
const simpleExample = `Simple \${query_coll("page limit 3", template[==[
* Hello there {name}]==])}`;
console.log(JSON.stringify(parseMarkdown(simpleExample), null, 2));
});

View File

@ -312,6 +312,7 @@ function evalPrefixExpression(
);
}
const handleFunctionCall = (prefixValue: LuaValue) => {
// Special handling for f(...) - propagate varargs
if (
e.args.length === 1 && e.args[0].type === "Variable" &&
@ -370,6 +371,12 @@ function evalPrefixExpression(
} else {
return luaCall(prefixValue, [...selfArgs, ...args], e.ctx, sf);
}
};
if (prefixValue instanceof Promise) {
return prefixValue.then(handleFunctionCall);
} else {
return handleFunctionCall(prefixValue);
}
}
default:
throw new Error(`Unknown prefix expression type ${e.type}`);

View File

@ -3,9 +3,10 @@ import {
LuaBuiltinFunction,
luaCall,
LuaEnv,
luaGet,
LuaMultiRes,
LuaRuntimeError,
type LuaTable,
LuaTable,
luaToString,
luaTypeOf,
type LuaValue,
@ -15,8 +16,8 @@ import { tableApi } from "$common/space_lua/stdlib/table.ts";
import { osApi } from "$common/space_lua/stdlib/os.ts";
import { jsApi } from "$common/space_lua/stdlib/js.ts";
const printFunction = new LuaBuiltinFunction((_sf, ...args) => {
console.log("[Lua]", ...args.map(luaToString));
const printFunction = new LuaBuiltinFunction(async (_sf, ...args) => {
console.log("[Lua]", ...(await Promise.all(args.map(luaToString))));
});
const assertFunction = new LuaBuiltinFunction(
@ -27,35 +28,35 @@ const assertFunction = new LuaBuiltinFunction(
},
);
const ipairsFunction = new LuaBuiltinFunction((_sf, ar: LuaTable) => {
const ipairsFunction = new LuaBuiltinFunction((sf, ar: LuaTable) => {
let i = 1;
return () => {
return async () => {
if (i > ar.length) {
return;
}
const result = new LuaMultiRes([i, ar.get(i)]);
const result = new LuaMultiRes([i, await luaGet(ar, i, sf)]);
i++;
return result;
};
});
const pairsFunction = new LuaBuiltinFunction((_sf, t: LuaTable) => {
const pairsFunction = new LuaBuiltinFunction((sf, t: LuaTable) => {
const keys = t.keys();
let i = 0;
return () => {
return async () => {
if (i >= keys.length) {
return;
}
const key = keys[i];
i++;
return new LuaMultiRes([key, t.get(key)]);
return new LuaMultiRes([key, await luaGet(t, key, sf)]);
};
});
const unpackFunction = new LuaBuiltinFunction((_sf, t: LuaTable) => {
const unpackFunction = new LuaBuiltinFunction(async (sf, t: LuaTable) => {
const values: LuaValue[] = [];
for (let i = 1; i <= t.length; i++) {
values.push(t.get(i));
values.push(await luaGet(t, i, sf));
}
return new LuaMultiRes(values);
});