2018-08-30 15:35:51 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { libdeno } from "./libdeno";
|
|
|
|
import { flatbuffers } from "flatbuffers";
|
2018-09-09 19:21:22 -04:00
|
|
|
import * as fbs from "gen/msg_generated";
|
2018-09-05 22:13:36 -04:00
|
|
|
import * as errors from "./errors";
|
|
|
|
import * as util from "./util";
|
2018-09-22 03:59:26 -04:00
|
|
|
import { maybePushTrace } from "./trace";
|
2018-09-05 22:13:36 -04:00
|
|
|
|
|
|
|
let nextCmdId = 0;
|
|
|
|
const promiseTable = new Map<number, util.Resolvable<fbs.Base>>();
|
|
|
|
|
|
|
|
export function handleAsyncMsgFromRust(ui8: Uint8Array) {
|
|
|
|
const bb = new flatbuffers.ByteBuffer(ui8);
|
|
|
|
const base = fbs.Base.getRootAsBase(bb);
|
|
|
|
const cmdId = base.cmdId();
|
|
|
|
const promise = promiseTable.get(cmdId);
|
|
|
|
util.assert(promise != null, `Expecting promise in table. ${cmdId}`);
|
|
|
|
promiseTable.delete(cmdId);
|
|
|
|
const err = errors.maybeError(base);
|
|
|
|
if (err != null) {
|
|
|
|
promise!.reject(err);
|
|
|
|
} else {
|
|
|
|
promise!.resolve(base);
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 15:35:51 -04:00
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
// @internal
|
|
|
|
export function sendAsync(
|
|
|
|
builder: flatbuffers.Builder,
|
|
|
|
msgType: fbs.Any,
|
|
|
|
msg: flatbuffers.Offset
|
|
|
|
): Promise<fbs.Base> {
|
2018-09-22 03:59:26 -04:00
|
|
|
maybePushTrace(msgType, false); // add to trace if tracing
|
2018-09-05 22:13:36 -04:00
|
|
|
const [cmdId, resBuf] = sendInternal(builder, msgType, msg, false);
|
|
|
|
util.assert(resBuf == null);
|
|
|
|
const promise = util.createResolvable<fbs.Base>();
|
|
|
|
promiseTable.set(cmdId, promise);
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-09-09 18:54:42 -04:00
|
|
|
export function sendSync(
|
2018-08-30 15:35:51 -04:00
|
|
|
builder: flatbuffers.Builder,
|
|
|
|
msgType: fbs.Any,
|
|
|
|
msg: flatbuffers.Offset
|
|
|
|
): null | fbs.Base {
|
2018-09-22 03:59:26 -04:00
|
|
|
maybePushTrace(msgType, true); // add to trace if tracing
|
2018-09-05 22:13:36 -04:00
|
|
|
const [cmdId, resBuf] = sendInternal(builder, msgType, msg, true);
|
|
|
|
util.assert(cmdId >= 0);
|
2018-08-30 15:35:51 -04:00
|
|
|
if (resBuf == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2018-09-05 22:13:36 -04:00
|
|
|
const u8 = new Uint8Array(resBuf!);
|
|
|
|
// console.log("recv sync message", util.hexdump(u8));
|
|
|
|
const bb = new flatbuffers.ByteBuffer(u8);
|
2018-08-30 15:35:51 -04:00
|
|
|
const baseRes = fbs.Base.getRootAsBase(bb);
|
2018-09-05 22:13:36 -04:00
|
|
|
errors.maybeThrowError(baseRes);
|
2018-08-30 15:35:51 -04:00
|
|
|
return baseRes;
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 22:13:36 -04:00
|
|
|
|
|
|
|
function sendInternal(
|
|
|
|
builder: flatbuffers.Builder,
|
|
|
|
msgType: fbs.Any,
|
|
|
|
msg: flatbuffers.Offset,
|
|
|
|
sync = true
|
|
|
|
): [number, null | Uint8Array] {
|
|
|
|
const cmdId = nextCmdId++;
|
|
|
|
fbs.Base.startBase(builder);
|
|
|
|
fbs.Base.addMsg(builder, msg);
|
|
|
|
fbs.Base.addMsgType(builder, msgType);
|
|
|
|
fbs.Base.addSync(builder, sync);
|
|
|
|
fbs.Base.addCmdId(builder, cmdId);
|
|
|
|
builder.finish(fbs.Base.endBase(builder));
|
|
|
|
|
|
|
|
return [cmdId, libdeno.send(builder.asUint8Array())];
|
|
|
|
}
|