2018-07-01 15:23:21 -04:00
|
|
|
// tslint:disable-next-line:no-reference
|
2018-06-11 21:54:55 -04:00
|
|
|
/// <reference path="deno.d.ts" />
|
2018-07-04 14:50:28 -04:00
|
|
|
import { flatbuffers } from "flatbuffers";
|
|
|
|
import { deno as fbs } from "./msg_generated";
|
2018-07-13 03:24:07 -04:00
|
|
|
import { assert, log } from "./util";
|
|
|
|
import * as runtime from "./runtime";
|
2018-06-11 21:54:55 -04:00
|
|
|
|
2018-06-09 18:32:04 -04:00
|
|
|
const globalEval = eval;
|
|
|
|
const window = globalEval("this");
|
2018-06-13 13:38:22 -04:00
|
|
|
|
2018-07-07 20:45:16 -04:00
|
|
|
let cmdIdCounter = 0;
|
|
|
|
function assignCmdId(): number {
|
|
|
|
// TODO(piscisaureus) Safely re-use so they don't overflow.
|
|
|
|
const cmdId = ++cmdIdCounter;
|
|
|
|
assert(cmdId < 2 ** 32, "cmdId overflow");
|
|
|
|
return cmdId;
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:35:34 -04:00
|
|
|
function startMsg(cmdId: number): Uint8Array {
|
2018-07-06 11:27:36 -04:00
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const msg = fbs.Start.createStart(builder, 0);
|
|
|
|
fbs.Base.startBase(builder);
|
2018-07-07 20:45:16 -04:00
|
|
|
fbs.Base.addCmdId(builder, cmdId);
|
2018-07-06 11:27:36 -04:00
|
|
|
fbs.Base.addMsg(builder, msg);
|
|
|
|
fbs.Base.addMsgType(builder, fbs.Any.Start);
|
|
|
|
builder.finish(fbs.Base.endBase(builder));
|
2018-07-08 21:35:34 -04:00
|
|
|
return builder.asUint8Array();
|
2018-07-06 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2018-06-11 16:29:34 -04:00
|
|
|
window["denoMain"] = () => {
|
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-07-07 20:45:16 -04:00
|
|
|
const cmdId = assignCmdId();
|
|
|
|
const res = deno.send(startMsg(cmdId));
|
2018-07-06 11:27:36 -04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2018-07-04 14:50:28 -04:00
|
|
|
|
2018-07-06 11:27:36 -04:00
|
|
|
// Deserialize res into startResMsg.
|
2018-07-08 21:35:34 -04:00
|
|
|
const bb = new flatbuffers.ByteBuffer(res);
|
2018-07-06 11:27:36 -04:00
|
|
|
const base = fbs.Base.getRootAsBase(bb);
|
2018-07-07 20:45:16 -04:00
|
|
|
assert(base.cmdId() === cmdId);
|
2018-07-06 11:27:36 -04:00
|
|
|
assert(fbs.Any.StartRes === base.msgType());
|
|
|
|
const startResMsg = new fbs.StartRes();
|
|
|
|
assert(base.msg(startResMsg) != null);
|
|
|
|
|
|
|
|
const cwd = startResMsg.cwd();
|
2018-07-13 03:24:07 -04:00
|
|
|
log("cwd", cwd);
|
2018-06-13 19:40:35 -04:00
|
|
|
|
2018-07-04 14:50:28 -04:00
|
|
|
const argv: string[] = [];
|
2018-07-06 11:27:36 -04:00
|
|
|
for (let i = 0; i < startResMsg.argvLength(); i++) {
|
|
|
|
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-07-06 11:27:36 -04:00
|
|
|
|
2018-07-13 03:24:07 -04:00
|
|
|
const inputFn = argv[1];
|
2018-07-06 11:27:36 -04:00
|
|
|
const mod = runtime.resolveModule(inputFn, `${cwd}/`);
|
|
|
|
mod.compileAndRun();
|
2018-06-11 15:32:06 -04:00
|
|
|
};
|