OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 | 6 |
7 #include "vm/assembler.h" | 7 #include "vm/assembler.h" |
8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
9 #include "vm/dart_api_impl.h" | 9 #include "vm/dart_api_impl.h" |
10 #include "vm/dart_entry.h" | 10 #include "vm/dart_entry.h" |
(...skipping 2506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2517 ISOLATE_UNIT_TEST_CASE(ContextScope) { | 2517 ISOLATE_UNIT_TEST_CASE(ContextScope) { |
2518 const intptr_t parent_scope_function_level = 0; | 2518 const intptr_t parent_scope_function_level = 0; |
2519 LocalScope* parent_scope = | 2519 LocalScope* parent_scope = |
2520 new LocalScope(NULL, parent_scope_function_level, 0); | 2520 new LocalScope(NULL, parent_scope_function_level, 0); |
2521 | 2521 |
2522 const intptr_t local_scope_function_level = 1; | 2522 const intptr_t local_scope_function_level = 1; |
2523 LocalScope* local_scope = | 2523 LocalScope* local_scope = |
2524 new LocalScope(parent_scope, local_scope_function_level, 0); | 2524 new LocalScope(parent_scope, local_scope_function_level, 0); |
2525 | 2525 |
2526 const Type& dynamic_type = Type::ZoneHandle(Type::DynamicType()); | 2526 const Type& dynamic_type = Type::ZoneHandle(Type::DynamicType()); |
| 2527 const String& ta = Symbols::FunctionTypeArgumentsVar(); |
| 2528 LocalVariable* var_ta = new LocalVariable( |
| 2529 TokenPosition::kNoSource, TokenPosition::kNoSource, ta, dynamic_type); |
| 2530 parent_scope->AddVariable(var_ta); |
| 2531 |
2527 const String& a = String::ZoneHandle(Symbols::New(thread, "a")); | 2532 const String& a = String::ZoneHandle(Symbols::New(thread, "a")); |
2528 LocalVariable* var_a = new LocalVariable( | 2533 LocalVariable* var_a = new LocalVariable( |
2529 TokenPosition::kNoSource, TokenPosition::kNoSource, a, dynamic_type); | 2534 TokenPosition::kNoSource, TokenPosition::kNoSource, a, dynamic_type); |
2530 parent_scope->AddVariable(var_a); | 2535 parent_scope->AddVariable(var_a); |
2531 | 2536 |
2532 const String& b = String::ZoneHandle(Symbols::New(thread, "b")); | 2537 const String& b = String::ZoneHandle(Symbols::New(thread, "b")); |
2533 LocalVariable* var_b = new LocalVariable( | 2538 LocalVariable* var_b = new LocalVariable( |
2534 TokenPosition::kNoSource, TokenPosition::kNoSource, b, dynamic_type); | 2539 TokenPosition::kNoSource, TokenPosition::kNoSource, b, dynamic_type); |
2535 local_scope->AddVariable(var_b); | 2540 local_scope->AddVariable(var_b); |
2536 | 2541 |
2537 const String& c = String::ZoneHandle(Symbols::New(thread, "c")); | 2542 const String& c = String::ZoneHandle(Symbols::New(thread, "c")); |
2538 LocalVariable* var_c = new LocalVariable( | 2543 LocalVariable* var_c = new LocalVariable( |
2539 TokenPosition::kNoSource, TokenPosition::kNoSource, c, dynamic_type); | 2544 TokenPosition::kNoSource, TokenPosition::kNoSource, c, dynamic_type); |
2540 parent_scope->AddVariable(var_c); | 2545 parent_scope->AddVariable(var_c); |
2541 | 2546 |
2542 bool test_only = false; // Please, insert alias. | 2547 bool test_only = false; // Please, insert alias. |
| 2548 var_ta = local_scope->LookupVariable(ta, test_only); |
| 2549 EXPECT(var_ta->is_captured()); |
| 2550 EXPECT_EQ(parent_scope_function_level, var_ta->owner()->function_level()); |
| 2551 EXPECT(local_scope->LocalLookupVariable(ta) == var_ta); // Alias. |
| 2552 |
2543 var_a = local_scope->LookupVariable(a, test_only); | 2553 var_a = local_scope->LookupVariable(a, test_only); |
2544 EXPECT(var_a->is_captured()); | 2554 EXPECT(var_a->is_captured()); |
2545 EXPECT_EQ(parent_scope_function_level, var_a->owner()->function_level()); | 2555 EXPECT_EQ(parent_scope_function_level, var_a->owner()->function_level()); |
2546 EXPECT(local_scope->LocalLookupVariable(a) == var_a); // Alias. | 2556 EXPECT(local_scope->LocalLookupVariable(a) == var_a); // Alias. |
2547 | 2557 |
2548 var_b = local_scope->LookupVariable(b, test_only); | 2558 var_b = local_scope->LookupVariable(b, test_only); |
2549 EXPECT(!var_b->is_captured()); | 2559 EXPECT(!var_b->is_captured()); |
2550 EXPECT_EQ(local_scope_function_level, var_b->owner()->function_level()); | 2560 EXPECT_EQ(local_scope_function_level, var_b->owner()->function_level()); |
2551 EXPECT(local_scope->LocalLookupVariable(b) == var_b); | 2561 EXPECT(local_scope->LocalLookupVariable(b) == var_b); |
2552 | 2562 |
2553 test_only = true; // Please, do not insert alias. | 2563 test_only = true; // Please, do not insert alias. |
2554 var_c = local_scope->LookupVariable(c, test_only); | 2564 var_c = local_scope->LookupVariable(c, test_only); |
2555 EXPECT(!var_c->is_captured()); | 2565 EXPECT(!var_c->is_captured()); |
2556 EXPECT_EQ(parent_scope_function_level, var_c->owner()->function_level()); | 2566 EXPECT_EQ(parent_scope_function_level, var_c->owner()->function_level()); |
2557 // c is not in local_scope. | 2567 // c is not in local_scope. |
2558 EXPECT(local_scope->LocalLookupVariable(c) == NULL); | 2568 EXPECT(local_scope->LocalLookupVariable(c) == NULL); |
2559 | 2569 |
2560 test_only = false; // Please, insert alias. | 2570 test_only = false; // Please, insert alias. |
2561 var_c = local_scope->LookupVariable(c, test_only); | 2571 var_c = local_scope->LookupVariable(c, test_only); |
2562 EXPECT(var_c->is_captured()); | 2572 EXPECT(var_c->is_captured()); |
2563 | 2573 |
2564 EXPECT_EQ(3, local_scope->num_variables()); // a, b, and c alias. | 2574 EXPECT_EQ(4, local_scope->num_variables()); // ta, a, b, c. |
2565 EXPECT_EQ(2, local_scope->NumCapturedVariables()); // a, c alias. | 2575 EXPECT_EQ(3, local_scope->NumCapturedVariables()); // ta, a, c. |
2566 | 2576 |
2567 const int first_parameter_index = 0; | 2577 const int first_parameter_index = 0; |
2568 const int num_parameters = 0; | 2578 const int num_parameters = 0; |
2569 const int first_frame_index = -1; | 2579 const int first_frame_index = -1; |
2570 bool found_captured_vars = false; | 2580 bool found_captured_vars = false; |
2571 int next_frame_index = parent_scope->AllocateVariables( | 2581 int next_frame_index = parent_scope->AllocateVariables( |
2572 first_parameter_index, num_parameters, first_frame_index, NULL, | 2582 first_parameter_index, num_parameters, first_frame_index, NULL, |
2573 &found_captured_vars); | 2583 &found_captured_vars); |
2574 EXPECT_EQ(first_frame_index, next_frame_index); // a and c not in frame. | 2584 // Variables a and c are captured, therefore are not allocated in frame. |
| 2585 // Variable var_ta, although captured, still requires a slot in frame. |
| 2586 EXPECT_EQ(-1, next_frame_index - first_frame_index); // Indices in frame < 0. |
2575 const intptr_t parent_scope_context_level = 1; | 2587 const intptr_t parent_scope_context_level = 1; |
2576 EXPECT_EQ(parent_scope_context_level, parent_scope->context_level()); | 2588 EXPECT_EQ(parent_scope_context_level, parent_scope->context_level()); |
2577 EXPECT(found_captured_vars); | 2589 EXPECT(found_captured_vars); |
2578 | 2590 |
2579 const intptr_t local_scope_context_level = 5; | 2591 const intptr_t local_scope_context_level = 5; |
2580 const ContextScope& context_scope = ContextScope::Handle( | 2592 const ContextScope& context_scope = ContextScope::Handle( |
2581 local_scope->PreserveOuterScope(local_scope_context_level)); | 2593 local_scope->PreserveOuterScope(local_scope_context_level)); |
2582 LocalScope* outer_scope = LocalScope::RestoreOuterScope(context_scope); | 2594 LocalScope* outer_scope = LocalScope::RestoreOuterScope(context_scope); |
2583 EXPECT_EQ(2, outer_scope->num_variables()); | 2595 EXPECT_EQ(3, outer_scope->num_variables()); |
| 2596 |
| 2597 var_ta = outer_scope->LocalLookupVariable(ta); |
| 2598 EXPECT(var_ta->is_captured()); |
| 2599 EXPECT_EQ(0, var_ta->index()); // First index. |
| 2600 EXPECT_EQ(parent_scope_context_level - local_scope_context_level, |
| 2601 var_ta->owner()->context_level()); // Adjusted context level. |
2584 | 2602 |
2585 var_a = outer_scope->LocalLookupVariable(a); | 2603 var_a = outer_scope->LocalLookupVariable(a); |
2586 EXPECT(var_a->is_captured()); | 2604 EXPECT(var_a->is_captured()); |
2587 EXPECT_EQ(0, var_a->index()); // First index. | 2605 EXPECT_EQ(1, var_a->index()); // First index. |
2588 EXPECT_EQ(parent_scope_context_level - local_scope_context_level, | 2606 EXPECT_EQ(parent_scope_context_level - local_scope_context_level, |
2589 var_a->owner()->context_level()); // Adjusted context level. | 2607 var_a->owner()->context_level()); // Adjusted context level. |
2590 | 2608 |
2591 // var b was not captured. | 2609 // var b was not captured. |
2592 EXPECT(outer_scope->LocalLookupVariable(b) == NULL); | 2610 EXPECT(outer_scope->LocalLookupVariable(b) == NULL); |
2593 | 2611 |
2594 var_c = outer_scope->LocalLookupVariable(c); | 2612 var_c = outer_scope->LocalLookupVariable(c); |
2595 EXPECT(var_c->is_captured()); | 2613 EXPECT(var_c->is_captured()); |
2596 EXPECT_EQ(1, var_c->index()); | 2614 EXPECT_EQ(2, var_c->index()); |
2597 EXPECT_EQ(parent_scope_context_level - local_scope_context_level, | 2615 EXPECT_EQ(parent_scope_context_level - local_scope_context_level, |
2598 var_c->owner()->context_level()); // Adjusted context level. | 2616 var_c->owner()->context_level()); // Adjusted context level. |
2599 } | 2617 } |
2600 | 2618 |
2601 | 2619 |
2602 ISOLATE_UNIT_TEST_CASE(Closure) { | 2620 ISOLATE_UNIT_TEST_CASE(Closure) { |
2603 // Allocate the class first. | 2621 // Allocate the class first. |
2604 const String& class_name = String::Handle(Symbols::New(thread, "MyClass")); | 2622 const String& class_name = String::Handle(Symbols::New(thread, "MyClass")); |
2605 const Script& script = Script::Handle(); | 2623 const Script& script = Script::Handle(); |
2606 const Class& cls = Class::Handle(CreateDummyClass(class_name, script)); | 2624 const Class& cls = Class::Handle(CreateDummyClass(class_name, script)); |
(...skipping 2115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4722 // utf32->utf16 conversion. | 4740 // utf32->utf16 conversion. |
4723 int32_t char_codes[] = {0, 0x0a, 0x0d, 0x7f, 0xff, | 4741 int32_t char_codes[] = {0, 0x0a, 0x0d, 0x7f, 0xff, |
4724 0xffff, 0xd800, 0xdc00, 0xdbff, 0xdfff}; | 4742 0xffff, 0xd800, 0xdc00, 0xdbff, 0xdfff}; |
4725 | 4743 |
4726 const String& str = | 4744 const String& str = |
4727 String::Handle(String::FromUTF32(char_codes, ARRAY_SIZE(char_codes))); | 4745 String::Handle(String::FromUTF32(char_codes, ARRAY_SIZE(char_codes))); |
4728 EXPECT(str.Equals(char_codes, ARRAY_SIZE(char_codes))); | 4746 EXPECT(str.Equals(char_codes, ARRAY_SIZE(char_codes))); |
4729 } | 4747 } |
4730 | 4748 |
4731 } // namespace dart | 4749 } // namespace dart |
OLD | NEW |