summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/compiler.ts5
-rw-r--r--js/dispatch.ts5
-rw-r--r--js/dispatch_flatbuffers.ts151
-rw-r--r--js/dispatch_minimal.ts16
-rw-r--r--js/files.ts62
5 files changed, 36 insertions, 203 deletions
diff --git a/js/compiler.ts b/js/compiler.ts
index 5399d59ad..60fe49701 100644
--- a/js/compiler.ts
+++ b/js/compiler.ts
@@ -1,6 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as ts from "typescript";
-
import { assetSourceCode } from "./assets";
import { bold, cyan, yellow } from "./colors";
import { Console } from "./console";
@@ -9,7 +8,6 @@ import { Diagnostic, fromTypeScriptDiagnostic } from "./diagnostics";
import { cwd } from "./dir";
import * as dispatch from "./dispatch";
import { sendSync } from "./dispatch_json";
-import { msg } from "./dispatch_flatbuffers";
import * as os from "./os";
import { TextEncoder } from "./text_encoding";
import { getMappedModuleName, parseTypeDirectives } from "./type_directives";
@@ -19,6 +17,9 @@ import { window } from "./window";
import { postMessage, workerClose, workerMain } from "./workers";
import { writeFileSync } from "./write_file";
+// TODO(ry) msg_generated import will be removed soon.
+import * as msg from "gen/cli/msg_generated";
+
// Startup boilerplate. This is necessary because the compiler has its own
// snapshot. (It would be great if we could remove these things or centralize
// them somewhere else.)
diff --git a/js/dispatch.ts b/js/dispatch.ts
index 2416c19e2..dfa276cda 100644
--- a/js/dispatch.ts
+++ b/js/dispatch.ts
@@ -1,10 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as minimal from "./dispatch_minimal";
-import * as flatbuffers from "./dispatch_flatbuffers";
import * as json from "./dispatch_json";
// These consts are shared with Rust. Update with care.
-export const OP_FLATBUFFER = 100;
export const OP_READ = 1;
export const OP_WRITE = 2;
export const OP_EXIT = 3;
@@ -64,9 +62,6 @@ export const OP_CWD = 56;
export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
switch (opId) {
- case OP_FLATBUFFER:
- flatbuffers.asyncMsgFromRust(opId, ui8);
- break;
case OP_WRITE:
case OP_READ:
minimal.asyncMsgFromRust(opId, ui8);
diff --git a/js/dispatch_flatbuffers.ts b/js/dispatch_flatbuffers.ts
deleted file mode 100644
index 0e375dbdf..000000000
--- a/js/dispatch_flatbuffers.ts
+++ /dev/null
@@ -1,151 +0,0 @@
-// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import * as flatbuffers from "./flatbuffers";
-import { DenoError } from "./errors";
-import { core } from "./core";
-import * as msg from "gen/cli/msg_generated";
-import * as util from "./util";
-import { OP_FLATBUFFER } from "./dispatch";
-export { msg, flatbuffers };
-
-const promiseTable = new Map<number, util.Resolvable<msg.Base>>();
-let _nextPromiseId = 1;
-
-export function nextPromiseId(): number {
- return _nextPromiseId++;
-}
-
-interface FlatbufferRecord {
- promiseId: number;
- base: msg.Base;
-}
-
-export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
- let { promiseId, base } = flatbufferRecordFromBuf(ui8);
- const promise = promiseTable.get(promiseId);
- util.assert(promise != null, `Expecting promise in table. ${promiseId}`);
- promiseTable.delete(promiseId);
- const err = maybeError(base);
- if (err != null) {
- promise!.reject(err);
- } else {
- promise!.resolve(base);
- }
-}
-
-function flatbufferRecordFromBuf(buf: Uint8Array): FlatbufferRecord {
- const bb = new flatbuffers.ByteBuffer(buf);
- const base = msg.Base.getRootAsBase(bb);
- return {
- promiseId: base.cmdId(),
- base
- };
-}
-
-function ui8FromArrayBufferView(abv: ArrayBufferView): Uint8Array {
- return new Uint8Array(abv.buffer, abv.byteOffset, abv.byteLength);
-}
-
-function sendInternal(
- builder: flatbuffers.Builder,
- innerType: msg.Any,
- inner: flatbuffers.Offset,
- zeroCopy: undefined | ArrayBufferView,
- isSync: true
-): Uint8Array;
-function sendInternal(
- builder: flatbuffers.Builder,
- innerType: msg.Any,
- inner: flatbuffers.Offset,
- zeroCopy: undefined | ArrayBufferView,
- isSync: false
-): Promise<msg.Base>;
-function sendInternal(
- builder: flatbuffers.Builder,
- innerType: msg.Any,
- inner: flatbuffers.Offset,
- zeroCopy: undefined | ArrayBufferView,
- isSync: boolean
-): Promise<msg.Base> | Uint8Array {
- const cmdId = nextPromiseId();
- msg.Base.startBase(builder);
- msg.Base.addInner(builder, inner);
- msg.Base.addInnerType(builder, innerType);
- msg.Base.addSync(builder, isSync);
- msg.Base.addCmdId(builder, cmdId);
- builder.finish(msg.Base.endBase(builder));
-
- const control = builder.asUint8Array();
-
- const response = core.dispatch(
- OP_FLATBUFFER, // TODO(ry) Use actual opId later.
- control,
- zeroCopy ? ui8FromArrayBufferView(zeroCopy) : undefined
- );
-
- builder.inUse = false;
-
- if (response == null) {
- util.assert(!isSync);
- const promise = util.createResolvable<msg.Base>();
- promiseTable.set(cmdId, promise);
- return promise;
- } else {
- if (!isSync) {
- // We can easily and correctly allow for sync responses to async calls
- // by creating and returning a promise from the sync response.
- const bb = new flatbuffers.ByteBuffer(response);
- const base = msg.Base.getRootAsBase(bb);
- const err = maybeError(base);
- if (err != null) {
- return Promise.reject(err);
- } else {
- return Promise.resolve(base);
- }
- }
- return response;
- }
-}
-
-// @internal
-export function sendAsync(
- builder: flatbuffers.Builder,
- innerType: msg.Any,
- inner: flatbuffers.Offset,
- data?: ArrayBufferView
-): Promise<msg.Base> {
- return sendInternal(builder, innerType, inner, data, false);
-}
-
-// @internal
-export function sendSync(
- builder: flatbuffers.Builder,
- innerType: msg.Any,
- inner: flatbuffers.Offset,
- data?: ArrayBufferView
-): null | msg.Base {
- const response = sendInternal(builder, innerType, inner, data, true);
- if (response!.length === 0) {
- return null;
- } else {
- const bb = new flatbuffers.ByteBuffer(response!);
- const baseRes = msg.Base.getRootAsBase(bb);
- maybeThrowError(baseRes);
- return baseRes;
- }
-}
-
-function maybeError(base: msg.Base): null | DenoError<msg.ErrorKind> {
- const kind = base.errorKind();
- if (kind === msg.ErrorKind.NoError) {
- return null;
- } else {
- return new DenoError(kind, base.error()!);
- }
-}
-
-function maybeThrowError(base: msg.Base): void {
- const err = maybeError(base);
- if (err != null) {
- throw err;
- }
-}
diff --git a/js/dispatch_minimal.ts b/js/dispatch_minimal.ts
index 9a310fd22..3df888e77 100644
--- a/js/dispatch_minimal.ts
+++ b/js/dispatch_minimal.ts
@@ -4,6 +4,9 @@ import * as util from "./util";
import { core } from "./core";
const promiseTableMin = new Map<number, util.Resolvable<number>>();
+// Note it's important that promiseId starts at 1 instead of 0, because sync
+// messages are indicated with promiseId 0. If we ever add wrap around logic for
+// overflows, this should be taken into account.
let _nextPromiseId = 1;
function nextPromiseId(): number {
@@ -63,3 +66,16 @@ export function sendAsyncMinimal(
core.dispatch(opId, scratchBytes, zeroCopy);
return promise;
}
+
+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;
+}
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 {