Fixes to template Lua expansion

pull/1232/head
Zef Hemel 2025-02-06 12:21:21 +01:00
parent e1c997616c
commit eb788c68ca
5 changed files with 18 additions and 16 deletions

View File

@ -57,7 +57,7 @@ export async function buildThreadLocalEnv(
}
}
tl.setLocal("_GLOBAL", globalEnv);
return Promise.resolve(tl);
return tl;
}
export async function handleLuaError(e: LuaRuntimeError, system: System<any>) {

View File

@ -3,6 +3,7 @@ import { evalExpression } from "$common/space_lua/eval.ts";
import { parse, parseExpressionString } from "../space_lua/parse.ts";
import type { CommonSystem } from "$common/common_system.ts";
import { LuaStackFrame, luaValueToJS } from "$common/space_lua/runtime.ts";
import { buildThreadLocalEnv } from "$common/space_lua_api.ts";
export function luaSyscalls(commonSystem: CommonSystem): SysCallMapping {
return {
@ -15,15 +16,21 @@ export function luaSyscalls(commonSystem: CommonSystem): SysCallMapping {
* @param expression
* @returns
*/
"lua.evalExpression": (_ctx, expression: string) => {
const ast = parseExpressionString(expression);
return luaValueToJS(
evalExpression(
ast,
"lua.evalExpression": async (_ctx, expression: string) => {
try {
const ast = parseExpressionString(expression);
const env = await buildThreadLocalEnv(
commonSystem.system,
commonSystem.spaceLuaEnv.env,
LuaStackFrame.lostFrame,
),
);
);
const sf = new LuaStackFrame(env, null);
return luaValueToJS(
evalExpression(ast, commonSystem.spaceLuaEnv.env, sf),
);
} catch (e: any) {
console.error("Lua eval error: ", e.message, e.sf?.astCtx);
throw e;
}
},
};
}

View File

@ -116,13 +116,11 @@ export async function expandCodeWidgets(
let result = await lua.evalExpression(exprText);
if (result.markdown) {
if (result?.markdown) {
result = result.markdown;
}
const markdown = renderExpressionResult(result);
console.log("Expanding LuaDirective", exprText, result, markdown);
return await parseMarkdown(markdown);
}
});

View File

@ -1,5 +1,4 @@
This describes the APIs available in [[Space Lua]]
${template.each(query[[
from index.tag "page" where string.startswith(name, "API/")
]], templates.pageItem)}

View File

@ -141,9 +141,7 @@ There's a magic `_CTX` global variable available from which you can access usefu
* `_CTX._GLOBAL` providing access to the global scope
# API
All of these are available via the global namespace:
${template.each(query[[from index.tag "page" where string.startswith(name, "API/")]], templates.pageItem)}
![[API]]
While in [[Space Script]] all syscalls are asynchronous and need to be called with `await`, this is happens transparently in Space Lua leading to cleaner code:
```space-lua