mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 08:09:08 -05:00
Revert "Split Runner from Compiler" (#1462)
This reverts commit 3d03f5b0cb
.
This commit is contained in:
parent
57301909cd
commit
f37d67e809
15 changed files with 534 additions and 521 deletions
1
BUILD.gn
1
BUILD.gn
|
@ -90,7 +90,6 @@ ts_sources = [
|
|||
"js/rename.ts",
|
||||
"js/repl.ts",
|
||||
"js/resources.ts",
|
||||
"js/runner.ts",
|
||||
"js/stat.ts",
|
||||
"js/symlink.ts",
|
||||
"js/text_encoding.ts",
|
||||
|
|
416
js/compiler.ts
416
js/compiler.ts
|
@ -1,16 +1,31 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
import * as ts from "typescript";
|
||||
import { MediaType } from "gen/msg_generated";
|
||||
|
||||
import { assetSourceCode } from "./assets";
|
||||
import * as os from "./os";
|
||||
import { CodeProvider } from "./runner";
|
||||
// tslint:disable-next-line:no-circular-imports
|
||||
import * as deno from "./deno";
|
||||
import { globalEval } from "./global_eval";
|
||||
import { assert, log, notImplemented } from "./util";
|
||||
|
||||
const window = globalEval("this");
|
||||
|
||||
const EOL = "\n";
|
||||
const ASSETS = "$asset$";
|
||||
const LIB_RUNTIME = "lib.deno_runtime.d.ts";
|
||||
|
||||
// tslint:disable:no-any
|
||||
type AmdCallback = (...args: any[]) => void;
|
||||
type AmdErrback = (err: any) => void;
|
||||
export type AmdFactory = (...args: any[]) => object | void;
|
||||
// tslint:enable:no-any
|
||||
export type AmdDefine = (deps: ModuleSpecifier[], factory: AmdFactory) => void;
|
||||
type AMDRequire = (
|
||||
deps: ModuleSpecifier[],
|
||||
callback: AmdCallback,
|
||||
errback: AmdErrback
|
||||
) => void;
|
||||
|
||||
/** The location that a module is being loaded from. This could be a directory,
|
||||
* like `.`, or it could be a module specifier like
|
||||
* `http://gist.github.com/somefile.ts`
|
||||
|
@ -61,6 +76,11 @@ export interface Ts {
|
|||
* the module, not the actual module instance.
|
||||
*/
|
||||
export class ModuleMetaData implements ts.IScriptSnapshot {
|
||||
public deps?: ModuleFileName[];
|
||||
public exports = {};
|
||||
public factory?: AmdFactory;
|
||||
public gatheringDeps = false;
|
||||
public hasRun = false;
|
||||
public scriptVersion = "";
|
||||
|
||||
constructor(
|
||||
|
@ -120,8 +140,8 @@ export function jsonAmdTemplate(
|
|||
* with Deno specific APIs to provide an interface for compiling and running
|
||||
* TypeScript and JavaScript modules.
|
||||
*/
|
||||
export class Compiler
|
||||
implements ts.LanguageServiceHost, ts.FormatDiagnosticsHost, CodeProvider {
|
||||
export class DenoCompiler
|
||||
implements ts.LanguageServiceHost, ts.FormatDiagnosticsHost {
|
||||
// Modules are usually referenced by their ModuleSpecifier and ContainingFile,
|
||||
// and keeping a map of the resolved module file name allows more efficient
|
||||
// future resolution
|
||||
|
@ -129,6 +149,8 @@ export class Compiler
|
|||
ContainingFile,
|
||||
Map<ModuleSpecifier, ModuleFileName>
|
||||
>();
|
||||
// A reference to global eval, so it can be monkey patched during testing
|
||||
private _globalEval = globalEval;
|
||||
// A reference to the log utility, so it can be monkey patched during testing
|
||||
private _log = log;
|
||||
// A map of module file names to module meta data
|
||||
|
@ -151,7 +173,10 @@ export class Compiler
|
|||
// A reference to the `./os.ts` module, so it can be monkey patched during
|
||||
// testing
|
||||
private _os: Os = os;
|
||||
// Used to contain the script file we are currently compiling
|
||||
// Contains a queue of modules that have been resolved, but not yet
|
||||
// run
|
||||
private _runQueue: ModuleMetaData[] = [];
|
||||
// Used to contain the script file we are currently running
|
||||
private _scriptFileNames: string[] = [];
|
||||
// A reference to the TypeScript LanguageService instance so it can be
|
||||
// monkey patched during testing
|
||||
|
@ -159,9 +184,84 @@ export class Compiler
|
|||
// A reference to `typescript` module so it can be monkey patched during
|
||||
// testing
|
||||
private _ts: Ts = ts;
|
||||
// A reference to the global scope so it can be monkey patched during
|
||||
// testing
|
||||
private _window = window;
|
||||
// Flags forcing recompilation of TS code
|
||||
public recompile = false;
|
||||
|
||||
/** Drain the run queue, retrieving the arguments for the module
|
||||
* factory and calling the module's factory.
|
||||
*/
|
||||
private _drainRunQueue(): void {
|
||||
this._log(
|
||||
"compiler._drainRunQueue",
|
||||
this._runQueue.map(metaData => metaData.fileName)
|
||||
);
|
||||
let moduleMetaData: ModuleMetaData | undefined;
|
||||
while ((moduleMetaData = this._runQueue.shift())) {
|
||||
assert(
|
||||
moduleMetaData.factory != null,
|
||||
"Cannot run module without factory."
|
||||
);
|
||||
assert(moduleMetaData.hasRun === false, "Module has already been run.");
|
||||
// asserts not tracked by TypeScripts, so using not null operator
|
||||
const exports = moduleMetaData.factory!(
|
||||
...this._getFactoryArguments(moduleMetaData)
|
||||
);
|
||||
// For JSON module support and potential future features.
|
||||
// TypeScript always imports `exports` and mutates it directly, but the
|
||||
// AMD specification allows values to be returned from the factory.
|
||||
if (exports != null) {
|
||||
moduleMetaData.exports = exports;
|
||||
}
|
||||
moduleMetaData.hasRun = true;
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the dependencies for a given module, but don't run the module,
|
||||
* just add the module factory to the run queue.
|
||||
*/
|
||||
private _gatherDependencies(moduleMetaData: ModuleMetaData): void {
|
||||
this._log("compiler._resolveDependencies", moduleMetaData.fileName);
|
||||
|
||||
// if the module has already run, we can short circuit.
|
||||
// it is intentional though that if we have already resolved dependencies,
|
||||
// we won't short circuit, as something may have changed, or we might have
|
||||
// only collected the dependencies to be able to able to obtain the graph of
|
||||
// dependencies
|
||||
if (moduleMetaData.hasRun) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._window.define = this._makeDefine(moduleMetaData);
|
||||
this._globalEval(this.compile(moduleMetaData));
|
||||
this._window.define = undefined;
|
||||
}
|
||||
|
||||
/** Retrieve the arguments to pass a module's factory function. */
|
||||
// tslint:disable-next-line:no-any
|
||||
private _getFactoryArguments(moduleMetaData: ModuleMetaData): any[] {
|
||||
if (!moduleMetaData.deps) {
|
||||
throw new Error("Cannot get arguments until dependencies resolved.");
|
||||
}
|
||||
return moduleMetaData.deps.map(dep => {
|
||||
if (dep === "require") {
|
||||
return this._makeLocalRequire(moduleMetaData);
|
||||
}
|
||||
if (dep === "exports") {
|
||||
return moduleMetaData.exports;
|
||||
}
|
||||
if (dep in DenoCompiler._builtins) {
|
||||
return DenoCompiler._builtins[dep];
|
||||
}
|
||||
const dependencyMetaData = this._getModuleMetaData(dep);
|
||||
assert(dependencyMetaData != null, `Missing dependency "${dep}".`);
|
||||
// TypeScript does not track assert, therefore using not null operator
|
||||
return dependencyMetaData!.exports;
|
||||
});
|
||||
}
|
||||
|
||||
/** The TypeScript language service often refers to the resolved fileName of
|
||||
* a module, this is a shortcut to avoid unnecessary module resolution logic
|
||||
* for modules that may have been initially resolved by a `moduleSpecifier`
|
||||
|
@ -176,10 +276,70 @@ export class Compiler
|
|||
return this._moduleMetaDataMap.has(fileName)
|
||||
? this._moduleMetaDataMap.get(fileName)
|
||||
: fileName.startsWith(ASSETS)
|
||||
? this._resolveModule(fileName, "")
|
||||
? this.resolveModule(fileName, "")
|
||||
: undefined;
|
||||
}
|
||||
|
||||
/** Create a localized AMD `define` function and return it. */
|
||||
private _makeDefine(moduleMetaData: ModuleMetaData): AmdDefine {
|
||||
return (deps: ModuleSpecifier[], factory: AmdFactory): void => {
|
||||
this._log("compiler.localDefine", moduleMetaData.fileName);
|
||||
moduleMetaData.factory = factory;
|
||||
// when there are circular dependencies, we need to skip recursing the
|
||||
// dependencies
|
||||
moduleMetaData.gatheringDeps = true;
|
||||
// we will recursively resolve the dependencies for any modules
|
||||
moduleMetaData.deps = deps.map(dep => {
|
||||
if (
|
||||
dep === "require" ||
|
||||
dep === "exports" ||
|
||||
dep in DenoCompiler._builtins
|
||||
) {
|
||||
return dep;
|
||||
}
|
||||
const dependencyMetaData = this.resolveModule(
|
||||
dep,
|
||||
moduleMetaData.fileName
|
||||
);
|
||||
if (!dependencyMetaData.gatheringDeps) {
|
||||
this._gatherDependencies(dependencyMetaData);
|
||||
}
|
||||
return dependencyMetaData.fileName;
|
||||
});
|
||||
moduleMetaData.gatheringDeps = false;
|
||||
if (!this._runQueue.includes(moduleMetaData)) {
|
||||
this._runQueue.push(moduleMetaData);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** Returns a require that specifically handles the resolution of a transpiled
|
||||
* emit of a dynamic ES `import()` from TypeScript.
|
||||
*/
|
||||
private _makeLocalRequire(moduleMetaData: ModuleMetaData): AMDRequire {
|
||||
return (
|
||||
deps: ModuleSpecifier[],
|
||||
callback: AmdCallback,
|
||||
errback: AmdErrback
|
||||
): void => {
|
||||
log("localRequire", deps);
|
||||
assert(
|
||||
deps.length === 1,
|
||||
"Local require requires exactly one dependency."
|
||||
);
|
||||
const [moduleSpecifier] = deps;
|
||||
try {
|
||||
const requiredMetaData = this.run(
|
||||
moduleSpecifier,
|
||||
moduleMetaData.fileName
|
||||
);
|
||||
callback(requiredMetaData.exports);
|
||||
} catch (e) {
|
||||
errback(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** Given a `moduleSpecifier` and `containingFile` retrieve the cached
|
||||
* `fileName` for a given module. If the module has yet to be resolved
|
||||
* this will return `undefined`.
|
||||
|
@ -196,81 +356,6 @@ export class Compiler
|
|||
return undefined;
|
||||
}
|
||||
|
||||
/** Given a `moduleSpecifier` and `containingFile`, resolve the module and
|
||||
* return the `ModuleMetaData`.
|
||||
*/
|
||||
private _resolveModule(
|
||||
moduleSpecifier: ModuleSpecifier,
|
||||
containingFile: ContainingFile
|
||||
): ModuleMetaData {
|
||||
this._log("compiler.resolveModule", { moduleSpecifier, containingFile });
|
||||
assert(moduleSpecifier != null && moduleSpecifier.length > 0);
|
||||
let fileName = this._resolveFileName(moduleSpecifier, containingFile);
|
||||
if (fileName && this._moduleMetaDataMap.has(fileName)) {
|
||||
return this._moduleMetaDataMap.get(fileName)!;
|
||||
}
|
||||
let moduleId: ModuleId | undefined;
|
||||
let mediaType = MediaType.Unknown;
|
||||
let sourceCode: SourceCode | undefined;
|
||||
let outputCode: OutputCode | undefined;
|
||||
let sourceMap: SourceMap | undefined;
|
||||
if (
|
||||
moduleSpecifier.startsWith(ASSETS) ||
|
||||
containingFile.startsWith(ASSETS)
|
||||
) {
|
||||
// Assets are compiled into the runtime javascript bundle.
|
||||
// we _know_ `.pop()` will return a string, but TypeScript doesn't so
|
||||
// not null assertion
|
||||
moduleId = moduleSpecifier.split("/").pop()!;
|
||||
const assetName = moduleId.includes(".") ? moduleId : `${moduleId}.d.ts`;
|
||||
assert(assetName in assetSourceCode, `No such asset "${assetName}"`);
|
||||
mediaType = MediaType.TypeScript;
|
||||
sourceCode = assetSourceCode[assetName];
|
||||
fileName = `${ASSETS}/${assetName}`;
|
||||
outputCode = "";
|
||||
sourceMap = "";
|
||||
} else {
|
||||
// We query Rust with a CodeFetch message. It will load the sourceCode,
|
||||
// and if there is any outputCode cached, will return that as well.
|
||||
const fetchResponse = this._os.codeFetch(moduleSpecifier, containingFile);
|
||||
moduleId = fetchResponse.moduleName;
|
||||
fileName = fetchResponse.filename;
|
||||
mediaType = fetchResponse.mediaType;
|
||||
sourceCode = fetchResponse.sourceCode;
|
||||
outputCode = fetchResponse.outputCode;
|
||||
sourceMap =
|
||||
fetchResponse.sourceMap && JSON.parse(fetchResponse.sourceMap);
|
||||
}
|
||||
assert(moduleId != null, "No module ID.");
|
||||
assert(fileName != null, "No file name.");
|
||||
assert(
|
||||
mediaType !== MediaType.Unknown,
|
||||
`Unknown media type for: "${moduleSpecifier}" from "${containingFile}".`
|
||||
);
|
||||
this._log(
|
||||
"resolveModule sourceCode length:",
|
||||
sourceCode && sourceCode.length
|
||||
);
|
||||
this._log("resolveModule has outputCode:", outputCode != null);
|
||||
this._log("resolveModule has source map:", sourceMap != null);
|
||||
this._log("resolveModule has media type:", MediaType[mediaType]);
|
||||
// fileName is asserted above, but TypeScript does not track so not null
|
||||
this._setFileName(moduleSpecifier, containingFile, fileName!);
|
||||
if (fileName && this._moduleMetaDataMap.has(fileName)) {
|
||||
return this._moduleMetaDataMap.get(fileName)!;
|
||||
}
|
||||
const moduleMetaData = new ModuleMetaData(
|
||||
moduleId!,
|
||||
fileName!,
|
||||
mediaType,
|
||||
sourceCode,
|
||||
outputCode,
|
||||
sourceMap
|
||||
);
|
||||
this._moduleMetaDataMap.set(fileName!, moduleMetaData);
|
||||
return moduleMetaData;
|
||||
}
|
||||
|
||||
/** Caches the resolved `fileName` in relationship to the `moduleSpecifier`
|
||||
* and `containingFile` in order to reduce calls to the privileged side
|
||||
* to retrieve the contents of a module.
|
||||
|
@ -290,7 +375,7 @@ export class Compiler
|
|||
}
|
||||
|
||||
private constructor() {
|
||||
if (Compiler._instance) {
|
||||
if (DenoCompiler._instance) {
|
||||
throw new TypeError("Attempt to create an additional compiler.");
|
||||
}
|
||||
this._service = this._ts.createLanguageService(this);
|
||||
|
@ -377,42 +462,130 @@ export class Compiler
|
|||
return moduleMetaData.outputCode;
|
||||
}
|
||||
|
||||
/** Given a module specifier and a containing file, return the filename of the
|
||||
* module. If the module is not resolvable, the method will throw.
|
||||
*/
|
||||
getFilename(
|
||||
moduleSpecifier: ModuleSpecifier,
|
||||
containingFile: ContainingFile
|
||||
): ModuleFileName {
|
||||
const moduleMetaData = this._resolveModule(moduleSpecifier, containingFile);
|
||||
return moduleMetaData.fileName;
|
||||
}
|
||||
|
||||
/** Given a fileName, return what was generated by the compiler. */
|
||||
getGeneratedSourceMap(fileName: string): string {
|
||||
const moduleMetaData = this._moduleMetaDataMap.get(fileName);
|
||||
return moduleMetaData ? moduleMetaData.sourceMap : "";
|
||||
}
|
||||
|
||||
/** Get the output code for a module based on its filename. A call to
|
||||
* `.getFilename()` should occur before attempting to get the output code as
|
||||
* this ensures the module is loaded.
|
||||
/** For a given module specifier and containing file, return a list of
|
||||
* absolute identifiers for dependent modules that are required by this
|
||||
* module.
|
||||
*/
|
||||
getOutput(filename: ModuleFileName): OutputCode {
|
||||
const moduleMetaData = this._getModuleMetaData(filename)!;
|
||||
assert(moduleMetaData != null, `Module not loaded: "${filename}"`);
|
||||
this._scriptFileNames = [moduleMetaData.fileName];
|
||||
return this.compile(moduleMetaData);
|
||||
getModuleDependencies(
|
||||
moduleSpecifier: ModuleSpecifier,
|
||||
containingFile: ContainingFile
|
||||
): ModuleFileName[] {
|
||||
assert(
|
||||
this._runQueue.length === 0,
|
||||
"Cannot get dependencies with modules queued to be run."
|
||||
);
|
||||
const moduleMetaData = this.resolveModule(moduleSpecifier, containingFile);
|
||||
assert(
|
||||
!moduleMetaData.hasRun,
|
||||
"Cannot get dependencies for a module that has already been run."
|
||||
);
|
||||
this._gatherDependencies(moduleMetaData);
|
||||
const dependencies = this._runQueue.map(
|
||||
moduleMetaData => moduleMetaData.moduleId
|
||||
);
|
||||
// empty the run queue, to free up references to factories we have collected
|
||||
// and to ensure that if there is a further invocation of `.run()` the
|
||||
// factories don't get called
|
||||
this._runQueue = [];
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
/** Get the source code for a module based on its filename. A call to
|
||||
* `.getFilename()` should occur before attempting to get the output code as
|
||||
* this ensures the module is loaded.
|
||||
/** Given a `moduleSpecifier` and `containingFile`, resolve the module and
|
||||
* return the `ModuleMetaData`.
|
||||
*/
|
||||
getSource(filename: ModuleFileName): SourceCode {
|
||||
const moduleMetaData = this._getModuleMetaData(filename)!;
|
||||
assert(moduleMetaData != null, `Module not loaded: "${filename}"`);
|
||||
return moduleMetaData.sourceCode;
|
||||
resolveModule(
|
||||
moduleSpecifier: ModuleSpecifier,
|
||||
containingFile: ContainingFile
|
||||
): ModuleMetaData {
|
||||
this._log("compiler.resolveModule", { moduleSpecifier, containingFile });
|
||||
assert(moduleSpecifier != null && moduleSpecifier.length > 0);
|
||||
let fileName = this._resolveFileName(moduleSpecifier, containingFile);
|
||||
if (fileName && this._moduleMetaDataMap.has(fileName)) {
|
||||
return this._moduleMetaDataMap.get(fileName)!;
|
||||
}
|
||||
let moduleId: ModuleId | undefined;
|
||||
let mediaType = MediaType.Unknown;
|
||||
let sourceCode: SourceCode | undefined;
|
||||
let outputCode: OutputCode | undefined;
|
||||
let sourceMap: SourceMap | undefined;
|
||||
if (
|
||||
moduleSpecifier.startsWith(ASSETS) ||
|
||||
containingFile.startsWith(ASSETS)
|
||||
) {
|
||||
// Assets are compiled into the runtime javascript bundle.
|
||||
// we _know_ `.pop()` will return a string, but TypeScript doesn't so
|
||||
// not null assertion
|
||||
moduleId = moduleSpecifier.split("/").pop()!;
|
||||
const assetName = moduleId.includes(".") ? moduleId : `${moduleId}.d.ts`;
|
||||
assert(assetName in assetSourceCode, `No such asset "${assetName}"`);
|
||||
mediaType = MediaType.TypeScript;
|
||||
sourceCode = assetSourceCode[assetName];
|
||||
fileName = `${ASSETS}/${assetName}`;
|
||||
outputCode = "";
|
||||
sourceMap = "";
|
||||
} else {
|
||||
// We query Rust with a CodeFetch message. It will load the sourceCode,
|
||||
// and if there is any outputCode cached, will return that as well.
|
||||
const fetchResponse = this._os.codeFetch(moduleSpecifier, containingFile);
|
||||
moduleId = fetchResponse.moduleName;
|
||||
fileName = fetchResponse.filename;
|
||||
mediaType = fetchResponse.mediaType;
|
||||
sourceCode = fetchResponse.sourceCode;
|
||||
outputCode = fetchResponse.outputCode;
|
||||
sourceMap =
|
||||
fetchResponse.sourceMap && JSON.parse(fetchResponse.sourceMap);
|
||||
}
|
||||
assert(moduleId != null, "No module ID.");
|
||||
assert(fileName != null, "No file name.");
|
||||
assert(
|
||||
mediaType !== MediaType.Unknown,
|
||||
`Unknown media type for: "${moduleSpecifier}" from "${containingFile}".`
|
||||
);
|
||||
this._log(
|
||||
"resolveModule sourceCode length:",
|
||||
sourceCode && sourceCode.length
|
||||
);
|
||||
this._log("resolveModule has outputCode:", outputCode != null);
|
||||
this._log("resolveModule has source map:", sourceMap != null);
|
||||
this._log("resolveModule has media type:", MediaType[mediaType]);
|
||||
// fileName is asserted above, but TypeScript does not track so not null
|
||||
this._setFileName(moduleSpecifier, containingFile, fileName!);
|
||||
if (fileName && this._moduleMetaDataMap.has(fileName)) {
|
||||
return this._moduleMetaDataMap.get(fileName)!;
|
||||
}
|
||||
const moduleMetaData = new ModuleMetaData(
|
||||
moduleId!,
|
||||
fileName!,
|
||||
mediaType,
|
||||
sourceCode,
|
||||
outputCode,
|
||||
sourceMap
|
||||
);
|
||||
this._moduleMetaDataMap.set(fileName!, moduleMetaData);
|
||||
return moduleMetaData;
|
||||
}
|
||||
|
||||
/** Load and run a module and all of its dependencies based on a module
|
||||
* specifier and a containing file
|
||||
*/
|
||||
run(
|
||||
moduleSpecifier: ModuleSpecifier,
|
||||
containingFile: ContainingFile
|
||||
): ModuleMetaData {
|
||||
this._log("compiler.run", { moduleSpecifier, containingFile });
|
||||
const moduleMetaData = this.resolveModule(moduleSpecifier, containingFile);
|
||||
this._scriptFileNames = [moduleMetaData.fileName];
|
||||
if (!moduleMetaData.deps) {
|
||||
this._gatherDependencies(moduleMetaData);
|
||||
}
|
||||
this._drainRunQueue();
|
||||
return moduleMetaData;
|
||||
}
|
||||
|
||||
// TypeScript Language Service and Format Diagnostic Host API
|
||||
|
@ -476,7 +649,7 @@ export class Compiler
|
|||
getDefaultLibFileName(): string {
|
||||
this._log("getDefaultLibFileName()");
|
||||
const moduleSpecifier = LIB_RUNTIME;
|
||||
const moduleMetaData = this._resolveModule(moduleSpecifier, ASSETS);
|
||||
const moduleMetaData = this.resolveModule(moduleSpecifier, ASSETS);
|
||||
return moduleMetaData.fileName;
|
||||
}
|
||||
|
||||
|
@ -506,11 +679,11 @@ export class Compiler
|
|||
let moduleMetaData: ModuleMetaData;
|
||||
if (name === "deno") {
|
||||
// builtin modules are part of the runtime lib
|
||||
moduleMetaData = this._resolveModule(LIB_RUNTIME, ASSETS);
|
||||
moduleMetaData = this.resolveModule(LIB_RUNTIME, ASSETS);
|
||||
} else if (name === "typescript") {
|
||||
moduleMetaData = this._resolveModule("typescript.d.ts", ASSETS);
|
||||
moduleMetaData = this.resolveModule("typescript.d.ts", ASSETS);
|
||||
} else {
|
||||
moduleMetaData = this._resolveModule(name, containingFile);
|
||||
moduleMetaData = this.resolveModule(name, containingFile);
|
||||
}
|
||||
// According to the interface we shouldn't return `undefined` but if we
|
||||
// fail to return the same length of modules to those we cannot resolve
|
||||
|
@ -533,10 +706,23 @@ export class Compiler
|
|||
|
||||
// Deno specific static properties and methods
|
||||
|
||||
private static _instance: Compiler | undefined;
|
||||
/** Built in modules which can be returned to external modules
|
||||
*
|
||||
* Placed as a private static otherwise we get use before
|
||||
* declared with the `DenoCompiler`
|
||||
*/
|
||||
// tslint:disable-next-line:no-any
|
||||
private static _builtins: { [mid: string]: any } = {
|
||||
typescript: ts,
|
||||
deno
|
||||
};
|
||||
|
||||
private static _instance: DenoCompiler | undefined;
|
||||
|
||||
/** Returns the instance of `DenoCompiler` or creates a new instance. */
|
||||
static instance(): Compiler {
|
||||
return Compiler._instance || (Compiler._instance = new Compiler());
|
||||
static instance(): DenoCompiler {
|
||||
return (
|
||||
DenoCompiler._instance || (DenoCompiler._instance = new DenoCompiler())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import * as ts from "typescript";
|
|||
// We use a silly amount of `any` in these tests...
|
||||
// tslint:disable:no-any
|
||||
|
||||
const { Compiler, jsonAmdTemplate } = (deno as any)._compiler;
|
||||
const { DenoCompiler, jsonAmdTemplate } = (deno as any)._compiler;
|
||||
|
||||
interface ModuleInfo {
|
||||
moduleName: string | undefined;
|
||||
|
@ -17,14 +17,16 @@ interface ModuleInfo {
|
|||
sourceMap: string | undefined;
|
||||
}
|
||||
|
||||
const compilerInstance = Compiler.instance();
|
||||
const compilerInstance = DenoCompiler.instance();
|
||||
|
||||
// References to original items we are going to mock
|
||||
const originals = {
|
||||
_globalEval: (compilerInstance as any)._globalEval,
|
||||
_log: (compilerInstance as any)._log,
|
||||
_os: (compilerInstance as any)._os,
|
||||
_ts: (compilerInstance as any)._ts,
|
||||
_service: (compilerInstance as any)._service
|
||||
_service: (compilerInstance as any)._service,
|
||||
_window: (compilerInstance as any)._window
|
||||
};
|
||||
|
||||
enum MediaType {
|
||||
|
@ -250,6 +252,7 @@ const emittedFiles = {
|
|||
"/root/project/foo/qat.ts": "console.log('foo');"
|
||||
};
|
||||
|
||||
let globalEvalStack: string[] = [];
|
||||
let getEmitOutputStack: string[] = [];
|
||||
let logStack: any[][] = [];
|
||||
let codeCacheStack: Array<{
|
||||
|
@ -266,6 +269,12 @@ let codeFetchStack: Array<{
|
|||
let mockDepsStack: string[][] = [];
|
||||
let mockFactoryStack: any[] = [];
|
||||
|
||||
function globalEvalMock(x: string): void {
|
||||
globalEvalStack.push(x);
|
||||
if (windowMock.define && mockDepsStack.length && mockFactoryStack.length) {
|
||||
windowMock.define(mockDepsStack.pop(), mockFactoryStack.pop());
|
||||
}
|
||||
}
|
||||
function logMock(...args: any[]): void {
|
||||
logStack.push(args);
|
||||
}
|
||||
|
@ -305,7 +314,6 @@ const osMock = {
|
|||
throw new Error(`Unexpected call to os.exit(${code})`);
|
||||
}
|
||||
};
|
||||
|
||||
const tsMock = {
|
||||
createLanguageService(host: ts.LanguageServiceHost): ts.LanguageService {
|
||||
return {} as ts.LanguageService;
|
||||
|
@ -355,11 +363,14 @@ const serviceMock = {
|
|||
);
|
||||
}
|
||||
};
|
||||
const windowMock: { define?: any } = {};
|
||||
const mocks = {
|
||||
_globalEval: globalEvalMock,
|
||||
_log: logMock,
|
||||
_os: osMock,
|
||||
_ts: tsMock,
|
||||
_service: serviceMock
|
||||
_service: serviceMock,
|
||||
_window: windowMock
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -383,6 +394,7 @@ function teardown() {
|
|||
codeCacheStack = [];
|
||||
logStack = [];
|
||||
getEmitOutputStack = [];
|
||||
globalEvalStack = [];
|
||||
|
||||
assertEqual(mockDepsStack.length, 0);
|
||||
assertEqual(mockFactoryStack.length, 0);
|
||||
|
@ -401,10 +413,7 @@ test(function testJsonAmdTemplate() {
|
|||
factory = f;
|
||||
}
|
||||
|
||||
const code = jsonAmdTemplate(
|
||||
`{ "hello": "world", "foo": "bar" }`,
|
||||
"example.json"
|
||||
);
|
||||
const code = jsonAmdTemplate(`{ "hello": "world", "foo": "bar" }`);
|
||||
const result = eval(code);
|
||||
assert(result == null);
|
||||
assertEqual(deps && deps.length, 0);
|
||||
|
@ -414,51 +423,156 @@ test(function testJsonAmdTemplate() {
|
|||
});
|
||||
|
||||
test(function compilerInstance() {
|
||||
assert(Compiler != null);
|
||||
assert(Compiler.instance() != null);
|
||||
assert(DenoCompiler != null);
|
||||
assert(DenoCompiler.instance() != null);
|
||||
});
|
||||
|
||||
// Testing the internal APIs
|
||||
|
||||
test(function testGetFilename() {
|
||||
test(function compilerRun() {
|
||||
// equal to `deno foo/bar.ts`
|
||||
setup();
|
||||
let factoryRun = false;
|
||||
mockDepsStack.push(["require", "exports", "deno"]);
|
||||
mockFactoryStack.push((_require, _exports, _deno) => {
|
||||
factoryRun = true;
|
||||
assertEqual(typeof _require, "function");
|
||||
assertEqual(typeof _exports, "object");
|
||||
assert(_deno === deno);
|
||||
_exports.foo = "bar";
|
||||
});
|
||||
const moduleMetaData = compilerInstance.run("foo/bar.ts", "/root/project");
|
||||
assert(factoryRun);
|
||||
assert(moduleMetaData.hasRun);
|
||||
assertEqual(moduleMetaData.sourceCode, fooBarTsSource);
|
||||
assertEqual(moduleMetaData.outputCode, fooBarTsOutput);
|
||||
// assertEqual(JSON.stringify(moduleMetaData.sourceMap), fooBarTsSourcemap);
|
||||
assertEqual(moduleMetaData.exports, { foo: "bar" });
|
||||
|
||||
assertEqual(
|
||||
compilerInstance.getFilename("foo/bar.ts", "/root/project"),
|
||||
"/root/project/foo/bar.ts"
|
||||
codeFetchStack.length,
|
||||
1,
|
||||
"Module should have only been fetched once."
|
||||
);
|
||||
assertEqual(
|
||||
codeCacheStack.length,
|
||||
1,
|
||||
"Compiled code should have only been cached once."
|
||||
);
|
||||
const [codeCacheCall] = codeCacheStack;
|
||||
assertEqual(codeCacheCall.fileName, "/root/project/foo/bar.ts");
|
||||
assertEqual(codeCacheCall.sourceCode, fooBarTsSource);
|
||||
assertEqual(codeCacheCall.outputCode, fooBarTsOutput);
|
||||
assertEqual(codeCacheCall.sourceMap, fooBarTsSourcemap);
|
||||
teardown();
|
||||
});
|
||||
|
||||
test(function testGetOutput() {
|
||||
test(function compilerRunMultiModule() {
|
||||
// equal to `deno foo/baz.ts`
|
||||
setup();
|
||||
const filename = compilerInstance.getFilename("foo/bar.ts", "/root/project");
|
||||
assertEqual(compilerInstance.getOutput(filename), fooBarTsOutput);
|
||||
const factoryStack: string[] = [];
|
||||
const bazDeps = ["require", "exports", "./bar.ts"];
|
||||
const bazFactory = (_require, _exports, _bar) => {
|
||||
factoryStack.push("baz");
|
||||
assertEqual(_bar.foo, "bar");
|
||||
};
|
||||
const barDeps = ["require", "exports", "deno"];
|
||||
const barFactory = (_require, _exports, _deno) => {
|
||||
factoryStack.push("bar");
|
||||
_exports.foo = "bar";
|
||||
};
|
||||
mockDepsStack.push(barDeps);
|
||||
mockFactoryStack.push(barFactory);
|
||||
mockDepsStack.push(bazDeps);
|
||||
mockFactoryStack.push(bazFactory);
|
||||
compilerInstance.run("foo/baz.ts", "/root/project");
|
||||
assertEqual(factoryStack, ["bar", "baz"]);
|
||||
|
||||
assertEqual(
|
||||
codeFetchStack.length,
|
||||
2,
|
||||
"Modules should have only been fetched once."
|
||||
);
|
||||
assertEqual(codeCacheStack.length, 0, "No code should have been cached.");
|
||||
teardown();
|
||||
});
|
||||
|
||||
test(function testGetOutputJson() {
|
||||
test(function compilerRunCircularDependency() {
|
||||
setup();
|
||||
const filename = compilerInstance.getFilename(
|
||||
"./config.json",
|
||||
"/root/project/loadConfig.ts"
|
||||
);
|
||||
assertEqual(
|
||||
compilerInstance.getOutput(filename),
|
||||
jsonAmdTemplate(configJsonSource, filename)
|
||||
);
|
||||
const factoryStack: string[] = [];
|
||||
const modADeps = ["require", "exports", "./modB.ts"];
|
||||
const modAFactory = (_require, _exports, _modB) => {
|
||||
assertEqual(_modB.foo, "bar");
|
||||
factoryStack.push("modA");
|
||||
_exports.bar = "baz";
|
||||
_modB.assertModA();
|
||||
};
|
||||
const modBDeps = ["require", "exports", "./modA.ts"];
|
||||
const modBFactory = (_require, _exports, _modA) => {
|
||||
assertEqual(_modA, {});
|
||||
factoryStack.push("modB");
|
||||
_exports.foo = "bar";
|
||||
_exports.assertModA = () => {
|
||||
assertEqual(_modA, {
|
||||
bar: "baz"
|
||||
});
|
||||
};
|
||||
};
|
||||
mockDepsStack.push(modBDeps);
|
||||
mockFactoryStack.push(modBFactory);
|
||||
mockDepsStack.push(modADeps);
|
||||
mockFactoryStack.push(modAFactory);
|
||||
compilerInstance.run("modA.ts", "/root/project");
|
||||
assertEqual(factoryStack, ["modB", "modA"]);
|
||||
teardown();
|
||||
});
|
||||
|
||||
test(function testGetSource() {
|
||||
test(function compilerLoadJsonModule() {
|
||||
setup();
|
||||
const filename = compilerInstance.getFilename("foo/bar.ts", "/root/project");
|
||||
assertEqual(compilerInstance.getSource(filename), fooBarTsSource);
|
||||
const factoryStack: string[] = [];
|
||||
const configJsonDeps: string[] = [];
|
||||
const configJsonFactory = () => {
|
||||
factoryStack.push("configJson");
|
||||
return JSON.parse(configJsonSource);
|
||||
};
|
||||
const loadConfigDeps = ["require", "exports", "./config.json"];
|
||||
const loadConfigFactory = (_require, _exports, _config) => {
|
||||
factoryStack.push("loadConfig");
|
||||
assertEqual(_config, JSON.parse(configJsonSource));
|
||||
};
|
||||
|
||||
mockDepsStack.push(configJsonDeps);
|
||||
mockFactoryStack.push(configJsonFactory);
|
||||
mockDepsStack.push(loadConfigDeps);
|
||||
mockFactoryStack.push(loadConfigFactory);
|
||||
compilerInstance.run("loadConfig.ts", "/root/project");
|
||||
assertEqual(factoryStack, ["configJson", "loadConfig"]);
|
||||
teardown();
|
||||
});
|
||||
|
||||
test(function testGetOutputUnknownMediaType() {
|
||||
test(function compilerResolveModule() {
|
||||
setup();
|
||||
const moduleMetaData = compilerInstance.resolveModule(
|
||||
"foo/baz.ts",
|
||||
"/root/project"
|
||||
);
|
||||
assertEqual(moduleMetaData.sourceCode, fooBazTsSource);
|
||||
assertEqual(moduleMetaData.outputCode, fooBazTsOutput);
|
||||
assertEqual(JSON.stringify(moduleMetaData.sourceMap), fooBazTsSourcemap);
|
||||
assert(!moduleMetaData.hasRun);
|
||||
assert(!moduleMetaData.deps);
|
||||
assertEqual(moduleMetaData.exports, {});
|
||||
assertEqual(moduleMetaData.scriptVersion, "1");
|
||||
|
||||
assertEqual(codeFetchStack.length, 1, "Only initial module is resolved.");
|
||||
teardown();
|
||||
});
|
||||
|
||||
test(function compilerResolveModuleUnknownMediaType() {
|
||||
setup();
|
||||
let didThrow = false;
|
||||
try {
|
||||
compilerInstance.getFilename("some.txt", "/root/project");
|
||||
compilerInstance.resolveModule("some.txt", "/root/project");
|
||||
} catch (e) {
|
||||
assert(e instanceof Error);
|
||||
assertEqual(
|
||||
|
@ -471,6 +585,28 @@ test(function testGetOutputUnknownMediaType() {
|
|||
teardown();
|
||||
});
|
||||
|
||||
test(function compilerGetModuleDependencies() {
|
||||
setup();
|
||||
const bazDeps = ["require", "exports", "./bar.ts"];
|
||||
const bazFactory = () => {
|
||||
throw new Error("Unexpected factory call");
|
||||
};
|
||||
const barDeps = ["require", "exports", "deno"];
|
||||
const barFactory = () => {
|
||||
throw new Error("Unexpected factory call");
|
||||
};
|
||||
mockDepsStack.push(barDeps);
|
||||
mockFactoryStack.push(barFactory);
|
||||
mockDepsStack.push(bazDeps);
|
||||
mockFactoryStack.push(bazFactory);
|
||||
const deps = compilerInstance.getModuleDependencies(
|
||||
"foo/baz.ts",
|
||||
"/root/project"
|
||||
);
|
||||
assertEqual(deps, ["/root/project/foo/bar.ts", "/root/project/foo/baz.ts"]);
|
||||
teardown();
|
||||
});
|
||||
|
||||
// TypeScript LanguageServiceHost APIs
|
||||
|
||||
test(function compilerGetCompilationSettings() {
|
||||
|
@ -498,8 +634,7 @@ test(function compilerGetNewLine() {
|
|||
|
||||
test(function compilerGetScriptFileNames() {
|
||||
setup();
|
||||
const filename = compilerInstance.getFilename("foo/bar.ts", "/root/project");
|
||||
compilerInstance.getOutput(filename);
|
||||
compilerInstance.run("foo/bar.ts", "/root/project");
|
||||
const result = compilerInstance.getScriptFileNames();
|
||||
assertEqual(result.length, 1, "Expected only a single filename.");
|
||||
assertEqual(result[0], "/root/project/foo/bar.ts");
|
||||
|
@ -508,22 +643,21 @@ test(function compilerGetScriptFileNames() {
|
|||
|
||||
test(function compilerRecompileFlag() {
|
||||
setup();
|
||||
const filename = compilerInstance.getFilename("foo/bar.ts", "/root/project");
|
||||
compilerInstance.getOutput(filename);
|
||||
compilerInstance.run("foo/bar.ts", "/root/project");
|
||||
assertEqual(
|
||||
getEmitOutputStack.length,
|
||||
1,
|
||||
"Expected only a single emitted file."
|
||||
);
|
||||
// running compiler against same file should use cached code
|
||||
compilerInstance.getOutput(filename);
|
||||
compilerInstance.run("foo/bar.ts", "/root/project");
|
||||
assertEqual(
|
||||
getEmitOutputStack.length,
|
||||
1,
|
||||
"Expected only a single emitted file."
|
||||
);
|
||||
compilerInstance.recompile = true;
|
||||
compilerInstance.getOutput(filename);
|
||||
compilerInstance.run("foo/bar.ts", "/root/project");
|
||||
assertEqual(getEmitOutputStack.length, 2, "Expected two emitted file.");
|
||||
assert(
|
||||
getEmitOutputStack[0] === getEmitOutputStack[1],
|
||||
|
@ -534,25 +668,43 @@ test(function compilerRecompileFlag() {
|
|||
|
||||
test(function compilerGetScriptKind() {
|
||||
setup();
|
||||
compilerInstance.getFilename("foo.ts", "/moduleKinds");
|
||||
compilerInstance.getFilename("foo.d.ts", "/moduleKinds");
|
||||
compilerInstance.getFilename("foo.js", "/moduleKinds");
|
||||
compilerInstance.getFilename("foo.json", "/moduleKinds");
|
||||
compilerInstance.getFilename("foo.txt", "/moduleKinds");
|
||||
assertEqual(compilerInstance.getScriptKind("/moduleKinds/foo.ts"), 3);
|
||||
assertEqual(compilerInstance.getScriptKind("/moduleKinds/foo.d.ts"), 3);
|
||||
assertEqual(compilerInstance.getScriptKind("/moduleKinds/foo.js"), 1);
|
||||
assertEqual(compilerInstance.getScriptKind("/moduleKinds/foo.json"), 6);
|
||||
assertEqual(compilerInstance.getScriptKind("/moduleKinds/foo.txt"), 1);
|
||||
compilerInstance.resolveModule("foo.ts", "/moduleKinds");
|
||||
compilerInstance.resolveModule("foo.d.ts", "/moduleKinds");
|
||||
compilerInstance.resolveModule("foo.js", "/moduleKinds");
|
||||
compilerInstance.resolveModule("foo.json", "/moduleKinds");
|
||||
compilerInstance.resolveModule("foo.txt", "/moduleKinds");
|
||||
assertEqual(
|
||||
compilerInstance.getScriptKind("/moduleKinds/foo.ts"),
|
||||
ts.ScriptKind.TS
|
||||
);
|
||||
assertEqual(
|
||||
compilerInstance.getScriptKind("/moduleKinds/foo.d.ts"),
|
||||
ts.ScriptKind.TS
|
||||
);
|
||||
assertEqual(
|
||||
compilerInstance.getScriptKind("/moduleKinds/foo.js"),
|
||||
ts.ScriptKind.JS
|
||||
);
|
||||
assertEqual(
|
||||
compilerInstance.getScriptKind("/moduleKinds/foo.json"),
|
||||
ts.ScriptKind.JSON
|
||||
);
|
||||
assertEqual(
|
||||
compilerInstance.getScriptKind("/moduleKinds/foo.txt"),
|
||||
ts.ScriptKind.JS
|
||||
);
|
||||
teardown();
|
||||
});
|
||||
|
||||
test(function compilerGetScriptVersion() {
|
||||
setup();
|
||||
const filename = compilerInstance.getFilename("foo/bar.ts", "/root/project");
|
||||
compilerInstance.getOutput(filename);
|
||||
const moduleMetaData = compilerInstance.resolveModule(
|
||||
"foo/bar.ts",
|
||||
"/root/project"
|
||||
);
|
||||
compilerInstance.compile(moduleMetaData);
|
||||
assertEqual(
|
||||
compilerInstance.getScriptVersion(filename),
|
||||
compilerInstance.getScriptVersion(moduleMetaData.fileName),
|
||||
"1",
|
||||
"Expected known module to have script version of 1"
|
||||
);
|
||||
|
@ -569,8 +721,11 @@ test(function compilerGetScriptVersionUnknown() {
|
|||
|
||||
test(function compilerGetScriptSnapshot() {
|
||||
setup();
|
||||
const filename = compilerInstance.getFilename("foo/bar.ts", "/root/project");
|
||||
const result = compilerInstance.getScriptSnapshot(filename);
|
||||
const moduleMetaData = compilerInstance.resolveModule(
|
||||
"foo/bar.ts",
|
||||
"/root/project"
|
||||
);
|
||||
const result = compilerInstance.getScriptSnapshot(moduleMetaData.fileName);
|
||||
assert(result != null, "Expected snapshot to be defined.");
|
||||
assertEqual(result.getLength(), fooBarTsSource.length);
|
||||
assertEqual(
|
||||
|
@ -582,6 +737,11 @@ test(function compilerGetScriptSnapshot() {
|
|||
// This is and optional part of the `IScriptSnapshot` API which we don't
|
||||
// define, os checking for the lack of this property.
|
||||
assert(!("dispose" in result));
|
||||
|
||||
assert(
|
||||
result === moduleMetaData,
|
||||
"result should strictly equal moduleMetaData"
|
||||
);
|
||||
teardown();
|
||||
});
|
||||
|
||||
|
@ -615,8 +775,11 @@ test(function compilerReadFile() {
|
|||
|
||||
test(function compilerFileExists() {
|
||||
setup();
|
||||
const filename = compilerInstance.getFilename("foo/bar.ts", "/root/project");
|
||||
assert(compilerInstance.fileExists(filename));
|
||||
const moduleMetaData = compilerInstance.resolveModule(
|
||||
"foo/bar.ts",
|
||||
"/root/project"
|
||||
);
|
||||
assert(compilerInstance.fileExists(moduleMetaData.fileName));
|
||||
assert(compilerInstance.fileExists("$asset$/lib.deno_runtime.d.ts"));
|
||||
assertEqual(
|
||||
compilerInstance.fileExists("/root/project/unknown-module.ts"),
|
||||
|
|
|
@ -66,6 +66,3 @@ export { deferred } from "./util";
|
|||
import * as compiler from "./compiler";
|
||||
// @internal
|
||||
export const _compiler = compiler;
|
||||
import * as runner from "./runner";
|
||||
// @internal
|
||||
export const _runner = runner;
|
||||
|
|
13
js/main.ts
13
js/main.ts
|
@ -6,14 +6,15 @@ import * as flatbuffers from "./flatbuffers";
|
|||
import * as msg from "gen/msg_generated";
|
||||
import { assert, log, setLogDebug } from "./util";
|
||||
import * as os from "./os";
|
||||
import { Compiler } from "./compiler";
|
||||
import { Runner } from "./runner";
|
||||
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";
|
||||
|
||||
const compiler = DenoCompiler.instance();
|
||||
|
||||
function sendStart(): msg.StartRes {
|
||||
const builder = flatbuffers.createBuilder();
|
||||
msg.Start.startStart(builder);
|
||||
|
@ -37,12 +38,11 @@ export default function denoMain() {
|
|||
|
||||
setLogDebug(startResMsg.debugFlag());
|
||||
|
||||
const compiler = Compiler.instance();
|
||||
|
||||
// handle `--types`
|
||||
if (startResMsg.typesFlag()) {
|
||||
const defaultLibFileName = compiler.getDefaultLibFileName();
|
||||
console.log(compiler.getSource(defaultLibFileName));
|
||||
const defaultLibModule = compiler.resolveModule(defaultLibFileName, "");
|
||||
console.log(defaultLibModule.sourceCode);
|
||||
os.exit(0);
|
||||
}
|
||||
|
||||
|
@ -67,10 +67,9 @@ export default function denoMain() {
|
|||
const inputFn = args[0];
|
||||
|
||||
compiler.recompile = startResMsg.recompileFlag();
|
||||
const runner = new Runner(compiler);
|
||||
|
||||
if (inputFn) {
|
||||
runner.run(inputFn, `${cwd}/`);
|
||||
compiler.run(inputFn, `${cwd}/`);
|
||||
} else {
|
||||
replLoop();
|
||||
}
|
||||
|
|
187
js/runner.ts
187
js/runner.ts
|
@ -1,187 +0,0 @@
|
|||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||
// tslint:disable-next-line:no-circular-imports
|
||||
import * as deno from "./deno";
|
||||
import { globalEval } from "./global_eval";
|
||||
import { assert, log } from "./util";
|
||||
|
||||
// tslint:disable:no-any
|
||||
type AmdCallback = (...args: any[]) => void;
|
||||
type AmdDefine = (deps: ModuleSpecifier[], factory: AmdFactory) => void;
|
||||
type AmdErrback = (err: any) => void;
|
||||
type AmdFactory = (...args: any[]) => object | void;
|
||||
type AmdRequire = (
|
||||
deps: ModuleSpecifier[],
|
||||
callback: AmdCallback,
|
||||
errback: AmdErrback
|
||||
) => void;
|
||||
// tslint:enable:no-any
|
||||
|
||||
// tslint:disable-next-line:no-any
|
||||
type BuiltinMap = { [moduleSpecifier: string]: any };
|
||||
|
||||
// Type aliases to make the code more readable
|
||||
type ContainingFile = string;
|
||||
type Filename = string;
|
||||
type ModuleSpecifier = string;
|
||||
type OutputCode = string;
|
||||
|
||||
/** Internal representation of a module being loaded */
|
||||
class Module {
|
||||
deps?: Filename[];
|
||||
factory?: AmdFactory;
|
||||
|
||||
// tslint:disable-next-line:no-any
|
||||
constructor(public filename: Filename, public exports: any) {}
|
||||
}
|
||||
|
||||
/** External APIs which the runner depends upon to be able to retrieve
|
||||
* transpiled modules.
|
||||
*/
|
||||
export interface CodeProvider {
|
||||
/** Given a module specifier and a containing file, return the filename. */
|
||||
getFilename(
|
||||
moduleSpecifier: ModuleSpecifier,
|
||||
containingFile: ContainingFile
|
||||
): Filename;
|
||||
|
||||
/** Given a filename, return the transpiled output code. */
|
||||
getOutput(filename: Filename): OutputCode;
|
||||
}
|
||||
|
||||
const window = globalEval("this");
|
||||
|
||||
/** A class which can load and run modules into the current environment. */
|
||||
export class Runner {
|
||||
private _globalEval = globalEval;
|
||||
/** A map of modules indexed by filename. */
|
||||
private _modules = new Map<Filename, Module>();
|
||||
private _provider: CodeProvider;
|
||||
/** Modules are placed in here to have their factories run after all the
|
||||
* the dependencies have been collected.
|
||||
*/
|
||||
private _runQueue: Module[] = [];
|
||||
|
||||
private _drainRunQueue(): void {
|
||||
log("runner._drainRunQueue", this._runQueue.length);
|
||||
let module: Module | undefined;
|
||||
while ((module = this._runQueue.shift())) {
|
||||
assert(module.factory != null, "Cannot run module without factory.");
|
||||
// TypeScript always imports `exports` and mutates it directly, but the
|
||||
// AMD specification allows values to be returned from the factory and
|
||||
// is the case with JSON modules and potentially other future features.
|
||||
const exports = module.factory!(...this._getFactoryArguments(module));
|
||||
if (exports != null) {
|
||||
module.exports = exports;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _gatherDependencies(filename: Filename): void {
|
||||
log("runner._gatherDependencies", filename);
|
||||
|
||||
if (this._modules.has(filename)) {
|
||||
log("Module already exists:", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
const module = new Module(filename, {});
|
||||
this._modules.set(filename, module);
|
||||
|
||||
window.define = this._makeDefine(module);
|
||||
this._globalEval(this._provider.getOutput(filename));
|
||||
window.define = undefined;
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:no-any
|
||||
private _getFactoryArguments(module: Module): any[] {
|
||||
log("runner._getFactoryArguments", module.filename);
|
||||
assert(module.deps != null, "Missing dependencies for module.");
|
||||
return module.deps!.map(dep => {
|
||||
if (dep === "require") {
|
||||
return this._makeLocalRequire(module);
|
||||
}
|
||||
if (dep === "exports") {
|
||||
return module.exports;
|
||||
}
|
||||
if (dep in Runner._builtins) {
|
||||
return Runner._builtins[dep];
|
||||
}
|
||||
const depModule = this._modules.get(dep)!;
|
||||
assert(dep != null, `Missing dependency "${dep}"`);
|
||||
return depModule.exports;
|
||||
});
|
||||
}
|
||||
|
||||
private _makeDefine(module: Module): AmdDefine {
|
||||
log("runner._makeDefine", module.filename);
|
||||
return (deps: ModuleSpecifier[], factory: AmdFactory): void => {
|
||||
module.factory = factory;
|
||||
module.deps = deps.map(dep => {
|
||||
if (dep === "require" || dep === "exports" || dep in Runner._builtins) {
|
||||
return dep;
|
||||
}
|
||||
const depFilename = this._provider.getFilename(dep, module.filename);
|
||||
if (!this._modules.get(depFilename)) {
|
||||
this._gatherDependencies(depFilename);
|
||||
}
|
||||
return depFilename;
|
||||
});
|
||||
if (!this._runQueue.includes(module)) {
|
||||
this._runQueue.push(module);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private _makeLocalRequire(module: Module): AmdRequire {
|
||||
log("runner._makeLocalRequire", module.filename);
|
||||
return (
|
||||
deps: ModuleSpecifier[],
|
||||
callback: AmdCallback,
|
||||
errback: AmdErrback
|
||||
): void => {
|
||||
log("runner._makeLocalRequire", deps);
|
||||
assert(
|
||||
deps.length === 1,
|
||||
"Local require supports exactly one dependency."
|
||||
);
|
||||
const [moduleSpecifier] = deps;
|
||||
try {
|
||||
this.run(moduleSpecifier, module.filename);
|
||||
const requiredFilename = this._provider.getFilename(
|
||||
moduleSpecifier,
|
||||
module.filename
|
||||
);
|
||||
const requiredModule = this._modules.get(requiredFilename)!;
|
||||
assert(requiredModule != null);
|
||||
callback(requiredModule.exports);
|
||||
} catch (e) {
|
||||
errback(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
constructor(provider: CodeProvider) {
|
||||
this._provider = provider;
|
||||
}
|
||||
|
||||
/** Given a module specifier and the containing file, resolve the module and
|
||||
* ensure that it is in the runtime environment, returning the exports of the
|
||||
* module.
|
||||
*/
|
||||
// tslint:disable-next-line:no-any
|
||||
run(moduleSpecifier: ModuleSpecifier, containingFile: ContainingFile): any {
|
||||
log("runner.run", moduleSpecifier, containingFile);
|
||||
const filename = this._provider.getFilename(
|
||||
moduleSpecifier,
|
||||
containingFile
|
||||
);
|
||||
if (!this._modules.has(filename)) {
|
||||
this._gatherDependencies(filename);
|
||||
this._drainRunQueue();
|
||||
}
|
||||
return this._modules.get(filename)!.exports;
|
||||
}
|
||||
|
||||
/** Builtin modules which can be loaded by user modules. */
|
||||
private static _builtins: BuiltinMap = { deno };
|
||||
}
|
|
@ -1,141 +0,0 @@
|
|||
import { test, assert, assertEqual } from "./test_util.ts";
|
||||
import * as deno from "deno";
|
||||
|
||||
// tslint:disable-next-line:no-any
|
||||
const Runner = (deno as any)._runner.Runner;
|
||||
|
||||
let mockGetOutputStack: string[] = [];
|
||||
let mockGetFilenameStack: Array<[string, string]> = [];
|
||||
|
||||
// tslint:disable:max-line-length
|
||||
const mockOutput = {
|
||||
"/root/project/foo/bar.ts": `define(["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.foo = "bar";
|
||||
});
|
||||
//# sourceMappingURL=bar.js.map
|
||||
//# sourceURL=/root/project/foo/bar.ts`,
|
||||
"/root/project/foo/baz.ts": `define(["require", "exports", "./qat.ts"], function (require, exports, qat) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.foo = qat.foo;
|
||||
});
|
||||
//# sourceMappingURL=baz.js.map
|
||||
//# sourceURL=/root/project/foo/baz.ts`,
|
||||
"/root/project/foo/qat.ts": `define(["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.foo = "qat";
|
||||
});
|
||||
//# sourceMappingURL=qat.js.map
|
||||
//# sourceURL=/root/project/foo/qat.ts`,
|
||||
"/root/project/foo/config.json": `define([], function () {
|
||||
return JSON.parse('{"foo":{"bar": true,"baz": ["qat", 1]}}');
|
||||
});
|
||||
//# sourceURL=/root/project/foo/config.json`,
|
||||
"/circular/modA.ts": `define(["require", "exports", "./modB.ts"], function (require, exports, modB) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.foo = modB.foo;
|
||||
});
|
||||
//# sourceMappingURL=modA.js.map
|
||||
//# sourceURL=/circular/modA.ts`,
|
||||
"/circular/modB.ts": `define(["require", "exports", "./modA.ts"], function (require, exports, modA) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
if (modA && typeof modA === "object") {
|
||||
modA;
|
||||
} else {
|
||||
throw new Error("Mod A is empty!");
|
||||
}
|
||||
exports.foo = "bar";
|
||||
});
|
||||
//# sourceMappingURL=modB.js.map
|
||||
//# sourceURL=/circular/modB.ts`
|
||||
};
|
||||
// tslint:enable
|
||||
|
||||
const mockFilenames = {
|
||||
"/root/project": {
|
||||
"foo/bar.ts": "/root/project/foo/bar.ts",
|
||||
"foo/baz.ts": "/root/project/foo/baz.ts",
|
||||
"foo/config.json": "/root/project/foo/config.json"
|
||||
},
|
||||
"/root/project/foo/baz.ts": {
|
||||
"./qat.ts": "/root/project/foo/qat.ts"
|
||||
},
|
||||
"/circular": {
|
||||
"modA.ts": "/circular/modA.ts"
|
||||
},
|
||||
"/circular/modA.ts": {
|
||||
"./modB.ts": "/circular/modB.ts"
|
||||
},
|
||||
"/circular/modB.ts": {
|
||||
"./modA.ts": "/circular/modA.ts"
|
||||
}
|
||||
};
|
||||
|
||||
const mockCodeProvider = {
|
||||
getOutput(filename: string) {
|
||||
mockGetOutputStack.push(filename);
|
||||
if (filename in mockOutput) {
|
||||
return mockOutput[filename];
|
||||
}
|
||||
throw new Error("Module not found.");
|
||||
},
|
||||
getFilename(moduleSpecifier: string, containingFile: string) {
|
||||
mockGetFilenameStack.push([moduleSpecifier, containingFile]);
|
||||
if (
|
||||
containingFile in mockFilenames &&
|
||||
moduleSpecifier in mockFilenames[containingFile]
|
||||
) {
|
||||
return mockFilenames[containingFile][moduleSpecifier];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function setup() {
|
||||
mockGetOutputStack = [];
|
||||
mockGetFilenameStack = [];
|
||||
return new Runner(mockCodeProvider);
|
||||
}
|
||||
|
||||
test(function runnerConstruction() {
|
||||
const runner = setup();
|
||||
assert(runner);
|
||||
});
|
||||
|
||||
test(function runnerRun() {
|
||||
const runner = setup();
|
||||
const result = runner.run("foo/bar.ts", "/root/project");
|
||||
assertEqual(result, { foo: "bar" });
|
||||
assertEqual(mockGetFilenameStack, [["foo/bar.ts", "/root/project"]]);
|
||||
assertEqual(mockGetOutputStack, ["/root/project/foo/bar.ts"]);
|
||||
});
|
||||
|
||||
test(function runnerRunImports() {
|
||||
const runner = setup();
|
||||
const result = runner.run("foo/baz.ts", "/root/project");
|
||||
assertEqual(result, { foo: "qat" });
|
||||
assertEqual(mockGetFilenameStack, [
|
||||
["foo/baz.ts", "/root/project"],
|
||||
["./qat.ts", "/root/project/foo/baz.ts"]
|
||||
]);
|
||||
assertEqual(mockGetOutputStack, [
|
||||
"/root/project/foo/baz.ts",
|
||||
"/root/project/foo/qat.ts"
|
||||
]);
|
||||
});
|
||||
|
||||
test(function runnerRunExportReturn() {
|
||||
const runner = setup();
|
||||
const result = runner.run("foo/config.json", "/root/project");
|
||||
assertEqual(result, { foo: { bar: true, baz: ["qat", 1] } });
|
||||
});
|
||||
|
||||
test(function runnerCircularReference() {
|
||||
const runner = setup();
|
||||
const result = runner.run("modA.ts", "/circular");
|
||||
assertEqual(result, { foo: "bar" });
|
||||
});
|
|
@ -32,7 +32,6 @@ import "./read_file_test.ts";
|
|||
import "./read_link_test.ts";
|
||||
import "./rename_test.ts";
|
||||
import "./resources_test.ts";
|
||||
import "./runner_test.ts";
|
||||
import "./stat_test.ts";
|
||||
import "./symlink_test.ts";
|
||||
import "./text_encoding_test.ts";
|
||||
|
|
|
@ -497,7 +497,6 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn js_error_apply_source_map_2() {
|
||||
// Because this is accessing the live bundle, this test might be more fragile
|
||||
let e = JSError {
|
||||
message: "TypeError: baz".to_string(),
|
||||
frames: vec![StackFrame {
|
||||
|
@ -513,9 +512,8 @@ mod tests {
|
|||
let getter = MockSourceMapGetter {};
|
||||
let actual = e.apply_source_map(&getter);
|
||||
assert_eq!(actual.message, "TypeError: baz");
|
||||
// Because this is accessing the live bundle, this test might be more fragile
|
||||
assert_eq!(actual.frames.len(), 1);
|
||||
assert_eq!(actual.frames[0].line, 15);
|
||||
assert_eq!(actual.frames[0].column, 16);
|
||||
assert!(actual.frames[0].script_name.ends_with("js/util.ts"));
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ world
|
|||
Error: error
|
||||
at foo ([WILDCARD]tests/async_error.ts:4:9)
|
||||
at eval ([WILDCARD]tests/async_error.ts:7:1)
|
||||
at _gatherDependencies ([WILDCARD]/js/runner.ts:[WILDCARD])
|
||||
at run ([WILDCARD]/js/runner.ts:[WILDCARD])
|
||||
at _gatherDependencies ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at run ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at denoMain ([WILDCARD]/js/main.ts:[WILDCARD])
|
||||
at <anonymous>:1:1
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
at foo (file://[WILDCARD]tests/error_001.ts:2:9)
|
||||
at bar (file://[WILDCARD]tests/error_001.ts:6:3)
|
||||
at eval (file://[WILDCARD]tests/error_001.ts:9:1)
|
||||
at _gatherDependencies ([WILDCARD]/js/runner.ts:[WILDCARD])
|
||||
at run ([WILDCARD]/js/runner.ts:[WILDCARD])
|
||||
at _gatherDependencies ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at run ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at denoMain ([WILDCARD]/js/main.ts:[WILDCARD])
|
||||
at <anonymous>:1:1
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
at throwsError (file://[WILDCARD]/tests/subdir/mod1.ts:16:9)
|
||||
at foo (file://[WILDCARD]/tests/error_002.ts:4:3)
|
||||
at eval (file://[WILDCARD]/tests/error_002.ts:7:1)
|
||||
at _drainRunQueue ([WILDCARD]/js/runner.ts:[WILDCARD])
|
||||
at run ([WILDCARD]/js/runner.ts:[WILDCARD])
|
||||
at _drainRunQueue ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at run ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at denoMain ([WILDCARD]/js/main.ts:[WILDCARD])
|
||||
at <anonymous>:1:1
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
at maybeThrowError ([WILDCARD]/js/errors.ts:[WILDCARD])
|
||||
at sendSync ([WILDCARD]/js/dispatch.ts:[WILDCARD])
|
||||
at codeFetch ([WILDCARD]/js/os.ts:[WILDCARD])
|
||||
at _resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at moduleNames.map.name ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at resolveModuleNames ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at compilerHost.resolveModuleNames ([WILDCARD]typescript.js:[WILDCARD])
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
at maybeThrowError ([WILDCARD]/js/errors.ts:[WILDCARD])
|
||||
at sendSync ([WILDCARD]/js/dispatch.ts:[WILDCARD])
|
||||
at codeFetch ([WILDCARD]/js/os.ts:[WILDCARD])
|
||||
at _resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at moduleNames.map.name ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at resolveModuleNames ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at compilerHost.resolveModuleNames ([WILDCARD])
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
at maybeThrowError ([WILDCARD]/js/errors.ts:[WILDCARD])
|
||||
at sendSync ([WILDCARD]/js/dispatch.ts:[WILDCARD])
|
||||
at codeFetch ([WILDCARD]/js/os.ts:[WILDCARD])
|
||||
at _resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at resolveModule ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at moduleNames.map.name ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at resolveModuleNames ([WILDCARD]/js/compiler.ts:[WILDCARD])
|
||||
at compilerHost.resolveModuleNames ([WILDCARD])
|
||||
|
|
Loading…
Reference in a new issue