Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: scripts/abprewrite.js

Issue 6256752412590080: Issue 301 - jshydra: Handle for .. of .. (Closed)
Patch Set: Created April 10, 2014, 5:05 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« jshydra.js ('K') | « jshydra.js ('k') | scripts/astDecompile.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: scripts/abprewrite.js
===================================================================
--- a/scripts/abprewrite.js
+++ b/scripts/abprewrite.js
@@ -315,51 +315,57 @@ function modifyIfStatement(ast)
ast.alternate = ensureBlock(ast.alternate);
return ast;
}
function modifyForInStatement(ast)
{
if (ast.each)
{
- // Convert "for each" loops:
- // for each (var foo in fooList)
- // {
- // ...
- // }
- //
- // Change into:
- // for (var _loopIndex44 = 0; _loopIndex44 < fooList.length; ++_loopIndex44)
- // {
- // var foo = fooList[_loopIndex44];
- // ...
- // }
- let loopIndex = Identifier("_loopIndex" + options.varIndex++);
-
- let block = ensureBlock(ast.body);
- if (ast.left.type == "VariableDeclaration")
- block.body.unshift(VariableDeclaration(ast.left.declarations[0].id, Member(ast.right, loopIndex, true)));
- else
- block.body.unshift(Assignment(ast.left, Member(ast.right, loopIndex, true)));
-
- return {
- type: "ForStatement",
- init: VariableDeclaration(loopIndex, 0),
- test: LogicalExpression(loopIndex, "<", Member(ast.right, "length", false)),
- update: IncExpression(loopIndex),
- body: block
- };
+ print("Use for (.. of ..)!\n");
+ throw new Error("Use for (.. of ..)!");
}
// Make sure that the loop body is always wrapped in a block
ast.body = ensureBlock(ast.body);
return ast;
}
+function modifyForOfStatement(ast)
+{
+ // Convert "for of" loops:
+ // for (var foo of fooList)
+ // {
+ // ...
+ // }
+ //
+ // Change into:
+ // for (var _loopIndex44 = 0; _loopIndex44 < fooList.length; ++_loopIndex44)
+ // {
+ // var foo = fooList[_loopIndex44];
+ // ...
+ // }
+ let loopIndex = Identifier("_loopIndex" + options.varIndex++);
+
+ let block = ensureBlock(ast.body);
+ if (ast.left.type == "VariableDeclaration")
+ block.body.unshift(VariableDeclaration(ast.left.declarations[0].id, Member(ast.right, loopIndex, true)));
+ else
+ block.body.unshift(Assignment(ast.left, Member(ast.right, loopIndex, true)));
+
+ return {
+ type: "ForStatement",
+ init: VariableDeclaration(loopIndex, 0),
+ test: LogicalExpression(loopIndex, "<", Member(ast.right, "length", false)),
+ update: IncExpression(loopIndex),
+ body: block
+ };
+}
+
function modifyLetStatement(ast)
{
if (ast.body.type == "ForStatement" && ast.body.init == null)
{
// Convert back "for" loops written as "let" statements:
// let (foo = 0) for (; foo < bar; ++foo)
// {
// ...
« jshydra.js ('K') | « jshydra.js ('k') | scripts/astDecompile.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld