Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library rewrite_async; | 5 library rewrite_async; |
| 6 | 6 |
| 7 import "dart:math" show max; | 7 import "dart:math" show max; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'package:_internal/compiler/js_lib/shared/async_await_error_codes.dart' | 10 import 'package:_internal/compiler/js_lib/shared/async_await_error_codes.dart' |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 /// | 392 /// |
| 393 /// - _storeIfNecessary(someLiteral) returns someLiteral. | 393 /// - _storeIfNecessary(someLiteral) returns someLiteral. |
| 394 /// - _storeIfNecessary(someVariable) | 394 /// - _storeIfNecessary(someVariable) |
| 395 /// inserts: var tempX = someVariable | 395 /// inserts: var tempX = someVariable |
| 396 /// returns: tempX | 396 /// returns: tempX |
| 397 /// where tempX is a fresh temporary variable. | 397 /// where tempX is a fresh temporary variable. |
| 398 js.Expression _storeIfNecessary(js.Expression result) { | 398 js.Expression _storeIfNecessary(js.Expression result) { |
| 399 // Note that RegExes, js.ArrayInitializer and js.ObjectInitializer are not | 399 // Note that RegExes, js.ArrayInitializer and js.ObjectInitializer are not |
| 400 // [js.Literal]s. | 400 // [js.Literal]s. |
| 401 if (result is js.Literal) return result; | 401 if (result is js.Literal) return result; |
| 402 | |
| 402 js.Expression tempVar = useTempVar(allocateTempVar()); | 403 js.Expression tempVar = useTempVar(allocateTempVar()); |
| 403 addStatement(js.js.statement('# = #;', [tempVar, result])); | 404 addStatement(js.js.statement('# = #;', [tempVar, result])); |
| 404 return tempVar; | 405 return tempVar; |
| 405 } | 406 } |
| 406 | 407 |
| 408 // TODO(sigurdm): This is obsolete - all calls use store: false. Replace with | |
| 409 // visitExpression(node); | |
| 407 withExpression(js.Expression node, fn(js.Expression result), {bool store}) { | 410 withExpression(js.Expression node, fn(js.Expression result), {bool store}) { |
| 408 int oldTempVarIndex = currentTempVarIndex; | 411 int oldTempVarIndex = currentTempVarIndex; |
| 409 js.Expression visited = visitExpression(node); | 412 js.Expression visited = visitExpression(node); |
| 410 if (store) { | 413 if (store) { |
| 411 visited = _storeIfNecessary(visited); | 414 visited = _storeIfNecessary(visited); |
| 412 } | 415 } |
| 413 var result = fn(visited); | 416 var result = fn(visited); |
| 414 currentTempVarIndex = oldTempVarIndex; | 417 currentTempVarIndex = oldTempVarIndex; |
| 415 return result; | 418 return result; |
| 416 } | 419 } |
| 417 | 420 |
| 421 /// Calls [fn] with the result of evaluating [node]. Taking special care of | |
| 422 /// property accesses. | |
| 423 /// | |
| 424 /// If [store] is true the result of evaluating [node] is stored in a | |
| 425 /// temporary. | |
| 426 /// | |
| 427 /// We cannot rewrite `<receiver>.m()` to: | |
| 428 /// temp = <receiver>.m; | |
|
floitsch
2015/03/06 16:32:16
Make sure this is correct MD.
sigurdm
2015/03/09 14:34:11
Yes - indenting by 4 gives a code block
| |
| 429 /// temp(); | |
| 430 /// Because this leaves `this` unbound in the call. But because dart2js never | |
| 431 /// changes function properties we write: | |
|
floitsch
2015/03/06 16:32:16
Make sure to write a test for this.
Something lik
sigurdm
2015/03/09 14:34:12
Done.
| |
| 432 /// temp = <receiver>; | |
| 433 /// temp.m(); | |
| 434 withExpressionDontStoreFunction(js.Expression node, | |
|
floitsch
2015/03/06 16:32:15
I don't get the name.
sigurdm
2015/03/09 14:34:12
I changed it. Hopefully better.
| |
| 435 fn(js.Expression result), {bool store}) { | |
| 436 int oldTempVarIndex = currentTempVarIndex; | |
| 437 js.Expression visited = visitExpression(node); | |
| 438 js.Expression selector; | |
| 439 if (store) { | |
| 440 if (visited is js.PropertyAccess) { | |
| 441 js.PropertyAccess propertyAccess = visited; | |
| 442 selector = propertyAccess.selector; | |
| 443 visited = propertyAccess.receiver; | |
| 444 } | |
| 445 visited = _storeIfNecessary(visited); | |
|
floitsch
2015/03/06 16:32:15
Avoid updating visited.
Rather have a new variable
sigurdm
2015/03/09 14:34:11
Done.
| |
| 446 } | |
| 447 js.Expression result; | |
| 448 if (selector == null) { | |
| 449 result = fn(visited); | |
| 450 } else { | |
| 451 result = fn(new js.PropertyAccess(visited, selector)); | |
| 452 } | |
| 453 currentTempVarIndex = oldTempVarIndex; | |
| 454 return result; | |
| 455 } | |
| 456 | |
| 457 | |
| 418 /// Calls [fn] with the value of evaluating [node1] and [node2]. | 458 /// Calls [fn] with the value of evaluating [node1] and [node2]. |
| 419 /// | 459 /// |
| 420 /// If `shouldTransform(node2)` the first expression is stored in a temporary | 460 /// If `shouldTransform(node2)` the first expression is stored in a temporary |
| 421 /// variable. | 461 /// variable. |
| 422 /// | 462 /// |
| 423 /// This is because node1 must be evaluated before visiting node2, | 463 /// This is because node1 must be evaluated before visiting node2, |
| 424 /// because the evaluation of an await or yield cannot be expressed as | 464 /// because the evaluation of an await or yield cannot be expressed as |
| 425 /// an expression, visiting node2 it will output statements that | 465 /// an expression, visiting node2 it will output statements that |
| 426 /// might have an influence on the value of node1. | 466 /// might have an influence on the value of node1. |
| 427 withExpression2(js.Expression node1, js.Expression node2, | 467 withExpression2(js.Expression node1, js.Expression node2, |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 798 if (!shouldTransform(target)) { | 838 if (!shouldTransform(target)) { |
| 799 addStatement(node); | 839 addStatement(node); |
| 800 return; | 840 return; |
| 801 } | 841 } |
| 802 translateJump(target, breakLabels[target]); | 842 translateJump(target, breakLabels[target]); |
| 803 } | 843 } |
| 804 | 844 |
| 805 @override | 845 @override |
| 806 js.Expression visitCall(js.Call node) { | 846 js.Expression visitCall(js.Call node) { |
| 807 bool storeTarget = node.arguments.any(shouldTransform); | 847 bool storeTarget = node.arguments.any(shouldTransform); |
| 808 return withExpression(node.target, (target) { | 848 return withExpressionDontStoreFunction(node.target, (target) { |
| 809 return withExpressions(node.arguments, (List<js.Expression> arguments) { | 849 return withExpressions(node.arguments, (List<js.Expression> arguments) { |
| 810 return new js.Call(target, arguments); | 850 return new js.Call(target, arguments); |
| 811 }); | 851 }); |
| 812 }, store: storeTarget); | 852 }, store: storeTarget); |
| 813 } | 853 } |
| 814 | 854 |
| 815 @override | 855 @override |
| 816 void visitCase(js.Case node) { | 856 void visitCase(js.Case node) { |
| 817 return unreachable(node); | 857 return unreachable(node); |
| 818 } | 858 } |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 913 | 953 |
| 914 @override | 954 @override |
| 915 void visitDefault(js.Default node) => unreachable(node); | 955 void visitDefault(js.Default node) => unreachable(node); |
| 916 | 956 |
| 917 @override | 957 @override |
| 918 void visitDo(js.Do node) { | 958 void visitDo(js.Do node) { |
| 919 if (!shouldTransform(node)) { | 959 if (!shouldTransform(node)) { |
| 920 bool oldInsideUntranslatedBreakable = insideUntranslatedBreakable; | 960 bool oldInsideUntranslatedBreakable = insideUntranslatedBreakable; |
| 921 insideUntranslatedBreakable = true; | 961 insideUntranslatedBreakable = true; |
| 922 withExpression(node.condition, (js.Expression condition) { | 962 withExpression(node.condition, (js.Expression condition) { |
| 923 addStatement(js.js.statement('do {#} while (#)', [node.body, condition]) ); | 963 addStatement(js.js.statement('do {#} while (#)', |
| 964 [node.body, condition])); | |
| 924 }, store: false); | 965 }, store: false); |
| 925 insideUntranslatedBreakable = oldInsideUntranslatedBreakable; | 966 insideUntranslatedBreakable = oldInsideUntranslatedBreakable; |
| 926 return; | 967 return; |
| 927 } | 968 } |
| 928 int startLabel = newLabel("do body"); | 969 int startLabel = newLabel("do body"); |
| 929 | 970 |
| 930 int continueLabel = newLabel("do condition"); | 971 int continueLabel = newLabel("do condition"); |
| 931 continueLabels[node] = continueLabel; | 972 continueLabels[node] = continueLabel; |
| 932 | 973 |
| 933 int afterLabel = newLabel("after do"); | 974 int afterLabel = newLabel("after do"); |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1141 js.Expression visitLiteralString(js.LiteralString node) => node; | 1182 js.Expression visitLiteralString(js.LiteralString node) => node; |
| 1142 | 1183 |
| 1143 @override | 1184 @override |
| 1144 visitNamedFunction(js.NamedFunction node) { | 1185 visitNamedFunction(js.NamedFunction node) { |
| 1145 unsupported(node); | 1186 unsupported(node); |
| 1146 } | 1187 } |
| 1147 | 1188 |
| 1148 @override | 1189 @override |
| 1149 js.Expression visitNew(js.New node) { | 1190 js.Expression visitNew(js.New node) { |
| 1150 bool storeTarget = node.arguments.any(shouldTransform); | 1191 bool storeTarget = node.arguments.any(shouldTransform); |
| 1151 return withExpression(node.target, (target) { | 1192 return withExpressionDontStoreFunction(node.target, (target) { |
| 1152 return withExpressions(node.arguments, (List<js.Expression> arguments) { | 1193 return withExpressions(node.arguments, (List<js.Expression> arguments) { |
| 1153 return new js.New(target, arguments); | 1194 return new js.New(target, arguments); |
| 1154 }); | 1195 }); |
| 1155 }, store: storeTarget); | 1196 }, store: storeTarget); |
| 1156 } | 1197 } |
| 1157 | 1198 |
| 1158 @override | 1199 @override |
| 1159 js.Expression visitObjectInitializer(js.ObjectInitializer node) { | 1200 js.Expression visitObjectInitializer(js.ObjectInitializer node) { |
| 1160 return withExpressions( | 1201 return withExpressions( |
| 1161 node.properties.map((js.Property property) => property.value).toList(), | 1202 node.properties.map((js.Property property) => property.value).toList(), |
| (...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1770 addStatement(new js.Return(expression)); | 1811 addStatement(new js.Return(expression)); |
| 1771 } | 1812 } |
| 1772 } | 1813 } |
| 1773 | 1814 |
| 1774 @override | 1815 @override |
| 1775 js.Fun finishFunction(List<js.Parameter> parameters, | 1816 js.Fun finishFunction(List<js.Parameter> parameters, |
| 1776 js.Statement rewrittenBody, | 1817 js.Statement rewrittenBody, |
| 1777 js.VariableDeclarationList variableDeclarations) { | 1818 js.VariableDeclarationList variableDeclarations) { |
| 1778 // Each iterator invocation on the iterable should work on its own copy of | 1819 // Each iterator invocation on the iterable should work on its own copy of |
| 1779 // the parameters. | 1820 // the parameters. |
| 1780 // TODO(sigurdm): We only need to do this copying for parameters that are mu tated. | 1821 // TODO(sigurdm): We only need to do this copying for parameters that are |
| 1822 // mutated. | |
| 1781 List<js.VariableInitialization> declarations = | 1823 List<js.VariableInitialization> declarations = |
| 1782 new List<js.VariableInitialization>(); | 1824 new List<js.VariableInitialization>(); |
| 1783 List<js.Parameter> renamedParameters = new List<js.Parameter>(); | 1825 List<js.Parameter> renamedParameters = new List<js.Parameter>(); |
| 1784 for (js.Parameter parameter in parameters) { | 1826 for (js.Parameter parameter in parameters) { |
| 1785 String name = parameter.name; | 1827 String name = parameter.name; |
| 1786 String renamedName = freshName(name); | 1828 String renamedName = freshName(name); |
| 1787 renamedParameters.add(new js.Parameter(renamedName)); | 1829 renamedParameters.add(new js.Parameter(renamedName)); |
| 1788 declarations.add( | 1830 declarations.add( |
| 1789 new js.VariableInitialization(new js.VariableDeclaration(name), | 1831 new js.VariableInitialization(new js.VariableDeclaration(name), |
| 1790 new js.VariableUse(renamedName))); | 1832 new js.VariableUse(renamedName))); |
| (...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2476 return condition || body; | 2518 return condition || body; |
| 2477 } | 2519 } |
| 2478 | 2520 |
| 2479 @override | 2521 @override |
| 2480 bool visitDartYield(js.DartYield node) { | 2522 bool visitDartYield(js.DartYield node) { |
| 2481 hasYield = true; | 2523 hasYield = true; |
| 2482 visit(node.expression); | 2524 visit(node.expression); |
| 2483 return true; | 2525 return true; |
| 2484 } | 2526 } |
| 2485 } | 2527 } |
| OLD | NEW |