Bug 852792 - load sources from the sourcesContent field in a source map, if available; r=robcee

This commit is contained in:
Nick Fitzgerald 2013-05-11 10:22:36 +01:00
parent 8f426ba999
commit 4754d7fefd
3 changed files with 107 additions and 5 deletions

View File

@ -1381,10 +1381,13 @@ PauseScopedActor.prototype = {
* The url of the source we are representing.
* @param aThreadActor ThreadActor
* The current thread actor.
* @param aSourceContent String
* Optional. The contents of the source, if we already know it.
*/
function SourceActor(aUrl, aThreadActor) {
function SourceActor(aUrl, aThreadActor, aSourceContent=null) {
this._threadActor = aThreadActor;
this._url = aUrl;
this._sourceContent = aSourceContent;
}
SourceActor.prototype = {
@ -1412,6 +1415,14 @@ SourceActor.prototype = {
* Handler for the "source" packet.
*/
onSource: function SA_onSource(aRequest) {
if (this._sourceContent) {
return {
from: this.actorID,
source: this.threadActor.createValueGrip(
this._sourceContent, this.threadActor.threadLifetimePool)
};
}
return fetch(this._url)
.then(function(aSource) {
return this.threadActor.createValueGrip(
@ -2431,10 +2442,13 @@ ThreadSources.prototype = {
* Right now this takes a URL, but in the future it should
* take a Debugger.Source. See bug 637572.
*
* @param string the source URL.
* @param String aURL
* The source URL.
* @param String aSourceContent
* Optional. The content of the source, if we already know it.
* @returns a SourceActor representing the source or null.
*/
source: function TS_source(aURL) {
source: function TS_source(aURL, aSourceContent=null) {
if (!this._allow(aURL)) {
return null;
}
@ -2443,7 +2457,7 @@ ThreadSources.prototype = {
return this._sourceActors[aURL];
}
let actor = new SourceActor(aURL, this._thread);
let actor = new SourceActor(aURL, this._thread, aSourceContent);
this._thread.threadLifetimePool.addActor(actor);
this._sourceActors[aURL] = actor;
try {
@ -2465,7 +2479,8 @@ ThreadSources.prototype = {
return this.sourceMap(aScript)
.then((aSourceMap) => {
return [
this.source(s) for (s of aSourceMap.sources)
this.source(s, aSourceMap.sourceContentFor(s))
for (s of aSourceMap.sources)
];
}, (e) => {
reportError(e);

View File

@ -0,0 +1,86 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* Check that we can load sources whose content is embedded in the
* "sourcesContent" field of a source map.
*/
var gDebuggee;
var gClient;
var gThreadClient;
Components.utils.import("resource:///modules/devtools/SourceMap.jsm");
function run_test()
{
initTestDebuggerServer();
gDebuggee = addTestGlobal("test-source-map");
gClient = new DebuggerClient(DebuggerServer.connectPipe());
gClient.connect(function() {
attachTestGlobalClientAndResume(gClient, "test-source-map", function(aResponse, aThreadClient) {
gThreadClient = aThreadClient;
test_source_content();
});
});
do_test_pending();
}
function test_source_content()
{
let numNewSources = 0;
gClient.addListener("newSource", function _onNewSource(aEvent, aPacket) {
if (++numNewSources !== 3) {
return;
}
gClient.removeListener("newSource", _onNewSource);
gThreadClient.getSources(function (aResponse) {
do_check_true(!aResponse.error, "Should not get an error");
testContents(aResponse.sources, () => {
finishClient(gClient);
});
});
});
let node = new SourceNode(null, null, null, [
new SourceNode(1, 0, "a.js", "function a() { return 'a'; }\n"),
new SourceNode(1, 0, "b.js", "function b() { return 'b'; }\n"),
new SourceNode(1, 0, "c.js", "function c() { return 'c'; }\n"),
]);
node.setSourceContent("a.js", "content for a.js");
node.setSourceContent("b.js", "content for b.js");
node.setSourceContent("c.js", "content for c.js");
let { code, map } = node.toStringWithSourceMap({
file: "abc.js"
});
code += "//@ sourceMappingURL=data:text/json;base64," + btoa(map.toString());
Components.utils.evalInSandbox(code, gDebuggee, "1.8",
"http://example.com/www/js/abc.js", 1);
}
function testContents(aSources, aCallback) {
if (aSources.length === 0) {
return aCallback();
}
let source = aSources[0];
let sourceClient = gThreadClient.source(aSources[0]);
sourceClient.source((aResponse) => {
do_check_true(!aResponse.error,
"Should not get an error loading the source from sourcesContent");
let expectedContent = "content for " + source.url;
do_check_eq(aResponse.source, expectedContent,
"Should have the expected source content");
testContents(aSources.slice(1), aCallback);
});
}

View File

@ -87,6 +87,7 @@ reason = bug 820380
[test_sourcemaps-05.js]
skip-if = toolkit == "gonk"
reason = bug 820380
[test_sourcemaps-06.js]
[test_objectgrips-01.js]
[test_objectgrips-02.js]
[test_objectgrips-03.js]