2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-08 08:09:22 -04:00
|
|
|
import * as util from "../util.ts";
|
|
|
|
import { core } from "../core.ts";
|
|
|
|
import { OPS_CACHE } from "../runtime.ts";
|
|
|
|
import { ErrorKind, getErrorClass } from "../errors.ts";
|
2019-08-23 01:30:14 -04:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
type Ok = any;
|
|
|
|
|
|
|
|
interface JsonError {
|
|
|
|
kind: ErrorKind;
|
|
|
|
message: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface JsonResponse {
|
|
|
|
ok?: Ok;
|
|
|
|
err?: JsonError;
|
2019-08-27 11:33:39 -04:00
|
|
|
promiseId?: number; // Only present in async messages.
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
2020-03-09 22:04:49 -04:00
|
|
|
// Using an object without a prototype because `Map` was causing GC problems.
|
|
|
|
const promiseTable: {
|
|
|
|
[key: number]: util.Resolvable<JsonResponse>;
|
|
|
|
} = Object.create(null);
|
2019-08-23 01:30:14 -04:00
|
|
|
let _nextPromiseId = 1;
|
|
|
|
|
|
|
|
function nextPromiseId(): number {
|
|
|
|
return _nextPromiseId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
function decode(ui8: Uint8Array): JsonResponse {
|
2020-03-15 10:31:55 -04:00
|
|
|
const s = core.decode(ui8);
|
2019-08-23 01:30:14 -04:00
|
|
|
return JSON.parse(s) as JsonResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
function encode(args: object): Uint8Array {
|
|
|
|
const s = JSON.stringify(args);
|
2020-03-15 10:31:55 -04:00
|
|
|
return core.encode(s);
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
2019-08-27 11:33:39 -04:00
|
|
|
function unwrapResponse(res: JsonResponse): Ok {
|
|
|
|
if (res.err != null) {
|
2020-02-29 13:04:10 -05:00
|
|
|
throw new (getErrorClass(res.err.kind))(res.err.message);
|
2019-08-27 11:33:39 -04:00
|
|
|
}
|
|
|
|
util.assert(res.ok != null);
|
2019-11-13 13:42:34 -05:00
|
|
|
return res.ok;
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
2020-01-17 08:26:11 -05:00
|
|
|
export function asyncMsgFromRust(resUi8: Uint8Array): void {
|
2019-08-27 11:33:39 -04:00
|
|
|
const res = decode(resUi8);
|
|
|
|
util.assert(res.promiseId != null);
|
2019-08-23 01:30:14 -04:00
|
|
|
|
2020-03-09 22:04:49 -04:00
|
|
|
const promise = promiseTable[res.promiseId!];
|
2019-08-27 11:33:39 -04:00
|
|
|
util.assert(promise != null);
|
2020-03-09 22:04:49 -04:00
|
|
|
delete promiseTable[res.promiseId!];
|
2019-11-13 13:42:34 -05:00
|
|
|
promise.resolve(res);
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sendSync(
|
2020-02-25 09:14:27 -05:00
|
|
|
opName: string,
|
2019-08-23 01:30:14 -04:00
|
|
|
args: object = {},
|
|
|
|
zeroCopy?: Uint8Array
|
|
|
|
): Ok {
|
2020-02-25 09:14:27 -05:00
|
|
|
const opId = OPS_CACHE[opName];
|
|
|
|
util.log("sendSync", opName, opId);
|
2019-08-23 01:30:14 -04:00
|
|
|
const argsUi8 = encode(args);
|
2019-08-27 11:33:39 -04:00
|
|
|
const resUi8 = core.dispatch(opId, argsUi8, zeroCopy);
|
|
|
|
util.assert(resUi8 != null);
|
|
|
|
|
2020-01-22 14:18:01 -05:00
|
|
|
const res = decode(resUi8);
|
2019-08-27 11:33:39 -04:00
|
|
|
util.assert(res.promiseId == null);
|
|
|
|
return unwrapResponse(res);
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
2019-08-27 11:33:39 -04:00
|
|
|
export async function sendAsync(
|
2020-02-25 09:14:27 -05:00
|
|
|
opName: string,
|
2019-08-23 01:30:14 -04:00
|
|
|
args: object = {},
|
|
|
|
zeroCopy?: Uint8Array
|
|
|
|
): Promise<Ok> {
|
2020-02-25 09:14:27 -05:00
|
|
|
const opId = OPS_CACHE[opName];
|
|
|
|
util.log("sendAsync", opName, opId);
|
2019-08-23 01:30:14 -04:00
|
|
|
const promiseId = nextPromiseId();
|
|
|
|
args = Object.assign(args, { promiseId });
|
|
|
|
const promise = util.createResolvable<Ok>();
|
2019-08-27 11:33:39 -04:00
|
|
|
|
|
|
|
const argsUi8 = encode(args);
|
2019-10-14 17:46:27 -04:00
|
|
|
const buf = core.dispatch(opId, argsUi8, zeroCopy);
|
|
|
|
if (buf) {
|
|
|
|
// Sync result.
|
|
|
|
const res = decode(buf);
|
|
|
|
promise.resolve(res);
|
|
|
|
} else {
|
|
|
|
// Async result.
|
2020-03-09 22:04:49 -04:00
|
|
|
promiseTable[promiseId] = promise;
|
2019-10-14 17:46:27 -04:00
|
|
|
}
|
2019-08-27 11:33:39 -04:00
|
|
|
|
|
|
|
const res = await promise;
|
|
|
|
return unwrapResponse(res);
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|