deno/js/main.ts

77 lines
2.2 KiB
TypeScript
Raw Normal View History

// Copyright 2018 the Deno authors. All rights reserved. MIT license.
// We need to make sure this module loads, for its side effects.
import "./globals";
import * as flatbuffers from "./flatbuffers";
2018-10-04 01:18:23 +00:00
import * as msg from "gen/msg_generated";
import { assert, log, setLogDebug } from "./util";
import * as os from "./os";
import { DenoCompiler } from "./compiler";
import { libdeno } from "./libdeno";
import { args } from "./deno";
import { sendSync, handleAsyncMsgFromRust } from "./dispatch";
import { replLoop } from "./repl";
import { version } from "typescript";
2018-06-12 01:54:55 +00:00
const compiler = DenoCompiler.instance();
2018-10-04 01:18:23 +00:00
function sendStart(): msg.StartRes {
const builder = flatbuffers.createBuilder();
2018-10-04 01:18:23 +00:00
msg.Start.startStart(builder);
const startOffset = msg.Start.endStart(builder);
const baseRes = sendSync(builder, msg.Any.Start, startOffset);
assert(baseRes != null);
2018-10-04 01:18:23 +00:00
assert(msg.Any.StartRes === baseRes!.innerType());
const startRes = new msg.StartRes();
assert(baseRes!.inner(startRes) != null);
return startRes;
}
2018-07-22 01:14:52 +00:00
/* tslint:disable-next-line:no-default-export */
export default function denoMain() {
libdeno.recv(handleAsyncMsgFromRust);
2018-09-24 19:33:50 +00:00
// First we send an empty "Start" message to let the privileged side know we
// are ready. The response should be a "StartRes" message containing the CLI
// args and other info.
const startResMsg = sendStart();
setLogDebug(startResMsg.debugFlag());
2018-10-11 21:23:22 +00:00
// handle `--types`
if (startResMsg.typesFlag()) {
const defaultLibFileName = compiler.getDefaultLibFileName();
const defaultLibModule = compiler.resolveModule(defaultLibFileName, "");
console.log(defaultLibModule.sourceCode);
2018-10-11 21:23:22 +00:00
os.exit(0);
}
// handle `--version`
if (startResMsg.versionFlag()) {
console.log("deno:", startResMsg.denoVersion());
console.log("v8:", startResMsg.v8Version());
console.log("typescript:", version);
os.exit(0);
}
2019-01-06 19:16:42 +00:00
os.setPid(startResMsg.pid());
const cwd = startResMsg.cwd();
log("cwd", cwd);
2018-06-13 23:40:35 +00:00
2018-08-23 23:46:21 +00:00
for (let i = 1; i < startResMsg.argvLength(); i++) {
args.push(startResMsg.argv(i));
2018-06-13 23:40:35 +00:00
}
log("args", args);
Object.freeze(args);
const inputFn = args[0];
2018-09-24 19:33:50 +00:00
compiler.recompile = startResMsg.recompileFlag();
if (inputFn) {
compiler.run(inputFn, `${cwd}/`);
} else {
replLoop();
}
2018-07-22 01:14:52 +00:00
}