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 "vm/assembler.h" | 5 #include "vm/assembler.h" |
6 #include "vm/bigint_operations.h" | 6 #include "vm/bigint_operations.h" |
7 #include "vm/class_finalizer.h" | 7 #include "vm/class_finalizer.h" |
8 #include "vm/dart_api_impl.h" | 8 #include "vm/dart_api_impl.h" |
9 #include "vm/dart_entry.h" | 9 #include "vm/dart_entry.h" |
10 #include "vm/debugger.h" | 10 #include "vm/debugger.h" |
(...skipping 3450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3461 EXPECT_EQ(field_a.raw(), field_a_from_index.raw()); | 3461 EXPECT_EQ(field_a.raw(), field_a_from_index.raw()); |
3462 Field& field_b_from_index = Field::Handle(); | 3462 Field& field_b_from_index = Field::Handle(); |
3463 field_b_from_index ^= class_a_fields.At(field_b_index); | 3463 field_b_from_index ^= class_a_fields.At(field_b_index); |
3464 ASSERT(!field_b_from_index.IsNull()); | 3464 ASSERT(!field_b_from_index.IsNull()); |
3465 // Same field. | 3465 // Same field. |
3466 EXPECT_EQ(field_b.raw(), field_b_from_index.raw()); | 3466 EXPECT_EQ(field_b.raw(), field_b_from_index.raw()); |
3467 } | 3467 } |
3468 | 3468 |
3469 | 3469 |
3470 TEST_CASE(FindFunctionIndex) { | 3470 TEST_CASE(FindFunctionIndex) { |
| 3471 // Tests both FindFunctionIndex and FindImplicitClosureFunctionIndex. |
3471 const char* kScriptChars = | 3472 const char* kScriptChars = |
3472 "class A {\n" | 3473 "class A {\n" |
3473 " void a() {}\n" | 3474 " void a() {}\n" |
3474 " void b() {}\n" | 3475 " void b() { return a; }\n" |
3475 "}\n" | 3476 "}\n" |
3476 "class B {\n" | 3477 "class B {\n" |
3477 " dynamic d() {}\n" | 3478 " dynamic d() {}\n" |
3478 "}\n" | 3479 "}\n" |
| 3480 "var x;\n" |
3479 "test() {\n" | 3481 "test() {\n" |
3480 " new A();\n" | 3482 " x = new A().b();\n" |
| 3483 " x();\n" |
3481 " new B();\n" | 3484 " new B();\n" |
| 3485 " return x;\n" |
3482 "}"; | 3486 "}"; |
3483 Dart_Handle h_lib = TestCase::LoadTestScript(kScriptChars, NULL); | 3487 Dart_Handle h_lib = TestCase::LoadTestScript(kScriptChars, NULL); |
3484 EXPECT_VALID(h_lib); | 3488 EXPECT_VALID(h_lib); |
3485 Dart_Handle result = Dart_Invoke(h_lib, NewString("test"), 0, NULL); | 3489 Dart_Handle result = Dart_Invoke(h_lib, NewString("test"), 0, NULL); |
3486 EXPECT_VALID(result); | 3490 EXPECT_VALID(result); |
3487 Library& lib = Library::Handle(); | 3491 Library& lib = Library::Handle(); |
3488 lib ^= Api::UnwrapHandle(h_lib); | 3492 lib ^= Api::UnwrapHandle(h_lib); |
3489 EXPECT(!lib.IsNull()); | 3493 EXPECT(!lib.IsNull()); |
3490 const Class& class_a = Class::Handle(GetClass(lib, "A")); | 3494 const Class& class_a = Class::Handle(GetClass(lib, "A")); |
3491 const Array& class_a_funcs = Array::Handle(class_a.functions()); | |
3492 const Class& class_b = Class::Handle(GetClass(lib, "B")); | 3495 const Class& class_b = Class::Handle(GetClass(lib, "B")); |
3493 const Function& func_a = Function::Handle(GetFunction(class_a, "a")); | 3496 const Function& func_a = Function::Handle(GetFunction(class_a, "a")); |
3494 const Function& func_b = Function::Handle(GetFunction(class_a, "b")); | 3497 const Function& func_b = Function::Handle(GetFunction(class_a, "b")); |
3495 const Function& func_d = Function::Handle(GetFunction(class_b, "d")); | 3498 const Function& func_d = Function::Handle(GetFunction(class_b, "d")); |
| 3499 EXPECT(func_a.HasImplicitClosureFunction()); |
| 3500 const Function& func_x = Function::Handle(func_a.ImplicitClosureFunction()); |
3496 intptr_t func_a_index = class_a.FindFunctionIndex(func_a); | 3501 intptr_t func_a_index = class_a.FindFunctionIndex(func_a); |
3497 intptr_t func_b_index = class_a.FindFunctionIndex(func_b); | 3502 intptr_t func_b_index = class_a.FindFunctionIndex(func_b); |
3498 intptr_t func_d_index = class_a.FindFunctionIndex(func_d); | 3503 intptr_t func_d_index = class_a.FindFunctionIndex(func_d); |
| 3504 intptr_t func_x_index = class_a.FindImplicitClosureFunctionIndex(func_x); |
3499 // Valid index. | 3505 // Valid index. |
3500 EXPECT_GE(func_a_index, 0); | 3506 EXPECT_GE(func_a_index, 0); |
3501 // Valid index. | 3507 // Valid index. |
3502 EXPECT_GE(func_b_index, 0); | 3508 EXPECT_GE(func_b_index, 0); |
3503 // Invalid index. | 3509 // Invalid index. |
3504 EXPECT_EQ(func_d_index, -1); | 3510 EXPECT_EQ(func_d_index, -1); |
| 3511 // Valid index. |
| 3512 EXPECT_GE(func_x_index, 0); |
3505 Function& func_a_from_index = Function::Handle(); | 3513 Function& func_a_from_index = Function::Handle(); |
3506 func_a_from_index ^= class_a_funcs.At(func_a_index); | 3514 func_a_from_index ^= class_a.FunctionFromIndex(func_a_index); |
3507 ASSERT(!func_a_from_index.IsNull()); | 3515 EXPECT(!func_a_from_index.IsNull()); |
3508 // Same function. | 3516 // Same function. |
3509 EXPECT_EQ(func_a.raw(), func_a_from_index.raw()); | 3517 EXPECT_EQ(func_a.raw(), func_a_from_index.raw()); |
3510 Function& func_b_from_index = Function::Handle(); | 3518 Function& func_b_from_index = Function::Handle(); |
3511 func_b_from_index ^= class_a_funcs.At(func_b_index); | 3519 func_b_from_index ^= class_a.FunctionFromIndex(func_b_index); |
3512 ASSERT(!func_b_from_index.IsNull()); | 3520 EXPECT(!func_b_from_index.IsNull()); |
3513 // Same function. | 3521 // Same function. |
3514 EXPECT_EQ(func_b.raw(), func_b_from_index.raw()); | 3522 EXPECT_EQ(func_b.raw(), func_b_from_index.raw()); |
| 3523 // Retrieve implicit closure function. |
| 3524 Function& func_x_from_index = Function::Handle(); |
| 3525 func_x_from_index ^= class_a.ImplicitClosureFunctionFromIndex(func_x_index); |
| 3526 EXPECT_EQ(func_x.raw(), func_x_from_index.raw()); |
3515 } | 3527 } |
3516 | 3528 |
3517 | 3529 |
3518 TEST_CASE(FindClosureIndex) { | 3530 TEST_CASE(FindClosureIndex) { |
3519 // Allocate the class first. | 3531 // Allocate the class first. |
3520 const String& class_name = String::Handle(Symbols::New("MyClass")); | 3532 const String& class_name = String::Handle(Symbols::New("MyClass")); |
3521 const Script& script = Script::Handle(); | 3533 const Script& script = Script::Handle(); |
3522 const Class& cls = Class::Handle(CreateDummyClass(class_name, script)); | 3534 const Class& cls = Class::Handle(CreateDummyClass(class_name, script)); |
3523 const Array& functions = Array::Handle(Array::New(1)); | 3535 const Array& functions = Array::Handle(Array::New(1)); |
3524 | 3536 |
3525 Function& parent = Function::Handle(); | 3537 Function& parent = Function::Handle(); |
3526 const String& parent_name = String::Handle(Symbols::New("foo_papa")); | 3538 const String& parent_name = String::Handle(Symbols::New("foo_papa")); |
3527 parent = Function::New(parent_name, RawFunction::kRegularFunction, | 3539 parent = Function::New(parent_name, RawFunction::kRegularFunction, |
3528 false, false, false, false, false, cls, 0); | 3540 false, false, false, false, false, cls, 0); |
3529 functions.SetAt(0, parent); | 3541 functions.SetAt(0, parent); |
3530 cls.SetFunctions(functions); | 3542 cls.SetFunctions(functions); |
3531 | 3543 |
3532 Function& function = Function::Handle(); | 3544 Function& function = Function::Handle(); |
3533 const String& function_name = String::Handle(Symbols::New("foo")); | 3545 const String& function_name = String::Handle(Symbols::New("foo")); |
3534 function = Function::NewClosureFunction(function_name, parent, 0); | 3546 function = Function::NewClosureFunction(function_name, parent, 0); |
3535 // Add closure function to class. | 3547 // Add closure function to class. |
3536 cls.AddClosureFunction(function); | 3548 cls.AddClosureFunction(function); |
3537 | 3549 |
3538 // Token position 0 should return a valid index. | 3550 // The closure should return a valid index. |
3539 intptr_t good_closure_index = cls.FindClosureIndex(0); | 3551 intptr_t good_closure_index = cls.FindClosureIndex(function); |
3540 EXPECT_GE(good_closure_index, 0); | 3552 EXPECT_GE(good_closure_index, 0); |
3541 // Token position 1 should return an invalid index. | 3553 // The parent function should return an invalid index. |
3542 intptr_t bad_closure_index = cls.FindClosureIndex(1); | 3554 intptr_t bad_closure_index = cls.FindClosureIndex(parent); |
3543 EXPECT_EQ(bad_closure_index, -1); | 3555 EXPECT_EQ(bad_closure_index, -1); |
3544 | 3556 |
3545 // Retrieve closure function via index. | 3557 // Retrieve closure function via index. |
3546 const GrowableObjectArray& closures = GrowableObjectArray::Handle( | |
3547 cls.closures()); | |
3548 Function& func_from_index = Function::Handle(); | 3558 Function& func_from_index = Function::Handle(); |
3549 func_from_index ^= closures.At(good_closure_index); | 3559 func_from_index ^= cls.ClosureFunctionFromIndex(good_closure_index); |
3550 | |
3551 // Same closure function. | 3560 // Same closure function. |
3552 EXPECT_EQ(func_from_index.raw(), function.raw()); | 3561 EXPECT_EQ(func_from_index.raw(), function.raw()); |
3553 } | 3562 } |
3554 | 3563 |
3555 | 3564 |
| 3565 TEST_CASE(FindInvocationDispatcherFunctionIndex) { |
| 3566 const String& class_name = String::Handle(Symbols::New("MyClass")); |
| 3567 const Script& script = Script::Handle(); |
| 3568 const Class& cls = Class::Handle(CreateDummyClass(class_name, script)); |
| 3569 ClassFinalizer::FinalizeTypesInClass(cls); |
| 3570 |
| 3571 const Array& functions = Array::Handle(Array::New(1)); |
| 3572 Function& parent = Function::Handle(); |
| 3573 const String& parent_name = String::Handle(Symbols::New("foo_papa")); |
| 3574 parent = Function::New(parent_name, RawFunction::kRegularFunction, |
| 3575 false, false, false, false, false, cls, 0); |
| 3576 functions.SetAt(0, parent); |
| 3577 cls.SetFunctions(functions); |
| 3578 cls.Finalize(); |
| 3579 |
| 3580 // Add invocation dispatcher. |
| 3581 const String& invocation_dispatcher_name = |
| 3582 String::Handle(Symbols::New("myMethod")); |
| 3583 const Array& args_desc = Array::Handle(ArgumentsDescriptor::New(1)); |
| 3584 Function& invocation_dispatcher = Function::Handle(); |
| 3585 invocation_dispatcher ^= |
| 3586 cls.GetInvocationDispatcher(invocation_dispatcher_name, args_desc, |
| 3587 RawFunction::kNoSuchMethodDispatcher); |
| 3588 EXPECT(!invocation_dispatcher.IsNull()); |
| 3589 // Get index to function. |
| 3590 intptr_t invocation_dispatcher_index = |
| 3591 cls.FindInvocationDispatcherFunctionIndex(invocation_dispatcher); |
| 3592 // Expect a valid index. |
| 3593 EXPECT_GE(invocation_dispatcher_index, 0); |
| 3594 // Retrieve function through index. |
| 3595 Function& invocation_dispatcher_from_index = Function::Handle(); |
| 3596 invocation_dispatcher_from_index ^= |
| 3597 cls.InvocationDispatcherFunctionFromIndex(invocation_dispatcher_index); |
| 3598 // Same function. |
| 3599 EXPECT_EQ(invocation_dispatcher.raw(), |
| 3600 invocation_dispatcher_from_index.raw()); |
| 3601 // Test function not found case. |
| 3602 const Function& bad_function = Function::Handle(Function::null()); |
| 3603 intptr_t bad_invocation_dispatcher_index = |
| 3604 cls.FindInvocationDispatcherFunctionIndex(bad_function); |
| 3605 EXPECT_EQ(bad_invocation_dispatcher_index, -1); |
| 3606 } |
| 3607 |
| 3608 |
3556 static void PrintMetadata(const char* name, const Object& data) { | 3609 static void PrintMetadata(const char* name, const Object& data) { |
3557 if (data.IsError()) { | 3610 if (data.IsError()) { |
3558 OS::Print("Error in metadata evaluation for %s: '%s'\n", | 3611 OS::Print("Error in metadata evaluation for %s: '%s'\n", |
3559 name, | 3612 name, |
3560 Error::Cast(data).ToErrorCString()); | 3613 Error::Cast(data).ToErrorCString()); |
3561 } | 3614 } |
3562 EXPECT(data.IsArray()); | 3615 EXPECT(data.IsArray()); |
3563 const Array& metadata = Array::Cast(data); | 3616 const Array& metadata = Array::Cast(data); |
3564 OS::Print("Metadata for %s has %" Pd " values:\n", name, metadata.Length()); | 3617 OS::Print("Metadata for %s has %" Pd " values:\n", name, metadata.Length()); |
3565 Object& elem = Object::Handle(); | 3618 Object& elem = Object::Handle(); |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3929 // Simple map. | 3982 // Simple map. |
3930 // | 3983 // |
3931 // TODO(turnidge): Consider showing something like: {1: 2, 2: 'otter'} | 3984 // TODO(turnidge): Consider showing something like: {1: 2, 2: 'otter'} |
3932 result = Dart_GetField(lib, NewString("simple_map")); | 3985 result = Dart_GetField(lib, NewString("simple_map")); |
3933 EXPECT_VALID(result); | 3986 EXPECT_VALID(result); |
3934 obj ^= Api::UnwrapHandle(result); | 3987 obj ^= Api::UnwrapHandle(result); |
3935 EXPECT_STREQ("Instance of '_LinkedHashMap'", obj.ToUserCString()); | 3988 EXPECT_STREQ("Instance of '_LinkedHashMap'", obj.ToUserCString()); |
3936 } | 3989 } |
3937 | 3990 |
3938 } // namespace dart | 3991 } // namespace dart |
OLD | NEW |