worker.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. "no use strict";
  2. ;(function(window) {
  3. if (typeof window.window != "undefined" && window.document)
  4. return;
  5. if (window.require && window.define)
  6. return;
  7. window.console = function() {
  8. var msgs = Array.prototype.slice.call(arguments, 0);
  9. postMessage({type: "log", data: msgs});
  10. };
  11. window.console.error =
  12. window.console.warn =
  13. window.console.log =
  14. window.console.trace = window.console;
  15. window.window = window;
  16. window.ace = window;
  17. window.onerror = function(message, file, line, col, err) {
  18. postMessage({type: "error", data: {
  19. message: message,
  20. data: err.data,
  21. file: file,
  22. line: line,
  23. col: col,
  24. stack: err.stack
  25. }});
  26. };
  27. window.normalizeModule = function(parentId, moduleName) {
  28. // normalize plugin requires
  29. if (moduleName.indexOf("!") !== -1) {
  30. var chunks = moduleName.split("!");
  31. return window.normalizeModule(parentId, chunks[0]) + "!" + window.normalizeModule(parentId, chunks[1]);
  32. }
  33. // normalize relative requires
  34. if (moduleName.charAt(0) == ".") {
  35. var base = parentId.split("/").slice(0, -1).join("/");
  36. moduleName = (base ? base + "/" : "") + moduleName;
  37. while (moduleName.indexOf(".") !== -1 && previous != moduleName) {
  38. var previous = moduleName;
  39. moduleName = moduleName.replace(/^\.\//, "").replace(/\/\.\//, "/").replace(/[^\/]+\/\.\.\//, "");
  40. }
  41. }
  42. return moduleName;
  43. };
  44. window.require = function require(parentId, id) {
  45. if (!id) {
  46. id = parentId;
  47. parentId = null;
  48. }
  49. if (!id.charAt)
  50. throw new Error("worker.js require() accepts only (parentId, id) as arguments");
  51. id = window.normalizeModule(parentId, id);
  52. var module = window.require.modules[id];
  53. if (module) {
  54. if (!module.initialized) {
  55. module.initialized = true;
  56. module.exports = module.factory().exports;
  57. }
  58. return module.exports;
  59. }
  60. if (!window.require.tlns)
  61. return console.log("unable to load " + id);
  62. var path = resolveModuleId(id, window.require.tlns);
  63. if (path.slice(-3) != ".js") path += ".js";
  64. window.require.id = id;
  65. window.require.modules[id] = {}; // prevent infinite loop on broken modules
  66. importScripts(path);
  67. return window.require(parentId, id);
  68. };
  69. function resolveModuleId(id, paths) {
  70. var testPath = id, tail = "";
  71. while (testPath) {
  72. var alias = paths[testPath];
  73. if (typeof alias == "string") {
  74. return alias + tail;
  75. } else if (alias) {
  76. return alias.location.replace(/\/*$/, "/") + (tail || alias.main || alias.name);
  77. } else if (alias === false) {
  78. return "";
  79. }
  80. var i = testPath.lastIndexOf("/");
  81. if (i === -1) break;
  82. tail = testPath.substr(i) + tail;
  83. testPath = testPath.slice(0, i);
  84. }
  85. return id;
  86. }
  87. window.require.modules = {};
  88. window.require.tlns = {};
  89. window.define = function(id, deps, factory) {
  90. if (arguments.length == 2) {
  91. factory = deps;
  92. if (typeof id != "string") {
  93. deps = id;
  94. id = window.require.id;
  95. }
  96. } else if (arguments.length == 1) {
  97. factory = id;
  98. deps = [];
  99. id = window.require.id;
  100. }
  101. if (typeof factory != "function") {
  102. window.require.modules[id] = {
  103. exports: factory,
  104. initialized: true
  105. };
  106. return;
  107. }
  108. if (!deps.length)
  109. // If there is no dependencies, we inject "require", "exports" and
  110. // "module" as dependencies, to provide CommonJS compatibility.
  111. deps = ["require", "exports", "module"];
  112. var req = function(childId) {
  113. return window.require(id, childId);
  114. };
  115. window.require.modules[id] = {
  116. exports: {},
  117. factory: function() {
  118. var module = this;
  119. var returnExports = factory.apply(this, deps.map(function(dep) {
  120. switch (dep) {
  121. // Because "require", "exports" and "module" aren't actual
  122. // dependencies, we must handle them seperately.
  123. case "require": return req;
  124. case "exports": return module.exports;
  125. case "module": return module;
  126. // But for all other dependencies, we can just go ahead and
  127. // require them.
  128. default: return req(dep);
  129. }
  130. }));
  131. if (returnExports)
  132. module.exports = returnExports;
  133. return module;
  134. }
  135. };
  136. };
  137. window.define.amd = {};
  138. require.tlns = {};
  139. window.initBaseUrls = function initBaseUrls(topLevelNamespaces) {
  140. for (var i in topLevelNamespaces)
  141. require.tlns[i] = topLevelNamespaces[i];
  142. };
  143. window.initSender = function initSender() {
  144. var EventEmitter = window.require("ace/lib/event_emitter").EventEmitter;
  145. var oop = window.require("ace/lib/oop");
  146. var Sender = function() {};
  147. (function() {
  148. oop.implement(this, EventEmitter);
  149. this.callback = function(data, callbackId) {
  150. postMessage({
  151. type: "call",
  152. id: callbackId,
  153. data: data
  154. });
  155. };
  156. this.emit = function(name, data) {
  157. postMessage({
  158. type: "event",
  159. name: name,
  160. data: data
  161. });
  162. };
  163. }).call(Sender.prototype);
  164. return new Sender();
  165. };
  166. var main = window.main = null;
  167. var sender = window.sender = null;
  168. window.onmessage = function(e) {
  169. var msg = e.data;
  170. if (msg.event && sender) {
  171. sender._signal(msg.event, msg.data);
  172. }
  173. else if (msg.command) {
  174. if (main[msg.command])
  175. main[msg.command].apply(main, msg.args);
  176. else if (window[msg.command])
  177. window[msg.command].apply(window, msg.args);
  178. else
  179. throw new Error("Unknown command:" + msg.command);
  180. }
  181. else if (msg.init) {
  182. window.initBaseUrls(msg.tlns);
  183. require("ace/lib/es5-shim");
  184. sender = window.sender = window.initSender();
  185. var clazz = require(msg.module)[msg.classname];
  186. main = window.main = new clazz(sender);
  187. }
  188. };
  189. })(this);