1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 17:49:08 -05:00
denoland-deno/js/dispatch_minimal.ts
Ryan Dahl e438ac2c74
Add op_id throughout op API (#2734)
Removes the magic number hack to switch between flatbuffers and the
minimal dispatcher.

Adds machinery to pass the op_id through the shared_queue.
2019-08-07 14:02:29 -04:00

68 lines
1.7 KiB
TypeScript

// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// Do not add flatbuffer dependencies to this module.
import * as util from "./util";
import { core } from "./core";
const promiseTableMin = new Map<number, util.Resolvable<number>>();
let _nextPromiseId = 0;
export function nextPromiseId(): number {
return _nextPromiseId++;
}
export interface RecordMinimal {
promiseId: number;
opId: number; // Maybe better called dispatchId
arg: number;
result: number;
}
export function recordFromBufMinimal(
opId: number,
buf32: Int32Array
): RecordMinimal {
if (buf32.length != 3) {
throw Error("Bad message");
}
return {
promiseId: buf32[0],
opId,
arg: buf32[1],
result: buf32[2]
};
}
const scratch32 = new Int32Array(3);
const scratchBytes = new Uint8Array(
scratch32.buffer,
scratch32.byteOffset,
scratch32.byteLength
);
util.assert(scratchBytes.byteLength === scratch32.length * 4);
export function handleAsyncMsgFromRustMinimal(
ui8: Uint8Array,
record: RecordMinimal
): void {
// Fast and new
util.log("minimal handleAsyncMsgFromRust ", ui8.length);
const { promiseId, result } = record;
const promise = promiseTableMin.get(promiseId);
promiseTableMin.delete(promiseId);
promise!.resolve(result);
}
export function sendAsyncMinimal(
opId: number,
arg: number,
zeroCopy: Uint8Array
): Promise<number> {
const promiseId = nextPromiseId(); // AKA cmdId
scratch32[0] = promiseId;
scratch32[1] = arg;
scratch32[2] = 0; // result
const promise = util.createResolvable<number>();
promiseTableMin.set(promiseId, promise);
core.dispatch(opId, scratchBytes, zeroCopy);
return promise;
}