2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-07-04 14:50:28 -04:00
|
|
|
import { flatbuffers } from "flatbuffers";
|
2018-07-25 23:07:50 -04:00
|
|
|
import { deno as fbs } from "gen/msg_generated";
|
2018-08-30 15:35:51 -04:00
|
|
|
import { assert, log, setLogDebug } from "./util";
|
2018-08-17 16:34:30 -04:00
|
|
|
import * as os from "./os";
|
2018-08-22 17:17:26 -04:00
|
|
|
import { DenoCompiler } from "./compiler";
|
2018-08-25 15:42:49 -04:00
|
|
|
import { libdeno } from "./libdeno";
|
2018-08-09 15:17:08 -04:00
|
|
|
import * as timers from "./timers";
|
2018-08-15 20:57:36 -04:00
|
|
|
import { onFetchRes } from "./fetch";
|
2018-08-23 19:46:21 -04:00
|
|
|
import { argv } from "./deno";
|
2018-08-30 15:35:51 -04:00
|
|
|
import { send } from "./fbs_util";
|
2018-06-11 21:54:55 -04:00
|
|
|
|
2018-08-30 15:35:51 -04:00
|
|
|
function sendStart(): fbs.StartRes {
|
2018-07-06 11:27:36 -04:00
|
|
|
const builder = new flatbuffers.Builder();
|
2018-07-23 14:13:12 -04:00
|
|
|
fbs.Start.startStart(builder);
|
|
|
|
const startOffset = fbs.Start.endStart(builder);
|
2018-08-30 15:35:51 -04:00
|
|
|
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;
|
2018-07-06 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2018-08-09 15:17:08 -04:00
|
|
|
function onMessage(ui8: Uint8Array) {
|
|
|
|
const bb = new flatbuffers.ByteBuffer(ui8);
|
|
|
|
const base = fbs.Base.getRootAsBase(bb);
|
|
|
|
switch (base.msgType()) {
|
2018-08-15 20:57:36 -04:00
|
|
|
case fbs.Any.FetchRes: {
|
|
|
|
const msg = new fbs.FetchRes();
|
|
|
|
assert(base.msg(msg) != null);
|
|
|
|
onFetchRes(base, msg);
|
|
|
|
break;
|
|
|
|
}
|
2018-08-09 15:17:08 -04:00
|
|
|
case fbs.Any.TimerReady: {
|
|
|
|
const msg = new fbs.TimerReady();
|
|
|
|
assert(base.msg(msg) != null);
|
|
|
|
timers.onMessage(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
assert(false, "Unhandled message type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 03:57:16 -04:00
|
|
|
function onGlobalError(
|
|
|
|
message: string,
|
|
|
|
source: string,
|
|
|
|
lineno: number,
|
|
|
|
colno: number,
|
|
|
|
error: Error
|
|
|
|
) {
|
|
|
|
console.log(error.stack);
|
|
|
|
os.exit(1);
|
|
|
|
}
|
|
|
|
|
2018-07-21 21:14:52 -04:00
|
|
|
/* tslint:disable-next-line:no-default-export */
|
|
|
|
export default function denoMain() {
|
2018-08-09 15:17:08 -04:00
|
|
|
libdeno.recv(onMessage);
|
2018-08-26 03:57:16 -04:00
|
|
|
libdeno.setGlobalErrorHandler(onGlobalError);
|
2018-08-22 17:17:26 -04:00
|
|
|
const compiler = DenoCompiler.instance();
|
2018-08-02 13:13:32 -04:00
|
|
|
|
2018-07-06 11:27:36 -04:00
|
|
|
// 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.
|
2018-08-30 15:35:51 -04:00
|
|
|
const startResMsg = sendStart();
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-08-22 17:17:26 -04:00
|
|
|
setLogDebug(startResMsg.debugFlag());
|
2018-08-17 16:34:30 -04:00
|
|
|
|
2018-07-06 11:27:36 -04:00
|
|
|
const cwd = startResMsg.cwd();
|
2018-07-13 03:24:07 -04:00
|
|
|
log("cwd", cwd);
|
2018-06-13 19:40:35 -04:00
|
|
|
|
2018-08-23 19:46:21 -04:00
|
|
|
// TODO handle shebang.
|
|
|
|
for (let i = 1; i < startResMsg.argvLength(); i++) {
|
2018-07-06 11:27:36 -04:00
|
|
|
argv.push(startResMsg.argv(i));
|
2018-06-13 19:40:35 -04:00
|
|
|
}
|
2018-07-13 03:24:07 -04:00
|
|
|
log("argv", argv);
|
2018-08-23 19:46:21 -04:00
|
|
|
Object.freeze(argv);
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-08-23 19:46:21 -04:00
|
|
|
const inputFn = argv[0];
|
2018-08-17 16:34:30 -04:00
|
|
|
if (!inputFn) {
|
|
|
|
console.log("No input script specified.");
|
|
|
|
os.exit(1);
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:17:26 -04:00
|
|
|
compiler.run(inputFn, `${cwd}/`);
|
2018-07-21 21:14:52 -04:00
|
|
|
}
|