loader.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { resolveURI, Require,
  6. unload, override, descriptor } = require('../../toolkit/loader');
  7. const { ensure } = require('../system/unload');
  8. const addonWindow = require('../addon/window');
  9. const { PlainTextConsole } = require("sdk/console/plain-text");
  10. let defaultGlobals = override(require('../system/globals'), {
  11. console: console
  12. });
  13. function CustomLoader(module, globals, packaging, overrides={}) {
  14. let options = packaging || require("@loader/options");
  15. options = override(options, {
  16. id: overrides.id || options.id,
  17. globals: override(defaultGlobals, globals || {}),
  18. modules: override(options.modules || {}, {
  19. 'sdk/addon/window': addonWindow
  20. })
  21. });
  22. let loaderModule = options.isNative ? '../../toolkit/loader' : '../loader/cuddlefish';
  23. let { Loader } = require(loaderModule);
  24. let loader = Loader(options);
  25. let wrapper = Object.create(loader, descriptor({
  26. require: Require(loader, module),
  27. sandbox: function(id) {
  28. let requirement = loader.resolve(id, module.id);
  29. let uri = resolveURI(requirement, loader.mapping);
  30. return loader.sandboxes[uri];
  31. },
  32. unload: function(reason) {
  33. unload(loader, reason);
  34. }
  35. }));
  36. ensure(wrapper);
  37. return wrapper;
  38. };
  39. exports.Loader = CustomLoader;
  40. // Creates a custom loader instance whose console module is hooked in order
  41. // to avoid printing messages to the console, and instead, expose them in the
  42. // returned `messages` array attribute
  43. exports.LoaderWithHookedConsole = function (module, callback) {
  44. let messages = [];
  45. function hook(msg) {
  46. messages.push({type: this, msg: msg});
  47. if (callback)
  48. callback(this, msg);
  49. }
  50. return {
  51. loader: CustomLoader(module, {
  52. console: {
  53. log: hook.bind("log"),
  54. info: hook.bind("info"),
  55. warn: hook.bind("warn"),
  56. error: hook.bind("error"),
  57. debug: hook.bind("debug"),
  58. exception: hook.bind("exception"),
  59. __exposedProps__: {
  60. log: "rw", info: "rw", warn: "rw", error: "rw", debug: "rw",
  61. exception: "rw"
  62. }
  63. }
  64. }),
  65. messages: messages
  66. };
  67. }
  68. // Same than LoaderWithHookedConsole with lower level, instead we get what is
  69. // actually printed to the command line console
  70. exports.LoaderWithHookedConsole2 = function (module, callback) {
  71. let messages = [];
  72. return {
  73. loader: CustomLoader(module, {
  74. console: new PlainTextConsole(function (msg) {
  75. messages.push(msg);
  76. if (callback)
  77. callback(msg);
  78. })
  79. }),
  80. messages: messages
  81. };
  82. }
  83. // Creates a custom loader with a filtered console. The callback is passed every
  84. // console message type and message and if it returns false the message will
  85. // not be logged normally
  86. exports.LoaderWithFilteredConsole = function (module, callback) {
  87. function hook(msg) {
  88. if (callback && callback(this, msg) == false)
  89. return;
  90. console[this](msg);
  91. }
  92. return CustomLoader(module, {
  93. console: {
  94. log: hook.bind("log"),
  95. info: hook.bind("info"),
  96. warn: hook.bind("warn"),
  97. error: hook.bind("error"),
  98. debug: hook.bind("debug"),
  99. exception: hook.bind("exception"),
  100. __exposedProps__: {
  101. log: "rw", info: "rw", warn: "rw", error: "rw", debug: "rw",
  102. exception: "rw"
  103. }
  104. }
  105. });
  106. }