summaryrefslogtreecommitdiff
path: root/js/files.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2019-08-24 13:20:48 -0700
committerGitHub <noreply@github.com>2019-08-24 13:20:48 -0700
commit2235dd795d3cc6c24ff1bdd1bbdcd110b4b0bdfc (patch)
treea5811adc062cbb1c66f05c863c9be245cf4fd2d2 /js/files.ts
parentbdc0a13261deaa3748f51d9948b4e7b92864c324 (diff)
Revert json ops (#2814)
* Revert "port more ops to JSON (#2809)" This reverts commit 137f33733d365026903d40e7cde6e34ac6c36dcf. * Revert "port ops to JSON: compiler, errors, fetch, files (#2804)" This reverts commit 79f82cf10ed1dbf91346994250d7311a4d74377a. * Revert "Port rest of os ops to JSON (#2802)" This reverts commit 5b2baa5c990fbeae747e952c5dcd7a5369e950b1.
Diffstat (limited to 'js/files.ts')
-rw-r--r--js/files.ts52
1 files changed, 39 insertions, 13 deletions
diff --git a/js/files.ts b/js/files.ts
index 4eff17aac..6f0e523c9 100644
--- a/js/files.ts
+++ b/js/files.ts
@@ -12,22 +12,37 @@ import {
} from "./io";
import { sendAsyncMinimal } from "./dispatch_minimal";
import { assert } from "./util";
-import * as dispatch from "./dispatch";
-import {
- sendSync as sendSyncJson,
- sendAsync as sendAsyncJson
-} from "./dispatch_json";
-import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers";
+import { sendAsync, sendSync, msg, flatbuffers } from "./dispatch_flatbuffers";
import { OP_READ, OP_WRITE } from "./dispatch";
+function reqOpen(
+ filename: string,
+ mode: OpenMode
+): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
+ const builder = flatbuffers.createBuilder();
+ const filename_ = builder.createString(filename);
+ const mode_ = builder.createString(mode);
+ const inner = msg.Open.createOpen(builder, filename_, 0, mode_);
+ return [builder, msg.Any.Open, inner];
+}
+
+function resOpen(baseRes: null | msg.Base): File {
+ assert(baseRes != null);
+ assert(msg.Any.OpenRes === baseRes!.innerType());
+ const res = new msg.OpenRes();
+ assert(baseRes!.inner(res) != null);
+ const rid = res.rid();
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
+ return new File(rid);
+}
+
/** Open a file and return an instance of the `File` object
* synchronously.
*
* const file = Deno.openSync("/foo/bar.txt");
*/
export function openSync(filename: string, mode: OpenMode = "r"): File {
- const rid = sendSyncJson(dispatch.OP_OPEN, { filename, mode });
- return new File(rid);
+ return resOpen(sendSync(...reqOpen(filename, mode)));
}
/** Open a file and return an instance of the `File` object.
@@ -40,8 +55,7 @@ export async function open(
filename: string,
mode: OpenMode = "r"
): Promise<File> {
- const rid = await sendAsyncJson(dispatch.OP_OPEN, { filename, mode });
- return new File(rid);
+ return resOpen(await sendAsync(...reqOpen(filename, mode)));
}
function reqRead(
@@ -151,13 +165,23 @@ export async function write(rid: number, p: Uint8Array): Promise<number> {
}
}
+function reqSeek(
+ rid: number,
+ offset: number,
+ whence: SeekMode
+): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
+ const builder = flatbuffers.createBuilder();
+ const inner = msg.Seek.createSeek(builder, rid, offset, whence);
+ return [builder, msg.Any.Seek, inner];
+}
+
/** Seek a file ID synchronously to the given offset under mode given by `whence`.
*
* const file = Deno.openSync("/foo/bar.txt");
* Deno.seekSync(file.rid, 0, 0);
*/
export function seekSync(rid: number, offset: number, whence: SeekMode): void {
- sendSyncJson(dispatch.OP_SEEK, { rid, offset, whence });
+ sendSync(...reqSeek(rid, offset, whence));
}
/** Seek a file ID to the given offset under mode given by `whence`.
@@ -172,12 +196,14 @@ export async function seek(
offset: number,
whence: SeekMode
): Promise<void> {
- await sendAsyncJson(dispatch.OP_SEEK, { rid, offset, whence });
+ await sendAsync(...reqSeek(rid, offset, whence));
}
/** Close the file ID. */
export function close(rid: number): void {
- sendSyncJson(dispatch.OP_CLOSE, { rid });
+ const builder = flatbuffers.createBuilder();
+ const inner = msg.Close.createClose(builder, rid);
+ sendSync(builder, msg.Any.Close, inner);
}
/** The Deno abstraction for reading and writing files. */