Reduce code duplication
parent
cf81594b02
commit
9d3b9a8a22
|
@ -15,11 +15,7 @@ import {
|
|||
defaultConfig,
|
||||
} from "@silverbulletmd/silverbullet/type/config";
|
||||
import { parse } from "$common/space_lua/parse.ts";
|
||||
import {
|
||||
LuaEnv,
|
||||
LuaRuntimeError,
|
||||
LuaStackFrame,
|
||||
} from "$common/space_lua/runtime.ts";
|
||||
import { LuaRuntimeError, LuaStackFrame } from "$common/space_lua/runtime.ts";
|
||||
|
||||
import { evalStatement } from "$common/space_lua/eval.ts";
|
||||
|
||||
|
@ -68,8 +64,7 @@ export async function luaRunCommand(
|
|||
const luaFile = await Deno.readTextFile(scriptPath);
|
||||
const chunk = parse(luaFile, {});
|
||||
const env = serverSystem.spaceLuaEnv.env;
|
||||
const sf = new LuaStackFrame(new LuaEnv(), chunk.ctx);
|
||||
sf.threadLocal.setLocal("_GLOBAL", env);
|
||||
const sf = LuaStackFrame.createWithGlobalEnv(env, chunk.ctx);
|
||||
|
||||
try {
|
||||
await evalStatement(chunk, env, sf);
|
||||
|
|
|
@ -43,8 +43,7 @@ async function runLuaTest(luaPath: string) {
|
|||
);
|
||||
const chunk = parse(luaFile, {});
|
||||
const env = new LuaEnv(luaBuildStandardEnv());
|
||||
const sf = new LuaStackFrame(new LuaEnv(), chunk.ctx);
|
||||
sf.threadLocal.setLocal("_GLOBAL", env);
|
||||
const sf = LuaStackFrame.createWithGlobalEnv(env, chunk.ctx);
|
||||
|
||||
try {
|
||||
await evalStatement(chunk, env, sf);
|
||||
|
|
|
@ -108,7 +108,7 @@ export class LuaStackFrame {
|
|||
static lostFrame = new LuaStackFrame(new LuaEnv(), null);
|
||||
|
||||
static createWithGlobalEnv(
|
||||
globalEnv: any,
|
||||
globalEnv: LuaEnv,
|
||||
ctx: ASTCtx | null = null,
|
||||
): LuaStackFrame {
|
||||
const env = new LuaEnv();
|
||||
|
|
|
@ -49,9 +49,9 @@ export function dataStoreReadSyscalls(
|
|||
scopeVariables: Record<string, any> = {},
|
||||
): Promise<any[]> => {
|
||||
const dsQueryCollection = new DataStoreQueryCollection(ds, prefix);
|
||||
const tl = new LuaEnv();
|
||||
tl.setLocal("_GLOBAL", commonSystem.spaceLuaEnv.env);
|
||||
const sf = new LuaStackFrame(tl, null);
|
||||
const sf = LuaStackFrame.createWithGlobalEnv(
|
||||
commonSystem.spaceLuaEnv.env,
|
||||
);
|
||||
const env = new LuaEnv(commonSystem.spaceLuaEnv.env);
|
||||
for (const [key, value] of Object.entries(scopeVariables)) {
|
||||
env.set(key, jsToLuaValue(value));
|
||||
|
|
|
@ -64,8 +64,10 @@ export function luaDirectivePlugin(client: Client) {
|
|||
currentPageMeta ||
|
||||
{ name: client.ui.viewState.currentPage },
|
||||
);
|
||||
tl.setLocal("_GLOBAL", client.clientSystem.spaceLuaEnv.env);
|
||||
const sf = new LuaStackFrame(tl, expr.ctx);
|
||||
const sf = LuaStackFrame.createWithGlobalEnv(
|
||||
client.clientSystem.spaceLuaEnv.env,
|
||||
expr.ctx,
|
||||
);
|
||||
const threadLocalizedEnv = new LuaEnv(
|
||||
client.clientSystem.spaceLuaEnv.env,
|
||||
);
|
||||
|
|
|
@ -12,7 +12,7 @@ import { activeWidgets } from "./markdown_widget.ts";
|
|||
import { attachWidgetEventHandlers } from "./widget_util.ts";
|
||||
import { renderExpressionResult } from "../../plugs/template/util.ts";
|
||||
import { expandCodeWidgets } from "$common/markdown.ts";
|
||||
import { LuaEnv, LuaStackFrame } from "$common/space_lua/runtime.ts";
|
||||
import { LuaStackFrame } from "$common/space_lua/runtime.ts";
|
||||
|
||||
export type LuaWidgetCallback = (
|
||||
bodyText: string,
|
||||
|
@ -104,9 +104,9 @@ export class LuaWidget extends WidgetType {
|
|||
mdContent,
|
||||
);
|
||||
|
||||
const tl = new LuaEnv();
|
||||
const sf = new LuaStackFrame(tl, null, LuaStackFrame.lostFrame);
|
||||
tl.setLocal("_GLOBAL", client.clientSystem.spaceLuaEnv.env);
|
||||
const sf = LuaStackFrame.createWithGlobalEnv(
|
||||
client.clientSystem.spaceLuaEnv.env,
|
||||
);
|
||||
mdTree = await expandCodeWidgets(
|
||||
client.clientSystem.codeWidgetHook,
|
||||
mdTree,
|
||||
|
|
Loading…
Reference in New Issue