michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const { utils: Cu } = Components; michael@0: const { Services } = Cu.import("resource://gre/modules/Services.jsm", {}); michael@0: const LoaderModule = Cu.import("resource://gre/modules/commonjs/toolkit/loader.js", {}).Loader; michael@0: const { console } = Cu.import("resource://gre/modules/devtools/Console.jsm", {}); michael@0: let { michael@0: Loader, main, Module, Require, unload michael@0: } = LoaderModule; michael@0: michael@0: let CURRENT_DIR = gTestPath.replace(/\/[^\/]*\.js$/,'/'); michael@0: let loaders = []; michael@0: michael@0: // All tests are asynchronous. michael@0: waitForExplicitFinish(); michael@0: michael@0: let gEnableLogging = Services.prefs.getBoolPref("devtools.debugger.log"); michael@0: Services.prefs.setBoolPref("devtools.debugger.log", true); michael@0: michael@0: registerCleanupFunction(() => { michael@0: info("finish() was called, cleaning up..."); michael@0: loaders.forEach(unload); michael@0: Services.prefs.setBoolPref("devtools.debugger.log", gEnableLogging); michael@0: }); michael@0: michael@0: function makePaths (root) { michael@0: return { michael@0: './': CURRENT_DIR, michael@0: '': 'resource://gre/modules/commonjs/' michael@0: }; michael@0: } michael@0: michael@0: function makeLoader (options) { michael@0: let { paths, globals } = options || {}; michael@0: michael@0: // We have to have `console` as a global, otherwise michael@0: // many SDK modules will fail michael@0: // bug 961252 michael@0: let globalDefaults = { michael@0: console: console michael@0: }; michael@0: michael@0: let loader = Loader({ michael@0: paths: paths || makePaths(), michael@0: globals: extend({}, globalDefaults, globals) || null, michael@0: modules: { michael@0: // Needed because sdk/ modules reference utilities in michael@0: // `toolkit/loader`, until Bug 961194 is completed michael@0: 'toolkit/loader': LoaderModule michael@0: }, michael@0: // We need rootURI due to `sdk/self` (or are using native loader) michael@0: // which overloads with pseudo modules michael@0: // bug 961245 michael@0: rootURI: CURRENT_DIR, michael@0: // We also need metadata dummy object michael@0: // bug 961245 michael@0: metadata: {} michael@0: }); michael@0: michael@0: loaders.push(loader); michael@0: return loader; michael@0: } michael@0: michael@0: function isUUID (string) { michael@0: return /^\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}$/.test(string); michael@0: } michael@0: michael@0: function extend (...objs) { michael@0: if (objs.length === 0 || objs.length === 1) michael@0: return objs[0] || {}; michael@0: michael@0: for (let i = objs.length; i > 1; i--) { michael@0: for (var prop in objs[i - 1]) michael@0: objs[0][prop] = objs[i - 1][prop]; michael@0: } michael@0: return objs[0]; michael@0: }