silverbullet/common/syscalls/index.ts

123 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-01-17 17:41:02 +08:00
import type {
KvQuery,
ObjectQuery,
ObjectValue,
} from "@silverbulletmd/silverbullet/types";
import type { SysCallMapping } from "$lib/plugos/system.ts";
import {
findAllQueryVariables,
type LuaCollectionQuery,
type LuaQueryCollection,
} from "$common/space_lua/query_collection.ts";
import {
type LuaEnv,
LuaRuntimeError,
type LuaStackFrame,
luaValueToJS,
} from "$common/space_lua/runtime.ts";
import type { CommonSystem } from "$common/common_system.ts";
2025-01-17 17:41:02 +08:00
// These are just wrappers around the system.invokeFunction calls, but they make it easier to use the index
export function indexSyscalls(commonSystem: CommonSystem): SysCallMapping {
2025-01-17 17:41:02 +08:00
return {
2025-01-18 01:31:17 +08:00
"index.indexObjects": (ctx, page: string, objects: ObjectValue<any>[]) => {
return commonSystem.system.syscall(ctx, "system.invokeFunction", [
2025-01-18 01:31:17 +08:00
"index.indexObjects",
page,
objects,
]);
2025-01-17 17:41:02 +08:00
},
"index.queryObjects": (
2025-01-18 01:31:17 +08:00
ctx,
2025-01-17 17:41:02 +08:00
tag: string,
query: ObjectQuery,
ttlSecs?: number,
) => {
return commonSystem.system.syscall(ctx, "system.invokeFunction", [
2025-01-18 01:31:17 +08:00
"index.queryObjects",
2025-01-17 17:41:02 +08:00
tag,
query,
ttlSecs,
]);
},
"index.queryLuaObjects": (
2025-01-18 01:31:17 +08:00
ctx,
2025-01-17 17:41:02 +08:00
tag: string,
query: LuaCollectionQuery,
scopedVariables?: Record<string, any>,
) => {
return commonSystem.system.syscall(ctx, "system.invokeFunction", [
2025-01-17 17:41:02 +08:00
"index.queryLuaObjects",
2025-01-18 01:31:17 +08:00
tag,
query,
scopedVariables,
]);
2025-01-17 17:41:02 +08:00
},
2025-01-18 01:31:17 +08:00
"index.queryDeleteObjects": (ctx, tag: string, query: ObjectQuery) => {
return commonSystem.system.syscall(ctx, "system.invokeFunction", [
2025-01-18 01:31:17 +08:00
"index.queryDeleteObjects",
tag,
query,
]);
2025-01-17 17:41:02 +08:00
},
2025-01-18 01:31:17 +08:00
"index.query": (ctx, query: KvQuery, variables?: Record<string, any>) => {
return commonSystem.system.syscall(ctx, "system.invokeFunction", [
2025-01-18 01:31:17 +08:00
"index.query",
query,
variables,
]);
2025-01-17 17:41:02 +08:00
},
2025-01-18 01:31:17 +08:00
"index.getObjectByRef": (ctx, page: string, tag: string, ref: string) => {
return commonSystem.system.syscall(ctx, "system.invokeFunction", [
2025-01-18 01:31:17 +08:00
"index.getObjectByRef",
page,
tag,
ref,
]);
2025-01-17 17:41:02 +08:00
},
"index.tag": (_ctx, tagName: string): LuaQueryCollection => {
return {
query: async (
query: LuaCollectionQuery,
env: LuaEnv,
sf: LuaStackFrame,
): Promise<any[]> => {
const global = commonSystem.spaceLuaEnv.env;
const localVars = findAllQueryVariables(query).filter((v) =>
!global.has(v) && v !== "_"
);
const scopedVariables: Record<string, any> = {};
for (const v of localVars) {
try {
const jsonValue = await luaValueToJS(env.get(v));
// Ensure this is JSON serializable
JSON.stringify(jsonValue);
scopedVariables[v] = jsonValue;
} catch (e: any) {
console.error(
"Failed to JSON serialize variable",
v,
e,
);
throw new LuaRuntimeError(
`Failed to JSON serialize variable ${v} in query`,
sf,
);
}
}
return (await global.get("datastore").get("query_lua").call(
sf,
[
"idx",
tagName,
],
query,
scopedVariables,
)).toJSArray();
},
};
},
2025-01-17 17:41:02 +08:00
};
}