1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 01:59:06 -05:00
denoland-deno/js/main.ts

59 lines
1.9 KiB
TypeScript
Raw Normal View History

// Copyright 2018 the Deno authors. All rights reserved. MIT license.
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";
2018-07-25 23:07:50 -04:00
import { deno as fbs } from "gen/msg_generated";
import { assert, log, assignCmdId } from "./util";
import * as runtime from "./runtime";
2018-06-11 21:54:55 -04:00
function startMsg(cmdId: number): Uint8Array {
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();
}
2018-07-21 21:14:52 -04:00
/* tslint:disable-next-line:no-default-export */
export default function denoMain() {
runtime.setup();
// 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 = deno.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;
}
2018-07-04 14:50:28 -04:00
// Deserialize res into startResMsg.
const bb = new flatbuffers.ByteBuffer(res);
const base = fbs.Base.getRootAsBase(bb);
assert(base.cmdId() === cmdId);
assert(fbs.Any.StartRes === base.msgType());
const startResMsg = new fbs.StartRes();
assert(base.msg(startResMsg) != null);
const cwd = startResMsg.cwd();
log("cwd", cwd);
2018-06-13 19:40:35 -04:00
2018-07-04 14:50:28 -04:00
const argv: string[] = [];
for (let i = 0; i < startResMsg.argvLength(); i++) {
argv.push(startResMsg.argv(i));
2018-06-13 19:40:35 -04:00
}
log("argv", argv);
const inputFn = argv[1];
const mod = runtime.resolveModule(inputFn, `${cwd}/`);
mod.compileAndRun();
2018-07-21 21:14:52 -04:00
}