diff options
author | Jose Antonio Marquez | 2012-06-20 16:48:34 -0700 |
---|---|---|
committer | Jose Antonio Marquez | 2012-06-20 16:48:34 -0700 |
commit | 4eff115126ab946c5852d787fd596e07b156c9a5 (patch) | |
tree | f7d45b260f2fb5ba5dc4d1879076ca032d5ae2e6 /node_modules/montage/require/worker-script.js | |
parent | 7615da5c6ba54bd082eac4b8a6d9196084e5c590 (diff) | |
parent | 392a559e90357d48c910a07617261483b2b45476 (diff) | |
download | ninja-4eff115126ab946c5852d787fd596e07b156c9a5.tar.gz |
Merge branch 'refs/heads/Ninja-Internal' into Color
Diffstat (limited to 'node_modules/montage/require/worker-script.js')
-rw-r--r-- | node_modules/montage/require/worker-script.js | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/node_modules/montage/require/worker-script.js b/node_modules/montage/require/worker-script.js new file mode 100644 index 00000000..58a3da7f --- /dev/null +++ b/node_modules/montage/require/worker-script.js | |||
@@ -0,0 +1,233 @@ | |||
1 | (function (global) { | ||
2 | |||
3 | var postMessage = global.postMessage.bind(global); | ||
4 | var addEventListener = global.addEventListener.bind(global); | ||
5 | var removeEventListener = global.removeEventListener.bind(global); | ||
6 | |||
7 | console = { | ||
8 | log: function () { | ||
9 | postMessage({ | ||
10 | type: "console", | ||
11 | method: "log", | ||
12 | args: Array.prototype.map.call(arguments, function (value) { | ||
13 | if (typeof value === "string") { | ||
14 | return value; | ||
15 | } else { | ||
16 | return JSON.stringify(value); | ||
17 | } | ||
18 | }) | ||
19 | }); | ||
20 | }, | ||
21 | error: function () { | ||
22 | postMessage({ | ||
23 | type: "console", | ||
24 | method: "error", | ||
25 | args: Array.prototype.map.call(arguments, function (value) { | ||
26 | if (typeof value === "string") { | ||
27 | return value; | ||
28 | } else { | ||
29 | return JSON.stringify(value); | ||
30 | } | ||
31 | }) | ||
32 | }); | ||
33 | } | ||
34 | }; | ||
35 | |||
36 | var factories = {}; | ||
37 | bootstrap = function (id, factory) { | ||
38 | factories[id] = factory; | ||
39 | }; | ||
40 | |||
41 | importScripts( | ||
42 | "require.js", | ||
43 | "../core/promise.js", | ||
44 | "../core/next-tick.js", | ||
45 | "../core/url.js" | ||
46 | ); | ||
47 | |||
48 | delete bootstrap; | ||
49 | |||
50 | var modules = {}; | ||
51 | function bootRequire(id) { | ||
52 | if (!modules[id]) { | ||
53 | var exports = modules[id] = {}; | ||
54 | factories[id](bootRequire, exports); | ||
55 | } | ||
56 | return modules[id]; | ||
57 | } | ||
58 | |||
59 | var Require = bootRequire("require/require"); | ||
60 | var Promise = bootRequire("core/promise").Promise; | ||
61 | |||
62 | var packageDeferred = Promise.defer(); | ||
63 | var location; | ||
64 | var module; | ||
65 | |||
66 | Require.getLocation = function () { | ||
67 | return location; | ||
68 | }; | ||
69 | |||
70 | Require.overlays = ["worker", "browser", "montage"]; | ||
71 | |||
72 | var reads = {}; | ||
73 | Require.read = function (url) { | ||
74 | if (reads[url] === void 0) { | ||
75 | var deferred = Promise.defer(); | ||
76 | postMessage({ | ||
77 | type: "read", | ||
78 | url: url | ||
79 | }); | ||
80 | reads[url] = deferred; | ||
81 | } | ||
82 | return reads[url].promise; | ||
83 | }; | ||
84 | |||
85 | // By using a named "eval" most browsers will execute in the global scope. | ||
86 | // http://www.davidflanagan.com/2010/12/global-eval-in.html | ||
87 | // Unfortunately execScript doesn't always return the value of the evaluated expression (at least in Chrome) | ||
88 | var globalEval = /*this.execScript ||*/eval; | ||
89 | // For Firebug evaled code isn't debuggable otherwise | ||
90 | // http://code.google.com/p/fbug/issues/detail?id=2198 | ||
91 | if (global.navigator && global.navigator.userAgent.indexOf("Firefox") >= 0) { | ||
92 | globalEval = new Function("_", "return eval(_)"); | ||
93 | } | ||
94 | |||
95 | var __FILE__String = "__FILE__", | ||
96 | DoubleUnderscoreString = "__", | ||
97 | globalEvalConstantA = "(function ", | ||
98 | globalEvalConstantB = "(require, exports, module) {", | ||
99 | globalEvalConstantC = "//*/\n})\n//@ sourceURL="; | ||
100 | |||
101 | Require.Compiler = function (config) { | ||
102 | return function (module) { | ||
103 | if (module.factory || module.text === void 0) | ||
104 | return module; | ||
105 | // Here we use a couple tricks to make debugging better in various browsers: | ||
106 | // TODO: determine if these are all necessary / the best options | ||
107 | // 1. name the function with something inteligible since some debuggers display the first part of each eval (Firebug) | ||
108 | // 2. append the "//@ sourceURL=location" hack (Safari, Chrome, Firebug) | ||
109 | // * http://pmuellr.blogspot.com/2009/06/debugger-friendly.html | ||
110 | // * http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/ | ||
111 | // TODO: investigate why this isn't working in Firebug. | ||
112 | // 3. set displayName property on the factory function (Safari, Chrome) | ||
113 | |||
114 | var displayName = __FILE__String+module.location.replace(/\.\w+$|\W/g, DoubleUnderscoreString); | ||
115 | |||
116 | try { | ||
117 | module.factory = globalEval(globalEvalConstantA+displayName+globalEvalConstantB+module.text+globalEvalConstantC+module.location); | ||
118 | } catch (exception) { | ||
119 | throw new SyntaxError("in " + module.location + ": " + exception.message); | ||
120 | } | ||
121 | |||
122 | // This should work and would be simpler, but Firebug does not show scripts executed via "new Function()" constructor. | ||
123 | // TODO: sniff browser? | ||
124 | // module.factory = new Function("require", "exports", "module", module.text + "\n//*/"+sourceURLComment); | ||
125 | |||
126 | module.factory.displayName = displayName; | ||
127 | }; | ||
128 | }; | ||
129 | |||
130 | Require.ReadLoader = function (config) { | ||
131 | return function (url, module) { | ||
132 | return Require.read(url) | ||
133 | .then(function (text) { | ||
134 | module.type = "javascript"; | ||
135 | module.text = text; | ||
136 | module.location = url; | ||
137 | }); | ||
138 | }; | ||
139 | }; | ||
140 | |||
141 | Require.makeLoader = function (config) { | ||
142 | return Require.MappingsLoader( | ||
143 | config, | ||
144 | Require.ExtensionsLoader( | ||
145 | config, | ||
146 | Require.PathsLoader( | ||
147 | config, | ||
148 | Require.MemoizedLoader( | ||
149 | config, | ||
150 | Require.ReadLoader(config) | ||
151 | ) | ||
152 | ) | ||
153 | ) | ||
154 | ); | ||
155 | }; | ||
156 | |||
157 | |||
158 | global.postMessage = function (event) { | ||
159 | postMessage({ | ||
160 | type: "forward", | ||
161 | data: event.data | ||
162 | }) | ||
163 | }; | ||
164 | |||
165 | var workerDeferred = Promise.defer(); | ||
166 | |||
167 | var handlers = []; | ||
168 | function dispatch(event) { | ||
169 | handlers.forEach(function (handler) { | ||
170 | if (handler.handleEvent) { | ||
171 | handler.handleEvent(event); | ||
172 | } else { | ||
173 | handler(event); | ||
174 | } | ||
175 | }); | ||
176 | if (global.onmessage) { | ||
177 | global.onmessage(event); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | global.addEventListener = function (name, handler, capture, untrusted) { | ||
182 | if (name === "message") { | ||
183 | handlers.push(handler); | ||
184 | } else { | ||
185 | return addEventListener(name, handler, caputre, untrusted); | ||
186 | } | ||
187 | }; | ||
188 | |||
189 | global.removeEventListener = function (name, handler) { | ||
190 | if (name === "message") { | ||
191 | var pos = handlers.indexOf(handler); | ||
192 | if (pos !== -1) { | ||
193 | handlers.splice(pos, 1); | ||
194 | } | ||
195 | } else { | ||
196 | return removeEventListener(name, handler); | ||
197 | } | ||
198 | }; | ||
199 | |||
200 | addEventListener("message", function (event) { | ||
201 | event.stopPropagation(); | ||
202 | event.preventDefault(); | ||
203 | if (event.data.type === "init") { | ||
204 | location = event.data.package; | ||
205 | module = event.data.module; | ||
206 | var packagePromise = Require.loadPackage(location) | ||
207 | .then(function (package) { | ||
208 | return package.async(module) | ||
209 | .then(function () { | ||
210 | workerDeferred.resolve(global); | ||
211 | }) | ||
212 | }) | ||
213 | .end() | ||
214 | } else if (event.data.type === "read") { | ||
215 | if (event.data.content !== void 0) { | ||
216 | reads[event.data.url].resolve(event.data.content); | ||
217 | } else { | ||
218 | reads[event.data.url].reject(event.data.error); | ||
219 | } | ||
220 | delete reads[event.data.url]; | ||
221 | } else if (event.data.type === "forward") { | ||