summaryrefslogtreecommitdiff
path: root/cli/js/plugins.ts
diff options
context:
space:
mode:
authorAndy Finch <andyfinch7@gmail.com>2019-12-05 15:30:20 -0500
committerRy Dahl <ry@tinyclouds.org>2019-12-05 15:30:20 -0500
commit7c3b9b4f4f2f4ec8fdeb0e77bb853fd22ffaa476 (patch)
treeaeafe5cc2560c5366704d7a580a5b0e0dced504d /cli/js/plugins.ts
parent214b3eb29aa9cce8a55a247b4bd816cbd19bfe6b (diff)
feat: first pass at native plugins (#3372)
Diffstat (limited to 'cli/js/plugins.ts')
-rw-r--r--cli/js/plugins.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/cli/js/plugins.ts b/cli/js/plugins.ts
new file mode 100644
index 000000000..324ae3408
--- /dev/null
+++ b/cli/js/plugins.ts
@@ -0,0 +1,66 @@
+import { sendSync } from "./dispatch_json.ts";
+import { OP_OPEN_PLUGIN, setPluginAsyncHandler } from "./dispatch.ts";
+import { core } from "./core.ts";
+
+export interface AsyncHandler {
+ (msg: Uint8Array): void;
+}
+
+interface PluginOp {
+ dispatch(
+ control: Uint8Array,
+ zeroCopy?: ArrayBufferView | null
+ ): Uint8Array | null;
+ setAsyncHandler(handler: AsyncHandler): void;
+}
+
+class PluginOpImpl implements PluginOp {
+ constructor(private readonly opId: number) {}
+
+ dispatch(
+ control: Uint8Array,
+ zeroCopy?: ArrayBufferView | null
+ ): Uint8Array | null {
+ return core.dispatch(this.opId, control, zeroCopy);
+ }
+
+ setAsyncHandler(handler: AsyncHandler): void {
+ setPluginAsyncHandler(this.opId, handler);
+ }
+}
+
+// TODO(afinch7): add close method.
+
+interface Plugin {
+ ops: {
+ [name: string]: PluginOp;
+ };
+}
+
+class PluginImpl implements Plugin {
+ private _ops: { [name: string]: PluginOp } = {};
+
+ constructor(private readonly rid: number, ops: { [name: string]: number }) {
+ for (const op in ops) {
+ this._ops[op] = new PluginOpImpl(ops[op]);
+ }
+ }
+
+ get ops(): { [name: string]: PluginOp } {
+ return Object.assign({}, this._ops);
+ }
+}
+
+interface OpenPluginResponse {
+ rid: number;
+ ops: {
+ [name: string]: number;
+ };
+}
+
+export function openPlugin(filename: string): Plugin {
+ const response: OpenPluginResponse = sendSync(OP_OPEN_PLUGIN, {
+ filename
+ });
+ return new PluginImpl(response.rid, response.ops);
+}