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";
|
2019-10-24 17:22:31 -04:00
|
|
|
import { TextDecoder } from "./text_encoding.ts";
|
|
|
|
import { ErrorKind, DenoError } 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-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-10-24 17:22:31 -04:00
|
|
|
err?: {
|
|
|
|
kind: ErrorKind;
|
|
|
|
message: string;
|
|
|
|
};
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
export function recordFromBufMinimal(
|
|
|
|
opId: number,
|
2019-10-24 17:22:31 -04:00
|
|
|
ui8: Uint8Array
|
2019-08-07 14:02:29 -04:00
|
|
|
): RecordMinimal {
|
2019-10-24 17:22:31 -04:00
|
|
|
const header = ui8.slice(0, 12);
|
|
|
|
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;
|
|
|
|
const message = decoder.decode(ui8.slice(12));
|
|
|
|
err = { kind, message };
|
|
|
|
} else if (ui8.length != 12) {
|
|
|
|
err = { kind: ErrorKind.InvalidData, message: "Bad message" };
|
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,
|
2019-08-07 14:02:29 -04:00
|
|
|
opId,
|
2019-10-24 17:22:31 -04:00
|
|
|
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) {
|
|
|
|
throw new DenoError(res.err!.kind, res.err!.message);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2019-08-23 01:30:14 -04:00
|
|
|
export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
|
2019-10-24 17:22:31 -04:00
|
|
|
const record = recordFromBufMinimal(opId, ui8);
|
|
|
|
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) {
|
2019-10-24 17:22:31 -04:00
|
|
|
const record = recordFromBufMinimal(opId, 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)!;
|
2019-10-24 17:22:31 -04:00
|
|
|
const resRecord = recordFromBufMinimal(opId, res);
|
|
|
|
return unwrapResponse(resRecord);
|
2019-08-26 10:58:44 -04:00
|
|
|
}
|