2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-14 19:17:52 -04:00
|
|
|
import { window } from "./window";
|
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
|
|
|
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,
|
2019-03-14 19:17:52 -04:00
|
|
|
zeroCopy: undefined | ArrayBufferView,
|
2019-03-09 12:30:38 -05:00
|
|
|
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));
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
const control = builder.asUint8Array();
|
|
|
|
const response = window.DenoCore.dispatch(control, zeroCopy);
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
builder.inUse = false;
|
2019-03-14 19:17:52 -04:00
|
|
|
return [cmdId, response];
|
2019-03-09 12:30:38 -05: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> {
|
2019-03-14 19:17:52 -04:00
|
|
|
const [cmdId, response] = sendInternal(
|
|
|
|
builder,
|
|
|
|
innerType,
|
|
|
|
inner,
|
|
|
|
data,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
util.assert(response == 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 {
|
2019-03-14 19:17:52 -04:00
|
|
|
const [cmdId, response] = sendInternal(builder, innerType, inner, data, true);
|
2018-09-05 22:13:36 -04:00
|
|
|
util.assert(cmdId >= 0);
|
2019-03-14 19:17:52 -04:00
|
|
|
if (response == null || response.length === 0) {
|
2018-08-30 15:35:51 -04:00
|
|
|
return null;
|
|
|
|
} else {
|
2019-03-14 19:17:52 -04:00
|
|
|
const bb = new flatbuffers.ByteBuffer(response);
|
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;
|
|
|
|
}
|
|
|
|
}
|