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

Side by Side Diff: scripts/abprewrite.js

Issue 6256752412590080: Issue 301 - jshydra: Handle for .. of .. (Closed)
Patch Set: Created April 10, 2014, 5:05 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 // This script rewrites AST to be compatible with JavaScript 1.5 and decompiles 1 // This script rewrites AST to be compatible with JavaScript 1.5 and decompiles
2 // the modified tree then 2 // the modified tree then
3 3
4 include("../scripts/astDecompile.js"); 4 include("../scripts/astDecompile.js");
5 include("../utils/beautify.js"); 5 include("../utils/beautify.js");
6 6
7 let headerPrinted = false; 7 let headerPrinted = false;
8 8
9 // See https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API for 9 // See https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API for
10 // AST structure. 10 // AST structure.
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 ast.consequent = ensureBlock(ast.consequent); 313 ast.consequent = ensureBlock(ast.consequent);
314 if (ast.alternate && ast.alternate.type != "IfStatement") 314 if (ast.alternate && ast.alternate.type != "IfStatement")
315 ast.alternate = ensureBlock(ast.alternate); 315 ast.alternate = ensureBlock(ast.alternate);
316 return ast; 316 return ast;
317 } 317 }
318 318
319 function modifyForInStatement(ast) 319 function modifyForInStatement(ast)
320 { 320 {
321 if (ast.each) 321 if (ast.each)
322 { 322 {
323 // Convert "for each" loops: 323 print("Use for (.. of ..)!\n");
324 // for each (var foo in fooList) 324 throw new Error("Use for (.. of ..)!");
325 // {
326 // ...
327 // }
328 //
329 // Change into:
330 // for (var _loopIndex44 = 0; _loopIndex44 < fooList.length; ++_loopIndex44)
331 // {
332 // var foo = fooList[_loopIndex44];
333 // ...
334 // }
335 let loopIndex = Identifier("_loopIndex" + options.varIndex++);
336
337 let block = ensureBlock(ast.body);
338 if (ast.left.type == "VariableDeclaration")
339 block.body.unshift(VariableDeclaration(ast.left.declarations[0].id, Member (ast.right, loopIndex, true)));
340 else
341 block.body.unshift(Assignment(ast.left, Member(ast.right, loopIndex, true) ));
342
343 return {
344 type: "ForStatement",
345 init: VariableDeclaration(loopIndex, 0),
346 test: LogicalExpression(loopIndex, "<", Member(ast.right, "length", false) ),
347 update: IncExpression(loopIndex),
348 body: block
349 };
350 } 325 }
351 326
352 // Make sure that the loop body is always wrapped in a block 327 // Make sure that the loop body is always wrapped in a block
353 ast.body = ensureBlock(ast.body); 328 ast.body = ensureBlock(ast.body);
354 329
355 return ast; 330 return ast;
356 } 331 }
357 332
333 function modifyForOfStatement(ast)
334 {
335 // Convert "for of" loops:
336 // for (var foo of fooList)
337 // {
338 // ...
339 // }
340 //
341 // Change into:
342 // for (var _loopIndex44 = 0; _loopIndex44 < fooList.length; ++_loopIndex44)
343 // {
344 // var foo = fooList[_loopIndex44];
345 // ...
346 // }
347 let loopIndex = Identifier("_loopIndex" + options.varIndex++);
348
349 let block = ensureBlock(ast.body);
350 if (ast.left.type == "VariableDeclaration")
351 block.body.unshift(VariableDeclaration(ast.left.declarations[0].id, Member(a st.right, loopIndex, true)));
352 else
353 block.body.unshift(Assignment(ast.left, Member(ast.right, loopIndex, true))) ;
354
355 return {
356 type: "ForStatement",
357 init: VariableDeclaration(loopIndex, 0),
358 test: LogicalExpression(loopIndex, "<", Member(ast.right, "length", false)),
359 update: IncExpression(loopIndex),
360 body: block
361 };
362 }
363
358 function modifyLetStatement(ast) 364 function modifyLetStatement(ast)
359 { 365 {
360 if (ast.body.type == "ForStatement" && ast.body.init == null) 366 if (ast.body.type == "ForStatement" && ast.body.init == null)
361 { 367 {
362 // Convert back "for" loops written as "let" statements: 368 // Convert back "for" loops written as "let" statements:
363 // let (foo = 0) for (; foo < bar; ++foo) 369 // let (foo = 0) for (; foo < bar; ++foo)
364 // { 370 // {
365 // ... 371 // ...
366 // } 372 // }
367 // 373 //
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 let code = 'require.scopes["' + options.filename + '"] = (function() {\n' + 543 let code = 'require.scopes["' + options.filename + '"] = (function() {\n' +
538 'var exports = {};\n' + 544 'var exports = {};\n' +
539 decompileAST(ast) + 545 decompileAST(ast) +
540 'return exports;\n' + 546 'return exports;\n' +
541 '})();\n'; 547 '})();\n';
542 _print(js_beautify(code, options)); 548 _print(js_beautify(code, options));
543 } 549 }
544 else 550 else
545 _print(js_beautify(decompileAST(ast), options)); 551 _print(js_beautify(decompileAST(ast), options));
546 } 552 }
OLDNEW
« jshydra.js ('K') | « jshydra.js ('k') | scripts/astDecompile.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld