mirror of
https://github.com/denoland/deno.git
synced 2025-02-02 12:50:43 -05:00
0ceb554343
* Native ES modules This is a major refactor of internal compiler. Before: JS and TS both were sent through the typescript compiler where their imports were parsed and handled. Both compiled to AMD JS and finally sent to V8 Now: JS is sent directly into V8. TS is sent through the typescript compiler, but tsc generates ES modules now instead of AMD. This generated JS is then dumped into V8. This should much faster for pure JS code. It may improve TS compilation speed. In the future this allows us to separate TS out of the runtime heap and into its own dedicated snapshot. This will result in a smaller runtime heap, and thus should be faster. Some tests were unfortunately disabled to ease landing this patch: 1. compiler_tests.ts which I intend to bring back in later commits. 2. Some text_encoding_test.ts tests which made the file invalid utf8. See PR for a discussion. Also worth noting that this is necessary to support WASM
100 lines
3.2 KiB
TypeScript
100 lines
3.2 KiB
TypeScript
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
import { window } from "./globals";
|
|
|
|
import * as flatbuffers from "./flatbuffers";
|
|
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";
|
|
|
|
// builtin modules
|
|
import * as deno from "./deno";
|
|
|
|
function sendStart(): msg.StartRes {
|
|
const builder = flatbuffers.createBuilder();
|
|
msg.Start.startStart(builder);
|
|
const startOffset = msg.Start.endStart(builder);
|
|
const baseRes = sendSync(builder, msg.Any.Start, startOffset);
|
|
assert(baseRes != null);
|
|
assert(msg.Any.StartRes === baseRes!.innerType());
|
|
const startRes = new msg.StartRes();
|
|
assert(baseRes!.inner(startRes) != null);
|
|
return startRes;
|
|
}
|
|
|
|
import { postMessage } from "./workers";
|
|
import { TextDecoder, TextEncoder } from "./text_encoding";
|
|
import { ModuleSpecifier, ContainingFile } from "./compiler";
|
|
type CompilerLookup = { specifier: ModuleSpecifier; referrer: ContainingFile };
|
|
|
|
function compilerMain() {
|
|
// workerMain should have already been called since a compiler is a worker.
|
|
const compiler = DenoCompiler.instance();
|
|
// compiler.recompile = startResMsg.recompileFlag();
|
|
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;
|
|
|
|
/* tslint:disable-next-line:no-default-export */
|
|
export default function denoMain() {
|
|
libdeno.recv(handleAsyncMsgFromRust);
|
|
|
|
libdeno.builtinModules["deno"] = deno;
|
|
// libdeno.builtinModules["typescript"] = typescript;
|
|
Object.freeze(libdeno.builtinModules);
|
|
|
|
// 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());
|
|
|
|
// handle `--types`
|
|
if (startResMsg.typesFlag()) {
|
|
const compiler = DenoCompiler.instance();
|
|
const defaultLibFileName = compiler.getDefaultLibFileName();
|
|
const defaultLibModule = compiler.resolveModule(defaultLibFileName, "");
|
|
console.log(defaultLibModule.sourceCode);
|
|
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);
|
|
}
|
|
|
|
os.setPid(startResMsg.pid());
|
|
|
|
const cwd = startResMsg.cwd();
|
|
log("cwd", cwd);
|
|
|
|
for (let i = 1; i < startResMsg.argvLength(); i++) {
|
|
args.push(startResMsg.argv(i));
|
|
}
|
|
log("args", args);
|
|
Object.freeze(args);
|
|
|
|
const inputFn = args[0];
|
|
if (!inputFn) {
|
|
replLoop();
|
|
}
|
|
}
|