summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/dispatch.ts12
-rw-r--r--js/dispatch_minimal.ts45
2 files changed, 27 insertions, 30 deletions
diff --git a/js/dispatch.ts b/js/dispatch.ts
index cd11c93f6..babea5739 100644
--- a/js/dispatch.ts
+++ b/js/dispatch.ts
@@ -10,6 +10,11 @@ import {
handleAsyncMsgFromRustMinimal
} from "./dispatch_minimal";
+// TODO(ry) Currently we only use three values for opId: OP_READ, OP_WRITE,
+// FLATBUFFER_OP_ID. Later on use opId for actual individual ops, not just
+// classes of ops.
+const FLATBUFFER_OP_ID = 44;
+
const promiseTable = new Map<number, util.Resolvable<msg.Base>>();
interface FlatbufferRecord {
@@ -26,11 +31,11 @@ function flatbufferRecordFromBuf(buf: Uint8Array): FlatbufferRecord {
};
}
-export function handleAsyncMsgFromRust(ui8: Uint8Array): void {
+export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void {
const buf32 = new Int32Array(ui8.buffer, ui8.byteOffset, ui8.byteLength / 4);
- const recordMin = recordFromBufMinimal(buf32);
- if (recordMin) {
+ if (opId !== FLATBUFFER_OP_ID) {
// Fast and new
+ const recordMin = recordFromBufMinimal(opId, buf32);
handleAsyncMsgFromRustMinimal(ui8, recordMin);
} else {
// Legacy
@@ -83,6 +88,7 @@ function sendInternal(
const control = builder.asUint8Array();
const response = core.dispatch(
+ FLATBUFFER_OP_ID, // TODO(ry) Use actual opId later.
control,
zeroCopy ? ui8FromArrayBufferView(zeroCopy) : undefined
);
diff --git a/js/dispatch_minimal.ts b/js/dispatch_minimal.ts
index 17d328110..df0a290b2 100644
--- a/js/dispatch_minimal.ts
+++ b/js/dispatch_minimal.ts
@@ -3,7 +3,6 @@
import * as util from "./util";
import { core } from "./core";
-const DISPATCH_MINIMAL_TOKEN = 0xcafe;
const promiseTableMin = new Map<number, util.Resolvable<number>>();
let _nextPromiseId = 0;
@@ -13,31 +12,27 @@ export function nextPromiseId(): number {
export interface RecordMinimal {
promiseId: number;
- opId: number;
+ opId: number; // Maybe better called dispatchId
arg: number;
result: number;
}
-/** Determines if a message has the "minimal" serialization format. If false, it
- * is flatbuffer encoded.
- */
-export function hasMinimalToken(i32: Int32Array): boolean {
- return i32[0] == DISPATCH_MINIMAL_TOKEN;
-}
-
-export function recordFromBufMinimal(buf32: Int32Array): null | RecordMinimal {
- if (hasMinimalToken(buf32)) {
- return {
- promiseId: buf32[1],
- opId: buf32[2],
- arg: buf32[3],
- result: buf32[4]
- };
+export function recordFromBufMinimal(
+ opId: number,
+ buf32: Int32Array
+): RecordMinimal {
+ if (buf32.length != 3) {
+ throw Error("Bad message");
}
- return null;
+ return {
+ promiseId: buf32[0],
+ opId,
+ arg: buf32[1],
+ result: buf32[2]
+ };
}
-const scratch32 = new Int32Array(5);
+const scratch32 = new Int32Array(3);
const scratchBytes = new Uint8Array(
scratch32.buffer,
scratch32.byteOffset,
@@ -63,15 +58,11 @@ export function sendAsyncMinimal(
zeroCopy: Uint8Array
): Promise<number> {
const promiseId = nextPromiseId(); // AKA cmdId
-
- scratch32[0] = DISPATCH_MINIMAL_TOKEN;
- scratch32[1] = promiseId;
- scratch32[2] = opId;
- scratch32[3] = arg;
-
+ scratch32[0] = promiseId;
+ scratch32[1] = arg;
+ scratch32[2] = 0; // result
const promise = util.createResolvable<number>();
promiseTableMin.set(promiseId, promise);
-
- core.dispatch(scratchBytes, zeroCopy);
+ core.dispatch(opId, scratchBytes, zeroCopy);
return promise;
}