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";
|
2020-03-05 07:05:41 -05:00
|
|
|
import { TextEncoder, TextDecoder } from "./web/text_encoding.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { core } from "./core.ts";
|
2020-02-25 09:14:27 -05:00
|
|
|
import { OPS_CACHE } from "./runtime.ts";
|
2020-02-29 13:04:10 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-27 11:33:39 -04:00
|
|
|
const promiseTable = new Map<number, util.Resolvable<JsonResponse>>();
|
2019-08-23 01:30:14 -04:00
|
|
|
let _nextPromiseId = 1;
|
|
|
|
|
|
|
|
function nextPromiseId(): number {
|
|
|
|
return _nextPromiseId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
function decode(ui8: Uint8Array): JsonResponse {
|
|
|
|
const s = new TextDecoder().decode(ui8);
|
|
|
|
return JSON.parse(s) as JsonResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
function encode(args: object): Uint8Array {
|
|
|
|
const s = JSON.stringify(args);
|
|
|
|
return new TextEncoder().encode(s);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-08-27 11:33:39 -04:00
|
|
|
const promise = promiseTable.get(res.promiseId!);
|
|
|
|
util.assert(promise != null);
|
|
|
|
promiseTable.delete(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.
|
|
|
|
promiseTable.set(promiseId, promise);
|
|
|
|
}
|
2019-08-27 11:33:39 -04:00
|
|
|
|
|
|
|
const res = await promise;
|
|
|
|
return unwrapResponse(res);
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|