Bug 788405 - variables created via importScripts should be globals. r=gavin

This commit is contained in:
Mark Hammond 2012-09-12 12:48:38 +10:00
parent cdd4ae9faf
commit 88360f0511
4 changed files with 19 additions and 2 deletions

View File

@ -130,6 +130,13 @@ FrameWorker.prototype = {
};
sandbox.navigator = navigator;
// Our importScripts function needs to 'eval' the script code from inside
// a function, but using eval() directly means functions in the script
// don't end up in the global scope.
sandbox._evalInSandbox = function(s) {
Cu.evalInSandbox(s, sandbox);
};
// and we delegate ononline and onoffline events to the worker.
// See http://www.whatwg.org/specs/web-apps/current-work/multipage/workers.html#workerglobalscope
this.frame.addEventListener('offline', function fw_onoffline(event) {

View File

@ -32,7 +32,7 @@ function importScripts() {
xhr.onreadystatechange = function(aEvt) {
if (xhr.readyState == 4) {
if (xhr.status == 200 || xhr.status == 0) {
eval(xhr.responseText);
_evalInSandbox(xhr.responseText);
}
else {
throw new Error("Unable to importScripts ["+scriptURL+"], status " + xhr.status)

View File

@ -1 +1,6 @@
dump("relative_import file\n");
testVar = "oh hai";
function testFunc() {
return "oh hai";
}

View File

@ -4,7 +4,12 @@ onconnect = function(e) {
let req;
try {
importScripts("relative_import.js");
port.postMessage({topic: "done", result: "ok"});
// the import should have exposed "testVar" and "testFunc" from the module.
if (testVar == "oh hai" && testFunc() == "oh hai") {
port.postMessage({topic: "done", result: "ok"});
} else {
port.postMessage({topic: "done", result: "import worked but global is not available"});
}
} catch(e) {
port.postMessage({topic: "done", result: "FAILED to importScripts, " + e.toString() });
return;