Bug 998291 - Allow for relatively-loaded modules to be loaded into a given scope. r=bsmedberg

This commit is contained in:
Jeremy Morton 2014-04-22 23:37:26 +01:00
parent 39b924ec2e
commit efed059bea
3 changed files with 10 additions and 3 deletions

View File

@ -279,13 +279,13 @@ this.XPCOMUtils = {
* Allows you to fake a relative import. Expects the global object from the
* module that's calling us, and the relative filename that we wish to import.
*/
importRelative: function XPCOMUtils__importRelative(that, path) {
importRelative: function XPCOMUtils__importRelative(that, path, scope) {
if (!("__URI__" in that))
throw Error("importRelative may only be used from a JSM, and its first argument "+
"must be that JSM's global object (hint: use this)");
let uri = that.__URI__;
let i = uri.lastIndexOf("/");
Components.utils.import(uri.substring(0, i+1) + path, that);
Components.utils.import(uri.substring(0, i+1) + path, scope || that);
},
/**

View File

@ -4,7 +4,7 @@
// Module used by test_import_module.js
const EXPORTED_SYMBOLS = [ "MODULE_IMPORTED", "MODULE_URI", "SUBMODULE_IMPORTED", "same_scope" ];
const EXPORTED_SYMBOLS = [ "MODULE_IMPORTED", "MODULE_URI", "SUBMODULE_IMPORTED", "same_scope", "SUBMODULE_IMPORTED_TO_SCOPE" ];
const MODULE_IMPORTED = true;
const MODULE_URI = __URI__;
@ -26,3 +26,9 @@ XPCOMUtils.importRelative(scope2, "duh/../import_sub_module.jsm");
// We'll leave it up to test_import_module.js to check that this variable is
// true.
var same_scope = (scope1.test_obj.i == scope2.test_obj.i);
// Check that importRelative can also import into a given scope
var testScope = {};
XPCOMUtils.importRelative(this, "import_sub_module.jsm", testScope);
var SUBMODULE_IMPORTED_TO_SCOPE = testScope.SUBMODULE_IMPORTED;

View File

@ -18,4 +18,5 @@ function run_test() {
do_check_true(MODULE_IMPORTED);
do_check_true(SUBMODULE_IMPORTED);
do_check_true(same_scope);
do_check_true(SUBMODULE_IMPORTED_TO_SCOPE);
}