diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-06-21 16:34:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-21 16:34:43 +0200 |
commit | 79adc7b000a807caff01a3639b18cb91aef9bd9c (patch) | |
tree | 8592baf9a2d88aea7a55f55903739b63bd23fded /cli/js/ops | |
parent | 86448fd9aaa9d70078f7928c9ea1d5af2679ea08 (diff) |
core: add Deno.core.dispatchByName (#6395)
This commit adds alternate dispatch method to core JS API.
"Deno.core.dispatchByName()" works like "Deno.core.dispatch()",
but takes op name instead of op id as a first argument.
Diffstat (limited to 'cli/js/ops')
-rw-r--r-- | cli/js/ops/dispatch_json.ts | 13 | ||||
-rw-r--r-- | cli/js/ops/dispatch_minimal.ts | 8 | ||||
-rw-r--r-- | cli/js/ops/io.ts | 27 |
3 files changed, 12 insertions, 36 deletions
diff --git a/cli/js/ops/dispatch_json.ts b/cli/js/ops/dispatch_json.ts index 6292c188a..6a91f6a4f 100644 --- a/cli/js/ops/dispatch_json.ts +++ b/cli/js/ops/dispatch_json.ts @@ -1,7 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as util from "../util.ts"; import { core } from "../core.ts"; -import { OPS_CACHE } from "../runtime.ts"; import { ErrorKind, getErrorClass } from "../errors.ts"; // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -61,12 +60,10 @@ export function sendSync( args: object = {}, ...zeroCopy: Uint8Array[] ): Ok { - const opId = OPS_CACHE[opName]; - util.log("sendSync", opName, opId); + util.log("sendSync", opName); const argsUi8 = encode(args); - const resUi8 = core.dispatch(opId, argsUi8, ...zeroCopy); + const resUi8 = core.dispatchByName(opName, argsUi8, ...zeroCopy); util.assert(resUi8 != null); - const res = decode(resUi8); util.assert(res.promiseId == null); return unwrapResponse(res); @@ -77,14 +74,12 @@ export async function sendAsync( args: object = {}, ...zeroCopy: Uint8Array[] ): Promise<Ok> { - const opId = OPS_CACHE[opName]; - util.log("sendAsync", opName, opId); + util.log("sendAsync", opName); const promiseId = nextPromiseId(); args = Object.assign(args, { promiseId }); const promise = util.createResolvable<Ok>(); - const argsUi8 = encode(args); - const buf = core.dispatch(opId, argsUi8, ...zeroCopy); + const buf = core.dispatchByName(opName, argsUi8, ...zeroCopy); if (buf) { // Sync result. const res = decode(buf); diff --git a/cli/js/ops/dispatch_minimal.ts b/cli/js/ops/dispatch_minimal.ts index 570463583..0d1dac44e 100644 --- a/cli/js/ops/dispatch_minimal.ts +++ b/cli/js/ops/dispatch_minimal.ts @@ -83,7 +83,7 @@ export function asyncMsgFromRust(ui8: Uint8Array): void { } export async function sendAsyncMinimal( - opId: number, + opName: string, arg: number, zeroCopy: Uint8Array ): Promise<number> { @@ -92,7 +92,7 @@ export async function sendAsyncMinimal( scratch32[1] = arg; scratch32[2] = 0; // result const promise = util.createResolvable<RecordMinimal>(); - const buf = core.dispatch(opId, scratchBytes, zeroCopy); + const buf = core.dispatchByName(opName, scratchBytes, zeroCopy); if (buf) { const record = recordFromBufMinimal(buf); // Sync result. @@ -107,13 +107,13 @@ export async function sendAsyncMinimal( } export function sendSyncMinimal( - opId: number, + opName: string, arg: number, zeroCopy: Uint8Array ): number { scratch32[0] = 0; // promiseId 0 indicates sync scratch32[1] = arg; - const res = core.dispatch(opId, scratchBytes, zeroCopy)!; + const res = core.dispatchByName(opName, scratchBytes, zeroCopy)!; const resRecord = recordFromBufMinimal(res); return unwrapResponse(resRecord); } diff --git a/cli/js/ops/io.ts b/cli/js/ops/io.ts index b4ef837e0..cc3d1d170 100644 --- a/cli/js/ops/io.ts +++ b/cli/js/ops/io.ts @@ -1,22 +1,12 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal.ts"; -// TODO(bartlomieju): remove this import and maybe lazy-initialize -// OPS_CACHE that belongs only to this module -import { OPS_CACHE } from "../runtime.ts"; - -// This is done because read/write are extremely performance sensitive. -let OP_READ = -1; -let OP_WRITE = -1; export function readSync(rid: number, buffer: Uint8Array): number | null { if (buffer.length == 0) { return 0; } - if (OP_READ < 0) { - OP_READ = OPS_CACHE["op_read"]; - } - const nread = sendSyncMinimal(OP_READ, rid, buffer); + const nread = sendSyncMinimal("op_read", rid, buffer); if (nread < 0) { throw new Error("read error"); } else if (nread == 0) { @@ -33,10 +23,7 @@ export async function read( if (buffer.length == 0) { return 0; } - if (OP_READ < 0) { - OP_READ = OPS_CACHE["op_read"]; - } - const nread = await sendAsyncMinimal(OP_READ, rid, buffer); + const nread = await sendAsyncMinimal("op_read", rid, buffer); if (nread < 0) { throw new Error("read error"); } else if (nread == 0) { @@ -47,10 +34,7 @@ export async function read( } export function writeSync(rid: number, data: Uint8Array): number { - if (OP_WRITE < 0) { - OP_WRITE = OPS_CACHE["op_write"]; - } - const result = sendSyncMinimal(OP_WRITE, rid, data); + const result = sendSyncMinimal("op_write", rid, data); if (result < 0) { throw new Error("write error"); } else { @@ -59,10 +43,7 @@ export function writeSync(rid: number, data: Uint8Array): number { } export async function write(rid: number, data: Uint8Array): Promise<number> { - if (OP_WRITE < 0) { - OP_WRITE = OPS_CACHE["op_write"]; - } - const result = await sendAsyncMinimal(OP_WRITE, rid, data); + const result = await sendAsyncMinimal("op_write", rid, data); if (result < 0) { throw new Error("write error"); } else { |