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:
parent
cce3bd470b
commit
45dafe15ee
6 changed files with 66 additions and 137 deletions
26
js/fbs_util.ts
Normal file
26
js/fbs_util.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
12
js/fetch.ts
12
js/fetch.ts
|
@ -8,7 +8,7 @@ import {
|
||||||
notImplemented
|
notImplemented
|
||||||
} from "./util";
|
} from "./util";
|
||||||
import { flatbuffers } from "flatbuffers";
|
import { flatbuffers } from "flatbuffers";
|
||||||
import { libdeno } from "./libdeno";
|
import { send } from "./fbs_util";
|
||||||
import { deno as fbs } from "gen/msg_generated";
|
import { deno as fbs } from "gen/msg_generated";
|
||||||
import {
|
import {
|
||||||
Headers,
|
Headers,
|
||||||
|
@ -166,14 +166,8 @@ class FetchRequest {
|
||||||
fbs.FetchReq.addId(builder, this.id);
|
fbs.FetchReq.addId(builder, this.id);
|
||||||
fbs.FetchReq.addUrl(builder, url);
|
fbs.FetchReq.addUrl(builder, url);
|
||||||
const msg = fbs.FetchReq.endFetchReq(builder);
|
const msg = fbs.FetchReq.endFetchReq(builder);
|
||||||
fbs.Base.startBase(builder);
|
const res = send(builder, fbs.Any.FetchReq, msg);
|
||||||
fbs.Base.addMsg(builder, msg);
|
assert(res == null);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
34
js/main.ts
34
js/main.ts
|
@ -1,24 +1,25 @@
|
||||||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||||
import { flatbuffers } from "flatbuffers";
|
import { flatbuffers } from "flatbuffers";
|
||||||
import { deno as fbs } from "gen/msg_generated";
|
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 * as os from "./os";
|
||||||
import { DenoCompiler } from "./compiler";
|
import { DenoCompiler } from "./compiler";
|
||||||
import { libdeno } from "./libdeno";
|
import { libdeno } from "./libdeno";
|
||||||
import * as timers from "./timers";
|
import * as timers from "./timers";
|
||||||
import { onFetchRes } from "./fetch";
|
import { onFetchRes } from "./fetch";
|
||||||
import { argv } from "./deno";
|
import { argv } from "./deno";
|
||||||
|
import { send } from "./fbs_util";
|
||||||
|
|
||||||
function startMsg(cmdId: number): Uint8Array {
|
function sendStart(): fbs.StartRes {
|
||||||
const builder = new flatbuffers.Builder();
|
const builder = new flatbuffers.Builder();
|
||||||
fbs.Start.startStart(builder);
|
fbs.Start.startStart(builder);
|
||||||
const startOffset = fbs.Start.endStart(builder);
|
const startOffset = fbs.Start.endStart(builder);
|
||||||
fbs.Base.startBase(builder);
|
const baseRes = send(builder, fbs.Any.Start, startOffset);
|
||||||
fbs.Base.addCmdId(builder, cmdId);
|
assert(baseRes != null);
|
||||||
fbs.Base.addMsg(builder, startOffset);
|
assert(fbs.Any.StartRes === baseRes!.msgType());
|
||||||
fbs.Base.addMsgType(builder, fbs.Any.Start);
|
const startRes = new fbs.StartRes();
|
||||||
builder.finish(fbs.Base.endBase(builder));
|
assert(baseRes!.msg(startRes) != null);
|
||||||
return builder.asUint8Array();
|
return startRes;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onMessage(ui8: Uint8Array) {
|
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
|
// 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
|
// are ready. The response should be a "StartRes" message containing the CLI
|
||||||
// argv and other info.
|
// argv and other info.
|
||||||
const cmdId = assignCmdId();
|
const startResMsg = sendStart();
|
||||||
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);
|
|
||||||
|
|
||||||
setLogDebug(startResMsg.debugFlag());
|
setLogDebug(startResMsg.debugFlag());
|
||||||
|
|
||||||
|
|
104
js/os.ts
104
js/os.ts
|
@ -3,20 +3,15 @@ import { ModuleInfo } from "./types";
|
||||||
import { deno as fbs } from "gen/msg_generated";
|
import { deno as fbs } from "gen/msg_generated";
|
||||||
import { assert } from "./util";
|
import { assert } from "./util";
|
||||||
import * as util from "./util";
|
import * as util from "./util";
|
||||||
import { maybeThrowError } from "./errors";
|
|
||||||
import { flatbuffers } from "flatbuffers";
|
import { flatbuffers } from "flatbuffers";
|
||||||
import { libdeno } from "./libdeno";
|
import { send } from "./fbs_util";
|
||||||
|
|
||||||
export function exit(exitCode = 0): never {
|
export function exit(exitCode = 0): never {
|
||||||
const builder = new flatbuffers.Builder();
|
const builder = new flatbuffers.Builder();
|
||||||
fbs.Exit.startExit(builder);
|
fbs.Exit.startExit(builder);
|
||||||
fbs.Exit.addCode(builder, exitCode);
|
fbs.Exit.addCode(builder, exitCode);
|
||||||
const msg = fbs.Exit.endExit(builder);
|
const msg = fbs.Exit.endExit(builder);
|
||||||
fbs.Base.startBase(builder);
|
send(builder, fbs.Any.Exit, msg);
|
||||||
fbs.Base.addMsg(builder, msg);
|
|
||||||
fbs.Base.addMsgType(builder, fbs.Any.Exit);
|
|
||||||
builder.finish(fbs.Base.endBase(builder));
|
|
||||||
libdeno.send(builder.asUint8Array());
|
|
||||||
return util.unreachable();
|
return util.unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,21 +28,11 @@ export function codeFetch(
|
||||||
fbs.CodeFetch.addModuleSpecifier(builder, moduleSpecifier_);
|
fbs.CodeFetch.addModuleSpecifier(builder, moduleSpecifier_);
|
||||||
fbs.CodeFetch.addContainingFile(builder, containingFile_);
|
fbs.CodeFetch.addContainingFile(builder, containingFile_);
|
||||||
const msg = fbs.CodeFetch.endCodeFetch(builder);
|
const msg = fbs.CodeFetch.endCodeFetch(builder);
|
||||||
fbs.Base.startBase(builder);
|
const baseRes = send(builder, fbs.Any.CodeFetch, msg);
|
||||||
fbs.Base.addMsg(builder, msg);
|
assert(baseRes != null);
|
||||||
fbs.Base.addMsgType(builder, fbs.Any.CodeFetch);
|
assert(fbs.Any.CodeFetchRes === baseRes!.msgType());
|
||||||
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 codeFetchRes = new fbs.CodeFetchRes();
|
const codeFetchRes = new fbs.CodeFetchRes();
|
||||||
assert(baseRes.msg(codeFetchRes) != null);
|
assert(baseRes!.msg(codeFetchRes) != null);
|
||||||
const r = {
|
const r = {
|
||||||
moduleName: codeFetchRes.moduleName(),
|
moduleName: codeFetchRes.moduleName(),
|
||||||
filename: codeFetchRes.filename(),
|
filename: codeFetchRes.filename(),
|
||||||
|
@ -72,17 +57,8 @@ export function codeCache(
|
||||||
fbs.CodeCache.addSourceCode(builder, sourceCode_);
|
fbs.CodeCache.addSourceCode(builder, sourceCode_);
|
||||||
fbs.CodeCache.addOutputCode(builder, outputCode_);
|
fbs.CodeCache.addOutputCode(builder, outputCode_);
|
||||||
const msg = fbs.CodeCache.endCodeCache(builder);
|
const msg = fbs.CodeCache.endCodeCache(builder);
|
||||||
fbs.Base.startBase(builder);
|
const baseRes = send(builder, fbs.Any.CodeCache, msg);
|
||||||
fbs.Base.addMsg(builder, msg);
|
assert(baseRes == null); // Expect null or error.
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -119,18 +95,11 @@ export function makeTempDirSync({
|
||||||
fbs.MakeTempDir.addSuffix(builder, fbSuffix);
|
fbs.MakeTempDir.addSuffix(builder, fbSuffix);
|
||||||
}
|
}
|
||||||
const msg = fbs.MakeTempDir.endMakeTempDir(builder);
|
const msg = fbs.MakeTempDir.endMakeTempDir(builder);
|
||||||
fbs.Base.startBase(builder);
|
const baseRes = send(builder, fbs.Any.MakeTempDir, msg);
|
||||||
fbs.Base.addMsg(builder, msg);
|
assert(baseRes != null);
|
||||||
fbs.Base.addMsgType(builder, fbs.Any.MakeTempDir);
|
assert(fbs.Any.MakeTempDirRes === baseRes!.msgType());
|
||||||
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 res = new fbs.MakeTempDirRes();
|
const res = new fbs.MakeTempDirRes();
|
||||||
assert(baseRes.msg(res) != null);
|
assert(baseRes!.msg(res) != null);
|
||||||
const path = res.path();
|
const path = res.path();
|
||||||
assert(path != null);
|
assert(path != null);
|
||||||
return path!;
|
return path!;
|
||||||
|
@ -149,23 +118,13 @@ export function readFileSync(filename: string): Uint8Array {
|
||||||
fbs.ReadFileSync.startReadFileSync(builder);
|
fbs.ReadFileSync.startReadFileSync(builder);
|
||||||
fbs.ReadFileSync.addFilename(builder, filename_);
|
fbs.ReadFileSync.addFilename(builder, filename_);
|
||||||
const msg = fbs.ReadFileSync.endReadFileSync(builder);
|
const msg = fbs.ReadFileSync.endReadFileSync(builder);
|
||||||
fbs.Base.startBase(builder);
|
const baseRes = send(builder, fbs.Any.ReadFileSync, msg);
|
||||||
fbs.Base.addMsg(builder, msg);
|
assert(baseRes != null);
|
||||||
fbs.Base.addMsgType(builder, fbs.Any.ReadFileSync);
|
assert(fbs.Any.ReadFileSyncRes === baseRes!.msgType());
|
||||||
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 res = new fbs.ReadFileSyncRes();
|
const res = new fbs.ReadFileSyncRes();
|
||||||
assert(baseRes.msg(res) != null);
|
assert(baseRes!.msg(res) != null);
|
||||||
const dataArray = res.dataArray();
|
const dataArray = res.dataArray();
|
||||||
assert(dataArray != null);
|
assert(dataArray != null);
|
||||||
// TypeScript cannot track assertion above, therefore not null assertion
|
|
||||||
return new Uint8Array(dataArray!);
|
return new Uint8Array(dataArray!);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,22 +185,12 @@ function statSyncInner(filename: string, lstat: boolean): FileInfo {
|
||||||
fbs.StatSync.addFilename(builder, filename_);
|
fbs.StatSync.addFilename(builder, filename_);
|
||||||
fbs.StatSync.addLstat(builder, lstat);
|
fbs.StatSync.addLstat(builder, lstat);
|
||||||
const msg = fbs.StatSync.endStatSync(builder);
|
const msg = fbs.StatSync.endStatSync(builder);
|
||||||
fbs.Base.startBase(builder);
|
const baseRes = send(builder, fbs.Any.StatSync, msg);
|
||||||
fbs.Base.addMsg(builder, msg);
|
assert(baseRes != null);
|
||||||
fbs.Base.addMsgType(builder, fbs.Any.StatSync);
|
assert(fbs.Any.StatSyncRes === baseRes!.msgType());
|
||||||
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 res = new fbs.StatSyncRes();
|
const res = new fbs.StatSyncRes();
|
||||||
assert(baseRes.msg(res) != null);
|
assert(baseRes!.msg(res) != null);
|
||||||
// TypeScript cannot track assertion above, therefore not null assertion
|
return new FileInfo(res);
|
||||||
return new FileInfo(baseRes.msg(res)!);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writeFileSync(
|
export function writeFileSync(
|
||||||
|
@ -265,14 +214,5 @@ export function writeFileSync(
|
||||||
fbs.WriteFileSync.addData(builder, dataOffset);
|
fbs.WriteFileSync.addData(builder, dataOffset);
|
||||||
fbs.WriteFileSync.addPerm(builder, perm);
|
fbs.WriteFileSync.addPerm(builder, perm);
|
||||||
const msg = fbs.WriteFileSync.endWriteFileSync(builder);
|
const msg = fbs.WriteFileSync.endWriteFileSync(builder);
|
||||||
fbs.Base.startBase(builder);
|
send(builder, fbs.Any.WriteFileSync, msg);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
19
js/timers.ts
19
js/timers.ts
|
@ -3,7 +3,7 @@ import { assert } from "./util";
|
||||||
import * as util from "./util";
|
import * as util from "./util";
|
||||||
import { deno as fbs } from "gen/msg_generated";
|
import { deno as fbs } from "gen/msg_generated";
|
||||||
import { flatbuffers } from "flatbuffers";
|
import { flatbuffers } from "flatbuffers";
|
||||||
import { libdeno } from "./libdeno";
|
import { send } from "./fbs_util";
|
||||||
|
|
||||||
let nextTimerId = 1;
|
let nextTimerId = 1;
|
||||||
|
|
||||||
|
@ -60,13 +60,8 @@ function startTimer(
|
||||||
fbs.TimerStart.addInterval(builder, timer.interval);
|
fbs.TimerStart.addInterval(builder, timer.interval);
|
||||||
fbs.TimerStart.addDelay(builder, timer.delay);
|
fbs.TimerStart.addDelay(builder, timer.delay);
|
||||||
const msg = fbs.TimerStart.endTimerStart(builder);
|
const msg = fbs.TimerStart.endTimerStart(builder);
|
||||||
fbs.Base.startBase(builder);
|
const baseRes = send(builder, fbs.Any.TimerStart, msg);
|
||||||
fbs.Base.addMsg(builder, msg);
|
assert(baseRes == null);
|
||||||
fbs.Base.addMsgType(builder, fbs.Any.TimerStart);
|
|
||||||
builder.finish(fbs.Base.endBase(builder));
|
|
||||||
const resBuf = libdeno.send(builder.asUint8Array());
|
|
||||||
assert(resBuf == null);
|
|
||||||
|
|
||||||
return timer.id;
|
return timer.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,10 +90,6 @@ export function clearTimer(id: number) {
|
||||||
fbs.TimerClear.startTimerClear(builder);
|
fbs.TimerClear.startTimerClear(builder);
|
||||||
fbs.TimerClear.addId(builder, id);
|
fbs.TimerClear.addId(builder, id);
|
||||||
const msg = fbs.TimerClear.endTimerClear(builder);
|
const msg = fbs.TimerClear.endTimerClear(builder);
|
||||||
fbs.Base.startBase(builder);
|
const res = send(builder, fbs.Any.TimerClear, msg);
|
||||||
fbs.Base.addMsg(builder, msg);
|
assert(res == null);
|
||||||
fbs.Base.addMsgType(builder, fbs.Any.TimerClear);
|
|
||||||
builder.finish(fbs.Base.endBase(builder));
|
|
||||||
const resBuf = libdeno.send(builder.asUint8Array());
|
|
||||||
assert(resBuf == null);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
export function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
|
||||||
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
|
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
|
||||||
return ab as ArrayBuffer;
|
return ab as ArrayBuffer;
|
||||||
|
|
Loading…
Reference in a new issue