Migrating core use of queryObjects to queryLuaObjects

This commit is contained in:
Zef Hemel
2025-03-18 18:22:17 +01:00
parent bafd2974e1
commit f4de6a4b85
8 changed files with 49 additions and 32 deletions
+9 -4
View File
@@ -5,11 +5,15 @@ import {
LuaRuntimeError,
LuaStackFrame,
} from "$common/space_lua/runtime.ts";
import { parse as parseLua } from "$common/space_lua/parse.ts";
import {
parse as parseLua,
parseExpressionString,
} from "$common/space_lua/parse.ts";
import { evalStatement } from "$common/space_lua/eval.ts";
import { parseRef, type Ref } from "@silverbulletmd/silverbullet/lib/page_ref";
import type { ASTCtx } from "$common/space_lua/ast.ts";
import { buildLuaEnv } from "$common/space_lua_api.ts";
import type { LuaCollectionQuery } from "$common/space_lua/query_collection.ts";
export class SpaceLuaEnvironment {
env: LuaEnv = new LuaEnv();
@@ -22,13 +26,14 @@ export class SpaceLuaEnvironment {
system: System<any>,
) {
const allScripts: ScriptObject[] = await system.invokeFunction(
"index.queryObjects",
"index.queryLuaObjects",
["space-lua", {
objectVariable: "script",
orderBy: [{
expr: ["attr", "priority"],
expr: parseExpressionString("script.priority"),
desc: true,
}],
}],
} as LuaCollectionQuery],
);
try {
this.env = buildLuaEnv(system);
+3 -1
View File
@@ -67,7 +67,7 @@ export class LuaEnv implements ILuaSettable, ILuaGettable {
if (this.parent) {
return this.parent.get(name, sf);
}
return undefined;
return null;
}
/**
@@ -607,6 +607,8 @@ export function luaGet(
if (typeof val === "function") {
// Automatically bind the function to the object
return val.bind(obj);
} else if (val === undefined) {
return null;
} else {
return val;
}
+1 -1
View File
@@ -143,7 +143,7 @@ export class ScriptEnvironment {
return;
}
const allScripts: ScriptObject[] = await system.invokeFunction(
"index.queryObjects",
"index.queryLuaObjects",
["space-script", {}],
);
for (const script of allScripts) {
+5 -1
View File
@@ -9,12 +9,16 @@ import {
} from "$common/space_lua/runtime.ts";
import { buildThreadLocalEnv } from "$common/space_lua_api.ts";
import { isSendable } from "$lib/plugos/util.ts";
import type { LuaBlock, LuaExpression } from "$common/space_lua/ast.ts";
export function luaSyscalls(commonSystem: CommonSystem): SysCallMapping {
return {
"lua.parse": (_ctx, code: string) => {
"lua.parse": (_ctx, code: string): LuaBlock => {
return parse(code);
},
"lua.parseExpression": (_ctx, expression: string): LuaExpression => {
return parseExpressionString(expression);
},
/**
* Evaluates a Lua expression and returns the result as a JavaScript value
* @param _ctx
+8 -2
View File
@@ -1,12 +1,18 @@
import { syscall } from "../syscall.ts";
import type { ParseTree } from "../lib/tree.ts";
import type { LuaBlock, LuaExpression } from "$common/space_lua/ast.ts";
export function parse(
code: string,
): Promise<ParseTree> {
): Promise<LuaBlock> {
return syscall("lua.parse", code);
}
export function parseExpression(
expression: string,
): Promise<LuaExpression> {
return syscall("lua.parseExpression", expression);
}
export function evalExpression(
expression: string,
): Promise<any> {
+1 -1
View File
@@ -40,7 +40,7 @@ function template.fromPage(name)
removeTags = true
})
return template.new(fm.text)
return template.new(string.trimStart(fm.text))
end
-- Creates a template-based slash command, keys for def are:
+19 -17
View File
@@ -75,10 +75,10 @@ import { LimitedMap } from "$lib/limited_map.ts";
import { plugPrefix } from "$common/spaces/constants.ts";
import { lezerToParseTree } from "$common/markdown_parser/parse_tree.ts";
import { findNodeMatching } from "@silverbulletmd/silverbullet/lib/tree";
import type { AspiringPageObject } from "../plugs/index/page_links.ts";
import type { Config, ConfigContainer } from "../type/config.ts";
import { diffAndPrepareChanges } from "./cm_util.ts";
import { DocumentEditor } from "./document_editor.ts";
import { parseExpressionString } from "$common/space_lua/parse.ts";
const frontMatterRegex = /^---\n(([^\n]|\n)*?)---\n/;
@@ -836,20 +836,22 @@ export class Client implements ConfigContainer {
console.warn("Index plug not loaded, cannot update page list cache");
return;
}
const allPages = await this.clientSystem.queryObjects<PageMeta>("page", {});
const allAspiringPages = (await this.clientSystem.queryObjects<
AspiringPageObject
>("aspiring-page", {
select: [{ name: "name" }],
})).map((aspiringPage): PageMeta => ({
ref: aspiringPage.name,
tag: "page",
_isAspiring: true,
name: aspiringPage.name,
created: "",
lastModified: "",
perm: "rw",
}));
const allPages = await this.clientSystem.queryLuaObjects<PageMeta>(
"page",
{},
);
const allAspiringPages =
(await this.clientSystem.queryLuaObjects<string>("aspiring-page", {
select: parseExpressionString("name"),
})).map((name): PageMeta => ({
ref: name,
tag: "page",
_isAspiring: true,
name: name,
created: "",
lastModified: "",
perm: "rw",
}));
this.ui.viewDispatch({
type: "update-page-list",
@@ -860,7 +862,7 @@ export class Client implements ConfigContainer {
async updateDocumentListCache() {
console.log("Updating document list cache");
const allDocuments = await this.clientSystem.queryObjects<DocumentMeta>(
const allDocuments = await this.clientSystem.queryLuaObjects<DocumentMeta>(
"document",
{},
);
@@ -1511,7 +1513,7 @@ export class Client implements ConfigContainer {
}
async loadCustomStyles() {
const spaceStyles = await this.clientSystem.queryObjects<StyleObject>(
const spaceStyles = await this.clientSystem.queryLuaObjects<StyleObject>(
"space-style",
{},
);
+3 -5
View File
@@ -49,6 +49,7 @@ import { indexSyscalls } from "$common/syscalls/index.ts";
import { commandSyscalls } from "$common/syscalls/command.ts";
import { eventListenerSyscalls } from "$common/syscalls/event.ts";
import { DocumentEditorHook } from "./hooks/document_editor.ts";
import type { LuaCollectionQuery } from "$common/space_lua/query_collection.ts";
const plugNameExtractRegex = /\/(.+)\.plug\.js$/;
@@ -240,11 +241,8 @@ export class ClientSystem extends CommonSystem {
return this.system.localSyscall(name, args);
}
queryObjects<T>(tag: string, query: Query): Promise<T[]> {
return this.localSyscall(
"system.invokeFunction",
["index.queryObjects", tag, query],
);
queryLuaObjects<T>(tag: string, query: LuaCollectionQuery): Promise<T[]> {
return this.system.invokeFunction("index.queryLuaObjects", [tag, query]);
}
getObjectByRef<T>(page: string, tag: string, ref: string) {