OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 | 5 |
6 #include "vm/ast_printer.h" | 6 #include "vm/ast_printer.h" |
7 #include "vm/class_finalizer.h" | 7 #include "vm/class_finalizer.h" |
8 #include "vm/debugger.h" | 8 #include "vm/debugger.h" |
9 #include "vm/longjump.h" | 9 #include "vm/longjump.h" |
10 #include "vm/object.h" | 10 #include "vm/object.h" |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 " 0 StackVar scope=1 begin=0 end=0 name=this\n" | 444 " 0 StackVar scope=1 begin=0 end=0 name=this\n" |
445 | 445 |
446 // No context is saved here since no vars are captured. | 446 // No context is saved here since no vars are captured. |
447 "::.doIt\n" | 447 "::.doIt\n" |
448 " 0 StackVar scope=2 begin=29 end=77 name=x\n", | 448 " 0 StackVar scope=2 begin=29 end=77 name=x\n", |
449 CaptureVarsAtLine(lib, "doIt", 12)); | 449 CaptureVarsAtLine(lib, "doIt", 12)); |
450 } | 450 } |
451 | 451 |
452 | 452 |
453 TEST_CASE(Parser_AllocateVariables_CaptureLoopVar) { | 453 TEST_CASE(Parser_AllocateVariables_CaptureLoopVar) { |
| 454 // This test verifies that... |
| 455 // |
| 456 // https://code.google.com/p/dart/issues/detail?id=18561 |
| 457 // |
| 458 // ...stays fixed. |
454 const char* kScriptChars = | 459 const char* kScriptChars = |
455 "int outer() {\n" | 460 "int outer() {\n" |
456 " for(int i = 0; i < 1; i++) {\n" | 461 " for(int i = 0; i < 1; i++) {\n" |
457 " var value = 11 + i;\n" | 462 " var value = 11 + i;\n" |
458 " int inner() {\n" | 463 " int inner() {\n" |
459 " return value;\n" // line 5 | 464 " return value;\n" // line 5 |
460 " }\n" | 465 " }\n" |
461 " return inner();\n" | 466 " return inner();\n" |
462 " }\n" | 467 " }\n" |
463 "}\n"; | 468 "}\n"; |
464 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 469 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
465 EXPECT_VALID(lib); | 470 EXPECT_VALID(lib); |
466 EXPECT_STREQ( | 471 EXPECT_STREQ( |
467 // inner function captures variable value. That's fine. | 472 // inner function captures variable value. That's fine. |
468 "::.outer_inner\n" | 473 "::.outer_inner\n" |
469 " 0 ContextVar level=0 begin=32 end=42 name=value\n" | 474 " 0 ContextVar level=0 begin=32 end=42 name=value\n" |
470 | 475 |
471 // Closure call saves current context. | 476 // Closure call saves current context. |
472 "(dynamic) => int.call\n" | 477 "(dynamic) => int.call\n" |
473 " 0 StackVar scope=1 begin=0 end=0 name=this\n" | 478 " 0 StackVar scope=1 begin=0 end=0 name=this\n" |
474 " 1 SavedCurrentCtx scope=0 begin=0 end=0" | 479 " 1 SavedCurrentCtx scope=0 begin=0 end=0" |
475 " name=:saved_current_context_var\n" | 480 " name=:saved_current_context_var\n" |
476 | 481 |
477 // Notice that the outer function neglects to save the entry | 482 // The outer function saves the entry context, even though the |
478 // context. This is a bug. | 483 // captured variable is in a loop. Good. |
479 // | |
480 // TODO(turnidge): Fix this very soon and update this test. | |
481 // | |
482 // https://code.google.com/p/dart/issues/detail?id=18561 | |
483 "::.outer\n" | 484 "::.outer\n" |
484 " 0 StackVar scope=3 begin=9 end=50 name=i\n" | 485 " 0 SavedEntryCtx scope=0 begin=0 end=0" |
485 " 1 ContextLevel level=1 scope=4 begin=20 end=50\n" | 486 " name=:saved_entry_context_var\n" |
486 " 2 ContextVar level=1 begin=23 end=50 name=value\n" | 487 " 1 StackVar scope=3 begin=9 end=50 name=i\n" |
487 " 3 StackVar scope=4 begin=30 end=50 name=inner\n", | 488 " 2 ContextLevel level=1 scope=4 begin=20 end=50\n" |
| 489 " 3 ContextVar level=1 begin=23 end=50 name=value\n" |
| 490 " 4 StackVar scope=4 begin=30 end=50 name=inner\n", |
488 CaptureVarsAtLine(lib, "outer", 5)); | 491 CaptureVarsAtLine(lib, "outer", 5)); |
489 } | 492 } |
490 | 493 |
| 494 TEST_CASE(Parser_AllocateVariables_MiddleChain) { |
| 495 const char* kScriptChars = |
| 496 "a() {\n" |
| 497 " int x = 11;\n" |
| 498 " b() {\n" |
| 499 " for (int i = 0; i < 1; i++) {\n" |
| 500 " int d() {\n" |
| 501 " return i;\n" |
| 502 " }\n" |
| 503 " }\n" |
| 504 " int c() {\n" |
| 505 " return x + 1;\n" // line 10 |
| 506 " }\n" |
| 507 " return c();\n" |
| 508 " }\n" |
| 509 " return b();\n" |
| 510 "}\n"; |
| 511 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 512 EXPECT_VALID(lib); |
| 513 EXPECT_STREQ( |
| 514 "::.a_b_c\n" |
| 515 " 0 ContextVar level=0 begin=48 end=60 name=x\n" |
| 516 "(dynamic) => int.call\n" |
| 517 " 0 StackVar scope=1 begin=0 end=0 name=this\n" |
| 518 " 1 SavedCurrentCtx scope=0 begin=0 end=0" |
| 519 " name=:saved_current_context_var\n" |
| 520 |
| 521 // Doesn't save the entry context. Chains to parent instead. |
| 522 "::.a_b\n" |
| 523 " 0 ContextVar level=0 begin=12 end=68 name=x\n" |
| 524 " 1 StackVar scope=2 begin=46 end=68 name=c\n" |
| 525 " 2 ContextLevel level=1 scope=3 begin=18 end=46\n" |
| 526 " 3 ContextVar level=1 begin=19 end=46 name=i\n" |
| 527 " 4 StackVar scope=4 begin=32 end=46 name=d\n" |
| 528 |
| 529 "(dynamic) => dynamic.call\n" |
| 530 " 0 StackVar scope=1 begin=0 end=0 name=this\n" |
| 531 " 1 SavedCurrentCtx scope=0 begin=0 end=0" |
| 532 " name=:saved_current_context_var\n" |
| 533 |
| 534 "::.a\n" |
| 535 " 0 ContextLevel level=1 scope=1 begin=1 end=76\n" |
| 536 " 1 SavedEntryCtx scope=0 begin=0 end=0" |
| 537 " name=:saved_entry_context_var\n" |
| 538 " 2 ContextVar level=1 begin=6 end=76 name=x\n" |
| 539 " 3 StackVar scope=2 begin=11 end=76 name=b\n", |
| 540 CaptureVarsAtLine(lib, "a", 10)); |
| 541 } |
| 542 |
491 } // namespace dart | 543 } // namespace dart |
OLD | NEW |