Fixes to template Lua expansion
parent
e1c997616c
commit
eb788c68ca
|
@ -57,7 +57,7 @@ export async function buildThreadLocalEnv(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tl.setLocal("_GLOBAL", globalEnv);
|
tl.setLocal("_GLOBAL", globalEnv);
|
||||||
return Promise.resolve(tl);
|
return tl;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handleLuaError(e: LuaRuntimeError, system: System<any>) {
|
export async function handleLuaError(e: LuaRuntimeError, system: System<any>) {
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { evalExpression } from "$common/space_lua/eval.ts";
|
||||||
import { parse, parseExpressionString } from "../space_lua/parse.ts";
|
import { parse, parseExpressionString } from "../space_lua/parse.ts";
|
||||||
import type { CommonSystem } from "$common/common_system.ts";
|
import type { CommonSystem } from "$common/common_system.ts";
|
||||||
import { LuaStackFrame, luaValueToJS } from "$common/space_lua/runtime.ts";
|
import { LuaStackFrame, luaValueToJS } from "$common/space_lua/runtime.ts";
|
||||||
|
import { buildThreadLocalEnv } from "$common/space_lua_api.ts";
|
||||||
|
|
||||||
export function luaSyscalls(commonSystem: CommonSystem): SysCallMapping {
|
export function luaSyscalls(commonSystem: CommonSystem): SysCallMapping {
|
||||||
return {
|
return {
|
||||||
|
@ -15,15 +16,21 @@ export function luaSyscalls(commonSystem: CommonSystem): SysCallMapping {
|
||||||
* @param expression
|
* @param expression
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
"lua.evalExpression": (_ctx, expression: string) => {
|
"lua.evalExpression": async (_ctx, expression: string) => {
|
||||||
|
try {
|
||||||
const ast = parseExpressionString(expression);
|
const ast = parseExpressionString(expression);
|
||||||
return luaValueToJS(
|
const env = await buildThreadLocalEnv(
|
||||||
evalExpression(
|
commonSystem.system,
|
||||||
ast,
|
|
||||||
commonSystem.spaceLuaEnv.env,
|
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;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,13 +116,11 @@ export async function expandCodeWidgets(
|
||||||
|
|
||||||
let result = await lua.evalExpression(exprText);
|
let result = await lua.evalExpression(exprText);
|
||||||
|
|
||||||
if (result.markdown) {
|
if (result?.markdown) {
|
||||||
result = result.markdown;
|
result = result.markdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
const markdown = renderExpressionResult(result);
|
const markdown = renderExpressionResult(result);
|
||||||
|
|
||||||
console.log("Expanding LuaDirective", exprText, result, markdown);
|
|
||||||
return await parseMarkdown(markdown);
|
return await parseMarkdown(markdown);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
This describes the APIs available in [[Space Lua]]
|
This describes the APIs available in [[Space Lua]]
|
||||||
|
|
||||||
${template.each(query[[
|
${template.each(query[[
|
||||||
from index.tag "page" where string.startswith(name, "API/")
|
from index.tag "page" where string.startswith(name, "API/")
|
||||||
]], templates.pageItem)}
|
]], templates.pageItem)}
|
|
@ -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
|
* `_CTX._GLOBAL` providing access to the global scope
|
||||||
|
|
||||||
# API
|
# API
|
||||||
All of these are available via the global namespace:
|
![[API]]
|
||||||
${template.each(query[[from index.tag "page" where string.startswith(name, "API/")]], templates.pageItem)}
|
|
||||||
|
|
||||||
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:
|
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
|
```space-lua
|
||||||
|
|
Loading…
Reference in New Issue