2024-07-30 23:33:33 +08:00
|
|
|
import type { System } from "../lib/plugos/system.ts";
|
|
|
|
import type { ParseTree } from "../plug-api/lib/tree.ts";
|
|
|
|
import type { ScriptObject } from "../plugs/index/script.ts";
|
|
|
|
import type { AppCommand, CommandDef } from "$lib/command.ts";
|
2024-03-16 22:29:24 +08:00
|
|
|
import { Intl, Temporal, toTemporalInstant } from "@js-temporal/polyfill";
|
2024-08-07 17:09:56 +08:00
|
|
|
import * as syscalls from "@silverbulletmd/silverbullet/syscalls";
|
2024-03-16 22:29:24 +08:00
|
|
|
|
|
|
|
// @ts-ignore: Temporal polyfill
|
|
|
|
Date.prototype.toTemporalInstant = toTemporalInstant;
|
|
|
|
// @ts-ignore: Temporal polyfill
|
|
|
|
globalThis.Temporal = Temporal;
|
|
|
|
// @ts-ignore: Intl polyfill
|
2024-04-03 01:26:06 +08:00
|
|
|
Object.apply(globalThis.Intl, Intl);
|
2024-02-06 23:51:04 +08:00
|
|
|
|
2024-02-23 21:06:55 +08:00
|
|
|
type FunctionDef = {
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
2024-02-28 03:05:12 +08:00
|
|
|
type AttributeExtractorDef = {
|
|
|
|
tags: string[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type EventListenerDef = {
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type AttributeExtractorCallback = (
|
|
|
|
text: string,
|
|
|
|
tree: ParseTree,
|
|
|
|
) => Record<string, any> | null | Promise<Record<string, any> | null>;
|
|
|
|
|
2024-02-06 23:51:04 +08:00
|
|
|
export class ScriptEnvironment {
|
2024-02-07 21:50:01 +08:00
|
|
|
functions: Record<string, (...args: any[]) => any> = {};
|
|
|
|
commands: Record<string, AppCommand> = {};
|
2024-02-28 03:05:12 +08:00
|
|
|
attributeExtractors: Record<string, AttributeExtractorCallback[]> = {};
|
|
|
|
eventHandlers: Record<string, ((...args: any[]) => any)[]> = {};
|
2024-02-06 23:51:04 +08:00
|
|
|
|
|
|
|
// Public API
|
2024-02-23 21:06:55 +08:00
|
|
|
|
|
|
|
// Register function
|
|
|
|
registerFunction(def: FunctionDef, fn: (...args: any[]) => any): void;
|
|
|
|
// Legacy invocation
|
|
|
|
registerFunction(name: string, fn: (...args: any[]) => any): void;
|
|
|
|
registerFunction(
|
|
|
|
arg: string | FunctionDef,
|
|
|
|
fn: (...args: any[]) => any,
|
|
|
|
): void {
|
|
|
|
if (typeof arg === "string") {
|
|
|
|
console.warn(
|
|
|
|
"registerFunction with string is deprecated, use `{name: string}` instead",
|
|
|
|
);
|
|
|
|
arg = { name: arg };
|
|
|
|
}
|
2024-02-28 17:54:47 +08:00
|
|
|
if (this.functions[arg.name]) {
|
|
|
|
console.warn(`Function ${arg.name} already registered, overwriting`);
|
|
|
|
}
|
2024-02-23 21:06:55 +08:00
|
|
|
this.functions[arg.name] = fn;
|
2024-02-06 23:51:04 +08:00
|
|
|
}
|
|
|
|
|
2024-02-07 21:50:01 +08:00
|
|
|
registerCommand(command: CommandDef, fn: (...args: any[]) => any) {
|
|
|
|
this.commands[command.name] = {
|
|
|
|
command,
|
|
|
|
run: (...args: any[]) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
// Next tick
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve(fn(...args));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-02-28 03:05:12 +08:00
|
|
|
registerAttributeExtractor(
|
|
|
|
def: AttributeExtractorDef,
|
|
|
|
callback: AttributeExtractorCallback,
|
|
|
|
) {
|
|
|
|
for (const tag of def.tags) {
|
|
|
|
if (!this.attributeExtractors[tag]) {
|
|
|
|
this.attributeExtractors[tag] = [];
|
|
|
|
}
|
|
|
|
this.attributeExtractors[tag].push(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registerEventListener(
|
|
|
|
def: EventListenerDef,
|
|
|
|
callback: (...args: any[]) => any,
|
|
|
|
) {
|
|
|
|
if (!this.eventHandlers[def.name]) {
|
|
|
|
this.eventHandlers[def.name] = [];
|
|
|
|
}
|
|
|
|
this.eventHandlers[def.name].push(callback);
|
|
|
|
}
|
|
|
|
|
2024-02-06 23:51:04 +08:00
|
|
|
// Internal API
|
|
|
|
evalScript(script: string, system: System<any>) {
|
|
|
|
try {
|
2024-08-07 17:09:56 +08:00
|
|
|
const syscallArgs = [];
|
|
|
|
const syscallValues = [];
|
|
|
|
for (const [tl, value] of Object.entries(syscalls)) {
|
|
|
|
syscallArgs.push(tl);
|
|
|
|
syscallValues.push(value);
|
|
|
|
}
|
2024-02-07 21:50:01 +08:00
|
|
|
const fn = Function(
|
|
|
|
"silverbullet",
|
|
|
|
"syscall",
|
2024-08-07 17:09:56 +08:00
|
|
|
...syscallArgs,
|
2024-02-07 21:50:01 +08:00
|
|
|
script,
|
|
|
|
);
|
|
|
|
fn.call(
|
|
|
|
{},
|
2024-02-06 23:51:04 +08:00
|
|
|
this,
|
|
|
|
(name: string, ...args: any[]) => system.syscall({}, name, args),
|
2024-08-07 17:09:56 +08:00
|
|
|
...syscallValues,
|
2024-02-06 23:51:04 +08:00
|
|
|
);
|
|
|
|
} catch (e: any) {
|
|
|
|
throw new Error(
|
|
|
|
`Error evaluating script: ${e.message} for script: ${script}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadFromSystem(system: System<any>) {
|
2024-08-07 17:09:56 +08:00
|
|
|
// Install global syscall function on globalThis
|
|
|
|
(globalThis as any).syscall = (name: string, ...args: any[]) =>
|
|
|
|
system.syscall({}, name, args);
|
|
|
|
|
2024-02-06 23:51:04 +08:00
|
|
|
if (!system.loadedPlugs.has("index")) {
|
|
|
|
console.warn("Index plug not found, skipping loading space scripts");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const allScripts: ScriptObject[] = await system.invokeFunction(
|
|
|
|
"index.queryObjects",
|
|
|
|
["space-script", {}],
|
|
|
|
);
|
|
|
|
for (const script of allScripts) {
|
|
|
|
this.evalScript(script.script, system);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|