diff --git a/common/space_lua_api.ts b/common/space_lua_api.ts index 71190a22..86927adf 100644 --- a/common/space_lua_api.ts +++ b/common/space_lua_api.ts @@ -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) { diff --git a/common/syscalls/lua.ts b/common/syscalls/lua.ts index dfb0884a..81087f9c 100644 --- a/common/syscalls/lua.ts +++ b/common/syscalls/lua.ts @@ -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; + } }, }; } diff --git a/plugs/markdown/api.ts b/plugs/markdown/api.ts index 2ce256b0..2b6685d9 100644 --- a/plugs/markdown/api.ts +++ b/plugs/markdown/api.ts @@ -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); } }); diff --git a/website/API.md b/website/API.md index 41cfb02b..08f03ff8 100644 --- a/website/API.md +++ b/website/API.md @@ -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)} \ No newline at end of file diff --git a/website/Space Lua.md b/website/Space Lua.md index da3a957f..3b9840b7 100644 --- a/website/Space Lua.md +++ b/website/Space Lua.md @@ -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