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.
This commit is contained in:
Bartek Iwańczuk 2020-06-21 16:34:43 +02:00 committed by GitHub
parent 86448fd9aa
commit 79adc7b000
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 41 deletions

View file

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