1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00

Refactor libdeno.send() code to reduce boilerplate.

Also removes assignCmdId as it's currently unused.
This commit is contained in:
Ryan Dahl 2018-08-30 15:35:51 -04:00
parent cce3bd470b
commit 45dafe15ee
6 changed files with 66 additions and 137 deletions

26
js/fbs_util.ts Normal file
View file

@ -0,0 +1,26 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { libdeno } from "./libdeno";
import { flatbuffers } from "flatbuffers";
import { maybeThrowError } from "./errors";
import { deno as fbs } from "gen/msg_generated";
export function send(
builder: flatbuffers.Builder,
msgType: fbs.Any,
msg: flatbuffers.Offset
): null | fbs.Base {
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, msgType);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
if (resBuf == null) {
return null;
} else {
const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!));
const baseRes = fbs.Base.getRootAsBase(bb);
maybeThrowError(baseRes);
return baseRes;
}
}

View file

@ -8,7 +8,7 @@ import {
notImplemented
} from "./util";
import { flatbuffers } from "flatbuffers";
import { libdeno } from "./libdeno";
import { send } from "./fbs_util";
import { deno as fbs } from "gen/msg_generated";
import {
Headers,
@ -166,14 +166,8 @@ class FetchRequest {
fbs.FetchReq.addId(builder, this.id);
fbs.FetchReq.addUrl(builder, url);
const msg = fbs.FetchReq.endFetchReq(builder);
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, fbs.Any.FetchReq);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
assert(resBuf == null);
//console.log("FetchReq sent", builder);
const res = send(builder, fbs.Any.FetchReq, msg);
assert(res == null);
}
}

View file

@ -1,24 +1,25 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { flatbuffers } from "flatbuffers";
import { deno as fbs } from "gen/msg_generated";
import { assert, assignCmdId, log, setLogDebug } from "./util";
import { assert, log, setLogDebug } from "./util";
import * as os from "./os";
import { DenoCompiler } from "./compiler";
import { libdeno } from "./libdeno";
import * as timers from "./timers";
import { onFetchRes } from "./fetch";
import { argv } from "./deno";
import { send } from "./fbs_util";
function startMsg(cmdId: number): Uint8Array {
function sendStart(): fbs.StartRes {
const builder = new flatbuffers.Builder();
fbs.Start.startStart(builder);
const startOffset = fbs.Start.endStart(builder);
fbs.Base.startBase(builder);
fbs.Base.addCmdId(builder, cmdId);
fbs.Base.addMsg(builder, startOffset);
fbs.Base.addMsgType(builder, fbs.Any.Start);
builder.finish(fbs.Base.endBase(builder));
return builder.asUint8Array();
const baseRes = send(builder, fbs.Any.Start, startOffset);
assert(baseRes != null);
assert(fbs.Any.StartRes === baseRes!.msgType());
const startRes = new fbs.StartRes();
assert(baseRes!.msg(startRes) != null);
return startRes;
}
function onMessage(ui8: Uint8Array) {
@ -64,22 +65,7 @@ export default function denoMain() {
// First we send an empty "Start" message to let the privlaged side know we
// are ready. The response should be a "StartRes" message containing the CLI
// argv and other info.
const cmdId = assignCmdId();
const res = libdeno.send(startMsg(cmdId));
// TODO(ry) Remove this conditional once main.rs gets up to speed.
if (res == null) {
console.log(`The 'Start' message got a null response. Normally this would
be an error but main.rs currently does this."); Exiting without error.`);
return;
}
// Deserialize res into startResMsg.
const bb = new flatbuffers.ByteBuffer(res);
const base = fbs.Base.getRootAsBase(bb);
assert(fbs.Any.StartRes === base.msgType());
const startResMsg = new fbs.StartRes();
assert(base.msg(startResMsg) != null);
const startResMsg = sendStart();
setLogDebug(startResMsg.debugFlag());

104
js/os.ts
View file

@ -3,20 +3,15 @@ import { ModuleInfo } from "./types";
import { deno as fbs } from "gen/msg_generated";
import { assert } from "./util";
import * as util from "./util";
import { maybeThrowError } from "./errors";
import { flatbuffers } from "flatbuffers";
import { libdeno } from "./libdeno";
import { send } from "./fbs_util";
export function exit(exitCode = 0): never {
const builder = new flatbuffers.Builder();
fbs.Exit.startExit(builder);
fbs.Exit.addCode(builder, exitCode);
const msg = fbs.Exit.endExit(builder);
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, fbs.Any.Exit);
builder.finish(fbs.Base.endBase(builder));
libdeno.send(builder.asUint8Array());
send(builder, fbs.Any.Exit, msg);
return util.unreachable();
}
@ -33,21 +28,11 @@ export function codeFetch(
fbs.CodeFetch.addModuleSpecifier(builder, moduleSpecifier_);
fbs.CodeFetch.addContainingFile(builder, containingFile_);
const msg = fbs.CodeFetch.endCodeFetch(builder);
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, fbs.Any.CodeFetch);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
assert(resBuf != null);
// Process CodeFetchRes
// TypeScript does not track `assert` from a CFA perspective, therefore not
// null assertion `!`
const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!));
const baseRes = fbs.Base.getRootAsBase(bb);
maybeThrowError(baseRes);
assert(fbs.Any.CodeFetchRes === baseRes.msgType());
const baseRes = send(builder, fbs.Any.CodeFetch, msg);
assert(baseRes != null);
assert(fbs.Any.CodeFetchRes === baseRes!.msgType());
const codeFetchRes = new fbs.CodeFetchRes();
assert(baseRes.msg(codeFetchRes) != null);
assert(baseRes!.msg(codeFetchRes) != null);
const r = {
moduleName: codeFetchRes.moduleName(),
filename: codeFetchRes.filename(),
@ -72,17 +57,8 @@ export function codeCache(
fbs.CodeCache.addSourceCode(builder, sourceCode_);
fbs.CodeCache.addOutputCode(builder, outputCode_);
const msg = fbs.CodeCache.endCodeCache(builder);
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, fbs.Any.CodeCache);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
// Expect null or error.
if (resBuf != null) {
const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf));
const baseRes = fbs.Base.getRootAsBase(bb);
maybeThrowError(baseRes);
}
const baseRes = send(builder, fbs.Any.CodeCache, msg);
assert(baseRes == null); // Expect null or error.
}
/**
@ -119,18 +95,11 @@ export function makeTempDirSync({
fbs.MakeTempDir.addSuffix(builder, fbSuffix);
}
const msg = fbs.MakeTempDir.endMakeTempDir(builder);
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, fbs.Any.MakeTempDir);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
assert(resBuf != null);
const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!));
const baseRes = fbs.Base.getRootAsBase(bb);
maybeThrowError(baseRes);
assert(fbs.Any.MakeTempDirRes === baseRes.msgType());
const baseRes = send(builder, fbs.Any.MakeTempDir, msg);
assert(baseRes != null);
assert(fbs.Any.MakeTempDirRes === baseRes!.msgType());
const res = new fbs.MakeTempDirRes();
assert(baseRes.msg(res) != null);
assert(baseRes!.msg(res) != null);
const path = res.path();
assert(path != null);
return path!;
@ -149,23 +118,13 @@ export function readFileSync(filename: string): Uint8Array {
fbs.ReadFileSync.startReadFileSync(builder);
fbs.ReadFileSync.addFilename(builder, filename_);
const msg = fbs.ReadFileSync.endReadFileSync(builder);
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, fbs.Any.ReadFileSync);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
assert(resBuf != null);
// TypeScript does not track `assert` from a CFA perspective, therefore not
// null assertion `!`
const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!));
const baseRes = fbs.Base.getRootAsBase(bb);
maybeThrowError(baseRes);
assert(fbs.Any.ReadFileSyncRes === baseRes.msgType());
const baseRes = send(builder, fbs.Any.ReadFileSync, msg);
assert(baseRes != null);
assert(fbs.Any.ReadFileSyncRes === baseRes!.msgType());
const res = new fbs.ReadFileSyncRes();
assert(baseRes.msg(res) != null);
assert(baseRes!.msg(res) != null);
const dataArray = res.dataArray();
assert(dataArray != null);
// TypeScript cannot track assertion above, therefore not null assertion
return new Uint8Array(dataArray!);
}
@ -226,22 +185,12 @@ function statSyncInner(filename: string, lstat: boolean): FileInfo {
fbs.StatSync.addFilename(builder, filename_);
fbs.StatSync.addLstat(builder, lstat);
const msg = fbs.StatSync.endStatSync(builder);
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, fbs.Any.StatSync);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
assert(resBuf != null);
// TypeScript does not track `assert` from a CFA perspective, therefore not
// null assertion `!`
const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!));
const baseRes = fbs.Base.getRootAsBase(bb);
maybeThrowError(baseRes);
assert(fbs.Any.StatSyncRes === baseRes.msgType());
const baseRes = send(builder, fbs.Any.StatSync, msg);
assert(baseRes != null);
assert(fbs.Any.StatSyncRes === baseRes!.msgType());
const res = new fbs.StatSyncRes();
assert(baseRes.msg(res) != null);
// TypeScript cannot track assertion above, therefore not null assertion
return new FileInfo(baseRes.msg(res)!);
assert(baseRes!.msg(res) != null);
return new FileInfo(res);
}
export function writeFileSync(
@ -265,14 +214,5 @@ export function writeFileSync(
fbs.WriteFileSync.addData(builder, dataOffset);
fbs.WriteFileSync.addPerm(builder, perm);
const msg = fbs.WriteFileSync.endWriteFileSync(builder);
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, fbs.Any.WriteFileSync);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
if (resBuf != null) {
const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!));
const baseRes = fbs.Base.getRootAsBase(bb);
maybeThrowError(baseRes);
}
send(builder, fbs.Any.WriteFileSync, msg);
}

View file

@ -3,7 +3,7 @@ import { assert } from "./util";
import * as util from "./util";
import { deno as fbs } from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import { libdeno } from "./libdeno";
import { send } from "./fbs_util";
let nextTimerId = 1;
@ -60,13 +60,8 @@ function startTimer(
fbs.TimerStart.addInterval(builder, timer.interval);
fbs.TimerStart.addDelay(builder, timer.delay);
const msg = fbs.TimerStart.endTimerStart(builder);
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, fbs.Any.TimerStart);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
assert(resBuf == null);
const baseRes = send(builder, fbs.Any.TimerStart, msg);
assert(baseRes == null);
return timer.id;
}
@ -95,10 +90,6 @@ export function clearTimer(id: number) {
fbs.TimerClear.startTimerClear(builder);
fbs.TimerClear.addId(builder, id);
const msg = fbs.TimerClear.endTimerClear(builder);
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, fbs.Any.TimerClear);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
assert(resBuf == null);
const res = send(builder, fbs.Any.TimerClear, msg);
assert(res == null);
}

View file

@ -21,14 +21,6 @@ export function assert(cond: boolean, msg = "assert") {
}
}
let cmdIdCounter = 0;
export function assignCmdId(): number {
// TODO(piscisaureus) Safely re-use so they don't overflow.
const cmdId = ++cmdIdCounter;
assert(cmdId < 2 ** 32, "cmdId overflow");
return cmdId;
}
export function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
return ab as ArrayBuffer;