Skip to content

Commit

Permalink
Allow the runtime to be required as a module.
Browse files Browse the repository at this point in the history
Specifically, you can now require("regenerator/runtime-module") if you
want to access the runtime without relying on the global namespace.

Note that runtime.js is now fully strict-mode-compliant.

Fixes #165.
Fixes #166.
  • Loading branch information
benjamn committed Jan 14, 2015
1 parent ee6464e commit 0f2c4c5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
20 changes: 20 additions & 0 deletions runtime-module.js
@@ -0,0 +1,20 @@
// This method of obtaining a reference to the global object needs to be
// kept identical to the way it is obtained in runtime.js
var g =
typeof global === "object" ? global :
typeof window === "object" ? window : this;

var hasOwn = Object.prototype.hasOwnProperty;
var hadRuntime = hasOwn.call(g, "regeneratorRuntime");
var oldRuntime = hadRuntime && g.regeneratorRuntime;
delete g.regeneratorRuntime; // Force reevalutation of runtime.js.

module.exports = require("./runtime");

if (hadRuntime) {
// Restore the original runtime.
g.regeneratorRuntime = oldRuntime;
} else {
// Remove the global property added by runtime.js.
delete g.regeneratorRuntime;
}
34 changes: 25 additions & 9 deletions runtime.js
Expand Up @@ -8,18 +8,30 @@
* the same directory.
*/

!(function() {
!(function(global) {
"use strict";

var hasOwn = Object.prototype.hasOwnProperty;
var undefined; // More compressible than void 0.
var iteratorSymbol =
typeof Symbol === "function" && Symbol.iterator || "@@iterator";

if (typeof regeneratorRuntime === "object") {
var inModule = typeof module === "object";
var runtime = global.regeneratorRuntime;
if (runtime) {
if (inModule) {
// If regeneratorRuntime is defined globally and we're in a module,
// make the exports object identical to regeneratorRuntime.
module.exports = runtime;
}
// Don't bother evaluating the rest of this file if the runtime was
// already defined globally.
return;
}

var runtime = regeneratorRuntime =
typeof exports === "undefined" ? {} : exports;
// Define the runtime globally (as expected by generated code) as either
// module.exports (if we're in a module) or a new, empty object.
runtime = global.regeneratorRuntime = inModule ? module.exports : {};

function wrap(innerFn, outerFn, self, tryList) {
return new Generator(innerFn, outerFn, self || null, tryList || []);
Expand Down Expand Up @@ -313,9 +325,7 @@
}

if (!isNaN(iterable.length)) {
var i = -1;

function next() {
var i = -1, next = function next() {
while (++i < iterable.length) {
if (hasOwn.call(iterable, i)) {
next.value = iterable[i];
Expand All @@ -328,7 +338,7 @@
next.done = true;

return next;
}
};

return next.next = next;
}
Expand Down Expand Up @@ -505,4 +515,10 @@
return ContinueSentinel;
}
};
})();
})(
// Among the various tricks for obtaining a reference to the global
// object, this seems to be the most reliable technique that does not
// use indirect eval (which violates Content Security Policy).
typeof global === "object" ? global :
typeof window === "object" ? window : this
);

0 comments on commit 0f2c4c5

Please sign in to comment.