Process source maps in Rust instead of JS (#1280)

- Improves speed and binary size significantly.
- Makes deno_last_exception() output a JSON structure.
- Isolate::execute and Isolate::event_loop now return
  structured, mapped JSError objects on errors.
- Removes libdeno functions:
  libdeno.setGlobalErrorHandler()
  libdeno.setPromiseRejectHandler()
  libdeno.setPromiseErrorExaminer()

In collaboration with Ryan Dahl.
This commit is contained in:
Ryan Dahl 2018-12-06 23:05:36 -05:00 committed by GitHub
parent 568ac0c902
commit c113df1bb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 854 additions and 807 deletions

View file

@ -3,10 +3,8 @@ import * as ts from "typescript";
import { MediaType } from "gen/msg_generated";
import { assetSourceCode } from "./assets";
import { libdeno } from "./libdeno";
import * as os from "./os";
import { CodeProvider } from "./runner";
import { RawSourceMap } from "./types";
import { assert, log, notImplemented } from "./util";
const EOL = "\n";
@ -71,7 +69,7 @@ export class ModuleMetaData implements ts.IScriptSnapshot {
public readonly mediaType: MediaType,
public readonly sourceCode: SourceCode = "",
public outputCode: OutputCode = "",
public sourceMap: SourceMap | RawSourceMap = ""
public sourceMap: SourceMap = ""
) {
if (outputCode !== "" || fileName.endsWith(".d.ts")) {
this.scriptVersion = "1";
@ -131,8 +129,6 @@ export class Compiler
ContainingFile,
Map<ModuleSpecifier, ModuleFileName>
>();
// Keep track of state of the last module requested via `getGeneratedContents`
private _lastModule: ModuleMetaData | undefined;
// 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
@ -369,19 +365,15 @@ export class Compiler
moduleMetaData.outputCode = `${
outputFile.text
}\n//# sourceURL=${fileName}`;
moduleMetaData.sourceMap = JSON.parse(sourceMapFile.text);
moduleMetaData.sourceMap = sourceMapFile.text;
}
moduleMetaData.scriptVersion = "1";
const sourceMap =
moduleMetaData.sourceMap === "string"
? moduleMetaData.sourceMap
: JSON.stringify(moduleMetaData.sourceMap);
this._os.codeCache(
fileName,
sourceCode,
moduleMetaData.outputCode,
sourceMap
moduleMetaData.sourceMap
);
return moduleMetaData.outputCode;
}
@ -398,36 +390,10 @@ export class Compiler
}
/** Given a fileName, return what was generated by the compiler. */
getGeneratedContents = (fileName: string): string | RawSourceMap => {
this._log("compiler.getGeneratedContents", fileName);
if (fileName === "gen/bundle/main.js") {
assert(libdeno.mainSource.length > 0);
return libdeno.mainSource;
} else if (fileName === "main.js.map") {
return libdeno.mainSourceMap;
} else if (fileName === "deno_main.js") {
return "";
} else if (!fileName.endsWith(".map")) {
const moduleMetaData = this._moduleMetaDataMap.get(fileName);
if (!moduleMetaData) {
this._lastModule = undefined;
return "";
}
this._lastModule = moduleMetaData;
return moduleMetaData.outputCode;
} else {
if (this._lastModule && this._lastModule.sourceMap) {
// Assuming the the map will always be asked for after the source
// code.
const { sourceMap } = this._lastModule;
this._lastModule = undefined;
return sourceMap;
} else {
// Errors thrown here are caught by source-map.
throw new Error(`Unable to find source map: "${fileName}"`);
}
}
};
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