UNPKG

11.8 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2014, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
7 * additional grant of patent rights can be found in the PATENTS file in
8 * the same directory.
9 */
10
11(function(
12 // Reliable reference to the global object (i.e. window in browsers).
13 global,
14
15 // Dummy constructor that we use as the .constructor property for
16 // functions that return Generator objects.
17 GeneratorFunction,
18
19 // Undefined value, more compressible than void 0.
20 undefined
21) {
22 var hasOwn = Object.prototype.hasOwnProperty;
23
24 if (global.wrapGenerator) {
25 return;
26 }
27
28 function wrapGenerator(innerFn, self, tryList) {
29 return new Generator(innerFn, self || null, tryList || []);
30 }
31
32 global.wrapGenerator = wrapGenerator;
33 if (typeof exports !== "undefined") {
34 exports.wrapGenerator = wrapGenerator;
35 }
36
37 var GenStateSuspendedStart = "suspendedStart";
38 var GenStateSuspendedYield = "suspendedYield";
39 var GenStateExecuting = "executing";
40 var GenStateCompleted = "completed";
41
42 // Returning this object from the innerFn has the same effect as
43 // breaking out of the dispatch switch statement.
44 var ContinueSentinel = {};
45
46 wrapGenerator.mark = function(genFun) {
47 genFun.constructor = GeneratorFunction;
48 return genFun;
49 };
50
51 // Ensure isGeneratorFunction works when Function#name not supported.
52 if (GeneratorFunction.name !== "GeneratorFunction") {
53 GeneratorFunction.name = "GeneratorFunction";
54 }
55
56 wrapGenerator.isGeneratorFunction = function(genFun) {
57 var ctor = genFun && genFun.constructor;
58 return ctor ? GeneratorFunction.name === ctor.name : false;
59 };
60
61 function Generator(innerFn, self, tryList) {
62 var generator = this;
63 var context = new Context(tryList);
64 var state = GenStateSuspendedStart;
65
66 function invoke(method, arg) {
67 if (state === GenStateExecuting) {
68 throw new Error("Generator is already running");
69 }
70
71 if (state === GenStateCompleted) {
72 throw new Error("Generator has already finished");
73 }
74
75 while (true) {
76 var delegate = context.delegate;
77 if (delegate) {
78 try {
79 var info = delegate.generator[method](arg);
80
81 // Delegate generator ran and handled its own exceptions so
82 // regardless of what the method was, we continue as if it is
83 // "next" with an undefined arg.
84 method = "next";
85 arg = undefined;
86
87 } catch (uncaught) {
88 context.delegate = null;
89
90 // Like returning generator.throw(uncaught), but without the
91 // overhead of an extra function call.
92 method = "throw";
93 arg = uncaught;
94
95 continue;
96 }
97
98 if (info.done) {
99 context[delegate.resultName] = info.value;
100 context.next = delegate.nextLoc;
101 } else {
102 state = GenStateSuspendedYield;
103 return info;
104 }
105
106 context.delegate = null;
107 }
108
109 if (method === "next") {
110 if (state === GenStateSuspendedStart &&
111 typeof arg !== "undefined") {
112 // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
113 throw new TypeError(
114 "attempt to send " + JSON.stringify(arg) + " to newborn generator"
115 );
116 }
117
118 if (state === GenStateSuspendedYield) {
119 context.sent = arg;
120 } else {
121 delete context.sent;
122 }
123
124 } else if (method === "throw") {
125 if (state === GenStateSuspendedStart) {
126 state = GenStateCompleted;
127 throw arg;
128 }
129
130 if (context.dispatchException(arg)) {
131 // If the dispatched exception was caught by a catch block,
132 // then let that catch block handle the exception normally.
133 method = "next";
134 arg = undefined;
135 }
136 }
137
138 state = GenStateExecuting;
139
140 try {
141 var value = innerFn.call(self, context);
142
143 // If an exception is thrown from innerFn, we leave state ===
144 // GenStateExecuting and loop back for another invocation.
145 state = context.done
146 ? GenStateCompleted
147 : GenStateSuspendedYield;
148
149 var info = {
150 value: value,
151 done: context.done
152 };
153
154 if (value === ContinueSentinel) {
155 if (context.delegate && method === "next") {
156 // Deliberately forget the last sent value so that we don't
157 // accidentally pass it on to the delegate.
158 arg = undefined;
159 }
160 } else {
161 return info;
162 }
163
164 } catch (thrown) {
165 state = GenStateCompleted;
166
167 if (method === "next") {
168 context.dispatchException(thrown);
169 } else {
170 arg = thrown;
171 }
172 }
173 }
174 }
175
176 generator.next = invoke.bind(generator, "next");
177 generator.throw = invoke.bind(generator, "throw");
178 }
179
180 Generator.prototype.toString = function() {
181 return "[object Generator]";
182 };
183
184 function pushTryEntry(triple) {
185 var entry = { tryLoc: triple[0] };
186
187 if (1 in triple) {
188 entry.catchLoc = triple[1];
189 }
190
191 if (2 in triple) {
192 entry.finallyLoc = triple[2];
193 }
194
195 this.tryEntries.push(entry);
196 }
197
198 function resetTryEntry(entry, i) {
199 var record = entry.completion || {};
200 record.type = i === 0 ? "normal" : "return";
201 delete record.arg;
202 entry.completion = record;
203 }
204
205 function Context(tryList) {
206 // The root entry object (effectively a try statement without a catch
207 // or a finally block) gives us a place to store values thrown from
208 // locations where there is no enclosing try statement.
209 this.tryEntries = [{ tryLoc: "root" }];
210 tryList.forEach(pushTryEntry, this);
211 this.reset();
212 }
213
214 Context.prototype = {
215 constructor: Context,
216
217 reset: function() {
218 this.prev = 0;
219 this.next = 0;
220 this.sent = undefined;
221 this.done = false;
222 this.delegate = null;
223
224 this.tryEntries.forEach(resetTryEntry);
225
226 // Pre-initialize at least 20 temporary variables to enable hidden
227 // class optimizations for simple generators.
228 for (var tempIndex = 0, tempName;
229 hasOwn.call(this, tempName = "t" + tempIndex) || tempIndex < 20;
230 ++tempIndex) {
231 this[tempName] = null;
232 }
233 },
234
235 stop: function() {
236 this.done = true;
237
238 var rootEntry = this.tryEntries[0];
239 var rootRecord = rootEntry.completion;
240 if (rootRecord.type === "throw") {
241 throw rootRecord.arg;
242 }
243
244 return this.rval;
245 },
246
247 keys: function(object) {
248 var keys = [];
249 for (var key in object) {
250 keys.push(key);
251 }
252 keys.reverse();
253
254 // Rather than returning an object with a next method, we keep
255 // things simple and return the next function itself.
256 return function next() {
257 while (keys.length) {
258 var key = keys.pop();
259 if (key in object) {
260 next.value = key;
261 next.done = false;
262 return next;
263 }
264 }
265
266 // To avoid creating an additional object, we just hang the .value
267 // and .done properties off the next function object itself. This
268 // also ensures that the minifier will not anonymize the function.
269 next.done = true;
270 return next;
271 };
272 },
273
274 dispatchException: function(exception) {
275 if (this.done) {
276 throw exception;
277 }
278
279 var context = this;
280 function handle(loc, caught) {
281 record.type = "throw";
282 record.arg = exception;
283 context.next = loc;
284 return !!caught;
285 }
286
287 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
288 var entry = this.tryEntries[i];
289 var record = entry.completion;
290
291 if (entry.tryLoc === "root") {
292 // Exception thrown outside of any try block that could handle
293 // it, so set the completion value of the entire function to
294 // throw the exception.
295 return handle("end");
296 }
297
298 if (entry.tryLoc <= this.prev) {
299 var hasCatch = hasOwn.call(entry, "catchLoc");
300 var hasFinally = hasOwn.call(entry, "finallyLoc");
301
302 if (hasCatch && hasFinally) {
303 if (this.prev < entry.catchLoc) {
304 return handle(entry.catchLoc, true);
305 } else if (this.prev < entry.finallyLoc) {
306 return handle(entry.finallyLoc);
307 }
308
309 } else if (hasCatch) {
310 if (this.prev < entry.catchLoc) {
311 return handle(entry.catchLoc, true);
312 }
313
314 } else if (hasFinally) {
315 if (this.prev < entry.finallyLoc) {
316 return handle(entry.finallyLoc);
317 }
318
319 } else {
320 throw new Error("try statement without catch or finally");
321 }
322 }
323 }
324 },
325
326 _findFinallyEntry: function(finallyLoc) {
327 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
328 var entry = this.tryEntries[i];
329 if (entry.tryLoc <= this.prev &&
330 hasOwn.call(entry, "finallyLoc") && (
331 entry.finallyLoc === finallyLoc ||
332 this.prev < entry.finallyLoc)) {
333 return entry;
334 }
335 }
336 },
337
338 abrupt: function(type, arg) {
339 var entry = this._findFinallyEntry();
340 var record = entry ? entry.completion : {};
341
342 record.type = type;
343 record.arg = arg;
344
345 if (entry) {
346 this.next = entry.finallyLoc;
347 } else {
348 this.complete(record);
349 }
350
351 return ContinueSentinel;
352 },
353
354 complete: function(record) {
355 if (record.type === "throw") {
356 throw record.arg;
357 }
358
359 if (record.type === "break" ||
360 record.type === "continue") {
361 this.next = record.arg;
362 } else if (record.type === "return") {
363 this.rval = record.arg;
364 this.next = "end";
365 }
366
367 return ContinueSentinel;
368 },
369
370 finish: function(finallyLoc) {
371 var entry = this._findFinallyEntry(finallyLoc);
372 return this.complete(entry.completion);
373 },
374
375 "catch": function(tryLoc) {
376 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
377 var entry = this.tryEntries[i];
378 if (entry.tryLoc === tryLoc) {
379 var record = entry.completion;
380 if (record.type === "throw") {
381 var thrown = record.arg;
382 resetTryEntry(entry, i);
383 }
384 return thrown;
385 }
386 }
387
388 // The context.catch method must only be called with a location
389 // argument that corresponds to a known catch block.
390 throw new Error("illegal catch attempt");
391 },
392
393 delegateYield: function(generator, resultName, nextLoc) {
394 this.delegate = {
395 generator: generator,
396 resultName: resultName,
397 nextLoc: nextLoc
398 };
399
400 return ContinueSentinel;
401 }
402 };
403}).apply(this, Function("return [this, function GeneratorFunction(){}]")());
404
405
406exports.Builder = require('./builders/builder');
407
408exports.scripts = require('./builders/scripts');
409exports.styles = require('./builders/styles');
410exports.files = require('./builders/files');
411
412exports.plugin =
413exports.plugins = {
414 js: require('./plugins/js'),
415 css: require('./plugins/css'),
416 json: require('./plugins/json'),
417 string: require('./plugins/string'),
418 copy: require('./plugins/copy'),
419 symlink: require('./plugins/symlink'),
420 urlRewriter: require('./plugins/url-rewriter'),
421};
\No newline at end of file