Reduce code duplication

pull/1219/merge
Zef Hemel 2025-02-13 20:11:02 +01:00
parent cf81594b02
commit 9d3b9a8a22
6 changed files with 15 additions and 19 deletions

View File

@ -15,11 +15,7 @@ import {
defaultConfig, defaultConfig,
} from "@silverbulletmd/silverbullet/type/config"; } from "@silverbulletmd/silverbullet/type/config";
import { parse } from "$common/space_lua/parse.ts"; import { parse } from "$common/space_lua/parse.ts";
import { import { LuaRuntimeError, LuaStackFrame } from "$common/space_lua/runtime.ts";
LuaEnv,
LuaRuntimeError,
LuaStackFrame,
} from "$common/space_lua/runtime.ts";
import { evalStatement } from "$common/space_lua/eval.ts"; import { evalStatement } from "$common/space_lua/eval.ts";
@ -68,8 +64,7 @@ export async function luaRunCommand(
const luaFile = await Deno.readTextFile(scriptPath); const luaFile = await Deno.readTextFile(scriptPath);
const chunk = parse(luaFile, {}); const chunk = parse(luaFile, {});
const env = serverSystem.spaceLuaEnv.env; const env = serverSystem.spaceLuaEnv.env;
const sf = new LuaStackFrame(new LuaEnv(), chunk.ctx); const sf = LuaStackFrame.createWithGlobalEnv(env, chunk.ctx);
sf.threadLocal.setLocal("_GLOBAL", env);
try { try {
await evalStatement(chunk, env, sf); await evalStatement(chunk, env, sf);

View File

@ -43,8 +43,7 @@ async function runLuaTest(luaPath: string) {
); );
const chunk = parse(luaFile, {}); const chunk = parse(luaFile, {});
const env = new LuaEnv(luaBuildStandardEnv()); const env = new LuaEnv(luaBuildStandardEnv());
const sf = new LuaStackFrame(new LuaEnv(), chunk.ctx); const sf = LuaStackFrame.createWithGlobalEnv(env, chunk.ctx);
sf.threadLocal.setLocal("_GLOBAL", env);
try { try {
await evalStatement(chunk, env, sf); await evalStatement(chunk, env, sf);

View File

@ -108,7 +108,7 @@ export class LuaStackFrame {
static lostFrame = new LuaStackFrame(new LuaEnv(), null); static lostFrame = new LuaStackFrame(new LuaEnv(), null);
static createWithGlobalEnv( static createWithGlobalEnv(
globalEnv: any, globalEnv: LuaEnv,
ctx: ASTCtx | null = null, ctx: ASTCtx | null = null,
): LuaStackFrame { ): LuaStackFrame {
const env = new LuaEnv(); const env = new LuaEnv();

View File

@ -49,9 +49,9 @@ export function dataStoreReadSyscalls(
scopeVariables: Record<string, any> = {}, scopeVariables: Record<string, any> = {},
): Promise<any[]> => { ): Promise<any[]> => {
const dsQueryCollection = new DataStoreQueryCollection(ds, prefix); const dsQueryCollection = new DataStoreQueryCollection(ds, prefix);
const tl = new LuaEnv(); const sf = LuaStackFrame.createWithGlobalEnv(
tl.setLocal("_GLOBAL", commonSystem.spaceLuaEnv.env); commonSystem.spaceLuaEnv.env,
const sf = new LuaStackFrame(tl, null); );
const env = new LuaEnv(commonSystem.spaceLuaEnv.env); const env = new LuaEnv(commonSystem.spaceLuaEnv.env);
for (const [key, value] of Object.entries(scopeVariables)) { for (const [key, value] of Object.entries(scopeVariables)) {
env.set(key, jsToLuaValue(value)); env.set(key, jsToLuaValue(value));

View File

@ -64,8 +64,10 @@ export function luaDirectivePlugin(client: Client) {
currentPageMeta || currentPageMeta ||
{ name: client.ui.viewState.currentPage }, { name: client.ui.viewState.currentPage },
); );
tl.setLocal("_GLOBAL", client.clientSystem.spaceLuaEnv.env); const sf = LuaStackFrame.createWithGlobalEnv(
const sf = new LuaStackFrame(tl, expr.ctx); client.clientSystem.spaceLuaEnv.env,
expr.ctx,
);
const threadLocalizedEnv = new LuaEnv( const threadLocalizedEnv = new LuaEnv(
client.clientSystem.spaceLuaEnv.env, client.clientSystem.spaceLuaEnv.env,
); );

View File

@ -12,7 +12,7 @@ import { activeWidgets } from "./markdown_widget.ts";
import { attachWidgetEventHandlers } from "./widget_util.ts"; import { attachWidgetEventHandlers } from "./widget_util.ts";
import { renderExpressionResult } from "../../plugs/template/util.ts"; import { renderExpressionResult } from "../../plugs/template/util.ts";
import { expandCodeWidgets } from "$common/markdown.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 = ( export type LuaWidgetCallback = (
bodyText: string, bodyText: string,
@ -104,9 +104,9 @@ export class LuaWidget extends WidgetType {
mdContent, mdContent,
); );
const tl = new LuaEnv(); const sf = LuaStackFrame.createWithGlobalEnv(
const sf = new LuaStackFrame(tl, null, LuaStackFrame.lostFrame); client.clientSystem.spaceLuaEnv.env,
tl.setLocal("_GLOBAL", client.clientSystem.spaceLuaEnv.env); );
mdTree = await expandCodeWidgets( mdTree = await expandCodeWidgets(
client.clientSystem.codeWidgetHook, client.clientSystem.codeWidgetHook,
mdTree, mdTree,