tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ModuleLoader.sys.mjs (1471B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 export function ModuleLoader(base, depth, proto) {
      7  const modules = {};
      8 
      9  const principal = Cc["@mozilla.org/systemprincipal;1"].createInstance(
     10    Ci.nsIPrincipal
     11  );
     12 
     13  const sharedGlobalsandbox = Cu.Sandbox(principal, {
     14    invisibleToDebugger: true,
     15    sandboxName: "FS Module Loader",
     16    sandboxPrototype: proto,
     17    wantComponents: false,
     18    wantGlobalProperties: [],
     19    wantXrays: false,
     20  });
     21 
     22  const require = async function (id) {
     23    if (modules[id]) {
     24      return modules[id].exported_symbols;
     25    }
     26 
     27    const url = new URL(depth + id, base);
     28 
     29    const module = Object.create(null, {
     30      exported_symbols: {
     31        configurable: false,
     32        enumerable: true,
     33        value: Object.create(null),
     34        writable: true,
     35      },
     36    });
     37 
     38    modules[id] = module;
     39 
     40    const properties = {
     41      require_module: require,
     42      exported_symbols: module.exported_symbols,
     43    };
     44 
     45    // Create a new object in this sandbox, that will be used as the scope
     46    // object for this particular module.
     47    const sandbox = sharedGlobalsandbox.Object();
     48    Object.assign(sandbox, properties);
     49 
     50    Services.scriptloader.loadSubScript(url.href, sandbox);
     51 
     52    return module.exported_symbols;
     53  };
     54 
     55  const returnObj = {
     56    require: {
     57      enumerable: true,
     58      value: require,
     59    },
     60  };
     61 
     62  return Object.create(null, returnObj);
     63 }