summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2019-08-14 19:54:35 -0400
committerGitHub <noreply@github.com>2019-08-14 19:54:35 -0400
commit498f6ad431478f655b136782093e19e29248b24d (patch)
treef1a39982a1a372422ea93b1c9bd7298fe9a1e838 /js
parente6c349af9f7260c2c4ec713bd231fe554240721e (diff)
Remove dead code: legacy read/write ops (#2776)
readSync and writeSync use dispatch_minimal now.
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, 34 insertions, 41 deletions
diff --git a/js/dispatch.ts b/js/dispatch.ts
index babea5739..6edd2981c 100644
--- a/js/dispatch.ts
+++ b/js/dispatch.ts
@@ -32,9 +32,13 @@ 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 df0a290b2..483342127 100644
--- a/js/dispatch_minimal.ts
+++ b/js/dispatch_minimal.ts
@@ -52,6 +52,19 @@ 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 eb899d738..1582d8fd4 100644
--- a/js/files.ts
+++ b/js/files.ts
@@ -11,11 +11,12 @@ import {
SyncSeeker
} from "./io";
import * as dispatch from "./dispatch";
-import { sendAsyncMinimal } from "./dispatch_minimal";
+import { sendAsyncMinimal, sendSyncMinimal } 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;
@@ -62,26 +63,6 @@ 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.
@@ -93,7 +74,14 @@ function resRead(baseRes: null | msg.Base): number | EOF {
*
*/
export function readSync(rid: number, p: Uint8Array): number | EOF {
- return resRead(dispatch.sendSync(...reqRead(rid, p)));
+ const nread = sendSyncMinimal(OP_READ, rid, p);
+ if (nread < 0) {
+ throw new Error("read error");
+ } else if (nread == 0) {
+ return EOF;
+ } else {
+ return nread;
+ }
}
/** Read from a file ID into an array buffer.
@@ -118,23 +106,6 @@ 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.
@@ -145,7 +116,12 @@ function resWrite(baseRes: null | msg.Base): number {
* Deno.writeSync(file.rid, data);
*/
export function writeSync(rid: number, p: Uint8Array): number {
- return resWrite(dispatch.sendSync(...reqWrite(rid, p)));
+ let result = sendSyncMinimal(OP_WRITE, rid, p);
+ if (result < 0) {
+ throw new Error("write error");
+ } else {
+ return result;
+ }
}
/** Write to the file ID the contents of the array buffer.