summaryrefslogtreecommitdiff
path: root/js/files.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2019-08-26 10:58:44 -0400
committerRyan Dahl <ry@tinyclouds.org>2019-08-26 14:56:42 -0400
commitd8ada4d3fcc5dfe7f76103399a1d765fbab2ee45 (patch)
tree2209cb60cd537d1da5c1687ab2b7996655654c06 /js/files.ts
parent7ff67017f2153eeed393da670777ff85df2801ca (diff)
Port readSync/writeSync ops to minimal
This removes dispatch_flatbuffers as it is now unused. There are still a few places where msg_generated is used: ErrorKind and MediaType. These will be dealt with later.
Diffstat (limited to 'js/files.ts')
-rw-r--r--js/files.ts62
1 files changed, 17 insertions, 45 deletions
diff --git a/js/files.ts b/js/files.ts
index 4eff17aac..0335056b3 100644
--- a/js/files.ts
+++ b/js/files.ts
@@ -10,15 +10,12 @@ import {
SyncWriter,
SyncSeeker
} from "./io";
-import { sendAsyncMinimal } from "./dispatch_minimal";
-import { assert } from "./util";
+import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal";
import * as dispatch from "./dispatch";
import {
sendSync as sendSyncJson,
sendAsync as sendAsyncJson
} from "./dispatch_json";
-import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers";
-import { OP_READ, OP_WRITE } from "./dispatch";
/** Open a file and return an instance of the `File` object
* synchronously.
@@ -44,26 +41,6 @@ export async function open(
return new File(rid);
}
-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.
@@ -75,7 +52,14 @@ function resRead(baseRes: null | msg.Base): number | EOF {
*
*/
export function readSync(rid: number, p: Uint8Array): number | EOF {
- return resRead(sendSync(...reqRead(rid, p)));
+ const nread = sendSyncMinimal(dispatch.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.
@@ -90,7 +74,7 @@ export function readSync(rid: number, p: Uint8Array): number | EOF {
* })();
*/
export async function read(rid: number, p: Uint8Array): Promise<number | EOF> {
- const nread = await sendAsyncMinimal(OP_READ, rid, p);
+ const nread = await sendAsyncMinimal(dispatch.OP_READ, rid, p);
if (nread < 0) {
throw new Error("read error");
} else if (nread == 0) {
@@ -100,23 +84,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.
@@ -127,7 +94,12 @@ function resWrite(baseRes: null | msg.Base): number {
* Deno.writeSync(file.rid, data);
*/
export function writeSync(rid: number, p: Uint8Array): number {
- return resWrite(sendSync(...reqWrite(rid, p)));
+ const result = sendSyncMinimal(dispatch.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.
@@ -143,7 +115,7 @@ export function writeSync(rid: number, p: Uint8Array): number {
*
*/
export async function write(rid: number, p: Uint8Array): Promise<number> {
- let result = await sendAsyncMinimal(OP_WRITE, rid, p);
+ let result = await sendAsyncMinimal(dispatch.OP_WRITE, rid, p);
if (result < 0) {
throw new Error("write error");
} else {