2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
import * as util from "./util.ts";
|
|
|
|
import { core } from "./core.ts";
|
2020-03-05 07:05:41 -05:00
|
|
|
import { TextDecoder } from "./web/text_encoding.ts";
|
2020-02-29 13:04:10 -05:00
|
|
|
import { ErrorKind, errors, getErrorClass } from "./errors.ts";
|
2019-05-03 00:06:43 -04:00
|
|
|
|
2019-10-24 17:22:31 -04:00
|
|
|
const promiseTableMin = new Map<number, util.Resolvable<RecordMinimal>>();
|
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-10-24 17:22:31 -04:00
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
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-05-03 00:06:43 -04:00
|
|
|
arg: number;
|
|
|
|
result: number;
|
2019-10-24 17:22:31 -04:00
|
|
|
err?: {
|
|
|
|
kind: ErrorKind;
|
|
|
|
message: string;
|
|
|
|
};
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
|
|
|
|
2020-01-17 08:26:11 -05:00
|
|
|
export function recordFromBufMinimal(ui8: Uint8Array): RecordMinimal {
|
2020-03-01 17:17:59 -05:00
|
|
|
const header = ui8.subarray(0, 12);
|
2019-10-24 17:22:31 -04:00
|
|
|
const buf32 = new Int32Array(
|
|
|
|
header.buffer,
|
|
|
|
header.byteOffset,
|
|
|
|
header.byteLength / 4
|
|
|
|
);
|
|
|
|
const promiseId = buf32[0];
|
|
|
|
const arg = buf32[1];
|
|
|
|
const result = buf32[2];
|
|
|
|
let err;
|
|
|
|
|
|
|
|
if (arg < 0) {
|
|
|
|
const kind = result as ErrorKind;
|
2020-03-01 17:17:59 -05:00
|
|
|
const message = decoder.decode(ui8.subarray(12));
|
2019-10-24 17:22:31 -04:00
|
|
|
err = { kind, message };
|
|
|
|
} else if (ui8.length != 12) {
|
2020-02-24 15:48:35 -05:00
|
|
|
throw new errors.InvalidData("BadMessage");
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
2019-10-24 17:22:31 -04:00
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
return {
|
2019-10-24 17:22:31 -04:00
|
|
|
promiseId,
|
|
|
|
arg,
|
|
|
|
result,
|
|
|
|
err
|
2019-08-07 14:02:29 -04:00
|
|
|
};
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 17:22:31 -04:00
|
|
|
function unwrapResponse(res: RecordMinimal): number {
|
|
|
|
if (res.err != null) {
|
2020-02-29 13:04:10 -05:00
|
|
|
throw new (getErrorClass(res.err.kind))(res.err.message);
|
2019-10-24 17:22:31 -04:00
|
|
|
}
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2020-01-17 08:26:11 -05:00
|
|
|
export function asyncMsgFromRust(ui8: Uint8Array): void {
|
|
|
|
const record = recordFromBufMinimal(ui8);
|
2019-10-24 17:22:31 -04:00
|
|
|
const { promiseId } = record;
|
2019-05-03 00:06:43 -04:00
|
|
|
const promise = promiseTableMin.get(promiseId);
|
|
|
|
promiseTableMin.delete(promiseId);
|
2019-11-13 13:42:34 -05:00
|
|
|
util.assert(promise);
|
|
|
|
promise.resolve(record);
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 17:22:31 -04:00
|
|
|
export async function sendAsyncMinimal(
|
2019-05-03 00:06:43 -04:00
|
|
|
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-10-24 17:22:31 -04:00
|
|
|
const promise = util.createResolvable<RecordMinimal>();
|
2019-10-14 17:46:27 -04:00
|
|
|
const buf = core.dispatch(opId, scratchBytes, zeroCopy);
|
|
|
|
if (buf) {
|
2020-01-17 08:26:11 -05:00
|
|
|
const record = recordFromBufMinimal(buf);
|
2019-10-14 17:46:27 -04:00
|
|
|
// Sync result.
|
2019-10-24 17:22:31 -04:00
|
|
|
promise.resolve(record);
|
2019-10-14 17:46:27 -04:00
|
|
|
} else {
|
|
|
|
// Async result.
|
|
|
|
promiseTableMin.set(promiseId, promise);
|
|
|
|
}
|
2019-10-24 17:22:31 -04:00
|
|
|
|
|
|
|
const res = await promise;
|
|
|
|
return unwrapResponse(res);
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
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)!;
|
2020-01-17 08:26:11 -05:00
|
|
|
const resRecord = recordFromBufMinimal(res);
|
2019-10-24 17:22:31 -04:00
|
|
|
return unwrapResponse(resRecord);
|
2019-08-26 10:58:44 -04:00
|
|
|
}
|