feat: support individual async handler for each op (#3690)

This commit is contained in:
Andy Finch 2020-01-17 08:26:11 -05:00 committed by Ry Dahl
parent d8ad81d3fb
commit fe5662058e
9 changed files with 37 additions and 84 deletions

View file

@ -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;
}
}

View file

@ -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);

View file

@ -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);
}

View file

@ -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

View file

@ -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);
}
}