Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: pkg/compiler/lib/src/js/rewrite_async.dart

Issue 972063003: Make sure to bind a method to its receiver if it is stored in a temporary. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/language/async_this_bound_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
409 /// Like [_storeIfNecessary] but binds JavaScript `this` of the function if
410 /// the receiver is a [js.PropertyAccess].
411 /// This is necessary because we cannot translate `a.b()` to:
412 /// temp = a.b;
floitsch 2015/03/06 16:32:15 nit: make sure that this mark-downs right. (maybe
413 /// temp();
414 /// This leaves `this` unbound in the call. Take care to bind a
floitsch 2015/03/06 16:32:15 ditto here.
415 js.Expression _storeAndBindIfNecessary(js.Expression result) {
416 // Note that RegExes, js.ArrayInitializer and js.ObjectInitializer are not
417 // [js.Literal]s.
418 if (result is js.Literal) return result;
419 if (result is js.PropertyAccess) {
420 js.Expression tempVar = useTempVar(allocateTempVar());
421 addStatement(js.js.statement('# = #', [tempVar, result.receiver]));
422 addStatement(js.js.statement('# = #.#.bind(#)', [
423 tempVar, tempVar, result.selector, tempVar
424 ]));
425 return tempVar;
426 }
427
428 js.Expression tempVar = useTempVar(allocateTempVar());
429 addStatement(js.js.statement('# = #;', [tempVar, result]));
430 return tempVar;
431 }
432
433 // TODO(sigurdm): This is obsolete - all calls use store: false. Replace with
434 // visitExpression(node);
407 withExpression(js.Expression node, fn(js.Expression result), {bool store}) { 435 withExpression(js.Expression node, fn(js.Expression result), {bool store}) {
408 int oldTempVarIndex = currentTempVarIndex; 436 int oldTempVarIndex = currentTempVarIndex;
409 js.Expression visited = visitExpression(node); 437 js.Expression visited = visitExpression(node);
410 if (store) { 438 if (store) {
411 visited = _storeIfNecessary(visited); 439 visited = _storeIfNecessary(visited);
412 } 440 }
413 var result = fn(visited); 441 var result = fn(visited);
414 currentTempVarIndex = oldTempVarIndex; 442 currentTempVarIndex = oldTempVarIndex;
415 return result; 443 return result;
416 } 444 }
417 445
446 withExpressionBinding(js.Expression node,
447 fn(js.Expression result), {bool store}) {
448 int oldTempVarIndex = currentTempVarIndex;
449 js.Expression visited = visitExpression(node);
450 if (store) {
451 visited = _storeAndBindIfNecessary(visited);
452 }
453 var result = fn(visited);
454 currentTempVarIndex = oldTempVarIndex;
455 return result;
456 }
457
458
418 /// Calls [fn] with the value of evaluating [node1] and [node2]. 459 /// Calls [fn] with the value of evaluating [node1] and [node2].
419 /// 460 ///
420 /// If `shouldTransform(node2)` the first expression is stored in a temporary 461 /// If `shouldTransform(node2)` the first expression is stored in a temporary
421 /// variable. 462 /// variable.
422 /// 463 ///
423 /// This is because node1 must be evaluated before visiting node2, 464 /// This is because node1 must be evaluated before visiting node2,
424 /// because the evaluation of an await or yield cannot be expressed as 465 /// because the evaluation of an await or yield cannot be expressed as
425 /// an expression, visiting node2 it will output statements that 466 /// an expression, visiting node2 it will output statements that
426 /// might have an influence on the value of node1. 467 /// might have an influence on the value of node1.
427 withExpression2(js.Expression node1, js.Expression node2, 468 withExpression2(js.Expression node1, js.Expression node2,
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
797 if (!shouldTransform(target)) { 838 if (!shouldTransform(target)) {
798 addStatement(node); 839 addStatement(node);
799 return; 840 return;
800 } 841 }
801 translateJump(target, breakLabels[target]); 842 translateJump(target, breakLabels[target]);
802 } 843 }
803 844
804 @override 845 @override
805 js.Expression visitCall(js.Call node) { 846 js.Expression visitCall(js.Call node) {
806 bool storeTarget = node.arguments.any(shouldTransform); 847 bool storeTarget = node.arguments.any(shouldTransform);
807 return withExpression(node.target, (target) { 848 return withExpressionBinding(node.target, (target) {
808 return withExpressions(node.arguments, (List<js.Expression> arguments) { 849 return withExpressions(node.arguments, (List<js.Expression> arguments) {
809 return new js.Call(target, arguments); 850 return new js.Call(target, arguments);
810 }); 851 });
811 }, store: storeTarget); 852 }, store: storeTarget);
812 } 853 }
813 854
814 @override 855 @override
815 void visitCase(js.Case node) { 856 void visitCase(js.Case node) {
816 return unreachable(node); 857 return unreachable(node);
817 } 858 }
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
912 953
913 @override 954 @override
914 void visitDefault(js.Default node) => unreachable(node); 955 void visitDefault(js.Default node) => unreachable(node);
915 956
916 @override 957 @override
917 void visitDo(js.Do node) { 958 void visitDo(js.Do node) {
918 if (!shouldTransform(node)) { 959 if (!shouldTransform(node)) {
919 bool oldInsideUntranslatedBreakable = insideUntranslatedBreakable; 960 bool oldInsideUntranslatedBreakable = insideUntranslatedBreakable;
920 insideUntranslatedBreakable = true; 961 insideUntranslatedBreakable = true;
921 withExpression(node.condition, (js.Expression condition) { 962 withExpression(node.condition, (js.Expression condition) {
922 addStatement(js.js.statement('do {#} while (#)', [node.body, condition]) ); 963 addStatement(js.js.statement('do {#} while (#)',
964 [node.body, condition]));
923 }, store: false); 965 }, store: false);
924 insideUntranslatedBreakable = oldInsideUntranslatedBreakable; 966 insideUntranslatedBreakable = oldInsideUntranslatedBreakable;
925 return; 967 return;
926 } 968 }
927 int startLabel = newLabel("do body"); 969 int startLabel = newLabel("do body");
928 970
929 int continueLabel = newLabel("do condition"); 971 int continueLabel = newLabel("do condition");
930 continueLabels[node] = continueLabel; 972 continueLabels[node] = continueLabel;
931 973
932 int afterLabel = newLabel("after do"); 974 int afterLabel = newLabel("after do");
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1140 js.Expression visitLiteralString(js.LiteralString node) => node; 1182 js.Expression visitLiteralString(js.LiteralString node) => node;
1141 1183
1142 @override 1184 @override
1143 visitNamedFunction(js.NamedFunction node) { 1185 visitNamedFunction(js.NamedFunction node) {
1144 unsupported(node); 1186 unsupported(node);
1145 } 1187 }
1146 1188
1147 @override 1189 @override
1148 js.Expression visitNew(js.New node) { 1190 js.Expression visitNew(js.New node) {
1149 bool storeTarget = node.arguments.any(shouldTransform); 1191 bool storeTarget = node.arguments.any(shouldTransform);
1150 return withExpression(node.target, (target) { 1192 return withExpressionBinding(node.target, (target) {
1151 return withExpressions(node.arguments, (List<js.Expression> arguments) { 1193 return withExpressions(node.arguments, (List<js.Expression> arguments) {
1152 return new js.New(target, arguments); 1194 return new js.New(target, arguments);
1153 }); 1195 });
1154 }, store: storeTarget); 1196 }, store: storeTarget);
1155 } 1197 }
1156 1198
1157 @override 1199 @override
1158 js.Expression visitObjectInitializer(js.ObjectInitializer node) { 1200 js.Expression visitObjectInitializer(js.ObjectInitializer node) {
1159 return withExpressions( 1201 return withExpressions(
1160 node.properties.map((js.Property property) => property.value).toList(), 1202 node.properties.map((js.Property property) => property.value).toList(),
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after
1768 addStatement(new js.Return(expression)); 1810 addStatement(new js.Return(expression));
1769 } 1811 }
1770 } 1812 }
1771 1813
1772 @override 1814 @override
1773 js.Fun finishFunction(List<js.Parameter> parameters, 1815 js.Fun finishFunction(List<js.Parameter> parameters,
1774 js.Statement rewrittenBody, 1816 js.Statement rewrittenBody,
1775 js.VariableDeclarationList variableDeclarations) { 1817 js.VariableDeclarationList variableDeclarations) {
1776 // Each iterator invocation on the iterable should work on its own copy of 1818 // Each iterator invocation on the iterable should work on its own copy of
1777 // the parameters. 1819 // the parameters.
1778 // TODO(sigurdm): We only need to do this copying for parameters that are mu tated. 1820 // TODO(sigurdm): We only need to do this copying for parameters that are
1821 // mutated.
1779 List<js.VariableInitialization> declarations = 1822 List<js.VariableInitialization> declarations =
1780 new List<js.VariableInitialization>(); 1823 new List<js.VariableInitialization>();
1781 List<js.Parameter> renamedParameters = new List<js.Parameter>(); 1824 List<js.Parameter> renamedParameters = new List<js.Parameter>();
1782 for (js.Parameter parameter in parameters) { 1825 for (js.Parameter parameter in parameters) {
1783 String name = parameter.name; 1826 String name = parameter.name;
1784 String renamedName = freshName(name); 1827 String renamedName = freshName(name);
1785 renamedParameters.add(new js.Parameter(renamedName)); 1828 renamedParameters.add(new js.Parameter(renamedName));
1786 declarations.add( 1829 declarations.add(
1787 new js.VariableInitialization(new js.VariableDeclaration(name), 1830 new js.VariableInitialization(new js.VariableDeclaration(name),
1788 new js.VariableUse(renamedName))); 1831 new js.VariableUse(renamedName)));
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after
2474 return condition || body; 2517 return condition || body;
2475 } 2518 }
2476 2519
2477 @override 2520 @override
2478 bool visitDartYield(js.DartYield node) { 2521 bool visitDartYield(js.DartYield node) {
2479 hasYield = true; 2522 hasYield = true;
2480 visit(node.expression); 2523 visit(node.expression);
2481 return true; 2524 return true;
2482 } 2525 }
2483 } 2526 }
OLDNEW
« no previous file with comments | « no previous file | tests/language/async_this_bound_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698