1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 09:39:08 -05:00
denoland-deno/js/main.ts

105 lines
3.1 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-03 21:18:23 -04:00
import * as msg from "gen/msg_generated";
import { assert, log, setLogDebug } from "./util";
import * as os from "./os";
2018-11-19 18:51:35 -05:00
import { Compiler } from "./compiler";
import { Runner } from "./runner";
import { libdeno } from "./libdeno";
import { args } from "./deno";
import { sendSync, handleAsyncMsgFromRust } from "./dispatch";
2018-10-12 14:22:52 -04:00
import { promiseErrorExaminer, promiseRejectHandler } from "./promise_util";
import { replLoop } from "./repl";
2018-11-05 23:08:25 -05:00
import * as sourceMaps from "./v8_source_maps";
import { version } from "typescript";
2018-06-11 21:54:55 -04:00
2018-11-05 23:08:25 -05:00
// Install the source maps handler and do some pre-calculations so all of it is
// available in the snapshot
2018-11-19 18:51:35 -05:00
const compiler = Compiler.instance();
2018-11-05 23:08:25 -05:00
sourceMaps.install({
installPrepareStackTrace: true,
getGeneratedContents: compiler.getGeneratedContents
});
const consumer = sourceMaps.loadConsumer("gen/bundle/main.js");
assert(consumer != null);
consumer!.computeColumnSpans();
2018-10-03 21:18:23 -04:00
function sendStart(): msg.StartRes {
const builder = flatbuffers.createBuilder();
2018-10-03 21:18:23 -04:00
msg.Start.startStart(builder);
const startOffset = msg.Start.endStart(builder);
const baseRes = sendSync(builder, msg.Any.Start, startOffset);
assert(baseRes != null);
2018-10-03 21:18:23 -04:00
assert(msg.Any.StartRes === baseRes!.innerType());
const startRes = new msg.StartRes();
assert(baseRes!.inner(startRes) != null);
return startRes;
}
function onGlobalError(
message: string,
source: string,
lineno: number,
colno: number,
error: any // tslint:disable-line:no-any
) {
if (error instanceof Error) {
console.log(error.stack);
} else {
console.log(`Thrown: ${String(error)}`);
}
os.exit(1);
}
2018-07-21 21:14:52 -04:00
/* tslint:disable-next-line:no-default-export */
export default function denoMain() {
libdeno.recv(handleAsyncMsgFromRust);
libdeno.setGlobalErrorHandler(onGlobalError);
2018-10-12 14:22:52 -04:00
libdeno.setPromiseRejectHandler(promiseRejectHandler);
libdeno.setPromiseErrorExaminer(promiseErrorExaminer);
2018-09-24 15:33:50 -04: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 17:23:22 -04:00
// handle `--types`
if (startResMsg.typesFlag()) {
const defaultLibFileName = compiler.getDefaultLibFileName();
2018-11-19 18:51:35 -05:00
console.log(compiler.getSource(defaultLibFileName));
2018-10-11 17:23:22 -04: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);
}
const cwd = startResMsg.cwd();
log("cwd", cwd);
2018-06-13 19:40:35 -04:00
2018-08-23 19:46:21 -04:00
for (let i = 1; i < startResMsg.argvLength(); i++) {
args.push(startResMsg.argv(i));
2018-06-13 19:40:35 -04:00
}
log("args", args);
Object.freeze(args);
const inputFn = args[0];
2018-09-24 15:33:50 -04:00
compiler.recompile = startResMsg.recompileFlag();
2018-11-19 18:51:35 -05:00
const runner = new Runner(compiler);
if (inputFn) {
2018-11-19 18:51:35 -05:00
runner.run(inputFn, `${cwd}/`);
} else {
replLoop();
}
2018-07-21 21:14:52 -04:00
}