2019-08-23 01:30:14 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import * as util from "./util";
|
|
|
|
import { TextEncoder, TextDecoder } from "./text_encoding";
|
|
|
|
import { core } from "./core";
|
2019-08-26 17:02:34 -04:00
|
|
|
import { ErrorKind, DenoError } from "./errors";
|
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) {
|
|
|
|
throw new DenoError(res.err!.kind, res.err!.message);
|
|
|
|
}
|
|
|
|
util.assert(res.ok != null);
|
|
|
|
return res.ok!;
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
2019-08-27 11:33:39 -04:00
|
|
|
export function asyncMsgFromRust(opId: number, resUi8: Uint8Array): void {
|
|
|
|
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!);
|
|
|
|
promise!.resolve(res);
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sendSync(
|
|
|
|
opId: number,
|
|
|
|
args: object = {},
|
|
|
|
zeroCopy?: Uint8Array
|
|
|
|
): Ok {
|
|
|
|
const argsUi8 = encode(args);
|
2019-08-27 11:33:39 -04:00
|
|
|
const resUi8 = core.dispatch(opId, argsUi8, zeroCopy);
|
|
|
|
util.assert(resUi8 != null);
|
|
|
|
|
|
|
|
const res = decode(resUi8!);
|
|
|
|
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(
|
2019-08-23 01:30:14 -04:00
|
|
|
opId: number,
|
|
|
|
args: object = {},
|
|
|
|
zeroCopy?: Uint8Array
|
|
|
|
): Promise<Ok> {
|
|
|
|
const promiseId = nextPromiseId();
|
|
|
|
args = Object.assign(args, { promiseId });
|
|
|
|
const promise = util.createResolvable<Ok>();
|
|
|
|
promiseTable.set(promiseId, promise);
|
2019-08-27 11:33:39 -04:00
|
|
|
|
|
|
|
const argsUi8 = encode(args);
|
|
|
|
const resUi8 = core.dispatch(opId, argsUi8, zeroCopy);
|
|
|
|
util.assert(resUi8 == null);
|
|
|
|
|
|
|
|
const res = await promise;
|
|
|
|
return unwrapResponse(res);
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|