add function.sent

This commit is contained in:
Sebastian McKenzie
2015-11-17 23:02:21 -08:00
parent f8c2beb9d8
commit 983ca5c71f
21 changed files with 295 additions and 25 deletions

View File

@@ -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"
}
}

View File

@@ -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.

View File

@@ -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);

View File

@@ -0,0 +1,3 @@
{
"plugins": ["syntax-function-sent", "transform-regenerator", "transform-es2015-parameters", "transform-es2015-block-scoping"]
}

View File

@@ -0,0 +1 @@
require("babel-helper-plugin-test-runner")(__dirname);

View File

@@ -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"
}
}

View File

@@ -4,4 +4,4 @@ if (global._babelPolyfill) {
global._babelPolyfill = true;
import "core-js/shim";
import "regenerator/runtime";
import "babel-regenerator-runtime";

View File

@@ -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.

View File

@@ -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.

View File

@@ -0,0 +1,9 @@
{
"name": "babel-regenerator-runtime",
"author": "Ben Newman <bn@cs.stanford.edu>",
"description": "",
"version": "6.1.18",
"homepage": "https://github.com/babel/babel/tree/master/packages/babel-regenerator-runtime",
"main": "runtime.js",
"license": "BSD"
}

View File

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

View File

@@ -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"
}
}

View File

@@ -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")));

View File

@@ -71,3 +71,4 @@ require("babylon").parse("code", {
- `exportExtensions`
- `exponentiationOperator`
- `asyncGenerators`
- `functionSent`

View File

@@ -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();

View File

@@ -0,0 +1,3 @@
function foo() {
return function.sent;
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (2:17)"
}

View File

@@ -0,0 +1,3 @@
function* foo() {
return function.sent;
}

View File

@@ -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": []
}
}

View File

@@ -0,0 +1,3 @@
{
"plugins": ["functionSent"]
}