Bug 788405 - variables created via importScripts should be globals. r=gavin
authorMark Hammond <mhammond@skippinet.com.au>
Wed, 12 Sep 2012 12:48:38 +1000
changeset 106867 d9e96444da92b8da4448fa2dcb548520a187300e
parent 106866 092a8add22fde3c7a5d06c63a8517a4a5fe3c7c0
child 106868 df2ddcab71437f77beffbd58d049286132a25219
push id14725
push usermhammond@skippinet.com.au
push dateWed, 12 Sep 2012 02:48:46 +0000
treeherdermozilla-inbound@df2ddcab7143 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin
bugs788405
milestone18.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 788405 - variables created via importScripts should be globals. r=gavin
toolkit/components/social/FrameWorker.jsm
toolkit/components/social/MessagePortWorker.js
toolkit/components/social/test/browser/relative_import.js
toolkit/components/social/test/browser/worker_relative.js
--- a/toolkit/components/social/FrameWorker.jsm
+++ b/toolkit/components/social/FrameWorker.jsm
@@ -125,16 +125,23 @@ FrameWorker.prototype = {
       appVersion: workerWindow.navigator.appVersion,
       platform: workerWindow.navigator.platform,
       userAgent: workerWindow.navigator.userAgent,
       // interface NavigatorOnLine
       get onLine() workerWindow.navigator.onLine
     };
     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) {
       Cu.evalInSandbox("onoffline();", sandbox);
     }, false);
     this.frame.addEventListener('online', function fw_ononline(event) {
       Cu.evalInSandbox("ononline();", sandbox);
     }, false);
--- a/toolkit/components/social/MessagePortWorker.js
+++ b/toolkit/components/social/MessagePortWorker.js
@@ -27,17 +27,17 @@ function importScripts() {
   for (var i=0; i < arguments.length; i++) {
     // load the url *synchronously*
     var scriptURL = arguments[i];
     var xhr = new XMLHttpRequest();
     xhr.open('GET', scriptURL, false);
     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)
         }
       }
     };
     xhr.send(null);
   }
--- a/toolkit/components/social/test/browser/relative_import.js
+++ b/toolkit/components/social/test/browser/relative_import.js
@@ -1,1 +1,6 @@
 dump("relative_import file\n");
+
+testVar = "oh hai";
+function testFunc() {
+  return "oh hai";
+}
--- a/toolkit/components/social/test/browser/worker_relative.js
+++ b/toolkit/components/social/test/browser/worker_relative.js
@@ -1,13 +1,18 @@
 // Used to test XHR in the worker.
 onconnect = function(e) {
   let port = e.ports[0];
   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;
   }
   port.postMessage({topic: "done", result: "FAILED to importScripts, no exception" });
 }