forked from google/traceur-compiler
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow compiling async functions to generators
This is done by setting `--async-functions=generator --generators=parse`. If you pass --output-language=es6 then we know transform async functions to generators. Related to google#1231 Fixes google#1780
- Loading branch information
Showing
12 changed files
with
290 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2015 Traceur Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the 'License'); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an 'AS IS' BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { | ||
FunctionBody, | ||
FunctionDeclaration, | ||
FunctionExpression, | ||
Method, | ||
YieldExpression | ||
} from '../syntax/trees/ParseTrees.js'; | ||
import {ParenTrait} from './ParenTrait.js'; | ||
import {parseStatement} from './PlaceholderParser.js'; | ||
import {STAR} from '../syntax/TokenType.js'; | ||
import {TempVarTransformer} from './TempVarTransformer.js'; | ||
import FindArguments from './FindArguments.js'; | ||
import { | ||
createIdentifierExpression, | ||
createNullLiteral, | ||
} from './ParseTreeFactory.js'; | ||
import {ARGUMENTS} from '../syntax/PredefinedName.js'; | ||
|
||
export class AsyncToGeneratorTransformer extends ParenTrait(TempVarTransformer) { | ||
constructor(identifierGenerator, reporter, options) { | ||
super(identifierGenerator, reporter, options); | ||
this.inAsyncFunction_ = false; | ||
} | ||
|
||
transformFunctionDeclaration(tree) { | ||
if (tree.isAsyncFunction()) { | ||
return this.transformFunctionShared_(tree, FunctionDeclaration); | ||
} | ||
return super.transformFunctionDeclaration(tree); | ||
} | ||
|
||
transformFunctionExpression(tree) { | ||
if (tree.isAsyncFunction()) { | ||
return this.transformFunctionShared_(tree, FunctionExpression); | ||
} | ||
return super.transformFunctionExpression(tree); | ||
} | ||
|
||
transformFunctionShared_(tree, ctor) { | ||
const parameterList = this.transformAny(tree.parameterList); | ||
const typeAnnotation = this.transformAny(tree.typeAnnotation); | ||
const annotations = this.transformList(tree.annotations); | ||
const body = this.transformAsyncBody_(tree.body); | ||
return new ctor(tree.location, tree.name, null, | ||
parameterList, typeAnnotation, annotations, body); | ||
} | ||
|
||
transformAsyncBody_(body) { | ||
const inAsyncFunction = this.inAsyncFunction_; | ||
this.inAsyncFunction_ = true; | ||
body = this.transformFunctionBody(body); | ||
|
||
body = wrapBodyInSpawn(body); | ||
this.inAsyncFunction_ = inAsyncFunction; | ||
return body; | ||
} | ||
|
||
transformMethod(tree) { | ||
if (tree.isAsyncFunction()) { | ||
const name = this.transformAny(tree.name); | ||
const parameterList = this.transformAny(tree.parameterList); | ||
const typeAnnotation = this.transformAny(tree.typeAnnotation); | ||
const annotations = this.transformList(tree.annotations); | ||
const body = this.transformAsyncBody_(tree.body); | ||
return new Method(tree.location, tree.isStatic, null, name, | ||
parameterList, typeAnnotation, annotations, body, | ||
tree.debugName); | ||
} | ||
return super.transformMethod(tree); | ||
} | ||
|
||
transformAwaitExpression(tree) { | ||
if (this.inAsyncFunction_) { | ||
const expression = this.transformAny(tree.expression); | ||
return new YieldExpression(tree.location, expression, false); | ||
} | ||
return super.transformAwaitExpression(tree); | ||
} | ||
} | ||
|
||
function wrapBodyInSpawn(body) { | ||
const visitor = new FindArguments(); | ||
visitor.visitAny(body); | ||
const hasArguments = visitor.found; | ||
const argExpr = visitor.found ? | ||
createIdentifierExpression(ARGUMENTS) : | ||
createNullLiteral(); | ||
const statement = parseStatement | ||
`return $traceurRuntime.spawn(this, ${argExpr}, function*() { ${body} });` | ||
return new FunctionBody(body.location, [statement]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2016 Traceur Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the 'License'); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an 'AS IS' BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {ARGUMENTS} from '../syntax/PredefinedName.js'; | ||
import {FindInFunctionScope} from './FindInFunctionScope.js'; | ||
|
||
/** | ||
* This is used to find whether a function contains a reference to `arguments`. | ||
*/ | ||
export default class FindArguments extends FindInFunctionScope { | ||
visitIdentifierExpression(tree) { | ||
if (tree.identifierToken.value === ARGUMENTS) { | ||
this.found = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2016 Traceur Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
export default function spawn(self, args, gen) { | ||
return new Promise((resolve, reject) => { | ||
function fulfill(v) { | ||
try { | ||
step(gen.next(v)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
|
||
function rejected(v) { | ||
try { | ||
step(gen.throw(v)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
|
||
function step(res) { | ||
if (res.done) { | ||
resolve(res.value); | ||
} else { | ||
Promise.resolve(res.value).then(fulfill, rejected); | ||
} | ||
} | ||
|
||
step((gen = gen.apply(self, args)).next()); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2016 Traceur Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import spawn from './modules/spawn.js' | ||
$traceurRuntime.spawn = spawn; |
Oops, something went wrong.