summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/dispatch.ts6
-rw-r--r--js/dispatch_minimal.ts13
-rw-r--r--js/files.ts56
3 files changed, 41 insertions, 34 deletions
diff --git a/js/dispatch.ts b/js/dispatch.ts
index 6edd2981c..babea5739 100644
--- a/js/dispatch.ts
+++ b/js/dispatch.ts
@@ -32,13 +32,9 @@ function flatbufferRecordFromBuf(buf: Uint8Array): FlatbufferRecord {
}
export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void {
+ const buf32 = new Int32Array(ui8.buffer, ui8.byteOffset, ui8.byteLength / 4);
if (opId !== FLATBUFFER_OP_ID) {
// Fast and new
- const buf32 = new Int32Array(
- ui8.buffer,
- ui8.byteOffset,
- ui8.byteLength / 4
- );
const recordMin = recordFromBufMinimal(opId, buf32);
handleAsyncMsgFromRustMinimal(ui8, recordMin);
} else {
diff --git a/js/dispatch_minimal.ts b/js/dispatch_minimal.ts
index 483342127..df0a290b2 100644
--- a/js/dispatch_minimal.ts
+++ b/js/dispatch_minimal.ts
@@ -52,19 +52,6 @@ export function handleAsyncMsgFromRustMinimal(
promise!.resolve(result);
}
-export function sendSyncMinimal(
- opId: number,
- arg: number,
- zeroCopy: Uint8Array
-): number {
- scratch32[0] = 0; // promiseId 0 indicates sync
- scratch32[1] = arg;
- const res = core.dispatch(opId, scratchBytes, zeroCopy)!;
- const res32 = new Int32Array(res.buffer, res.byteOffset, 3);
- const resRecord = recordFromBufMinimal(opId, res32);
- return resRecord.result;
-}
-
export function sendAsyncMinimal(
opId: number,
arg: number,
diff --git a/js/files.ts b/js/files.ts
index 1582d8fd4..eb899d738 100644
--- a/js/files.ts
+++ b/js/files.ts
@@ -11,12 +11,11 @@ import {
SyncSeeker
} from "./io";
import * as dispatch from "./dispatch";
-import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal";
+import { sendAsyncMinimal } from "./dispatch_minimal";
import * as msg from "gen/cli/msg_generated";
import { assert } from "./util";
import * as flatbuffers from "./flatbuffers";
-// Warning: These constants defined in two places. Here and in cli/ops/mod.rs.
const OP_READ = 1;
const OP_WRITE = 2;
@@ -63,6 +62,26 @@ export async function open(
return resOpen(await dispatch.sendAsync(...reqOpen(filename, mode)));
}
+function reqRead(
+ rid: number,
+ p: Uint8Array
+): [flatbuffers.Builder, msg.Any, flatbuffers.Offset, Uint8Array] {
+ const builder = flatbuffers.createBuilder();
+ const inner = msg.Read.createRead(builder, rid);
+ return [builder, msg.Any.Read, inner, p];
+}
+
+function resRead(baseRes: null | msg.Base): number | EOF {
+ assert(baseRes != null);
+ assert(msg.Any.ReadRes === baseRes!.innerType());
+ const res = new msg.ReadRes();
+ assert(baseRes!.inner(res) != null);
+ if (res.eof()) {
+ return EOF;
+ }
+ return res.nread();
+}
+
/** Read synchronously from a file ID into an array buffer.
*
* Return `number | EOF` for the operation.
@@ -74,14 +93,7 @@ export async function open(
*
*/
export function readSync(rid: number, p: Uint8Array): number | EOF {
- const nread = sendSyncMinimal(OP_READ, rid, p);
- if (nread < 0) {
- throw new Error("read error");
- } else if (nread == 0) {
- return EOF;
- } else {
- return nread;
- }
+ return resRead(dispatch.sendSync(...reqRead(rid, p)));
}
/** Read from a file ID into an array buffer.
@@ -106,6 +118,23 @@ export async function read(rid: number, p: Uint8Array): Promise<number | EOF> {
}
}
+function reqWrite(
+ rid: number,
+ p: Uint8Array
+): [flatbuffers.Builder, msg.Any, flatbuffers.Offset, Uint8Array] {
+ const builder = flatbuffers.createBuilder();
+ const inner = msg.Write.createWrite(builder, rid);
+ return [builder, msg.Any.Write, inner, p];
+}
+
+function resWrite(baseRes: null | msg.Base): number {
+ assert(baseRes != null);
+ assert(msg.Any.WriteRes === baseRes!.innerType());
+ const res = new msg.WriteRes();
+ assert(baseRes!.inner(res) != null);
+ return res.nbyte();
+}
+
/** Write synchronously to the file ID the contents of the array buffer.
*
* Resolves with the number of bytes written.
@@ -116,12 +145,7 @@ export async function read(rid: number, p: Uint8Array): Promise<number | EOF> {
* Deno.writeSync(file.rid, data);
*/
export function writeSync(rid: number, p: Uint8Array): number {
- let result = sendSyncMinimal(OP_WRITE, rid, p);
- if (result < 0) {
- throw new Error("write error");
- } else {
- return result;
- }
+ return resWrite(dispatch.sendSync(...reqWrite(rid, p)));
}
/** Write to the file ID the contents of the array buffer.