Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert runtime.js to a normal CommonJS module. #353

Merged
merged 3 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/regenerator-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "Ben Newman <[email protected]>",
"description": "Runtime for Regenerator-compiled generator and async functions.",
"version": "0.12.1",
"main": "runtime-module.js",
"main": "runtime.js",
"keywords": [
"regenerator",
"runtime",
Expand Down
37 changes: 0 additions & 37 deletions packages/regenerator-runtime/runtime-module.js

This file was deleted.

59 changes: 24 additions & 35 deletions packages/regenerator-runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

!(function(global) {
var regeneratorRuntime = (function (exports) {
"use strict";

var Op = Object.prototype;
Expand All @@ -16,23 +16,6 @@
var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";

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;
}

// 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, tryLocsList) {
// If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator.
var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
Expand All @@ -45,7 +28,7 @@

return generator;
}
runtime.wrap = wrap;
exports.wrap = wrap;

// Try/catch helper to minimize deoptimizations. Returns a completion
// record like context.tryEntries[i].completion. This interface could
Expand Down Expand Up @@ -116,7 +99,7 @@
});
}

runtime.isGeneratorFunction = function(genFun) {
exports.isGeneratorFunction = function(genFun) {
var ctor = typeof genFun === "function" && genFun.constructor;
return ctor
? ctor === GeneratorFunction ||
Expand All @@ -126,7 +109,7 @@
: false;
};

runtime.mark = function(genFun) {
exports.mark = function(genFun) {
if (Object.setPrototypeOf) {
Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
} else {
Expand All @@ -143,7 +126,7 @@
// `yield regeneratorRuntime.awrap(x)`, so that the runtime can test
// `hasOwn.call(value, "__await")` to determine if the yielded value is
// meant to be awaited.
runtime.awrap = function(arg) {
exports.awrap = function(arg) {
return { __await: arg };
};

Expand Down Expand Up @@ -218,17 +201,17 @@
AsyncIterator.prototype[asyncIteratorSymbol] = function () {
return this;
};
runtime.AsyncIterator = AsyncIterator;
exports.AsyncIterator = AsyncIterator;

// Note that simple async functions are implemented on top of
// AsyncIterator objects; they just return a Promise for the value of
// the final result produced by the iterator.
runtime.async = function(innerFn, outerFn, self, tryLocsList) {
exports.async = function(innerFn, outerFn, self, tryLocsList) {
var iter = new AsyncIterator(
wrap(innerFn, outerFn, self, tryLocsList)
);

return runtime.isGeneratorFunction(outerFn)
return exports.isGeneratorFunction(outerFn)
? iter // If outerFn is a generator, return the full iterator.
: iter.next().then(function(result) {
return result.done ? result.value : iter.next();
Expand Down Expand Up @@ -445,7 +428,7 @@
this.reset(true);
}

runtime.keys = function(object) {
exports.keys = function(object) {
var keys = [];
for (var key in object) {
keys.push(key);
Expand Down Expand Up @@ -506,7 +489,7 @@
// Return an iterator with no values.
return { next: doneResult };
}
runtime.values = values;
exports.values = values;

function doneResult() {
return { value: undefined, done: true };
Expand Down Expand Up @@ -711,11 +694,17 @@
return ContinueSentinel;
}
};
})(
// In sloppy mode, unbound `this` refers to the global object, fallback to
// Function constructor if we're in global strict mode. That is sadly a form
// of indirect eval which violates Content Security Policy.
(function() {
return this || (typeof self === "object" && self);
})() || Function("return this")()
);

// Regardless of whether this script is executing as a CommonJS module
// or not, return the runtime object so that we can declare the variable
// regeneratorRuntime in the outer scope, which allows this module to be
// injected easily by `bin/regenerator --include-runtime script.js`.
return exports;

}(
// If this script is executing as a CommonJS module, use module.exports
// as the regeneratorRuntime namespace. Otherwise create a new empty
// object. Either way, the resulting object will be used to initialize
// the regeneratorRuntime variable at the top of this file.
typeof module === "object" ? module.exports : {}
));
3 changes: 3 additions & 0 deletions test/run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env bash

set -e
set -u

cd $(dirname $0)/..
ROOT_DIR=$(pwd)

Expand Down
2 changes: 1 addition & 1 deletion test/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* LICENSE file in the root directory of this source tree.
*/

module.exports = require("regenerator-runtime/runtime.js");
global.regeneratorRuntime = require("regenerator-runtime");