diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/lib.deno.d.ts | 9 | ||||
-rw-r--r-- | js/main.ts | 2 | ||||
-rw-r--r-- | js/runtime.ts | 33 | ||||
-rw-r--r-- | js/v8_source_maps.ts | 20 |
4 files changed, 49 insertions, 15 deletions
diff --git a/js/lib.deno.d.ts b/js/lib.deno.d.ts index 81e8daf4e..b11a08edf 100644 --- a/js/lib.deno.d.ts +++ b/js/lib.deno.d.ts @@ -27,10 +27,15 @@ declare class Console { interface Window { console: Console; - mainSource: string; // TODO(ry) This shouldn't be global. + // TODO(ry) These shouldn't be global. + mainSource: string; + setMainSourceMap(sm: string): void; } // Globals in the runtime environment declare let console: Console; -declare let mainSource: string; // TODO(ry) This shouldn't be global. declare const window: Window; + +// TODO(ry) These shouldn't be global. +declare let mainSource: string; +declare function setMainSourceMap(sm: string): void; diff --git a/js/main.ts b/js/main.ts index 50f73a4c6..81dabab33 100644 --- a/js/main.ts +++ b/js/main.ts @@ -20,6 +20,8 @@ function startMsg(cmdId: number): Uint8Array { /* tslint:disable-next-line:no-default-export */ export default function denoMain() { + runtime.setup(); + // First we send an empty "Start" message to let the privlaged side know we // are ready. The response should be a "StartRes" message containing the CLI // argv and other info. diff --git a/js/runtime.ts b/js/runtime.ts index 79f463759..4e9cb657d 100644 --- a/js/runtime.ts +++ b/js/runtime.ts @@ -12,7 +12,7 @@ import * as util from "./util"; import { log } from "./util"; import { assetSourceCode } from "./assets"; import * as os from "./os"; -//import * as sourceMaps from "./v8_source_maps"; +import * as sourceMaps from "./v8_source_maps"; import { window, globalEval } from "./globals"; //import * as deno from "./deno"; @@ -39,26 +39,39 @@ window.onerror = ( os.exit(1); }; -/* -export function setup(mainJs: string, mainMap: string): void { +// This is called during snapshot creation with the contents of +// out/debug/gen/bundle/main.js.map. +import { RawSourceMap } from "source-map"; +let mainSourceMap: RawSourceMap = null; +function setMainSourceMap(rawSourceMap: RawSourceMap) { + util.assert(Number(rawSourceMap.version) === 3); + mainSourceMap = rawSourceMap; +} +window["setMainSourceMap"] = setMainSourceMap; + +export function setup(): void { sourceMaps.install({ installPrepareStackTrace: true, - getGeneratedContents: (filename: string): string => { - if (filename === "/main.js") { - return mainJs; - } else if (filename === "/main.map") { - return mainMap; + getGeneratedContents: (filename: string): string | RawSourceMap => { + util.log("getGeneratedContents", filename); + if (filename === "gen/bundle/main.js") { + util.assert(window["mainSource"].length > 0); + return window["mainSource"]; + } else if (filename === "main.js.map") { + return mainSourceMap; + } else if (filename === "deno_main.js") { + return ""; } else { const mod = FileModule.load(filename); if (!mod) { - console.error("getGeneratedContents cannot find", filename); + util.log("getGeneratedContents cannot find", filename); + return null; } return mod.outputCode; } } }); } -*/ // This class represents a module. We call it FileModule to make it explicit // that each module represents a single file. diff --git a/js/v8_source_maps.ts b/js/v8_source_maps.ts index 956ffbe82..d5feeb1c0 100644 --- a/js/v8_source_maps.ts +++ b/js/v8_source_maps.ts @@ -1,7 +1,15 @@ // Copyright 2014 Evan Wallace // Copyright 2018 the Deno authors. All rights reserved. MIT license. // Originated from source-map-support but has been heavily modified for deno. + +// Because NodeJS.CallSite and Error.prepareStackTrace are used we add a +// dependency on the Node types. +// TODO(ry) Ideally this triple slash directive should be removed as we only +// need CallSite and Error.prepareStackTrace but nothing else. +/// <reference types="node" /> + import { SourceMapConsumer, MappedPosition } from "source-map"; +import { RawSourceMap } from "source-map"; import * as base64 from "base64-js"; import { arrayToStr } from "./util"; @@ -24,7 +32,7 @@ interface Position { line: number; } -type GetGeneratedContentsCallback = (fileName: string) => string; +type GetGeneratedContentsCallback = (fileName: string) => string | RawSourceMap; let getGeneratedContents: GetGeneratedContentsCallback; @@ -190,13 +198,16 @@ function loadConsumer(source: string): SourceMapConsumer { if (!code) { return null; } + if (typeof code !== "string") { + throw new Error("expected string"); + } let sourceMappingURL = retrieveSourceMapURL(code); if (!sourceMappingURL) { throw Error("No source map?"); } - let sourceMapData: string; + let sourceMapData: string | RawSourceMap; if (reSourceMap.test(sourceMappingURL)) { // Support source map URL as a data url const rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(",") + 1); @@ -209,8 +220,11 @@ function loadConsumer(source: string): SourceMapConsumer { sourceMapData = getGeneratedContents(sourceMappingURL); } + const rawSourceMap = + typeof sourceMapData === "string" + ? JSON.parse(sourceMapData) + : sourceMapData; //console.log("sourceMapData", sourceMapData); - const rawSourceMap = JSON.parse(sourceMapData); consumer = new SourceMapConsumer(rawSourceMap); consumers.set(source, consumer); } |