2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-26 08:22:07 -04:00
|
|
|
import { core } from "./core";
|
2018-10-17 13:04:28 -04:00
|
|
|
import * as flatbuffers from "./flatbuffers";
|
2019-03-30 14:45:36 -04:00
|
|
|
import * as msg from "gen/cli/msg_generated";
|
2018-09-05 22:13:36 -04:00
|
|
|
import * as errors from "./errors";
|
|
|
|
import * as util from "./util";
|
2019-05-03 00:06:43 -04:00
|
|
|
import {
|
|
|
|
recordFromBufMinimal,
|
|
|
|
handleAsyncMsgFromRustMinimal
|
|
|
|
} from "./dispatch_minimal";
|
2018-09-05 22:13:36 -04:00
|
|
|
|
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-06-13 23:43:54 -04:00
|
|
|
function flatbufferRecordFromBuf(buf: Uint8Array): msg.Base {
|
2019-05-03 00:06:43 -04:00
|
|
|
const bb = new flatbuffers.ByteBuffer(buf);
|
2019-03-10 15:37:05 -04:00
|
|
|
const base = msg.Base.getRootAsBase(bb);
|
2019-06-13 23:43:54 -04:00
|
|
|
return base;
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
|
|
|
|
2019-06-13 23:43:54 -04:00
|
|
|
export function handleAsyncMsgFromRust(
|
|
|
|
promiseId: number,
|
|
|
|
ui8: Uint8Array
|
|
|
|
): void {
|
2019-05-03 00:06:43 -04:00
|
|
|
const buf32 = new Int32Array(ui8.buffer, ui8.byteOffset, ui8.byteLength / 4);
|
|
|
|
const recordMin = recordFromBufMinimal(buf32);
|
|
|
|
if (recordMin) {
|
|
|
|
// Fast and new
|
2019-06-13 23:43:54 -04:00
|
|
|
handleAsyncMsgFromRustMinimal(promiseId, ui8, recordMin);
|
2019-03-10 15:37:05 -04:00
|
|
|
} else {
|
2019-05-03 00:06:43 -04:00
|
|
|
// Legacy
|
2019-06-13 23:43:54 -04:00
|
|
|
let base = flatbufferRecordFromBuf(ui8);
|
2019-05-03 00:06:43 -04:00
|
|
|
const promise = promiseTable.get(promiseId);
|
|
|
|
util.assert(promise != null, `Expecting promise in table. ${promiseId}`);
|
|
|
|
promiseTable.delete(promiseId);
|
|
|
|
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-05-03 00:06:43 -04:00
|
|
|
function ui8FromArrayBufferView(abv: ArrayBufferView): Uint8Array {
|
|
|
|
return new Uint8Array(abv.buffer, abv.byteOffset, abv.byteLength);
|
|
|
|
}
|
|
|
|
|
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-06-13 23:43:54 -04:00
|
|
|
isSync: true
|
|
|
|
): Uint8Array | null;
|
|
|
|
function sendInternal(
|
|
|
|
builder: flatbuffers.Builder,
|
|
|
|
innerType: msg.Any,
|
|
|
|
inner: flatbuffers.Offset,
|
|
|
|
zeroCopy: undefined | ArrayBufferView,
|
|
|
|
isSync: false
|
|
|
|
): Promise<msg.Base>;
|
|
|
|
function sendInternal(
|
|
|
|
builder: flatbuffers.Builder,
|
|
|
|
innerType: msg.Any,
|
|
|
|
inner: flatbuffers.Offset,
|
|
|
|
zeroCopy: undefined | ArrayBufferView,
|
|
|
|
isSync: boolean
|
|
|
|
): Promise<msg.Base> | Uint8Array | null {
|
2019-05-03 00:06:43 -04:00
|
|
|
msg.Base.startBase(builder);
|
2019-06-13 23:43:54 -04:00
|
|
|
msg.Base.addSync(builder, isSync);
|
2019-05-03 00:06:43 -04:00
|
|
|
msg.Base.addInner(builder, inner);
|
|
|
|
msg.Base.addInnerType(builder, innerType);
|
|
|
|
builder.finish(msg.Base.endBase(builder));
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
const control = builder.asUint8Array();
|
2019-05-03 00:06:43 -04:00
|
|
|
|
|
|
|
const response = core.dispatch(
|
|
|
|
control,
|
|
|
|
zeroCopy ? ui8FromArrayBufferView(zeroCopy) : undefined
|
|
|
|
);
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
builder.inUse = false;
|
2019-06-13 23:43:54 -04:00
|
|
|
|
|
|
|
if (typeof response === "number") {
|
|
|
|
const promise = util.createResolvable<msg.Base>();
|
|
|
|
promiseTable.set(response, promise);
|
|
|
|
util.assert(!isSync);
|
|
|
|
return promise;
|
|
|
|
} else {
|
|
|
|
if (!isSync) {
|
|
|
|
util.assert(response !== null);
|
|
|
|
const base = flatbufferRecordFromBuf(response as Uint8Array);
|
|
|
|
const err = errors.maybeError(base);
|
|
|
|
if (err != null) {
|
|
|
|
return Promise.reject(err);
|
|
|
|
} else {
|
|
|
|
return Promise.resolve(base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 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-06-13 23:43:54 -04:00
|
|
|
const promise = sendInternal(builder, innerType, inner, data, false);
|
2018-09-05 22:13:36 -04:00
|
|
|
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-06-13 23:43:54 -04:00
|
|
|
const response = sendInternal(builder, innerType, inner, data, true);
|
|
|
|
if (response == null || response.length === 0) {
|
2018-08-30 15:35:51 -04:00
|
|
|
return null;
|
|
|
|
} else {
|
2019-06-08 18:11:47 -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;
|
|
|
|
}
|
|
|
|
}
|