From 983ca5c71fa7cd5c076a4ae42ff69b9128bf5035 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 17 Nov 2015 23:02:21 -0800 Subject: [PATCH] add function.sent --- .../package.json | 5 +- .../src/visit.js | 26 ++- .../fixtures/function-sent/example/exec.js | 19 +++ .../test/fixtures/function-sent/options.json | 3 + .../test/index.js | 1 + packages/babel-polyfill/package.json | 2 +- packages/babel-polyfill/src/index.js | 2 +- packages/babel-regenerator-runtime/LICENSE | 14 ++ packages/babel-regenerator-runtime/PATENTS | 33 ++++ .../babel-regenerator-runtime/package.json | 9 ++ .../runtime-module.js | 0 .../runtime.js | 3 +- packages/babel-runtime/package.json | 2 +- packages/babel-runtime/scripts/build-dist.js | 4 +- packages/babylon/README.md | 1 + packages/babylon/src/parser/expression.js | 36 +++-- .../function-sent/inside-function/actual.js | 3 + .../inside-function/options.json | 3 + .../function-sent/inside-generator/actual.js | 3 + .../inside-generator/expected.json | 148 ++++++++++++++++++ .../experimental/function-sent/options.json | 3 + 21 files changed, 295 insertions(+), 25 deletions(-) create mode 100644 packages/babel-plugin-transform-regenerator/test/fixtures/function-sent/example/exec.js create mode 100644 packages/babel-plugin-transform-regenerator/test/fixtures/function-sent/options.json create mode 100644 packages/babel-plugin-transform-regenerator/test/index.js create mode 100644 packages/babel-regenerator-runtime/LICENSE create mode 100644 packages/babel-regenerator-runtime/PATENTS create mode 100644 packages/babel-regenerator-runtime/package.json rename packages/{babel-plugin-transform-regenerator => babel-regenerator-runtime}/runtime-module.js (100%) rename packages/{babel-plugin-transform-regenerator => babel-regenerator-runtime}/runtime.js (99%) create mode 100644 packages/babylon/test/fixtures/experimental/function-sent/inside-function/actual.js create mode 100644 packages/babylon/test/fixtures/experimental/function-sent/inside-function/options.json create mode 100644 packages/babylon/test/fixtures/experimental/function-sent/inside-generator/actual.js create mode 100644 packages/babylon/test/fixtures/experimental/function-sent/inside-generator/expected.json create mode 100644 packages/babylon/test/fixtures/experimental/function-sent/options.json diff --git a/packages/babel-plugin-transform-regenerator/package.json b/packages/babel-plugin-transform-regenerator/package.json index 663152da6e..d304b26519 100644 --- a/packages/babel-plugin-transform-regenerator/package.json +++ b/packages/babel-plugin-transform-regenerator/package.json @@ -16,5 +16,8 @@ "babylon": "^6.1.18", "private": "~0.1.5" }, - "license": "BSD" + "license": "BSD", + "devDependencies": { + "babel-helper-plugin-test-runner": "^6.1.18" + } } diff --git a/packages/babel-plugin-transform-regenerator/src/visit.js b/packages/babel-plugin-transform-regenerator/src/visit.js index 10706d43f0..9750fdd837 100644 --- a/packages/babel-plugin-transform-regenerator/src/visit.js +++ b/packages/babel-plugin-transform-regenerator/src/visit.js @@ -37,13 +37,20 @@ exports.visitor = { return; } + let contextId = path.scope.generateUidIdentifier("context"); + let argsId = path.scope.generateUidIdentifier("args"); + path.ensureBlock(); + let bodyBlockPath = path.get("body"); if (node.async) { - path.get("body").traverse(awaitVisitor); + bodyBlockPath.traverse(awaitVisitor); } - let bodyBlockPath = path.get("body"); + bodyBlockPath.traverse(functionSentVisitor, { + context: contextId + }); + let outerBody = []; let innerBody = []; @@ -68,8 +75,6 @@ exports.visitor = { // if a temporary name has to be synthesized. t.assertIdentifier(node.id); let innerFnId = t.identifier(node.id.name + "$"); - let contextId = path.scope.generateUidIdentifier("context"); - let argsId = path.scope.generateUidIdentifier("args"); // Turn all declarations into vars, and replace the original // declarations with equivalent assignment expressions. @@ -121,8 +126,7 @@ exports.visitor = { node.async = false; } - if (wasGeneratorFunction && - t.isExpression(node)) { + if (wasGeneratorFunction && t.isExpression(node)) { path.replaceWith(t.callExpression(util.runtimeProperty("mark"), [node])); } } @@ -223,6 +227,16 @@ let argumentsVisitor = { } }; +let functionSentVisitor = { + MetaProperty(path) { + let { node } = path; + + if (node.meta.name === "function" && node.property.name === "sent") { + path.replaceWith(t.memberExpression(this.context, t.identifier("_sent"))); + } + } +}; + let awaitVisitor = { Function: function(path) { path.skip(); // Don't descend into nested function scopes. diff --git a/packages/babel-plugin-transform-regenerator/test/fixtures/function-sent/example/exec.js b/packages/babel-plugin-transform-regenerator/test/fixtures/function-sent/example/exec.js new file mode 100644 index 0000000000..7d66ff138e --- /dev/null +++ b/packages/babel-plugin-transform-regenerator/test/fixtures/function-sent/example/exec.js @@ -0,0 +1,19 @@ +function *adder(total = 0) { + let increment = 1; + while (true) { + let request = function.sent; + switch (request) { + case undefined: break; + case "done": return total; + default: increment = Number(request); + } + yield total += increment; + } +} + +let tally = adder(); +tally.next(0.1); +tally.next(0.1); +tally.next(0.1); +let last = tally.next("done"); +assert.equal(last.value, 0.30000000000000004); diff --git a/packages/babel-plugin-transform-regenerator/test/fixtures/function-sent/options.json b/packages/babel-plugin-transform-regenerator/test/fixtures/function-sent/options.json new file mode 100644 index 0000000000..f0b7e79d68 --- /dev/null +++ b/packages/babel-plugin-transform-regenerator/test/fixtures/function-sent/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["syntax-function-sent", "transform-regenerator", "transform-es2015-parameters", "transform-es2015-block-scoping"] +} diff --git a/packages/babel-plugin-transform-regenerator/test/index.js b/packages/babel-plugin-transform-regenerator/test/index.js new file mode 100644 index 0000000000..1f6634aabd --- /dev/null +++ b/packages/babel-plugin-transform-regenerator/test/index.js @@ -0,0 +1 @@ +require("babel-helper-plugin-test-runner")(__dirname); diff --git a/packages/babel-polyfill/package.json b/packages/babel-polyfill/package.json index e4ef938a15..ce0328ac4a 100644 --- a/packages/babel-polyfill/package.json +++ b/packages/babel-polyfill/package.json @@ -9,7 +9,7 @@ "main": "lib/index.js", "dependencies": { "core-js": "^1.0.1", - "regenerator": "^0.8.36", + "babel-regenerator-runtime": "^6.0.0", "babel-runtime": "^5.0.0" } } diff --git a/packages/babel-polyfill/src/index.js b/packages/babel-polyfill/src/index.js index 64c6e48a50..a281041a77 100644 --- a/packages/babel-polyfill/src/index.js +++ b/packages/babel-polyfill/src/index.js @@ -4,4 +4,4 @@ if (global._babelPolyfill) { global._babelPolyfill = true; import "core-js/shim"; -import "regenerator/runtime"; +import "babel-regenerator-runtime"; diff --git a/packages/babel-regenerator-runtime/LICENSE b/packages/babel-regenerator-runtime/LICENSE new file mode 100644 index 0000000000..187bfe283d --- /dev/null +++ b/packages/babel-regenerator-runtime/LICENSE @@ -0,0 +1,14 @@ +BSD License + +For "regenerator" software + +Copyright (c) 2014, Facebook, Inc. +All rights reserved. + +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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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. diff --git a/packages/babel-regenerator-runtime/PATENTS b/packages/babel-regenerator-runtime/PATENTS new file mode 100644 index 0000000000..a2bd67d0db --- /dev/null +++ b/packages/babel-regenerator-runtime/PATENTS @@ -0,0 +1,33 @@ +Additional Grant of Patent Rights Version 2 + +"Software" means the Regenerator software distributed by Facebook, Inc. + +Facebook, Inc. ("Facebook") hereby grants to each recipient of the Software +("you") a perpetual, worldwide, royalty-free, non-exclusive, irrevocable +(subject to the termination provision below) license under any Necessary +Claims, to make, have made, use, sell, offer to sell, import, and otherwise +transfer the Software. For avoidance of doubt, no license is granted under +Facebook's rights in any patent claims that are infringed by (i) modifications +to the Software made by you or any third party or (ii) the Software in +combination with any software or other technology. + +The license granted hereunder will terminate, automatically and without notice, +if you (or any of your subsidiaries, corporate affiliates or agents) initiate +directly or indirectly, or take a direct financial interest in, any Patent +Assertion: (i) against Facebook or any of its subsidiaries or corporate +affiliates, (ii) against any party if such Patent Assertion arises in whole or +in part from any software, technology, product or service of Facebook or any of +its subsidiaries or corporate affiliates, or (iii) against any party relating +to the Software. Notwithstanding the foregoing, if Facebook or any of its +subsidiaries or corporate affiliates files a lawsuit alleging patent +infringement against you in the first instance, and you respond by filing a +patent infringement counterclaim in that lawsuit against that party that is +unrelated to the Software, the license granted hereunder will not terminate +under section (i) of this paragraph due to such counterclaim. + +A "Necessary Claim" is a claim of a patent owned by Facebook that is +necessarily infringed by the Software standing alone. + +A "Patent Assertion" is any lawsuit or other action alleging direct, indirect, +or contributory infringement or inducement to infringe any patent, including a +cross-claim or counterclaim. diff --git a/packages/babel-regenerator-runtime/package.json b/packages/babel-regenerator-runtime/package.json new file mode 100644 index 0000000000..83214dd42f --- /dev/null +++ b/packages/babel-regenerator-runtime/package.json @@ -0,0 +1,9 @@ +{ + "name": "babel-regenerator-runtime", + "author": "Ben Newman ", + "description": "", + "version": "6.1.18", + "homepage": "https://github.com/babel/babel/tree/master/packages/babel-regenerator-runtime", + "main": "runtime.js", + "license": "BSD" +} diff --git a/packages/babel-plugin-transform-regenerator/runtime-module.js b/packages/babel-regenerator-runtime/runtime-module.js similarity index 100% rename from packages/babel-plugin-transform-regenerator/runtime-module.js rename to packages/babel-regenerator-runtime/runtime-module.js diff --git a/packages/babel-plugin-transform-regenerator/runtime.js b/packages/babel-regenerator-runtime/runtime.js similarity index 99% rename from packages/babel-plugin-transform-regenerator/runtime.js rename to packages/babel-regenerator-runtime/runtime.js index 911a077b89..ab9ee58b13 100644 --- a/packages/babel-plugin-transform-regenerator/runtime.js +++ b/packages/babel-regenerator-runtime/runtime.js @@ -300,12 +300,13 @@ } if (method === "next") { + context._sent = arg; + if (state === GenStateSuspendedYield) { context.sent = arg; } else { context.sent = undefined; } - } else if (method === "throw") { if (state === GenStateSuspendedStart) { state = GenStateCompleted; diff --git a/packages/babel-runtime/package.json b/packages/babel-runtime/package.json index ab965ac1dc..56d00b6b36 100644 --- a/packages/babel-runtime/package.json +++ b/packages/babel-runtime/package.json @@ -13,6 +13,6 @@ "babel-plugin-transform-runtime": "^6.1.18", "babel-template": "^6.1.18", "babel-runtime": "^5.0.0", - "regenerator": "^0.8.34" + "babel-regenerator-runtime": "^6.0.0" } } diff --git a/packages/babel-runtime/scripts/build-dist.js b/packages/babel-runtime/scripts/build-dist.js index 8df84595e3..c3f27b6a47 100644 --- a/packages/babel-runtime/scripts/build-dist.js +++ b/packages/babel-runtime/scripts/build-dist.js @@ -92,5 +92,5 @@ each(helpers.list, function (helperName) { writeFile("helpers/" + helperAlias + ".js", buildHelper(helperName)); }); -writeFile("regenerator/index.js", readFile("regenerator/runtime-module", true)); -writeFile("regenerator/runtime.js", selfContainify(readFile("regenerator/runtime"))); +writeFile("regenerator/index.js", readFile("../../babel-regenerator-runtime/runtime-module", true)); +writeFile("regenerator/runtime.js", selfContainify(readFile("../../babel-regenerator-runtime/runtime"))); diff --git a/packages/babylon/README.md b/packages/babylon/README.md index 7013566608..69d83a6af9 100644 --- a/packages/babylon/README.md +++ b/packages/babylon/README.md @@ -71,3 +71,4 @@ require("babylon").parse("code", { - `exportExtensions` - `exponentiationOperator` - `asyncGenerators` + - `functionSent` diff --git a/packages/babylon/src/parser/expression.js b/packages/babylon/src/parser/expression.js index 784eb3a3ea..64b7f1c18d 100644 --- a/packages/babylon/src/parser/expression.js +++ b/packages/babylon/src/parser/expression.js @@ -28,7 +28,7 @@ const pp = Parser.prototype; // strict mode, init properties are also not allowed to be repeated. pp.checkPropClash = function (prop, propHash) { - if (prop.computed || prop.method) return; + if (prop.computed) return; let key = prop.key; let name; @@ -459,9 +459,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) { return this.parseObj(false, refShorthandDefaultPos); case tt._function: - node = this.startNode(); - this.next(); - return this.parseFunction(node, false); + return this.parseFunctionExpression(); case tt.at: this.parseDecorators(); @@ -493,6 +491,27 @@ pp.parseExprAtom = function (refShorthandDefaultPos) { } }; +pp.parseFunctionExpression = function () { + let node = this.startNode(); + let meta = this.parseIdentifier(true); + if (this.state.inGenerator && this.eat(tt.dot) && this.hasPlugin("functionSent")) { + return this.parseMetaProperty(node, meta, "sent"); + } else { + return this.parseFunction(node, false); + } +}; + +pp.parseMetaProperty = function (node, meta, propertyName) { + node.meta = meta; + node.property = this.parseIdentifier(true); + + if (node.property.name !== propertyName) { + this.raise(node.property.start, `The only valid meta property for new is ${meta.name}.${propertyName}`); + } + + return this.finishNode(node, "MetaProperty"); +}; + pp.parseLiteral = function (value, type) { let node = this.startNode(); this.addExtra(node, "rawValue", value); @@ -592,14 +611,7 @@ pp.parseNew = function () { let meta = this.parseIdentifier(true); if (this.eat(tt.dot)) { - node.meta = meta; - node.property = this.parseIdentifier(true); - - if (node.property.name !== "target") { - this.raise(node.property.start, "The only valid meta property for new is new.target"); - } - - return this.finishNode(node, "MetaProperty"); + return this.parseMetaProperty(node, meta, "target"); } node.callee = this.parseNoCallExpr(); diff --git a/packages/babylon/test/fixtures/experimental/function-sent/inside-function/actual.js b/packages/babylon/test/fixtures/experimental/function-sent/inside-function/actual.js new file mode 100644 index 0000000000..203379b698 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/function-sent/inside-function/actual.js @@ -0,0 +1,3 @@ +function foo() { + return function.sent; +} diff --git a/packages/babylon/test/fixtures/experimental/function-sent/inside-function/options.json b/packages/babylon/test/fixtures/experimental/function-sent/inside-function/options.json new file mode 100644 index 0000000000..43a82ec8a1 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/function-sent/inside-function/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (2:17)" +} diff --git a/packages/babylon/test/fixtures/experimental/function-sent/inside-generator/actual.js b/packages/babylon/test/fixtures/experimental/function-sent/inside-generator/actual.js new file mode 100644 index 0000000000..28760b261b --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/function-sent/inside-generator/actual.js @@ -0,0 +1,3 @@ +function* foo() { + return function.sent; +} diff --git a/packages/babylon/test/fixtures/experimental/function-sent/inside-generator/expected.json b/packages/babylon/test/fixtures/experimental/function-sent/inside-generator/expected.json new file mode 100644 index 0000000000..c0692c9a9d --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/function-sent/inside-generator/expected.json @@ -0,0 +1,148 @@ +{ + "type": "File", + "start": 0, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "name": "foo" + }, + "generator": true, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 16, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 20, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "argument": { + "type": "MetaProperty", + "start": 27, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "meta": { + "type": "Identifier", + "start": 27, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "name": "function" + }, + "property": { + "type": "Identifier", + "start": 36, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "name": "sent" + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/experimental/function-sent/options.json b/packages/babylon/test/fixtures/experimental/function-sent/options.json new file mode 100644 index 0000000000..fea46c76c3 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/function-sent/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["functionSent"] +}