summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/test/loader.js
blob: 33ba2ca5a029472fbf49c836e679f48ca74541d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const { resolveURI, Require,
        unload, override, descriptor } = require('../../toolkit/loader');
const { ensure } = require('../system/unload');
const addonWindow = require('../addon/window');
const { PlainTextConsole } = require('sdk/console/plain-text');

var defaultGlobals = override(require('../system/globals'), {
  console: console
});

function CustomLoader(module, globals, packaging, overrides={}) {
  let options = packaging || require("@loader/options");
  options = override(options, {
    id: overrides.id || options.id,
    globals: override(defaultGlobals, globals || {}),
    modules: override(override(options.modules || {}, overrides.modules || {}), {
      'sdk/addon/window': addonWindow
    })
  });

  let loaderModule = options.isNative ? '../../toolkit/loader' : '../loader/cuddlefish';
  let { Loader } = require(loaderModule);
  let loader = Loader(options);
  let wrapper = Object.create(loader, descriptor({
    require: Require(loader, module),
    sandbox: function(id) {
      let requirement = loader.resolve(id, module.id);
      if (!requirement)
        requirement = id;
      let uri = resolveURI(requirement, loader.mapping);
      return loader.sandboxes[uri];
    },
    unload: function(reason) {
      unload(loader, reason);
    }
  }));
  ensure(wrapper);
  return wrapper;
};
exports.Loader = CustomLoader;

function HookedPlainTextConsole(hook, print, innerID) {
  this.log = hook.bind(null, "log", innerID);
  this.info = hook.bind(null, "info", innerID);
  this.warn = hook.bind(null, "warn", innerID);
  this.error = hook.bind(null, "error", innerID);
  this.debug = hook.bind(null, "debug", innerID);
  this.exception = hook.bind(null, "exception", innerID);
  this.time = hook.bind(null, "time", innerID);
  this.timeEnd = hook.bind(null, "timeEnd", innerID);

  this.__exposedProps__ = {
    log: "rw", info: "rw", warn: "rw", error: "rw", debug: "rw",
    exception: "rw", time: "rw", timeEnd: "rw"
  };
}

// Creates a custom loader instance whose console module is hooked in order
// to avoid printing messages to the console, and instead, expose them in the
// returned `messages` array attribute
exports.LoaderWithHookedConsole = function (module, callback) {
  let messages = [];
  function hook(type, innerID, msg) {
    messages.push({ type: type, msg: msg, innerID: innerID });
    if (callback)
      callback(type, msg, innerID);
  }

  return {
    loader: CustomLoader(module, {
      console: new HookedPlainTextConsole(hook, null, null)
    }, null, {
      modules: {
        'sdk/console/plain-text': {
          PlainTextConsole: HookedPlainTextConsole.bind(null, hook)
        }
      }
    }),
    messages: messages
  };
}

// Same than LoaderWithHookedConsole with lower level, instead we get what is
// actually printed to the command line console
exports.LoaderWithHookedConsole2 = function (module, callback) {
  let messages = [];
  return {
    loader: CustomLoader(module, {
      console: new PlainTextConsole(function (msg) {
        messages.push(msg);
        if (callback)
          callback(msg);
      })
    }),
    messages: messages
  };
}

// Creates a custom loader with a filtered console. The callback is passed every
// console message type and message and if it returns false the message will
// not be logged normally
exports.LoaderWithFilteredConsole = function (module, callback) {
  function hook(type, innerID, msg) {
    if (callback && callback(type, msg, innerID) == false)
      return;
    console[type](msg);
  }

  return CustomLoader(module, {
    console: new HookedPlainTextConsole(hook, null, null)
  }, null, {
    modules: {
      'sdk/console/plain-text': {
        PlainTextConsole: HookedPlainTextConsole.bind(null, hook)
      }
    }
  });
}