2019-05-03 00:06:43 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import * as util from "./util";
|
|
|
|
import { core } from "./core";
|
|
|
|
|
|
|
|
const promiseTableMin = new Map<number, util.Resolvable<number>>();
|
2019-08-26 10:58:44 -04:00
|
|
|
// Note it's important that promiseId starts at 1 instead of 0, because sync
|
|
|
|
// messages are indicated with promiseId 0. If we ever add wrap around logic for
|
|
|
|
// overflows, this should be taken into account.
|
2019-08-21 20:42:48 -04:00
|
|
|
let _nextPromiseId = 1;
|
2019-06-14 13:58:20 -04:00
|
|
|
|
2019-08-21 20:42:48 -04:00
|
|
|
function nextPromiseId(): number {
|
2019-06-14 13:58:20 -04:00
|
|
|
return _nextPromiseId++;
|
|
|
|
}
|
2019-05-03 00:06:43 -04:00
|
|
|
|
|
|
|
export interface RecordMinimal {
|
2019-06-14 13:58:20 -04:00
|
|
|
promiseId: number;
|
2019-08-07 14:02:29 -04:00
|
|
|
opId: number; // Maybe better called dispatchId
|
2019-05-03 00:06:43 -04:00
|
|
|
arg: number;
|
|
|
|
result: number;
|
|
|
|
}
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
export function recordFromBufMinimal(
|
|
|
|
opId: number,
|
|
|
|
buf32: Int32Array
|
|
|
|
): RecordMinimal {
|
|
|
|
if (buf32.length != 3) {
|
|
|
|
throw Error("Bad message");
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
2019-08-07 14:02:29 -04:00
|
|
|
return {
|
|
|
|
promiseId: buf32[0],
|
|
|
|
opId,
|
|
|
|
arg: buf32[1],
|
|
|
|
result: buf32[2]
|
|
|
|
};
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
const scratch32 = new Int32Array(3);
|
2019-05-03 00:06:43 -04:00
|
|
|
const scratchBytes = new Uint8Array(
|
|
|
|
scratch32.buffer,
|
|
|
|
scratch32.byteOffset,
|
|
|
|
scratch32.byteLength
|
|
|
|
);
|
|
|
|
util.assert(scratchBytes.byteLength === scratch32.length * 4);
|
|
|
|
|
2019-08-23 01:30:14 -04:00
|
|
|
export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
|
2019-08-21 20:42:48 -04:00
|
|
|
const buf32 = new Int32Array(ui8.buffer, ui8.byteOffset, ui8.byteLength / 4);
|
|
|
|
const record = recordFromBufMinimal(opId, buf32);
|
2019-06-14 13:58:20 -04:00
|
|
|
const { promiseId, result } = record;
|
2019-05-03 00:06:43 -04:00
|
|
|
const promise = promiseTableMin.get(promiseId);
|
|
|
|
promiseTableMin.delete(promiseId);
|
|
|
|
promise!.resolve(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function sendAsyncMinimal(
|
|
|
|
opId: number,
|
|
|
|
arg: number,
|
|
|
|
zeroCopy: Uint8Array
|
|
|
|
): Promise<number> {
|
2019-06-14 13:58:20 -04:00
|
|
|
const promiseId = nextPromiseId(); // AKA cmdId
|
2019-08-07 14:02:29 -04:00
|
|
|
scratch32[0] = promiseId;
|
|
|
|
scratch32[1] = arg;
|
|
|
|
scratch32[2] = 0; // result
|
2019-05-03 00:06:43 -04:00
|
|
|
const promise = util.createResolvable<number>();
|
2019-06-14 13:58:20 -04:00
|
|
|
promiseTableMin.set(promiseId, promise);
|
2019-08-07 14:02:29 -04:00
|
|
|
core.dispatch(opId, scratchBytes, zeroCopy);
|
2019-05-03 00:06:43 -04:00
|
|
|
return promise;
|
|
|
|
}
|
2019-08-26 10:58:44 -04:00
|
|
|
|
|
|
|
export function sendSyncMinimal(
|
|
|
|
opId: number,
|
|
|
|
arg: number,
|
|
|
|
zeroCopy: Uint8Array
|
|
|
|
): number {
|
|
|
|
scratch32[0] = 0; // promiseId 0 indicates sync
|
|
|
|
scratch32[1] = arg;
|
|
|
|
const res = core.dispatch(opId, scratchBytes, zeroCopy)!;
|
|
|
|
const res32 = new Int32Array(res.buffer, res.byteOffset, 3);
|
|
|
|
const resRecord = recordFromBufMinimal(opId, res32);
|
|
|
|
return resRecord.result;
|
|
|
|
}
|