summaryrefslogtreecommitdiff
path: root/cli/js
diff options
context:
space:
mode:
authorAndy Finch <andyfinch7@gmail.com>2020-01-17 08:26:11 -0500
committerRy Dahl <ry@tinyclouds.org>2020-01-17 08:26:11 -0500
commitfe5662058e0c7767e7b2724451f9d5a3f133ffbf (patch)
treebe43744898f64af6605246ba95ede55a869f7ab4 /cli/js
parentd8ad81d3fb5b4b9502ea021edfc99201538553e6 (diff)
feat: support individual async handler for each op (#3690)
Diffstat (limited to 'cli/js')
-rw-r--r--cli/js/dispatch.ts57
-rw-r--r--cli/js/dispatch_json.ts2
-rw-r--r--cli/js/dispatch_minimal.ts15
-rw-r--r--cli/js/os.ts2
-rw-r--r--cli/js/plugins.ts4
5 files changed, 15 insertions, 65 deletions
diff --git a/cli/js/dispatch.ts b/cli/js/dispatch.ts
index 31c42c224..d658c5de7 100644
--- a/cli/js/dispatch.ts
+++ b/cli/js/dispatch.ts
@@ -85,57 +85,12 @@ export function setPluginAsyncHandler(
PLUGIN_ASYNC_HANDLER_MAP.set(opId, handler);
}
-export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
- switch (opId) {
- case OP_WRITE:
- case OP_READ:
- minimal.asyncMsgFromRust(opId, ui8);
- break;
- case OP_GET_DIR:
- case OP_EXIT:
- case OP_IS_TTY:
- case OP_ENV:
- case OP_EXEC_PATH:
- case OP_UTIME:
- case OP_OPEN:
- case OP_SEEK:
- case OP_FETCH:
- case OP_REPL_START:
- case OP_REPL_READLINE:
- case OP_ACCEPT:
- case OP_ACCEPT_TLS:
- case OP_DIAL:
- case OP_GLOBAL_TIMER:
- case OP_HOST_GET_WORKER_CLOSED:
- case OP_HOST_GET_MESSAGE:
- case OP_WORKER_GET_MESSAGE:
- case OP_RUN_STATUS:
- case OP_MKDIR:
- case OP_CHMOD:
- case OP_CHOWN:
- case OP_REMOVE:
- case OP_COPY_FILE:
- case OP_STAT:
- case OP_REALPATH:
- case OP_READ_DIR:
- case OP_RENAME:
- case OP_LINK:
- case OP_SYMLINK:
- case OP_READ_LINK:
- case OP_TRUNCATE:
- case OP_MAKE_TEMP_DIR:
- case OP_DIAL_TLS:
- case OP_FETCH_SOURCE_FILES:
- case OP_COMPILE:
- case OP_TRANSPILE:
- json.asyncMsgFromRust(opId, ui8);
- break;
+export function getAsyncHandler(opName: string): (msg: Uint8Array) => void {
+ switch (opName) {
+ case "OP_WRITE":
+ case "OP_READ":
+ return minimal.asyncMsgFromRust;
default:
- const handler = PLUGIN_ASYNC_HANDLER_MAP.get(opId);
- if (handler) {
- handler(ui8);
- } else {
- throw Error("bad async opId");
- }
+ return json.asyncMsgFromRust;
}
}
diff --git a/cli/js/dispatch_json.ts b/cli/js/dispatch_json.ts
index 07095ea4b..adccb69c6 100644
--- a/cli/js/dispatch_json.ts
+++ b/cli/js/dispatch_json.ts
@@ -43,7 +43,7 @@ function unwrapResponse(res: JsonResponse): Ok {
return res.ok;
}
-export function asyncMsgFromRust(opId: number, resUi8: Uint8Array): void {
+export function asyncMsgFromRust(resUi8: Uint8Array): void {
const res = decode(resUi8);
util.assert(res.promiseId != null);
diff --git a/cli/js/dispatch_minimal.ts b/cli/js/dispatch_minimal.ts
index 32b282c20..1ce3fbaef 100644
--- a/cli/js/dispatch_minimal.ts
+++ b/cli/js/dispatch_minimal.ts
@@ -18,7 +18,6 @@ function nextPromiseId(): number {
export interface RecordMinimal {
promiseId: number;
- opId: number; // Maybe better called dispatchId
arg: number;
result: number;
err?: {
@@ -27,10 +26,7 @@ export interface RecordMinimal {
};
}
-export function recordFromBufMinimal(
- opId: number,
- ui8: Uint8Array
-): RecordMinimal {
+export function recordFromBufMinimal(ui8: Uint8Array): RecordMinimal {
const header = ui8.slice(0, 12);
const buf32 = new Int32Array(
header.buffer,
@@ -52,7 +48,6 @@ export function recordFromBufMinimal(
return {
promiseId,
- opId,
arg,
result,
err
@@ -74,8 +69,8 @@ const scratchBytes = new Uint8Array(
);
util.assert(scratchBytes.byteLength === scratch32.length * 4);
-export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
- const record = recordFromBufMinimal(opId, ui8);
+export function asyncMsgFromRust(ui8: Uint8Array): void {
+ const record = recordFromBufMinimal(ui8);
const { promiseId } = record;
const promise = promiseTableMin.get(promiseId);
promiseTableMin.delete(promiseId);
@@ -95,7 +90,7 @@ export async function sendAsyncMinimal(
const promise = util.createResolvable<RecordMinimal>();
const buf = core.dispatch(opId, scratchBytes, zeroCopy);
if (buf) {
- const record = recordFromBufMinimal(opId, buf);
+ const record = recordFromBufMinimal(buf);
// Sync result.
promise.resolve(record);
} else {
@@ -115,6 +110,6 @@ export function sendSyncMinimal(
scratch32[0] = 0; // promiseId 0 indicates sync
scratch32[1] = arg;
const res = core.dispatch(opId, scratchBytes, zeroCopy)!;
- const resRecord = recordFromBufMinimal(opId, res);
+ const resRecord = recordFromBufMinimal(res);
return unwrapResponse(resRecord);
}
diff --git a/cli/js/os.ts b/cli/js/os.ts
index e0dd0752b..9b4301aea 100644
--- a/cli/js/os.ts
+++ b/cli/js/os.ts
@@ -89,7 +89,6 @@ interface Start {
// the runtime and the compiler environments.
// @internal
export function start(preserveDenoNamespace = true, source?: string): Start {
- core.setAsyncHandler(dispatch.asyncMsgFromRust);
const ops = core.ops();
// TODO(bartlomieju): this is a prototype, we should come up with
// something a bit more sophisticated
@@ -98,6 +97,7 @@ export function start(preserveDenoNamespace = true, source?: string): Start {
// Assign op ids to actual variables
// TODO(ry) This type casting is gross and should be fixed.
((dispatch as unknown) as { [key: string]: number })[opName] = opId;
+ core.setAsyncHandler(opId, dispatch.getAsyncHandler(opName));
}
// First we send an empty `Start` message to let the privileged side know we
// are ready. The response should be a `StartRes` message containing the CLI
diff --git a/cli/js/plugins.ts b/cli/js/plugins.ts
index 324ae3408..4d2072c79 100644
--- a/cli/js/plugins.ts
+++ b/cli/js/plugins.ts
@@ -1,5 +1,5 @@
import { sendSync } from "./dispatch_json.ts";
-import { OP_OPEN_PLUGIN, setPluginAsyncHandler } from "./dispatch.ts";
+import { OP_OPEN_PLUGIN } from "./dispatch.ts";
import { core } from "./core.ts";
export interface AsyncHandler {
@@ -25,7 +25,7 @@ class PluginOpImpl implements PluginOp {
}
setAsyncHandler(handler: AsyncHandler): void {
- setPluginAsyncHandler(this.opId, handler);
+ core.setAsyncHandler(this.opId, handler);
}
}