2019-01-09 12:59:46 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { window } from "./globals";
|
2018-11-08 19:09:18 -05:00
|
|
|
|
2018-10-17 13:04:28 -04:00
|
|
|
import * as flatbuffers from "./flatbuffers";
|
2018-10-03 21:18:23 -04:00
|
|
|
import * as msg 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";
|
2019-01-06 14:17:13 -05:00
|
|
|
import { DenoCompiler } from "./compiler";
|
2018-08-25 15:42:49 -04:00
|
|
|
import { libdeno } from "./libdeno";
|
2018-09-22 08:47:44 -04:00
|
|
|
import { args } from "./deno";
|
2018-09-09 18:54:42 -04:00
|
|
|
import { sendSync, handleAsyncMsgFromRust } from "./dispatch";
|
2018-11-05 12:55:59 -05:00
|
|
|
import { replLoop } from "./repl";
|
2018-10-15 20:52:33 -04:00
|
|
|
import { version } from "typescript";
|
2019-01-09 20:52:13 -05:00
|
|
|
import { postMessage } from "./workers";
|
|
|
|
import { TextDecoder, TextEncoder } from "./text_encoding";
|
|
|
|
import { ModuleSpecifier, ContainingFile } from "./compiler";
|
2018-06-11 21:54:55 -04:00
|
|
|
|
2019-01-09 12:59:46 -05:00
|
|
|
// builtin modules
|
|
|
|
import * as deno from "./deno";
|
2019-01-06 14:17:13 -05:00
|
|
|
|
2019-01-09 20:52:13 -05:00
|
|
|
type CompilerLookup = { specifier: ModuleSpecifier; referrer: ContainingFile };
|
|
|
|
|
|
|
|
// Global reference to StartRes so it can be shared between compilerMain and
|
|
|
|
// denoMain.
|
|
|
|
let startResMsg: msg.StartRes;
|
|
|
|
|
|
|
|
function sendStart(): void {
|
2018-10-17 13:04:28 -04:00
|
|
|
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);
|
2018-08-30 15:35:51 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:18:23 -04:00
|
|
|
assert(msg.Any.StartRes === baseRes!.innerType());
|
2019-01-09 20:52:13 -05:00
|
|
|
startResMsg = new msg.StartRes();
|
|
|
|
assert(baseRes!.inner(startResMsg) != null);
|
2018-07-06 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2019-01-09 12:59:46 -05:00
|
|
|
function compilerMain() {
|
|
|
|
// workerMain should have already been called since a compiler is a worker.
|
|
|
|
const compiler = DenoCompiler.instance();
|
2019-01-09 20:52:13 -05:00
|
|
|
compiler.recompile = startResMsg.recompileFlag();
|
|
|
|
log(`recompile ${compiler.recompile}`);
|
2019-01-09 12:59:46 -05:00
|
|
|
window.onmessage = (e: { data: Uint8Array }) => {
|
|
|
|
const json = new TextDecoder().decode(e.data);
|
|
|
|
const lookup = JSON.parse(json) as CompilerLookup;
|
|
|
|
|
|
|
|
const moduleMetaData = compiler.run(lookup.specifier, lookup.referrer);
|
|
|
|
moduleMetaData.outputCode = compiler.compile(moduleMetaData);
|
|
|
|
|
|
|
|
const responseJson = JSON.stringify(moduleMetaData);
|
|
|
|
const response = new TextEncoder().encode(responseJson);
|
|
|
|
postMessage(response);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
window["compilerMain"] = compilerMain;
|
|
|
|
|
2018-07-21 21:14:52 -04:00
|
|
|
/* tslint:disable-next-line:no-default-export */
|
|
|
|
export default function denoMain() {
|
2018-09-05 22:13:36 -04:00
|
|
|
libdeno.recv(handleAsyncMsgFromRust);
|
2018-08-02 13:13:32 -04:00
|
|
|
|
2019-01-09 12:59:46 -05:00
|
|
|
libdeno.builtinModules["deno"] = deno;
|
|
|
|
// libdeno.builtinModules["typescript"] = typescript;
|
|
|
|
Object.freeze(libdeno.builtinModules);
|
|
|
|
|
2018-09-24 15:33:50 -04:00
|
|
|
// First we send an empty "Start" message to let the privileged side know we
|
2018-07-06 11:27:36 -04:00
|
|
|
// are ready. The response should be a "StartRes" message containing the CLI
|
2018-09-22 08:47:44 -04:00
|
|
|
// args and other info.
|
2019-01-09 20:52:13 -05:00
|
|
|
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-10-11 17:23:22 -04:00
|
|
|
// handle `--types`
|
|
|
|
if (startResMsg.typesFlag()) {
|
2019-01-09 12:59:46 -05:00
|
|
|
const compiler = DenoCompiler.instance();
|
2018-10-11 17:23:22 -04:00
|
|
|
const defaultLibFileName = compiler.getDefaultLibFileName();
|
2019-01-06 14:17:13 -05:00
|
|
|
const defaultLibModule = compiler.resolveModule(defaultLibFileName, "");
|
|
|
|
console.log(defaultLibModule.sourceCode);
|
2018-10-11 17:23:22 -04:00
|
|
|
os.exit(0);
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:52:33 -04:00
|
|
|
// 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 14:16:42 -05:00
|
|
|
os.setPid(startResMsg.pid());
|
|
|
|
|
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
|
|
|
for (let i = 1; i < startResMsg.argvLength(); i++) {
|
2018-09-22 08:47:44 -04:00
|
|
|
args.push(startResMsg.argv(i));
|
2018-06-13 19:40:35 -04:00
|
|
|
}
|
2018-09-22 08:47:44 -04:00
|
|
|
log("args", args);
|
|
|
|
Object.freeze(args);
|
2018-08-17 16:34:30 -04:00
|
|
|
|
2019-01-09 12:59:46 -05:00
|
|
|
const inputFn = args[0];
|
|
|
|
if (!inputFn) {
|
2018-11-05 12:55:59 -05:00
|
|
|
replLoop();
|
|
|
|
}
|
2018-07-21 21:14:52 -04:00
|
|
|
}
|