diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-08-16 11:05:24 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-08-16 14:41:08 -0400 |
commit | 81f809f2a675ff4ff7f93231ca87a18cb5b4628e (patch) | |
tree | 09a8bd8eedc5b03a4399cdfac896b2d445ed8037 /js/files.ts | |
parent | 52a66c2796f97f5a08d679389172c39c0652cb16 (diff) |
Revert "Remove dead code: legacy read/write ops"
This is causing a segfault for unknown reasons - see #2787.
This reverts commit 498f6ad431478f655b136782093e19e29248b24d.
Diffstat (limited to 'js/files.ts')
-rw-r--r-- | js/files.ts | 56 |
1 files changed, 40 insertions, 16 deletions
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. |