diff --git a/README.md b/README.md index b84e5025..6a50cf50 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ Compiling will give you the fastest startup time because node will directly load You have three options to use streamline in the browser: * The first one is to compile the source with `_node --standalone -c`. The compiler generates vanilla Javascript code that you can load with ` - + diff --git a/examples/streamlineMe/yieldMe.html b/examples/streamlineMe/yieldMe.html index 53a90959..aaff065a 100644 --- a/examples/streamlineMe/yieldMe.html +++ b/examples/streamlineMe/yieldMe.html @@ -5,7 +5,7 @@ - + diff --git a/lib/callbacks/runtime-all.js b/lib/callbacks/runtime-all.js new file mode 100644 index 00000000..2145ff4b --- /dev/null +++ b/lib/callbacks/runtime-all.js @@ -0,0 +1,791 @@ +if (!Object.create || !Object.defineProperty || !Object.defineProperties) alert("Example will fail because your browser does not support ECMAScript 5. Try with another browser!"); +var __filename = "" + window.location; + +window.Streamline = { globals: {} }; + +function require(str) { + if (/^\.\.\//.test(str)) str = "streamline/lib" + str.substring(2); + if (str == "streamline/lib/util/flows") return Streamline.flows; + else if (str == "streamline/lib/globals") return Streamline.globals; + else if (str == "streamline/lib/version") return Streamline.version; + else if (str == "streamline/lib/callbacks/runtime") return Streamline.runtime; + else if (str == "streamline/lib/callbacks/transform") return Streamline; + else if (str == "streamline/lib/callbacks/builtins") return Streamline.builtins; + else if (str == "streamline/lib/util/future") return Streamline.future; + else if (str == "streamline/lib/util/source-map") return Streamline.sourceMap.exports; + else alert("cannot require " + str) +} + +/// +/// # Container for global context +/// +/// The `globals` module is a container for the global `context` object which is maintained across +/// asynchronous calls. +/// +/// This context is very handy to store information that all calls should be able to access +/// but that you don't want to pass explicitly via function parameters. The most obvious example is +/// the `locale` that each request may set differently and that your low level libraries should +/// be able to retrieve to format messages. +/// +/// `var globals = require('streamline/lib/globals')` +/// +/// * `globals.context = ctx` +/// * `ctx = globals.context` +/// sets and gets the context +/// +/// Note: an empty context (`{}`) is automatically set by the server wrappers of the `streams` module, +/// before they dispatch a request. So, with these wrappers, each request starts with a fresh empty context. +// This module may be loaded several times so we need a true global (with a secret name!). +// This implementation also allows us to share the context between modules compiled in callback and fibers mode. +(function() { + var glob = typeof global === "object" ? global : window; + var secret = "_20c7abceb95c4eb88b7ca1895b1170d1"; + var g = glob[secret] || (glob[secret] = { context: {} }); + if (typeof exports !== 'undefined') { + module.exports = g; + } else { + Streamline.globals = g; + } + // Internal call to manage runtimes + g.runtime || Object.defineProperty(g, 'runtime', { + get: function() { return g.__runtime__; }, + set: function(value) { + if (g.__runtime__ !== value) { + if (g.__runtime__) { + if (/-fast$/.test(g.__runtime__) || + /-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value); + console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value); + } + g.__runtime__ = value; + } + } + }); + + /// + /// * `fn = globals.withContext(fn, cx)` + /// wraps a function so that it executes with context `cx` (or with a new empty context if `cx` is falsy). + /// The previous context will be restored when the function returns (or throws). + /// returns the wrapped function. + g.withContext = function(fn, cx) { + return function() { + var oldContext = g.context; + g.context = cx || {}; + try { + return fn.apply(this, arguments) + } finally { + g.context = oldContext; + } + }; + }; + + g.setPromise = function(name) { + if (g.Promise) return; // first caller wins + var req = require; // defeat streamline-require dependencies + if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise'); + else g.Promise = require(name); + }; +})(); + +/** + * Copyright (c) 2011 Bruno Jouhier + * MIT License + */ +(function(exports) { + var globals = require("../globals"); + + exports.future = function(fn, args, i) { + var err, result, done, q = [], self = this; + args = Array.prototype.slice.call(args); + args[i] = function(e, r) { + err = e, result = r, done = true; + q && q.forEach(function(f) { + f.call(self, e, r); + }); + q = null; + }; + args[i].__futurecb = true; + fn.apply(this, args); + var ret = function F(cb) { + if (typeof cb !== 'function') { + var globals = require('../globals'); + if (cb == null && globals.Promise) return exports.promise.call(this, F, [], 0); + if (cb !== false && !globals.oldStyleFutures) throw new Error("callback missing (argument #0). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error"); + return F; + } + if (done) cb.call(self, err, result); + else q.push(cb); + }; + ret.__future = true; + return ret; + }; + + exports.streamlinify = function(fn, idx) { + return function() { + if (!arguments[idx]) return exports.future.call(this, fn, arguments, idx); + else return fn.apply(this, arguments); + }; + }; + + exports.promise = function(fn, args, i) { + if (args[i] === false) return exports.future.call(this, fn, args, i); + if (args[i] != null) throw new Error("invalid callback: " + typeof(args[i])); + if (globals.oldStyleFutures) return exports.future.call(this, fn, args, i); + if (!globals.Promise) throw new Error("callback missing (argument #" + i + "). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error"); + + var self = this; + args = Array.prototype.slice.call(args); + return new globals.Promise(function(resolve, reject) { + args[i] = function(e, r) { + if (e) reject(e); + else resolve(r); + }; + fn.apply(self, args); + }); + }; + + exports.then = function(promise, method, cb) { + promise[method](function(r) { + cb && cb(null, r); + cb = null; + }, function(e) { + cb && cb(e); + cb = null; + }); + }; +})(typeof exports !== 'undefined' ? exports : (Streamline.future = Streamline.future || {})); + +/** + * Copyright (c) 2011 Bruno Jouhier + * MIT License + */ +(function(exports) { + var __g = require("../globals"); + __g.runtime = 'callbacks'; + var __fut = require("../util/future"); + __g.context = __g.context || {}; + __g.depth = __g.depth || 0; + __g.async = __g.async || false; + + __g.trampoline = (function() { + var q = []; + return { + queue: function(fn) { + q.push(fn); + }, + flush: function() { + var oldContext = __g.context; + __g.depth++; + try { + var fn; + while (fn = q.shift()) fn(); + } finally { + __g.context = oldContext; + __g.depth--; + } + } + } + })(); + + exports.runtime = function(filename, oldStyleFutures) { + __g.oldStyleFutures = oldStyleFutures; + function __func(_, __this, __arguments, fn, index, frame, body) { + if (typeof _ !== 'function') return __fut.promise.call(__this, fn, __arguments, index); + frame.file = filename; + frame.prev = __g.frame; + frame.calls = 0; + if (frame.prev) frame.prev.calls++; + var emitter = __g.emitter; + __g.frame = frame; + __g.depth++; + if (emitter) emitter.emit("enter", frame, _); // <- This allows the event handler to detect if the callback starts a new asynchronous path. + try { + frame.active = true; + body(); + } catch (e) { + __setEF(e, frame.prev); + __propagate(_, e); + } finally { + frame.active = false; + // We emit this before resetting the frame so that the 'exit' handler has access to the current frame. + if (emitter) { + emitter.emit("exit", frame); + } + __g.frame = frame.prev; + if (--__g.depth === 0 && __g.trampoline) __g.trampoline.flush(); + } + } + + return { + __g: __g, + __func: __func, + __cb: __cb, + __future: __fut.future, + __propagate: __propagate, + __trap: __trap, + __tryCatch: __tryCatch, + __catch: __catch, + __forIn: __forIn, + __apply: __apply, + __construct: __construct, + __setEF: __setEF, + streamlinify: __fut.streamlinify, + __pthen: __fut.then, + }; + }; + + function __cb(_, frame, offset, col, fn, trampo, returnArray) { + frame.offset = offset; + frame.col = col; + var ctx = __g.context; + var calls = frame.calls; + var emitter = __g.emitter; + var ret = function ___(err, result) { + if (returnArray) result = Array.prototype.slice.call(arguments, 1); + returnArray = false; // so that we don't do it twice if we trampoline + var oldFrame = __g.frame; + __g.frame = frame; + var oldContext = __g.context; + __g.context = ctx; + if (emitter && __g.depth === 0) emitter.emit('resume', frame); + if (emitter) emitter.emit('enter', frame); + __g.depth++; + try { + if (trampo && frame.active && __g.trampoline) { + __g.trampoline.queue(function() { + return ___(err, result); + }); + } else { + // detect extra callback. + // The offset/col test is necessary because __cb is also used by loops and called multiple times then. + /*if (___.dispatched && (offset || col)) throw new Error("callback called twice");*/ + ___.dispatched = true; + if (err) { + __setEF(err, frame); + return _(err); + } + frame.active = true; + return fn(null, result); + } + } catch (ex) { + if (___.dispatched && _.name !== '___' && _.name !== '__trap' && calls !== frame.calls) throw ex; + __setEF(ex, frame); + return __propagate(_, ex); + } finally { + frame.active = false; + // We emit this before resetting the frame so that the 'exit' handler has access to the current frame. + if (emitter) emitter.emit("exit", frame); + __g.frame = oldFrame; + __g.context = oldContext; + if (--__g.depth === 0 && __g.trampoline) __g.trampoline.flush(); + } + }; + if (emitter && !ret.dispatched) emitter.emit('yield', frame); + ret.__streamlined = true; + return ret; + } + + function __propagate(_, err) { + try { + _(err); + } catch (ex) { + __trap(ex); + } + } + + function __trap(err) { + if (err) { + if (__g.context && __g.context.errorHandler) __g.context.errorHandler(err); + else __g.trampoline.queue(function() { + throw err; + }); + } + } + + function __tryCatch(_, fn) { + try { + fn(); + } catch (e) { + try { + _(e); + } catch (ex) { + __trap(ex); + } + } + } + + function __catch(fn, _) { + var frame = __g.frame, + context = __g.context; + __g.trampoline.queue(function() { + var oldFrame = __g.frame, + oldContext = __g.context; + __g.frame = frame; + __g.context = context; + try { + fn(); + } catch (ex) { + _(ex); + } finally { + __g.frame = oldFrame; + __g.context = oldContext; + } + }); + } + + function __forIn(object) { + var array = []; + for (var obj in object) { + array.push(obj); + } + return array; + } + + function __apply(cb, fn, thisObj, args, index) { + if (cb == null) return __fut.future(__apply, arguments, 0); + args = Array.prototype.slice.call(args, 0); + args[index != null ? index : args.length] = cb; + return fn.apply(thisObj, args); + } + + function __construct(constructor, i) { + var key = '__async' + i, + f; + return constructor[key] || (constructor[key] = function() { + var args = arguments; + + function F() { + var self = this; + var cb = args[i]; + args[i] = function(e, r) { + cb(e, self); + }; + args[i].__streamlined = cb.__streamlined; + args[i].__futurecb = cb.__futurecb; + return constructor.apply(self, args); + } + F.prototype = constructor.prototype; + return new F(); + }); + } + + function __setEF(e, f) { + function formatStack(e, raw) { + var ff = typeof navigator === 'object' && navigator.userAgent.toLowerCase().indexOf('firefox') > -1; + // firefox does not include message + if (ff) raw = "Error: " + e.message + '\n' + raw; + var s = raw, + f, skip; + var cut = (e.message || '').split('\n').length; + var lines = s.split('\n'); + s = lines.slice(cut).map(function(l) { + // try to map firefox format to V8 format + var m = /([^@]*)\@(.*?)\:(\d+)(?:\:(\d+))?$/.exec(l); + l = m ? " at " + m[1] + " (" + m[2] + ":" + parseInt(m[3]) + ":" + (m[4] || "0") + ")" : l; + var i = l.indexOf('__$'); + if (i >= 0 && !skip) { + skip = true; + return l.substring(0, i) + l.substring(i + 3); + } + return skip ? '' : l; + }).filter(function(l) { + return l; + }).join('\n'); + // if __$ marker is missing (if skip is false) the stack is 100% raw so we don't include its frames in the async stack + s = lines.slice(0, cut).join('\n') + '\n <<< async stack >>>' + (skip ? '\n' + s : ''); + for (var f = e.__frame; f; f = f.prev) { + if (f.offset >= 0) s += "\n at " + f.name + " (" + f.file + ":" + (f.line + f.offset) + ":" + (f.col+1) + ")" + } + s += '\n <<< raw stack >>>' + '\n' + lines.slice(cut).join('\n'); + return s; + }; + e.__frame = e.__frame || f; + if (exports.stackTraceEnabled && e.__lookupGetter__ && e.__lookupGetter__("rawStack") == null) { + var getter = e.__lookupGetter__("stack"); + if (!getter) { // FF or Safari case + var raw = e.stack || "raw stack unavailable"; + getter = function() { + return raw; + } + } + e.__defineGetter__("rawStack", getter); + e.__defineGetter__("stack", function() { + return formatStack(e, getter.call(this)); + }); + } + } + + /// * `runtime.stackTraceEnabled = true/false;` + /// If true, `err.stack` returns the reconstructed _sync_ stack trace. + /// Otherwise, it returns the _raw_ stack trace. + /// The default is true, but you must require the flows module + /// at least once to enable sync stack traces. + exports.stackTraceEnabled = true; +})(typeof exports !== 'undefined' ? exports : (Streamline.runtime = Streamline.runtime || {})); +require && require("../callbacks/builtins"); +/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('../callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb; + + + + + + + + (function (exports) { + 'use strict'; + var VERSION = 3; + + + + var future = function (fn, args, i) { + var err, result, done, q = [], self = this; + + args = Array.prototype.slice.call(args); + args[i] = function (e, r) { + err = e, result = r, done = true; + q && q.forEach(function (f) { + f.call(self, e, r); }); + + q = null; }; + + fn.apply(this, args); + return function F(cb) { + if (!cb) return F; + if (done) cb.call(self, err, result); else q.push(cb); }; }; + + + + + + exports.funnel = function (max) { + max = max == null ? -1 : max; + if (max === 0) max = funnel.defaultSize; + if (typeof max !== 'number') throw new Error('bad max number: ' + max); + var queue = [], active = 0, closed = false; + + + + var funCb = function (callback, fn) { + if (callback == null) return future(funCb, arguments, 0); + + if (max < 0 || max == Infinity) return fn(callback); + + queue.push({ fn: fn, cb: callback }); + + + + + function _doOne() { + var current = queue.splice(0, 1)[0]; + if (!current.cb) return current.fn(); + active++; + current.fn(function (err, result) { + active--; + if (!closed) { + current.cb(err, result); + while (active < max && queue.length > 0) _doOne(); } }); } + + + + + while (active < max && queue.length > 0) _doOne(); }; + + var fun = __rt.streamlinify(funCb, 0); + + fun.close = function () { + queue = [], closed = true; }; + + return fun; }; + + var funnel = exports.funnel; + funnel.defaultSize = 4; + + function _parallel(options) { + if (typeof options === 'number') return options; + if (typeof options.parallel === 'number') return options.parallel; + return options.parallel ? -1 : 1; } + + + if (Array.prototype.forEach_ && Array.prototype.forEach_.version_ >= VERSION) return; + + + try { + Object.defineProperty({}, 'x', {}); } catch (e) { + + return; } + + + var has = Object.prototype.hasOwnProperty; + + + + + + + + + + + + + + + + + + + + + + + + + + delete Array.prototype.forEach_; + Object.defineProperty(Array.prototype, 'forEach_', { configurable: true, writable: true, enumerable: false, value: function value__1(_, options, fn, thisObj) { + + + + var par, len, i, __this = this; var __frame = { name: 'value__1', line: 124 }; return __func(_, this, arguments, value__1, 0, __frame, function __$value__1() { + if (typeof options === 'function') { thisObj = fn, fn = options, options = 1; } par = _parallel(options); + + thisObj = thisObj !== undefined ? thisObj : __this; len = __this.length; + + return function __$value__1(__then) { if (par === 1 || len <= 1) { i = 0; + var __2 = false; return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$value__1() { __more = false; if (__2) { i++; } else { __2 = true; } var __1 = i < len; if (__1) { + return function __$value__1(__then) { if (has.call(__this, i)) { return fn.call(thisObj, __cb(_, __frame, 7, 0, __then, true, false), __this[i], i); } else { __then(); } }(function __$value__1() { while (__more) { __loop(); } __more = true; }); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(__then); } else { + + + return __this.map_(__cb(_, __frame, 10, 0, __then, true, false), par, fn, thisObj); } }(function __$value__1() { + + return _(null, __this); }); }); } }); + + + Array.prototype.forEach_.version_ = VERSION; + + + delete Array.prototype.map_; + Object.defineProperty(Array.prototype, 'map_', { configurable: true, writable: true, enumerable: false, value: function value__2(_, options, fn, thisObj) { + + + + var par, len, result, i, fun, __this = this; var __frame = { name: 'value__2', line: 147 }; return __func(_, this, arguments, value__2, 0, __frame, function __$value__2() { + if (typeof options === 'function') { thisObj = fn, fn = options, options = 1; } par = _parallel(options); + + thisObj = thisObj !== undefined ? thisObj : __this; len = __this.length; + + + return function __$value__2(__then) { if (par === 1 || len <= 1) { + result = new Array(len); i = 0; + var __4 = false; return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$value__2() { __more = false; if (__4) { i++; } else { __4 = true; } var __3 = i < len; if (__3) { + return function __$value__2(__then) { if (has.call(__this, i)) { return fn.call(thisObj, __cb(_, __frame, 9, 0, function ___(__0, __1) { result[i] = __1; __then(); }, true, false), __this[i], i); } else { __then(); } }(function __$value__2() { while (__more) { __loop(); } __more = true; }); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(__then); } else { + + fun = funnel(par); + + result = __this.map(function (elt, i) { + return fun(false, function __1(_) { var __frame = { name: '__1', line: 161 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { + return fn.call(thisObj, __cb(_, __frame, 1, 0, _, true, false), elt, i); }); }); }); i = 0; + + + var __7 = false; return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$value__2() { __more = false; if (__7) { i++; } else { __7 = true; } var __6 = i < len; if (__6) { + return function __$value__2(__then) { if (has.call(__this, i)) { return result[i](__cb(_, __frame, 19, 0, function ___(__0, __2) { result[i] = __2; __then(); }, true, false)); } else { __then(); } }(function __$value__2() { while (__more) { __loop(); } __more = true; }); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(__then); } }(function __$value__2() { + + + return _(null, result); }); }); } }); + + + + + delete Array.prototype.filter_; + Object.defineProperty(Array.prototype, 'filter_', { configurable: true, writable: true, enumerable: false, value: function value__3(_, options, fn, thisObj) { + + + + var par, result, len, i, elt, __this = this; var __frame = { name: 'value__3', line: 179 }; return __func(_, this, arguments, value__3, 0, __frame, function __$value__3() { + if (typeof options === 'function') { thisObj = fn, fn = options, options = 1; } par = _parallel(options); + + thisObj = thisObj !== undefined ? thisObj : __this; result = []; + len = __this.length; + + return function __$value__3(__then) { if (par === 1 || len <= 1) { i = 0; + var __5 = false; return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$value__3() { __more = false; if (__5) { i++; } else { __5 = true; } var __4 = i < len; if (__4) { + return function __$value__3(__then) { if (has.call(__this, i)) { elt = __this[i]; + + return fn.call(thisObj, __cb(_, __frame, 10, 0, function ___(__0, __3) { var __2 = __3; return function __$value__3(__then) { if (__2) { result.push(elt); __then(); } else { __then(); } }(__then); }, true, false), elt); } else { __then(); } }(function __$value__3() { while (__more) { __loop(); } __more = true; }); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(__then); } else { + + + + return __this.map_(__cb(_, __frame, 14, 0, __then, true, false), par, function __1(_, elt) { var __frame = { name: '__1', line: 193 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { + return fn.call(thisObj, __cb(_, __frame, 1, 0, function ___(__0, __2) { var __1 = __2; return function __$__1(__then) { if (__1) { result.push(elt); __then(); } else { __then(); } }(_); }, true, false), elt); }); }, thisObj); } }(function __$value__3() { + + + return _(null, result); }); }); } }); + + + + + delete Array.prototype.every_; + Object.defineProperty(Array.prototype, 'every_', { configurable: true, writable: true, enumerable: false, value: function value__4(_, options, fn, thisObj) { + + + + var par, len, i, fun, futures, __this = this; var __frame = { name: 'value__4', line: 207 }; return __func(_, this, arguments, value__4, 0, __frame, function __$value__4() { + if (typeof options === 'function') { thisObj = fn, fn = options, options = 1; } par = _parallel(options); + + thisObj = thisObj !== undefined ? thisObj : __this; len = __this.length; + + return function __$value__4(__then) { if (par === 1 || len <= 1) { i = 0; + var __8 = false; return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$value__4() { __more = false; if (__8) { i++; } else { __8 = true; } var __7 = i < len; if (__7) { + + return function __$value__4(_) { var __1 = has.call(__this, i); if (!__1) { return _(null, __1); } return fn.call(thisObj, __cb(_, __frame, 8, 0, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true, false), __this[i]); }(__cb(_, __frame, 0, 0, function ___(__0, __4) { var __3 = __4; return function __$value__4(__then) { if (__3) { return _(null, false); } else { __then(); } }(function __$value__4() { while (__more) { __loop(); } __more = true; }); }, true, false)); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(__then); } else { + + fun = funnel(par); + futures = __this.map(function (elt) { + + return fun(false, function __1(_) { var __frame = { name: '__1', line: 220 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { + return fn.call(thisObj, __cb(_, __frame, 1, 0, _, true, false), elt); }); }); }); i = 0; + + + var __11 = false; return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$value__4() { __more = false; if (__11) { i++; } else { __11 = true; } var __10 = i < len; if (__10) { + return function __$value__4(_) { var __2 = has.call(__this, i); if (!__2) { return _(null, __2); } return futures[i](__cb(_, __frame, 18, 0, function ___(__0, __4) { var __3 = !__4; return _(null, __3); }, true, false)); }(__cb(_, __frame, 0, 0, function ___(__0, __6) { var __5 = __6; return function __$value__4(__then) { if (__5) { + fun.close(); + return _(null, false); } else { __then(); } }(function __$value__4() { while (__more) { __loop(); } __more = true; }); }, true, false)); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(__then); } }(function __$value__4() { + + + + return _(null, true); }); }); } }); + + + + + delete Array.prototype.some_; + Object.defineProperty(Array.prototype, 'some_', { configurable: true, writable: true, enumerable: false, value: function value__5(_, options, fn, thisObj) { + + + + var par, len, i, fun, futures, __this = this; var __frame = { name: 'value__5', line: 241 }; return __func(_, this, arguments, value__5, 0, __frame, function __$value__5() { + if (typeof options === 'function') { thisObj = fn, fn = options, options = 1; } par = _parallel(options); + + thisObj = thisObj !== undefined ? thisObj : __this; len = __this.length; + + return function __$value__5(__then) { if (par === 1 || len <= 1) { i = 0; + var __8 = false; return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$value__5() { __more = false; if (__8) { i++; } else { __8 = true; } var __7 = i < len; if (__7) { + return function __$value__5(_) { var __1 = has.call(__this, i); if (!__1) { return _(null, __1); } return fn.call(thisObj, __cb(_, __frame, 7, 0, _, true, false), __this[i]); }(__cb(_, __frame, 0, 0, function ___(__0, __4) { var __3 = __4; return function __$value__5(__then) { if (__3) { return _(null, true); } else { __then(); } }(function __$value__5() { while (__more) { __loop(); } __more = true; }); }, true, false)); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(__then); } else { + + fun = funnel(par); + futures = __this.map(function (elt) { + + return fun(false, function __1(_) { var __frame = { name: '__1', line: 253 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { + return fn.call(thisObj, __cb(_, __frame, 1, 0, _, true, false), elt); }); }); }); i = 0; + + + var __11 = false; return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$value__5() { __more = false; if (__11) { i++; } else { __11 = true; } var __10 = i < len; if (__10) { + return function __$value__5(_) { var __2 = has.call(__this, i); if (!__2) { return _(null, __2); } return futures[i](__cb(_, __frame, 17, 0, _, true, false)); }(__cb(_, __frame, 0, 0, function ___(__0, __6) { var __5 = __6; return function __$value__5(__then) { if (__5) { + fun.close(); + return _(null, true); } else { __then(); } }(function __$value__5() { while (__more) { __loop(); } __more = true; }); }, true, false)); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(__then); } }(function __$value__5() { + + + + return _(null, false); }); }); } }); + + + + + delete Array.prototype.reduce_; + Object.defineProperty(Array.prototype, 'reduce_', { configurable: true, writable: true, enumerable: false, value: function value__6(_, fn, v, thisObj) { + + + + var len, i, __this = this; var __frame = { name: 'value__6', line: 274 }; return __func(_, this, arguments, value__6, 0, __frame, function __$value__6() { + thisObj = thisObj !== undefined ? thisObj : __this; len = __this.length; + i = 0; + var __3 = false; return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$value__6() { __more = false; if (__3) { i++; } else { __3 = true; } var __2 = i < len; if (__2) { + return function __$value__6(__then) { if (has.call(__this, i)) { return fn.call(thisObj, __cb(_, __frame, 4, 0, function ___(__0, __1) { v = __1; __then(); }, true, false), v, __this[i], i, __this); } else { __then(); } }(function __$value__6() { while (__more) { __loop(); } __more = true; }); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(function __$value__6() { + + return _(null, v); }); }); } }); + + + + + delete Array.prototype.reduceRight_; + Object.defineProperty(Array.prototype, 'reduceRight_', { configurable: true, writable: true, enumerable: false, value: function value__7(_, fn, v, thisObj) { + + + + var len, i, __this = this; var __frame = { name: 'value__7', line: 290 }; return __func(_, this, arguments, value__7, 0, __frame, function __$value__7() { + thisObj = thisObj !== undefined ? thisObj : __this; len = __this.length; + i = len - 1; + var __3 = false; return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$value__7() { __more = false; if (__3) { i--; } else { __3 = true; } var __2 = i >= 0; if (__2) { + return function __$value__7(__then) { if (has.call(__this, i)) { return fn.call(thisObj, __cb(_, __frame, 4, 0, function ___(__0, __1) { v = __1; __then(); }, true, false), v, __this[i], i, __this); } else { __then(); } }(function __$value__7() { while (__more) { __loop(); } __more = true; }); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(function __$value__7() { + + return _(null, v); }); }); } }); + + + + + + + delete Array.prototype.sort_; + Object.defineProperty(Array.prototype, 'sort_', { configurable: true, writable: true, enumerable: false, value: function value__8(_, compare, beg, end) { + + + + var array, __this = this; + + + + + function _qsort(_, beg, end) { var tmp, mid, o, nbeg, nend; var __frame = { name: '_qsort', line: 313 }; return __func(_, this, arguments, _qsort, 0, __frame, function __$_qsort() { + if (beg >= end) { return _(null); } + + return function __$_qsort(__then) { if (end == beg + 1) { + return compare(__cb(_, __frame, 4, 0, function ___(__0, __4) { var __3 = __4 > 0; return function __$_qsort(__then) { if (__3) { tmp = array[beg]; + + array[beg] = array[end]; + array[end] = tmp; __then(); } else { __then(); } }(function __$_qsort() { + + return _(null); }); }, true, false), array[beg], array[end]); } else { __then(); } }(function __$_qsort() { mid = Math.floor((beg + end) / 2); + + + o = array[mid]; + nbeg = beg; + nend = end; + return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$_qsort() { __more = false; var __6 = nbeg <= nend; + + if (__6) { return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$_qsort() { __more = false; return function __$_qsort(_) { + return function __$_qsort(_) { var __1 = nbeg < end; if (!__1) { return _(null, __1); } return compare(__cb(_, __frame, 18, 0, function ___(__0, __3) { var __2 = __3 < 0; return _(null, __2); }, true, false), array[nbeg], o); }(__cb(_, __frame, 0, 0, _, true, false)); }(__cb(_, __frame, 0, 0, function ___(__0, __7) { if (__7) { nbeg++; while (__more) { __loop(); } __more = true; } else { __break(); } }, true, false)); }); do { __loop(); } while (__more); __more = true; }(function __$_qsort() { return function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$_qsort() { __more = false; return function __$_qsort(_) { + return function __$_qsort(_) { var __2 = beg < nend; if (!__2) { return _(null, __2); } return compare(__cb(_, __frame, 19, 0, function ___(__0, __4) { var __3 = __4 < 0; return _(null, __3); }, true, false), o, array[nend]); }(__cb(_, __frame, 0, 0, _, true, false)); }(__cb(_, __frame, 0, 0, function ___(__0, __9) { if (__9) { nend--; while (__more) { __loop(); } __more = true; } else { __break(); } }, true, false)); }); do { __loop(); } while (__more); __more = true; }(function __$_qsort() { + + if (nbeg <= nend) { tmp = array[nbeg]; + + array[nbeg] = array[nend]; + array[nend] = tmp; + nbeg++; + nend--; } while (__more) { __loop(); } __more = true; }); }); } else { __break(); } }); do { __loop(); } while (__more); __more = true; }(function __$_qsort() { + + + + return function __$_qsort(__then) { if (nbeg < end) { return _qsort(__cb(_, __frame, 30, 0, __then, true, false), nbeg, end); } else { __then(); } }(function __$_qsort() { + return function __$_qsort(__then) { if (beg < nend) { return _qsort(__cb(_, __frame, 31, 0, __then, true, false), beg, nend); } else { __then(); } }(_); }); }); }); }); } var __frame = { name: 'value__8', line: 308 }; return __func(_, this, arguments, value__8, 0, __frame, function __$value__8() { array = __this; beg = beg || 0; end = end == null ? array.length - 1 : end; + + return _qsort(__cb(_, __frame, 38, 0, function __$value__8() { + return _(null, array); }, true, false), beg, end); }); } }); + + + + + + + + + + + + delete Function.prototype.apply_; + Object.defineProperty(Function.prototype, 'apply_', { configurable: true, writable: true, enumerable: false, value: function (callback, thisObj, args, index) { + + + + + args = Array.prototype.slice.call(args, 0); + args.splice(index != null && index >= 0 ? index : args.length, 0, callback); + return this.apply(thisObj, args); } }); }(typeof exports !== 'undefined' ? exports : Streamline.builtins = Streamline.builtins || {})); diff --git a/lib/transform-all.js b/lib/callbacks/transform-all.js similarity index 99% rename from lib/transform-all.js rename to lib/callbacks/transform-all.js index 88fd547a..2cabcea3 100644 --- a/lib/transform-all.js +++ b/lib/callbacks/transform-all.js @@ -9846,6 +9846,77 @@ if (typeof exports !== 'undefined') { return s; } })(typeof exports !== 'undefined' ? exports : (window.Streamline = window.Streamline || {})); + +/// +/// # Container for global context +/// +/// The `globals` module is a container for the global `context` object which is maintained across +/// asynchronous calls. +/// +/// This context is very handy to store information that all calls should be able to access +/// but that you don't want to pass explicitly via function parameters. The most obvious example is +/// the `locale` that each request may set differently and that your low level libraries should +/// be able to retrieve to format messages. +/// +/// `var globals = require('streamline/lib/globals')` +/// +/// * `globals.context = ctx` +/// * `ctx = globals.context` +/// sets and gets the context +/// +/// Note: an empty context (`{}`) is automatically set by the server wrappers of the `streams` module, +/// before they dispatch a request. So, with these wrappers, each request starts with a fresh empty context. +// This module may be loaded several times so we need a true global (with a secret name!). +// This implementation also allows us to share the context between modules compiled in callback and fibers mode. +(function() { + var glob = typeof global === "object" ? global : window; + var secret = "_20c7abceb95c4eb88b7ca1895b1170d1"; + var g = glob[secret] || (glob[secret] = { context: {} }); + if (typeof exports !== 'undefined') { + module.exports = g; + } else { + Streamline.globals = g; + } + // Internal call to manage runtimes + g.runtime || Object.defineProperty(g, 'runtime', { + get: function() { return g.__runtime__; }, + set: function(value) { + if (g.__runtime__ !== value) { + if (g.__runtime__) { + if (/-fast$/.test(g.__runtime__) || + /-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value); + console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value); + } + g.__runtime__ = value; + } + } + }); + + /// + /// * `fn = globals.withContext(fn, cx)` + /// wraps a function so that it executes with context `cx` (or with a new empty context if `cx` is falsy). + /// The previous context will be restored when the function returns (or throws). + /// returns the wrapped function. + g.withContext = function(fn, cx) { + return function() { + var oldContext = g.context; + g.context = cx || {}; + try { + return fn.apply(this, arguments) + } finally { + g.context = oldContext; + } + }; + }; + + g.setPromise = function(name) { + if (g.Promise) return; // first caller wins + var req = require; // defeat streamline-require dependencies + if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise'); + else g.Promise = require(name); + }; +})(); + /** * Copyright (c) 2011 Bruno Jouhier * MIT License diff --git a/lib/compiler/build.sh b/lib/compiler/build.sh index e63315c6..2d9085c8 100755 --- a/lib/compiler/build.sh +++ b/lib/compiler/build.sh @@ -32,8 +32,10 @@ rm builtins.js flows.js # compile test files for client too (standalone, except flows-test) cd ../.. TEST=test/common -bin/_node -lp -v -f --standalone -o $TEST/callbacks/ -c $TEST/{eval,stack,futures}-test._js -bin/_node -lp -v -f -o $TEST/callbacks/ -c $TEST/flows-test._js +# compile eval-test with --standalone to exercise this option +bin/_node -lp -v -f --standalone -o $TEST/callbacks/ -c $TEST/eval-test._js +# the other 3 test the runtime-all.js file. +bin/_node -lp -v -f -o $TEST/callbacks/ -c $TEST/{flows,stack,futures}-test._js bin/_node --generators -v -f -o $TEST/generators/ -c $TEST/*._js cat lib/callbacks/require-stub.js \ @@ -42,20 +44,35 @@ cat lib/callbacks/require-stub.js \ lib/version.js \ lib/util/source-map.js \ lib/callbacks/transform.js \ + lib/globals.js \ lib/util/future.js \ lib/callbacks/runtime.js \ - lib/callbacks/builtins.js | sed -e "s/\/\/\/ \!doc//" > lib/transform-all.js + lib/callbacks/builtins.js | sed -e "s/\/\/\/ \!doc//" > lib/callbacks/transform-all.js + +cat lib/callbacks/require-stub.js \ + lib/globals.js \ + lib/util/future.js \ + lib/callbacks/runtime.js \ + lib/callbacks/builtins.js | sed -e "s/\/\/\/ \!doc//" > lib/callbacks/runtime-all.js cat lib/generators/require-stub.js \ node_modules/esprima/esprima.js \ - node_modules/galaxy/lib/galaxy.js \ lib/callbacks/escodegen-browser.js \ lib/version.js \ lib/util/source-map.js \ lib/fibers/walker.js \ lib/generators/transform.js \ + node_modules/galaxy/lib/galaxy.js \ + lib/globals.js \ + lib/util/future.js \ + lib/generators/runtime.js \ + lib/generators/builtins.js | sed -e "s/\/\/\/ \!doc//" > lib/generators/transform-all.js + +cat lib/generators/require-stub.js \ + node_modules/galaxy/lib/galaxy.js \ + lib/globals.js \ lib/util/future.js \ lib/generators/runtime.js \ - lib/generators/builtins.js | sed -e "s/\/\/\/ \!doc//" > lib/transform-all-generators.js + lib/generators/builtins.js | sed -e "s/\/\/\/ \!doc//" > lib/generators/runtime-all.js popd > /dev/null diff --git a/lib/generators/runtime-all.js b/lib/generators/runtime-all.js new file mode 100644 index 00000000..5dfe54ee --- /dev/null +++ b/lib/generators/runtime-all.js @@ -0,0 +1,1103 @@ +if (!Object.create || !Object.defineProperty || !Object.defineProperties) alert("Example will fail because your browser does not support ECMAScript 5. Try with another browser!"); +var __filename = "" + window.location; + +window.Streamline = { globals: {} }; + +function require(str) { + if (/^\.\.\//.test(str)) str = "streamline/lib" + str.substring(2); + if (str == "streamline/lib/util/flows") return Streamline.flows; + else if (str == "streamline/lib/globals") return Streamline.globals; + else if (str == "streamline/lib/version") return Streamline.version; + else if (str == "streamline/lib/fibers/walker") return Streamline.walker; + else if (str == "streamline/lib/generators/runtime") return Streamline.runtime; + else if (str == "streamline/lib/generators/transform") return Streamline; + else if (str == "streamline/lib/generators/builtins") return Streamline.builtins; + else if (str == "streamline/lib/util/future") return Streamline.future; + else if (str == "galaxy") return galaxy; + else if (str == "galaxy-stack") return null; + else alert("cannot require " + str) +} +"use strict"; +/** + * Copyright (c) 2013 Bruno Jouhier + * MIT License + */ +(function(exports) { + + /// + /// # Main galaxy module + /// + /// `var galaxy = require('galaxy');` + /// + + var resume; + + function link(src, name, dst) { + Object.defineProperty(src, name, { + configurable: false, + writable: true, + enumerable: false, + value: dst + }); + return dst; + } + + + function makeArgs(i) { + if (i <= 0) return ""; + return i > 1 ? makeArgs(i - 1) + ', a' + i : "a1"; + } + + // glob is the hidden global in which we store the context. + // it must be unique, even if we have several versions of the galaxy module. + var glob = (function() { + var gl = typeof global === "object" ? global : window; + // share the secret with streamline's global module. + var secret = "_20c7abceb95c4eb88b7ca1895b1170d1"; + return gl[secret] || link(gl, secret, { context: {} }); + })(); + + var stackHelper, stackHelperError; + try { + stackHelper = require('galaxy-stack'); + } catch (ex) { + stackHelperError = ex.message; + } + + function future(fn, args, i) { + var err, result, done, q = [], + self = this; + args = Array.prototype.slice.call(args); + args[i] = function(e, r) { + //if (arguments.length > 2) r = Array.prototype.slice.call(arguments, 1); + err = e, result = r, done = true; + q && q.forEach(function(f) { + f.call(self, e, r); + }); + q = null; + }; + fn.apply(this, args); + return function F(cb) { + if (typeof cb !== 'function') { + if (cb !== false) throw new Error("invalid argument #0: you must pass _ or !_"); + return F; + } + if (done) cb.call(self, err, result); + else q.push(cb); + } + } + + + function isGenerator(val) { + return Object.prototype.toString.call(val) === "[object Generator]"; + } + + var PENDING = {}; + + function Frame(g) { + this.g = g; + this.prev = glob.frame; + g.frame = this; + this._info = null; + this.recurse = 0; + this.yielded = 0; + } + + Object.defineProperty(Frame.prototype, "info", { + get: function() { + return this._info || stackHelper.getStackFrame(this.g); + } + }); + + Object.defineProperty(Frame.prototype, "name", { + get: function() { return this.info.functionName; } + }); + + Object.defineProperty(Frame.prototype, "file", { + get: function() { return this.info.scriptName; } + }); + + Object.defineProperty(Frame.prototype, "line", { + get: function() { return this.info.lineNumber; } + }); + + function pushFrame(g) { + glob.frame = g.frame = g.frame || new Frame(g); + glob.emitter.emit('enter', g.frame); + } + + function popFrame(g) { + if (!glob.frame) return; + glob.emitter.emit('exit', g.frame); + glob.frame = glob.frame.prev; + } + + var yielded = true; + + function run(g, cb, options) { + var rsm = resume; + var emit = glob.emitter && stackHelper && function(ev, g) { + g.frame = g.frame || new Frame(g); + glob.emitter.emit(ev, g.frame); + } + + try { + resume = function(err, val) { + if (emit && yielded) { + emit("resume", g); + yielded = false; + } + while (g) { + if (options && options.interrupt && options.interrupt()) return; + try { + // ES6 is deprecating send in favor of next. Following line makes us compatible with both. + var send = g.send || g.next; + var v = err ? g.throw (err) : send.call(g, val); + val = v.value; + err = null; + // if we get PENDING, the current call completed with a pending I/O + // resume will be called again when the I/O completes. So just save the context and return here. + if (val === PENDING) { + if (emit && !yielded) { + emit("yield", g); + yielded = true; + } + return; + } + // if we get [PENDING, e, r], the current call invoked its callback synchronously + // we just loop to send/throw what the callback gave us. + if (val && val[0] === PENDING) { + err = val[1]; + val = val[2]; + if (err) err = wrapError(err, g, resume); + } + // else, if g is done we unwind it we send val to the parent generator (or through cb if we are at the top) + else if (v.done) { + //g.close(); + if (emit) popFrame(g); + g = g.prev; + } + // else if val is not a generator we have an error. Yield was not applied to a generators + else { + if (!isGenerator(val)) { + // but we accept an array of generators, and we parallelize in this case + if (!Array.isArray(val)) throw new Error("invalid value was yielded. Expected a generator, got " + val); + val = val.mapStar(-1, function*(elt) { + if (!isGenerator(elt)) throw new Error("invalid array element was yielded. Expected a generator, got " + elt); + return yield elt; + }); + } + // we got a new generator which means that g called another generator function + // the new generator become current and we loop with g.send(undefined) (equiv to g.next()) + val.prev = g; + g = val; + if (emit) pushFrame(g); + val = undefined; + } + } catch (ex) { + // the send/throw call failed. + // we unwind the current generator and we rethrow into the parent generator (or through cb if at the top) + //g.close(); + err = wrapError(ex, g, resume); + if (emit) popFrame(g); + g = g.prev; + val = undefined; + } + } + // we have exhausted the stack of generators. + // return the result or error through the callback. + cb(err, val); + } + + // start the resume loop + resume(); + } finally { + // restore resume global + resume = rsm; + } + } + + function mapResults(options, args) { + if (options && typeof options === "object") { + if (options.returnArray) return args; + if (options.returnObject) return options.returnObject.reduce(function(res, key, i) { + res[key] = args[i]; + return res; + }, {}); + } + return args[0]; + } + + function getTag(options, idx) { + if (options && typeof options === "object") { + if (options.returnArray) return "A" + idx; + if (options.returnObject) return "O" + options.returnObject.join('/') + idx; + } + return idx; + } + + function invoke(that, fn, args, idx, options) { + if (fn['__unstarred__' + idx]) throw new Error("cannot invoke starred function: " + fn['__unstarred__' + idx]); + // Set things up so that call returns: + // * PENDING if it completes with a pending I/O (and cb will be called later) + // * [PENDING, e, r] if the callback is called synchronously. + var result = PENDING, + sync = true; + var rsm = resume; + + // convert args to array so that args.length gets correctly set if idx is args.length + args = Array.prototype.slice.call(args, 0); + var cx = glob.context; + args[idx == null ? args.length : idx] = function(e, r) { + var oldContext = glob.context; + var oldResume = resume; + try { + if (options) r = mapResults(options, Array.prototype.slice.call(arguments, 1)); + glob.context = cx; + resume = rsm; + if (sync) { + result = [PENDING, e, r]; + } else { + resume(e, r); + } + } finally { + glob.context = oldContext; + resume = oldResume; + } + } + fn.apply(that, args); + sync = false; + return result; + } + + function convertAPI(converter, api, idx) { + if (typeof idx === 'string') { + var fn = api[idx]; + if (typeof fn !== 'function') throw new Error("not a function: " + idx); + return converter(fn.bind(api)); + } + return Object.keys(api).reduce(function(result, key) { + var fn = api[key]; + result[key] = (typeof fn === 'function' && !/Sync$/.test(fn.name)) ? converter(fn, idx) : fn; + return result; + }, {}); + } + + /// + /// ## API wrappers + /// + /// * `var genFn = galaxy.star(asyncFn, cbIndex)` + /// This function turns an asynchronous function into a generator function. + /// `asyncFn` is the asynchronous function. + /// `cbIndex` is the index of the callback parameter. It is optional. + /// If omitted the callback is assumed to be the last parameter of `asyncFn`. + var starTemplate = function star(fn, options) { + if (typeof fn === 'object') return convertAPI(exports.star, fn, options); + + var idx = (options && typeof options === 'object') ? options.callbackIndex : options; + var idx2 = idx < 0 ? -(idx + 1) : idx; + var tag = getTag(options, idx); + + var key = '__starred__' + tag; + if (fn[key]) return fn[key]; + + //if (idx == null) idx = fn.length - 1; + + var F = function *() { + if (idx < 0) Array.prototype.splice.call(arguments, idx2, 0, null); + return (yield invoke(this, fn, arguments, idx2, options)); + }; + link(F, '__unstarred__' + tag, fn); + link(fn, key, F); + return F; + } + + var starBody = starTemplate.toString(); + starBody = starBody.substring(starBody.indexOf('{')); + var starrors = []; + + function makeStarror(i) { + return eval("(function(fn, options)" + starBody.replace(/function\s*\*\s*\(\)/, "function*(" + makeArgs(i) + ")") + ")"); + } + + exports.star = function(fn, idx) { + var i = fn.length; + var starror = starrors[i] || (starrors[i] = makeStarror(i)); + return starror(fn, idx); + } + + /// * `var asyncFn = galaxy.unstar(genFn, cbIndex)` + /// This function converts in the other direction. It allows you to turn a generator function into an asynchronous function. + /// `genFn` is the generator function. + /// `cbIndex` is the index of the callback parameter. It is optional. If omitted the callback is added at the end of the parameter list of `genFn`. + /// + /// As previously mentioned these calls may also be applied to a whole module, or to any object containing functions. + /// `Sync` calls are skipped. + // entering is undocumented streamline parameter for future. + var unstarTemplate = function unstar(fn, options, entering) { + if (typeof fn === 'object') return convertAPI(exports.unstar, fn, options); + + var idx = (options && typeof options === 'object') ? options.callbackIndex : options; + if (idx == null) idx = fn.length; + var idx2 = idx < 0 ? -(idx + 1) : idx; + + var key = '__unstarred__' + idx; + if (fn[key]) return fn[key]; + + var F = function() { + var cb = arguments[idx2]; + if (idx < 0) Array.prototype.splice.call(arguments, idx2, 1); + // preserve streamline future semantics in unstarred space. + if (typeof cb !== 'function') { + if (entering && !cb) cb = arguments[idx2] = function(err) { if (err) throw err; }; + return ((options && options.promise) || future).call(this, F, arguments, idx2); + } + var g = fn.apply(this, arguments); + run.call(this, g, cb); + }; + link(F, '__starred__' + idx, fn); + link(fn, key, F); + return F; + } + + var unstarBody = unstarTemplate.toString(); + unstarBody = unstarBody.substring(unstarBody.indexOf('{')); + var unstarrors = []; + + function makeUnstarror(i) { + return eval("(function(fn, options, entering)" + unstarBody.replace(/function\s*\(\)/, "function(" + makeArgs(i) + ")") + ")"); + } + + exports.unstar = function(fn, idx, entering) { + var i = idx == null ? fn.length + 1 : fn.length; + var unstarror = unstarrors[i] || (unstarrors[i] = makeUnstarror(i)); + return unstarror(fn, idx, entering); + } + + + /// + /// ## Parallelizing + /// + /// * `var genFn = galaxy.spin(generator)` + /// Start spinning a generator that you obtained by calling a starred function (without yield). + /// The generator will execute in parallel with other code, at the points where the code yields. + /// The returned value is a generator function on which you can yield later to obtain the result of the computation. + exports.spin = function(g, options) { + if (!isGenerator(g)) throw new Error("Invalid spin call, expected a generator, got " + g); + var that = this; + // use starTemplate rather than galaxy.star because future has arity of 1 and we want arity of 0 + return starTemplate(future(function(cb) { + run.call(that, g, cb, options); + }, [], 0), 0); + } + + /// * `fun = galaxy.funnel(max)` + /// limits the number of concurrent executions of a given code block. + /// + /// The `funnel` function is typically used with the following pattern: + /// + /// ``` javascript + /// // somewhere + /// var myFunnel = galaxy.funnel(10); // create a funnel that only allows 10 concurrent executions. + /// + /// // elsewhere + /// var result = yield myFunnel(function* () { /* code with at most 10 concurrent executions */ }); + /// ``` + /// + /// The `funnel` function can also be used to implement critical sections. Just set funnel's `max` parameter to 1. + /// + /// If `max` is set to 0, a default number of parallel executions is allowed. + /// This default number can be read and set via `galaxy.funnel.defaultSize`. + /// If `max` is negative, the funnel does not limit the level of parallelism. + /// + /// The funnel can be closed with `fun.close()`. + /// When a funnel is closed, the operations that are still in the funnel will continue but their callbacks + /// won't be called, and no other operation will enter the funnel. + exports.funnel = function(max) { + max = max == null ? -1 : max; + if (max === 0) max = funnel.defaultSize; + if (typeof max !== "number") throw new Error("bad max number: " + max); + var queue = [], + active = 0, + closed = false; + + var fun = exports.star(function(fnStar, callback) { + var fn = exports.unstar(fnStar); + if (max < 0 || max == Infinity) return fn(callback); + + queue.push({ + fn: fn, + cb: callback + }); + + function _doOne() { + var current = queue.splice(0, 1)[0]; + if (!current.cb) return current.fn(); + active++; + current.fn(function(err, result) { + active--; + if (!closed) { + current.cb(err, result); + while (active < max && queue.length > 0) _doOne(); + } + }); + } + + while (active < max && queue.length > 0) _doOne(); + }); + fun.close = function() { + queue = [], closed = true; + } + return fun; + } + + exports.funnel.defaultSize = 4; + + /// + /// ## Stable context (TLS-like) + /// + /// * `galaxy.context = ctx` + /// `ctx = galaxy.context` + /// Sets and gets the stable context. + Object.defineProperty(exports, 'context', { + set: function(val) { + glob.context = val; + }, + get: function() { + return glob.context; + } + }); + + /// + /// ## Miscellaneous + /// + /// * `var genCreate = galaxy.new(genConstructor)` + /// Converts a constructor generator function to a _creator_ function. + /// `genConstructor` is a _starred_ constructor that may contain `yield` calls. + /// The returned `genCreate` is a _starred_ function that you can call as `yield genCreate(args)` + + // undocumented idx is set by streamline because prototype is on unstarred function in this case + exports.new = function(constructor, idx) { + var key = '__new__' + idx; + if (constructor[key]) return constructor[key]; + + var F = function *() { + var that = Object.create((idx != null ? constructor['__unstarred__' + idx] : constructor).prototype); + yield constructor.apply(that, arguments); + return that; + }; + link(constructor, key, F); + return F; + } + + // undocumented helper for streamline compat + exports.invoke = function *(that, fn, args, options) { + var idx = (options && typeof options === 'object') ? options.callbackIndex : options; + var tag = getTag(options, idx); + if (typeof fn !== 'function') { + if (typeof that === 'function' && that['__starred__' + tag] && fn === 'call') { + return yield that['__starred__' + tag].apply(args[0], args.slice(1)); + } + fn = that[fn]; + } + return yield exports.star(fn, options).apply(that, args); + } + + function wrapError(err, g, resume) { + if (!(err instanceof Error)) return err; // handle throw "some string"; + if (err.__async__ && err.__async__.resume === resume) return err; + var continuations = []; + g = g.prev; // first frame will be in err.stack + if (stackHelper) { + for (var gg = g; gg; gg = gg.prev) { + continuations.push(stackHelper.getContinuation(gg)); + } + } + err = Object.create(err); + Object.defineProperty(err, 'stack', { + get: function() { + return stackTrace(this); + } + }); + link(err, "__async__", { + continuations: continuations, + generator: g, + resume: resume, + }); + return err; + } + + function stackTrace(err) { + var extra; + var starredStack = ""; + while (extra = err.__async__) { + if (stackHelper) { + var frames = []; + for (var g = extra.generator; g; g = g.prev) { + var frame = stackHelper.getStackFrame(g, extra.continuations[frames.length]); + frames.push(frame); + } + starredStack = frames.filter(function(frame){ + return !(frame.functionName === "exports.invoke" && /\/galaxy\/lib\/galaxy/.test(frame.scriptName)); + }).map(function(frame) { + return ' at ' + frame.functionName + ' (' + frame.scriptName + ':' + frame.lineNumber + ':' + frame.column + ')'; + }).join('\n') + '\n' + starredStack; + } + err = Object.getPrototypeOf(err); + } + if (!stackHelper) starredStack = ' UNAVAILABLE: ' + stackHelperError + '\n'; + + var rawStack = err.stack; + var cut = rawStack.indexOf(' at GeneratorFunctionPrototype'); + if (cut < 0) cut = rawStack.indexOf('\n') + 1; + var result = rawStack.substring(0, cut) + // + ' <<< yield stack >>>\n' + starredStack + // + ' <<< raw stack >>>\n' + rawStack.substring(cut); + return result; + } + + /// * `galaxy.main(function*() { ... })` + /// Wrapper for a main asynchronous script. + /// See the [tutorial](../tutorial/tutorial.md) for an example + exports.main = function(fn) { + var that = this; + exports.unstar(function*() { + yield fn.call(that); + })(function(err) { + if (err) throw err; + }); + } +})(typeof exports !== 'undefined' ? exports : (window.galaxy = window.galaxy || {})); +/// +/// # Container for global context +/// +/// The `globals` module is a container for the global `context` object which is maintained across +/// asynchronous calls. +/// +/// This context is very handy to store information that all calls should be able to access +/// but that you don't want to pass explicitly via function parameters. The most obvious example is +/// the `locale` that each request may set differently and that your low level libraries should +/// be able to retrieve to format messages. +/// +/// `var globals = require('streamline/lib/globals')` +/// +/// * `globals.context = ctx` +/// * `ctx = globals.context` +/// sets and gets the context +/// +/// Note: an empty context (`{}`) is automatically set by the server wrappers of the `streams` module, +/// before they dispatch a request. So, with these wrappers, each request starts with a fresh empty context. +// This module may be loaded several times so we need a true global (with a secret name!). +// This implementation also allows us to share the context between modules compiled in callback and fibers mode. +(function() { + var glob = typeof global === "object" ? global : window; + var secret = "_20c7abceb95c4eb88b7ca1895b1170d1"; + var g = glob[secret] || (glob[secret] = { context: {} }); + if (typeof exports !== 'undefined') { + module.exports = g; + } else { + Streamline.globals = g; + } + // Internal call to manage runtimes + g.runtime || Object.defineProperty(g, 'runtime', { + get: function() { return g.__runtime__; }, + set: function(value) { + if (g.__runtime__ !== value) { + if (g.__runtime__) { + if (/-fast$/.test(g.__runtime__) || + /-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value); + console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value); + } + g.__runtime__ = value; + } + } + }); + + /// + /// * `fn = globals.withContext(fn, cx)` + /// wraps a function so that it executes with context `cx` (or with a new empty context if `cx` is falsy). + /// The previous context will be restored when the function returns (or throws). + /// returns the wrapped function. + g.withContext = function(fn, cx) { + return function() { + var oldContext = g.context; + g.context = cx || {}; + try { + return fn.apply(this, arguments) + } finally { + g.context = oldContext; + } + }; + }; + + g.setPromise = function(name) { + if (g.Promise) return; // first caller wins + var req = require; // defeat streamline-require dependencies + if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise'); + else g.Promise = require(name); + }; +})(); + +/** + * Copyright (c) 2011 Bruno Jouhier + * MIT License + */ +(function(exports) { + var globals = require("../globals"); + + exports.future = function(fn, args, i) { + var err, result, done, q = [], self = this; + args = Array.prototype.slice.call(args); + args[i] = function(e, r) { + err = e, result = r, done = true; + q && q.forEach(function(f) { + f.call(self, e, r); + }); + q = null; + }; + args[i].__futurecb = true; + fn.apply(this, args); + var ret = function F(cb) { + if (typeof cb !== 'function') { + var globals = require('../globals'); + if (cb == null && globals.Promise) return exports.promise.call(this, F, [], 0); + if (cb !== false && !globals.oldStyleFutures) throw new Error("callback missing (argument #0). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error"); + return F; + } + if (done) cb.call(self, err, result); + else q.push(cb); + }; + ret.__future = true; + return ret; + }; + + exports.streamlinify = function(fn, idx) { + return function() { + if (!arguments[idx]) return exports.future.call(this, fn, arguments, idx); + else return fn.apply(this, arguments); + }; + }; + + exports.promise = function(fn, args, i) { + if (args[i] === false) return exports.future.call(this, fn, args, i); + if (args[i] != null) throw new Error("invalid callback: " + typeof(args[i])); + if (globals.oldStyleFutures) return exports.future.call(this, fn, args, i); + if (!globals.Promise) throw new Error("callback missing (argument #" + i + "). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error"); + + var self = this; + args = Array.prototype.slice.call(args); + return new globals.Promise(function(resolve, reject) { + args[i] = function(e, r) { + if (e) reject(e); + else resolve(r); + }; + fn.apply(self, args); + }); + }; + + exports.then = function(promise, method, cb) { + promise[method](function(r) { + cb && cb(null, r); + cb = null; + }, function(e) { + cb && cb(e); + cb = null; + }); + }; +})(typeof exports !== 'undefined' ? exports : (Streamline.future = Streamline.future || {})); + +(function(exports) { + var globals = exports.globals = require('../globals'); + exports.globals.runtime = 'generators'; + require("../generators/builtins"); + var fut = require("../util/future"); + exports.streamlinify = fut.streamlinify; + + var unstar = exports.unstar; + + exports.unstar = function(fn, options, entering) { + if (typeof options === "number") options = { + callbackIndex: options, + }; + else options = options || {}; + options.promise = options.callbackDefault ? function(fn, args, i) { + return fut.future.call(this, fn, args, i)(options.callbackDefault()); + } : fut.promise; + return unstar(fn, options, entering); + } + + exports.then = exports.star(fut.then, 2); +})(typeof exports !== 'undefined' ? module.exports = Object.create(require('galaxy')) : Streamline.runtime = Streamline.runtime || Object.create(require('galaxy'))); +/*** Generated by streamline 0.10.17 (generators) - DO NOT EDIT ***/var galaxy = require("../generators/runtime");(function(){})();(galaxy.unstar(function*(_) { /** + * Copyright (c) 2012 Bruno Jouhier + * MIT License + */ + +/// +/// # Streamline built-ins +/// +(function(exports) { + "use strict"; + var VERSION = 3; + + + + var future = function(fn, args, i) { + var err, result, done, q = [], + self = this; + args = Array.prototype.slice.call(args); + args[i] = function(e, r) { + err = e, result = r, done = true; + q && q.forEach(function(f) { + f.call(self, e, r); + }); + q = null; + }; + fn.apply(this, args); + return function F(cb) { + if (!cb) return F; + if (done) cb.call(self, err, result); + else q.push(cb); + }; + }; + + // Do not use this one directly, require it through the flows module. + exports.funnel = function(max) { + max = max == null ? -1 : max; + if (max === 0) max = funnel.defaultSize; + if (typeof max !== "number") throw new Error("bad max number: " + max); + var queue = [], + active = 0, + closed = false; + + var funCb = function(callback, fn) { + if (callback == null) return future(funCb, arguments, 0); + //console.log("FUNNEL: active=" + active + ", queued=" + queue.length); + if (max < 0 || max == Infinity) return fn( callback); + + queue.push({ + fn: fn, + cb: callback + }); + + function _doOne() { + var current = queue.splice(0, 1)[0]; + if (!current.cb) return current.fn(); + active++; + current.fn( function(err, result) { + active--; + if (!closed) { + current.cb(err, result); + while (active < max && queue.length > 0) _doOne(); + } + }); + } + + while (active < max && queue.length > 0) _doOne(); + }; + var fun = galaxy.streamlinify(funCb, 0); + + fun.close = function() { + queue = [], closed = true; + }; + return fun; + }; + var funnel = exports.funnel; + funnel.defaultSize = 4; + + function _parallel(options) { + if (typeof options === "number") return options; + if (typeof options.parallel === "number") return options.parallel; + return options.parallel ? -1 : 1; + } + + if (Array.prototype.forEach_ && Array.prototype.forEach_.version_ >= VERSION) return; + + // bail out (silently) if JS does not support defineProperty (IE 8). + try { + Object.defineProperty({}, 'x', {}); + } catch (e) { + return; + } + + var has = Object.prototype.hasOwnProperty; + + /// ## Array functions + /// + /// These functions are asynchronous variants of the EcmaScript 5 Array functions. + /// + /// Common Rules: + /// + /// These variants are postfixed by an underscore. + /// They take the `_` callback as first parameter. + /// They pass the `_` callback as first argument to their `fn` callback. + /// Most of them have an optional `options` second parameter which controls the level of + /// parallelism. This `options` parameter may be specified either as `{ parallel: par }` + /// where `par` is an integer, or directly as a `par` integer value. + /// The `par` values are interpreted as follows: + /// + /// * If absent or equal to 1, execution is sequential. + /// * If > 1, at most `par` operations are parallelized. + /// * if 0, a default number of operations are parallelized. + /// This default is defined by `flows.funnel.defaultSize` (4 by default - see `flows` module). + /// * If < 0 or Infinity, operations are fully parallelized (no limit). + /// + /// Functions: + /// + /// * `array.forEach_(_[, options], fn[, thisObj])` + /// `fn` is called as `fn(_, elt, i)`. + delete Array.prototype.forEach_; + Object.defineProperty(Array.prototype, 'forEach_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, options, fn, thisObj) { + if (typeof options === "function") thisObj = fn, fn = options, options = 1; + var par = _parallel(options); + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + if (par === 1 || len <= 1) { + for (var i = 0; i < len; i++) { + if (has.call(this, i)) (yield galaxy.invoke(fn, "call", [thisObj , _ , this[i] , i], 1)); + } + } else { + (yield galaxy.invoke(this, "map_", [_ , par , fn , thisObj], 0)); + } + return this; + }, 0) + }); + Array.prototype.forEach_.version_ = VERSION; + /// * `result = array.map_(_[, options], fn[, thisObj])` + /// `fn` is called as `fn(_, elt, i)`. + delete Array.prototype.map_; + Object.defineProperty(Array.prototype, 'map_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, options, fn, thisObj) { + if (typeof options === "function") thisObj = fn, fn = options, options = 1; + var par = _parallel(options); + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + var result; + if (par === 1 || len <= 1) { + result = new Array(len); + for (var i = 0; i < len; i++) { + if (has.call(this, i)) result[i] = (yield galaxy.invoke(fn, "call", [thisObj , _ , this[i] , i], 1)); + } + } else { + var fun = funnel(par); + result = this.map(function(elt, i) { + return fun(false, galaxy.unstar(function*(_) { + return (yield galaxy.invoke(fn, "call", [thisObj , _ , elt , i], 1)); + }, 0)); + }); + for (var i = 0; i < len; i++) { + if (has.call(this, i)) result[i] = (yield galaxy.invoke(result, i, [_], 0)); + } + } + return result; + }, 0) + }); + /// * `result = array.filter_(_[, options], fn[, thisObj])` + /// `fn` is called as `fn(_, elt)`. + delete Array.prototype.filter_; + Object.defineProperty(Array.prototype, 'filter_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, options, fn, thisObj) { + if (typeof options === "function") thisObj = fn, fn = options, options = 1; + var par = _parallel(options); + thisObj = thisObj !== undefined ? thisObj : this; + var result = []; + var len = this.length; + if (par === 1 || len <= 1) { + for (var i = 0; i < len; i++) { + if (has.call(this, i)) { + var elt = this[i]; + if ((yield galaxy.invoke(fn, "call", [thisObj , _ , elt], 1))) result.push(elt); + } + } + } else { + (yield galaxy.invoke(this, "map_", [_ , par , galaxy.unstar(function*(_, elt) { + if ((yield galaxy.invoke(fn, "call", [thisObj , _ , elt], 1))) result.push(elt); + }, 0) , thisObj], 0)); + } + return result; + }, 0) + }); + /// * `bool = array.every_(_[, options], fn[, thisObj])` + /// `fn` is called as `fn(_, elt)`. + delete Array.prototype.every_; + Object.defineProperty(Array.prototype, 'every_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, options, fn, thisObj) { + if (typeof options === "function") thisObj = fn, fn = options, options = 1; + var par = _parallel(options); + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + if (par === 1 || len <= 1) { + for (var i = 0; i < len; i++) { + + if (has.call(this, i) && !(yield galaxy.invoke(fn, "call", [thisObj , _ , this[i]], 1))) return false; + } + } else { + var fun = funnel(par); + var futures = this.map(function(elt) { + return fun(false, galaxy.unstar(function*(_) { + return (yield galaxy.invoke(fn, "call", [thisObj , _ , elt], 1)); + }, 0)); + }); + for (var i = 0; i < len; i++) { + if (has.call(this, i) && !(yield galaxy.invoke(futures, i, [_], 0))) { + fun.close(); + return false; + } + } + } + return true; + }, 0) + }); + /// * `bool = array.some_(_[, options], fn[, thisObj])` + /// `fn` is called as `fn(_, elt)`. + delete Array.prototype.some_; + Object.defineProperty(Array.prototype, 'some_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, options, fn, thisObj) { + if (typeof options === "function") thisObj = fn, fn = options, options = 1; + var par = _parallel(options); + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + if (par === 1 || len <= 1) { + for (var i = 0; i < len; i++) { + if (has.call(this, i) && (yield galaxy.invoke(fn, "call", [thisObj , _ , this[i]], 1))) return true; + } + } else { + var fun = funnel(par); + var futures = this.map(function(elt) { + return fun(false, galaxy.unstar(function*(_) { + return (yield galaxy.invoke(fn, "call", [thisObj , _ , elt], 1)); + }, 0)); + }); + for (var i = 0; i < len; i++) { + if (has.call(this, i) && (yield galaxy.invoke(futures, i, [_], 0))) { + fun.close(); + return true; + } + } + } + return false; + }, 0) + }); + /// * `result = array.reduce_(_, fn, val[, thisObj])` + /// `fn` is called as `val = fn(_, val, elt, i, array)`. + delete Array.prototype.reduce_; + Object.defineProperty(Array.prototype, 'reduce_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, fn, v, thisObj) { + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + for (var i = 0; i < len; i++) { + if (has.call(this, i)) v = (yield galaxy.invoke(fn, "call", [thisObj , _ , v , this[i] , i , this], 1)); + } + return v; + }, 0) + }); + /// * `result = array.reduceRight_(_, fn, val[, thisObj])` + /// `fn` is called as `val = fn(_, val, elt, i, array)`. + delete Array.prototype.reduceRight_; + Object.defineProperty(Array.prototype, 'reduceRight_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, fn, v, thisObj) { + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + for (var i = len - 1; i >= 0; i--) { + if (has.call(this, i)) v = (yield galaxy.invoke(fn, "call", [thisObj , _ , v , this[i] , i , this], 1)); + } + return v; + }, 0) + }); + + /// * `array = array.sort_(_, compare [, beg [, end]])` + /// `compare` is called as `cmp = compare(_, elt1, elt2)`. + /// Note: this function _changes_ the original array (and returns it). + delete Array.prototype.sort_; + Object.defineProperty(Array.prototype, 'sort_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, compare, beg, end) {var _qsort_ = galaxy.unstar(_qsort, 0); + var array = this; + beg = beg || 0; + end = end == null ? array.length - 1 : end; + + function* _qsort(_, beg, end) { + if (beg >= end) return; + + if (end == beg + 1) { + if ((yield galaxy.invoke(null, compare, [_ , array[beg] , array[end]], 0)) > 0) { + var tmp = array[beg]; + array[beg] = array[end]; + array[end] = tmp; + } + return; + } + + var mid = Math.floor((beg + end) / 2); + var o = array[mid]; + var nbeg = beg; + var nend = end; + + while (nbeg <= nend) { + while (nbeg < end && (yield galaxy.invoke(null, compare, [_ , array[nbeg] , o], 0)) < 0) nbeg++; + while (beg < nend && (yield galaxy.invoke(null, compare, [_ , o , array[nend]], 0)) < 0) nend--; + + if (nbeg <= nend) { + var tmp = array[nbeg]; + array[nbeg] = array[nend]; + array[nend] = tmp; + nbeg++; + nend--; + } + } + + if (nbeg < end) (yield _qsort(_ , nbeg , end)); + if (beg < nend) (yield _qsort(_ , beg , nend)); + } + (yield _qsort(_ , beg , end)); + return array; + }, 0) + }); + + /// + /// ## Function functions + /// + /// * `result = fn.apply_(_, thisObj, args[, index])` + /// Helper to use `Function.prototype.apply` inside streamlined functions. + /// Equivalent to `result = fn.apply(thisObj, argsWith_)` where `argsWith_` is + /// a modified `args` in which the callback has been inserted at `index` + /// (at the end of the argument list if `index` is omitted or negative). + delete Function.prototype.apply_; + Object.defineProperty(Function.prototype, 'apply_', { + configurable: true, + writable: true, + enumerable: false, + value: function(callback, thisObj, args, index) { + args = Array.prototype.slice.call(args, 0); + args.splice(index != null && index >= 0 ? index : args.length, 0, callback); + return this.apply(thisObj, args); + } + }); +})(typeof exports !== 'undefined' ? exports : (Streamline.builtins = Streamline.builtins || {})); +}, 0).call(this, function(err) { + if (err) throw err; +})); diff --git a/lib/generators/transform-all.js b/lib/generators/transform-all.js new file mode 100644 index 00000000..3031da79 --- /dev/null +++ b/lib/generators/transform-all.js @@ -0,0 +1,9825 @@ +if (!Object.create || !Object.defineProperty || !Object.defineProperties) alert("Example will fail because your browser does not support ECMAScript 5. Try with another browser!"); +var __filename = "" + window.location; + +window.Streamline = { globals: {} }; + +function require(str) { + if (/^\.\.\//.test(str)) str = "streamline/lib" + str.substring(2); + if (str == "streamline/lib/util/flows") return Streamline.flows; + else if (str == "streamline/lib/globals") return Streamline.globals; + else if (str == "streamline/lib/version") return Streamline.version; + else if (str == "streamline/lib/fibers/walker") return Streamline.walker; + else if (str == "streamline/lib/generators/runtime") return Streamline.runtime; + else if (str == "streamline/lib/generators/transform") return Streamline; + else if (str == "streamline/lib/generators/builtins") return Streamline.builtins; + else if (str == "streamline/lib/util/future") return Streamline.future; + else if (str == "galaxy") return galaxy; + else if (str == "galaxy-stack") return null; + else alert("cannot require " + str) +} +/* + Copyright (C) 2013 Ariya Hidayat + Copyright (C) 2013 Thaddee Tyl + Copyright (C) 2013 Mathias Bynens + Copyright (C) 2012 Ariya Hidayat + Copyright (C) 2012 Mathias Bynens + Copyright (C) 2012 Joost-Wim Boekesteijn + Copyright (C) 2012 Kris Kowal + Copyright (C) 2012 Yusuke Suzuki + Copyright (C) 2012 Arpad Borsos + Copyright (C) 2011 Ariya Hidayat + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +(function (root, factory) { + 'use strict'; + + // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js, + // Rhino, and plain browser loading. + + /* istanbul ignore next */ + if (typeof define === 'function' && define.amd) { + define(['exports'], factory); + } else if (typeof exports !== 'undefined') { + factory(exports); + } else { + factory((root.esprima = {})); + } +}(this, function (exports) { + 'use strict'; + + var Token, + TokenName, + FnExprTokens, + Syntax, + PlaceHolders, + PropertyKind, + Messages, + Regex, + source, + strict, + index, + lineNumber, + lineStart, + length, + lookahead, + state, + extra; + + Token = { + BooleanLiteral: 1, + EOF: 2, + Identifier: 3, + Keyword: 4, + NullLiteral: 5, + NumericLiteral: 6, + Punctuator: 7, + StringLiteral: 8, + RegularExpression: 9 + }; + + TokenName = {}; + TokenName[Token.BooleanLiteral] = 'Boolean'; + TokenName[Token.EOF] = ''; + TokenName[Token.Identifier] = 'Identifier'; + TokenName[Token.Keyword] = 'Keyword'; + TokenName[Token.NullLiteral] = 'Null'; + TokenName[Token.NumericLiteral] = 'Numeric'; + TokenName[Token.Punctuator] = 'Punctuator'; + TokenName[Token.StringLiteral] = 'String'; + TokenName[Token.RegularExpression] = 'RegularExpression'; + + // A function following one of those tokens is an expression. + FnExprTokens = ['(', '{', '[', 'in', 'typeof', 'instanceof', 'new', + 'return', 'case', 'delete', 'throw', 'void', + // assignment operators + '=', '+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '>>>=', + '&=', '|=', '^=', ',', + // binary/unary operators + '+', '-', '*', '/', '%', '++', '--', '<<', '>>', '>>>', '&', + '|', '^', '!', '~', '&&', '||', '?', ':', '===', '==', '>=', + '<=', '<', '>', '!=', '!==']; + + Syntax = { + AssignmentExpression: 'AssignmentExpression', + ArrayExpression: 'ArrayExpression', + ArrowFunctionExpression: 'ArrowFunctionExpression', + BlockStatement: 'BlockStatement', + BinaryExpression: 'BinaryExpression', + BreakStatement: 'BreakStatement', + CallExpression: 'CallExpression', + CatchClause: 'CatchClause', + ConditionalExpression: 'ConditionalExpression', + ContinueStatement: 'ContinueStatement', + DoWhileStatement: 'DoWhileStatement', + DebuggerStatement: 'DebuggerStatement', + EmptyStatement: 'EmptyStatement', + ExpressionStatement: 'ExpressionStatement', + ForStatement: 'ForStatement', + ForInStatement: 'ForInStatement', + FunctionDeclaration: 'FunctionDeclaration', + FunctionExpression: 'FunctionExpression', + Identifier: 'Identifier', + IfStatement: 'IfStatement', + Literal: 'Literal', + LabeledStatement: 'LabeledStatement', + LogicalExpression: 'LogicalExpression', + MemberExpression: 'MemberExpression', + NewExpression: 'NewExpression', + ObjectExpression: 'ObjectExpression', + Program: 'Program', + Property: 'Property', + ReturnStatement: 'ReturnStatement', + SequenceExpression: 'SequenceExpression', + SwitchStatement: 'SwitchStatement', + SwitchCase: 'SwitchCase', + ThisExpression: 'ThisExpression', + ThrowStatement: 'ThrowStatement', + TryStatement: 'TryStatement', + UnaryExpression: 'UnaryExpression', + UpdateExpression: 'UpdateExpression', + VariableDeclaration: 'VariableDeclaration', + VariableDeclarator: 'VariableDeclarator', + WhileStatement: 'WhileStatement', + WithStatement: 'WithStatement' + }; + + PlaceHolders = { + ArrowParameterPlaceHolder: { + type: 'ArrowParameterPlaceHolder' + } + }; + + PropertyKind = { + Data: 1, + Get: 2, + Set: 4 + }; + + // Error messages should be identical to V8. + Messages = { + UnexpectedToken: 'Unexpected token %0', + UnexpectedNumber: 'Unexpected number', + UnexpectedString: 'Unexpected string', + UnexpectedIdentifier: 'Unexpected identifier', + UnexpectedReserved: 'Unexpected reserved word', + UnexpectedEOS: 'Unexpected end of input', + NewlineAfterThrow: 'Illegal newline after throw', + InvalidRegExp: 'Invalid regular expression', + UnterminatedRegExp: 'Invalid regular expression: missing /', + InvalidLHSInAssignment: 'Invalid left-hand side in assignment', + InvalidLHSInForIn: 'Invalid left-hand side in for-in', + MultipleDefaultsInSwitch: 'More than one default clause in switch statement', + NoCatchOrFinally: 'Missing catch or finally after try', + UnknownLabel: 'Undefined label \'%0\'', + Redeclaration: '%0 \'%1\' has already been declared', + IllegalContinue: 'Illegal continue statement', + IllegalBreak: 'Illegal break statement', + IllegalReturn: 'Illegal return statement', + StrictModeWith: 'Strict mode code may not include a with statement', + StrictCatchVariable: 'Catch variable may not be eval or arguments in strict mode', + StrictVarName: 'Variable name may not be eval or arguments in strict mode', + StrictParamName: 'Parameter name eval or arguments is not allowed in strict mode', + StrictParamDupe: 'Strict mode function may not have duplicate parameter names', + StrictFunctionName: 'Function name may not be eval or arguments in strict mode', + StrictOctalLiteral: 'Octal literals are not allowed in strict mode.', + StrictDelete: 'Delete of an unqualified identifier in strict mode.', + StrictDuplicateProperty: 'Duplicate data property in object literal not allowed in strict mode', + AccessorDataProperty: 'Object literal may not have data and accessor property with the same name', + AccessorGetSet: 'Object literal may not have multiple get/set accessors with the same name', + StrictLHSAssignment: 'Assignment to eval or arguments is not allowed in strict mode', + StrictLHSPostfix: 'Postfix increment/decrement may not have eval or arguments operand in strict mode', + StrictLHSPrefix: 'Prefix increment/decrement may not have eval or arguments operand in strict mode', + StrictReservedWord: 'Use of future reserved word in strict mode' + }; + + // See also tools/generate-unicode-regex.py. + Regex = { + NonAsciiIdentifierStart: new RegExp('[\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0-\u08B2\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA7AD\uA7B0\uA7B1\uA7F7-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB5F\uAB64\uAB65\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]'), + NonAsciiIdentifierPart: new RegExp('[\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0300-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u0483-\u0487\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u05D0-\u05EA\u05F0-\u05F2\u0610-\u061A\u0620-\u0669\u066E-\u06D3\u06D5-\u06DC\u06DF-\u06E8\u06EA-\u06FC\u06FF\u0710-\u074A\u074D-\u07B1\u07C0-\u07F5\u07FA\u0800-\u082D\u0840-\u085B\u08A0-\u08B2\u08E4-\u0963\u0966-\u096F\u0971-\u0983\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BC-\u09C4\u09C7\u09C8\u09CB-\u09CE\u09D7\u09DC\u09DD\u09DF-\u09E3\u09E6-\u09F1\u0A01-\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A59-\u0A5C\u0A5E\u0A66-\u0A75\u0A81-\u0A83\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABC-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AD0\u0AE0-\u0AE3\u0AE6-\u0AEF\u0B01-\u0B03\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3C-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B5C\u0B5D\u0B5F-\u0B63\u0B66-\u0B6F\u0B71\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD0\u0BD7\u0BE6-\u0BEF\u0C00-\u0C03\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C58\u0C59\u0C60-\u0C63\u0C66-\u0C6F\u0C81-\u0C83\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBC-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CDE\u0CE0-\u0CE3\u0CE6-\u0CEF\u0CF1\u0CF2\u0D01-\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D-\u0D44\u0D46-\u0D48\u0D4A-\u0D4E\u0D57\u0D60-\u0D63\u0D66-\u0D6F\u0D7A-\u0D7F\u0D82\u0D83\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E01-\u0E3A\u0E40-\u0E4E\u0E50-\u0E59\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB9\u0EBB-\u0EBD\u0EC0-\u0EC4\u0EC6\u0EC8-\u0ECD\u0ED0-\u0ED9\u0EDC-\u0EDF\u0F00\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E-\u0F47\u0F49-\u0F6C\u0F71-\u0F84\u0F86-\u0F97\u0F99-\u0FBC\u0FC6\u1000-\u1049\u1050-\u109D\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u135D-\u135F\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176C\u176E-\u1770\u1772\u1773\u1780-\u17D3\u17D7\u17DC\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u1820-\u1877\u1880-\u18AA\u18B0-\u18F5\u1900-\u191E\u1920-\u192B\u1930-\u193B\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19D9\u1A00-\u1A1B\u1A20-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AA7\u1AB0-\u1ABD\u1B00-\u1B4B\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1BF3\u1C00-\u1C37\u1C40-\u1C49\u1C4D-\u1C7D\u1CD0-\u1CD2\u1CD4-\u1CF6\u1CF8\u1CF9\u1D00-\u1DF5\u1DFC-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u200C\u200D\u203F\u2040\u2054\u2071\u207F\u2090-\u209C\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D7F-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2DE0-\u2DFF\u2E2F\u3005-\u3007\u3021-\u302F\u3031-\u3035\u3038-\u303C\u3041-\u3096\u3099\u309A\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66F\uA674-\uA67D\uA67F-\uA69D\uA69F-\uA6F1\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA7AD\uA7B0\uA7B1\uA7F7-\uA827\uA840-\uA873\uA880-\uA8C4\uA8D0-\uA8D9\uA8E0-\uA8F7\uA8FB\uA900-\uA92D\uA930-\uA953\uA960-\uA97C\uA980-\uA9C0\uA9CF-\uA9D9\uA9E0-\uA9FE\uAA00-\uAA36\uAA40-\uAA4D\uAA50-\uAA59\uAA60-\uAA76\uAA7A-\uAAC2\uAADB-\uAADD\uAAE0-\uAAEF\uAAF2-\uAAF6\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB5F\uAB64\uAB65\uABC0-\uABEA\uABEC\uABED\uABF0-\uABF9\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE00-\uFE0F\uFE20-\uFE2D\uFE33\uFE34\uFE4D-\uFE4F\uFE70-\uFE74\uFE76-\uFEFC\uFF10-\uFF19\uFF21-\uFF3A\uFF3F\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]') + }; + + // Ensure the condition is true, otherwise throw an error. + // This is only to have a better contract semantic, i.e. another safety net + // to catch a logic error. The condition shall be fulfilled in normal case. + // Do NOT use this to enforce a certain condition on any user input. + + function assert(condition, message) { + /* istanbul ignore if */ + if (!condition) { + throw new Error('ASSERT: ' + message); + } + } + + function isDecimalDigit(ch) { + return (ch >= 0x30 && ch <= 0x39); // 0..9 + } + + function isHexDigit(ch) { + return '0123456789abcdefABCDEF'.indexOf(ch) >= 0; + } + + function isOctalDigit(ch) { + return '01234567'.indexOf(ch) >= 0; + } + + + // 7.2 White Space + + function isWhiteSpace(ch) { + return (ch === 0x20) || (ch === 0x09) || (ch === 0x0B) || (ch === 0x0C) || (ch === 0xA0) || + (ch >= 0x1680 && [0x1680, 0x180E, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x202F, 0x205F, 0x3000, 0xFEFF].indexOf(ch) >= 0); + } + + // 7.3 Line Terminators + + function isLineTerminator(ch) { + return (ch === 0x0A) || (ch === 0x0D) || (ch === 0x2028) || (ch === 0x2029); + } + + // 7.6 Identifier Names and Identifiers + + function isIdentifierStart(ch) { + return (ch === 0x24) || (ch === 0x5F) || // $ (dollar) and _ (underscore) + (ch >= 0x41 && ch <= 0x5A) || // A..Z + (ch >= 0x61 && ch <= 0x7A) || // a..z + (ch === 0x5C) || // \ (backslash) + ((ch >= 0x80) && Regex.NonAsciiIdentifierStart.test(String.fromCharCode(ch))); + } + + function isIdentifierPart(ch) { + return (ch === 0x24) || (ch === 0x5F) || // $ (dollar) and _ (underscore) + (ch >= 0x41 && ch <= 0x5A) || // A..Z + (ch >= 0x61 && ch <= 0x7A) || // a..z + (ch >= 0x30 && ch <= 0x39) || // 0..9 + (ch === 0x5C) || // \ (backslash) + ((ch >= 0x80) && Regex.NonAsciiIdentifierPart.test(String.fromCharCode(ch))); + } + + // 7.6.1.2 Future Reserved Words + + function isFutureReservedWord(id) { + switch (id) { + case 'class': + case 'enum': + case 'export': + case 'extends': + case 'import': + case 'super': + return true; + default: + return false; + } + } + + function isStrictModeReservedWord(id) { + switch (id) { + case 'implements': + case 'interface': + case 'package': + case 'private': + case 'protected': + case 'public': + case 'static': + case 'yield': + case 'let': + return true; + default: + return false; + } + } + + function isRestrictedWord(id) { + return id === 'eval' || id === 'arguments'; + } + + // 7.6.1.1 Keywords + + function isKeyword(id) { + if (strict && isStrictModeReservedWord(id)) { + return true; + } + + // 'const' is specialized as Keyword in V8. + // 'yield' and 'let' are for compatibility with SpiderMonkey and ES.next. + // Some others are from future reserved words. + + switch (id.length) { + case 2: + return (id === 'if') || (id === 'in') || (id === 'do'); + case 3: + return (id === 'var') || (id === 'for') || (id === 'new') || + (id === 'try') || (id === 'let'); + case 4: + return (id === 'this') || (id === 'else') || (id === 'case') || + (id === 'void') || (id === 'with') || (id === 'enum'); + case 5: + return (id === 'while') || (id === 'break') || (id === 'catch') || + (id === 'throw') || (id === 'const') || (id === 'yield') || + (id === 'class') || (id === 'super'); + case 6: + return (id === 'return') || (id === 'typeof') || (id === 'delete') || + (id === 'switch') || (id === 'export') || (id === 'import'); + case 7: + return (id === 'default') || (id === 'finally') || (id === 'extends'); + case 8: + return (id === 'function') || (id === 'continue') || (id === 'debugger'); + case 10: + return (id === 'instanceof'); + default: + return false; + } + } + + // 7.4 Comments + + function addComment(type, value, start, end, loc) { + var comment; + + assert(typeof start === 'number', 'Comment must have valid position'); + + // Because the way the actual token is scanned, often the comments + // (if any) are skipped twice during the lexical analysis. + // Thus, we need to skip adding a comment if the comment array already + // handled it. + if (state.lastCommentStart >= start) { + return; + } + state.lastCommentStart = start; + + comment = { + type: type, + value: value + }; + if (extra.range) { + comment.range = [start, end]; + } + if (extra.loc) { + comment.loc = loc; + } + extra.comments.push(comment); + if (extra.attachComment) { + extra.leadingComments.push(comment); + extra.trailingComments.push(comment); + } + } + + function skipSingleLineComment(offset) { + var start, loc, ch, comment; + + start = index - offset; + loc = { + start: { + line: lineNumber, + column: index - lineStart - offset + } + }; + + while (index < length) { + ch = source.charCodeAt(index); + ++index; + if (isLineTerminator(ch)) { + if (extra.comments) { + comment = source.slice(start + offset, index - 1); + loc.end = { + line: lineNumber, + column: index - lineStart - 1 + }; + addComment('Line', comment, start, index - 1, loc); + } + if (ch === 13 && source.charCodeAt(index) === 10) { + ++index; + } + ++lineNumber; + lineStart = index; + return; + } + } + + if (extra.comments) { + comment = source.slice(start + offset, index); + loc.end = { + line: lineNumber, + column: index - lineStart + }; + addComment('Line', comment, start, index, loc); + } + } + + function skipMultiLineComment() { + var start, loc, ch, comment; + + if (extra.comments) { + start = index - 2; + loc = { + start: { + line: lineNumber, + column: index - lineStart - 2 + } + }; + } + + while (index < length) { + ch = source.charCodeAt(index); + if (isLineTerminator(ch)) { + if (ch === 0x0D && source.charCodeAt(index + 1) === 0x0A) { + ++index; + } + ++lineNumber; + ++index; + lineStart = index; + if (index >= length) { + throwUnexpectedToken(); + } + } else if (ch === 0x2A) { + // Block comment ends with '*/'. + if (source.charCodeAt(index + 1) === 0x2F) { + ++index; + ++index; + if (extra.comments) { + comment = source.slice(start + 2, index - 2); + loc.end = { + line: lineNumber, + column: index - lineStart + }; + addComment('Block', comment, start, index, loc); + } + return; + } + ++index; + } else { + ++index; + } + } + + throwUnexpectedToken(); + } + + function skipComment() { + var ch, start; + + start = (index === 0); + while (index < length) { + ch = source.charCodeAt(index); + + if (isWhiteSpace(ch)) { + ++index; + } else if (isLineTerminator(ch)) { + ++index; + if (ch === 0x0D && source.charCodeAt(index) === 0x0A) { + ++index; + } + ++lineNumber; + lineStart = index; + start = true; + } else if (ch === 0x2F) { // U+002F is '/' + ch = source.charCodeAt(index + 1); + if (ch === 0x2F) { + ++index; + ++index; + skipSingleLineComment(2); + start = true; + } else if (ch === 0x2A) { // U+002A is '*' + ++index; + ++index; + skipMultiLineComment(); + } else { + break; + } + } else if (start && ch === 0x2D) { // U+002D is '-' + // U+003E is '>' + if ((source.charCodeAt(index + 1) === 0x2D) && (source.charCodeAt(index + 2) === 0x3E)) { + // '-->' is a single-line comment + index += 3; + skipSingleLineComment(3); + } else { + break; + } + } else if (ch === 0x3C) { // U+003C is '<' + if (source.slice(index + 1, index + 4) === '!--') { + ++index; // `<` + ++index; // `!` + ++index; // `-` + ++index; // `-` + skipSingleLineComment(4); + } else { + break; + } + } else { + break; + } + } + } + + function scanHexEscape(prefix) { + var i, len, ch, code = 0; + + len = (prefix === 'u') ? 4 : 2; + for (i = 0; i < len; ++i) { + if (index < length && isHexDigit(source[index])) { + ch = source[index++]; + code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase()); + } else { + return ''; + } + } + return String.fromCharCode(code); + } + + function scanUnicodeCodePointEscape() { + var ch, code, cu1, cu2; + + ch = source[index]; + code = 0; + + // At least, one hex digit is required. + if (ch === '}') { + throwUnexpectedToken(); + } + + while (index < length) { + ch = source[index++]; + if (!isHexDigit(ch)) { + break; + } + code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase()); + } + + if (code > 0x10FFFF || ch !== '}') { + throwUnexpectedToken(); + } + + // UTF-16 Encoding + if (code <= 0xFFFF) { + return String.fromCharCode(code); + } + cu1 = ((code - 0x10000) >> 10) + 0xD800; + cu2 = ((code - 0x10000) & 1023) + 0xDC00; + return String.fromCharCode(cu1, cu2); + } + + function getEscapedIdentifier() { + var ch, id; + + ch = source.charCodeAt(index++); + id = String.fromCharCode(ch); + + // '\u' (U+005C, U+0075) denotes an escaped character. + if (ch === 0x5C) { + if (source.charCodeAt(index) !== 0x75) { + throwUnexpectedToken(); + } + ++index; + ch = scanHexEscape('u'); + if (!ch || ch === '\\' || !isIdentifierStart(ch.charCodeAt(0))) { + throwUnexpectedToken(); + } + id = ch; + } + + while (index < length) { + ch = source.charCodeAt(index); + if (!isIdentifierPart(ch)) { + break; + } + ++index; + id += String.fromCharCode(ch); + + // '\u' (U+005C, U+0075) denotes an escaped character. + if (ch === 0x5C) { + id = id.substr(0, id.length - 1); + if (source.charCodeAt(index) !== 0x75) { + throwUnexpectedToken(); + } + ++index; + ch = scanHexEscape('u'); + if (!ch || ch === '\\' || !isIdentifierPart(ch.charCodeAt(0))) { + throwUnexpectedToken(); + } + id += ch; + } + } + + return id; + } + + function getIdentifier() { + var start, ch; + + start = index++; + while (index < length) { + ch = source.charCodeAt(index); + if (ch === 0x5C) { + // Blackslash (U+005C) marks Unicode escape sequence. + index = start; + return getEscapedIdentifier(); + } + if (isIdentifierPart(ch)) { + ++index; + } else { + break; + } + } + + return source.slice(start, index); + } + + function scanIdentifier() { + var start, id, type; + + start = index; + + // Backslash (U+005C) starts an escaped character. + id = (source.charCodeAt(index) === 0x5C) ? getEscapedIdentifier() : getIdentifier(); + + // There is no keyword or literal with only one character. + // Thus, it must be an identifier. + if (id.length === 1) { + type = Token.Identifier; + } else if (isKeyword(id)) { + type = Token.Keyword; + } else if (id === 'null') { + type = Token.NullLiteral; + } else if (id === 'true' || id === 'false') { + type = Token.BooleanLiteral; + } else { + type = Token.Identifier; + } + + return { + type: type, + value: id, + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + + // 7.7 Punctuators + + function scanPunctuator() { + var start = index, + code = source.charCodeAt(index), + code2, + ch1 = source[index], + ch2, + ch3, + ch4; + + switch (code) { + + // Check for most common single-character punctuators. + case 0x2E: // . dot + case 0x28: // ( open bracket + case 0x29: // ) close bracket + case 0x3B: // ; semicolon + case 0x2C: // , comma + case 0x7B: // { open curly brace + case 0x7D: // } close curly brace + case 0x5B: // [ + case 0x5D: // ] + case 0x3A: // : + case 0x3F: // ? + case 0x7E: // ~ + ++index; + if (extra.tokenize) { + if (code === 0x28) { + extra.openParenToken = extra.tokens.length; + } else if (code === 0x7B) { + extra.openCurlyToken = extra.tokens.length; + } + } + return { + type: Token.Punctuator, + value: String.fromCharCode(code), + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + + default: + code2 = source.charCodeAt(index + 1); + + // '=' (U+003D) marks an assignment or comparison operator. + if (code2 === 0x3D) { + switch (code) { + case 0x2B: // + + case 0x2D: // - + case 0x2F: // / + case 0x3C: // < + case 0x3E: // > + case 0x5E: // ^ + case 0x7C: // | + case 0x25: // % + case 0x26: // & + case 0x2A: // * + index += 2; + return { + type: Token.Punctuator, + value: String.fromCharCode(code) + String.fromCharCode(code2), + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + + case 0x21: // ! + case 0x3D: // = + index += 2; + + // !== and === + if (source.charCodeAt(index) === 0x3D) { + ++index; + } + return { + type: Token.Punctuator, + value: source.slice(start, index), + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + } + } + + // 4-character punctuator: >>>= + + ch4 = source.substr(index, 4); + + if (ch4 === '>>>=') { + index += 4; + return { + type: Token.Punctuator, + value: ch4, + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + // 3-character punctuators: === !== >>> <<= >>= + + ch3 = ch4.substr(0, 3); + + if (ch3 === '>>>' || ch3 === '<<=' || ch3 === '>>=') { + index += 3; + return { + type: Token.Punctuator, + value: ch3, + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + // Other 2-character punctuators: ++ -- << >> && || + ch2 = ch3.substr(0, 2); + + if ((ch1 === ch2[1] && ('+-<>&|'.indexOf(ch1) >= 0)) || ch2 === '=>') { + index += 2; + return { + type: Token.Punctuator, + value: ch2, + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + // 1-character punctuators: < > = ! + - * % & | ^ / + + if ('<>=!+-*%&|^/'.indexOf(ch1) >= 0) { + ++index; + return { + type: Token.Punctuator, + value: ch1, + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + throwUnexpectedToken(); + } + + // 7.8.3 Numeric Literals + + function scanHexLiteral(start) { + var number = ''; + + while (index < length) { + if (!isHexDigit(source[index])) { + break; + } + number += source[index++]; + } + + if (number.length === 0) { + throwUnexpectedToken(); + } + + if (isIdentifierStart(source.charCodeAt(index))) { + throwUnexpectedToken(); + } + + return { + type: Token.NumericLiteral, + value: parseInt('0x' + number, 16), + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + function scanBinaryLiteral(start) { + var ch, number; + + number = ''; + + while (index < length) { + ch = source[index]; + if (ch !== '0' && ch !== '1') { + break; + } + number += source[index++]; + } + + if (number.length === 0) { + // only 0b or 0B + throwUnexpectedToken(); + } + + if (index < length) { + ch = source.charCodeAt(index); + /* istanbul ignore else */ + if (isIdentifierStart(ch) || isDecimalDigit(ch)) { + throwUnexpectedToken(); + } + } + + return { + type: Token.NumericLiteral, + value: parseInt(number, 2), + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + function scanOctalLiteral(prefix, start) { + var number, octal; + + if (isOctalDigit(prefix)) { + octal = true; + number = '0' + source[index++]; + } else { + octal = false; + ++index; + number = ''; + } + + while (index < length) { + if (!isOctalDigit(source[index])) { + break; + } + number += source[index++]; + } + + if (!octal && number.length === 0) { + // only 0o or 0O + throwUnexpectedToken(); + } + + if (isIdentifierStart(source.charCodeAt(index)) || isDecimalDigit(source.charCodeAt(index))) { + throwUnexpectedToken(); + } + + return { + type: Token.NumericLiteral, + value: parseInt(number, 8), + octal: octal, + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + function isImplicitOctalLiteral() { + var i, ch; + + // Implicit octal, unless there is a non-octal digit. + // (Annex B.1.1 on Numeric Literals) + for (i = index + 1; i < length; ++i) { + ch = source[i]; + if (ch === '8' || ch === '9') { + return false; + } + if (!isOctalDigit(ch)) { + return true; + } + } + + return true; + } + + function scanNumericLiteral() { + var number, start, ch; + + ch = source[index]; + assert(isDecimalDigit(ch.charCodeAt(0)) || (ch === '.'), + 'Numeric literal must start with a decimal digit or a decimal point'); + + start = index; + number = ''; + if (ch !== '.') { + number = source[index++]; + ch = source[index]; + + // Hex number starts with '0x'. + // Octal number starts with '0'. + // Octal number in ES6 starts with '0o'. + // Binary number in ES6 starts with '0b'. + if (number === '0') { + if (ch === 'x' || ch === 'X') { + ++index; + return scanHexLiteral(start); + } + if (ch === 'b' || ch === 'B') { + ++index; + return scanBinaryLiteral(start); + } + if (ch === 'o' || ch === 'O') { + return scanOctalLiteral(ch, start); + } + + if (isOctalDigit(ch)) { + if (isImplicitOctalLiteral()) { + return scanOctalLiteral(ch, start); + } + } + } + + while (isDecimalDigit(source.charCodeAt(index))) { + number += source[index++]; + } + ch = source[index]; + } + + if (ch === '.') { + number += source[index++]; + while (isDecimalDigit(source.charCodeAt(index))) { + number += source[index++]; + } + ch = source[index]; + } + + if (ch === 'e' || ch === 'E') { + number += source[index++]; + + ch = source[index]; + if (ch === '+' || ch === '-') { + number += source[index++]; + } + if (isDecimalDigit(source.charCodeAt(index))) { + while (isDecimalDigit(source.charCodeAt(index))) { + number += source[index++]; + } + } else { + throwUnexpectedToken(); + } + } + + if (isIdentifierStart(source.charCodeAt(index))) { + throwUnexpectedToken(); + } + + return { + type: Token.NumericLiteral, + value: parseFloat(number), + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + // 7.8.4 String Literals + + function scanStringLiteral() { + var str = '', quote, start, ch, code, unescaped, restore, octal = false, startLineNumber, startLineStart; + startLineNumber = lineNumber; + startLineStart = lineStart; + + quote = source[index]; + assert((quote === '\'' || quote === '"'), + 'String literal must starts with a quote'); + + start = index; + ++index; + + while (index < length) { + ch = source[index++]; + + if (ch === quote) { + quote = ''; + break; + } else if (ch === '\\') { + ch = source[index++]; + if (!ch || !isLineTerminator(ch.charCodeAt(0))) { + switch (ch) { + case 'u': + case 'x': + if (source[index] === '{') { + ++index; + str += scanUnicodeCodePointEscape(); + } else { + restore = index; + unescaped = scanHexEscape(ch); + if (unescaped) { + str += unescaped; + } else { + index = restore; + str += ch; + } + } + break; + case 'n': + str += '\n'; + break; + case 'r': + str += '\r'; + break; + case 't': + str += '\t'; + break; + case 'b': + str += '\b'; + break; + case 'f': + str += '\f'; + break; + case 'v': + str += '\x0B'; + break; + + default: + if (isOctalDigit(ch)) { + code = '01234567'.indexOf(ch); + + // \0 is not octal escape sequence + if (code !== 0) { + octal = true; + } + + if (index < length && isOctalDigit(source[index])) { + octal = true; + code = code * 8 + '01234567'.indexOf(source[index++]); + + // 3 digits are only allowed when string starts + // with 0, 1, 2, 3 + if ('0123'.indexOf(ch) >= 0 && + index < length && + isOctalDigit(source[index])) { + code = code * 8 + '01234567'.indexOf(source[index++]); + } + } + str += String.fromCharCode(code); + } else { + str += ch; + } + break; + } + } else { + ++lineNumber; + if (ch === '\r' && source[index] === '\n') { + ++index; + } + lineStart = index; + } + } else if (isLineTerminator(ch.charCodeAt(0))) { + break; + } else { + str += ch; + } + } + + if (quote !== '') { + throwUnexpectedToken(); + } + + return { + type: Token.StringLiteral, + value: str, + octal: octal, + startLineNumber: startLineNumber, + startLineStart: startLineStart, + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + function testRegExp(pattern, flags) { + var tmp = pattern, + value; + + if (flags.indexOf('u') >= 0) { + // Replace each astral symbol and every Unicode code point + // escape sequence with a single ASCII symbol to avoid throwing on + // regular expressions that are only valid in combination with the + // `/u` flag. + // Note: replacing with the ASCII symbol `x` might cause false + // negatives in unlikely scenarios. For example, `[\u{61}-b]` is a + // perfectly valid pattern that is equivalent to `[a-b]`, but it + // would be replaced by `[x-b]` which throws an error. + tmp = tmp + .replace(/\\u\{([0-9a-fA-F]+)\}/g, function ($0, $1) { + if (parseInt($1, 16) <= 0x10FFFF) { + return 'x'; + } + throwError(Messages.InvalidRegExp); + }) + .replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, 'x'); + } + + // First, detect invalid regular expressions. + try { + value = new RegExp(tmp); + } catch (e) { + throwError(Messages.InvalidRegExp); + } + + // Return a regular expression object for this pattern-flag pair, or + // `null` in case the current environment doesn't support the flags it + // uses. + try { + return new RegExp(pattern, flags); + } catch (exception) { + return null; + } + } + + function scanRegExpBody() { + var ch, str, classMarker, terminated, body; + + ch = source[index]; + assert(ch === '/', 'Regular expression literal must start with a slash'); + str = source[index++]; + + classMarker = false; + terminated = false; + while (index < length) { + ch = source[index++]; + str += ch; + if (ch === '\\') { + ch = source[index++]; + // ECMA-262 7.8.5 + if (isLineTerminator(ch.charCodeAt(0))) { + throwError(Messages.UnterminatedRegExp); + } + str += ch; + } else if (isLineTerminator(ch.charCodeAt(0))) { + throwError(Messages.UnterminatedRegExp); + } else if (classMarker) { + if (ch === ']') { + classMarker = false; + } + } else { + if (ch === '/') { + terminated = true; + break; + } else if (ch === '[') { + classMarker = true; + } + } + } + + if (!terminated) { + throwError(Messages.UnterminatedRegExp); + } + + // Exclude leading and trailing slash. + body = str.substr(1, str.length - 2); + return { + value: body, + literal: str + }; + } + + function scanRegExpFlags() { + var ch, str, flags, restore; + + str = ''; + flags = ''; + while (index < length) { + ch = source[index]; + if (!isIdentifierPart(ch.charCodeAt(0))) { + break; + } + + ++index; + if (ch === '\\' && index < length) { + ch = source[index]; + if (ch === 'u') { + ++index; + restore = index; + ch = scanHexEscape('u'); + if (ch) { + flags += ch; + for (str += '\\u'; restore < index; ++restore) { + str += source[restore]; + } + } else { + index = restore; + flags += 'u'; + str += '\\u'; + } + tolerateUnexpectedToken(); + } else { + str += '\\'; + tolerateUnexpectedToken(); + } + } else { + flags += ch; + str += ch; + } + } + + return { + value: flags, + literal: str + }; + } + + function scanRegExp() { + var start, body, flags, value; + + lookahead = null; + skipComment(); + start = index; + + body = scanRegExpBody(); + flags = scanRegExpFlags(); + value = testRegExp(body.value, flags.value); + + if (extra.tokenize) { + return { + type: Token.RegularExpression, + value: value, + regex: { + pattern: body.value, + flags: flags.value + }, + lineNumber: lineNumber, + lineStart: lineStart, + start: start, + end: index + }; + } + + return { + literal: body.literal + flags.literal, + value: value, + regex: { + pattern: body.value, + flags: flags.value + }, + start: start, + end: index + }; + } + + function collectRegex() { + var pos, loc, regex, token; + + skipComment(); + + pos = index; + loc = { + start: { + line: lineNumber, + column: index - lineStart + } + }; + + regex = scanRegExp(); + + loc.end = { + line: lineNumber, + column: index - lineStart + }; + + /* istanbul ignore next */ + if (!extra.tokenize) { + // Pop the previous token, which is likely '/' or '/=' + if (extra.tokens.length > 0) { + token = extra.tokens[extra.tokens.length - 1]; + if (token.range[0] === pos && token.type === 'Punctuator') { + if (token.value === '/' || token.value === '/=') { + extra.tokens.pop(); + } + } + } + + extra.tokens.push({ + type: 'RegularExpression', + value: regex.literal, + regex: regex.regex, + range: [pos, index], + loc: loc + }); + } + + return regex; + } + + function isIdentifierName(token) { + return token.type === Token.Identifier || + token.type === Token.Keyword || + token.type === Token.BooleanLiteral || + token.type === Token.NullLiteral; + } + + function advanceSlash() { + var prevToken, + checkToken; + // Using the following algorithm: + // https://github.com/mozilla/sweet.js/wiki/design + prevToken = extra.tokens[extra.tokens.length - 1]; + if (!prevToken) { + // Nothing before that: it cannot be a division. + return collectRegex(); + } + if (prevToken.type === 'Punctuator') { + if (prevToken.value === ']') { + return scanPunctuator(); + } + if (prevToken.value === ')') { + checkToken = extra.tokens[extra.openParenToken - 1]; + if (checkToken && + checkToken.type === 'Keyword' && + (checkToken.value === 'if' || + checkToken.value === 'while' || + checkToken.value === 'for' || + checkToken.value === 'with')) { + return collectRegex(); + } + return scanPunctuator(); + } + if (prevToken.value === '}') { + // Dividing a function by anything makes little sense, + // but we have to check for that. + if (extra.tokens[extra.openCurlyToken - 3] && + extra.tokens[extra.openCurlyToken - 3].type === 'Keyword') { + // Anonymous function. + checkToken = extra.tokens[extra.openCurlyToken - 4]; + if (!checkToken) { + return scanPunctuator(); + } + } else if (extra.tokens[extra.openCurlyToken - 4] && + extra.tokens[extra.openCurlyToken - 4].type === 'Keyword') { + // Named function. + checkToken = extra.tokens[extra.openCurlyToken - 5]; + if (!checkToken) { + return collectRegex(); + } + } else { + return scanPunctuator(); + } + // checkToken determines whether the function is + // a declaration or an expression. + if (FnExprTokens.indexOf(checkToken.value) >= 0) { + // It is an expression. + return scanPunctuator(); + } + // It is a declaration. + return collectRegex(); + } + return collectRegex(); + } + if (prevToken.type === 'Keyword' && prevToken.value !== 'this') { + return collectRegex(); + } + return scanPunctuator(); + } + + function advance() { + var ch; + + skipComment(); + + if (index >= length) { + return { + type: Token.EOF, + lineNumber: lineNumber, + lineStart: lineStart, + start: index, + end: index + }; + } + + ch = source.charCodeAt(index); + + if (isIdentifierStart(ch)) { + return scanIdentifier(); + } + + // Very common: ( and ) and ; + if (ch === 0x28 || ch === 0x29 || ch === 0x3B) { + return scanPunctuator(); + } + + // String literal starts with single quote (U+0027) or double quote (U+0022). + if (ch === 0x27 || ch === 0x22) { + return scanStringLiteral(); + } + + + // Dot (.) U+002E can also start a floating-point number, hence the need + // to check the next character. + if (ch === 0x2E) { + if (isDecimalDigit(source.charCodeAt(index + 1))) { + return scanNumericLiteral(); + } + return scanPunctuator(); + } + + if (isDecimalDigit(ch)) { + return scanNumericLiteral(); + } + + // Slash (/) U+002F can also start a regex. + if (extra.tokenize && ch === 0x2F) { + return advanceSlash(); + } + + return scanPunctuator(); + } + + function collectToken() { + var loc, token, value, entry; + + skipComment(); + loc = { + start: { + line: lineNumber, + column: index - lineStart + } + }; + + token = advance(); + loc.end = { + line: lineNumber, + column: index - lineStart + }; + + if (token.type !== Token.EOF) { + value = source.slice(token.start, token.end); + entry = { + type: TokenName[token.type], + value: value, + range: [token.start, token.end], + loc: loc + }; + if (token.regex) { + entry.regex = { + pattern: token.regex.pattern, + flags: token.regex.flags + }; + } + extra.tokens.push(entry); + } + + return token; + } + + function lex() { + var token; + + token = lookahead; + index = token.end; + lineNumber = token.lineNumber; + lineStart = token.lineStart; + + lookahead = (typeof extra.tokens !== 'undefined') ? collectToken() : advance(); + + index = token.end; + lineNumber = token.lineNumber; + lineStart = token.lineStart; + + return token; + } + + function peek() { + var pos, line, start; + + pos = index; + line = lineNumber; + start = lineStart; + lookahead = (typeof extra.tokens !== 'undefined') ? collectToken() : advance(); + index = pos; + lineNumber = line; + lineStart = start; + } + + function Position() { + this.line = lineNumber; + this.column = index - lineStart; + } + + function SourceLocation() { + this.start = new Position(); + this.end = null; + } + + function WrappingSourceLocation(startToken) { + if (startToken.type === Token.StringLiteral) { + this.start = { + line: startToken.startLineNumber, + column: startToken.start - startToken.startLineStart + }; + } else { + this.start = { + line: startToken.lineNumber, + column: startToken.start - startToken.lineStart + }; + } + this.end = null; + } + + function Node() { + // Skip comment. + index = lookahead.start; + if (lookahead.type === Token.StringLiteral) { + lineNumber = lookahead.startLineNumber; + lineStart = lookahead.startLineStart; + } else { + lineNumber = lookahead.lineNumber; + lineStart = lookahead.lineStart; + } + if (extra.range) { + this.range = [index, 0]; + } + if (extra.loc) { + this.loc = new SourceLocation(); + } + } + + function WrappingNode(startToken) { + if (extra.range) { + this.range = [startToken.start, 0]; + } + if (extra.loc) { + this.loc = new WrappingSourceLocation(startToken); + } + } + + WrappingNode.prototype = Node.prototype = { + + processComment: function () { + var lastChild, + leadingComments, + trailingComments, + bottomRight = extra.bottomRightStack, + i, + comment, + last = bottomRight[bottomRight.length - 1]; + + if (this.type === Syntax.Program) { + if (this.body.length > 0) { + return; + } + } + + if (extra.trailingComments.length > 0) { + trailingComments = []; + for (i = extra.trailingComments.length - 1; i >= 0; --i) { + comment = extra.trailingComments[i]; + if (comment.range[0] >= this.range[1]) { + trailingComments.unshift(comment); + extra.trailingComments.splice(i, 1); + } + } + extra.trailingComments = []; + } else { + if (last && last.trailingComments && last.trailingComments[0].range[0] >= this.range[1]) { + trailingComments = last.trailingComments; + delete last.trailingComments; + } + } + + // Eating the stack. + if (last) { + while (last && last.range[0] >= this.range[0]) { + lastChild = last; + last = bottomRight.pop(); + } + } + + if (lastChild) { + if (lastChild.leadingComments && lastChild.leadingComments[lastChild.leadingComments.length - 1].range[1] <= this.range[0]) { + this.leadingComments = lastChild.leadingComments; + lastChild.leadingComments = undefined; + } + } else if (extra.leadingComments.length > 0) { + leadingComments = []; + for (i = extra.leadingComments.length - 1; i >= 0; --i) { + comment = extra.leadingComments[i]; + if (comment.range[1] <= this.range[0]) { + leadingComments.unshift(comment); + extra.leadingComments.splice(i, 1); + } + } + } + + + if (leadingComments && leadingComments.length > 0) { + this.leadingComments = leadingComments; + } + if (trailingComments && trailingComments.length > 0) { + this.trailingComments = trailingComments; + } + + bottomRight.push(this); + }, + + finish: function () { + if (extra.range) { + this.range[1] = index; + } + if (extra.loc) { + this.loc.end = new Position(); + if (extra.source) { + this.loc.source = extra.source; + } + } + + if (extra.attachComment) { + this.processComment(); + } + }, + + finishArrayExpression: function (elements) { + this.type = Syntax.ArrayExpression; + this.elements = elements; + this.finish(); + return this; + }, + + finishArrowFunctionExpression: function (params, defaults, body, expression) { + this.type = Syntax.ArrowFunctionExpression; + this.id = null; + this.params = params; + this.defaults = defaults; + this.body = body; + this.rest = null; + this.generator = false; + this.expression = expression; + this.finish(); + return this; + }, + + finishAssignmentExpression: function (operator, left, right) { + this.type = Syntax.AssignmentExpression; + this.operator = operator; + this.left = left; + this.right = right; + this.finish(); + return this; + }, + + finishBinaryExpression: function (operator, left, right) { + this.type = (operator === '||' || operator === '&&') ? Syntax.LogicalExpression : Syntax.BinaryExpression; + this.operator = operator; + this.left = left; + this.right = right; + this.finish(); + return this; + }, + + finishBlockStatement: function (body) { + this.type = Syntax.BlockStatement; + this.body = body; + this.finish(); + return this; + }, + + finishBreakStatement: function (label) { + this.type = Syntax.BreakStatement; + this.label = label; + this.finish(); + return this; + }, + + finishCallExpression: function (callee, args) { + this.type = Syntax.CallExpression; + this.callee = callee; + this.arguments = args; + this.finish(); + return this; + }, + + finishCatchClause: function (param, body) { + this.type = Syntax.CatchClause; + this.param = param; + this.body = body; + this.finish(); + return this; + }, + + finishConditionalExpression: function (test, consequent, alternate) { + this.type = Syntax.ConditionalExpression; + this.test = test; + this.consequent = consequent; + this.alternate = alternate; + this.finish(); + return this; + }, + + finishContinueStatement: function (label) { + this.type = Syntax.ContinueStatement; + this.label = label; + this.finish(); + return this; + }, + + finishDebuggerStatement: function () { + this.type = Syntax.DebuggerStatement; + this.finish(); + return this; + }, + + finishDoWhileStatement: function (body, test) { + this.type = Syntax.DoWhileStatement; + this.body = body; + this.test = test; + this.finish(); + return this; + }, + + finishEmptyStatement: function () { + this.type = Syntax.EmptyStatement; + this.finish(); + return this; + }, + + finishExpressionStatement: function (expression) { + this.type = Syntax.ExpressionStatement; + this.expression = expression; + this.finish(); + return this; + }, + + finishForStatement: function (init, test, update, body) { + this.type = Syntax.ForStatement; + this.init = init; + this.test = test; + this.update = update; + this.body = body; + this.finish(); + return this; + }, + + finishForInStatement: function (left, right, body) { + this.type = Syntax.ForInStatement; + this.left = left; + this.right = right; + this.body = body; + this.each = false; + this.finish(); + return this; + }, + + finishFunctionDeclaration: function (id, params, defaults, body) { + this.type = Syntax.FunctionDeclaration; + this.id = id; + this.params = params; + this.defaults = defaults; + this.body = body; + this.rest = null; + this.generator = false; + this.expression = false; + this.finish(); + return this; + }, + + finishFunctionExpression: function (id, params, defaults, body) { + this.type = Syntax.FunctionExpression; + this.id = id; + this.params = params; + this.defaults = defaults; + this.body = body; + this.rest = null; + this.generator = false; + this.expression = false; + this.finish(); + return this; + }, + + finishIdentifier: function (name) { + this.type = Syntax.Identifier; + this.name = name; + this.finish(); + return this; + }, + + finishIfStatement: function (test, consequent, alternate) { + this.type = Syntax.IfStatement; + this.test = test; + this.consequent = consequent; + this.alternate = alternate; + this.finish(); + return this; + }, + + finishLabeledStatement: function (label, body) { + this.type = Syntax.LabeledStatement; + this.label = label; + this.body = body; + this.finish(); + return this; + }, + + finishLiteral: function (token) { + this.type = Syntax.Literal; + this.value = token.value; + this.raw = source.slice(token.start, token.end); + if (token.regex) { + this.regex = token.regex; + } + this.finish(); + return this; + }, + + finishMemberExpression: function (accessor, object, property) { + this.type = Syntax.MemberExpression; + this.computed = accessor === '['; + this.object = object; + this.property = property; + this.finish(); + return this; + }, + + finishNewExpression: function (callee, args) { + this.type = Syntax.NewExpression; + this.callee = callee; + this.arguments = args; + this.finish(); + return this; + }, + + finishObjectExpression: function (properties) { + this.type = Syntax.ObjectExpression; + this.properties = properties; + this.finish(); + return this; + }, + + finishPostfixExpression: function (operator, argument) { + this.type = Syntax.UpdateExpression; + this.operator = operator; + this.argument = argument; + this.prefix = false; + this.finish(); + return this; + }, + + finishProgram: function (body) { + this.type = Syntax.Program; + this.body = body; + this.finish(); + return this; + }, + + finishProperty: function (kind, key, value, method, shorthand) { + this.type = Syntax.Property; + this.key = key; + this.value = value; + this.kind = kind; + this.method = method; + this.shorthand = shorthand; + this.finish(); + return this; + }, + + finishReturnStatement: function (argument) { + this.type = Syntax.ReturnStatement; + this.argument = argument; + this.finish(); + return this; + }, + + finishSequenceExpression: function (expressions) { + this.type = Syntax.SequenceExpression; + this.expressions = expressions; + this.finish(); + return this; + }, + + finishSwitchCase: function (test, consequent) { + this.type = Syntax.SwitchCase; + this.test = test; + this.consequent = consequent; + this.finish(); + return this; + }, + + finishSwitchStatement: function (discriminant, cases) { + this.type = Syntax.SwitchStatement; + this.discriminant = discriminant; + this.cases = cases; + this.finish(); + return this; + }, + + finishThisExpression: function () { + this.type = Syntax.ThisExpression; + this.finish(); + return this; + }, + + finishThrowStatement: function (argument) { + this.type = Syntax.ThrowStatement; + this.argument = argument; + this.finish(); + return this; + }, + + finishTryStatement: function (block, guardedHandlers, handlers, finalizer) { + this.type = Syntax.TryStatement; + this.block = block; + this.guardedHandlers = guardedHandlers; + this.handlers = handlers; + this.finalizer = finalizer; + this.finish(); + return this; + }, + + finishUnaryExpression: function (operator, argument) { + this.type = (operator === '++' || operator === '--') ? Syntax.UpdateExpression : Syntax.UnaryExpression; + this.operator = operator; + this.argument = argument; + this.prefix = true; + this.finish(); + return this; + }, + + finishVariableDeclaration: function (declarations, kind) { + this.type = Syntax.VariableDeclaration; + this.declarations = declarations; + this.kind = kind; + this.finish(); + return this; + }, + + finishVariableDeclarator: function (id, init) { + this.type = Syntax.VariableDeclarator; + this.id = id; + this.init = init; + this.finish(); + return this; + }, + + finishWhileStatement: function (test, body) { + this.type = Syntax.WhileStatement; + this.test = test; + this.body = body; + this.finish(); + return this; + }, + + finishWithStatement: function (object, body) { + this.type = Syntax.WithStatement; + this.object = object; + this.body = body; + this.finish(); + return this; + } + }; + + // Return true if there is a line terminator before the next token. + + function peekLineTerminator() { + var pos, line, start, found; + + pos = index; + line = lineNumber; + start = lineStart; + skipComment(); + found = lineNumber !== line; + index = pos; + lineNumber = line; + lineStart = start; + + return found; + } + + function createError(line, pos, description) { + var error = new Error('Line ' + line + ': ' + description); + error.index = pos; + error.lineNumber = line; + error.column = pos - lineStart + 1; + error.description = description; + return error; + } + + // Throw an exception + + function throwError(messageFormat) { + var args, msg; + + args = Array.prototype.slice.call(arguments, 1); + msg = messageFormat.replace(/%(\d)/g, + function (whole, idx) { + assert(idx < args.length, 'Message reference must be in range'); + return args[idx]; + } + ); + + throw createError(lineNumber, index, msg); + } + + function tolerateError(messageFormat) { + var args, msg, error; + + args = Array.prototype.slice.call(arguments, 1); + /* istanbul ignore next */ + msg = messageFormat.replace(/%(\d)/g, + function (whole, idx) { + assert(idx < args.length, 'Message reference must be in range'); + return args[idx]; + } + ); + + error = createError(lineNumber, index, msg); + if (extra.errors) { + extra.errors.push(error); + } else { + throw error; + } + } + + // Throw an exception because of the token. + + function unexpectedTokenError(token, message) { + var msg = Messages.UnexpectedToken; + + if (token) { + msg = message ? message : + (token.type === Token.EOF) ? Messages.UnexpectedEOS : + (token.type === Token.Identifier) ? Messages.UnexpectedIdentifier : + (token.type === Token.NumericLiteral) ? Messages.UnexpectedNumber : + (token.type === Token.StringLiteral) ? Messages.UnexpectedString : + Messages.UnexpectedToken; + + if (token.type === Token.Keyword) { + if (isFutureReservedWord(token.value)) { + msg = Messages.UnexpectedReserved; + } else if (strict && isStrictModeReservedWord(token.value)) { + msg = Messages.StrictReservedWord; + } + } + } + + msg = msg.replace('%0', token ? token.value : 'ILLEGAL'); + + return (token && typeof token.lineNumber === 'number') ? + createError(token.lineNumber, token.start, msg) : + createError(lineNumber, index, msg); + } + + function throwUnexpectedToken(token, message) { + throw unexpectedTokenError(token, message); + } + + function tolerateUnexpectedToken(token, message) { + var error = unexpectedTokenError(token, message); + if (extra.errors) { + extra.errors.push(error); + } else { + throw error; + } + } + + // Expect the next token to match the specified punctuator. + // If not, an exception will be thrown. + + function expect(value) { + var token = lex(); + if (token.type !== Token.Punctuator || token.value !== value) { + throwUnexpectedToken(token); + } + } + + /** + * @name expectCommaSeparator + * @description Quietly expect a comma when in tolerant mode, otherwise delegates + * to expect(value) + * @since 2.0 + */ + function expectCommaSeparator() { + var token; + + if (extra.errors) { + token = lookahead; + if (token.type === Token.Punctuator && token.value === ',') { + lex(); + } else if (token.type === Token.Punctuator && token.value === ';') { + lex(); + tolerateUnexpectedToken(token); + } else { + tolerateUnexpectedToken(token, Messages.UnexpectedToken); + } + } else { + expect(','); + } + } + + // Expect the next token to match the specified keyword. + // If not, an exception will be thrown. + + function expectKeyword(keyword) { + var token = lex(); + if (token.type !== Token.Keyword || token.value !== keyword) { + throwUnexpectedToken(token); + } + } + + // Return true if the next token matches the specified punctuator. + + function match(value) { + return lookahead.type === Token.Punctuator && lookahead.value === value; + } + + // Return true if the next token matches the specified keyword + + function matchKeyword(keyword) { + return lookahead.type === Token.Keyword && lookahead.value === keyword; + } + + // Return true if the next token is an assignment operator + + function matchAssign() { + var op; + + if (lookahead.type !== Token.Punctuator) { + return false; + } + op = lookahead.value; + return op === '=' || + op === '*=' || + op === '/=' || + op === '%=' || + op === '+=' || + op === '-=' || + op === '<<=' || + op === '>>=' || + op === '>>>=' || + op === '&=' || + op === '^=' || + op === '|='; + } + + function consumeSemicolon() { + var line, oldIndex = index, oldLineNumber = lineNumber, + oldLineStart = lineStart, oldLookahead = lookahead; + + // Catch the very common case first: immediately a semicolon (U+003B). + if (source.charCodeAt(index) === 0x3B || match(';')) { + lex(); + return; + } + + line = lineNumber; + skipComment(); + if (lineNumber !== line) { + index = oldIndex; + lineNumber = oldLineNumber; + lineStart = oldLineStart; + lookahead = oldLookahead; + return; + } + + if (lookahead.type !== Token.EOF && !match('}')) { + throwUnexpectedToken(lookahead); + } + } + + // Return true if provided expression is LeftHandSideExpression + + function isLeftHandSide(expr) { + return expr.type === Syntax.Identifier || expr.type === Syntax.MemberExpression; + } + + // 11.1.4 Array Initialiser + + function parseArrayInitialiser() { + var elements = [], node = new Node(); + + expect('['); + + while (!match(']')) { + if (match(',')) { + lex(); + elements.push(null); + } else { + elements.push(parseAssignmentExpression()); + + if (!match(']')) { + expect(','); + } + } + } + + lex(); + + return node.finishArrayExpression(elements); + } + + // 11.1.5 Object Initialiser + + function parsePropertyFunction(param, first) { + var previousStrict, body, node = new Node(); + + previousStrict = strict; + body = parseFunctionSourceElements(); + if (first && strict && isRestrictedWord(param[0].name)) { + tolerateUnexpectedToken(first, Messages.StrictParamName); + } + strict = previousStrict; + return node.finishFunctionExpression(null, param, [], body); + } + + function parsePropertyMethodFunction() { + var previousStrict, param, method; + + previousStrict = strict; + strict = true; + param = parseParams(); + method = parsePropertyFunction(param.params); + strict = previousStrict; + + return method; + } + + function parseObjectPropertyKey() { + var token, node = new Node(); + + token = lex(); + + // Note: This function is called only from parseObjectProperty(), where + // EOF and Punctuator tokens are already filtered out. + + if (token.type === Token.StringLiteral || token.type === Token.NumericLiteral) { + if (strict && token.octal) { + tolerateUnexpectedToken(token, Messages.StrictOctalLiteral); + } + return node.finishLiteral(token); + } + + return node.finishIdentifier(token.value); + } + + function parseObjectProperty() { + var token, key, id, value, param, node = new Node(); + + token = lookahead; + + if (token.type === Token.Identifier) { + + id = parseObjectPropertyKey(); + + // Property Assignment: Getter and Setter. + + if (token.value === 'get' && !(match(':') || match('('))) { + key = parseObjectPropertyKey(); + expect('('); + expect(')'); + value = parsePropertyFunction([]); + return node.finishProperty('get', key, value, false, false); + } + if (token.value === 'set' && !(match(':') || match('('))) { + key = parseObjectPropertyKey(); + expect('('); + token = lookahead; + if (token.type !== Token.Identifier) { + expect(')'); + tolerateUnexpectedToken(token); + value = parsePropertyFunction([]); + } else { + param = [ parseVariableIdentifier() ]; + expect(')'); + value = parsePropertyFunction(param, token); + } + return node.finishProperty('set', key, value, false, false); + } + if (match(':')) { + lex(); + value = parseAssignmentExpression(); + return node.finishProperty('init', id, value, false, false); + } + if (match('(')) { + value = parsePropertyMethodFunction(); + return node.finishProperty('init', id, value, true, false); + } + + value = id; + return node.finishProperty('init', id, value, false, true); + } + if (token.type === Token.EOF || token.type === Token.Punctuator) { + throwUnexpectedToken(token); + } else { + key = parseObjectPropertyKey(); + if (match(':')) { + lex(); + value = parseAssignmentExpression(); + return node.finishProperty('init', key, value, false, false); + } + if (match('(')) { + value = parsePropertyMethodFunction(); + return node.finishProperty('init', key, value, true, false); + } + throwUnexpectedToken(lex()); + } + } + + function parseObjectInitialiser() { + var properties = [], property, name, key, kind, map = {}, toString = String, node = new Node(); + + expect('{'); + + while (!match('}')) { + property = parseObjectProperty(); + + if (property.key.type === Syntax.Identifier) { + name = property.key.name; + } else { + name = toString(property.key.value); + } + kind = (property.kind === 'init') ? PropertyKind.Data : (property.kind === 'get') ? PropertyKind.Get : PropertyKind.Set; + + key = '$' + name; + if (Object.prototype.hasOwnProperty.call(map, key)) { + if (map[key] === PropertyKind.Data) { + if (strict && kind === PropertyKind.Data) { + tolerateError(Messages.StrictDuplicateProperty); + } else if (kind !== PropertyKind.Data) { + tolerateError(Messages.AccessorDataProperty); + } + } else { + if (kind === PropertyKind.Data) { + tolerateError(Messages.AccessorDataProperty); + } else if (map[key] & kind) { + tolerateError(Messages.AccessorGetSet); + } + } + map[key] |= kind; + } else { + map[key] = kind; + } + + properties.push(property); + + if (!match('}')) { + expectCommaSeparator(); + } + } + + expect('}'); + + return node.finishObjectExpression(properties); + } + + // 11.1.6 The Grouping Operator + + function parseGroupExpression() { + var expr; + + expect('('); + + if (match(')')) { + lex(); + return PlaceHolders.ArrowParameterPlaceHolder; + } + + ++state.parenthesisCount; + + expr = parseExpression(); + + expect(')'); + + return expr; + } + + + // 11.1 Primary Expressions + + function parsePrimaryExpression() { + var type, token, expr, node; + + if (match('(')) { + return parseGroupExpression(); + } + + if (match('[')) { + return parseArrayInitialiser(); + } + + if (match('{')) { + return parseObjectInitialiser(); + } + + type = lookahead.type; + node = new Node(); + + if (type === Token.Identifier) { + expr = node.finishIdentifier(lex().value); + } else if (type === Token.StringLiteral || type === Token.NumericLiteral) { + if (strict && lookahead.octal) { + tolerateUnexpectedToken(lookahead, Messages.StrictOctalLiteral); + } + expr = node.finishLiteral(lex()); + } else if (type === Token.Keyword) { + if (matchKeyword('function')) { + return parseFunctionExpression(); + } + if (matchKeyword('this')) { + lex(); + expr = node.finishThisExpression(); + } else { + throwUnexpectedToken(lex()); + } + } else if (type === Token.BooleanLiteral) { + token = lex(); + token.value = (token.value === 'true'); + expr = node.finishLiteral(token); + } else if (type === Token.NullLiteral) { + token = lex(); + token.value = null; + expr = node.finishLiteral(token); + } else if (match('/') || match('/=')) { + if (typeof extra.tokens !== 'undefined') { + expr = node.finishLiteral(collectRegex()); + } else { + expr = node.finishLiteral(scanRegExp()); + } + peek(); + } else { + throwUnexpectedToken(lex()); + } + + return expr; + } + + // 11.2 Left-Hand-Side Expressions + + function parseArguments() { + var args = []; + + expect('('); + + if (!match(')')) { + while (index < length) { + args.push(parseAssignmentExpression()); + if (match(')')) { + break; + } + expectCommaSeparator(); + } + } + + expect(')'); + + return args; + } + + function parseNonComputedProperty() { + var token, node = new Node(); + + token = lex(); + + if (!isIdentifierName(token)) { + throwUnexpectedToken(token); + } + + return node.finishIdentifier(token.value); + } + + function parseNonComputedMember() { + expect('.'); + + return parseNonComputedProperty(); + } + + function parseComputedMember() { + var expr; + + expect('['); + + expr = parseExpression(); + + expect(']'); + + return expr; + } + + function parseNewExpression() { + var callee, args, node = new Node(); + + expectKeyword('new'); + callee = parseLeftHandSideExpression(); + args = match('(') ? parseArguments() : []; + + return node.finishNewExpression(callee, args); + } + + function parseLeftHandSideExpressionAllowCall() { + var expr, args, property, startToken, previousAllowIn = state.allowIn; + + startToken = lookahead; + state.allowIn = true; + expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression(); + + for (;;) { + if (match('.')) { + property = parseNonComputedMember(); + expr = new WrappingNode(startToken).finishMemberExpression('.', expr, property); + } else if (match('(')) { + args = parseArguments(); + expr = new WrappingNode(startToken).finishCallExpression(expr, args); + } else if (match('[')) { + property = parseComputedMember(); + expr = new WrappingNode(startToken).finishMemberExpression('[', expr, property); + } else { + break; + } + } + state.allowIn = previousAllowIn; + + return expr; + } + + function parseLeftHandSideExpression() { + var expr, property, startToken; + assert(state.allowIn, 'callee of new expression always allow in keyword.'); + + startToken = lookahead; + + expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression(); + + for (;;) { + if (match('[')) { + property = parseComputedMember(); + expr = new WrappingNode(startToken).finishMemberExpression('[', expr, property); + } else if (match('.')) { + property = parseNonComputedMember(); + expr = new WrappingNode(startToken).finishMemberExpression('.', expr, property); + } else { + break; + } + } + return expr; + } + + // 11.3 Postfix Expressions + + function parsePostfixExpression() { + var expr, token, startToken = lookahead; + + expr = parseLeftHandSideExpressionAllowCall(); + + if (lookahead.type === Token.Punctuator) { + if ((match('++') || match('--')) && !peekLineTerminator()) { + // 11.3.1, 11.3.2 + if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) { + tolerateError(Messages.StrictLHSPostfix); + } + + if (!isLeftHandSide(expr)) { + tolerateError(Messages.InvalidLHSInAssignment); + } + + token = lex(); + expr = new WrappingNode(startToken).finishPostfixExpression(token.value, expr); + } + } + + return expr; + } + + // 11.4 Unary Operators + + function parseUnaryExpression() { + var token, expr, startToken; + + if (lookahead.type !== Token.Punctuator && lookahead.type !== Token.Keyword) { + expr = parsePostfixExpression(); + } else if (match('++') || match('--')) { + startToken = lookahead; + token = lex(); + expr = parseUnaryExpression(); + // 11.4.4, 11.4.5 + if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) { + tolerateError(Messages.StrictLHSPrefix); + } + + if (!isLeftHandSide(expr)) { + tolerateError(Messages.InvalidLHSInAssignment); + } + + expr = new WrappingNode(startToken).finishUnaryExpression(token.value, expr); + } else if (match('+') || match('-') || match('~') || match('!')) { + startToken = lookahead; + token = lex(); + expr = parseUnaryExpression(); + expr = new WrappingNode(startToken).finishUnaryExpression(token.value, expr); + } else if (matchKeyword('delete') || matchKeyword('void') || matchKeyword('typeof')) { + startToken = lookahead; + token = lex(); + expr = parseUnaryExpression(); + expr = new WrappingNode(startToken).finishUnaryExpression(token.value, expr); + if (strict && expr.operator === 'delete' && expr.argument.type === Syntax.Identifier) { + tolerateError(Messages.StrictDelete); + } + } else { + expr = parsePostfixExpression(); + } + + return expr; + } + + function binaryPrecedence(token, allowIn) { + var prec = 0; + + if (token.type !== Token.Punctuator && token.type !== Token.Keyword) { + return 0; + } + + switch (token.value) { + case '||': + prec = 1; + break; + + case '&&': + prec = 2; + break; + + case '|': + prec = 3; + break; + + case '^': + prec = 4; + break; + + case '&': + prec = 5; + break; + + case '==': + case '!=': + case '===': + case '!==': + prec = 6; + break; + + case '<': + case '>': + case '<=': + case '>=': + case 'instanceof': + prec = 7; + break; + + case 'in': + prec = allowIn ? 7 : 0; + break; + + case '<<': + case '>>': + case '>>>': + prec = 8; + break; + + case '+': + case '-': + prec = 9; + break; + + case '*': + case '/': + case '%': + prec = 11; + break; + + default: + break; + } + + return prec; + } + + // 11.5 Multiplicative Operators + // 11.6 Additive Operators + // 11.7 Bitwise Shift Operators + // 11.8 Relational Operators + // 11.9 Equality Operators + // 11.10 Binary Bitwise Operators + // 11.11 Binary Logical Operators + + function parseBinaryExpression() { + var marker, markers, expr, token, prec, stack, right, operator, left, i; + + marker = lookahead; + left = parseUnaryExpression(); + if (left === PlaceHolders.ArrowParameterPlaceHolder) { + return left; + } + + token = lookahead; + prec = binaryPrecedence(token, state.allowIn); + if (prec === 0) { + return left; + } + token.prec = prec; + lex(); + + markers = [marker, lookahead]; + right = parseUnaryExpression(); + + stack = [left, token, right]; + + while ((prec = binaryPrecedence(lookahead, state.allowIn)) > 0) { + + // Reduce: make a binary expression from the three topmost entries. + while ((stack.length > 2) && (prec <= stack[stack.length - 2].prec)) { + right = stack.pop(); + operator = stack.pop().value; + left = stack.pop(); + markers.pop(); + expr = new WrappingNode(markers[markers.length - 1]).finishBinaryExpression(operator, left, right); + stack.push(expr); + } + + // Shift. + token = lex(); + token.prec = prec; + stack.push(token); + markers.push(lookahead); + expr = parseUnaryExpression(); + stack.push(expr); + } + + // Final reduce to clean-up the stack. + i = stack.length - 1; + expr = stack[i]; + markers.pop(); + while (i > 1) { + expr = new WrappingNode(markers.pop()).finishBinaryExpression(stack[i - 1].value, stack[i - 2], expr); + i -= 2; + } + + return expr; + } + + + // 11.12 Conditional Operator + + function parseConditionalExpression() { + var expr, previousAllowIn, consequent, alternate, startToken; + + startToken = lookahead; + + expr = parseBinaryExpression(); + if (expr === PlaceHolders.ArrowParameterPlaceHolder) { + return expr; + } + if (match('?')) { + lex(); + previousAllowIn = state.allowIn; + state.allowIn = true; + consequent = parseAssignmentExpression(); + state.allowIn = previousAllowIn; + expect(':'); + alternate = parseAssignmentExpression(); + + expr = new WrappingNode(startToken).finishConditionalExpression(expr, consequent, alternate); + } + + return expr; + } + + // [ES6] 14.2 Arrow Function + + function parseConciseBody() { + if (match('{')) { + return parseFunctionSourceElements(); + } + return parseAssignmentExpression(); + } + + function reinterpretAsCoverFormalsList(expressions) { + var i, len, param, params, defaults, defaultCount, options, rest, token; + + params = []; + defaults = []; + defaultCount = 0; + rest = null; + options = { + paramSet: {} + }; + + for (i = 0, len = expressions.length; i < len; i += 1) { + param = expressions[i]; + if (param.type === Syntax.Identifier) { + params.push(param); + defaults.push(null); + validateParam(options, param, param.name); + } else if (param.type === Syntax.AssignmentExpression) { + params.push(param.left); + defaults.push(param.right); + ++defaultCount; + validateParam(options, param.left, param.left.name); + } else { + return null; + } + } + + if (options.message === Messages.StrictParamDupe) { + token = strict ? options.stricted : options.firstRestricted; + throwUnexpectedToken(token, options.message); + } + + if (defaultCount === 0) { + defaults = []; + } + + return { + params: params, + defaults: defaults, + rest: rest, + stricted: options.stricted, + firstRestricted: options.firstRestricted, + message: options.message + }; + } + + function parseArrowFunctionExpression(options, node) { + var previousStrict, body; + + expect('=>'); + previousStrict = strict; + + body = parseConciseBody(); + + if (strict && options.firstRestricted) { + throwUnexpectedToken(options.firstRestricted, options.message); + } + if (strict && options.stricted) { + tolerateUnexpectedToken(options.stricted, options.message); + } + + strict = previousStrict; + + return node.finishArrowFunctionExpression(options.params, options.defaults, body, body.type !== Syntax.BlockStatement); + } + + // 11.13 Assignment Operators + + function parseAssignmentExpression() { + var oldParenthesisCount, token, expr, right, list, startToken; + + oldParenthesisCount = state.parenthesisCount; + + startToken = lookahead; + token = lookahead; + + expr = parseConditionalExpression(); + + if (expr === PlaceHolders.ArrowParameterPlaceHolder || match('=>')) { + if (state.parenthesisCount === oldParenthesisCount || + state.parenthesisCount === (oldParenthesisCount + 1)) { + if (expr.type === Syntax.Identifier) { + list = reinterpretAsCoverFormalsList([ expr ]); + } else if (expr.type === Syntax.AssignmentExpression) { + list = reinterpretAsCoverFormalsList([ expr ]); + } else if (expr.type === Syntax.SequenceExpression) { + list = reinterpretAsCoverFormalsList(expr.expressions); + } else if (expr === PlaceHolders.ArrowParameterPlaceHolder) { + list = reinterpretAsCoverFormalsList([]); + } + if (list) { + return parseArrowFunctionExpression(list, new WrappingNode(startToken)); + } + } + } + + if (matchAssign()) { + // LeftHandSideExpression + if (!isLeftHandSide(expr)) { + tolerateError(Messages.InvalidLHSInAssignment); + } + + // 11.13.1 + if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) { + tolerateUnexpectedToken(token, Messages.StrictLHSAssignment); + } + + token = lex(); + right = parseAssignmentExpression(); + expr = new WrappingNode(startToken).finishAssignmentExpression(token.value, expr, right); + } + + return expr; + } + + // 11.14 Comma Operator + + function parseExpression() { + var expr, startToken = lookahead, expressions; + + expr = parseAssignmentExpression(); + + if (match(',')) { + expressions = [expr]; + + while (index < length) { + if (!match(',')) { + break; + } + lex(); + expressions.push(parseAssignmentExpression()); + } + + expr = new WrappingNode(startToken).finishSequenceExpression(expressions); + } + + return expr; + } + + // 12.1 Block + + function parseStatementList() { + var list = [], + statement; + + while (index < length) { + if (match('}')) { + break; + } + statement = parseSourceElement(); + if (typeof statement === 'undefined') { + break; + } + list.push(statement); + } + + return list; + } + + function parseBlock() { + var block, node = new Node(); + + expect('{'); + + block = parseStatementList(); + + expect('}'); + + return node.finishBlockStatement(block); + } + + // 12.2 Variable Statement + + function parseVariableIdentifier() { + var token, node = new Node(); + + token = lex(); + + if (token.type !== Token.Identifier) { + if (strict && token.type === Token.Keyword && isStrictModeReservedWord(token.value)) { + tolerateUnexpectedToken(token, Messages.StrictReservedWord); + } else { + throwUnexpectedToken(token); + } + } + + return node.finishIdentifier(token.value); + } + + function parseVariableDeclaration(kind) { + var init = null, id, node = new Node(); + + id = parseVariableIdentifier(); + + // 12.2.1 + if (strict && isRestrictedWord(id.name)) { + tolerateError(Messages.StrictVarName); + } + + if (kind === 'const') { + expect('='); + init = parseAssignmentExpression(); + } else if (match('=')) { + lex(); + init = parseAssignmentExpression(); + } + + return node.finishVariableDeclarator(id, init); + } + + function parseVariableDeclarationList(kind) { + var list = []; + + do { + list.push(parseVariableDeclaration(kind)); + if (!match(',')) { + break; + } + lex(); + } while (index < length); + + return list; + } + + function parseVariableStatement(node) { + var declarations; + + expectKeyword('var'); + + declarations = parseVariableDeclarationList(); + + consumeSemicolon(); + + return node.finishVariableDeclaration(declarations, 'var'); + } + + // kind may be `const` or `let` + // Both are experimental and not in the specification yet. + // see http://wiki.ecmascript.org/doku.php?id=harmony:const + // and http://wiki.ecmascript.org/doku.php?id=harmony:let + function parseConstLetDeclaration(kind) { + var declarations, node = new Node(); + + expectKeyword(kind); + + declarations = parseVariableDeclarationList(kind); + + consumeSemicolon(); + + return node.finishVariableDeclaration(declarations, kind); + } + + // 12.3 Empty Statement + + function parseEmptyStatement() { + var node = new Node(); + expect(';'); + return node.finishEmptyStatement(); + } + + // 12.4 Expression Statement + + function parseExpressionStatement(node) { + var expr = parseExpression(); + consumeSemicolon(); + return node.finishExpressionStatement(expr); + } + + // 12.5 If statement + + function parseIfStatement(node) { + var test, consequent, alternate; + + expectKeyword('if'); + + expect('('); + + test = parseExpression(); + + expect(')'); + + consequent = parseStatement(); + + if (matchKeyword('else')) { + lex(); + alternate = parseStatement(); + } else { + alternate = null; + } + + return node.finishIfStatement(test, consequent, alternate); + } + + // 12.6 Iteration Statements + + function parseDoWhileStatement(node) { + var body, test, oldInIteration; + + expectKeyword('do'); + + oldInIteration = state.inIteration; + state.inIteration = true; + + body = parseStatement(); + + state.inIteration = oldInIteration; + + expectKeyword('while'); + + expect('('); + + test = parseExpression(); + + expect(')'); + + if (match(';')) { + lex(); + } + + return node.finishDoWhileStatement(body, test); + } + + function parseWhileStatement(node) { + var test, body, oldInIteration; + + expectKeyword('while'); + + expect('('); + + test = parseExpression(); + + expect(')'); + + oldInIteration = state.inIteration; + state.inIteration = true; + + body = parseStatement(); + + state.inIteration = oldInIteration; + + return node.finishWhileStatement(test, body); + } + + function parseForVariableDeclaration() { + var token, declarations, node = new Node(); + + token = lex(); + declarations = parseVariableDeclarationList(); + + return node.finishVariableDeclaration(declarations, token.value); + } + + function parseForStatement(node) { + var init, test, update, left, right, body, oldInIteration, previousAllowIn = state.allowIn; + + init = test = update = null; + + expectKeyword('for'); + + expect('('); + + if (match(';')) { + lex(); + } else { + if (matchKeyword('var') || matchKeyword('let')) { + state.allowIn = false; + init = parseForVariableDeclaration(); + state.allowIn = previousAllowIn; + + if (init.declarations.length === 1 && matchKeyword('in')) { + lex(); + left = init; + right = parseExpression(); + init = null; + } + } else { + state.allowIn = false; + init = parseExpression(); + state.allowIn = previousAllowIn; + + if (matchKeyword('in')) { + // LeftHandSideExpression + if (!isLeftHandSide(init)) { + tolerateError(Messages.InvalidLHSInForIn); + } + + lex(); + left = init; + right = parseExpression(); + init = null; + } + } + + if (typeof left === 'undefined') { + expect(';'); + } + } + + if (typeof left === 'undefined') { + + if (!match(';')) { + test = parseExpression(); + } + expect(';'); + + if (!match(')')) { + update = parseExpression(); + } + } + + expect(')'); + + oldInIteration = state.inIteration; + state.inIteration = true; + + body = parseStatement(); + + state.inIteration = oldInIteration; + + return (typeof left === 'undefined') ? + node.finishForStatement(init, test, update, body) : + node.finishForInStatement(left, right, body); + } + + // 12.7 The continue statement + + function parseContinueStatement(node) { + var label = null, key; + + expectKeyword('continue'); + + // Optimize the most common form: 'continue;'. + if (source.charCodeAt(index) === 0x3B) { + lex(); + + if (!state.inIteration) { + throwError(Messages.IllegalContinue); + } + + return node.finishContinueStatement(null); + } + + if (peekLineTerminator()) { + if (!state.inIteration) { + throwError(Messages.IllegalContinue); + } + + return node.finishContinueStatement(null); + } + + if (lookahead.type === Token.Identifier) { + label = parseVariableIdentifier(); + + key = '$' + label.name; + if (!Object.prototype.hasOwnProperty.call(state.labelSet, key)) { + throwError(Messages.UnknownLabel, label.name); + } + } + + consumeSemicolon(); + + if (label === null && !state.inIteration) { + throwError(Messages.IllegalContinue); + } + + return node.finishContinueStatement(label); + } + + // 12.8 The break statement + + function parseBreakStatement(node) { + var label = null, key; + + expectKeyword('break'); + + // Catch the very common case first: immediately a semicolon (U+003B). + if (source.charCodeAt(index) === 0x3B) { + lex(); + + if (!(state.inIteration || state.inSwitch)) { + throwError(Messages.IllegalBreak); + } + + return node.finishBreakStatement(null); + } + + if (peekLineTerminator()) { + if (!(state.inIteration || state.inSwitch)) { + throwError(Messages.IllegalBreak); + } + + return node.finishBreakStatement(null); + } + + if (lookahead.type === Token.Identifier) { + label = parseVariableIdentifier(); + + key = '$' + label.name; + if (!Object.prototype.hasOwnProperty.call(state.labelSet, key)) { + throwError(Messages.UnknownLabel, label.name); + } + } + + consumeSemicolon(); + + if (label === null && !(state.inIteration || state.inSwitch)) { + throwError(Messages.IllegalBreak); + } + + return node.finishBreakStatement(label); + } + + // 12.9 The return statement + + function parseReturnStatement(node) { + var argument = null; + + expectKeyword('return'); + + if (!state.inFunctionBody) { + tolerateError(Messages.IllegalReturn); + } + + // 'return' followed by a space and an identifier is very common. + if (source.charCodeAt(index) === 0x20) { + if (isIdentifierStart(source.charCodeAt(index + 1))) { + argument = parseExpression(); + consumeSemicolon(); + return node.finishReturnStatement(argument); + } + } + + if (peekLineTerminator()) { + return node.finishReturnStatement(null); + } + + if (!match(';')) { + if (!match('}') && lookahead.type !== Token.EOF) { + argument = parseExpression(); + } + } + + consumeSemicolon(); + + return node.finishReturnStatement(argument); + } + + // 12.10 The with statement + + function parseWithStatement(node) { + var object, body; + + if (strict) { + // TODO(ikarienator): Should we update the test cases instead? + skipComment(); + tolerateError(Messages.StrictModeWith); + } + + expectKeyword('with'); + + expect('('); + + object = parseExpression(); + + expect(')'); + + body = parseStatement(); + + return node.finishWithStatement(object, body); + } + + // 12.10 The swith statement + + function parseSwitchCase() { + var test, consequent = [], statement, node = new Node(); + + if (matchKeyword('default')) { + lex(); + test = null; + } else { + expectKeyword('case'); + test = parseExpression(); + } + expect(':'); + + while (index < length) { + if (match('}') || matchKeyword('default') || matchKeyword('case')) { + break; + } + statement = parseStatement(); + consequent.push(statement); + } + + return node.finishSwitchCase(test, consequent); + } + + function parseSwitchStatement(node) { + var discriminant, cases, clause, oldInSwitch, defaultFound; + + expectKeyword('switch'); + + expect('('); + + discriminant = parseExpression(); + + expect(')'); + + expect('{'); + + cases = []; + + if (match('}')) { + lex(); + return node.finishSwitchStatement(discriminant, cases); + } + + oldInSwitch = state.inSwitch; + state.inSwitch = true; + defaultFound = false; + + while (index < length) { + if (match('}')) { + break; + } + clause = parseSwitchCase(); + if (clause.test === null) { + if (defaultFound) { + throwError(Messages.MultipleDefaultsInSwitch); + } + defaultFound = true; + } + cases.push(clause); + } + + state.inSwitch = oldInSwitch; + + expect('}'); + + return node.finishSwitchStatement(discriminant, cases); + } + + // 12.13 The throw statement + + function parseThrowStatement(node) { + var argument; + + expectKeyword('throw'); + + if (peekLineTerminator()) { + throwError(Messages.NewlineAfterThrow); + } + + argument = parseExpression(); + + consumeSemicolon(); + + return node.finishThrowStatement(argument); + } + + // 12.14 The try statement + + function parseCatchClause() { + var param, body, node = new Node(); + + expectKeyword('catch'); + + expect('('); + if (match(')')) { + throwUnexpectedToken(lookahead); + } + + param = parseVariableIdentifier(); + // 12.14.1 + if (strict && isRestrictedWord(param.name)) { + tolerateError(Messages.StrictCatchVariable); + } + + expect(')'); + body = parseBlock(); + return node.finishCatchClause(param, body); + } + + function parseTryStatement(node) { + var block, handlers = [], finalizer = null; + + expectKeyword('try'); + + block = parseBlock(); + + if (matchKeyword('catch')) { + handlers.push(parseCatchClause()); + } + + if (matchKeyword('finally')) { + lex(); + finalizer = parseBlock(); + } + + if (handlers.length === 0 && !finalizer) { + throwError(Messages.NoCatchOrFinally); + } + + return node.finishTryStatement(block, [], handlers, finalizer); + } + + // 12.15 The debugger statement + + function parseDebuggerStatement(node) { + expectKeyword('debugger'); + + consumeSemicolon(); + + return node.finishDebuggerStatement(); + } + + // 12 Statements + + function parseStatement() { + var type = lookahead.type, + expr, + labeledBody, + key, + node; + + if (type === Token.EOF) { + throwUnexpectedToken(lookahead); + } + + if (type === Token.Punctuator && lookahead.value === '{') { + return parseBlock(); + } + + node = new Node(); + + if (type === Token.Punctuator) { + switch (lookahead.value) { + case ';': + return parseEmptyStatement(node); + case '(': + return parseExpressionStatement(node); + default: + break; + } + } else if (type === Token.Keyword) { + switch (lookahead.value) { + case 'break': + return parseBreakStatement(node); + case 'continue': + return parseContinueStatement(node); + case 'debugger': + return parseDebuggerStatement(node); + case 'do': + return parseDoWhileStatement(node); + case 'for': + return parseForStatement(node); + case 'function': + return parseFunctionDeclaration(node); + case 'if': + return parseIfStatement(node); + case 'return': + return parseReturnStatement(node); + case 'switch': + return parseSwitchStatement(node); + case 'throw': + return parseThrowStatement(node); + case 'try': + return parseTryStatement(node); + case 'var': + return parseVariableStatement(node); + case 'while': + return parseWhileStatement(node); + case 'with': + return parseWithStatement(node); + default: + break; + } + } + + expr = parseExpression(); + + // 12.12 Labelled Statements + if ((expr.type === Syntax.Identifier) && match(':')) { + lex(); + + key = '$' + expr.name; + if (Object.prototype.hasOwnProperty.call(state.labelSet, key)) { + throwError(Messages.Redeclaration, 'Label', expr.name); + } + + state.labelSet[key] = true; + labeledBody = parseStatement(); + delete state.labelSet[key]; + return node.finishLabeledStatement(expr, labeledBody); + } + + consumeSemicolon(); + + return node.finishExpressionStatement(expr); + } + + // 13 Function Definition + + function parseFunctionSourceElements() { + var sourceElement, sourceElements = [], token, directive, firstRestricted, + oldLabelSet, oldInIteration, oldInSwitch, oldInFunctionBody, oldParenthesisCount, + node = new Node(); + + expect('{'); + + while (index < length) { + if (lookahead.type !== Token.StringLiteral) { + break; + } + token = lookahead; + + sourceElement = parseSourceElement(); + sourceElements.push(sourceElement); + if (sourceElement.expression.type !== Syntax.Literal) { + // this is not directive + break; + } + directive = source.slice(token.start + 1, token.end - 1); + if (directive === 'use strict') { + strict = true; + if (firstRestricted) { + tolerateUnexpectedToken(firstRestricted, Messages.StrictOctalLiteral); + } + } else { + if (!firstRestricted && token.octal) { + firstRestricted = token; + } + } + } + + oldLabelSet = state.labelSet; + oldInIteration = state.inIteration; + oldInSwitch = state.inSwitch; + oldInFunctionBody = state.inFunctionBody; + oldParenthesisCount = state.parenthesizedCount; + + state.labelSet = {}; + state.inIteration = false; + state.inSwitch = false; + state.inFunctionBody = true; + state.parenthesizedCount = 0; + + while (index < length) { + if (match('}')) { + break; + } + sourceElement = parseSourceElement(); + if (typeof sourceElement === 'undefined') { + break; + } + sourceElements.push(sourceElement); + } + + expect('}'); + + state.labelSet = oldLabelSet; + state.inIteration = oldInIteration; + state.inSwitch = oldInSwitch; + state.inFunctionBody = oldInFunctionBody; + state.parenthesizedCount = oldParenthesisCount; + + return node.finishBlockStatement(sourceElements); + } + + function validateParam(options, param, name) { + var key = '$' + name; + if (strict) { + if (isRestrictedWord(name)) { + options.stricted = param; + options.message = Messages.StrictParamName; + } + if (Object.prototype.hasOwnProperty.call(options.paramSet, key)) { + options.stricted = param; + options.message = Messages.StrictParamDupe; + } + } else if (!options.firstRestricted) { + if (isRestrictedWord(name)) { + options.firstRestricted = param; + options.message = Messages.StrictParamName; + } else if (isStrictModeReservedWord(name)) { + options.firstRestricted = param; + options.message = Messages.StrictReservedWord; + } else if (Object.prototype.hasOwnProperty.call(options.paramSet, key)) { + options.firstRestricted = param; + options.message = Messages.StrictParamDupe; + } + } + options.paramSet[key] = true; + } + + function parseParam(options) { + var token, param, def; + + token = lookahead; + param = parseVariableIdentifier(); + validateParam(options, token, token.value); + if (match('=')) { + lex(); + def = parseAssignmentExpression(); + ++options.defaultCount; + } + + options.params.push(param); + options.defaults.push(def); + + return !match(')'); + } + + function parseParams(firstRestricted) { + var options; + + options = { + params: [], + defaultCount: 0, + defaults: [], + firstRestricted: firstRestricted + }; + + expect('('); + + if (!match(')')) { + options.paramSet = {}; + while (index < length) { + if (!parseParam(options)) { + break; + } + expect(','); + } + } + + expect(')'); + + if (options.defaultCount === 0) { + options.defaults = []; + } + + return { + params: options.params, + defaults: options.defaults, + stricted: options.stricted, + firstRestricted: options.firstRestricted, + message: options.message + }; + } + + function parseFunctionDeclaration() { + var id, params = [], defaults = [], body, token, stricted, tmp, firstRestricted, message, previousStrict, node = new Node(); + + expectKeyword('function'); + token = lookahead; + id = parseVariableIdentifier(); + if (strict) { + if (isRestrictedWord(token.value)) { + tolerateUnexpectedToken(token, Messages.StrictFunctionName); + } + } else { + if (isRestrictedWord(token.value)) { + firstRestricted = token; + message = Messages.StrictFunctionName; + } else if (isStrictModeReservedWord(token.value)) { + firstRestricted = token; + message = Messages.StrictReservedWord; + } + } + + tmp = parseParams(firstRestricted); + params = tmp.params; + defaults = tmp.defaults; + stricted = tmp.stricted; + firstRestricted = tmp.firstRestricted; + if (tmp.message) { + message = tmp.message; + } + + previousStrict = strict; + body = parseFunctionSourceElements(); + if (strict && firstRestricted) { + throwUnexpectedToken(firstRestricted, message); + } + if (strict && stricted) { + tolerateUnexpectedToken(stricted, message); + } + strict = previousStrict; + + return node.finishFunctionDeclaration(id, params, defaults, body); + } + + function parseFunctionExpression() { + var token, id = null, stricted, firstRestricted, message, tmp, + params = [], defaults = [], body, previousStrict, node = new Node(); + + expectKeyword('function'); + + if (!match('(')) { + token = lookahead; + id = parseVariableIdentifier(); + if (strict) { + if (isRestrictedWord(token.value)) { + tolerateUnexpectedToken(token, Messages.StrictFunctionName); + } + } else { + if (isRestrictedWord(token.value)) { + firstRestricted = token; + message = Messages.StrictFunctionName; + } else if (isStrictModeReservedWord(token.value)) { + firstRestricted = token; + message = Messages.StrictReservedWord; + } + } + } + + tmp = parseParams(firstRestricted); + params = tmp.params; + defaults = tmp.defaults; + stricted = tmp.stricted; + firstRestricted = tmp.firstRestricted; + if (tmp.message) { + message = tmp.message; + } + + previousStrict = strict; + body = parseFunctionSourceElements(); + if (strict && firstRestricted) { + throwUnexpectedToken(firstRestricted, message); + } + if (strict && stricted) { + tolerateUnexpectedToken(stricted, message); + } + strict = previousStrict; + + return node.finishFunctionExpression(id, params, defaults, body); + } + + // 14 Program + + function parseSourceElement() { + if (lookahead.type === Token.Keyword) { + switch (lookahead.value) { + case 'const': + case 'let': + return parseConstLetDeclaration(lookahead.value); + case 'function': + return parseFunctionDeclaration(); + default: + return parseStatement(); + } + } + + if (lookahead.type !== Token.EOF) { + return parseStatement(); + } + } + + function parseSourceElements() { + var sourceElement, sourceElements = [], token, directive, firstRestricted; + + while (index < length) { + token = lookahead; + if (token.type !== Token.StringLiteral) { + break; + } + + sourceElement = parseSourceElement(); + sourceElements.push(sourceElement); + if (sourceElement.expression.type !== Syntax.Literal) { + // this is not directive + break; + } + directive = source.slice(token.start + 1, token.end - 1); + if (directive === 'use strict') { + strict = true; + if (firstRestricted) { + tolerateUnexpectedToken(firstRestricted, Messages.StrictOctalLiteral); + } + } else { + if (!firstRestricted && token.octal) { + firstRestricted = token; + } + } + } + + while (index < length) { + sourceElement = parseSourceElement(); + /* istanbul ignore if */ + if (typeof sourceElement === 'undefined') { + break; + } + sourceElements.push(sourceElement); + } + return sourceElements; + } + + function parseProgram() { + var body, node; + + skipComment(); + peek(); + node = new Node(); + strict = false; + + body = parseSourceElements(); + return node.finishProgram(body); + } + + function filterTokenLocation() { + var i, entry, token, tokens = []; + + for (i = 0; i < extra.tokens.length; ++i) { + entry = extra.tokens[i]; + token = { + type: entry.type, + value: entry.value + }; + if (entry.regex) { + token.regex = { + pattern: entry.regex.pattern, + flags: entry.regex.flags + }; + } + if (extra.range) { + token.range = entry.range; + } + if (extra.loc) { + token.loc = entry.loc; + } + tokens.push(token); + } + + extra.tokens = tokens; + } + + function tokenize(code, options) { + var toString, + tokens; + + toString = String; + if (typeof code !== 'string' && !(code instanceof String)) { + code = toString(code); + } + + source = code; + index = 0; + lineNumber = (source.length > 0) ? 1 : 0; + lineStart = 0; + length = source.length; + lookahead = null; + state = { + allowIn: true, + labelSet: {}, + inFunctionBody: false, + inIteration: false, + inSwitch: false, + lastCommentStart: -1 + }; + + extra = {}; + + // Options matching. + options = options || {}; + + // Of course we collect tokens here. + options.tokens = true; + extra.tokens = []; + extra.tokenize = true; + // The following two fields are necessary to compute the Regex tokens. + extra.openParenToken = -1; + extra.openCurlyToken = -1; + + extra.range = (typeof options.range === 'boolean') && options.range; + extra.loc = (typeof options.loc === 'boolean') && options.loc; + + if (typeof options.comment === 'boolean' && options.comment) { + extra.comments = []; + } + if (typeof options.tolerant === 'boolean' && options.tolerant) { + extra.errors = []; + } + + try { + peek(); + if (lookahead.type === Token.EOF) { + return extra.tokens; + } + + lex(); + while (lookahead.type !== Token.EOF) { + try { + lex(); + } catch (lexError) { + if (extra.errors) { + extra.errors.push(lexError); + // We have to break on the first error + // to avoid infinite loops. + break; + } else { + throw lexError; + } + } + } + + filterTokenLocation(); + tokens = extra.tokens; + if (typeof extra.comments !== 'undefined') { + tokens.comments = extra.comments; + } + if (typeof extra.errors !== 'undefined') { + tokens.errors = extra.errors; + } + } catch (e) { + throw e; + } finally { + extra = {}; + } + return tokens; + } + + function parse(code, options) { + var program, toString; + + toString = String; + if (typeof code !== 'string' && !(code instanceof String)) { + code = toString(code); + } + + source = code; + index = 0; + lineNumber = (source.length > 0) ? 1 : 0; + lineStart = 0; + length = source.length; + lookahead = null; + state = { + allowIn: true, + labelSet: {}, + parenthesisCount: 0, + inFunctionBody: false, + inIteration: false, + inSwitch: false, + lastCommentStart: -1 + }; + + extra = {}; + if (typeof options !== 'undefined') { + extra.range = (typeof options.range === 'boolean') && options.range; + extra.loc = (typeof options.loc === 'boolean') && options.loc; + extra.attachComment = (typeof options.attachComment === 'boolean') && options.attachComment; + + if (extra.loc && options.source !== null && options.source !== undefined) { + extra.source = toString(options.source); + } + + if (typeof options.tokens === 'boolean' && options.tokens) { + extra.tokens = []; + } + if (typeof options.comment === 'boolean' && options.comment) { + extra.comments = []; + } + if (typeof options.tolerant === 'boolean' && options.tolerant) { + extra.errors = []; + } + if (extra.attachComment) { + extra.range = true; + extra.comments = []; + extra.bottomRightStack = []; + extra.trailingComments = []; + extra.leadingComments = []; + } + } + + try { + program = parseProgram(); + if (typeof extra.comments !== 'undefined') { + program.comments = extra.comments; + } + if (typeof extra.tokens !== 'undefined') { + filterTokenLocation(); + program.tokens = extra.tokens; + } + if (typeof extra.errors !== 'undefined') { + program.errors = extra.errors; + } + } catch (e) { + throw e; + } finally { + extra = {}; + } + + return program; + } + + // Sync with *.json manifests. + exports.version = '2.0.0'; + + exports.tokenize = tokenize; + + exports.parse = parse; + + // Deep copy. + /* istanbul ignore next */ + exports.Syntax = (function () { + var name, types = {}; + + if (typeof Object.create === 'function') { + types = Object.create(null); + } + + for (name in Syntax) { + if (Syntax.hasOwnProperty(name)) { + types[name] = Syntax[name]; + } + } + + if (typeof Object.freeze === 'function') { + Object.freeze(types); + } + + return types; + }()); + +})); +/* vim: set sw=4 ts=4 et tw=80 : */ +"use strict"; +// template to expose escodegen server side +(function() { + // load utils.code + var modules = {}; + var mod = {}; + (function(module) { + /* + Copyright (C) 2013-2014 Yusuke Suzuki + Copyright (C) 2014 Ivan Nikulin + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +(function () { + 'use strict'; + + var Regex, NON_ASCII_WHITESPACES; + + // See `tools/generate-identifier-regex.js`. + Regex = { + NonAsciiIdentifierStart: new RegExp('[\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0\u08A2-\u08AC\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097F\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F0\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191C\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA697\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA793\uA7A0-\uA7AA\uA7F8-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA80-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]'), + NonAsciiIdentifierPart: new RegExp('[\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0300-\u0374\u0376\u0377\u037A-\u037D\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u0483-\u0487\u048A-\u0527\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u05D0-\u05EA\u05F0-\u05F2\u0610-\u061A\u0620-\u0669\u066E-\u06D3\u06D5-\u06DC\u06DF-\u06E8\u06EA-\u06FC\u06FF\u0710-\u074A\u074D-\u07B1\u07C0-\u07F5\u07FA\u0800-\u082D\u0840-\u085B\u08A0\u08A2-\u08AC\u08E4-\u08FE\u0900-\u0963\u0966-\u096F\u0971-\u0977\u0979-\u097F\u0981-\u0983\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BC-\u09C4\u09C7\u09C8\u09CB-\u09CE\u09D7\u09DC\u09DD\u09DF-\u09E3\u09E6-\u09F1\u0A01-\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A59-\u0A5C\u0A5E\u0A66-\u0A75\u0A81-\u0A83\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABC-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AD0\u0AE0-\u0AE3\u0AE6-\u0AEF\u0B01-\u0B03\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3C-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B5C\u0B5D\u0B5F-\u0B63\u0B66-\u0B6F\u0B71\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD0\u0BD7\u0BE6-\u0BEF\u0C01-\u0C03\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3D-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C58\u0C59\u0C60-\u0C63\u0C66-\u0C6F\u0C82\u0C83\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBC-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CDE\u0CE0-\u0CE3\u0CE6-\u0CEF\u0CF1\u0CF2\u0D02\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D-\u0D44\u0D46-\u0D48\u0D4A-\u0D4E\u0D57\u0D60-\u0D63\u0D66-\u0D6F\u0D7A-\u0D7F\u0D82\u0D83\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DF2\u0DF3\u0E01-\u0E3A\u0E40-\u0E4E\u0E50-\u0E59\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB9\u0EBB-\u0EBD\u0EC0-\u0EC4\u0EC6\u0EC8-\u0ECD\u0ED0-\u0ED9\u0EDC-\u0EDF\u0F00\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E-\u0F47\u0F49-\u0F6C\u0F71-\u0F84\u0F86-\u0F97\u0F99-\u0FBC\u0FC6\u1000-\u1049\u1050-\u109D\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u135D-\u135F\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F0\u1700-\u170C\u170E-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176C\u176E-\u1770\u1772\u1773\u1780-\u17D3\u17D7\u17DC\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u1820-\u1877\u1880-\u18AA\u18B0-\u18F5\u1900-\u191C\u1920-\u192B\u1930-\u193B\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19D9\u1A00-\u1A1B\u1A20-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AA7\u1B00-\u1B4B\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1BF3\u1C00-\u1C37\u1C40-\u1C49\u1C4D-\u1C7D\u1CD0-\u1CD2\u1CD4-\u1CF6\u1D00-\u1DE6\u1DFC-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u200C\u200D\u203F\u2040\u2054\u2071\u207F\u2090-\u209C\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D7F-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2DE0-\u2DFF\u2E2F\u3005-\u3007\u3021-\u302F\u3031-\u3035\u3038-\u303C\u3041-\u3096\u3099\u309A\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66F\uA674-\uA67D\uA67F-\uA697\uA69F-\uA6F1\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA793\uA7A0-\uA7AA\uA7F8-\uA827\uA840-\uA873\uA880-\uA8C4\uA8D0-\uA8D9\uA8E0-\uA8F7\uA8FB\uA900-\uA92D\uA930-\uA953\uA960-\uA97C\uA980-\uA9C0\uA9CF-\uA9D9\uAA00-\uAA36\uAA40-\uAA4D\uAA50-\uAA59\uAA60-\uAA76\uAA7A\uAA7B\uAA80-\uAAC2\uAADB-\uAADD\uAAE0-\uAAEF\uAAF2-\uAAF6\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uABC0-\uABEA\uABEC\uABED\uABF0-\uABF9\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE00-\uFE0F\uFE20-\uFE26\uFE33\uFE34\uFE4D-\uFE4F\uFE70-\uFE74\uFE76-\uFEFC\uFF10-\uFF19\uFF21-\uFF3A\uFF3F\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]') + }; + + function isDecimalDigit(ch) { + return (ch >= 48 && ch <= 57); // 0..9 + } + + function isHexDigit(ch) { + return isDecimalDigit(ch) || // 0..9 + (97 <= ch && ch <= 102) || // a..f + (65 <= ch && ch <= 70); // A..F + } + + function isOctalDigit(ch) { + return (ch >= 48 && ch <= 55); // 0..7 + } + + // 7.2 White Space + + NON_ASCII_WHITESPACES = [ + 0x1680, 0x180E, + 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, + 0x202F, 0x205F, + 0x3000, + 0xFEFF + ]; + + function isWhiteSpace(ch) { + return (ch === 0x20) || (ch === 0x09) || (ch === 0x0B) || (ch === 0x0C) || (ch === 0xA0) || + (ch >= 0x1680 && NON_ASCII_WHITESPACES.indexOf(ch) >= 0); + } + + // 7.3 Line Terminators + + function isLineTerminator(ch) { + return (ch === 0x0A) || (ch === 0x0D) || (ch === 0x2028) || (ch === 0x2029); + } + + // 7.6 Identifier Names and Identifiers + + function isIdentifierStart(ch) { + return (ch >= 97 && ch <= 122) || // a..z + (ch >= 65 && ch <= 90) || // A..Z + (ch === 36) || (ch === 95) || // $ (dollar) and _ (underscore) + (ch === 92) || // \ (backslash) + ((ch >= 0x80) && Regex.NonAsciiIdentifierStart.test(String.fromCharCode(ch))); + } + + function isIdentifierPart(ch) { + return (ch >= 97 && ch <= 122) || // a..z + (ch >= 65 && ch <= 90) || // A..Z + (ch >= 48 && ch <= 57) || // 0..9 + (ch === 36) || (ch === 95) || // $ (dollar) and _ (underscore) + (ch === 92) || // \ (backslash) + ((ch >= 0x80) && Regex.NonAsciiIdentifierPart.test(String.fromCharCode(ch))); + } + + module.exports = { + isDecimalDigit: isDecimalDigit, + isHexDigit: isHexDigit, + isOctalDigit: isOctalDigit, + isWhiteSpace: isWhiteSpace, + isLineTerminator: isLineTerminator, + isIdentifierStart: isIdentifierStart, + isIdentifierPart: isIdentifierPart + }; +}()); +/* vim: set sw=4 ts=4 et tw=80 : */ + + })(mod); + modules.esutils = { + code: mod.exports, + }; + + // load estraverse + (function(exports) { + /* + Copyright (C) 2012-2013 Yusuke Suzuki + Copyright (C) 2012 Ariya Hidayat + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/*jslint vars:false, bitwise:true*/ +/*jshint indent:4*/ +/*global exports:true, define:true*/ +(function (root, factory) { + 'use strict'; + + // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js, + // and plain browser loading, + if (typeof define === 'function' && define.amd) { + define(['exports'], factory); + } else if (typeof exports !== 'undefined') { + factory(exports); + } else { + factory((root.estraverse = {})); + } +}(this, function clone(exports) { + 'use strict'; + + var Syntax, + isArray, + VisitorOption, + VisitorKeys, + objectCreate, + objectKeys, + BREAK, + SKIP, + REMOVE; + + function ignoreJSHintError() { } + + isArray = Array.isArray; + if (!isArray) { + isArray = function isArray(array) { + return Object.prototype.toString.call(array) === '[object Array]'; + }; + } + + function deepCopy(obj) { + var ret = {}, key, val; + for (key in obj) { + if (obj.hasOwnProperty(key)) { + val = obj[key]; + if (typeof val === 'object' && val !== null) { + ret[key] = deepCopy(val); + } else { + ret[key] = val; + } + } + } + return ret; + } + + function shallowCopy(obj) { + var ret = {}, key; + for (key in obj) { + if (obj.hasOwnProperty(key)) { + ret[key] = obj[key]; + } + } + return ret; + } + ignoreJSHintError(shallowCopy); + + // based on LLVM libc++ upper_bound / lower_bound + // MIT License + + function upperBound(array, func) { + var diff, len, i, current; + + len = array.length; + i = 0; + + while (len) { + diff = len >>> 1; + current = i + diff; + if (func(array[current])) { + len = diff; + } else { + i = current + 1; + len -= diff + 1; + } + } + return i; + } + + function lowerBound(array, func) { + var diff, len, i, current; + + len = array.length; + i = 0; + + while (len) { + diff = len >>> 1; + current = i + diff; + if (func(array[current])) { + i = current + 1; + len -= diff + 1; + } else { + len = diff; + } + } + return i; + } + ignoreJSHintError(lowerBound); + + objectCreate = Object.create || (function () { + function F() { } + + return function (o) { + F.prototype = o; + return new F(); + }; + })(); + + objectKeys = Object.keys || function (o) { + var keys = [], key; + for (key in o) { + keys.push(key); + } + return keys; + }; + + function extend(to, from) { + objectKeys(from).forEach(function (key) { + to[key] = from[key]; + }); + return to; + } + + Syntax = { + AssignmentExpression: 'AssignmentExpression', + ArrayExpression: 'ArrayExpression', + ArrayPattern: 'ArrayPattern', + ArrowFunctionExpression: 'ArrowFunctionExpression', + AwaitExpression: 'AwaitExpression', // CAUTION: It's deferred to ES7. + BlockStatement: 'BlockStatement', + BinaryExpression: 'BinaryExpression', + BreakStatement: 'BreakStatement', + CallExpression: 'CallExpression', + CatchClause: 'CatchClause', + ClassBody: 'ClassBody', + ClassDeclaration: 'ClassDeclaration', + ClassExpression: 'ClassExpression', + ComprehensionBlock: 'ComprehensionBlock', // CAUTION: It's deferred to ES7. + ComprehensionExpression: 'ComprehensionExpression', // CAUTION: It's deferred to ES7. + ConditionalExpression: 'ConditionalExpression', + ContinueStatement: 'ContinueStatement', + DebuggerStatement: 'DebuggerStatement', + DirectiveStatement: 'DirectiveStatement', + DoWhileStatement: 'DoWhileStatement', + EmptyStatement: 'EmptyStatement', + ExportBatchSpecifier: 'ExportBatchSpecifier', + ExportDeclaration: 'ExportDeclaration', + ExportSpecifier: 'ExportSpecifier', + ExpressionStatement: 'ExpressionStatement', + ForStatement: 'ForStatement', + ForInStatement: 'ForInStatement', + ForOfStatement: 'ForOfStatement', + FunctionDeclaration: 'FunctionDeclaration', + FunctionExpression: 'FunctionExpression', + GeneratorExpression: 'GeneratorExpression', // CAUTION: It's deferred to ES7. + Identifier: 'Identifier', + IfStatement: 'IfStatement', + ImportDeclaration: 'ImportDeclaration', + ImportDefaultSpecifier: 'ImportDefaultSpecifier', + ImportNamespaceSpecifier: 'ImportNamespaceSpecifier', + ImportSpecifier: 'ImportSpecifier', + Literal: 'Literal', + LabeledStatement: 'LabeledStatement', + LogicalExpression: 'LogicalExpression', + MemberExpression: 'MemberExpression', + MethodDefinition: 'MethodDefinition', + ModuleSpecifier: 'ModuleSpecifier', + NewExpression: 'NewExpression', + ObjectExpression: 'ObjectExpression', + ObjectPattern: 'ObjectPattern', + Program: 'Program', + Property: 'Property', + ReturnStatement: 'ReturnStatement', + SequenceExpression: 'SequenceExpression', + SpreadElement: 'SpreadElement', + SwitchStatement: 'SwitchStatement', + SwitchCase: 'SwitchCase', + TaggedTemplateExpression: 'TaggedTemplateExpression', + TemplateElement: 'TemplateElement', + TemplateLiteral: 'TemplateLiteral', + ThisExpression: 'ThisExpression', + ThrowStatement: 'ThrowStatement', + TryStatement: 'TryStatement', + UnaryExpression: 'UnaryExpression', + UpdateExpression: 'UpdateExpression', + VariableDeclaration: 'VariableDeclaration', + VariableDeclarator: 'VariableDeclarator', + WhileStatement: 'WhileStatement', + WithStatement: 'WithStatement', + YieldExpression: 'YieldExpression' + }; + + VisitorKeys = { + AssignmentExpression: ['left', 'right'], + ArrayExpression: ['elements'], + ArrayPattern: ['elements'], + ArrowFunctionExpression: ['params', 'defaults', 'rest', 'body'], + AwaitExpression: ['argument'], // CAUTION: It's deferred to ES7. + BlockStatement: ['body'], + BinaryExpression: ['left', 'right'], + BreakStatement: ['label'], + CallExpression: ['callee', 'arguments'], + CatchClause: ['param', 'body'], + ClassBody: ['body'], + ClassDeclaration: ['id', 'body', 'superClass'], + ClassExpression: ['id', 'body', 'superClass'], + ComprehensionBlock: ['left', 'right'], // CAUTION: It's deferred to ES7. + ComprehensionExpression: ['blocks', 'filter', 'body'], // CAUTION: It's deferred to ES7. + ConditionalExpression: ['test', 'consequent', 'alternate'], + ContinueStatement: ['label'], + DebuggerStatement: [], + DirectiveStatement: [], + DoWhileStatement: ['body', 'test'], + EmptyStatement: [], + ExportBatchSpecifier: [], + ExportDeclaration: ['declaration', 'specifiers', 'source'], + ExportSpecifier: ['id', 'name'], + ExpressionStatement: ['expression'], + ForStatement: ['init', 'test', 'update', 'body'], + ForInStatement: ['left', 'right', 'body'], + ForOfStatement: ['left', 'right', 'body'], + FunctionDeclaration: ['id', 'params', 'defaults', 'rest', 'body'], + FunctionExpression: ['id', 'params', 'defaults', 'rest', 'body'], + GeneratorExpression: ['blocks', 'filter', 'body'], // CAUTION: It's deferred to ES7. + Identifier: [], + IfStatement: ['test', 'consequent', 'alternate'], + ImportDeclaration: ['specifiers', 'source'], + ImportDefaultSpecifier: ['id'], + ImportNamespaceSpecifier: ['id'], + ImportSpecifier: ['id', 'name'], + Literal: [], + LabeledStatement: ['label', 'body'], + LogicalExpression: ['left', 'right'], + MemberExpression: ['object', 'property'], + MethodDefinition: ['key', 'value'], + ModuleSpecifier: [], + NewExpression: ['callee', 'arguments'], + ObjectExpression: ['properties'], + ObjectPattern: ['properties'], + Program: ['body'], + Property: ['key', 'value'], + ReturnStatement: ['argument'], + SequenceExpression: ['expressions'], + SpreadElement: ['argument'], + SwitchStatement: ['discriminant', 'cases'], + SwitchCase: ['test', 'consequent'], + TaggedTemplateExpression: ['tag', 'quasi'], + TemplateElement: [], + TemplateLiteral: ['quasis', 'expressions'], + ThisExpression: [], + ThrowStatement: ['argument'], + TryStatement: ['block', 'handlers', 'handler', 'guardedHandlers', 'finalizer'], + UnaryExpression: ['argument'], + UpdateExpression: ['argument'], + VariableDeclaration: ['declarations'], + VariableDeclarator: ['id', 'init'], + WhileStatement: ['test', 'body'], + WithStatement: ['object', 'body'], + YieldExpression: ['argument'] + }; + + // unique id + BREAK = {}; + SKIP = {}; + REMOVE = {}; + + VisitorOption = { + Break: BREAK, + Skip: SKIP, + Remove: REMOVE + }; + + function Reference(parent, key) { + this.parent = parent; + this.key = key; + } + + Reference.prototype.replace = function replace(node) { + this.parent[this.key] = node; + }; + + Reference.prototype.remove = function remove() { + if (isArray(this.parent)) { + this.parent.splice(this.key, 1); + return true; + } else { + this.replace(null); + return false; + } + }; + + function Element(node, path, wrap, ref) { + this.node = node; + this.path = path; + this.wrap = wrap; + this.ref = ref; + } + + function Controller() { } + + // API: + // return property path array from root to current node + Controller.prototype.path = function path() { + var i, iz, j, jz, result, element; + + function addToPath(result, path) { + if (isArray(path)) { + for (j = 0, jz = path.length; j < jz; ++j) { + result.push(path[j]); + } + } else { + result.push(path); + } + } + + // root node + if (!this.__current.path) { + return null; + } + + // first node is sentinel, second node is root element + result = []; + for (i = 2, iz = this.__leavelist.length; i < iz; ++i) { + element = this.__leavelist[i]; + addToPath(result, element.path); + } + addToPath(result, this.__current.path); + return result; + }; + + // API: + // return type of current node + Controller.prototype.type = function () { + var node = this.current(); + return node.type || this.__current.wrap; + }; + + // API: + // return array of parent elements + Controller.prototype.parents = function parents() { + var i, iz, result; + + // first node is sentinel + result = []; + for (i = 1, iz = this.__leavelist.length; i < iz; ++i) { + result.push(this.__leavelist[i].node); + } + + return result; + }; + + // API: + // return current node + Controller.prototype.current = function current() { + return this.__current.node; + }; + + Controller.prototype.__execute = function __execute(callback, element) { + var previous, result; + + result = undefined; + + previous = this.__current; + this.__current = element; + this.__state = null; + if (callback) { + result = callback.call(this, element.node, this.__leavelist[this.__leavelist.length - 1].node); + } + this.__current = previous; + + return result; + }; + + // API: + // notify control skip / break + Controller.prototype.notify = function notify(flag) { + this.__state = flag; + }; + + // API: + // skip child nodes of current node + Controller.prototype.skip = function () { + this.notify(SKIP); + }; + + // API: + // break traversals + Controller.prototype['break'] = function () { + this.notify(BREAK); + }; + + // API: + // remove node + Controller.prototype.remove = function () { + this.notify(REMOVE); + }; + + Controller.prototype.__initialize = function(root, visitor) { + this.visitor = visitor; + this.root = root; + this.__worklist = []; + this.__leavelist = []; + this.__current = null; + this.__state = null; + this.__fallback = visitor.fallback === 'iteration'; + this.__keys = VisitorKeys; + if (visitor.keys) { + this.__keys = extend(objectCreate(this.__keys), visitor.keys); + } + }; + + function isNode(node) { + if (node == null) { + return false; + } + return typeof node === 'object' && typeof node.type === 'string'; + } + + function isProperty(nodeType, key) { + return (nodeType === Syntax.ObjectExpression || nodeType === Syntax.ObjectPattern) && 'properties' === key; + } + + Controller.prototype.traverse = function traverse(root, visitor) { + var worklist, + leavelist, + element, + node, + nodeType, + ret, + key, + current, + current2, + candidates, + candidate, + sentinel; + + this.__initialize(root, visitor); + + sentinel = {}; + + // reference + worklist = this.__worklist; + leavelist = this.__leavelist; + + // initialize + worklist.push(new Element(root, null, null, null)); + leavelist.push(new Element(null, null, null, null)); + + while (worklist.length) { + element = worklist.pop(); + + if (element === sentinel) { + element = leavelist.pop(); + + ret = this.__execute(visitor.leave, element); + + if (this.__state === BREAK || ret === BREAK) { + return; + } + continue; + } + + if (element.node) { + + ret = this.__execute(visitor.enter, element); + + if (this.__state === BREAK || ret === BREAK) { + return; + } + + worklist.push(sentinel); + leavelist.push(element); + + if (this.__state === SKIP || ret === SKIP) { + continue; + } + + node = element.node; + nodeType = element.wrap || node.type; + candidates = this.__keys[nodeType]; + if (!candidates) { + if (this.__fallback) { + candidates = objectKeys(node); + } else { + throw new Error('Unknown node type ' + nodeType + '.'); + } + } + + current = candidates.length; + while ((current -= 1) >= 0) { + key = candidates[current]; + candidate = node[key]; + if (!candidate) { + continue; + } + + if (isArray(candidate)) { + current2 = candidate.length; + while ((current2 -= 1) >= 0) { + if (!candidate[current2]) { + continue; + } + if (isProperty(nodeType, candidates[current])) { + element = new Element(candidate[current2], [key, current2], 'Property', null); + } else if (isNode(candidate[current2])) { + element = new Element(candidate[current2], [key, current2], null, null); + } else { + continue; + } + worklist.push(element); + } + } else if (isNode(candidate)) { + worklist.push(new Element(candidate, key, null, null)); + } + } + } + } + }; + + Controller.prototype.replace = function replace(root, visitor) { + function removeElem(element) { + var i, + key, + nextElem, + parent; + + if (element.ref.remove()) { + // When the reference is an element of an array. + key = element.ref.key; + parent = element.ref.parent; + + // If removed from array, then decrease following items' keys. + i = worklist.length; + while (i--) { + nextElem = worklist[i]; + if (nextElem.ref && nextElem.ref.parent === parent) { + if (nextElem.ref.key < key) { + break; + } + --nextElem.ref.key; + } + } + } + } + + var worklist, + leavelist, + node, + nodeType, + target, + element, + current, + current2, + candidates, + candidate, + sentinel, + outer, + key; + + this.__initialize(root, visitor); + + sentinel = {}; + + // reference + worklist = this.__worklist; + leavelist = this.__leavelist; + + // initialize + outer = { + root: root + }; + element = new Element(root, null, null, new Reference(outer, 'root')); + worklist.push(element); + leavelist.push(element); + + while (worklist.length) { + element = worklist.pop(); + + if (element === sentinel) { + element = leavelist.pop(); + + target = this.__execute(visitor.leave, element); + + // node may be replaced with null, + // so distinguish between undefined and null in this place + if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) { + // replace + element.ref.replace(target); + } + + if (this.__state === REMOVE || target === REMOVE) { + removeElem(element); + } + + if (this.__state === BREAK || target === BREAK) { + return outer.root; + } + continue; + } + + target = this.__execute(visitor.enter, element); + + // node may be replaced with null, + // so distinguish between undefined and null in this place + if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) { + // replace + element.ref.replace(target); + element.node = target; + } + + if (this.__state === REMOVE || target === REMOVE) { + removeElem(element); + element.node = null; + } + + if (this.__state === BREAK || target === BREAK) { + return outer.root; + } + + // node may be null + node = element.node; + if (!node) { + continue; + } + + worklist.push(sentinel); + leavelist.push(element); + + if (this.__state === SKIP || target === SKIP) { + continue; + } + + nodeType = element.wrap || node.type; + candidates = this.__keys[nodeType]; + if (!candidates) { + if (this.__fallback) { + candidates = objectKeys(node); + } else { + throw new Error('Unknown node type ' + nodeType + '.'); + } + } + + current = candidates.length; + while ((current -= 1) >= 0) { + key = candidates[current]; + candidate = node[key]; + if (!candidate) { + continue; + } + + if (isArray(candidate)) { + current2 = candidate.length; + while ((current2 -= 1) >= 0) { + if (!candidate[current2]) { + continue; + } + if (isProperty(nodeType, candidates[current])) { + element = new Element(candidate[current2], [key, current2], 'Property', new Reference(candidate, current2)); + } else if (isNode(candidate[current2])) { + element = new Element(candidate[current2], [key, current2], null, new Reference(candidate, current2)); + } else { + continue; + } + worklist.push(element); + } + } else if (isNode(candidate)) { + worklist.push(new Element(candidate, key, null, new Reference(node, key))); + } + } + } + + return outer.root; + }; + + function traverse(root, visitor) { + var controller = new Controller(); + return controller.traverse(root, visitor); + } + + function replace(root, visitor) { + var controller = new Controller(); + return controller.replace(root, visitor); + } + + function extendCommentRange(comment, tokens) { + var target; + + target = upperBound(tokens, function search(token) { + return token.range[0] > comment.range[0]; + }); + + comment.extendedRange = [comment.range[0], comment.range[1]]; + + if (target !== tokens.length) { + comment.extendedRange[1] = tokens[target].range[0]; + } + + target -= 1; + if (target >= 0) { + comment.extendedRange[0] = tokens[target].range[1]; + } + + return comment; + } + + function attachComments(tree, providedComments, tokens) { + // At first, we should calculate extended comment ranges. + var comments = [], comment, len, i, cursor; + + if (!tree.range) { + throw new Error('attachComments needs range information'); + } + + // tokens array is empty, we attach comments to tree as 'leadingComments' + if (!tokens.length) { + if (providedComments.length) { + for (i = 0, len = providedComments.length; i < len; i += 1) { + comment = deepCopy(providedComments[i]); + comment.extendedRange = [0, tree.range[0]]; + comments.push(comment); + } + tree.leadingComments = comments; + } + return tree; + } + + for (i = 0, len = providedComments.length; i < len; i += 1) { + comments.push(extendCommentRange(deepCopy(providedComments[i]), tokens)); + } + + // This is based on John Freeman's implementation. + cursor = 0; + traverse(tree, { + enter: function (node) { + var comment; + + while (cursor < comments.length) { + comment = comments[cursor]; + if (comment.extendedRange[1] > node.range[0]) { + break; + } + + if (comment.extendedRange[1] === node.range[0]) { + if (!node.leadingComments) { + node.leadingComments = []; + } + node.leadingComments.push(comment); + comments.splice(cursor, 1); + } else { + cursor += 1; + } + } + + // already out of owned node + if (cursor === comments.length) { + return VisitorOption.Break; + } + + if (comments[cursor].extendedRange[0] > node.range[1]) { + return VisitorOption.Skip; + } + } + }); + + cursor = 0; + traverse(tree, { + leave: function (node) { + var comment; + + while (cursor < comments.length) { + comment = comments[cursor]; + if (node.range[1] < comment.extendedRange[0]) { + break; + } + + if (node.range[1] === comment.extendedRange[0]) { + if (!node.trailingComments) { + node.trailingComments = []; + } + node.trailingComments.push(comment); + comments.splice(cursor, 1); + } else { + cursor += 1; + } + } + + // already out of owned node + if (cursor === comments.length) { + return VisitorOption.Break; + } + + if (comments[cursor].extendedRange[0] > node.range[1]) { + return VisitorOption.Skip; + } + } + }); + + return tree; + } + + exports.version = '1.8.1-dev'; + exports.Syntax = Syntax; + exports.traverse = traverse; + exports.replace = replace; + exports.attachComments = attachComments; + exports.VisitorKeys = VisitorKeys; + exports.VisitorOption = VisitorOption; + exports.Controller = Controller; + exports.cloneEnvironment = function () { return clone({}); }; + + return exports; +})); +/* vim: set sw=4 ts=4 et tw=80 : */ + + })(modules.estraverse = {}); + + modules['./package.json'] = { + "name": "streamline", + "description": "Asynchronous Javascript for dummies", + "version": "0.10.17", + "homepage": "http://github.com/Sage/streamlinejs", + "repository": { + "type": "git", + "url": "git://github.com/Sage/streamlinejs.git" + }, + "engines": { + "node": ">=0.10.0" + }, + "dependencies": { + "esprima": "^2.0.0", + "escodegen": "^1.6.1", + "source-map": "~0.1.43" + }, + "optionalDependencies": { + "fibers": "^1.0.1", + "galaxy": "^0.1.11" + }, + "author": "Bruno Jouhier", + "directories": { + "lib": "./lib", + "bin": "./bin" + }, + "main": "index.js" +} +; + + (function(require, exports) { + /* + Copyright (C) 2012-2014 Yusuke Suzuki + Copyright (C) 2015 Ingvar Stepanyan + Copyright (C) 2014 Ivan Nikulin + Copyright (C) 2012-2013 Michael Ficarra + Copyright (C) 2012-2013 Mathias Bynens + Copyright (C) 2013 Irakli Gozalishvili + Copyright (C) 2012 Robert Gust-Bardon + Copyright (C) 2012 John Freeman + Copyright (C) 2011-2012 Ariya Hidayat + Copyright (C) 2012 Joost-Wim Boekesteijn + Copyright (C) 2012 Kris Kowal + Copyright (C) 2012 Arpad Borsos + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/*global exports:true, require:true, global:true*/ +(function () { + 'use strict'; + + var Syntax, + Precedence, + BinaryPrecedence, + SourceNode, + estraverse, + esutils, + isArray, + base, + indent, + json, + renumber, + hexadecimal, + quotes, + escapeless, + newline, + space, + parentheses, + semicolons, + safeConcatenation, + directive, + extra, + parse, + sourceMap, + sourceCode, + preserveBlankLines, + FORMAT_MINIFY, + FORMAT_DEFAULTS; + + estraverse = require('estraverse'); + esutils = require('esutils'); + + Syntax = estraverse.Syntax; + + // Generation is done by generateExpression. + function isExpression(node) { + return CodeGenerator.Expression.hasOwnProperty(node.type); + } + + // Generation is done by generateStatement. + function isStatement(node) { + return CodeGenerator.Statement.hasOwnProperty(node.type); + } + + Precedence = { + Sequence: 0, + Yield: 1, + Await: 1, + Assignment: 1, + Conditional: 2, + ArrowFunction: 2, + LogicalOR: 3, + LogicalAND: 4, + BitwiseOR: 5, + BitwiseXOR: 6, + BitwiseAND: 7, + Equality: 8, + Relational: 9, + BitwiseSHIFT: 10, + Additive: 11, + Multiplicative: 12, + Unary: 13, + Postfix: 14, + Call: 15, + New: 16, + TaggedTemplate: 17, + Member: 18, + Primary: 19 + }; + + BinaryPrecedence = { + '||': Precedence.LogicalOR, + '&&': Precedence.LogicalAND, + '|': Precedence.BitwiseOR, + '^': Precedence.BitwiseXOR, + '&': Precedence.BitwiseAND, + '==': Precedence.Equality, + '!=': Precedence.Equality, + '===': Precedence.Equality, + '!==': Precedence.Equality, + 'is': Precedence.Equality, + 'isnt': Precedence.Equality, + '<': Precedence.Relational, + '>': Precedence.Relational, + '<=': Precedence.Relational, + '>=': Precedence.Relational, + 'in': Precedence.Relational, + 'instanceof': Precedence.Relational, + '<<': Precedence.BitwiseSHIFT, + '>>': Precedence.BitwiseSHIFT, + '>>>': Precedence.BitwiseSHIFT, + '+': Precedence.Additive, + '-': Precedence.Additive, + '*': Precedence.Multiplicative, + '%': Precedence.Multiplicative, + '/': Precedence.Multiplicative + }; + + //Flags + var F_ALLOW_IN = 1, + F_ALLOW_CALL = 1 << 1, + F_ALLOW_UNPARATH_NEW = 1 << 2, + F_FUNC_BODY = 1 << 3, + F_DIRECTIVE_CTX = 1 << 4, + F_SEMICOLON_OPT = 1 << 5; + + //Expression flag sets + //NOTE: Flag order: + // F_ALLOW_IN + // F_ALLOW_CALL + // F_ALLOW_UNPARATH_NEW + var E_FTT = F_ALLOW_CALL | F_ALLOW_UNPARATH_NEW, + E_TTF = F_ALLOW_IN | F_ALLOW_CALL, + E_TTT = F_ALLOW_IN | F_ALLOW_CALL | F_ALLOW_UNPARATH_NEW, + E_TFF = F_ALLOW_IN, + E_FFT = F_ALLOW_UNPARATH_NEW, + E_TFT = F_ALLOW_IN | F_ALLOW_UNPARATH_NEW; + + //Statement flag sets + //NOTE: Flag order: + // F_ALLOW_IN + // F_FUNC_BODY + // F_DIRECTIVE_CTX + // F_SEMICOLON_OPT + var S_TFFF = F_ALLOW_IN, + S_TFFT = F_ALLOW_IN | F_SEMICOLON_OPT, + S_FFFF = 0x00, + S_TFTF = F_ALLOW_IN | F_DIRECTIVE_CTX, + S_TTFF = F_ALLOW_IN | F_FUNC_BODY; + + function getDefaultOptions() { + // default options + return { + indent: null, + base: null, + parse: null, + comment: false, + format: { + indent: { + style: ' ', + base: 0, + adjustMultilineComment: false + }, + newline: '\n', + space: ' ', + json: false, + renumber: false, + hexadecimal: false, + quotes: 'single', + escapeless: false, + compact: false, + parentheses: true, + semicolons: true, + safeConcatenation: false, + preserveBlankLines: false + }, + moz: { + comprehensionExpressionStartsWithAssignment: false, + starlessGenerator: false + }, + sourceMap: null, + sourceMapRoot: null, + sourceMapWithCode: false, + directive: false, + raw: true, + verbatim: null, + sourceCode: null + }; + } + + function stringRepeat(str, num) { + var result = ''; + + for (num |= 0; num > 0; num >>>= 1, str += str) { + if (num & 1) { + result += str; + } + } + + return result; + } + + isArray = Array.isArray; + if (!isArray) { + isArray = function isArray(array) { + return Object.prototype.toString.call(array) === '[object Array]'; + }; + } + + function hasLineTerminator(str) { + return (/[\r\n]/g).test(str); + } + + function endsWithLineTerminator(str) { + var len = str.length; + return len && esutils.code.isLineTerminator(str.charCodeAt(len - 1)); + } + + function merge(target, override) { + var key; + for (key in override) { + if (override.hasOwnProperty(key)) { + target[key] = override[key]; + } + } + return target; + } + + function updateDeeply(target, override) { + var key, val; + + function isHashObject(target) { + return typeof target === 'object' && target instanceof Object && !(target instanceof RegExp); + } + + for (key in override) { + if (override.hasOwnProperty(key)) { + val = override[key]; + if (isHashObject(val)) { + if (isHashObject(target[key])) { + updateDeeply(target[key], val); + } else { + target[key] = updateDeeply({}, val); + } + } else { + target[key] = val; + } + } + } + return target; + } + + function generateNumber(value) { + var result, point, temp, exponent, pos; + + if (value !== value) { + throw new Error('Numeric literal whose value is NaN'); + } + if (value < 0 || (value === 0 && 1 / value < 0)) { + throw new Error('Numeric literal whose value is negative'); + } + + if (value === 1 / 0) { + return json ? 'null' : renumber ? '1e400' : '1e+400'; + } + + result = '' + value; + if (!renumber || result.length < 3) { + return result; + } + + point = result.indexOf('.'); + if (!json && result.charCodeAt(0) === 0x30 /* 0 */ && point === 1) { + point = 0; + result = result.slice(1); + } + temp = result; + result = result.replace('e+', 'e'); + exponent = 0; + if ((pos = temp.indexOf('e')) > 0) { + exponent = +temp.slice(pos + 1); + temp = temp.slice(0, pos); + } + if (point >= 0) { + exponent -= temp.length - point - 1; + temp = +(temp.slice(0, point) + temp.slice(point + 1)) + ''; + } + pos = 0; + while (temp.charCodeAt(temp.length + pos - 1) === 0x30 /* 0 */) { + --pos; + } + if (pos !== 0) { + exponent -= pos; + temp = temp.slice(0, pos); + } + if (exponent !== 0) { + temp += 'e' + exponent; + } + if ((temp.length < result.length || + (hexadecimal && value > 1e12 && Math.floor(value) === value && (temp = '0x' + value.toString(16)).length < result.length)) && + +temp === value) { + result = temp; + } + + return result; + } + + // Generate valid RegExp expression. + // This function is based on https://github.com/Constellation/iv Engine + + function escapeRegExpCharacter(ch, previousIsBackslash) { + // not handling '\' and handling \u2028 or \u2029 to unicode escape sequence + if ((ch & ~1) === 0x2028) { + return (previousIsBackslash ? 'u' : '\\u') + ((ch === 0x2028) ? '2028' : '2029'); + } else if (ch === 10 || ch === 13) { // \n, \r + return (previousIsBackslash ? '' : '\\') + ((ch === 10) ? 'n' : 'r'); + } + return String.fromCharCode(ch); + } + + function generateRegExp(reg) { + var match, result, flags, i, iz, ch, characterInBrack, previousIsBackslash; + + result = reg.toString(); + + if (reg.source) { + // extract flag from toString result + match = result.match(/\/([^/]*)$/); + if (!match) { + return result; + } + + flags = match[1]; + result = ''; + + characterInBrack = false; + previousIsBackslash = false; + for (i = 0, iz = reg.source.length; i < iz; ++i) { + ch = reg.source.charCodeAt(i); + + if (!previousIsBackslash) { + if (characterInBrack) { + if (ch === 93) { // ] + characterInBrack = false; + } + } else { + if (ch === 47) { // / + result += '\\'; + } else if (ch === 91) { // [ + characterInBrack = true; + } + } + result += escapeRegExpCharacter(ch, previousIsBackslash); + previousIsBackslash = ch === 92; // \ + } else { + // if new RegExp("\\\n') is provided, create /\n/ + result += escapeRegExpCharacter(ch, previousIsBackslash); + // prevent like /\\[/]/ + previousIsBackslash = false; + } + } + + return '/' + result + '/' + flags; + } + + return result; + } + + function escapeAllowedCharacter(code, next) { + var hex; + + if (code === 0x08 /* \b */) { + return '\\b'; + } + + if (code === 0x0C /* \f */) { + return '\\f'; + } + + if (code === 0x09 /* \t */) { + return '\\t'; + } + + hex = code.toString(16).toUpperCase(); + if (json || code > 0xFF) { + return '\\u' + '0000'.slice(hex.length) + hex; + } else if (code === 0x0000 && !esutils.code.isDecimalDigit(next)) { + return '\\0'; + } else if (code === 0x000B /* \v */) { // '\v' + return '\\x0B'; + } else { + return '\\x' + '00'.slice(hex.length) + hex; + } + } + + function escapeDisallowedCharacter(code) { + if (code === 0x5C /* \ */) { + return '\\\\'; + } + + if (code === 0x0A /* \n */) { + return '\\n'; + } + + if (code === 0x0D /* \r */) { + return '\\r'; + } + + if (code === 0x2028) { + return '\\u2028'; + } + + if (code === 0x2029) { + return '\\u2029'; + } + + throw new Error('Incorrectly classified character'); + } + + function escapeDirective(str) { + var i, iz, code, quote; + + quote = quotes === 'double' ? '"' : '\''; + for (i = 0, iz = str.length; i < iz; ++i) { + code = str.charCodeAt(i); + if (code === 0x27 /* ' */) { + quote = '"'; + break; + } else if (code === 0x22 /* " */) { + quote = '\''; + break; + } else if (code === 0x5C /* \ */) { + ++i; + } + } + + return quote + str + quote; + } + + function escapeString(str) { + var result = '', i, len, code, singleQuotes = 0, doubleQuotes = 0, single, quote; + + for (i = 0, len = str.length; i < len; ++i) { + code = str.charCodeAt(i); + if (code === 0x27 /* ' */) { + ++singleQuotes; + } else if (code === 0x22 /* " */) { + ++doubleQuotes; + } else if (code === 0x2F /* / */ && json) { + result += '\\'; + } else if (esutils.code.isLineTerminator(code) || code === 0x5C /* \ */) { + result += escapeDisallowedCharacter(code); + continue; + } else if ((json && code < 0x20 /* SP */) || !(json || escapeless || (code >= 0x20 /* SP */ && code <= 0x7E /* ~ */))) { + result += escapeAllowedCharacter(code, str.charCodeAt(i + 1)); + continue; + } + result += String.fromCharCode(code); + } + + single = !(quotes === 'double' || (quotes === 'auto' && doubleQuotes < singleQuotes)); + quote = single ? '\'' : '"'; + + if (!(single ? singleQuotes : doubleQuotes)) { + return quote + result + quote; + } + + str = result; + result = quote; + + for (i = 0, len = str.length; i < len; ++i) { + code = str.charCodeAt(i); + if ((code === 0x27 /* ' */ && single) || (code === 0x22 /* " */ && !single)) { + result += '\\'; + } + result += String.fromCharCode(code); + } + + return result + quote; + } + + /** + * flatten an array to a string, where the array can contain + * either strings or nested arrays + */ + function flattenToString(arr) { + var i, iz, elem, result = ''; + for (i = 0, iz = arr.length; i < iz; ++i) { + elem = arr[i]; + result += isArray(elem) ? flattenToString(elem) : elem; + } + return result; + } + + /** + * convert generated to a SourceNode when source maps are enabled. + */ + function toSourceNodeWhenNeeded(generated, node) { + if (!sourceMap) { + // with no source maps, generated is either an + // array or a string. if an array, flatten it. + // if a string, just return it + if (isArray(generated)) { + return flattenToString(generated); + } else { + return generated; + } + } + if (node == null) { + if (generated instanceof SourceNode) { + return generated; + } else { + node = {}; + } + } + if (node.loc == null) { + return new SourceNode(null, null, sourceMap, generated, node.name || null); + } + return new SourceNode(node.loc.start.line, node.loc.start.column, (sourceMap === true ? node.loc.source || null : sourceMap), generated, node.name || null); + } + + function noEmptySpace() { + return (space) ? space : ' '; + } + + function join(left, right) { + var leftSource, + rightSource, + leftCharCode, + rightCharCode; + + leftSource = toSourceNodeWhenNeeded(left).toString(); + if (leftSource.length === 0) { + return [right]; + } + + rightSource = toSourceNodeWhenNeeded(right).toString(); + if (rightSource.length === 0) { + return [left]; + } + + leftCharCode = leftSource.charCodeAt(leftSource.length - 1); + rightCharCode = rightSource.charCodeAt(0); + + if ((leftCharCode === 0x2B /* + */ || leftCharCode === 0x2D /* - */) && leftCharCode === rightCharCode || + esutils.code.isIdentifierPart(leftCharCode) && esutils.code.isIdentifierPart(rightCharCode) || + leftCharCode === 0x2F /* / */ && rightCharCode === 0x69 /* i */) { // infix word operators all start with `i` + return [left, noEmptySpace(), right]; + } else if (esutils.code.isWhiteSpace(leftCharCode) || esutils.code.isLineTerminator(leftCharCode) || + esutils.code.isWhiteSpace(rightCharCode) || esutils.code.isLineTerminator(rightCharCode)) { + return [left, right]; + } + return [left, space, right]; + } + + function addIndent(stmt) { + return [base, stmt]; + } + + function withIndent(fn) { + var previousBase; + previousBase = base; + base += indent; + fn(base); + base = previousBase; + } + + function calculateSpaces(str) { + var i; + for (i = str.length - 1; i >= 0; --i) { + if (esutils.code.isLineTerminator(str.charCodeAt(i))) { + break; + } + } + return (str.length - 1) - i; + } + + function adjustMultilineComment(value, specialBase) { + var array, i, len, line, j, spaces, previousBase, sn; + + array = value.split(/\r\n|[\r\n]/); + spaces = Number.MAX_VALUE; + + // first line doesn't have indentation + for (i = 1, len = array.length; i < len; ++i) { + line = array[i]; + j = 0; + while (j < line.length && esutils.code.isWhiteSpace(line.charCodeAt(j))) { + ++j; + } + if (spaces > j) { + spaces = j; + } + } + + if (typeof specialBase !== 'undefined') { + // pattern like + // { + // var t = 20; /* + // * this is comment + // */ + // } + previousBase = base; + if (array[1][spaces] === '*') { + specialBase += ' '; + } + base = specialBase; + } else { + if (spaces & 1) { + // /* + // * + // */ + // If spaces are odd number, above pattern is considered. + // We waste 1 space. + --spaces; + } + previousBase = base; + } + + for (i = 1, len = array.length; i < len; ++i) { + sn = toSourceNodeWhenNeeded(addIndent(array[i].slice(spaces))); + array[i] = sourceMap ? sn.join('') : sn; + } + + base = previousBase; + + return array.join('\n'); + } + + function generateComment(comment, specialBase) { + if (comment.type === 'Line') { + if (endsWithLineTerminator(comment.value)) { + return '//' + comment.value; + } else { + // Always use LineTerminator + var result = '//' + comment.value; + if (!preserveBlankLines) { + result += '\n'; + } + return result; + } + } + if (extra.format.indent.adjustMultilineComment && /[\n\r]/.test(comment.value)) { + return adjustMultilineComment('/*' + comment.value + '*/', specialBase); + } + return '/*' + comment.value + '*/'; + } + + function addComments(stmt, result) { + var i, len, comment, save, tailingToStatement, specialBase, fragment, + extRange, range, prevRange, prefix, infix, suffix, count; + + if (stmt.leadingComments && stmt.leadingComments.length > 0) { + save = result; + + if (preserveBlankLines) { + comment = stmt.leadingComments[0]; + result = []; + + extRange = comment.extendedRange; + range = comment.range; + + prefix = sourceCode.substring(extRange[0], range[0]); + count = (prefix.match(/\n/g) || []).length; + if (count > 0) { + result.push(stringRepeat('\n', count)); + result.push(addIndent(generateComment(comment))); + } else { + result.push(prefix); + result.push(generateComment(comment)); + } + + prevRange = range; + + for (i = 1, len = stmt.leadingComments.length; i < len; i++) { + comment = stmt.leadingComments[i]; + range = comment.range; + + infix = sourceCode.substring(prevRange[1], range[0]); + count = (infix.match(/\n/g) || []).length; + result.push(stringRepeat('\n', count)); + result.push(addIndent(generateComment(comment))); + + prevRange = range; + } + + suffix = sourceCode.substring(range[1], extRange[1]); + count = (suffix.match(/\n/g) || []).length; + result.push(stringRepeat('\n', count)); + } else { + comment = stmt.leadingComments[0]; + result = []; + if (safeConcatenation && stmt.type === Syntax.Program && stmt.body.length === 0) { + result.push('\n'); + } + result.push(generateComment(comment)); + if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) { + result.push('\n'); + } + + for (i = 1, len = stmt.leadingComments.length; i < len; ++i) { + comment = stmt.leadingComments[i]; + fragment = [generateComment(comment)]; + if (!endsWithLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) { + fragment.push('\n'); + } + result.push(addIndent(fragment)); + } + } + + result.push(addIndent(save)); + } + + if (stmt.trailingComments) { + + if (preserveBlankLines) { + comment = stmt.trailingComments[0]; + extRange = comment.extendedRange; + range = comment.range; + + prefix = sourceCode.substring(extRange[0], range[0]); + count = (prefix.match(/\n/g) || []).length; + + if (count > 0) { + result.push(stringRepeat('\n', count)); + result.push(addIndent(generateComment(comment))); + } else { + result.push(prefix); + result.push(generateComment(comment)); + } + } else { + tailingToStatement = !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString()); + specialBase = stringRepeat(' ', calculateSpaces(toSourceNodeWhenNeeded([base, result, indent]).toString())); + for (i = 0, len = stmt.trailingComments.length; i < len; ++i) { + comment = stmt.trailingComments[i]; + if (tailingToStatement) { + // We assume target like following script + // + // var t = 20; /** + // * This is comment of t + // */ + if (i === 0) { + // first case + result = [result, indent]; + } else { + result = [result, specialBase]; + } + result.push(generateComment(comment, specialBase)); + } else { + result = [result, addIndent(generateComment(comment))]; + } + if (i !== len - 1 && !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) { + result = [result, '\n']; + } + } + } + } + + return result; + } + + function generateBlankLines(start, end, result) { + var j, newlineCount = 0; + + for (j = start; j < end; j++) { + if (sourceCode[j] === '\n') { + newlineCount++; + } + } + + for (j = 1; j < newlineCount; j++) { + result.push(newline); + } + } + + function parenthesize(text, current, should) { + if (current < should) { + return ['(', text, ')']; + } + return text; + } + + function generateVerbatimString(string) { + var i, iz, result; + result = string.split(/\r\n|\n/); + for (i = 1, iz = result.length; i < iz; i++) { + result[i] = newline + base + result[i]; + } + return result; + } + + function generateVerbatim(expr, precedence) { + var verbatim, result, prec; + verbatim = expr[extra.verbatim]; + + if (typeof verbatim === 'string') { + result = parenthesize(generateVerbatimString(verbatim), Precedence.Sequence, precedence); + } else { + // verbatim is object + result = generateVerbatimString(verbatim.content); + prec = (verbatim.precedence != null) ? verbatim.precedence : Precedence.Sequence; + result = parenthesize(result, prec, precedence); + } + + return toSourceNodeWhenNeeded(result, expr); + } + + function CodeGenerator() { + } + + // Helpers. + + CodeGenerator.prototype.maybeBlock = function(stmt, flags) { + var result, noLeadingComment, that = this; + + noLeadingComment = !extra.comment || !stmt.leadingComments; + + if (stmt.type === Syntax.BlockStatement && noLeadingComment) { + return [space, this.generateStatement(stmt, flags)]; + } + + if (stmt.type === Syntax.EmptyStatement && noLeadingComment) { + return ';'; + } + + withIndent(function () { + result = [ + newline, + addIndent(that.generateStatement(stmt, flags)) + ]; + }); + + return result; + }; + + CodeGenerator.prototype.maybeBlockSuffix = function (stmt, result) { + var ends = endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString()); + if (stmt.type === Syntax.BlockStatement && (!extra.comment || !stmt.leadingComments) && !ends) { + return [result, space]; + } + if (ends) { + return [result, base]; + } + return [result, newline, base]; + }; + + function generateIdentifier(node) { + return toSourceNodeWhenNeeded(node.name, node); + } + + function generateAsyncPrefix(node, spaceRequired) { + return node.async ? 'async' + (spaceRequired ? noEmptySpace() : space) : ''; + } + + function generateStarSuffix(node) { + var isGenerator = node.generator && !extra.moz.starlessGenerator; + return isGenerator ? '*' + space : ''; + } + + function generateMethodPrefix(prop) { + var func = prop.value; + if (func.async) { + return generateAsyncPrefix(func, !prop.computed); + } else { + // avoid space before method name + return generateStarSuffix(func) ? '*' : ''; + } + } + + CodeGenerator.prototype.generatePattern = function (node, precedence, flags) { + if (node.type === Syntax.Identifier) { + return generateIdentifier(node); + } + return this.generateExpression(node, precedence, flags); + }; + + CodeGenerator.prototype.generateFunctionParams = function (node) { + var i, iz, result, hasDefault; + + hasDefault = false; + + if (node.type === Syntax.ArrowFunctionExpression && + !node.rest && (!node.defaults || node.defaults.length === 0) && + node.params.length === 1 && node.params[0].type === Syntax.Identifier) { + // arg => { } case + result = [generateAsyncPrefix(node, true), generateIdentifier(node.params[0])]; + } else { + result = node.type === Syntax.ArrowFunctionExpression ? [generateAsyncPrefix(node, false)] : []; + result.push('('); + if (node.defaults) { + hasDefault = true; + } + for (i = 0, iz = node.params.length; i < iz; ++i) { + if (hasDefault && node.defaults[i]) { + // Handle default values. + result.push(this.generateAssignment(node.params[i], node.defaults[i], '=', Precedence.Assignment, E_TTT)); + } else { + result.push(this.generatePattern(node.params[i], Precedence.Assignment, E_TTT)); + } + if (i + 1 < iz) { + result.push(',' + space); + } + } + + if (node.rest) { + if (node.params.length) { + result.push(',' + space); + } + result.push('...'); + result.push(generateIdentifier(node.rest)); + } + + result.push(')'); + } + + return result; + }; + + CodeGenerator.prototype.generateFunctionBody = function (node) { + var result, expr; + + result = this.generateFunctionParams(node); + + if (node.type === Syntax.ArrowFunctionExpression) { + result.push(space); + result.push('=>'); + } + + if (node.expression) { + result.push(space); + expr = this.generateExpression(node.body, Precedence.Assignment, E_TTT); + if (expr.toString().charAt(0) === '{') { + expr = ['(', expr, ')']; + } + result.push(expr); + } else { + result.push(this.maybeBlock(node.body, S_TTFF)); + } + + return result; + }; + + CodeGenerator.prototype.generateIterationForStatement = function (operator, stmt, flags) { + var result = ['for' + space + '('], that = this; + withIndent(function () { + if (stmt.left.type === Syntax.VariableDeclaration) { + withIndent(function () { + result.push(stmt.left.kind + noEmptySpace()); + result.push(that.generateStatement(stmt.left.declarations[0], S_FFFF)); + }); + } else { + result.push(that.generateExpression(stmt.left, Precedence.Call, E_TTT)); + } + + result = join(result, operator); + result = [join( + result, + that.generateExpression(stmt.right, Precedence.Sequence, E_TTT) + ), ')']; + }); + result.push(this.maybeBlock(stmt.body, flags)); + return result; + }; + + CodeGenerator.prototype.generatePropertyKey = function (expr, computed) { + var result = []; + + if (computed) { + result.push('['); + } + + result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT)); + if (computed) { + result.push(']'); + } + + return result; + }; + + CodeGenerator.prototype.generateAssignment = function (left, right, operator, precedence, flags) { + if (Precedence.Assignment < precedence) { + flags |= F_ALLOW_IN; + } + + return parenthesize( + [ + this.generateExpression(left, Precedence.Call, flags), + space + operator + space, + this.generateExpression(right, Precedence.Assignment, flags) + ], + Precedence.Assignment, + precedence + ); + }; + + CodeGenerator.prototype.semicolon = function (flags) { + if (!semicolons && flags & F_SEMICOLON_OPT) { + return ''; + } + return ';'; + }; + + // Statements. + + CodeGenerator.Statement = { + + BlockStatement: function (stmt, flags) { + var range, content, result = ['{', newline], that = this; + + withIndent(function () { + // handle functions without any code + if (stmt.body.length === 0 && preserveBlankLines) { + range = stmt.range; + if (range[1] - range[0] > 2) { + content = sourceCode.substring(range[0] + 1, range[1] - 1); + if (content[0] === '\n') { + result = ['{']; + } + result.push(content); + } + } + + var i, iz, fragment, bodyFlags; + bodyFlags = S_TFFF; + if (flags & F_FUNC_BODY) { + bodyFlags |= F_DIRECTIVE_CTX; + } + + for (i = 0, iz = stmt.body.length; i < iz; ++i) { + if (preserveBlankLines) { + // handle spaces before the first line + if (i === 0) { + if (stmt.body[0].leadingComments) { + range = stmt.body[0].leadingComments[0].extendedRange; + content = sourceCode.substring(range[0], range[1]); + if (content[0] === '\n') { + result = ['{']; + } + } + if (!stmt.body[0].leadingComments) { + generateBlankLines(stmt.range[0], stmt.body[0].range[0], result); + } + } + + // handle spaces between lines + if (i > 0) { + if (!stmt.body[i - 1].trailingComments && !stmt.body[i].leadingComments) { + generateBlankLines(stmt.body[i - 1].range[1], stmt.body[i].range[0], result); + } + } + } + + if (i === iz - 1) { + bodyFlags |= F_SEMICOLON_OPT; + } + + if (stmt.body[i].leadingComments && preserveBlankLines) { + fragment = that.generateStatement(stmt.body[i], bodyFlags); + } else { + fragment = addIndent(that.generateStatement(stmt.body[i], bodyFlags)); + } + + result.push(fragment); + if (!endsWithLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) { + if (preserveBlankLines && i < iz - 1) { + // don't add a new line if there are leading coments + // in the next statement + if (!stmt.body[i + 1].leadingComments) { + result.push(newline); + } + } else { + result.push(newline); + } + } + + if (preserveBlankLines) { + // handle spaces after the last line + if (i === iz - 1) { + if (!stmt.body[i].trailingComments) { + generateBlankLines(stmt.body[i].range[1], stmt.range[1], result); + } + } + } + } + }); + + result.push(addIndent('}')); + return result; + }, + + BreakStatement: function (stmt, flags) { + if (stmt.label) { + return 'break ' + stmt.label.name + this.semicolon(flags); + } + return 'break' + this.semicolon(flags); + }, + + ContinueStatement: function (stmt, flags) { + if (stmt.label) { + return 'continue ' + stmt.label.name + this.semicolon(flags); + } + return 'continue' + this.semicolon(flags); + }, + + ClassBody: function (stmt, flags) { + var result = [ '{', newline], that = this; + + withIndent(function (indent) { + var i, iz; + + for (i = 0, iz = stmt.body.length; i < iz; ++i) { + result.push(indent); + result.push(that.generateExpression(stmt.body[i], Precedence.Sequence, E_TTT)); + if (i + 1 < iz) { + result.push(newline); + } + } + }); + + if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) { + result.push(newline); + } + result.push(base); + result.push('}'); + return result; + }, + + ClassDeclaration: function (stmt, flags) { + var result, fragment; + result = ['class ' + stmt.id.name]; + if (stmt.superClass) { + fragment = join('extends', this.generateExpression(stmt.superClass, Precedence.Assignment, E_TTT)); + result = join(result, fragment); + } + result.push(space); + result.push(this.generateStatement(stmt.body, S_TFFT)); + return result; + }, + + DirectiveStatement: function (stmt, flags) { + if (extra.raw && stmt.raw) { + return stmt.raw + this.semicolon(flags); + } + return escapeDirective(stmt.directive) + this.semicolon(flags); + }, + + DoWhileStatement: function (stmt, flags) { + // Because `do 42 while (cond)` is Syntax Error. We need semicolon. + var result = join('do', this.maybeBlock(stmt.body, S_TFFF)); + result = this.maybeBlockSuffix(stmt.body, result); + return join(result, [ + 'while' + space + '(', + this.generateExpression(stmt.test, Precedence.Sequence, E_TTT), + ')' + this.semicolon(flags) + ]); + }, + + CatchClause: function (stmt, flags) { + var result, that = this; + withIndent(function () { + var guard; + + result = [ + 'catch' + space + '(', + that.generateExpression(stmt.param, Precedence.Sequence, E_TTT), + ')' + ]; + + if (stmt.guard) { + guard = that.generateExpression(stmt.guard, Precedence.Sequence, E_TTT); + result.splice(2, 0, ' if ', guard); + } + }); + result.push(this.maybeBlock(stmt.body, S_TFFF)); + return result; + }, + + DebuggerStatement: function (stmt, flags) { + return 'debugger' + this.semicolon(flags); + }, + + EmptyStatement: function (stmt, flags) { + return ';'; + }, + + ExportDeclaration: function (stmt, flags) { + var result = [ 'export' ], bodyFlags, that = this; + + bodyFlags = (flags & F_SEMICOLON_OPT) ? S_TFFT : S_TFFF; + + // export default HoistableDeclaration[Default] + // export default AssignmentExpression[In] ; + if (stmt['default']) { + result = join(result, 'default'); + if (isStatement(stmt.declaration)) { + result = join(result, this.generateStatement(stmt.declaration, bodyFlags)); + } else { + result = join(result, this.generateExpression(stmt.declaration, Precedence.Assignment, E_TTT) + this.semicolon(flags)); + } + return result; + } + + // export VariableStatement + // export Declaration[Default] + if (stmt.declaration) { + return join(result, this.generateStatement(stmt.declaration, bodyFlags)); + } + + // export * FromClause ; + // export ExportClause[NoReference] FromClause ; + // export ExportClause ; + if (stmt.specifiers) { + if (stmt.specifiers.length === 0) { + result = join(result, '{' + space + '}'); + } else if (stmt.specifiers[0].type === Syntax.ExportBatchSpecifier) { + result = join(result, this.generateExpression(stmt.specifiers[0], Precedence.Sequence, E_TTT)); + } else { + result = join(result, '{'); + withIndent(function (indent) { + var i, iz; + result.push(newline); + for (i = 0, iz = stmt.specifiers.length; i < iz; ++i) { + result.push(indent); + result.push(that.generateExpression(stmt.specifiers[i], Precedence.Sequence, E_TTT)); + if (i + 1 < iz) { + result.push(',' + newline); + } + } + }); + if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) { + result.push(newline); + } + result.push(base + '}'); + } + + if (stmt.source) { + result = join(result, [ + 'from' + space, + // ModuleSpecifier + this.generateExpression(stmt.source, Precedence.Sequence, E_TTT), + this.semicolon(flags) + ]); + } else { + result.push(this.semicolon(flags)); + } + } + return result; + }, + + ExpressionStatement: function (stmt, flags) { + var result, fragment; + + function isClassPrefixed(fragment) { + var code; + if (fragment.slice(0, 5) !== 'class') { + return false; + } + code = fragment.charCodeAt(5); + return code === 0x7B /* '{' */ || esutils.code.isWhiteSpace(code) || esutils.code.isLineTerminator(code); + } + + function isFunctionPrefixed(fragment) { + var code; + if (fragment.slice(0, 8) !== 'function') { + return false; + } + code = fragment.charCodeAt(8); + return code === 0x28 /* '(' */ || esutils.code.isWhiteSpace(code) || code === 0x2A /* '*' */ || esutils.code.isLineTerminator(code); + } + + function isAsyncPrefixed(fragment) { + var code, i, iz; + if (fragment.slice(0, 5) !== 'async') { + return false; + } + if (!esutils.code.isWhiteSpace(fragment.charCodeAt(5))) { + return false; + } + for (i = 6, iz = fragment.length; i < iz; ++i) { + if (!esutils.code.isWhiteSpace(fragment.charCodeAt(i))) { + break; + } + } + if (i === iz) { + return false; + } + if (fragment.slice(i, i + 8) !== 'function') { + return false; + } + code = fragment.charCodeAt(i + 8); + return code === 0x28 /* '(' */ || esutils.code.isWhiteSpace(code) || code === 0x2A /* '*' */ || esutils.code.isLineTerminator(code); + } + + result = [this.generateExpression(stmt.expression, Precedence.Sequence, E_TTT)]; + // 12.4 '{', 'function', 'class' is not allowed in this position. + // wrap expression with parentheses + fragment = toSourceNodeWhenNeeded(result).toString(); + if (fragment.charCodeAt(0) === 0x7B /* '{' */ || // ObjectExpression + isClassPrefixed(fragment) || + isFunctionPrefixed(fragment) || + isAsyncPrefixed(fragment) || + (directive && (flags & F_DIRECTIVE_CTX) && stmt.expression.type === Syntax.Literal && typeof stmt.expression.value === 'string')) { + result = ['(', result, ')' + this.semicolon(flags)]; + } else { + result.push(this.semicolon(flags)); + } + return result; + }, + + ImportDeclaration: function (stmt, flags) { + // ES6: 15.2.1 valid import declarations: + // - import ImportClause FromClause ; + // - import ModuleSpecifier ; + var result, cursor, that = this; + + // If no ImportClause is present, + // this should be `import ModuleSpecifier` so skip `from` + // ModuleSpecifier is StringLiteral. + if (stmt.specifiers.length === 0) { + // import ModuleSpecifier ; + return [ + 'import', + space, + // ModuleSpecifier + this.generateExpression(stmt.source, Precedence.Sequence, E_TTT), + this.semicolon(flags) + ]; + } + + // import ImportClause FromClause ; + result = [ + 'import' + ]; + cursor = 0; + + // ImportedBinding + if (stmt.specifiers[cursor].type === Syntax.ImportDefaultSpecifier) { + result = join(result, [ + this.generateExpression(stmt.specifiers[cursor], Precedence.Sequence, E_TTT) + ]); + ++cursor; + } + + if (stmt.specifiers[cursor]) { + if (cursor !== 0) { + result.push(','); + } + + if (stmt.specifiers[cursor].type === Syntax.ImportNamespaceSpecifier) { + // NameSpaceImport + result = join(result, [ + space, + this.generateExpression(stmt.specifiers[cursor], Precedence.Sequence, E_TTT) + ]); + } else { + // NamedImports + result.push(space + '{'); + + if ((stmt.specifiers.length - cursor) === 1) { + // import { ... } from "..."; + result.push(space); + result.push(this.generateExpression(stmt.specifiers[cursor], Precedence.Sequence, E_TTT)); + result.push(space + '}' + space); + } else { + // import { + // ..., + // ..., + // } from "..."; + withIndent(function (indent) { + var i, iz; + result.push(newline); + for (i = cursor, iz = stmt.specifiers.length; i < iz; ++i) { + result.push(indent); + result.push(that.generateExpression(stmt.specifiers[i], Precedence.Sequence, E_TTT)); + if (i + 1 < iz) { + result.push(',' + newline); + } + } + }); + if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) { + result.push(newline); + } + result.push(base + '}' + space); + } + } + } + + result = join(result, [ + 'from' + space, + // ModuleSpecifier + this.generateExpression(stmt.source, Precedence.Sequence, E_TTT), + this.semicolon(flags) + ]); + return result; + }, + + VariableDeclarator: function (stmt, flags) { + var itemFlags = (flags & F_ALLOW_IN) ? E_TTT : E_FTT; + if (stmt.init) { + return [ + this.generateExpression(stmt.id, Precedence.Assignment, itemFlags), + space, + '=', + space, + this.generateExpression(stmt.init, Precedence.Assignment, itemFlags) + ]; + } + return this.generatePattern(stmt.id, Precedence.Assignment, itemFlags); + }, + + VariableDeclaration: function (stmt, flags) { + // VariableDeclarator is typed as Statement, + // but joined with comma (not LineTerminator). + // So if comment is attached to target node, we should specialize. + var result, i, iz, node, bodyFlags, that = this; + + result = [ stmt.kind ]; + + bodyFlags = (flags & F_ALLOW_IN) ? S_TFFF : S_FFFF; + + function block() { + node = stmt.declarations[0]; + if (extra.comment && node.leadingComments) { + result.push('\n'); + result.push(addIndent(that.generateStatement(node, bodyFlags))); + } else { + result.push(noEmptySpace()); + result.push(that.generateStatement(node, bodyFlags)); + } + + for (i = 1, iz = stmt.declarations.length; i < iz; ++i) { + node = stmt.declarations[i]; + if (extra.comment && node.leadingComments) { + result.push(',' + newline); + result.push(addIndent(that.generateStatement(node, bodyFlags))); + } else { + result.push(',' + space); + result.push(that.generateStatement(node, bodyFlags)); + } + } + } + + if (stmt.declarations.length > 1) { + withIndent(block); + } else { + block(); + } + + result.push(this.semicolon(flags)); + + return result; + }, + + ThrowStatement: function (stmt, flags) { + return [join( + 'throw', + this.generateExpression(stmt.argument, Precedence.Sequence, E_TTT) + ), this.semicolon(flags)]; + }, + + TryStatement: function (stmt, flags) { + var result, i, iz, guardedHandlers; + + result = ['try', this.maybeBlock(stmt.block, S_TFFF)]; + result = this.maybeBlockSuffix(stmt.block, result); + + if (stmt.handlers) { + // old interface + for (i = 0, iz = stmt.handlers.length; i < iz; ++i) { + result = join(result, this.generateStatement(stmt.handlers[i], S_TFFF)); + if (stmt.finalizer || i + 1 !== iz) { + result = this.maybeBlockSuffix(stmt.handlers[i].body, result); + } + } + } else { + guardedHandlers = stmt.guardedHandlers || []; + + for (i = 0, iz = guardedHandlers.length; i < iz; ++i) { + result = join(result, this.generateStatement(guardedHandlers[i], S_TFFF)); + if (stmt.finalizer || i + 1 !== iz) { + result = this.maybeBlockSuffix(guardedHandlers[i].body, result); + } + } + + // new interface + if (stmt.handler) { + if (isArray(stmt.handler)) { + for (i = 0, iz = stmt.handler.length; i < iz; ++i) { + result = join(result, this.generateStatement(stmt.handler[i], S_TFFF)); + if (stmt.finalizer || i + 1 !== iz) { + result = this.maybeBlockSuffix(stmt.handler[i].body, result); + } + } + } else { + result = join(result, this.generateStatement(stmt.handler, S_TFFF)); + if (stmt.finalizer) { + result = this.maybeBlockSuffix(stmt.handler.body, result); + } + } + } + } + if (stmt.finalizer) { + result = join(result, ['finally', this.maybeBlock(stmt.finalizer, S_TFFF)]); + } + return result; + }, + + SwitchStatement: function (stmt, flags) { + var result, fragment, i, iz, bodyFlags, that = this; + withIndent(function () { + result = [ + 'switch' + space + '(', + that.generateExpression(stmt.discriminant, Precedence.Sequence, E_TTT), + ')' + space + '{' + newline + ]; + }); + if (stmt.cases) { + bodyFlags = S_TFFF; + for (i = 0, iz = stmt.cases.length; i < iz; ++i) { + if (i === iz - 1) { + bodyFlags |= F_SEMICOLON_OPT; + } + fragment = addIndent(this.generateStatement(stmt.cases[i], bodyFlags)); + result.push(fragment); + if (!endsWithLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) { + result.push(newline); + } + } + } + result.push(addIndent('}')); + return result; + }, + + SwitchCase: function (stmt, flags) { + var result, fragment, i, iz, bodyFlags, that = this; + withIndent(function () { + if (stmt.test) { + result = [ + join('case', that.generateExpression(stmt.test, Precedence.Sequence, E_TTT)), + ':' + ]; + } else { + result = ['default:']; + } + + i = 0; + iz = stmt.consequent.length; + if (iz && stmt.consequent[0].type === Syntax.BlockStatement) { + fragment = that.maybeBlock(stmt.consequent[0], S_TFFF); + result.push(fragment); + i = 1; + } + + if (i !== iz && !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) { + result.push(newline); + } + + bodyFlags = S_TFFF; + for (; i < iz; ++i) { + if (i === iz - 1 && flags & F_SEMICOLON_OPT) { + bodyFlags |= F_SEMICOLON_OPT; + } + fragment = addIndent(that.generateStatement(stmt.consequent[i], bodyFlags)); + result.push(fragment); + if (i + 1 !== iz && !endsWithLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) { + result.push(newline); + } + } + }); + return result; + }, + + IfStatement: function (stmt, flags) { + var result, bodyFlags, semicolonOptional, that = this; + withIndent(function () { + result = [ + 'if' + space + '(', + that.generateExpression(stmt.test, Precedence.Sequence, E_TTT), + ')' + ]; + }); + semicolonOptional = flags & F_SEMICOLON_OPT; + bodyFlags = S_TFFF; + if (semicolonOptional) { + bodyFlags |= F_SEMICOLON_OPT; + } + if (stmt.alternate) { + result.push(this.maybeBlock(stmt.consequent, S_TFFF)); + result = this.maybeBlockSuffix(stmt.consequent, result); + if (stmt.alternate.type === Syntax.IfStatement) { + result = join(result, ['else ', this.generateStatement(stmt.alternate, bodyFlags)]); + } else { + result = join(result, join('else', this.maybeBlock(stmt.alternate, bodyFlags))); + } + } else { + result.push(this.maybeBlock(stmt.consequent, bodyFlags)); + } + return result; + }, + + ForStatement: function (stmt, flags) { + var result, that = this; + withIndent(function () { + result = ['for' + space + '(']; + if (stmt.init) { + if (stmt.init.type === Syntax.VariableDeclaration) { + result.push(that.generateStatement(stmt.init, S_FFFF)); + } else { + // F_ALLOW_IN becomes false. + result.push(that.generateExpression(stmt.init, Precedence.Sequence, E_FTT)); + result.push(';'); + } + } else { + result.push(';'); + } + + if (stmt.test) { + result.push(space); + result.push(that.generateExpression(stmt.test, Precedence.Sequence, E_TTT)); + result.push(';'); + } else { + result.push(';'); + } + + if (stmt.update) { + result.push(space); + result.push(that.generateExpression(stmt.update, Precedence.Sequence, E_TTT)); + result.push(')'); + } else { + result.push(')'); + } + }); + + result.push(this.maybeBlock(stmt.body, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF)); + return result; + }, + + ForInStatement: function (stmt, flags) { + return this.generateIterationForStatement('in', stmt, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF); + }, + + ForOfStatement: function (stmt, flags) { + return this.generateIterationForStatement('of', stmt, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF); + }, + + LabeledStatement: function (stmt, flags) { + return [stmt.label.name + ':', this.maybeBlock(stmt.body, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF)]; + }, + + Program: function (stmt, flags) { + var result, fragment, i, iz, bodyFlags; + iz = stmt.body.length; + result = [safeConcatenation && iz > 0 ? '\n' : '']; + bodyFlags = S_TFTF; + for (i = 0; i < iz; ++i) { + if (!safeConcatenation && i === iz - 1) { + bodyFlags |= F_SEMICOLON_OPT; + } + + if (preserveBlankLines) { + // handle spaces before the first line + if (i === 0) { + if (!stmt.body[0].leadingComments) { + generateBlankLines(stmt.range[0], stmt.body[i].range[0], result); + } + } + + // handle spaces between lines + if (i > 0) { + if (!stmt.body[i - 1].trailingComments && !stmt.body[i].leadingComments) { + generateBlankLines(stmt.body[i - 1].range[1], stmt.body[i].range[0], result); + } + } + } + + fragment = addIndent(this.generateStatement(stmt.body[i], bodyFlags)); + result.push(fragment); + if (i + 1 < iz && !endsWithLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) { + if (preserveBlankLines) { + if (!stmt.body[i + 1].leadingComments) { + result.push(newline); + } + } else { + result.push(newline); + } + } + + if (preserveBlankLines) { + // handle spaces after the last line + if (i === iz - 1) { + if (!stmt.body[i].trailingComments) { + generateBlankLines(stmt.body[i].range[1], stmt.range[1], result); + } + } + } + } + return result; + }, + + FunctionDeclaration: function (stmt, flags) { + return [ + generateAsyncPrefix(stmt, true), + 'function', + generateStarSuffix(stmt) || noEmptySpace(), + generateIdentifier(stmt.id), + this.generateFunctionBody(stmt) + ]; + }, + + ReturnStatement: function (stmt, flags) { + if (stmt.argument) { + return [join( + 'return', + this.generateExpression(stmt.argument, Precedence.Sequence, E_TTT) + ), this.semicolon(flags)]; + } + return ['return' + this.semicolon(flags)]; + }, + + WhileStatement: function (stmt, flags) { + var result, that = this; + withIndent(function () { + result = [ + 'while' + space + '(', + that.generateExpression(stmt.test, Precedence.Sequence, E_TTT), + ')' + ]; + }); + result.push(this.maybeBlock(stmt.body, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF)); + return result; + }, + + WithStatement: function (stmt, flags) { + var result, that = this; + withIndent(function () { + result = [ + 'with' + space + '(', + that.generateExpression(stmt.object, Precedence.Sequence, E_TTT), + ')' + ]; + }); + result.push(this.maybeBlock(stmt.body, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF)); + return result; + } + + }; + + merge(CodeGenerator.prototype, CodeGenerator.Statement); + + // Expressions. + + CodeGenerator.Expression = { + + SequenceExpression: function (expr, precedence, flags) { + var result, i, iz; + if (Precedence.Sequence < precedence) { + flags |= F_ALLOW_IN; + } + result = []; + for (i = 0, iz = expr.expressions.length; i < iz; ++i) { + result.push(this.generateExpression(expr.expressions[i], Precedence.Assignment, flags)); + if (i + 1 < iz) { + result.push(',' + space); + } + } + return parenthesize(result, Precedence.Sequence, precedence); + }, + + AssignmentExpression: function (expr, precedence, flags) { + return this.generateAssignment(expr.left, expr.right, expr.operator, precedence, flags); + }, + + ArrowFunctionExpression: function (expr, precedence, flags) { + return parenthesize(this.generateFunctionBody(expr), Precedence.ArrowFunction, precedence); + }, + + ConditionalExpression: function (expr, precedence, flags) { + if (Precedence.Conditional < precedence) { + flags |= F_ALLOW_IN; + } + return parenthesize( + [ + this.generateExpression(expr.test, Precedence.LogicalOR, flags), + space + '?' + space, + this.generateExpression(expr.consequent, Precedence.Assignment, flags), + space + ':' + space, + this.generateExpression(expr.alternate, Precedence.Assignment, flags) + ], + Precedence.Conditional, + precedence + ); + }, + + LogicalExpression: function (expr, precedence, flags) { + return this.BinaryExpression(expr, precedence, flags); + }, + + BinaryExpression: function (expr, precedence, flags) { + var result, currentPrecedence, fragment, leftSource; + currentPrecedence = BinaryPrecedence[expr.operator]; + + if (currentPrecedence < precedence) { + flags |= F_ALLOW_IN; + } + + fragment = this.generateExpression(expr.left, currentPrecedence, flags); + + leftSource = fragment.toString(); + + if (leftSource.charCodeAt(leftSource.length - 1) === 0x2F /* / */ && esutils.code.isIdentifierPart(expr.operator.charCodeAt(0))) { + result = [fragment, noEmptySpace(), expr.operator]; + } else { + result = join(fragment, expr.operator); + } + + fragment = this.generateExpression(expr.right, currentPrecedence + 1, flags); + + if (expr.operator === '/' && fragment.toString().charAt(0) === '/' || + expr.operator.slice(-1) === '<' && fragment.toString().slice(0, 3) === '!--') { + // If '/' concats with '/' or `<` concats with `!--`, it is interpreted as comment start + result.push(noEmptySpace()); + result.push(fragment); + } else { + result = join(result, fragment); + } + + if (expr.operator === 'in' && !(flags & F_ALLOW_IN)) { + return ['(', result, ')']; + } + return parenthesize(result, currentPrecedence, precedence); + }, + + CallExpression: function (expr, precedence, flags) { + var result, i, iz; + // F_ALLOW_UNPARATH_NEW becomes false. + result = [this.generateExpression(expr.callee, Precedence.Call, E_TTF)]; + result.push('('); + for (i = 0, iz = expr['arguments'].length; i < iz; ++i) { + result.push(this.generateExpression(expr['arguments'][i], Precedence.Assignment, E_TTT)); + if (i + 1 < iz) { + result.push(',' + space); + } + } + result.push(')'); + + if (!(flags & F_ALLOW_CALL)) { + return ['(', result, ')']; + } + return parenthesize(result, Precedence.Call, precedence); + }, + + NewExpression: function (expr, precedence, flags) { + var result, length, i, iz, itemFlags; + length = expr['arguments'].length; + + // F_ALLOW_CALL becomes false. + // F_ALLOW_UNPARATH_NEW may become false. + itemFlags = (flags & F_ALLOW_UNPARATH_NEW && !parentheses && length === 0) ? E_TFT : E_TFF; + + result = join( + 'new', + this.generateExpression(expr.callee, Precedence.New, itemFlags) + ); + + if (!(flags & F_ALLOW_UNPARATH_NEW) || parentheses || length > 0) { + result.push('('); + for (i = 0, iz = length; i < iz; ++i) { + result.push(this.generateExpression(expr['arguments'][i], Precedence.Assignment, E_TTT)); + if (i + 1 < iz) { + result.push(',' + space); + } + } + result.push(')'); + } + + return parenthesize(result, Precedence.New, precedence); + }, + + MemberExpression: function (expr, precedence, flags) { + var result, fragment; + + // F_ALLOW_UNPARATH_NEW becomes false. + result = [this.generateExpression(expr.object, Precedence.Call, (flags & F_ALLOW_CALL) ? E_TTF : E_TFF)]; + + if (expr.computed) { + result.push('['); + result.push(this.generateExpression(expr.property, Precedence.Sequence, flags & F_ALLOW_CALL ? E_TTT : E_TFT)); + result.push(']'); + } else { + if (expr.object.type === Syntax.Literal && typeof expr.object.value === 'number') { + fragment = toSourceNodeWhenNeeded(result).toString(); + // When the following conditions are all true, + // 1. No floating point + // 2. Don't have exponents + // 3. The last character is a decimal digit + // 4. Not hexadecimal OR octal number literal + // we should add a floating point. + if ( + fragment.indexOf('.') < 0 && + !/[eExX]/.test(fragment) && + esutils.code.isDecimalDigit(fragment.charCodeAt(fragment.length - 1)) && + !(fragment.length >= 2 && fragment.charCodeAt(0) === 48) // '0' + ) { + result.push('.'); + } + } + result.push('.'); + result.push(generateIdentifier(expr.property)); + } + + return parenthesize(result, Precedence.Member, precedence); + }, + + UnaryExpression: function (expr, precedence, flags) { + var result, fragment, rightCharCode, leftSource, leftCharCode; + fragment = this.generateExpression(expr.argument, Precedence.Unary, E_TTT); + + if (space === '') { + result = join(expr.operator, fragment); + } else { + result = [expr.operator]; + if (expr.operator.length > 2) { + // delete, void, typeof + // get `typeof []`, not `typeof[]` + result = join(result, fragment); + } else { + // Prevent inserting spaces between operator and argument if it is unnecessary + // like, `!cond` + leftSource = toSourceNodeWhenNeeded(result).toString(); + leftCharCode = leftSource.charCodeAt(leftSource.length - 1); + rightCharCode = fragment.toString().charCodeAt(0); + + if (((leftCharCode === 0x2B /* + */ || leftCharCode === 0x2D /* - */) && leftCharCode === rightCharCode) || + (esutils.code.isIdentifierPart(leftCharCode) && esutils.code.isIdentifierPart(rightCharCode))) { + result.push(noEmptySpace()); + result.push(fragment); + } else { + result.push(fragment); + } + } + } + return parenthesize(result, Precedence.Unary, precedence); + }, + + YieldExpression: function (expr, precedence, flags) { + var result; + if (expr.delegate) { + result = 'yield*'; + } else { + result = 'yield'; + } + if (expr.argument) { + result = join( + result, + this.generateExpression(expr.argument, Precedence.Yield, E_TTT) + ); + } + return parenthesize(result, Precedence.Yield, precedence); + }, + + AwaitExpression: function (expr, precedence, flags) { + var result = join( + expr.delegate ? 'await*' : 'await', + this.generateExpression(expr.argument, Precedence.Await, E_TTT) + ); + return parenthesize(result, Precedence.Await, precedence); + }, + + UpdateExpression: function (expr, precedence, flags) { + if (expr.prefix) { + return parenthesize( + [ + expr.operator, + this.generateExpression(expr.argument, Precedence.Unary, E_TTT) + ], + Precedence.Unary, + precedence + ); + } + return parenthesize( + [ + this.generateExpression(expr.argument, Precedence.Postfix, E_TTT), + expr.operator + ], + Precedence.Postfix, + precedence + ); + }, + + FunctionExpression: function (expr, precedence, flags) { + var result = [ + generateAsyncPrefix(expr, true), + 'function' + ]; + if (expr.id) { + result.push(generateStarSuffix(expr) || noEmptySpace()); + result.push(generateIdentifier(expr.id)); + } else { + result.push(generateStarSuffix(expr) || space); + } + result.push(this.generateFunctionBody(expr)); + return result; + }, + + ExportBatchSpecifier: function (expr, precedence, flags) { + return '*'; + }, + + ArrayPattern: function (expr, precedence, flags) { + return this.ArrayExpression(expr, precedence, flags); + }, + + ArrayExpression: function (expr, precedence, flags) { + var result, multiline, that = this; + if (!expr.elements.length) { + return '[]'; + } + multiline = expr.elements.length > 1; + result = ['[', multiline ? newline : '']; + withIndent(function (indent) { + var i, iz; + for (i = 0, iz = expr.elements.length; i < iz; ++i) { + if (!expr.elements[i]) { + if (multiline) { + result.push(indent); + } + if (i + 1 === iz) { + result.push(','); + } + } else { + result.push(multiline ? indent : ''); + result.push(that.generateExpression(expr.elements[i], Precedence.Assignment, E_TTT)); + } + if (i + 1 < iz) { + result.push(',' + (multiline ? newline : space)); + } + } + }); + if (multiline && !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) { + result.push(newline); + } + result.push(multiline ? base : ''); + result.push(']'); + return result; + }, + + ClassExpression: function (expr, precedence, flags) { + var result, fragment; + result = ['class']; + if (expr.id) { + result = join(result, this.generateExpression(expr.id, Precedence.Sequence, E_TTT)); + } + if (expr.superClass) { + fragment = join('extends', this.generateExpression(expr.superClass, Precedence.Assignment, E_TTT)); + result = join(result, fragment); + } + result.push(space); + result.push(this.generateStatement(expr.body, S_TFFT)); + return result; + }, + + MethodDefinition: function (expr, precedence, flags) { + var result, fragment; + if (expr['static']) { + result = ['static' + space]; + } else { + result = []; + } + if (expr.kind === 'get' || expr.kind === 'set') { + fragment = [ + join(expr.kind, this.generatePropertyKey(expr.key, expr.computed)), + this.generateFunctionBody(expr.value) + ]; + } else { + fragment = [ + generateMethodPrefix(expr), + this.generatePropertyKey(expr.key, expr.computed), + this.generateFunctionBody(expr.value) + ]; + } + return join(result, fragment); + }, + + Property: function (expr, precedence, flags) { + if (expr.kind === 'get' || expr.kind === 'set') { + return [ + expr.kind, noEmptySpace(), + this.generatePropertyKey(expr.key, expr.computed), + this.generateFunctionBody(expr.value) + ]; + } + + if (expr.shorthand) { + return this.generatePropertyKey(expr.key, expr.computed); + } + + if (expr.method) { + return [ + generateMethodPrefix(expr), + this.generatePropertyKey(expr.key, expr.computed), + this.generateFunctionBody(expr.value) + ]; + } + return [ + this.generatePropertyKey(expr.key, expr.computed), + ':' + space, + this.generateExpression(expr.value, Precedence.Assignment, E_TTT) + ]; + }, + + ObjectExpression: function (expr, precedence, flags) { + var multiline, result, fragment, that = this; + + if (!expr.properties.length) { + return '{}'; + } + multiline = expr.properties.length > 1; + + withIndent(function () { + fragment = that.generateExpression(expr.properties[0], Precedence.Sequence, E_TTT); + }); + + if (!multiline) { + // issues 4 + // Do not transform from + // dejavu.Class.declare({ + // method2: function () {} + // }); + // to + // dejavu.Class.declare({method2: function () { + // }}); + if (!hasLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) { + return [ '{', space, fragment, space, '}' ]; + } + } + + withIndent(function (indent) { + var i, iz; + result = [ '{', newline, indent, fragment ]; + + if (multiline) { + result.push(',' + newline); + for (i = 1, iz = expr.properties.length; i < iz; ++i) { + result.push(indent); + result.push(that.generateExpression(expr.properties[i], Precedence.Sequence, E_TTT)); + if (i + 1 < iz) { + result.push(',' + newline); + } + } + } + }); + + if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) { + result.push(newline); + } + result.push(base); + result.push('}'); + return result; + }, + + ObjectPattern: function (expr, precedence, flags) { + var result, i, iz, multiline, property, that = this; + if (!expr.properties.length) { + return '{}'; + } + + multiline = false; + if (expr.properties.length === 1) { + property = expr.properties[0]; + if (property.value.type !== Syntax.Identifier) { + multiline = true; + } + } else { + for (i = 0, iz = expr.properties.length; i < iz; ++i) { + property = expr.properties[i]; + if (!property.shorthand) { + multiline = true; + break; + } + } + } + result = ['{', multiline ? newline : '' ]; + + withIndent(function (indent) { + var i, iz; + for (i = 0, iz = expr.properties.length; i < iz; ++i) { + result.push(multiline ? indent : ''); + result.push(that.generateExpression(expr.properties[i], Precedence.Sequence, E_TTT)); + if (i + 1 < iz) { + result.push(',' + (multiline ? newline : space)); + } + } + }); + + if (multiline && !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) { + result.push(newline); + } + result.push(multiline ? base : ''); + result.push('}'); + return result; + }, + + ThisExpression: function (expr, precedence, flags) { + return 'this'; + }, + + Identifier: function (expr, precedence, flags) { + return generateIdentifier(expr); + }, + + ImportDefaultSpecifier: function (expr, precedence, flags) { + return generateIdentifier(expr.id); + }, + + ImportNamespaceSpecifier: function (expr, precedence, flags) { + var result = ['*']; + if (expr.id) { + result.push(space + 'as' + noEmptySpace() + generateIdentifier(expr.id)); + } + return result; + }, + + ImportSpecifier: function (expr, precedence, flags) { + return this.ExportSpecifier(expr, precedence, flags); + }, + + ExportSpecifier: function (expr, precedence, flags) { + var result = [ expr.id.name ]; + if (expr.name) { + result.push(noEmptySpace() + 'as' + noEmptySpace() + generateIdentifier(expr.name)); + } + return result; + }, + + Literal: function (expr, precedence, flags) { + var raw; + if (expr.hasOwnProperty('raw') && parse && extra.raw) { + try { + raw = parse(expr.raw).body[0].expression; + if (raw.type === Syntax.Literal) { + if (raw.value === expr.value) { + return expr.raw; + } + } + } catch (e) { + // not use raw property + } + } + + if (expr.value === null) { + return 'null'; + } + + if (typeof expr.value === 'string') { + return escapeString(expr.value); + } + + if (typeof expr.value === 'number') { + return generateNumber(expr.value); + } + + if (typeof expr.value === 'boolean') { + return expr.value ? 'true' : 'false'; + } + + return generateRegExp(expr.value); + }, + + GeneratorExpression: function (expr, precedence, flags) { + return this.ComprehensionExpression(expr, precedence, flags); + }, + + ComprehensionExpression: function (expr, precedence, flags) { + // GeneratorExpression should be parenthesized with (...), ComprehensionExpression with [...] + // Due to https://bugzilla.mozilla.org/show_bug.cgi?id=883468 position of expr.body can differ in Spidermonkey and ES6 + + var result, i, iz, fragment, that = this; + result = (expr.type === Syntax.GeneratorExpression) ? ['('] : ['[']; + + if (extra.moz.comprehensionExpressionStartsWithAssignment) { + fragment = this.generateExpression(expr.body, Precedence.Assignment, E_TTT); + result.push(fragment); + } + + if (expr.blocks) { + withIndent(function () { + for (i = 0, iz = expr.blocks.length; i < iz; ++i) { + fragment = that.generateExpression(expr.blocks[i], Precedence.Sequence, E_TTT); + if (i > 0 || extra.moz.comprehensionExpressionStartsWithAssignment) { + result = join(result, fragment); + } else { + result.push(fragment); + } + } + }); + } + + if (expr.filter) { + result = join(result, 'if' + space); + fragment = this.generateExpression(expr.filter, Precedence.Sequence, E_TTT); + result = join(result, [ '(', fragment, ')' ]); + } + + if (!extra.moz.comprehensionExpressionStartsWithAssignment) { + fragment = this.generateExpression(expr.body, Precedence.Assignment, E_TTT); + + result = join(result, fragment); + } + + result.push((expr.type === Syntax.GeneratorExpression) ? ')' : ']'); + return result; + }, + + ComprehensionBlock: function (expr, precedence, flags) { + var fragment; + if (expr.left.type === Syntax.VariableDeclaration) { + fragment = [ + expr.left.kind, noEmptySpace(), + this.generateStatement(expr.left.declarations[0], S_FFFF) + ]; + } else { + fragment = this.generateExpression(expr.left, Precedence.Call, E_TTT); + } + + fragment = join(fragment, expr.of ? 'of' : 'in'); + fragment = join(fragment, this.generateExpression(expr.right, Precedence.Sequence, E_TTT)); + + return [ 'for' + space + '(', fragment, ')' ]; + }, + + SpreadElement: function (expr, precedence, flags) { + return [ + '...', + this.generateExpression(expr.argument, Precedence.Assignment, E_TTT) + ]; + }, + + TaggedTemplateExpression: function (expr, precedence, flags) { + var itemFlags = E_TTF; + if (!(flags & F_ALLOW_CALL)) { + itemFlags = E_TFF; + } + var result = [ + this.generateExpression(expr.tag, Precedence.Call, itemFlags), + this.generateExpression(expr.quasi, Precedence.Primary, E_FFT) + ]; + return parenthesize(result, Precedence.TaggedTemplate, precedence); + }, + + TemplateElement: function (expr, precedence, flags) { + // Don't use "cooked". Since tagged template can use raw template + // representation. So if we do so, it breaks the script semantics. + return expr.value.raw; + }, + + TemplateLiteral: function (expr, precedence, flags) { + var result, i, iz; + result = [ '`' ]; + for (i = 0, iz = expr.quasis.length; i < iz; ++i) { + result.push(this.generateExpression(expr.quasis[i], Precedence.Primary, E_TTT)); + if (i + 1 < iz) { + result.push('${' + space); + result.push(this.generateExpression(expr.expressions[i], Precedence.Sequence, E_TTT)); + result.push(space + '}'); + } + } + result.push('`'); + return result; + }, + + ModuleSpecifier: function (expr, precedence, flags) { + return this.Literal(expr, precedence, flags); + } + + }; + + merge(CodeGenerator.prototype, CodeGenerator.Expression); + + CodeGenerator.prototype.generateExpression = function (expr, precedence, flags) { + var result, type; + + type = expr.type || Syntax.Property; + + if (extra.verbatim && expr.hasOwnProperty(extra.verbatim)) { + return generateVerbatim(expr, precedence); + } + + result = this[type](expr, precedence, flags); + + + if (extra.comment) { + result = addComments(expr,result); + } + return toSourceNodeWhenNeeded(result, expr); + }; + + CodeGenerator.prototype.generateStatement = function (stmt, flags) { + var result, + fragment; + + result = this[stmt.type](stmt, flags); + + // Attach comments + + if (extra.comment) { + result = addComments(stmt, result); + } + + fragment = toSourceNodeWhenNeeded(result).toString(); + if (stmt.type === Syntax.Program && !safeConcatenation && newline === '' && fragment.charAt(fragment.length - 1) === '\n') { + result = sourceMap ? toSourceNodeWhenNeeded(result).replaceRight(/\s+$/, '') : fragment.replace(/\s+$/, ''); + } + + return toSourceNodeWhenNeeded(result, stmt); + }; + + function generateInternal(node) { + var codegen; + + codegen = new CodeGenerator(); + if (isStatement(node)) { + return codegen.generateStatement(node, S_TFFF); + } + + if (isExpression(node)) { + return codegen.generateExpression(node, Precedence.Sequence, E_TTT); + } + + throw new Error('Unknown node type: ' + node.type); + } + + function generate(node, options) { + var defaultOptions = getDefaultOptions(), result, pair; + + if (options != null) { + // Obsolete options + // + // `options.indent` + // `options.base` + // + // Instead of them, we can use `option.format.indent`. + if (typeof options.indent === 'string') { + defaultOptions.format.indent.style = options.indent; + } + if (typeof options.base === 'number') { + defaultOptions.format.indent.base = options.base; + } + options = updateDeeply(defaultOptions, options); + indent = options.format.indent.style; + if (typeof options.base === 'string') { + base = options.base; + } else { + base = stringRepeat(indent, options.format.indent.base); + } + } else { + options = defaultOptions; + indent = options.format.indent.style; + base = stringRepeat(indent, options.format.indent.base); + } + json = options.format.json; + renumber = options.format.renumber; + hexadecimal = json ? false : options.format.hexadecimal; + quotes = json ? 'double' : options.format.quotes; + escapeless = options.format.escapeless; + newline = options.format.newline; + space = options.format.space; + if (options.format.compact) { + newline = space = indent = base = ''; + } + parentheses = options.format.parentheses; + semicolons = options.format.semicolons; + safeConcatenation = options.format.safeConcatenation; + directive = options.directive; + parse = json ? null : options.parse; + sourceMap = options.sourceMap; + sourceCode = options.sourceCode; + preserveBlankLines = options.format.preserveBlankLines && sourceCode !== null; + extra = options; + + if (sourceMap) { + if (!exports.browser) { + // We assume environment is node.js + // And prevent from including source-map by browserify + SourceNode = require('source-map').SourceNode; + } else { + SourceNode = global.sourceMap.SourceNode; + } + } + + result = generateInternal(node); + + if (!sourceMap) { + pair = {code: result.toString(), map: null}; + return options.sourceMapWithCode ? pair : pair.code; + } + + + pair = result.toStringWithSourceMap({ + file: options.file, + sourceRoot: options.sourceMapRoot + }); + + if (options.sourceContent) { + pair.map.setSourceContent(options.sourceMap, + options.sourceContent); + } + + if (options.sourceMapWithCode) { + return pair; + } + + return pair.map.toString(); + } + + FORMAT_MINIFY = { + indent: { + style: '', + base: 0 + }, + renumber: true, + hexadecimal: true, + quotes: 'auto', + escapeless: true, + compact: true, + parentheses: false, + semicolons: false + }; + + FORMAT_DEFAULTS = getDefaultOptions().format; + + exports.version = require('./package.json').version; + exports.generate = generate; + exports.attachComments = estraverse.attachComments; + exports.Precedence = updateDeeply({}, Precedence); + exports.browser = false; + exports.FORMAT_MINIFY = FORMAT_MINIFY; + exports.FORMAT_DEFAULTS = FORMAT_DEFAULTS; +}()); +/* vim: set sw=4 ts=4 et tw=80 : */ + + })(function(path) { + if (!modules[path]) throw new Error("unexpected require: " + path); + return modules[path]; + }, window.escodegen = { + browser: true, + }); +})(); +"use strict"; +(function(exports) { + exports.version = "0.10.17"; +})(typeof exports !== 'undefined' ? exports : (Streamline.version = Streamline.version || {})); +"use strict"; +(function() { +var sourceMap; +if (typeof exports !== 'undefined') { + var req = require; // fool streamline-require so that we don't load source-map client-side + try { sourceMap = req('source-map'); } catch (ex) {} +} +if (!sourceMap) { + // Mock it for client-side + sourceMap = { + SourceNode: function(lineno, column, source, content) { + this.children = content ? [content] : []; + } + }; + sourceMap.SourceNode.prototype.add = function(elt) { + if (Array.isArray(elt)) this.children = this.children.concat(elt); + else this.children.push(elt); + return this; + }; + sourceMap.SourceNode.prototype.prepend = function(elt) { + if (Array.isArray(elt)) this.children = elt.concat(this.children.concat); + else this.children.unshift(elt); + return this; + }; + sourceMap.SourceNode.prototype.toString = function() { + var str = ""; + this.walk(function (chunk) { + str += chunk; + }); + return str; + }; + sourceMap.SourceNode.prototype.walk = function(f) { + this.children.forEach(function(n) { + if (n instanceof sourceMap.SourceNode) n.walk(f); + else f(n); + }); + return this; + }; +} +(function(module) { +function SourceNode() { + sourceMap.SourceNode.apply(this, arguments); +} + +SourceNode.prototype = Object.create(sourceMap.SourceNode.prototype, { + constructor: { + value: SourceNode, + enumerable: false, + writable: true, + configurable: true + }, + length: { + get: function() { + var len = 0; + this.walk(function(str) { len += str.length; }); + return len; + } + } +}); +SourceNode.prototype.stripPrefix = function(offset) { + var _len; + while (this.children.length > 0 && offset > 0 && (_len = this.children[0].length) <= offset) { + this.children.shift(); + offset -= _len; + } + if (this.children.length == 0 || offset == 0) return this; + if (typeof this.children[0] == 'string') { + this.children[0] = this.children[0].substring(offset); + } else { + this.children[0].stripPrefix(offset); + } + return this; +}; +SourceNode.prototype.stripSuffix = function(offset) { + var _len, chlen; + while ((chlen = this.children.length) > 0 && offset > 0 && (_len = this.children[chlen - 1].length) <= offset) { + this.children.pop(); + offset -= _len; + } + if (chlen == 0 || offset == 0) return this; + if (typeof this.children[chlen-1] == 'string') { + this.children[chlen-1] = this.children[0].slice(0, -offset); + } else { + this.children[chlen-1].stripSuffix(offset); + } + return this; +}; +SourceNode.prototype.map = function(f) { + this.children = this.children.map(function(chunk) { + if (chunk instanceof sourceMap.SourceNode) { + return chunk.map(f); + } else { + return f(chunk); + } + }); + return this; +}; +SourceNode.prototype.lastChar = function() { + for (var i = this.children.length; i--; ) { + var ret; + if (typeof this.children[i] == 'string') { + ret = this.children[i].slice(-1); + } else { + ret = this.children[i].lastChar(); + } + if (ret) return ret; + } + return ''; +}; +module.exports = Object.create(sourceMap, { SourceNode: { value: SourceNode } }); + +})(typeof exports !== 'undefined' ? module : (Streamline.sourceMap = Streamline.sourceMap || {})); +})();// Copyright 2011 Marcel Laverdet +"use strict"; +if (typeof exports !== 'undefined') { + module.exports = Walker; +} else { + Streamline.walker = Walker; +} + +/** + * It seems that Narcissus lacks a reasonable node walker. This implements a simple walker which + * lets you walk a tree linearly, in terms of source code, and only subscribe to the parts that + * you're interested in. + */ +function Walker(visitors) { + return function walk(node) { + var type = node.type; + if (type === undefined) { + throw new Error('Trying to walk unknown node!'); + } + switch (type) { + case 'AssignmentExpression': + if (visitors[type]) { + visitors[type].call(node, node.left, node.right); + } else { + walk(node.left); + walk(node.right); + } + break; + + case 'ArrayExpression': + if (visitors[type]) { + visitors[type].call(node, node.elements); + } else { + node.elements.map(walk); + } + break; + + case 'ArrowFunctionExpression': + throw new Error("NIY"); + + case 'BinaryExpression': + case 'LogicalExpression': + if (visitors[type]) { + visitors[type].call(node, node.operator, node.left, node.right); + } else { + walk(node.left); + walk(node.right); + } + break; + + case 'BlockStatement': + case 'Program': + if (visitors[type]) { + visitors[type].call(node, node.body); + } else { + node.body.map(walk); + } + break; + + case 'BreakStatement': + case 'ContinueStatement': + if (visitors[type]) { + visitors[type].call(node, node.label && node.label.name); + } + break; + + case 'CallExpression': + case 'NewExpression': + if (visitors[type]) { + visitors[type].call(node, node.callee, node.arguments); + } else { + walk(node.callee); + node.arguments.map(walk); + } + break; + + case 'CatchClause': + if (visitors[type]) { + visitors[type].call(node, node.body); + } else { + walk(node.body); + } + break; + + case 'ConditionalExpression': + case 'IfStatement': + if (visitors[type]) { + visitors[type].call(node, node.test, node.consequent, node.alternate); + } else { + walk(node.test); + walk(node.consequent); + node.alternate && walk(node.alternate); + } + break; + + case 'DebuggerStatement': + case 'EmptyStatement': + case 'ThisExpression': + if (visitors[type]) { + visitors[type].call(node); + } + break; + + case 'DoWhileStatement': + if (visitors[type]) { + visitors[type].call(node, node.test, node.body); + } else { + walk(node.body); + walk(node.test); + } + break; + + case 'ExpressionStatement': + if (visitors[type]) { + visitors[type].call(node, node.expression); + } else { + node.expression && walk(node.expression); + } + break; + + case 'ForStatement': + if (visitors[type]) { + visitors[type].call(node, node.init, node.test, node.update, node.body); + } else { + node.init && walk(node.init); + node.test && walk(node.test); + node.update && walk(node.update); + walk(node.body); + } + break; + + case 'ForInStatement': + if (visitors[type]) { + visitors[type].call(node, node.left, node.right, node.body); + } else { + walk(node.left); + walk(node.right); + walk(node.body); + } + break; + + case 'FunctionDeclaration': + case 'FunctionExpression': + if (visitors[type] || visitors['Function']) { + (visitors[type] || visitors['Function']).call(node, node.id && node.id.name, node.params, node.body.body); + } else { + node.body.body.map(walk); + } + break; + + case 'Identifier': + if (visitors[type]) { + visitors[type].call(node, node.name); + } + break; + + case 'LabeledStatement': + if (visitors[type]) { + visitors[type].call(node, node.label.id, node.body); + } else { + walk(node.body); + } + break; + + case 'Literal': + if (visitors[type]) { + visitors[type].call(node, node.value); + } + break; + + case 'MemberExpression': + if (visitors[type]) { + visitors[type].call(node, node.object, node.property); + } else { + walk(node.object); + } + break; + + case 'ObjectExpression': + if (visitors[type]) { + visitors[type].call(node, node.properties); + } else { + node.properties.map(walk); + } + break; + + case 'Property': + if (visitors[type]) { + visitors[type].call(node, node.key, node.value); + } else { + walk(node.value); + } + break; + + case 'ReturnStatement': + case 'ThrowStatement': + if (visitors[type]) { + visitors[type].call(node, node.argument); + } else { + node.argument && walk(node.argument); + } + break; + + case 'SequenceExpression': + if (visitors[type]) { + visitors[type].call(node, node.expressions); + } else { + node.expressions.map(walk); + } + break; + + case 'SwitchCase': + if (visitors[type]) { + visitors[type].call(node, node.test, node.consequent); + } else { + node.test && walk(node.test); + node.consequent.map(walk); + Walker.insideCase = false; + } + break; + + case 'SwitchStatement': + if (visitors[type]) { + visitors[type].call(node, node.discriminant, node.cases); + } else { + walk(node.discriminant); + node.cases.map(walk); + } + break; + + case 'TryStatement': + if (visitors[type]) { + visitors[type].call(node, node.block, node.handlers, node.finalizer); + } else { + walk(node.block); + node.handlers.map(walk); + node.finalizer && walk(node.finalizer); + } + break; + + case 'UnaryExpression': + case 'UpdateExpression': + if (visitors[type]) { + visitors[type].call(node, node.operator, node.argument); + } else { + walk(node.argument); + } + break; + + case 'VariableDeclaration': + if (visitors[type]) { + visitors[type].call(node, node.declarations); + } else { + node.declarations.map(walk); + } + break; + + case 'VariableDeclarator': + if (visitors[type]) { + visitors[type].call(node, node.id.name, node.init); + } else { + node.init && walk(node.init); + } + break; + + case 'WhileStatement': + if (visitors[type]) { + visitors[type].call(node, node.test, node.body); + } else { + walk(node.test); + walk(node.body); + } + break; + + case 'WithStatement': + if (visitors[type]) { + visitors[type].call(node, node.object, node.body); + } else { + walk(node.object); + walk(node.body); + } + break; + + + default: + throw new Error("unhandled node type: " + node.type); + } + }; +} +// Copyright 2011 Marcel Laverdet +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +"use strict"; +if (typeof exports !== 'undefined') { + var esprima = require('esprima'); + var sourceMap = require('../util/source-map'); +} +(function(exports) { + exports.transform = transform; + exports.version = require("../version").version + " (generators)"; + // hack to fix #123 + exports.transform.version = exports.version; + + try { + eval("(function*(){})"); + } catch (ex) { + console.log("warning: generators not supported or not enabled by your JS engine"); + } + + var Walker = require('../fibers/walker'); + + // TODO ensure `foo(_)` calls have a bounding fiber. streamline is smart enough to allow this: + // ~function() { foo(_) }(); + // and disallow this: + // foo(function() { foo(_) }); + /** + * Finds all variables which have been declared locally in this function. + */ + function getLocals(fn, recurse) { + var names = Object.create(null); + function decl() { + var vars = this.declarations; + for (var ii = 0; ii < vars.length; ++ii) { + names[vars[ii].id.name] = vars[ii].id.name; + vars[ii].init && walk(vars[ii].init); + } + } + var walk = Walker({ + Function: function(name, args, body) { + if (this.type !== 'FunctionExpression') { + names[this.id.name] = this.id.name; + } + // Don't walk further by default + if (recurse) { + for (var ii in getLocals(this, true)) { + names[ii] = ii; + } + } + }, + VariableDeclaration: decl, + }); + fn.body.body.map(walk); + for (var ii = 0; ii < fn.params; ++ii) { + names[fn.params[ii].name] = fn.params[ii].name; + } + if (fn.id && fn.type === 'FunctionExpression') { + names[fn.id.name] = fn.id.name; + } + return names; + } + + + /** + * Create a new object inheriting from `base` and extended by `vector` + */ + + function chain(base, vector) { + var obj = Object.create(base); + for (var ii in vector) { + obj[ii] = vector[ii]; + } + return obj; + } + + /** + * Transforms code to be streamliney. Line numbers are not maintained, but could be if I could + * figure out how to do it with uglifyjs. + */ + + function transform(source, options) { + source = source.replace(/\r\n/g, "\n"); + options = options || {}; + var callback = options.callback || '_'; + var didRewrite = 0; + var position = 0; + var buffer = ''; + var scope = Object.create(null); + var streamlined = Object.create(null); + var verboten = Object.create(null); + var async = false; + var asyncDepth = 0; + var allIdentifiers; + + /** + * Walks a parse tree and finds all functions which have been declared as streamline functions. + * Also returns variables which are always streamlined functions. Note that this does not enter + * new function scopes. + */ + + function getStreamlinedDeclarations(fn) { + var declared = Object.create(null); + var exprs = Object.create(null); + var not = Object.create(null); + var walk = Walker({ + Function: function(name, args, body) { + if (this.type !== 'FunctionExpression') { + var idx = getCallback(args); + (idx === -1 ? not : declared)[name] = getCallbackDefault(args, body) || idx; + } + // Don't walk further + }, + VariableDeclarator: function(name, initializer) { + if (!initializer) { + return; + } + if (initializer.type === 'FunctionExpression') { + (getCallback(initializer.params) === -1 ? not : exprs)[name] = true; + } else { + not[name] = true; + } + walk(initializer); + }, + AssignmentExpression: function(left, right) { + var name = left.type === 'Identifier' && left.name; + if (name) { + if (right.type === 'FunctionExpression') { + (getCallback(right.params) === -1 ? not : exprs)[name] = true; + } else { + not[name] = true; + } + } + walk(right); + }, + }); + fn.body.body.map(walk); + for (var ii in declared) { + exprs[ii] = true; + } + for (var ii in not) { + delete exprs[ii]; + } + return { + declared: declared, + strict: exprs, + }; + } + + /** + * Finds the index of the callback param in an argument list, -1 if not found. + */ + + function getCallback(args, lineno) { + var idx = -1; + for (var ii = 0; ii < args.length; ++ii) { + if (args[ii] === callback || (args[ii].type === 'Identifier' && args[ii].name === callback) || // + (args[ii].type === 'UnaryExpression' && args[ii].operator === '~' && args[ii].argument.type === 'Identifier' && args[ii].argument.name === callback) || + (args[ii].type === 'ArrayExpression' && args[ii].elements.length === 1 && args[ii].elements[0].type === 'Identifier' && args[ii].elements[0].name === callback)) { + if (idx === -1) { + idx = ii; + } else { + lineno = lineno || args[ii].loc.start.line; + throw error(lineno, 'Callback argument used more than once in function call '); + } + } + } + return idx; + } + + /** + * Adds to `buffer` everything that hasn't been rendered so far. + */ + + function catchup(end) { + if (end < position || end === undefined) { + throw new Error('Invalid catchup, ' + position + ' to ' + end); + } + buffer += source.substring(position, end); + position = end; + } + + function skipTo(pos) { + buffer += source.substring(position, pos).replace(/\S/g, ''); + position = pos; + } + + function startsWith(str, start, pat) { + return str.substring(start, start + pat.length) === pat; + } + + function endsWith(str, end, pat) { + return str.substring(end - pat.length, end) === pat; + } + + function getCallbackDefault(params, body) { + var paramI = -1; + for (var i = 0; i < body.length; i++) { + var child = body[i]; + if (i === 0 && child.type === 'VariableDeclaration') { continue; } + if (child.type !== 'IfStatement') return null; + if (child.test.type !== 'BinaryExpression' || child.test.operator !== '==') return null; + var ident = child.test.left; + if (ident.type !== 'Identifier') return null; + if (child.test.right.type !== 'Literal' || child.test.right.value !== null) return false; + if (!child.consequent.body || child.consequent.body.length !== 1) return false; + var assign = child.consequent.body[0]; + if (assign.type !== 'ExpressionStatement') return false; + assign = assign.expression; + if (assign.type !== 'AssignmentExpression') return false; + if (assign.left.type !== 'Identifier') return false; + if (assign.left.name !== ident.name) return false; + // we got a candidate - let us find the param + while (++paramI < params.length) { + if (ident.name === params[paramI].name) break; + } + if (paramI === params.length) return null; + if (ident.name === callback) { + body.splice(i, 1); // remove it from body + var def = assign.right; + return '{callbackIndex:' + paramI + ",callbackDefault:function(){ return " + // + source.substring(def.range[0], def.range[1]) + ';}}' + } + } + // we did not find it + return null; + } + + var walk = Walker({ + Function: function(name, args, body) { + // Open this function + if (name === callback) { + throw new Error('Invalid usage of callback on line ' + this.loc.start.line); + } + catchup(this.range[0]); + var idx = getCallback(args, this.loc.start.line), opts; + if (idx !== -1 && this.type === 'FunctionExpression') { + buffer += 'galaxy.unstar('; + opts = getCallbackDefault(args, body) || idx; + ++didRewrite; + } + if (idx !== -1 || this.forceAsync) { + catchup(source.indexOf('function', position)); + buffer += "function*"; + position += 8; + } + catchup(this.body.range[0] + 1); + // keep "use strict"; and similar directives at beginning of block + while (body[0] && body[0].type === 'ExpressionStatement' && body[0].expression.type === 'Literal' && typeof body[0].expression.value === 'string') { + catchup(body[0].range[1]); + body.splice(0, 1); + } + + // Analyze the scope of this function for locals and streamlined functions + // We need locals to avoid renaming collisions with streamlined functions, and the streamlined + // functions let us optimize `invoke`. + var locals = getLocals(this); + var localStreamlined = getStreamlinedDeclarations(this); + var oldScope = scope; + var oldStreamlined = streamlined; + var oldVerboten = verboten; + var oldAsync = async; + async = idx !== -1 || this.forceAsync; + if (async) asyncDepth++; + scope = chain(scope, locals); + streamlined = chain(streamlined, localStreamlined.strict); + verboten = chain(verboten); + for (var ii in locals) { + if (!localStreamlined.strict[ii]) { + streamlined[ii] = false; + } + verboten[ii] = false; + } + if (idx !== -1 && this.type === 'FunctionExpression' && name) { + // Can't use a streamline'd function by name from within that function + verboten[name] = true; + } + + // Hoist streamlined functions + var hoisted = []; + for (var ii in localStreamlined.declared) { + var fragment = '_', + len = 1; + while (scope[ii + fragment] || allIdentifiers[ii + fragment]) { + fragment = Array(++len + 1).join('_'); + } + scope[ii] = ii + fragment; + hoisted.push(ii + fragment + ' = galaxy.unstar(' + ii + ', ' + localStreamlined.declared[ii] + ')'); + ++didRewrite; + } + if (hoisted.length) { + buffer += 'var ' + hoisted.join(', ') + ';'; + } + + // Close up the function + body.map(walk); + catchup(this.range[1]); + if (idx !== -1 && this.type === 'FunctionExpression') { + buffer += ', ' + opts + ')'; + } + + // Reset scopes + scope = oldScope; + streamlined = oldStreamlined; + verboten = oldVerboten; + if (async) asyncDepth--; + async = oldAsync; + }, + CallExpression: function(expr, args) { + if (expr.type === 'Identifier' && expr.name === '_' && args.length === 2) { + catchup(this.range[0]); + buffer += 'galaxy.streamlinify('; + skipTo(args[0].range[0]); + args.map(walk); + catchup(args[1].range[1]); + buffer += ')'; + skipTo(this.range[1]); + ++didRewrite; + return; + } + if (expr.type === 'MemberExpression' && args.length === 2 && args[0].type === 'Identifier' && args[0].name === callback // + && args[1].type === 'Identifier' && args[1].name === callback) { + if (!async) throw error(this.loc.start.line, "Function contains async calls but does not have _ parameter"); + catchup(this.range[0]); + skipTo(expr.object.range[0]); + buffer += '(yield galaxy.then.call(this,'; + walk(expr.object); + catchup(expr.object.range[1]); + buffer += ', "' + expr.property.name + '", _))'; + skipTo(this.range[1]); + ++didRewrite; + return; + } + var idx = getCallback(args); + if (idx !== -1 && !async) throw new Error("Function contains async calls but does not have _ parameter on line " + this.loc.start.line); + if (idx !== -1 && expr.type === 'Identifier' && streamlined[expr.name]) { + // Optimized streamline callback. We know this call is to a streamlined function so we can + // just inline it. + catchup(this.range[0]); + buffer += '(yield '; + if (scope[expr.name] === expr.name) { + // In this case `expr` was declared with a function expression instead of a function + // declaration, so the original function is no longer around. + catchup(expr.range[0]); + buffer += '('; + catchup(expr.range[1]); + buffer += '.__starred__' + idx + ' || 0)'; + } else { + catchup(expr.range[1]); + } + buffer += '('; + for (var ii = 0; ii < args.length; ++ii) { + skipTo(args[ii].range[0]); + if (ii > 0) buffer += ', '; + if (ii !== idx) { + walk(args[ii]); + catchup(args[ii].range[1]); + } else { + buffer += '_'; + skipTo(args[ii].range[1]); + } + } + skipTo(this.range[1]); + buffer += '))'; + } else if (idx !== -1) { + // Rewrite streamlined calls + // issue #108: process between expr.range[0] and last arg end rather than this.range[0]/end + catchup(this.range[0]); + buffer += '(yield galaxy.invoke('; + skipTo(expr.range[0]) + if (expr.type === 'MemberExpression') { + skipTo(expr.object.range[0]); + walk(expr.object); + catchup(expr.object.range[1]); + if (!expr.computed) { + // Method call: foo.bar(_) + buffer += ', ' + JSON.stringify(expr.property.name); + } else { + // Dynamic method call: foo[bar](_) + buffer += ', '; + skipTo(expr.property.range[0]); + walk(expr.property); + catchup(expr.property.range[1]); + } + skipTo(expr.range[1]); + } else { + // Function call + buffer += 'null, '; + walk(expr); + } + catchup(expr.range[1]); + // Render arguments + buffer += ', ['; + skipTo(args[0].range[0]); + for (var ii = 0; ii < args.length; ++ii) { + skipTo(args[ii].range[0]); + if (ii > 0) buffer += ', '; + if (ii !== idx) { + walk(args[ii]); + catchup(args[ii].range[1]); + } else { + buffer += '_'; + skipTo(args[ii].range[1]); + } + } + var options = idx; + if (args[idx].type === 'ArrayExpression') options = '{ callbackIndex: ' + idx + ', returnArray: true }'; + buffer += '], ' + options + '))'; + skipTo(this.range[1]); + ++didRewrite; + } else { + var paren = 0, forceAsync = false; + if (source[this.range[0]] === '(' && source[this.range[0] + 1] === '(' && + source[this.range[1] - 1] === ')' && source[this.range[1] - 2] === ')') { + paren = 1; + } + if (async && asyncDepth > 1 && startsWith(source, this.range[0] + paren, '(function() {')) { + // handle coffeescript wrappers: set the forceAsync flag + // so that we don't get an error about _ being used inside non async function + if (endsWith(source, this.range[1] - paren, '})()')) { + expr.forceAsync = forceAsync = true; + } + if (endsWith(source, this.range[1] - paren, '}).call(this)') // + || endsWith(source, this.range[1] - paren, '}).call(_this)') // + || endsWith(source, this.range[1] - paren, '}).apply(this, arguments)')) { + expr.object.forceAsync = forceAsync = true; + } + } + if (forceAsync) { + catchup(this.range[0]); + buffer += '(yield '; + walk(expr); + args.map(walk); + catchup(this.range[1]); + buffer += ')'; + } else { + walk(expr); + args.map(walk); + } + } + }, + Identifier: function(name) { + if (name === callback) { + throw new Error('Invalid usage of callback on line ' + this.loc.start.line); + } else if (verboten[name]) { + throw new Error('Invalid use of indentifier `' + name + '` on line ' + this.loc.start.line); + } + if (scope[name]) { + catchup(this.range[0]); + buffer += scope[name]; + skipTo(this.range[1]); + } else { + // catchup to end will deal with all sort of oddities, like object initializer keys that are + // parsed as identifiers but need to be quoted. + catchup(this.range[1]); + } + }, + Property: function() { + // Dont't walk the property key, because that's an identifier and it will be clobbered, per + // the below code + walk(this.value); + }, + MemberExpression: function() { + // See comment above for propery_init + walk(this.object); + if (this.computed) walk(this.property) + }, + NewExpression: function(expr, args) { + var idx = getCallback(args); + if (idx !== -1) { + // assumes that this is a streamlined function! + catchup(this.range[0]); + skipTo(expr.range[0]); // skip new keyword + buffer += "(yield galaxy.new(galaxy.star(" + walk(expr); + catchup(expr.range[1]); + buffer += "," + idx + "), " + idx + ")("; + // process arguments to avoid 'invalid usage of callback' error + for (var ii = 0; ii < args.length; ++ii) { + skipTo(args[ii].range[0]); + if (ii > 0) buffer += ', '; + if (ii !== idx) { + walk(args[ii]); + catchup(args[ii].range[1]); + } else { + buffer += '_'; + skipTo(args[ii].range[1]); + } + } + buffer += "))"; + skipTo(this.range[1]); + } else { + walk(expr); + args.map(walk); + } + }, + ReturnStatement: function(argument) { + argument && walk(argument); + fixASI(this); + }, + ThrowStatement: function(argument) { + argument && walk(argument); + fixASI(this); + }, + YieldStatement: function(argument) { + argument && walk(argument); + fixASI(this); + }, + UnaryExpression: function(operator, argument) { + if (operator === '!' || operator === 'void') { + if (argument.type === 'Identifier' && argument.name === callback) { + catchup(this.range[0]); + buffer += operator === '!' ? 'false' : 'null'; + skipTo(this.range[1]); + } else { + walk(argument); + } + } else { + walk(argument); + } + }, + TryStatement: function(block, handlers, finalizer) { + walk(block); + handlers.map(walk); + finalizer && walk(finalizer); + }, + ExpressionStatement: function(expression) { + expression && walk(expression); + fixASI(this); + }, + BinaryExpression: function(operator, left, right) { + if (operator === '<<' || operator === '>>') { + walkShift.call(this, left, right); + } else { + walk(left); + walk(right); + } + }, + VariableDeclaration: function(declarations) { + declarations && declarations.map(walk); + if (this.eligibleForASI) fixASI(this); + }, + }); + + // take care of ASI, in case transformation parenthesized next statement + function fixASI(node) { + catchup(node.range[1]); + if (buffer[buffer.length - 1] !== ';') buffer += ';' + } + + function walkShift(left, right) { + if (left.type === 'Identifier' && left.name === callback) { + catchup(left.range[0]); + skipTo(source.indexOf(this.operator, left.range[1]) + 2); + + walk(right); + ++didRewrite; + } else { + walk(left); + walk(right); + } + } + + // Walk parsed source, rendering along the way + var originalSource = source; + var optStr = "(function(){"; + if (options.promise) { + var arg = typeof options.promise === "string" ? "'" + options.promise + "'" : "true"; + optStr += "galaxy.globals.setPromise(" + arg + ");"; + } + optStr += "})();"; + source = 'var galaxy = require("' + (options.internal ? '..' : 'streamline/lib') + '/generators/runtime");' + optStr + '(function(_) { ' + // + source + // + '\n}.call(this, function(err) {\n' + // + ' if (err) throw err;\n' + // + '}));\n'; + var parsed = esprima.parse(source, { + loc: true, + range: true, + }); + allIdentifiers = getLocals(parsed.body[2].expression.callee.object, true); + walk(parsed); + buffer += source.substring(position); + + if (didRewrite > 0) { + return buffer; + } else { + return originalSource; + } + } +})(typeof exports !== 'undefined' ? exports : (window.Streamline = window.Streamline || {})); +"use strict"; +/** + * Copyright (c) 2013 Bruno Jouhier + * MIT License + */ +(function(exports) { + + /// + /// # Main galaxy module + /// + /// `var galaxy = require('galaxy');` + /// + + var resume; + + function link(src, name, dst) { + Object.defineProperty(src, name, { + configurable: false, + writable: true, + enumerable: false, + value: dst + }); + return dst; + } + + + function makeArgs(i) { + if (i <= 0) return ""; + return i > 1 ? makeArgs(i - 1) + ', a' + i : "a1"; + } + + // glob is the hidden global in which we store the context. + // it must be unique, even if we have several versions of the galaxy module. + var glob = (function() { + var gl = typeof global === "object" ? global : window; + // share the secret with streamline's global module. + var secret = "_20c7abceb95c4eb88b7ca1895b1170d1"; + return gl[secret] || link(gl, secret, { context: {} }); + })(); + + var stackHelper, stackHelperError; + try { + stackHelper = require('galaxy-stack'); + } catch (ex) { + stackHelperError = ex.message; + } + + function future(fn, args, i) { + var err, result, done, q = [], + self = this; + args = Array.prototype.slice.call(args); + args[i] = function(e, r) { + //if (arguments.length > 2) r = Array.prototype.slice.call(arguments, 1); + err = e, result = r, done = true; + q && q.forEach(function(f) { + f.call(self, e, r); + }); + q = null; + }; + fn.apply(this, args); + return function F(cb) { + if (typeof cb !== 'function') { + if (cb !== false) throw new Error("invalid argument #0: you must pass _ or !_"); + return F; + } + if (done) cb.call(self, err, result); + else q.push(cb); + } + } + + + function isGenerator(val) { + return Object.prototype.toString.call(val) === "[object Generator]"; + } + + var PENDING = {}; + + function Frame(g) { + this.g = g; + this.prev = glob.frame; + g.frame = this; + this._info = null; + this.recurse = 0; + this.yielded = 0; + } + + Object.defineProperty(Frame.prototype, "info", { + get: function() { + return this._info || stackHelper.getStackFrame(this.g); + } + }); + + Object.defineProperty(Frame.prototype, "name", { + get: function() { return this.info.functionName; } + }); + + Object.defineProperty(Frame.prototype, "file", { + get: function() { return this.info.scriptName; } + }); + + Object.defineProperty(Frame.prototype, "line", { + get: function() { return this.info.lineNumber; } + }); + + function pushFrame(g) { + glob.frame = g.frame = g.frame || new Frame(g); + glob.emitter.emit('enter', g.frame); + } + + function popFrame(g) { + if (!glob.frame) return; + glob.emitter.emit('exit', g.frame); + glob.frame = glob.frame.prev; + } + + var yielded = true; + + function run(g, cb, options) { + var rsm = resume; + var emit = glob.emitter && stackHelper && function(ev, g) { + g.frame = g.frame || new Frame(g); + glob.emitter.emit(ev, g.frame); + } + + try { + resume = function(err, val) { + if (emit && yielded) { + emit("resume", g); + yielded = false; + } + while (g) { + if (options && options.interrupt && options.interrupt()) return; + try { + // ES6 is deprecating send in favor of next. Following line makes us compatible with both. + var send = g.send || g.next; + var v = err ? g.throw (err) : send.call(g, val); + val = v.value; + err = null; + // if we get PENDING, the current call completed with a pending I/O + // resume will be called again when the I/O completes. So just save the context and return here. + if (val === PENDING) { + if (emit && !yielded) { + emit("yield", g); + yielded = true; + } + return; + } + // if we get [PENDING, e, r], the current call invoked its callback synchronously + // we just loop to send/throw what the callback gave us. + if (val && val[0] === PENDING) { + err = val[1]; + val = val[2]; + if (err) err = wrapError(err, g, resume); + } + // else, if g is done we unwind it we send val to the parent generator (or through cb if we are at the top) + else if (v.done) { + //g.close(); + if (emit) popFrame(g); + g = g.prev; + } + // else if val is not a generator we have an error. Yield was not applied to a generators + else { + if (!isGenerator(val)) { + // but we accept an array of generators, and we parallelize in this case + if (!Array.isArray(val)) throw new Error("invalid value was yielded. Expected a generator, got " + val); + val = val.mapStar(-1, function*(elt) { + if (!isGenerator(elt)) throw new Error("invalid array element was yielded. Expected a generator, got " + elt); + return yield elt; + }); + } + // we got a new generator which means that g called another generator function + // the new generator become current and we loop with g.send(undefined) (equiv to g.next()) + val.prev = g; + g = val; + if (emit) pushFrame(g); + val = undefined; + } + } catch (ex) { + // the send/throw call failed. + // we unwind the current generator and we rethrow into the parent generator (or through cb if at the top) + //g.close(); + err = wrapError(ex, g, resume); + if (emit) popFrame(g); + g = g.prev; + val = undefined; + } + } + // we have exhausted the stack of generators. + // return the result or error through the callback. + cb(err, val); + } + + // start the resume loop + resume(); + } finally { + // restore resume global + resume = rsm; + } + } + + function mapResults(options, args) { + if (options && typeof options === "object") { + if (options.returnArray) return args; + if (options.returnObject) return options.returnObject.reduce(function(res, key, i) { + res[key] = args[i]; + return res; + }, {}); + } + return args[0]; + } + + function getTag(options, idx) { + if (options && typeof options === "object") { + if (options.returnArray) return "A" + idx; + if (options.returnObject) return "O" + options.returnObject.join('/') + idx; + } + return idx; + } + + function invoke(that, fn, args, idx, options) { + if (fn['__unstarred__' + idx]) throw new Error("cannot invoke starred function: " + fn['__unstarred__' + idx]); + // Set things up so that call returns: + // * PENDING if it completes with a pending I/O (and cb will be called later) + // * [PENDING, e, r] if the callback is called synchronously. + var result = PENDING, + sync = true; + var rsm = resume; + + // convert args to array so that args.length gets correctly set if idx is args.length + args = Array.prototype.slice.call(args, 0); + var cx = glob.context; + args[idx == null ? args.length : idx] = function(e, r) { + var oldContext = glob.context; + var oldResume = resume; + try { + if (options) r = mapResults(options, Array.prototype.slice.call(arguments, 1)); + glob.context = cx; + resume = rsm; + if (sync) { + result = [PENDING, e, r]; + } else { + resume(e, r); + } + } finally { + glob.context = oldContext; + resume = oldResume; + } + } + fn.apply(that, args); + sync = false; + return result; + } + + function convertAPI(converter, api, idx) { + if (typeof idx === 'string') { + var fn = api[idx]; + if (typeof fn !== 'function') throw new Error("not a function: " + idx); + return converter(fn.bind(api)); + } + return Object.keys(api).reduce(function(result, key) { + var fn = api[key]; + result[key] = (typeof fn === 'function' && !/Sync$/.test(fn.name)) ? converter(fn, idx) : fn; + return result; + }, {}); + } + + /// + /// ## API wrappers + /// + /// * `var genFn = galaxy.star(asyncFn, cbIndex)` + /// This function turns an asynchronous function into a generator function. + /// `asyncFn` is the asynchronous function. + /// `cbIndex` is the index of the callback parameter. It is optional. + /// If omitted the callback is assumed to be the last parameter of `asyncFn`. + var starTemplate = function star(fn, options) { + if (typeof fn === 'object') return convertAPI(exports.star, fn, options); + + var idx = (options && typeof options === 'object') ? options.callbackIndex : options; + var idx2 = idx < 0 ? -(idx + 1) : idx; + var tag = getTag(options, idx); + + var key = '__starred__' + tag; + if (fn[key]) return fn[key]; + + //if (idx == null) idx = fn.length - 1; + + var F = function *() { + if (idx < 0) Array.prototype.splice.call(arguments, idx2, 0, null); + return (yield invoke(this, fn, arguments, idx2, options)); + }; + link(F, '__unstarred__' + tag, fn); + link(fn, key, F); + return F; + } + + var starBody = starTemplate.toString(); + starBody = starBody.substring(starBody.indexOf('{')); + var starrors = []; + + function makeStarror(i) { + return eval("(function(fn, options)" + starBody.replace(/function\s*\*\s*\(\)/, "function*(" + makeArgs(i) + ")") + ")"); + } + + exports.star = function(fn, idx) { + var i = fn.length; + var starror = starrors[i] || (starrors[i] = makeStarror(i)); + return starror(fn, idx); + } + + /// * `var asyncFn = galaxy.unstar(genFn, cbIndex)` + /// This function converts in the other direction. It allows you to turn a generator function into an asynchronous function. + /// `genFn` is the generator function. + /// `cbIndex` is the index of the callback parameter. It is optional. If omitted the callback is added at the end of the parameter list of `genFn`. + /// + /// As previously mentioned these calls may also be applied to a whole module, or to any object containing functions. + /// `Sync` calls are skipped. + // entering is undocumented streamline parameter for future. + var unstarTemplate = function unstar(fn, options, entering) { + if (typeof fn === 'object') return convertAPI(exports.unstar, fn, options); + + var idx = (options && typeof options === 'object') ? options.callbackIndex : options; + if (idx == null) idx = fn.length; + var idx2 = idx < 0 ? -(idx + 1) : idx; + + var key = '__unstarred__' + idx; + if (fn[key]) return fn[key]; + + var F = function() { + var cb = arguments[idx2]; + if (idx < 0) Array.prototype.splice.call(arguments, idx2, 1); + // preserve streamline future semantics in unstarred space. + if (typeof cb !== 'function') { + if (entering && !cb) cb = arguments[idx2] = function(err) { if (err) throw err; }; + return ((options && options.promise) || future).call(this, F, arguments, idx2); + } + var g = fn.apply(this, arguments); + run.call(this, g, cb); + }; + link(F, '__starred__' + idx, fn); + link(fn, key, F); + return F; + } + + var unstarBody = unstarTemplate.toString(); + unstarBody = unstarBody.substring(unstarBody.indexOf('{')); + var unstarrors = []; + + function makeUnstarror(i) { + return eval("(function(fn, options, entering)" + unstarBody.replace(/function\s*\(\)/, "function(" + makeArgs(i) + ")") + ")"); + } + + exports.unstar = function(fn, idx, entering) { + var i = idx == null ? fn.length + 1 : fn.length; + var unstarror = unstarrors[i] || (unstarrors[i] = makeUnstarror(i)); + return unstarror(fn, idx, entering); + } + + + /// + /// ## Parallelizing + /// + /// * `var genFn = galaxy.spin(generator)` + /// Start spinning a generator that you obtained by calling a starred function (without yield). + /// The generator will execute in parallel with other code, at the points where the code yields. + /// The returned value is a generator function on which you can yield later to obtain the result of the computation. + exports.spin = function(g, options) { + if (!isGenerator(g)) throw new Error("Invalid spin call, expected a generator, got " + g); + var that = this; + // use starTemplate rather than galaxy.star because future has arity of 1 and we want arity of 0 + return starTemplate(future(function(cb) { + run.call(that, g, cb, options); + }, [], 0), 0); + } + + /// * `fun = galaxy.funnel(max)` + /// limits the number of concurrent executions of a given code block. + /// + /// The `funnel` function is typically used with the following pattern: + /// + /// ``` javascript + /// // somewhere + /// var myFunnel = galaxy.funnel(10); // create a funnel that only allows 10 concurrent executions. + /// + /// // elsewhere + /// var result = yield myFunnel(function* () { /* code with at most 10 concurrent executions */ }); + /// ``` + /// + /// The `funnel` function can also be used to implement critical sections. Just set funnel's `max` parameter to 1. + /// + /// If `max` is set to 0, a default number of parallel executions is allowed. + /// This default number can be read and set via `galaxy.funnel.defaultSize`. + /// If `max` is negative, the funnel does not limit the level of parallelism. + /// + /// The funnel can be closed with `fun.close()`. + /// When a funnel is closed, the operations that are still in the funnel will continue but their callbacks + /// won't be called, and no other operation will enter the funnel. + exports.funnel = function(max) { + max = max == null ? -1 : max; + if (max === 0) max = funnel.defaultSize; + if (typeof max !== "number") throw new Error("bad max number: " + max); + var queue = [], + active = 0, + closed = false; + + var fun = exports.star(function(fnStar, callback) { + var fn = exports.unstar(fnStar); + if (max < 0 || max == Infinity) return fn(callback); + + queue.push({ + fn: fn, + cb: callback + }); + + function _doOne() { + var current = queue.splice(0, 1)[0]; + if (!current.cb) return current.fn(); + active++; + current.fn(function(err, result) { + active--; + if (!closed) { + current.cb(err, result); + while (active < max && queue.length > 0) _doOne(); + } + }); + } + + while (active < max && queue.length > 0) _doOne(); + }); + fun.close = function() { + queue = [], closed = true; + } + return fun; + } + + exports.funnel.defaultSize = 4; + + /// + /// ## Stable context (TLS-like) + /// + /// * `galaxy.context = ctx` + /// `ctx = galaxy.context` + /// Sets and gets the stable context. + Object.defineProperty(exports, 'context', { + set: function(val) { + glob.context = val; + }, + get: function() { + return glob.context; + } + }); + + /// + /// ## Miscellaneous + /// + /// * `var genCreate = galaxy.new(genConstructor)` + /// Converts a constructor generator function to a _creator_ function. + /// `genConstructor` is a _starred_ constructor that may contain `yield` calls. + /// The returned `genCreate` is a _starred_ function that you can call as `yield genCreate(args)` + + // undocumented idx is set by streamline because prototype is on unstarred function in this case + exports.new = function(constructor, idx) { + var key = '__new__' + idx; + if (constructor[key]) return constructor[key]; + + var F = function *() { + var that = Object.create((idx != null ? constructor['__unstarred__' + idx] : constructor).prototype); + yield constructor.apply(that, arguments); + return that; + }; + link(constructor, key, F); + return F; + } + + // undocumented helper for streamline compat + exports.invoke = function *(that, fn, args, options) { + var idx = (options && typeof options === 'object') ? options.callbackIndex : options; + var tag = getTag(options, idx); + if (typeof fn !== 'function') { + if (typeof that === 'function' && that['__starred__' + tag] && fn === 'call') { + return yield that['__starred__' + tag].apply(args[0], args.slice(1)); + } + fn = that[fn]; + } + return yield exports.star(fn, options).apply(that, args); + } + + function wrapError(err, g, resume) { + if (!(err instanceof Error)) return err; // handle throw "some string"; + if (err.__async__ && err.__async__.resume === resume) return err; + var continuations = []; + g = g.prev; // first frame will be in err.stack + if (stackHelper) { + for (var gg = g; gg; gg = gg.prev) { + continuations.push(stackHelper.getContinuation(gg)); + } + } + err = Object.create(err); + Object.defineProperty(err, 'stack', { + get: function() { + return stackTrace(this); + } + }); + link(err, "__async__", { + continuations: continuations, + generator: g, + resume: resume, + }); + return err; + } + + function stackTrace(err) { + var extra; + var starredStack = ""; + while (extra = err.__async__) { + if (stackHelper) { + var frames = []; + for (var g = extra.generator; g; g = g.prev) { + var frame = stackHelper.getStackFrame(g, extra.continuations[frames.length]); + frames.push(frame); + } + starredStack = frames.filter(function(frame){ + return !(frame.functionName === "exports.invoke" && /\/galaxy\/lib\/galaxy/.test(frame.scriptName)); + }).map(function(frame) { + return ' at ' + frame.functionName + ' (' + frame.scriptName + ':' + frame.lineNumber + ':' + frame.column + ')'; + }).join('\n') + '\n' + starredStack; + } + err = Object.getPrototypeOf(err); + } + if (!stackHelper) starredStack = ' UNAVAILABLE: ' + stackHelperError + '\n'; + + var rawStack = err.stack; + var cut = rawStack.indexOf(' at GeneratorFunctionPrototype'); + if (cut < 0) cut = rawStack.indexOf('\n') + 1; + var result = rawStack.substring(0, cut) + // + ' <<< yield stack >>>\n' + starredStack + // + ' <<< raw stack >>>\n' + rawStack.substring(cut); + return result; + } + + /// * `galaxy.main(function*() { ... })` + /// Wrapper for a main asynchronous script. + /// See the [tutorial](../tutorial/tutorial.md) for an example + exports.main = function(fn) { + var that = this; + exports.unstar(function*() { + yield fn.call(that); + })(function(err) { + if (err) throw err; + }); + } +})(typeof exports !== 'undefined' ? exports : (window.galaxy = window.galaxy || {})); +/// +/// # Container for global context +/// +/// The `globals` module is a container for the global `context` object which is maintained across +/// asynchronous calls. +/// +/// This context is very handy to store information that all calls should be able to access +/// but that you don't want to pass explicitly via function parameters. The most obvious example is +/// the `locale` that each request may set differently and that your low level libraries should +/// be able to retrieve to format messages. +/// +/// `var globals = require('streamline/lib/globals')` +/// +/// * `globals.context = ctx` +/// * `ctx = globals.context` +/// sets and gets the context +/// +/// Note: an empty context (`{}`) is automatically set by the server wrappers of the `streams` module, +/// before they dispatch a request. So, with these wrappers, each request starts with a fresh empty context. +// This module may be loaded several times so we need a true global (with a secret name!). +// This implementation also allows us to share the context between modules compiled in callback and fibers mode. +(function() { + var glob = typeof global === "object" ? global : window; + var secret = "_20c7abceb95c4eb88b7ca1895b1170d1"; + var g = glob[secret] || (glob[secret] = { context: {} }); + if (typeof exports !== 'undefined') { + module.exports = g; + } else { + Streamline.globals = g; + } + // Internal call to manage runtimes + g.runtime || Object.defineProperty(g, 'runtime', { + get: function() { return g.__runtime__; }, + set: function(value) { + if (g.__runtime__ !== value) { + if (g.__runtime__) { + if (/-fast$/.test(g.__runtime__) || + /-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value); + console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value); + } + g.__runtime__ = value; + } + } + }); + + /// + /// * `fn = globals.withContext(fn, cx)` + /// wraps a function so that it executes with context `cx` (or with a new empty context if `cx` is falsy). + /// The previous context will be restored when the function returns (or throws). + /// returns the wrapped function. + g.withContext = function(fn, cx) { + return function() { + var oldContext = g.context; + g.context = cx || {}; + try { + return fn.apply(this, arguments) + } finally { + g.context = oldContext; + } + }; + }; + + g.setPromise = function(name) { + if (g.Promise) return; // first caller wins + var req = require; // defeat streamline-require dependencies + if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise'); + else g.Promise = require(name); + }; +})(); + +/** + * Copyright (c) 2011 Bruno Jouhier + * MIT License + */ +(function(exports) { + var globals = require("../globals"); + + exports.future = function(fn, args, i) { + var err, result, done, q = [], self = this; + args = Array.prototype.slice.call(args); + args[i] = function(e, r) { + err = e, result = r, done = true; + q && q.forEach(function(f) { + f.call(self, e, r); + }); + q = null; + }; + args[i].__futurecb = true; + fn.apply(this, args); + var ret = function F(cb) { + if (typeof cb !== 'function') { + var globals = require('../globals'); + if (cb == null && globals.Promise) return exports.promise.call(this, F, [], 0); + if (cb !== false && !globals.oldStyleFutures) throw new Error("callback missing (argument #0). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error"); + return F; + } + if (done) cb.call(self, err, result); + else q.push(cb); + }; + ret.__future = true; + return ret; + }; + + exports.streamlinify = function(fn, idx) { + return function() { + if (!arguments[idx]) return exports.future.call(this, fn, arguments, idx); + else return fn.apply(this, arguments); + }; + }; + + exports.promise = function(fn, args, i) { + if (args[i] === false) return exports.future.call(this, fn, args, i); + if (args[i] != null) throw new Error("invalid callback: " + typeof(args[i])); + if (globals.oldStyleFutures) return exports.future.call(this, fn, args, i); + if (!globals.Promise) throw new Error("callback missing (argument #" + i + "). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error"); + + var self = this; + args = Array.prototype.slice.call(args); + return new globals.Promise(function(resolve, reject) { + args[i] = function(e, r) { + if (e) reject(e); + else resolve(r); + }; + fn.apply(self, args); + }); + }; + + exports.then = function(promise, method, cb) { + promise[method](function(r) { + cb && cb(null, r); + cb = null; + }, function(e) { + cb && cb(e); + cb = null; + }); + }; +})(typeof exports !== 'undefined' ? exports : (Streamline.future = Streamline.future || {})); + +(function(exports) { + var globals = exports.globals = require('../globals'); + exports.globals.runtime = 'generators'; + require("../generators/builtins"); + var fut = require("../util/future"); + exports.streamlinify = fut.streamlinify; + + var unstar = exports.unstar; + + exports.unstar = function(fn, options, entering) { + if (typeof options === "number") options = { + callbackIndex: options, + }; + else options = options || {}; + options.promise = options.callbackDefault ? function(fn, args, i) { + return fut.future.call(this, fn, args, i)(options.callbackDefault()); + } : fut.promise; + return unstar(fn, options, entering); + } + + exports.then = exports.star(fut.then, 2); +})(typeof exports !== 'undefined' ? module.exports = Object.create(require('galaxy')) : Streamline.runtime = Streamline.runtime || Object.create(require('galaxy'))); +/*** Generated by streamline 0.10.17 (generators) - DO NOT EDIT ***/var galaxy = require("../generators/runtime");(function(){})();(galaxy.unstar(function*(_) { /** + * Copyright (c) 2012 Bruno Jouhier + * MIT License + */ + +/// +/// # Streamline built-ins +/// +(function(exports) { + "use strict"; + var VERSION = 3; + + + + var future = function(fn, args, i) { + var err, result, done, q = [], + self = this; + args = Array.prototype.slice.call(args); + args[i] = function(e, r) { + err = e, result = r, done = true; + q && q.forEach(function(f) { + f.call(self, e, r); + }); + q = null; + }; + fn.apply(this, args); + return function F(cb) { + if (!cb) return F; + if (done) cb.call(self, err, result); + else q.push(cb); + }; + }; + + // Do not use this one directly, require it through the flows module. + exports.funnel = function(max) { + max = max == null ? -1 : max; + if (max === 0) max = funnel.defaultSize; + if (typeof max !== "number") throw new Error("bad max number: " + max); + var queue = [], + active = 0, + closed = false; + + var funCb = function(callback, fn) { + if (callback == null) return future(funCb, arguments, 0); + //console.log("FUNNEL: active=" + active + ", queued=" + queue.length); + if (max < 0 || max == Infinity) return fn( callback); + + queue.push({ + fn: fn, + cb: callback + }); + + function _doOne() { + var current = queue.splice(0, 1)[0]; + if (!current.cb) return current.fn(); + active++; + current.fn( function(err, result) { + active--; + if (!closed) { + current.cb(err, result); + while (active < max && queue.length > 0) _doOne(); + } + }); + } + + while (active < max && queue.length > 0) _doOne(); + }; + var fun = galaxy.streamlinify(funCb, 0); + + fun.close = function() { + queue = [], closed = true; + }; + return fun; + }; + var funnel = exports.funnel; + funnel.defaultSize = 4; + + function _parallel(options) { + if (typeof options === "number") return options; + if (typeof options.parallel === "number") return options.parallel; + return options.parallel ? -1 : 1; + } + + if (Array.prototype.forEach_ && Array.prototype.forEach_.version_ >= VERSION) return; + + // bail out (silently) if JS does not support defineProperty (IE 8). + try { + Object.defineProperty({}, 'x', {}); + } catch (e) { + return; + } + + var has = Object.prototype.hasOwnProperty; + + /// ## Array functions + /// + /// These functions are asynchronous variants of the EcmaScript 5 Array functions. + /// + /// Common Rules: + /// + /// These variants are postfixed by an underscore. + /// They take the `_` callback as first parameter. + /// They pass the `_` callback as first argument to their `fn` callback. + /// Most of them have an optional `options` second parameter which controls the level of + /// parallelism. This `options` parameter may be specified either as `{ parallel: par }` + /// where `par` is an integer, or directly as a `par` integer value. + /// The `par` values are interpreted as follows: + /// + /// * If absent or equal to 1, execution is sequential. + /// * If > 1, at most `par` operations are parallelized. + /// * if 0, a default number of operations are parallelized. + /// This default is defined by `flows.funnel.defaultSize` (4 by default - see `flows` module). + /// * If < 0 or Infinity, operations are fully parallelized (no limit). + /// + /// Functions: + /// + /// * `array.forEach_(_[, options], fn[, thisObj])` + /// `fn` is called as `fn(_, elt, i)`. + delete Array.prototype.forEach_; + Object.defineProperty(Array.prototype, 'forEach_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, options, fn, thisObj) { + if (typeof options === "function") thisObj = fn, fn = options, options = 1; + var par = _parallel(options); + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + if (par === 1 || len <= 1) { + for (var i = 0; i < len; i++) { + if (has.call(this, i)) (yield galaxy.invoke(fn, "call", [thisObj , _ , this[i] , i], 1)); + } + } else { + (yield galaxy.invoke(this, "map_", [_ , par , fn , thisObj], 0)); + } + return this; + }, 0) + }); + Array.prototype.forEach_.version_ = VERSION; + /// * `result = array.map_(_[, options], fn[, thisObj])` + /// `fn` is called as `fn(_, elt, i)`. + delete Array.prototype.map_; + Object.defineProperty(Array.prototype, 'map_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, options, fn, thisObj) { + if (typeof options === "function") thisObj = fn, fn = options, options = 1; + var par = _parallel(options); + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + var result; + if (par === 1 || len <= 1) { + result = new Array(len); + for (var i = 0; i < len; i++) { + if (has.call(this, i)) result[i] = (yield galaxy.invoke(fn, "call", [thisObj , _ , this[i] , i], 1)); + } + } else { + var fun = funnel(par); + result = this.map(function(elt, i) { + return fun(false, galaxy.unstar(function*(_) { + return (yield galaxy.invoke(fn, "call", [thisObj , _ , elt , i], 1)); + }, 0)); + }); + for (var i = 0; i < len; i++) { + if (has.call(this, i)) result[i] = (yield galaxy.invoke(result, i, [_], 0)); + } + } + return result; + }, 0) + }); + /// * `result = array.filter_(_[, options], fn[, thisObj])` + /// `fn` is called as `fn(_, elt)`. + delete Array.prototype.filter_; + Object.defineProperty(Array.prototype, 'filter_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, options, fn, thisObj) { + if (typeof options === "function") thisObj = fn, fn = options, options = 1; + var par = _parallel(options); + thisObj = thisObj !== undefined ? thisObj : this; + var result = []; + var len = this.length; + if (par === 1 || len <= 1) { + for (var i = 0; i < len; i++) { + if (has.call(this, i)) { + var elt = this[i]; + if ((yield galaxy.invoke(fn, "call", [thisObj , _ , elt], 1))) result.push(elt); + } + } + } else { + (yield galaxy.invoke(this, "map_", [_ , par , galaxy.unstar(function*(_, elt) { + if ((yield galaxy.invoke(fn, "call", [thisObj , _ , elt], 1))) result.push(elt); + }, 0) , thisObj], 0)); + } + return result; + }, 0) + }); + /// * `bool = array.every_(_[, options], fn[, thisObj])` + /// `fn` is called as `fn(_, elt)`. + delete Array.prototype.every_; + Object.defineProperty(Array.prototype, 'every_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, options, fn, thisObj) { + if (typeof options === "function") thisObj = fn, fn = options, options = 1; + var par = _parallel(options); + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + if (par === 1 || len <= 1) { + for (var i = 0; i < len; i++) { + + if (has.call(this, i) && !(yield galaxy.invoke(fn, "call", [thisObj , _ , this[i]], 1))) return false; + } + } else { + var fun = funnel(par); + var futures = this.map(function(elt) { + return fun(false, galaxy.unstar(function*(_) { + return (yield galaxy.invoke(fn, "call", [thisObj , _ , elt], 1)); + }, 0)); + }); + for (var i = 0; i < len; i++) { + if (has.call(this, i) && !(yield galaxy.invoke(futures, i, [_], 0))) { + fun.close(); + return false; + } + } + } + return true; + }, 0) + }); + /// * `bool = array.some_(_[, options], fn[, thisObj])` + /// `fn` is called as `fn(_, elt)`. + delete Array.prototype.some_; + Object.defineProperty(Array.prototype, 'some_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, options, fn, thisObj) { + if (typeof options === "function") thisObj = fn, fn = options, options = 1; + var par = _parallel(options); + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + if (par === 1 || len <= 1) { + for (var i = 0; i < len; i++) { + if (has.call(this, i) && (yield galaxy.invoke(fn, "call", [thisObj , _ , this[i]], 1))) return true; + } + } else { + var fun = funnel(par); + var futures = this.map(function(elt) { + return fun(false, galaxy.unstar(function*(_) { + return (yield galaxy.invoke(fn, "call", [thisObj , _ , elt], 1)); + }, 0)); + }); + for (var i = 0; i < len; i++) { + if (has.call(this, i) && (yield galaxy.invoke(futures, i, [_], 0))) { + fun.close(); + return true; + } + } + } + return false; + }, 0) + }); + /// * `result = array.reduce_(_, fn, val[, thisObj])` + /// `fn` is called as `val = fn(_, val, elt, i, array)`. + delete Array.prototype.reduce_; + Object.defineProperty(Array.prototype, 'reduce_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, fn, v, thisObj) { + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + for (var i = 0; i < len; i++) { + if (has.call(this, i)) v = (yield galaxy.invoke(fn, "call", [thisObj , _ , v , this[i] , i , this], 1)); + } + return v; + }, 0) + }); + /// * `result = array.reduceRight_(_, fn, val[, thisObj])` + /// `fn` is called as `val = fn(_, val, elt, i, array)`. + delete Array.prototype.reduceRight_; + Object.defineProperty(Array.prototype, 'reduceRight_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, fn, v, thisObj) { + thisObj = thisObj !== undefined ? thisObj : this; + var len = this.length; + for (var i = len - 1; i >= 0; i--) { + if (has.call(this, i)) v = (yield galaxy.invoke(fn, "call", [thisObj , _ , v , this[i] , i , this], 1)); + } + return v; + }, 0) + }); + + /// * `array = array.sort_(_, compare [, beg [, end]])` + /// `compare` is called as `cmp = compare(_, elt1, elt2)`. + /// Note: this function _changes_ the original array (and returns it). + delete Array.prototype.sort_; + Object.defineProperty(Array.prototype, 'sort_', { + configurable: true, + writable: true, + enumerable: false, + value: galaxy.unstar(function*(_, compare, beg, end) {var _qsort_ = galaxy.unstar(_qsort, 0); + var array = this; + beg = beg || 0; + end = end == null ? array.length - 1 : end; + + function* _qsort(_, beg, end) { + if (beg >= end) return; + + if (end == beg + 1) { + if ((yield galaxy.invoke(null, compare, [_ , array[beg] , array[end]], 0)) > 0) { + var tmp = array[beg]; + array[beg] = array[end]; + array[end] = tmp; + } + return; + } + + var mid = Math.floor((beg + end) / 2); + var o = array[mid]; + var nbeg = beg; + var nend = end; + + while (nbeg <= nend) { + while (nbeg < end && (yield galaxy.invoke(null, compare, [_ , array[nbeg] , o], 0)) < 0) nbeg++; + while (beg < nend && (yield galaxy.invoke(null, compare, [_ , o , array[nend]], 0)) < 0) nend--; + + if (nbeg <= nend) { + var tmp = array[nbeg]; + array[nbeg] = array[nend]; + array[nend] = tmp; + nbeg++; + nend--; + } + } + + if (nbeg < end) (yield _qsort(_ , nbeg , end)); + if (beg < nend) (yield _qsort(_ , beg , nend)); + } + (yield _qsort(_ , beg , end)); + return array; + }, 0) + }); + + /// + /// ## Function functions + /// + /// * `result = fn.apply_(_, thisObj, args[, index])` + /// Helper to use `Function.prototype.apply` inside streamlined functions. + /// Equivalent to `result = fn.apply(thisObj, argsWith_)` where `argsWith_` is + /// a modified `args` in which the callback has been inserted at `index` + /// (at the end of the argument list if `index` is omitted or negative). + delete Function.prototype.apply_; + Object.defineProperty(Function.prototype, 'apply_', { + configurable: true, + writable: true, + enumerable: false, + value: function(callback, thisObj, args, index) { + args = Array.prototype.slice.call(args, 0); + args.splice(index != null && index >= 0 ? index : args.length, 0, callback); + return this.apply(thisObj, args); + } + }); +})(typeof exports !== 'undefined' ? exports : (Streamline.builtins = Streamline.builtins || {})); +}, 0).call(this, function(err) { + if (err) throw err; +})); diff --git a/lib/globals.js b/lib/globals.js index 040bc2fa..e32f6c48 100644 --- a/lib/globals.js +++ b/lib/globals.js @@ -20,47 +20,52 @@ /// before they dispatch a request. So, with these wrappers, each request starts with a fresh empty context. // This module may be loaded several times so we need a true global (with a secret name!). // This implementation also allows us to share the context between modules compiled in callback and fibers mode. -var glob = typeof global === "object" ? global : window; -var secret = "_20c7abceb95c4eb88b7ca1895b1170d1"; -module.exports = (glob[secret] || (glob[secret] = { context: {} })); - -var g = glob[secret]; - -// Internal call to manage runtimes -g.runtime || Object.defineProperty(g, 'runtime', { - get: function() { return g.__runtime__; }, - set: function(value) { - if (g.__runtime__ !== value) { - if (g.__runtime__) { - if (/-fast$/.test(g.__runtime__) || - /-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value); - console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value); +(function() { + var glob = typeof global === "object" ? global : window; + var secret = "_20c7abceb95c4eb88b7ca1895b1170d1"; + var g = glob[secret] || (glob[secret] = { context: {} }); + if (typeof exports !== 'undefined') { + module.exports = g; + } else { + Streamline.globals = g; + } + // Internal call to manage runtimes + g.runtime || Object.defineProperty(g, 'runtime', { + get: function() { return g.__runtime__; }, + set: function(value) { + if (g.__runtime__ !== value) { + if (g.__runtime__) { + if (/-fast$/.test(g.__runtime__) || + /-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value); + console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value); + } + g.__runtime__ = value; } - g.__runtime__ = value; } - } -}); + }); -/// -/// * `fn = globals.withContext(fn, cx)` -/// wraps a function so that it executes with context `cx` (or with a new empty context if `cx` is falsy). -/// The previous context will be restored when the function returns (or throws). -/// returns the wrapped function. -g.withContext = function(fn, cx) { - return function() { - var oldContext = g.context; - g.context = cx || {}; - try { - return fn.apply(this, arguments) - } finally { - g.context = oldContext; - } + /// + /// * `fn = globals.withContext(fn, cx)` + /// wraps a function so that it executes with context `cx` (or with a new empty context if `cx` is falsy). + /// The previous context will be restored when the function returns (or throws). + /// returns the wrapped function. + g.withContext = function(fn, cx) { + return function() { + var oldContext = g.context; + g.context = cx || {}; + try { + return fn.apply(this, arguments) + } finally { + g.context = oldContext; + } + }; + }; + + g.setPromise = function(name) { + if (g.Promise) return; // first caller wins + var req = require; // defeat streamline-require dependencies + if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise'); + else g.Promise = require(name); }; -}; +})(); -g.setPromise = function(name) { - if (g.Promise) return; // first caller wins - var req = require; // defeat streamline-require dependencies - if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise'); - else g.Promise = require(name); -}; diff --git a/lib/transform-all-generators.js b/lib/transform-all-generators.js index 36877e58..c7e1b25b 100644 --- a/lib/transform-all-generators.js +++ b/lib/transform-all-generators.js @@ -9289,6 +9289,77 @@ if (typeof exports !== 'undefined') { } } })(typeof exports !== 'undefined' ? exports : (window.Streamline = window.Streamline || {})); + +/// +/// # Container for global context +/// +/// The `globals` module is a container for the global `context` object which is maintained across +/// asynchronous calls. +/// +/// This context is very handy to store information that all calls should be able to access +/// but that you don't want to pass explicitly via function parameters. The most obvious example is +/// the `locale` that each request may set differently and that your low level libraries should +/// be able to retrieve to format messages. +/// +/// `var globals = require('streamline/lib/globals')` +/// +/// * `globals.context = ctx` +/// * `ctx = globals.context` +/// sets and gets the context +/// +/// Note: an empty context (`{}`) is automatically set by the server wrappers of the `streams` module, +/// before they dispatch a request. So, with these wrappers, each request starts with a fresh empty context. +// This module may be loaded several times so we need a true global (with a secret name!). +// This implementation also allows us to share the context between modules compiled in callback and fibers mode. +(function() { + var glob = typeof global === "object" ? global : window; + var secret = "_20c7abceb95c4eb88b7ca1895b1170d1"; + var g = glob[secret] || (glob[secret] = { context: {} }); + if (typeof exports !== 'undefined') { + module.exports = g; + } else { + Streamline.globals = g; + } + // Internal call to manage runtimes + g.runtime || Object.defineProperty(g, 'runtime', { + get: function() { return g.__runtime__; }, + set: function(value) { + if (g.__runtime__ !== value) { + if (g.__runtime__) { + if (/-fast$/.test(g.__runtime__) || + /-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value); + console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value); + } + g.__runtime__ = value; + } + } + }); + + /// + /// * `fn = globals.withContext(fn, cx)` + /// wraps a function so that it executes with context `cx` (or with a new empty context if `cx` is falsy). + /// The previous context will be restored when the function returns (or throws). + /// returns the wrapped function. + g.withContext = function(fn, cx) { + return function() { + var oldContext = g.context; + g.context = cx || {}; + try { + return fn.apply(this, arguments) + } finally { + g.context = oldContext; + } + }; + }; + + g.setPromise = function(name) { + if (g.Promise) return; // first caller wins + var req = require; // defeat streamline-require dependencies + if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise'); + else g.Promise = require(name); + }; +})(); + /** * Copyright (c) 2011 Bruno Jouhier * MIT License @@ -9357,31 +9428,28 @@ if (typeof exports !== 'undefined') { }; })(typeof exports !== 'undefined' ? exports : (Streamline.future = Streamline.future || {})); -if (typeof exports !== 'undefined') { - exports = module.exports = Object.create(require('galaxy')); -} else { - exports = (Streamline.runtime = Streamline.runtime || Object.create(require('galaxy'))); -} -var globals = exports.globals = require('../globals'); -exports.globals.runtime = 'generators'; -require("../generators/builtins"); -var fut = require("../util/future"); -exports.streamlinify = fut.streamlinify; +(function(exports) { + var globals = exports.globals = require('../globals'); + exports.globals.runtime = 'generators'; + require("../generators/builtins"); + var fut = require("../util/future"); + exports.streamlinify = fut.streamlinify; -var unstar = exports.unstar; + var unstar = exports.unstar; -exports.unstar = function(fn, options, entering) { - if (typeof options === "number") options = { - callbackIndex: options, - }; - else options = options || {}; - options.promise = options.callbackDefault ? function(fn, args, i) { - return fut.future.call(this, fn, args, i)(options.callbackDefault()); - } : fut.promise; - return unstar(fn, options, entering); -} + exports.unstar = function(fn, options, entering) { + if (typeof options === "number") options = { + callbackIndex: options, + }; + else options = options || {}; + options.promise = options.callbackDefault ? function(fn, args, i) { + return fut.future.call(this, fn, args, i)(options.callbackDefault()); + } : fut.promise; + return unstar(fn, options, entering); + } -exports.then = exports.star(fut.then, 2); + exports.then = exports.star(fut.then, 2); +})(typeof exports !== 'undefined' ? module.exports = Object.create(require('galaxy')) : Streamline.runtime = Streamline.runtime || Object.create(require('galaxy'))); /*** Generated by streamline 0.10.17 (generators) - DO NOT EDIT ***/var galaxy = require("../generators/runtime");(function(){})();(galaxy.unstar(function*(_) { /** * Copyright (c) 2012 Bruno Jouhier * MIT License diff --git a/test/common/callbacks/eval-test.js b/test/common/callbacks/eval-test.js index 414be70f..2fc0d7ee 100644 --- a/test/common/callbacks/eval-test.js +++ b/test/common/callbacks/eval-test.js @@ -1,4 +1,4 @@ -/*** Generated by streamline 0.10.17 (callbacks) --standalone - DO NOT EDIT ***/ var __rt=(function(){var __modules={},mod;function require(p){var m=__modules[p.substring(3)]; return m && m.exports};__modules['globals']=(mod={exports:{}});(function(module, exports){var glob = typeof global === "object" ? global : window;var secret = "_20c7abceb95c4eb88b7ca1895b1170d1";module.exports = (glob[secret] || (glob[secret] = { context: {} }));var g = glob[secret];g.runtime || Object.defineProperty(g, 'runtime', {get: function() { return g.__runtime__; },set: function(value) {if (g.__runtime__ !== value) {if (g.__runtime__) {if (/-fast$/.test(g.__runtime__) ||/-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value);console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value);}g.__runtime__ = value;}}});g.withContext = function(fn, cx) {return function() {var oldContext = g.context;g.context = cx || {};try {return fn.apply(this, arguments)} finally {g.context = oldContext;}};};g.setPromise = function(name) {if (g.Promise) return; var req = require; if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise');else g.Promise = require(name);};})(mod, mod.exports);__modules['util/future']=(mod={exports:{}});(function(module, exports){(function(exports) {var globals = require("../globals");exports.future = function(fn, args, i) {var err, result, done, q = [], self = this;args = Array.prototype.slice.call(args);args[i] = function(e, r) {err = e, result = r, done = true;q && q.forEach(function(f) {f.call(self, e, r);});q = null;};args[i].__futurecb = true;fn.apply(this, args);var ret = function F(cb) {if (typeof cb !== 'function') {var globals = require('../globals');if (cb == null && globals.Promise) return exports.promise.call(this, F, [], 0);if (cb !== false && !globals.oldStyleFutures) throw new Error("callback missing (argument #0). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error");return F;}if (done) cb.call(self, err, result);else q.push(cb);};ret.__future = true;return ret;};exports.streamlinify = function(fn, idx) {return function() {if (!arguments[idx]) return exports.future.call(this, fn, arguments, idx);else return fn.apply(this, arguments);};};exports.promise = function(fn, args, i) {if (args[i] === false) return exports.future.call(this, fn, args, i);if (args[i] != null) throw new Error("invalid callback: " + typeof(args[i]));if (globals.oldStyleFutures) return exports.future.call(this, fn, args, i);if (!globals.Promise) throw new Error("callback missing (argument #" + i + "). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error");var self = this;args = Array.prototype.slice.call(args);return new globals.Promise(function(resolve, reject) {args[i] = function(e, r) {if (e) reject(e);else resolve(r);};fn.apply(self, args);});};exports.then = function(promise, method, cb) {promise[method](function(r) {cb && cb(null, r);cb = null;}, function(e) {cb && cb(e);cb = null;});};})(typeof exports !== 'undefined' ? exports : (Streamline.future = Streamline.future || {}));})(mod, mod.exports);__modules['callbacks/runtime']=(mod={exports:{}});(function(module, exports){(function(exports) {var __g = require("../globals");__g.runtime = 'callbacks';var __fut = require("../util/future");__g.context = __g.context || {};__g.depth = __g.depth || 0;__g.async = __g.async || false;__g.trampoline = (function() {var q = [];return {queue: function(fn) {q.push(fn);},flush: function() {var oldContext = __g.context;__g.depth++;try {var fn;while (fn = q.shift()) fn();} finally {__g.context = oldContext;__g.depth--;}}}})();exports.runtime = function(filename, oldStyleFutures) {__g.oldStyleFutures = oldStyleFutures;function __func(_, __this, __arguments, fn, index, frame, body) {if (typeof _ !== 'function') return __fut.promise.call(__this, fn, __arguments, index);frame.file = filename;frame.prev = __g.frame;frame.calls = 0;if (frame.prev) frame.prev.calls++;var emitter = __g.emitter;__g.frame = frame;__g.depth++;if (emitter) emitter.emit("enter", frame, _); try {frame.active = true;body();} catch (e) {__setEF(e, frame.prev);__propagate(_, e);} finally {frame.active = false;if (emitter) {emitter.emit("exit", frame);}__g.frame = frame.prev;if (--__g.depth === 0 && __g.trampoline) __g.trampoline.flush();}}return {__g: __g,__func: __func,__cb: __cb,__future: __fut.future,__propagate: __propagate,__trap: __trap,__tryCatch: __tryCatch,__catch: __catch,__forIn: __forIn,__apply: __apply,__construct: __construct,__setEF: __setEF,streamlinify: __fut.streamlinify,__pthen: __fut.then,};};function __cb(_, frame, offset, col, fn, trampo, returnArray) {frame.offset = offset;frame.col = col;var ctx = __g.context;var calls = frame.calls;var emitter = __g.emitter;var ret = function ___(err, result) {if (returnArray) result = Array.prototype.slice.call(arguments, 1);returnArray = false; var oldFrame = __g.frame;__g.frame = frame;var oldContext = __g.context;__g.context = ctx;if (emitter && __g.depth === 0) emitter.emit('resume', frame);if (emitter) emitter.emit('enter', frame);__g.depth++;try {if (trampo && frame.active && __g.trampoline) {__g.trampoline.queue(function() {return ___(err, result);});} else {___.dispatched = true;if (err) {__setEF(err, frame);return _(err);}frame.active = true;return fn(null, result);}} catch (ex) {if (___.dispatched && _.name !== '___' && _.name !== '__trap' && calls !== frame.calls) throw ex;__setEF(ex, frame);return __propagate(_, ex);} finally {frame.active = false;if (emitter) emitter.emit("exit", frame);__g.frame = oldFrame;__g.context = oldContext;if (--__g.depth === 0 && __g.trampoline) __g.trampoline.flush();}};if (emitter && !ret.dispatched) emitter.emit('yield', frame);ret.__streamlined = true;return ret;}function __propagate(_, err) {try {_(err);} catch (ex) {__trap(ex);}}function __trap(err) {if (err) {if (__g.context && __g.context.errorHandler) __g.context.errorHandler(err);else __g.trampoline.queue(function() {throw err;});}}function __tryCatch(_, fn) {try {fn();} catch (e) {try {_(e);} catch (ex) {__trap(ex);}}}function __catch(fn, _) {var frame = __g.frame,context = __g.context;__g.trampoline.queue(function() {var oldFrame = __g.frame,oldContext = __g.context;__g.frame = frame;__g.context = context;try {fn();} catch (ex) {_(ex);} finally {__g.frame = oldFrame;__g.context = oldContext;}});}function __forIn(object) {var array = [];for (var obj in object) {array.push(obj);}return array;}function __apply(cb, fn, thisObj, args, index) {if (cb == null) return __fut.future(__apply, arguments, 0);args = Array.prototype.slice.call(args, 0);args[index != null ? index : args.length] = cb;return fn.apply(thisObj, args);}function __construct(constructor, i) {var key = '__async' + i,f;return constructor[key] || (constructor[key] = function() {var args = arguments;function F() {var self = this;var cb = args[i];args[i] = function(e, r) {cb(e, self);};args[i].__streamlined = cb.__streamlined;args[i].__futurecb = cb.__futurecb;return constructor.apply(self, args);}F.prototype = constructor.prototype;return new F();});}function __setEF(e, f) {function formatStack(e, raw) {var ff = typeof navigator === 'object' && navigator.userAgent.toLowerCase().indexOf('firefox') > -1;if (ff) raw = "Error: " + e.message + '\n' + raw;var s = raw,f, skip;var cut = (e.message || '').split('\n').length;var lines = s.split('\n');s = lines.slice(cut).map(function(l) {var m = /([^@]*)\@(.*?)\:(\d+)(?:\:(\d+))?$/.exec(l);l = m ? " at " + m[1] + " (" + m[2] + ":" + parseInt(m[3]) + ":" + (m[4] || "0") + ")" : l;var i = l.indexOf('__$');if (i >= 0 && !skip) {skip = true;return l.substring(0, i) + l.substring(i + 3);}return skip ? '' : l;}).filter(function(l) {return l;}).join('\n');s = lines.slice(0, cut).join('\n') + '\n <<< async stack >>>' + (skip ? '\n' + s : '');for (var f = e.__frame; f; f = f.prev) {if (f.offset >= 0) s += "\n at " + f.name + " (" + f.file + ":" + (f.line + f.offset) + ":" + (f.col+1) + ")"}s += '\n <<< raw stack >>>' + '\n' + lines.slice(cut).join('\n');return s;};e.__frame = e.__frame || f;if (exports.stackTraceEnabled && e.__lookupGetter__ && e.__lookupGetter__("rawStack") == null) {var getter = e.__lookupGetter__("stack");if (!getter) { var raw = e.stack || "raw stack unavailable";getter = function() {return raw;}}e.__defineGetter__("rawStack", getter);e.__defineGetter__("stack", function() {return formatStack(e, getter.call(this));});}}exports.stackTraceEnabled = true;})(typeof exports !== 'undefined' ? exports : (Streamline.runtime = Streamline.runtime || {}));require && require("../callbacks/builtins");})(mod, mod.exports);return __modules['callbacks/runtime'].exports.runtime('test/common/eval-test._js', false);})(),__func=__rt.__func,__cb=__rt.__cb,__catch=__rt.__catch,__tryCatch=__rt.__tryCatch,__forIn=__rt.__forIn,__construct=__rt.__construct,__pthen=__rt.__pthen; QUnit.module(module.id); +/*** Generated by streamline 0.10.17 (callbacks) --standalone - DO NOT EDIT ***/ var __rt=(function(){var __modules={},mod;function require(p){var m=__modules[p.substring(3)]; return m && m.exports};__modules['globals']=(mod={exports:{}});(function(module, exports){(function() {var glob = typeof global === "object" ? global : window;var secret = "_20c7abceb95c4eb88b7ca1895b1170d1";var g = glob[secret] || (glob[secret] = { context: {} });if (typeof exports !== 'undefined') {module.exports = g;} else {Streamline.globals = g;}g.runtime || Object.defineProperty(g, 'runtime', {get: function() { return g.__runtime__; },set: function(value) {if (g.__runtime__ !== value) {if (g.__runtime__) {if (/-fast$/.test(g.__runtime__) ||/-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value);console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value);}g.__runtime__ = value;}}});g.withContext = function(fn, cx) {return function() {var oldContext = g.context;g.context = cx || {};try {return fn.apply(this, arguments)} finally {g.context = oldContext;}};};g.setPromise = function(name) {if (g.Promise) return; var req = require; if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise');else g.Promise = require(name);};})();})(mod, mod.exports);__modules['util/future']=(mod={exports:{}});(function(module, exports){(function(exports) {var globals = require("../globals");exports.future = function(fn, args, i) {var err, result, done, q = [], self = this;args = Array.prototype.slice.call(args);args[i] = function(e, r) {err = e, result = r, done = true;q && q.forEach(function(f) {f.call(self, e, r);});q = null;};args[i].__futurecb = true;fn.apply(this, args);var ret = function F(cb) {if (typeof cb !== 'function') {var globals = require('../globals');if (cb == null && globals.Promise) return exports.promise.call(this, F, [], 0);if (cb !== false && !globals.oldStyleFutures) throw new Error("callback missing (argument #0). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error");return F;}if (done) cb.call(self, err, result);else q.push(cb);};ret.__future = true;return ret;};exports.streamlinify = function(fn, idx) {return function() {if (!arguments[idx]) return exports.future.call(this, fn, arguments, idx);else return fn.apply(this, arguments);};};exports.promise = function(fn, args, i) {if (args[i] === false) return exports.future.call(this, fn, args, i);if (args[i] != null) throw new Error("invalid callback: " + typeof(args[i]));if (globals.oldStyleFutures) return exports.future.call(this, fn, args, i);if (!globals.Promise) throw new Error("callback missing (argument #" + i + "). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error");var self = this;args = Array.prototype.slice.call(args);return new globals.Promise(function(resolve, reject) {args[i] = function(e, r) {if (e) reject(e);else resolve(r);};fn.apply(self, args);});};exports.then = function(promise, method, cb) {promise[method](function(r) {cb && cb(null, r);cb = null;}, function(e) {cb && cb(e);cb = null;});};})(typeof exports !== 'undefined' ? exports : (Streamline.future = Streamline.future || {}));})(mod, mod.exports);__modules['callbacks/runtime']=(mod={exports:{}});(function(module, exports){(function(exports) {var __g = require("../globals");__g.runtime = 'callbacks';var __fut = require("../util/future");__g.context = __g.context || {};__g.depth = __g.depth || 0;__g.async = __g.async || false;__g.trampoline = (function() {var q = [];return {queue: function(fn) {q.push(fn);},flush: function() {var oldContext = __g.context;__g.depth++;try {var fn;while (fn = q.shift()) fn();} finally {__g.context = oldContext;__g.depth--;}}}})();exports.runtime = function(filename, oldStyleFutures) {__g.oldStyleFutures = oldStyleFutures;function __func(_, __this, __arguments, fn, index, frame, body) {if (typeof _ !== 'function') return __fut.promise.call(__this, fn, __arguments, index);frame.file = filename;frame.prev = __g.frame;frame.calls = 0;if (frame.prev) frame.prev.calls++;var emitter = __g.emitter;__g.frame = frame;__g.depth++;if (emitter) emitter.emit("enter", frame, _); try {frame.active = true;body();} catch (e) {__setEF(e, frame.prev);__propagate(_, e);} finally {frame.active = false;if (emitter) {emitter.emit("exit", frame);}__g.frame = frame.prev;if (--__g.depth === 0 && __g.trampoline) __g.trampoline.flush();}}return {__g: __g,__func: __func,__cb: __cb,__future: __fut.future,__propagate: __propagate,__trap: __trap,__tryCatch: __tryCatch,__catch: __catch,__forIn: __forIn,__apply: __apply,__construct: __construct,__setEF: __setEF,streamlinify: __fut.streamlinify,__pthen: __fut.then,};};function __cb(_, frame, offset, col, fn, trampo, returnArray) {frame.offset = offset;frame.col = col;var ctx = __g.context;var calls = frame.calls;var emitter = __g.emitter;var ret = function ___(err, result) {if (returnArray) result = Array.prototype.slice.call(arguments, 1);returnArray = false; var oldFrame = __g.frame;__g.frame = frame;var oldContext = __g.context;__g.context = ctx;if (emitter && __g.depth === 0) emitter.emit('resume', frame);if (emitter) emitter.emit('enter', frame);__g.depth++;try {if (trampo && frame.active && __g.trampoline) {__g.trampoline.queue(function() {return ___(err, result);});} else {___.dispatched = true;if (err) {__setEF(err, frame);return _(err);}frame.active = true;return fn(null, result);}} catch (ex) {if (___.dispatched && _.name !== '___' && _.name !== '__trap' && calls !== frame.calls) throw ex;__setEF(ex, frame);return __propagate(_, ex);} finally {frame.active = false;if (emitter) emitter.emit("exit", frame);__g.frame = oldFrame;__g.context = oldContext;if (--__g.depth === 0 && __g.trampoline) __g.trampoline.flush();}};if (emitter && !ret.dispatched) emitter.emit('yield', frame);ret.__streamlined = true;return ret;}function __propagate(_, err) {try {_(err);} catch (ex) {__trap(ex);}}function __trap(err) {if (err) {if (__g.context && __g.context.errorHandler) __g.context.errorHandler(err);else __g.trampoline.queue(function() {throw err;});}}function __tryCatch(_, fn) {try {fn();} catch (e) {try {_(e);} catch (ex) {__trap(ex);}}}function __catch(fn, _) {var frame = __g.frame,context = __g.context;__g.trampoline.queue(function() {var oldFrame = __g.frame,oldContext = __g.context;__g.frame = frame;__g.context = context;try {fn();} catch (ex) {_(ex);} finally {__g.frame = oldFrame;__g.context = oldContext;}});}function __forIn(object) {var array = [];for (var obj in object) {array.push(obj);}return array;}function __apply(cb, fn, thisObj, args, index) {if (cb == null) return __fut.future(__apply, arguments, 0);args = Array.prototype.slice.call(args, 0);args[index != null ? index : args.length] = cb;return fn.apply(thisObj, args);}function __construct(constructor, i) {var key = '__async' + i,f;return constructor[key] || (constructor[key] = function() {var args = arguments;function F() {var self = this;var cb = args[i];args[i] = function(e, r) {cb(e, self);};args[i].__streamlined = cb.__streamlined;args[i].__futurecb = cb.__futurecb;return constructor.apply(self, args);}F.prototype = constructor.prototype;return new F();});}function __setEF(e, f) {function formatStack(e, raw) {var ff = typeof navigator === 'object' && navigator.userAgent.toLowerCase().indexOf('firefox') > -1;if (ff) raw = "Error: " + e.message + '\n' + raw;var s = raw,f, skip;var cut = (e.message || '').split('\n').length;var lines = s.split('\n');s = lines.slice(cut).map(function(l) {var m = /([^@]*)\@(.*?)\:(\d+)(?:\:(\d+))?$/.exec(l);l = m ? " at " + m[1] + " (" + m[2] + ":" + parseInt(m[3]) + ":" + (m[4] || "0") + ")" : l;var i = l.indexOf('__$');if (i >= 0 && !skip) {skip = true;return l.substring(0, i) + l.substring(i + 3);}return skip ? '' : l;}).filter(function(l) {return l;}).join('\n');s = lines.slice(0, cut).join('\n') + '\n <<< async stack >>>' + (skip ? '\n' + s : '');for (var f = e.__frame; f; f = f.prev) {if (f.offset >= 0) s += "\n at " + f.name + " (" + f.file + ":" + (f.line + f.offset) + ":" + (f.col+1) + ")"}s += '\n <<< raw stack >>>' + '\n' + lines.slice(cut).join('\n');return s;};e.__frame = e.__frame || f;if (exports.stackTraceEnabled && e.__lookupGetter__ && e.__lookupGetter__("rawStack") == null) {var getter = e.__lookupGetter__("stack");if (!getter) { var raw = e.stack || "raw stack unavailable";getter = function() {return raw;}}e.__defineGetter__("rawStack", getter);e.__defineGetter__("stack", function() {return formatStack(e, getter.call(this));});}}exports.stackTraceEnabled = true;})(typeof exports !== 'undefined' ? exports : (Streamline.runtime = Streamline.runtime || {}));require && require("../callbacks/builtins");})(mod, mod.exports);return __modules['callbacks/runtime'].exports.runtime('test/common/eval-test._js', false);})(),__func=__rt.__func,__cb=__rt.__cb,__catch=__rt.__catch,__tryCatch=__rt.__tryCatch,__forIn=__rt.__forIn,__construct=__rt.__construct,__pthen=__rt.__pthen; QUnit.module(module.id); function evalTest(f, val) { f(function (err, result) { diff --git a/test/common/callbacks/flows-test.html b/test/common/callbacks/flows-test.html index f8b0bc0d..8c6ed714 100644 --- a/test/common/callbacks/flows-test.html +++ b/test/common/callbacks/flows-test.html @@ -5,10 +5,7 @@ - - - - + diff --git a/test/common/callbacks/flows-test.js b/test/common/callbacks/flows-test.js index c9b148ad..bc072612 100644 --- a/test/common/callbacks/flows-test.js +++ b/test/common/callbacks/flows-test.js @@ -336,13 +336,14 @@ return function __1(_) { var __frame = { name: '__1', line: 336 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { count++; peak = Math.max(count, peak); - return delay(__cb(_, __frame, 3, 0, function ___(__0, __1) { total = __1 + total; - count--; - return _(null, 2 * i); }, true, false), i); }); }; } var __frame = { name: '__11', line: 330 }; return __func(_, this, arguments, __11, 0, __frame, function __$__11() { total = 0; peak = 0; count = 0; + return setTimeout(__cb(_, __frame, 3, 0, function __$__1() { + return delay(__cb(_, __frame, 4, 0, function ___(__0, __1) { total = __1 + total; + count--; + return _(null, 2 * i); }, true, false), i); }, true, false), 10); }); }; } var __frame = { name: '__11', line: 330 }; return __func(_, this, arguments, __11, 0, __frame, function __$__11() { total = 0; peak = 0; count = 0; - return flows.spray([ doIt(1), doIt(2), doIt(3) ]).collectAll(__cb(_, __frame, 15, 0, function ___(__0, __1) { results = __1; + return flows.spray([ doIt(1), doIt(2), doIt(3) ]).collectAll(__cb(_, __frame, 16, 0, function ___(__0, __1) { results = __1; equal(total, 6); ok(peak >= 2); equal(count, 0); @@ -355,16 +356,17 @@ function doIt(i) { - return function __1(_) { var __frame = { name: '__1', line: 358 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { + return function __1(_) { var __frame = { name: '__1', line: 359 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { count++; peak = Math.max(count, peak); - return delay(__cb(_, __frame, 3, 0, function ___(__0, __1) { total = __1 + total; - count--; - return _(null, 2 * i); }, true, false), i); }); }; } var __frame = { name: '__12', line: 352 }; return __func(_, this, arguments, __12, 0, __frame, function __$__12() { total = 0; peak = 0; count = 0; + return setTimeout(__cb(_, __frame, 3, 0, function __$__1() { + return delay(__cb(_, __frame, 4, 0, function ___(__0, __1) { total = __1 + total; + count--; + return _(null, 2 * i); }, true, false), i); }, true, false), 10); }); }; } var __frame = { name: '__12', line: 353 }; return __func(_, this, arguments, __12, 0, __frame, function __$__12() { total = 0; peak = 0; count = 0; - return flows.spray([ doIt(1), doIt(2), doIt(3) ]).collectOne(__cb(_, __frame, 15, 0, function ___(__0, __1) { result = __1; + return flows.spray([ doIt(1), doIt(2), doIt(3) ]).collectOne(__cb(_, __frame, 16, 0, function ___(__0, __1) { result = __1; ok(total == 1 || total == 2); ok(peak >= 2); ok(count > 0); @@ -377,36 +379,37 @@ function doIt(i) { - return function __1(_) { var __frame = { name: '__1', line: 380 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { + return function __1(_) { var __frame = { name: '__1', line: 382 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { count++; peak = Math.max(count, peak); - return delay(__cb(_, __frame, 3, 0, function ___(__0, __1) { total = __1 + total; - count--; - return _(null, 2 * i); }, true, false), i); }); }; } var __frame = { name: '__13', line: 374 }; return __func(_, this, arguments, __13, 0, __frame, function __$__13() { total = 0; peak = 0; count = 0; + return setTimeout(__cb(_, __frame, 3, 0, function __$__1() { + return delay(__cb(_, __frame, 4, 0, function ___(__0, __1) { total = __1 + total; + count--; + return _(null, 2 * i); }, true, false), i); }, true, false), 10); }); }; } var __frame = { name: '__13', line: 376 }; return __func(_, this, arguments, __13, 0, __frame, function __$__13() { total = 0; peak = 0; count = 0; - return flows.spray([ doIt(1), doIt(2), doIt(3) ], 2).collectAll(__cb(_, __frame, 15, 0, function ___(__0, __1) { results = __1; + return flows.spray([ doIt(1), doIt(2), doIt(3) ], 2).collectAll(__cb(_, __frame, 16, 0, function ___(__0, __1) { results = __1; deepEqual([ total, peak, count, results ], [ 6, 2, 0, [ 2, 4, 6 ] ]); start(); _(); }, true, false)); }); }); asyncTest('contexts', 3, function __14(_) { var result; - function testContext(_, x) { var y; var __frame = { name: 'testContext', line: 394 }; return __func(_, this, arguments, testContext, 0, __frame, function __$testContext() { + function testContext(_, x) { var y; var __frame = { name: 'testContext', line: 397 }; return __func(_, this, arguments, testContext, 0, __frame, function __$testContext() { flows.setContext({ val: x }); return delay(__cb(_, __frame, 4, 0, function ___(__0, __1) { y = __1; strictEqual(y, 2 * flows.getContext().val); - return _(null, y + 1); }, true, false), 2 * x); }); } var __frame = { name: '__14', line: 393 }; return __func(_, this, arguments, __14, 0, __frame, function __$__14() { + return _(null, y + 1); }, true, false), 2 * x); }); } var __frame = { name: '__14', line: 396 }; return __func(_, this, arguments, __14, 0, __frame, function __$__14() { return flows.spray([ function __1(_) { - var __frame = { name: '__1', line: 405 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { + var __frame = { name: '__1', line: 408 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { return testContext(__cb(_, __frame, 1, 0, _, true, false), 3); }); }, function __2(_) { - var __frame = { name: '__2', line: 409 }; return __func(_, this, arguments, __2, 0, __frame, function __$__2() { + var __frame = { name: '__2', line: 412 }; return __func(_, this, arguments, __2, 0, __frame, function __$__2() { return testContext(__cb(_, __frame, 1, 0, _, true, false), 5); }); } ]).collectAll(__cb(_, __frame, 20, 0, function ___(__0, __3) { @@ -420,11 +423,11 @@ - function doIt(future, _) { var __frame = { name: 'doIt', line: 423 }; return __func(_, this, arguments, doIt, 1, __frame, function __$doIt() { + function doIt(future, _) { var __frame = { name: 'doIt', line: 426 }; return __func(_, this, arguments, doIt, 1, __frame, function __$doIt() { return future(__cb(_, __frame, 1, 0, function ___(__0, __1) { result1 = __1 + result1; return future(__cb(_, __frame, 2, 0, function ___(__0, __2) { result2 = __2 + result2; return delay(__cb(_, __frame, 3, 0, function __$doIt() { - return future(__cb(_, __frame, 4, 0, function ___(__0, __3) { result3 = __3 + result3; _(); }, true, false)); }, true, false)); }, true, false)); }, true, false)); }); } var __frame = { name: '__15', line: 418 }; return __func(_, this, arguments, __15, 0, __frame, function __$__15() { result1 = 0; result2 = 0; result3 = 0; f1 = delay(false, 1); + return future(__cb(_, __frame, 4, 0, function ___(__0, __3) { result3 = __3 + result3; _(); }, true, false)); }, true, false)); }, true, false)); }, true, false)); }); } var __frame = { name: '__15', line: 421 }; return __func(_, this, arguments, __15, 0, __frame, function __$__15() { result1 = 0; result2 = 0; result3 = 0; f1 = delay(false, 1); f10 = delay(false, 10); @@ -439,20 +442,20 @@ asyncTest('trampoline', 1, function __16(_) { - function sums(_, n) { var fn; var __frame = { name: 'sums', line: 442 }; return __func(_, this, arguments, sums, 0, __frame, function __$sums() { fn = function __1(_) { - var __frame = { name: '__1', line: 443 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { + function sums(_, n) { var fn; var __frame = { name: 'sums', line: 445 }; return __func(_, this, arguments, sums, 0, __frame, function __$sums() { fn = function __1(_) { + var __frame = { name: '__1', line: 446 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { return function __$__1(_) { var __1 = n > 0; return function __$__1(__then) { if (__1) { return sums(__cb(_, __frame, 1, 0, function ___(__0, __3) { var __2 = n + __3; return _(null, __2); }, true, false), n - 1); } else { __then(); } }(function __$__1() { return _(null, 0); }); }(__cb(_, __frame, 0, 0, _, true, false)); }); }; return function __$sums(__then) { if (n % 1000 === 0) { return flows.trampoline(__cb(_, __frame, 4, 0, _, true, false), fn); } else { - return fn(__cb(_, __frame, 5, 0, _, true, false)); } }(_); }); } var __frame = { name: '__16', line: 441 }; return __func(_, this, arguments, __16, 0, __frame, function __$__16() { + return fn(__cb(_, __frame, 5, 0, _, true, false)); } }(_); }); } var __frame = { name: '__16', line: 444 }; return __func(_, this, arguments, __16, 0, __frame, function __$__16() { return sums(__cb(_, __frame, 8, 0, function ___(__0, __1) { equals(__1, 50000 * 100001); start(); _(); }, true, false), 100000); }); }); - asyncTest('trampoline preserves context', 2, function __17(_) { var globals, fn, result; var __frame = { name: '__17', line: 453 }; return __func(_, this, arguments, __17, 0, __frame, function __$__17() { globals = require('streamline/lib/globals'); + asyncTest('trampoline preserves context', 2, function __17(_) { var globals, fn, result; var __frame = { name: '__17', line: 456 }; return __func(_, this, arguments, __17, 0, __frame, function __$__17() { globals = require('streamline/lib/globals'); fn = function __1(_) { - var __frame = { name: '__1', line: 455 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { + var __frame = { name: '__1', line: 458 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() { return _(null, globals.context.val); }); }; globals.context.val = 'abc'; diff --git a/test/common/callbacks/futures-test.html b/test/common/callbacks/futures-test.html index 90af4877..6e6ee358 100644 --- a/test/common/callbacks/futures-test.html +++ b/test/common/callbacks/futures-test.html @@ -5,10 +5,7 @@ - - - - + diff --git a/test/common/callbacks/futures-test.js b/test/common/callbacks/futures-test.js index 3cb10b19..ae6a2c6c 100644 --- a/test/common/callbacks/futures-test.js +++ b/test/common/callbacks/futures-test.js @@ -1,4 +1,4 @@ -/*** Generated by streamline 0.10.17 (callbacks) --standalone - DO NOT EDIT ***/ var __rt=(function(){var __modules={},mod;function require(p){var m=__modules[p.substring(3)]; return m && m.exports};__modules['globals']=(mod={exports:{}});(function(module, exports){var glob = typeof global === "object" ? global : window;var secret = "_20c7abceb95c4eb88b7ca1895b1170d1";module.exports = (glob[secret] || (glob[secret] = { context: {} }));var g = glob[secret];g.runtime || Object.defineProperty(g, 'runtime', {get: function() { return g.__runtime__; },set: function(value) {if (g.__runtime__ !== value) {if (g.__runtime__) {if (/-fast$/.test(g.__runtime__) ||/-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value);console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value);}g.__runtime__ = value;}}});g.withContext = function(fn, cx) {return function() {var oldContext = g.context;g.context = cx || {};try {return fn.apply(this, arguments)} finally {g.context = oldContext;}};};g.setPromise = function(name) {if (g.Promise) return; var req = require; if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise');else g.Promise = require(name);};})(mod, mod.exports);__modules['util/future']=(mod={exports:{}});(function(module, exports){(function(exports) {var globals = require("../globals");exports.future = function(fn, args, i) {var err, result, done, q = [], self = this;args = Array.prototype.slice.call(args);args[i] = function(e, r) {err = e, result = r, done = true;q && q.forEach(function(f) {f.call(self, e, r);});q = null;};args[i].__futurecb = true;fn.apply(this, args);var ret = function F(cb) {if (typeof cb !== 'function') {var globals = require('../globals');if (cb == null && globals.Promise) return exports.promise.call(this, F, [], 0);if (cb !== false && !globals.oldStyleFutures) throw new Error("callback missing (argument #0). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error");return F;}if (done) cb.call(self, err, result);else q.push(cb);};ret.__future = true;return ret;};exports.streamlinify = function(fn, idx) {return function() {if (!arguments[idx]) return exports.future.call(this, fn, arguments, idx);else return fn.apply(this, arguments);};};exports.promise = function(fn, args, i) {if (args[i] === false) return exports.future.call(this, fn, args, i);if (args[i] != null) throw new Error("invalid callback: " + typeof(args[i]));if (globals.oldStyleFutures) return exports.future.call(this, fn, args, i);if (!globals.Promise) throw new Error("callback missing (argument #" + i + "). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error");var self = this;args = Array.prototype.slice.call(args);return new globals.Promise(function(resolve, reject) {args[i] = function(e, r) {if (e) reject(e);else resolve(r);};fn.apply(self, args);});};exports.then = function(promise, method, cb) {promise[method](function(r) {cb && cb(null, r);cb = null;}, function(e) {cb && cb(e);cb = null;});};})(typeof exports !== 'undefined' ? exports : (Streamline.future = Streamline.future || {}));})(mod, mod.exports);__modules['callbacks/runtime']=(mod={exports:{}});(function(module, exports){(function(exports) {var __g = require("../globals");__g.runtime = 'callbacks';var __fut = require("../util/future");__g.context = __g.context || {};__g.depth = __g.depth || 0;__g.async = __g.async || false;__g.trampoline = (function() {var q = [];return {queue: function(fn) {q.push(fn);},flush: function() {var oldContext = __g.context;__g.depth++;try {var fn;while (fn = q.shift()) fn();} finally {__g.context = oldContext;__g.depth--;}}}})();exports.runtime = function(filename, oldStyleFutures) {__g.oldStyleFutures = oldStyleFutures;function __func(_, __this, __arguments, fn, index, frame, body) {if (typeof _ !== 'function') return __fut.promise.call(__this, fn, __arguments, index);frame.file = filename;frame.prev = __g.frame;frame.calls = 0;if (frame.prev) frame.prev.calls++;var emitter = __g.emitter;__g.frame = frame;__g.depth++;if (emitter) emitter.emit("enter", frame, _); try {frame.active = true;body();} catch (e) {__setEF(e, frame.prev);__propagate(_, e);} finally {frame.active = false;if (emitter) {emitter.emit("exit", frame);}__g.frame = frame.prev;if (--__g.depth === 0 && __g.trampoline) __g.trampoline.flush();}}return {__g: __g,__func: __func,__cb: __cb,__future: __fut.future,__propagate: __propagate,__trap: __trap,__tryCatch: __tryCatch,__catch: __catch,__forIn: __forIn,__apply: __apply,__construct: __construct,__setEF: __setEF,streamlinify: __fut.streamlinify,__pthen: __fut.then,};};function __cb(_, frame, offset, col, fn, trampo, returnArray) {frame.offset = offset;frame.col = col;var ctx = __g.context;var calls = frame.calls;var emitter = __g.emitter;var ret = function ___(err, result) {if (returnArray) result = Array.prototype.slice.call(arguments, 1);returnArray = false; var oldFrame = __g.frame;__g.frame = frame;var oldContext = __g.context;__g.context = ctx;if (emitter && __g.depth === 0) emitter.emit('resume', frame);if (emitter) emitter.emit('enter', frame);__g.depth++;try {if (trampo && frame.active && __g.trampoline) {__g.trampoline.queue(function() {return ___(err, result);});} else {___.dispatched = true;if (err) {__setEF(err, frame);return _(err);}frame.active = true;return fn(null, result);}} catch (ex) {if (___.dispatched && _.name !== '___' && _.name !== '__trap' && calls !== frame.calls) throw ex;__setEF(ex, frame);return __propagate(_, ex);} finally {frame.active = false;if (emitter) emitter.emit("exit", frame);__g.frame = oldFrame;__g.context = oldContext;if (--__g.depth === 0 && __g.trampoline) __g.trampoline.flush();}};if (emitter && !ret.dispatched) emitter.emit('yield', frame);ret.__streamlined = true;return ret;}function __propagate(_, err) {try {_(err);} catch (ex) {__trap(ex);}}function __trap(err) {if (err) {if (__g.context && __g.context.errorHandler) __g.context.errorHandler(err);else __g.trampoline.queue(function() {throw err;});}}function __tryCatch(_, fn) {try {fn();} catch (e) {try {_(e);} catch (ex) {__trap(ex);}}}function __catch(fn, _) {var frame = __g.frame,context = __g.context;__g.trampoline.queue(function() {var oldFrame = __g.frame,oldContext = __g.context;__g.frame = frame;__g.context = context;try {fn();} catch (ex) {_(ex);} finally {__g.frame = oldFrame;__g.context = oldContext;}});}function __forIn(object) {var array = [];for (var obj in object) {array.push(obj);}return array;}function __apply(cb, fn, thisObj, args, index) {if (cb == null) return __fut.future(__apply, arguments, 0);args = Array.prototype.slice.call(args, 0);args[index != null ? index : args.length] = cb;return fn.apply(thisObj, args);}function __construct(constructor, i) {var key = '__async' + i,f;return constructor[key] || (constructor[key] = function() {var args = arguments;function F() {var self = this;var cb = args[i];args[i] = function(e, r) {cb(e, self);};args[i].__streamlined = cb.__streamlined;args[i].__futurecb = cb.__futurecb;return constructor.apply(self, args);}F.prototype = constructor.prototype;return new F();});}function __setEF(e, f) {function formatStack(e, raw) {var ff = typeof navigator === 'object' && navigator.userAgent.toLowerCase().indexOf('firefox') > -1;if (ff) raw = "Error: " + e.message + '\n' + raw;var s = raw,f, skip;var cut = (e.message || '').split('\n').length;var lines = s.split('\n');s = lines.slice(cut).map(function(l) {var m = /([^@]*)\@(.*?)\:(\d+)(?:\:(\d+))?$/.exec(l);l = m ? " at " + m[1] + " (" + m[2] + ":" + parseInt(m[3]) + ":" + (m[4] || "0") + ")" : l;var i = l.indexOf('__$');if (i >= 0 && !skip) {skip = true;return l.substring(0, i) + l.substring(i + 3);}return skip ? '' : l;}).filter(function(l) {return l;}).join('\n');s = lines.slice(0, cut).join('\n') + '\n <<< async stack >>>' + (skip ? '\n' + s : '');for (var f = e.__frame; f; f = f.prev) {if (f.offset >= 0) s += "\n at " + f.name + " (" + f.file + ":" + (f.line + f.offset) + ":" + (f.col+1) + ")"}s += '\n <<< raw stack >>>' + '\n' + lines.slice(cut).join('\n');return s;};e.__frame = e.__frame || f;if (exports.stackTraceEnabled && e.__lookupGetter__ && e.__lookupGetter__("rawStack") == null) {var getter = e.__lookupGetter__("stack");if (!getter) { var raw = e.stack || "raw stack unavailable";getter = function() {return raw;}}e.__defineGetter__("rawStack", getter);e.__defineGetter__("stack", function() {return formatStack(e, getter.call(this));});}}exports.stackTraceEnabled = true;})(typeof exports !== 'undefined' ? exports : (Streamline.runtime = Streamline.runtime || {}));require && require("../callbacks/builtins");})(mod, mod.exports);return __modules['callbacks/runtime'].exports.runtime('test/common/futures-test._js', false);})(),__func=__rt.__func,__cb=__rt.__cb; QUnit.module(module.id); +/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb; QUnit.module(module.id); function delay(millis, val, _) { var __frame = { name: 'delay', line: 3 }; return __func(_, this, arguments, delay, 2, __frame, function __$delay() { return setTimeout(__cb(_, __frame, 1, 0, function __$delay() { diff --git a/test/common/callbacks/stack-test.html b/test/common/callbacks/stack-test.html index 7e4f9e74..a6e16ada 100644 --- a/test/common/callbacks/stack-test.html +++ b/test/common/callbacks/stack-test.html @@ -5,6 +5,7 @@ + diff --git a/test/common/callbacks/stack-test.js b/test/common/callbacks/stack-test.js index 8b882672..0a929ff5 100644 --- a/test/common/callbacks/stack-test.js +++ b/test/common/callbacks/stack-test.js @@ -1,4 +1,4 @@ -/*** Generated by streamline 0.10.17 (callbacks) --standalone - DO NOT EDIT ***/ var __rt=(function(){var __modules={},mod;function require(p){var m=__modules[p.substring(3)]; return m && m.exports};__modules['globals']=(mod={exports:{}});(function(module, exports){var glob = typeof global === "object" ? global : window;var secret = "_20c7abceb95c4eb88b7ca1895b1170d1";module.exports = (glob[secret] || (glob[secret] = { context: {} }));var g = glob[secret];g.runtime || Object.defineProperty(g, 'runtime', {get: function() { return g.__runtime__; },set: function(value) {if (g.__runtime__ !== value) {if (g.__runtime__) {if (/-fast$/.test(g.__runtime__) ||/-fast$/.test(value)) throw new Error("cannot mix streamline runtimes: " + g.__runtime__ + " and " + value);console.log("warning: mixing streamline runtimes: " + g.__runtime__ + " and " + value);}g.__runtime__ = value;}}});g.withContext = function(fn, cx) {return function() {var oldContext = g.context;g.context = cx || {};try {return fn.apply(this, arguments)} finally {g.context = oldContext;}};};g.setPromise = function(name) {if (g.Promise) return; var req = require; if (name === true) g.Promise = typeof Promise === "function" ? Promise : req('es6-promise');else g.Promise = require(name);};})(mod, mod.exports);__modules['util/future']=(mod={exports:{}});(function(module, exports){(function(exports) {var globals = require("../globals");exports.future = function(fn, args, i) {var err, result, done, q = [], self = this;args = Array.prototype.slice.call(args);args[i] = function(e, r) {err = e, result = r, done = true;q && q.forEach(function(f) {f.call(self, e, r);});q = null;};args[i].__futurecb = true;fn.apply(this, args);var ret = function F(cb) {if (typeof cb !== 'function') {var globals = require('../globals');if (cb == null && globals.Promise) return exports.promise.call(this, F, [], 0);if (cb !== false && !globals.oldStyleFutures) throw new Error("callback missing (argument #0). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error");return F;}if (done) cb.call(self, err, result);else q.push(cb);};ret.__future = true;return ret;};exports.streamlinify = function(fn, idx) {return function() {if (!arguments[idx]) return exports.future.call(this, fn, arguments, idx);else return fn.apply(this, arguments);};};exports.promise = function(fn, args, i) {if (args[i] === false) return exports.future.call(this, fn, args, i);if (args[i] != null) throw new Error("invalid callback: " + typeof(args[i]));if (globals.oldStyleFutures) return exports.future.call(this, fn, args, i);if (!globals.Promise) throw new Error("callback missing (argument #" + i + "). See https://github.com/Sage/streamlinejs/blob/master/FAQ.md#no-callback-given-error");var self = this;args = Array.prototype.slice.call(args);return new globals.Promise(function(resolve, reject) {args[i] = function(e, r) {if (e) reject(e);else resolve(r);};fn.apply(self, args);});};exports.then = function(promise, method, cb) {promise[method](function(r) {cb && cb(null, r);cb = null;}, function(e) {cb && cb(e);cb = null;});};})(typeof exports !== 'undefined' ? exports : (Streamline.future = Streamline.future || {}));})(mod, mod.exports);__modules['callbacks/runtime']=(mod={exports:{}});(function(module, exports){(function(exports) {var __g = require("../globals");__g.runtime = 'callbacks';var __fut = require("../util/future");__g.context = __g.context || {};__g.depth = __g.depth || 0;__g.async = __g.async || false;__g.trampoline = (function() {var q = [];return {queue: function(fn) {q.push(fn);},flush: function() {var oldContext = __g.context;__g.depth++;try {var fn;while (fn = q.shift()) fn();} finally {__g.context = oldContext;__g.depth--;}}}})();exports.runtime = function(filename, oldStyleFutures) {__g.oldStyleFutures = oldStyleFutures;function __func(_, __this, __arguments, fn, index, frame, body) {if (typeof _ !== 'function') return __fut.promise.call(__this, fn, __arguments, index);frame.file = filename;frame.prev = __g.frame;frame.calls = 0;if (frame.prev) frame.prev.calls++;var emitter = __g.emitter;__g.frame = frame;__g.depth++;if (emitter) emitter.emit("enter", frame, _); try {frame.active = true;body();} catch (e) {__setEF(e, frame.prev);__propagate(_, e);} finally {frame.active = false;if (emitter) {emitter.emit("exit", frame);}__g.frame = frame.prev;if (--__g.depth === 0 && __g.trampoline) __g.trampoline.flush();}}return {__g: __g,__func: __func,__cb: __cb,__future: __fut.future,__propagate: __propagate,__trap: __trap,__tryCatch: __tryCatch,__catch: __catch,__forIn: __forIn,__apply: __apply,__construct: __construct,__setEF: __setEF,streamlinify: __fut.streamlinify,__pthen: __fut.then,};};function __cb(_, frame, offset, col, fn, trampo, returnArray) {frame.offset = offset;frame.col = col;var ctx = __g.context;var calls = frame.calls;var emitter = __g.emitter;var ret = function ___(err, result) {if (returnArray) result = Array.prototype.slice.call(arguments, 1);returnArray = false; var oldFrame = __g.frame;__g.frame = frame;var oldContext = __g.context;__g.context = ctx;if (emitter && __g.depth === 0) emitter.emit('resume', frame);if (emitter) emitter.emit('enter', frame);__g.depth++;try {if (trampo && frame.active && __g.trampoline) {__g.trampoline.queue(function() {return ___(err, result);});} else {___.dispatched = true;if (err) {__setEF(err, frame);return _(err);}frame.active = true;return fn(null, result);}} catch (ex) {if (___.dispatched && _.name !== '___' && _.name !== '__trap' && calls !== frame.calls) throw ex;__setEF(ex, frame);return __propagate(_, ex);} finally {frame.active = false;if (emitter) emitter.emit("exit", frame);__g.frame = oldFrame;__g.context = oldContext;if (--__g.depth === 0 && __g.trampoline) __g.trampoline.flush();}};if (emitter && !ret.dispatched) emitter.emit('yield', frame);ret.__streamlined = true;return ret;}function __propagate(_, err) {try {_(err);} catch (ex) {__trap(ex);}}function __trap(err) {if (err) {if (__g.context && __g.context.errorHandler) __g.context.errorHandler(err);else __g.trampoline.queue(function() {throw err;});}}function __tryCatch(_, fn) {try {fn();} catch (e) {try {_(e);} catch (ex) {__trap(ex);}}}function __catch(fn, _) {var frame = __g.frame,context = __g.context;__g.trampoline.queue(function() {var oldFrame = __g.frame,oldContext = __g.context;__g.frame = frame;__g.context = context;try {fn();} catch (ex) {_(ex);} finally {__g.frame = oldFrame;__g.context = oldContext;}});}function __forIn(object) {var array = [];for (var obj in object) {array.push(obj);}return array;}function __apply(cb, fn, thisObj, args, index) {if (cb == null) return __fut.future(__apply, arguments, 0);args = Array.prototype.slice.call(args, 0);args[index != null ? index : args.length] = cb;return fn.apply(thisObj, args);}function __construct(constructor, i) {var key = '__async' + i,f;return constructor[key] || (constructor[key] = function() {var args = arguments;function F() {var self = this;var cb = args[i];args[i] = function(e, r) {cb(e, self);};args[i].__streamlined = cb.__streamlined;args[i].__futurecb = cb.__futurecb;return constructor.apply(self, args);}F.prototype = constructor.prototype;return new F();});}function __setEF(e, f) {function formatStack(e, raw) {var ff = typeof navigator === 'object' && navigator.userAgent.toLowerCase().indexOf('firefox') > -1;if (ff) raw = "Error: " + e.message + '\n' + raw;var s = raw,f, skip;var cut = (e.message || '').split('\n').length;var lines = s.split('\n');s = lines.slice(cut).map(function(l) {var m = /([^@]*)\@(.*?)\:(\d+)(?:\:(\d+))?$/.exec(l);l = m ? " at " + m[1] + " (" + m[2] + ":" + parseInt(m[3]) + ":" + (m[4] || "0") + ")" : l;var i = l.indexOf('__$');if (i >= 0 && !skip) {skip = true;return l.substring(0, i) + l.substring(i + 3);}return skip ? '' : l;}).filter(function(l) {return l;}).join('\n');s = lines.slice(0, cut).join('\n') + '\n <<< async stack >>>' + (skip ? '\n' + s : '');for (var f = e.__frame; f; f = f.prev) {if (f.offset >= 0) s += "\n at " + f.name + " (" + f.file + ":" + (f.line + f.offset) + ":" + (f.col+1) + ")"}s += '\n <<< raw stack >>>' + '\n' + lines.slice(cut).join('\n');return s;};e.__frame = e.__frame || f;if (exports.stackTraceEnabled && e.__lookupGetter__ && e.__lookupGetter__("rawStack") == null) {var getter = e.__lookupGetter__("stack");if (!getter) { var raw = e.stack || "raw stack unavailable";getter = function() {return raw;}}e.__defineGetter__("rawStack", getter);e.__defineGetter__("stack", function() {return formatStack(e, getter.call(this));});}}exports.stackTraceEnabled = true;})(typeof exports !== 'undefined' ? exports : (Streamline.runtime = Streamline.runtime || {}));require && require("../callbacks/builtins");})(mod, mod.exports);return __modules['callbacks/runtime'].exports.runtime('test/common/stack-test._js', false);})(),__func=__rt.__func,__cb=__rt.__cb,__catch=__rt.__catch,__tryCatch=__rt.__tryCatch; +/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb,__catch=__rt.__catch,__tryCatch=__rt.__tryCatch; QUnit.module(module.id); diff --git a/test/common/flows-test._js b/test/common/flows-test._js index a1cbc80d..8d8adc47 100644 --- a/test/common/flows-test._js +++ b/test/common/flows-test._js @@ -336,6 +336,7 @@ asyncTest("collectAll", 4, function(_) { return function(_) { count++; peak = Math.max(count, peak); + setTimeout(_, 10); total = delay(_, i) + total; count--; return 2 * i; @@ -358,6 +359,7 @@ asyncTest("collectOne", 4, function(_) { return function(_) { count++; peak = Math.max(count, peak); + setTimeout(_, 10); total = delay(_, i) + total; count--; return 2 * i; @@ -380,6 +382,7 @@ asyncTest("collectAll with limit", 1, function(_) { return function(_) { count++; peak = Math.max(count, peak); + setTimeout(_, 10); total = delay(_, i) + total; count--; return 2 * i; diff --git a/test/common/generators/eval-test.html b/test/common/generators/eval-test.html index 3043391d..24b4c4b9 100644 --- a/test/common/generators/eval-test.html +++ b/test/common/generators/eval-test.html @@ -5,11 +5,7 @@ - - - - - + diff --git a/test/common/generators/flows-test.html b/test/common/generators/flows-test.html index 82688cfc..7cb795d0 100644 --- a/test/common/generators/flows-test.html +++ b/test/common/generators/flows-test.html @@ -5,11 +5,7 @@ - - - - - + diff --git a/test/common/generators/flows-test.js b/test/common/generators/flows-test.js index e29d260d..a2172987 100644 --- a/test/common/generators/flows-test.js +++ b/test/common/generators/flows-test.js @@ -336,6 +336,7 @@ asyncTest("collectAll", 4, galaxy.unstar(function*(_) { return galaxy.unstar(function*(_) { count++; peak = Math.max(count, peak); + (yield galaxy.invoke(null, setTimeout, [_ , 10], 0)); total = (yield delay(_ , i)) + total; count--; return 2 * i; @@ -358,6 +359,7 @@ asyncTest("collectOne", 4, galaxy.unstar(function*(_) { return galaxy.unstar(function*(_) { count++; peak = Math.max(count, peak); + (yield galaxy.invoke(null, setTimeout, [_ , 10], 0)); total = (yield delay(_ , i)) + total; count--; return 2 * i; @@ -380,6 +382,7 @@ asyncTest("collectAll with limit", 1, galaxy.unstar(function*(_) { return galaxy.unstar(function*(_) { count++; peak = Math.max(count, peak); + (yield galaxy.invoke(null, setTimeout, [_ , 10], 0)); total = (yield delay(_ , i)) + total; count--; return 2 * i; diff --git a/test/common/generators/futures-test.html b/test/common/generators/futures-test.html index 2ecf8290..08909b0b 100644 --- a/test/common/generators/futures-test.html +++ b/test/common/generators/futures-test.html @@ -5,11 +5,7 @@ - - - - - + diff --git a/test/common/generators/stack-test.html b/test/common/generators/stack-test.html index 6260bc0f..fd0f26f8 100644 --- a/test/common/generators/stack-test.html +++ b/test/common/generators/stack-test.html @@ -5,11 +5,7 @@ - - - - - + diff --git a/test/common/stack-test.html b/test/common/stack-test.html deleted file mode 100644 index 71c1cfe4..00000000 --- a/test/common/stack-test.html +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - -

streamline stack tests

-

-

-
    - - -