mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 08:39:09 -05:00
79adc7b000
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.
52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal.ts";
|
|
|
|
export function readSync(rid: number, buffer: Uint8Array): number | null {
|
|
if (buffer.length == 0) {
|
|
return 0;
|
|
}
|
|
const nread = sendSyncMinimal("op_read", rid, buffer);
|
|
if (nread < 0) {
|
|
throw new Error("read error");
|
|
} else if (nread == 0) {
|
|
return null;
|
|
} else {
|
|
return nread;
|
|
}
|
|
}
|
|
|
|
export async function read(
|
|
rid: number,
|
|
buffer: Uint8Array
|
|
): Promise<number | null> {
|
|
if (buffer.length == 0) {
|
|
return 0;
|
|
}
|
|
const nread = await sendAsyncMinimal("op_read", rid, buffer);
|
|
if (nread < 0) {
|
|
throw new Error("read error");
|
|
} else if (nread == 0) {
|
|
return null;
|
|
} else {
|
|
return nread;
|
|
}
|
|
}
|
|
|
|
export function writeSync(rid: number, data: Uint8Array): number {
|
|
const result = sendSyncMinimal("op_write", rid, data);
|
|
if (result < 0) {
|
|
throw new Error("write error");
|
|
} else {
|
|
return result;
|
|
}
|
|
}
|
|
|
|
export async function write(rid: number, data: Uint8Array): Promise<number> {
|
|
const result = await sendAsyncMinimal("op_write", rid, data);
|
|
if (result < 0) {
|
|
throw new Error("write error");
|
|
} else {
|
|
return result;
|
|
}
|
|
}
|