2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-08-30 15:35:51 -04:00
|
|
|
import { libdeno } from "./libdeno";
|
2018-10-17 13:04:28 -04:00
|
|
|
import * as flatbuffers from "./flatbuffers";
|
2018-10-03 21:18:23 -04:00
|
|
|
import * as msg from "gen/msg_generated";
|
2018-09-05 22:13:36 -04:00
|
|
|
import * as errors from "./errors";
|
|
|
|
import * as util from "./util";
|
|
|
|
|
|
|
|
let nextCmdId = 0;
|
2018-10-03 21:18:23 -04:00
|
|
|
const promiseTable = new Map<number, util.Resolvable<msg.Base>>();
|
2018-09-05 22:13:36 -04:00
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
export function handleAsyncMsgFromRust(ui8: Uint8Array): void {
|
2019-03-10 15:37:05 -04:00
|
|
|
util.assert(ui8 != null && ui8.length > 0);
|
|
|
|
const bb = new flatbuffers.ByteBuffer(ui8);
|
|
|
|
const base = msg.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-09-05 22:13:36 -04:00
|
|
|
}
|
|
|
|
}
|
2018-08-30 15:35:51 -04:00
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function sendInternal(
|
|
|
|
builder: flatbuffers.Builder,
|
|
|
|
innerType: msg.Any,
|
|
|
|
inner: flatbuffers.Offset,
|
|
|
|
data: undefined | ArrayBufferView,
|
|
|
|
sync = true
|
|
|
|
): [number, null | Uint8Array] {
|
|
|
|
const cmdId = nextCmdId++;
|
|
|
|
msg.Base.startBase(builder);
|
|
|
|
msg.Base.addInner(builder, inner);
|
|
|
|
msg.Base.addInnerType(builder, innerType);
|
|
|
|
msg.Base.addSync(builder, sync);
|
|
|
|
msg.Base.addCmdId(builder, cmdId);
|
|
|
|
builder.finish(msg.Base.endBase(builder));
|
|
|
|
const res = libdeno.send(builder.asUint8Array(), data);
|
|
|
|
builder.inUse = false;
|
|
|
|
return [cmdId, res];
|
|
|
|
}
|
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
// @internal
|
|
|
|
export function sendAsync(
|
|
|
|
builder: flatbuffers.Builder,
|
2018-10-03 21:18:23 -04:00
|
|
|
innerType: msg.Any,
|
2018-10-03 21:12:23 -04:00
|
|
|
inner: flatbuffers.Offset,
|
2018-09-27 17:33:10 -04:00
|
|
|
data?: ArrayBufferView
|
2018-10-03 21:18:23 -04:00
|
|
|
): Promise<msg.Base> {
|
2018-10-03 21:12:23 -04:00
|
|
|
const [cmdId, resBuf] = sendInternal(builder, innerType, inner, data, false);
|
2018-09-05 22:13:36 -04:00
|
|
|
util.assert(resBuf == null);
|
2018-10-03 21:18:23 -04:00
|
|
|
const promise = util.createResolvable<msg.Base>();
|
2018-09-05 22:13:36 -04:00
|
|
|
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,
|
2018-10-03 21:18:23 -04:00
|
|
|
innerType: msg.Any,
|
2018-10-03 21:12:23 -04:00
|
|
|
inner: flatbuffers.Offset,
|
2018-09-27 17:33:10 -04:00
|
|
|
data?: ArrayBufferView
|
2018-10-03 21:18:23 -04:00
|
|
|
): null | msg.Base {
|
2018-10-03 21:12:23 -04:00
|
|
|
const [cmdId, resBuf] = sendInternal(builder, innerType, inner, data, true);
|
2018-09-05 22:13:36 -04:00
|
|
|
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!);
|
|
|
|
const bb = new flatbuffers.ByteBuffer(u8);
|
2018-10-03 21:18:23 -04:00
|
|
|
const baseRes = msg.Base.getRootAsBase(bb);
|
2018-09-05 22:13:36 -04:00
|
|
|
errors.maybeThrowError(baseRes);
|
2018-08-30 15:35:51 -04:00
|
|
|
return baseRes;
|
|
|
|
}
|
|
|
|
}
|