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
|
|
|
|
2018-10-02 20:47:40 -04:00
|
|
|
let fireTimers: () => void;
|
|
|
|
|
|
|
|
export function setFireTimersCallback(fn: () => void) {
|
|
|
|
fireTimers = fn;
|
|
|
|
}
|
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
export function handleAsyncMsgFromRust(ui8: Uint8Array) {
|
2018-10-02 20:47:40 -04:00
|
|
|
// If a the buffer is empty, recv() on the native side timed out and we
|
|
|
|
// did not receive a message.
|
2019-02-26 17:36:05 -05:00
|
|
|
if (ui8 && ui8.length) {
|
2018-10-02 20:47:40 -04:00
|
|
|
const bb = new flatbuffers.ByteBuffer(ui8);
|
2018-10-03 21:18:23 -04:00
|
|
|
const base = msg.Base.getRootAsBase(bb);
|
2018-10-02 20:47:40 -04:00
|
|
|
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-10-02 20:47:40 -04:00
|
|
|
// Fire timers that have become runnable.
|
|
|
|
fireTimers();
|
2018-09-05 22:13:36 -04:00
|
|
|
}
|
2018-08-30 15:35:51 -04:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 22:13:36 -04:00
|
|
|
|
|
|
|
function sendInternal(
|
|
|
|
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: undefined | ArrayBufferView,
|
2018-09-05 22:13:36 -04:00
|
|
|
sync = true
|
|
|
|
): [number, null | Uint8Array] {
|
|
|
|
const cmdId = nextCmdId++;
|
2018-10-03 21:18:23 -04:00
|
|
|
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));
|
2018-10-17 13:04:28 -04:00
|
|
|
const res = libdeno.send(builder.asUint8Array(), data);
|
|
|
|
builder.inUse = false;
|
|
|
|
return [cmdId, res];
|
2018-09-05 22:13:36 -04:00
|
|
|
}
|