0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-31 09:14:20 -04:00
denoland-deno/js/dispatch.ts

141 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-01-21 14:03:30 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { core } from "./core";
import * as flatbuffers from "./flatbuffers";
import * as msg from "gen/cli/msg_generated";
import * as errors from "./errors";
import * as util from "./util";
2019-05-03 00:06:43 -04:00
import {
nextPromiseId,
2019-05-03 00:06:43 -04:00
recordFromBufMinimal,
handleAsyncMsgFromRustMinimal
} from "./dispatch_minimal";
2018-10-03 21:18:23 -04:00
const promiseTable = new Map<number, util.Resolvable<msg.Base>>();
interface FlatbufferRecord {
promiseId: number;
base: msg.Base;
}
function flatbufferRecordFromBuf(buf: Uint8Array): FlatbufferRecord {
2019-05-03 00:06:43 -04:00
const bb = new flatbuffers.ByteBuffer(buf);
const base = msg.Base.getRootAsBase(bb);
return {
promiseId: base.cmdId(),
base
};
2019-05-03 00:06:43 -04:00
}
export function handleAsyncMsgFromRust(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
handleAsyncMsgFromRustMinimal(ui8, recordMin);
} else {
2019-05-03 00:06:43 -04:00
// Legacy
let { promiseId, 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);
}
}
}
2019-05-03 00:06:43 -04:00
function ui8FromArrayBufferView(abv: ArrayBufferView): Uint8Array {
return new Uint8Array(abv.buffer, abv.byteOffset, abv.byteLength);
}
function sendInternal(
builder: flatbuffers.Builder,
innerType: msg.Any,
inner: flatbuffers.Offset,
zeroCopy: undefined | ArrayBufferView,
2019-06-17 21:02:08 -04:00
isSync: true
): Uint8Array;
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 {
const cmdId = nextPromiseId();
2019-05-03 00:06:43 -04:00
msg.Base.startBase(builder);
msg.Base.addInner(builder, inner);
msg.Base.addInnerType(builder, innerType);
2019-06-17 21:02:08 -04:00
msg.Base.addSync(builder, isSync);
msg.Base.addCmdId(builder, cmdId);
2019-05-03 00:06:43 -04:00
builder.finish(msg.Base.endBase(builder));
const control = builder.asUint8Array();
2019-05-03 00:06:43 -04:00
const response = core.dispatch(
control,
zeroCopy ? ui8FromArrayBufferView(zeroCopy) : undefined
);
builder.inUse = false;
2019-06-17 21:02:08 -04:00
if (response == null) {
util.assert(!isSync);
const promise = util.createResolvable<msg.Base>();
promiseTable.set(cmdId, promise);
return promise;
} else {
if (!isSync) {
// We can easily and correctly allow for sync responses to async calls
// by creating and returning a promise from the sync response.
const bb = new flatbuffers.ByteBuffer(response);
const base = msg.Base.getRootAsBase(bb);
const err = errors.maybeError(base);
if (err != null) {
return Promise.reject(err);
} else {
return Promise.resolve(base);
}
}
return response;
}
}
// @internal
export function sendAsync(
builder: flatbuffers.Builder,
2018-10-03 21:18:23 -04:00
innerType: msg.Any,
inner: flatbuffers.Offset,
data?: ArrayBufferView
2018-10-03 21:18:23 -04:00
): Promise<msg.Base> {
2019-06-17 21:02:08 -04:00
return sendInternal(builder, innerType, inner, data, false);
}
// @internal
export function sendSync(
builder: flatbuffers.Builder,
2018-10-03 21:18:23 -04:00
innerType: msg.Any,
inner: flatbuffers.Offset,
data?: ArrayBufferView
2018-10-03 21:18:23 -04:00
): null | msg.Base {
2019-06-17 21:02:08 -04:00
const response = sendInternal(builder, innerType, inner, data, true);
if (response!.length === 0) {
return null;
} else {
const bb = new flatbuffers.ByteBuffer(response!);
2018-10-03 21:18:23 -04:00
const baseRes = msg.Base.getRootAsBase(bb);
errors.maybeThrowError(baseRes);
return baseRes;
}
}