| 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 "bin/builtin.h" | 5 #include "bin/builtin.h" |
| 6 #include "include/dart_api.h" | 6 #include "include/dart_api.h" |
| 7 #include "include/dart_mirrors_api.h" | 7 #include "include/dart_mirrors_api.h" |
| 8 #include "include/dart_native_api.h" | 8 #include "include/dart_native_api.h" |
| 9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
| 10 #include "platform/json.h" | 10 #include "platform/json.h" |
| (...skipping 2676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2687 | 2687 |
| 2688 UNIT_TEST_CASE(SetMessageCallbacks) { | 2688 UNIT_TEST_CASE(SetMessageCallbacks) { |
| 2689 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate(); | 2689 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate(); |
| 2690 Dart_SetMessageNotifyCallback(&MyMessageNotifyCallback); | 2690 Dart_SetMessageNotifyCallback(&MyMessageNotifyCallback); |
| 2691 Isolate* isolate = reinterpret_cast<Isolate*>(dart_isolate); | 2691 Isolate* isolate = reinterpret_cast<Isolate*>(dart_isolate); |
| 2692 EXPECT_EQ(&MyMessageNotifyCallback, isolate->message_notify_callback()); | 2692 EXPECT_EQ(&MyMessageNotifyCallback, isolate->message_notify_callback()); |
| 2693 Dart_ShutdownIsolate(); | 2693 Dart_ShutdownIsolate(); |
| 2694 } | 2694 } |
| 2695 | 2695 |
| 2696 | 2696 |
| 2697 TEST_CASE(ClassTypedefsEtc) { | |
| 2698 const char* kScriptChars = | |
| 2699 "class SomeClass {\n" | |
| 2700 "}\n" | |
| 2701 "typedef void SomeHandler(String a);\n"; | |
| 2702 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 2703 EXPECT_VALID(lib); | |
| 2704 Dart_Handle normal_cls = Dart_GetClass(lib, NewString("SomeClass")); | |
| 2705 EXPECT_VALID(normal_cls); | |
| 2706 Dart_Handle typedef_cls = Dart_GetClass(lib, NewString("SomeHandler")); | |
| 2707 EXPECT_VALID(typedef_cls); | |
| 2708 | |
| 2709 EXPECT(Dart_IsClass(normal_cls)); | |
| 2710 EXPECT(!Dart_ClassIsTypedef(normal_cls)); | |
| 2711 EXPECT(!Dart_ClassIsFunctionType(normal_cls)); | |
| 2712 | |
| 2713 EXPECT(Dart_IsClass(typedef_cls)); | |
| 2714 EXPECT(Dart_ClassIsTypedef(typedef_cls)); | |
| 2715 EXPECT(!Dart_ClassIsFunctionType(typedef_cls)); | |
| 2716 | |
| 2717 // Exercise the typedef class api. | |
| 2718 Dart_Handle functype_cls = Dart_ClassGetTypedefReferent(typedef_cls); | |
| 2719 EXPECT_VALID(functype_cls); | |
| 2720 | |
| 2721 // Pass the wrong class kind to Dart_ClassGetTypedefReferent | |
| 2722 EXPECT_ERROR(Dart_ClassGetTypedefReferent(normal_cls), | |
| 2723 "class 'SomeClass' is not a typedef class."); | |
| 2724 | |
| 2725 EXPECT(Dart_IsClass(functype_cls)); | |
| 2726 EXPECT(!Dart_ClassIsTypedef(functype_cls)); | |
| 2727 EXPECT(Dart_ClassIsFunctionType(functype_cls)); | |
| 2728 | |
| 2729 // Exercise the function type class. | |
| 2730 Dart_Handle sig_func = Dart_ClassGetFunctionTypeSignature(functype_cls); | |
| 2731 EXPECT_VALID(sig_func); | |
| 2732 EXPECT(Dart_IsFunction(sig_func)); | |
| 2733 int64_t fixed_params = -1; | |
| 2734 int64_t opt_params = -1; | |
| 2735 EXPECT_VALID( | |
| 2736 Dart_FunctionParameterCounts(sig_func, &fixed_params, &opt_params)); | |
| 2737 EXPECT_EQ(1, fixed_params); | |
| 2738 EXPECT_EQ(0, opt_params); | |
| 2739 | |
| 2740 // Pass the wrong class kind to Dart_ClassGetFunctionTypeSignature | |
| 2741 EXPECT_ERROR(Dart_ClassGetFunctionTypeSignature(normal_cls), | |
| 2742 "class 'SomeClass' is not a function-type class."); | |
| 2743 } | |
| 2744 | |
| 2745 #define CHECK_CLASS(handle, name) \ | 2697 #define CHECK_CLASS(handle, name) \ |
| 2746 { \ | 2698 { \ |
| 2747 Dart_Handle tmp = (handle); \ | 2699 Dart_Handle tmp = (handle); \ |
| 2748 EXPECT_VALID(tmp); \ | 2700 EXPECT_VALID(tmp); \ |
| 2749 EXPECT(Dart_IsClass(tmp)); \ | 2701 EXPECT(Dart_IsClass(tmp)); \ |
| 2750 Dart_Handle intf_name = Dart_ClassName(tmp); \ | 2702 Dart_Handle intf_name = Dart_ClassName(tmp); \ |
| 2751 EXPECT_VALID(intf_name); \ | 2703 EXPECT_VALID(intf_name); \ |
| 2752 const char* intf_name_cstr = ""; \ | 2704 const char* intf_name_cstr = ""; \ |
| 2753 EXPECT_VALID(Dart_StringToCString(intf_name, &intf_name_cstr)); \ | 2705 EXPECT_VALID(Dart_StringToCString(intf_name, &intf_name_cstr)); \ |
| 2754 EXPECT_STREQ((name), intf_name_cstr); \ | 2706 EXPECT_STREQ((name), intf_name_cstr); \ |
| 2755 } | 2707 } |
| 2756 | 2708 |
| 2757 | 2709 |
| 2758 TEST_CASE(ClassGetInterfaces) { | |
| 2759 const char* kScriptChars = | |
| 2760 "class MyClass0 {\n" | |
| 2761 "}\n" | |
| 2762 "\n" | |
| 2763 "class MyClass1 implements MyInterface1 {\n" | |
| 2764 "}\n" | |
| 2765 "\n" | |
| 2766 "class MyClass2 implements MyInterface0, MyInterface1 {\n" | |
| 2767 "}\n" | |
| 2768 "\n" | |
| 2769 "abstract class MyInterface0 {\n" | |
| 2770 "}\n" | |
| 2771 "\n" | |
| 2772 "abstract class MyInterface1 implements MyInterface0 {\n" | |
| 2773 "}\n"; | |
| 2774 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 2775 | |
| 2776 Dart_Handle cls0 = Dart_GetClass(lib, NewString("MyClass0")); | |
| 2777 Dart_Handle cls1 = Dart_GetClass(lib, NewString("MyClass1")); | |
| 2778 Dart_Handle cls2 = Dart_GetClass(lib, NewString("MyClass2")); | |
| 2779 Dart_Handle intf0 = Dart_GetClass(lib, NewString("MyInterface0")); | |
| 2780 Dart_Handle intf1 = Dart_GetClass(lib, NewString("MyInterface1")); | |
| 2781 | |
| 2782 intptr_t len = -1; | |
| 2783 EXPECT_VALID(Dart_ClassGetInterfaceCount(cls0, &len)); | |
| 2784 EXPECT_EQ(0, len); | |
| 2785 | |
| 2786 EXPECT_ERROR(Dart_ClassGetInterfaceAt(cls0, 0), | |
| 2787 "Dart_ClassGetInterfaceAt: argument 'index' out of bounds"); | |
| 2788 | |
| 2789 len = -1; | |
| 2790 EXPECT_VALID(Dart_ClassGetInterfaceCount(cls1, &len)); | |
| 2791 EXPECT_EQ(1, len); | |
| 2792 CHECK_CLASS(Dart_ClassGetInterfaceAt(cls1, 0), "MyInterface1"); | |
| 2793 | |
| 2794 EXPECT_ERROR(Dart_ClassGetInterfaceAt(cls1, -1), | |
| 2795 "Dart_ClassGetInterfaceAt: argument 'index' out of bounds"); | |
| 2796 EXPECT_ERROR(Dart_ClassGetInterfaceAt(cls1, 1), | |
| 2797 "Dart_ClassGetInterfaceAt: argument 'index' out of bounds"); | |
| 2798 | |
| 2799 len = -1; | |
| 2800 EXPECT_VALID(Dart_ClassGetInterfaceCount(cls2, &len)); | |
| 2801 EXPECT_EQ(2, len); | |
| 2802 | |
| 2803 // TODO(turnidge): The test relies on the ordering here. Sort this. | |
| 2804 CHECK_CLASS(Dart_ClassGetInterfaceAt(cls2, 0), "MyInterface0"); | |
| 2805 CHECK_CLASS(Dart_ClassGetInterfaceAt(cls2, 1), "MyInterface1"); | |
| 2806 | |
| 2807 len = -1; | |
| 2808 EXPECT_VALID(Dart_ClassGetInterfaceCount(intf0, &len)); | |
| 2809 EXPECT_EQ(0, len); | |
| 2810 | |
| 2811 len = -1; | |
| 2812 EXPECT_VALID(Dart_ClassGetInterfaceCount(intf1, &len)); | |
| 2813 EXPECT_EQ(1, len); | |
| 2814 CHECK_CLASS(Dart_ClassGetInterfaceAt(intf1, 0), "MyInterface0"); | |
| 2815 | |
| 2816 // Error cases. | |
| 2817 EXPECT_ERROR(Dart_ClassGetInterfaceCount(Dart_True(), &len), | |
| 2818 "Dart_ClassGetInterfaceCount expects argument 'object' to be of " | |
| 2819 "type Class/Type."); | |
| 2820 EXPECT_ERROR(Dart_ClassGetInterfaceCount(Dart_NewApiError("MyError"), &len), | |
| 2821 "MyError"); | |
| 2822 } | |
| 2823 | |
| 2824 | |
| 2825 TEST_CASE(TypeGetNonParamtericTypes) { | 2710 TEST_CASE(TypeGetNonParamtericTypes) { |
| 2826 const char* kScriptChars = | 2711 const char* kScriptChars = |
| 2827 "class MyClass0 {\n" | 2712 "class MyClass0 {\n" |
| 2828 "}\n" | 2713 "}\n" |
| 2829 "\n" | 2714 "\n" |
| 2830 "class MyClass1 implements MyInterface1 {\n" | 2715 "class MyClass1 implements MyInterface1 {\n" |
| 2831 "}\n" | 2716 "}\n" |
| 2832 "\n" | 2717 "\n" |
| 2833 "class MyClass2 implements MyInterface0, MyInterface1 {\n" | 2718 "class MyClass2 implements MyInterface0, MyInterface1 {\n" |
| 2834 "}\n" | 2719 "}\n" |
| (...skipping 1554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4389 // We can invoke both private and non-private local functions. | 4274 // We can invoke both private and non-private local functions. |
| 4390 EXPECT_VALID(Dart_Invoke(lib1, NewString("local"), 0, NULL)); | 4275 EXPECT_VALID(Dart_Invoke(lib1, NewString("local"), 0, NULL)); |
| 4391 EXPECT_VALID(Dart_Invoke(lib1, NewString("_local"), 0, NULL)); | 4276 EXPECT_VALID(Dart_Invoke(lib1, NewString("_local"), 0, NULL)); |
| 4392 | 4277 |
| 4393 // We can only invoke non-private imported functions. | 4278 // We can only invoke non-private imported functions. |
| 4394 EXPECT_VALID(Dart_Invoke(lib1, NewString("imported"), 0, NULL)); | 4279 EXPECT_VALID(Dart_Invoke(lib1, NewString("imported"), 0, NULL)); |
| 4395 EXPECT_ERROR(Dart_Invoke(lib1, NewString("_imported"), 0, NULL), | 4280 EXPECT_ERROR(Dart_Invoke(lib1, NewString("_imported"), 0, NULL), |
| 4396 "did not find top-level function '_imported'"); | 4281 "did not find top-level function '_imported'"); |
| 4397 } | 4282 } |
| 4398 | 4283 |
| 4399 TEST_CASE(ClosureFunction) { | |
| 4400 const char* kScriptChars = | |
| 4401 "Function getClosure() {\n" | |
| 4402 " return (x, y, [z]) => x + y + z;\n" | |
| 4403 "}\n" | |
| 4404 "class Foo {\n" | |
| 4405 " getInstanceClosure() {\n" | |
| 4406 " return () { return this; };\n" | |
| 4407 " }\n" | |
| 4408 " getInstanceClosureWithArgs() {\n" | |
| 4409 " return (x, y, [z]) { return this; };\n" | |
| 4410 " }\n" | |
| 4411 " static getStaticClosure() {\n" | |
| 4412 " return () { return 42; };\n" | |
| 4413 " }\n" | |
| 4414 " static getStaticClosureWithArgs() {\n" | |
| 4415 " return (x, y, [z]) { return 42; };\n" | |
| 4416 " }\n" | |
| 4417 "}\n" | |
| 4418 "Function getInstanceClosure() {\n" | |
| 4419 " return new Foo().getInstanceClosure();\n" | |
| 4420 "}\n" | |
| 4421 "Function getInstanceClosureWithArgs() {\n" | |
| 4422 " return new Foo().getInstanceClosureWithArgs();\n" | |
| 4423 "}\n" | |
| 4424 "Function getStaticClosure() {\n" | |
| 4425 " return Foo.getStaticClosure();\n" | |
| 4426 "}\n" | |
| 4427 "Function getStaticClosureWithArgs() {\n" | |
| 4428 " return Foo.getStaticClosureWithArgs();\n" | |
| 4429 "}\n"; | |
| 4430 Dart_Handle result; | |
| 4431 Dart_Handle owner; | |
| 4432 Dart_Handle defining_function; | |
| 4433 DARTSCOPE(Isolate::Current()); | |
| 4434 | |
| 4435 // Create a test library and Load up a test script in it. | |
| 4436 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 4437 EXPECT_VALID(lib); | |
| 4438 Dart_Handle type = Dart_GetType(lib, NewString("Foo"), 0, NULL); | |
| 4439 EXPECT_VALID(type); | |
| 4440 | |
| 4441 // Invoke a function which returns a closure. | |
| 4442 Dart_Handle retobj = Dart_Invoke(lib, NewString("getClosure"), 0, NULL); | |
| 4443 EXPECT_VALID(retobj); | |
| 4444 | |
| 4445 EXPECT(Dart_IsClosure(retobj)); | |
| 4446 EXPECT(!Dart_IsClosure(Dart_NewInteger(101))); | |
| 4447 | |
| 4448 // Retrieve the closure's function | |
| 4449 result = Dart_ClosureFunction(retobj); | |
| 4450 EXPECT_VALID(result); | |
| 4451 EXPECT(Dart_IsFunction(result)); | |
| 4452 owner = Dart_FunctionOwner(result); | |
| 4453 EXPECT_VALID(owner); | |
| 4454 defining_function = Dart_LookupFunction(lib, NewString("getClosure")); | |
| 4455 EXPECT(Dart_IdentityEquals(owner, defining_function)); | |
| 4456 int64_t fixed_param_count = -999; | |
| 4457 int64_t opt_param_count = -999; | |
| 4458 result = Dart_FunctionParameterCounts(result, | |
| 4459 &fixed_param_count, | |
| 4460 &opt_param_count); | |
| 4461 EXPECT_VALID(result); | |
| 4462 EXPECT_EQ(2, fixed_param_count); | |
| 4463 EXPECT_EQ(1, opt_param_count); | |
| 4464 | |
| 4465 // Try to retrieve function from a non-closure object | |
| 4466 result = Dart_ClosureFunction(Dart_NewInteger(1)); | |
| 4467 EXPECT(Dart_IsError(result)); | |
| 4468 | |
| 4469 // Invoke a function which returns an "instance" closure. | |
| 4470 retobj = Dart_Invoke(lib, NewString("getInstanceClosure"), 0, NULL); | |
| 4471 EXPECT_VALID(retobj); | |
| 4472 EXPECT(Dart_IsClosure(retobj)); | |
| 4473 | |
| 4474 // Retrieve the closure's function | |
| 4475 result = Dart_ClosureFunction(retobj); | |
| 4476 EXPECT_VALID(result); | |
| 4477 EXPECT(Dart_IsFunction(result)); | |
| 4478 owner = Dart_FunctionOwner(result); | |
| 4479 EXPECT_VALID(owner); | |
| 4480 Isolate* isolate = Isolate::Current(); | |
| 4481 Dart_Handle cls = Api::NewHandle( | |
| 4482 isolate, Api::UnwrapTypeHandle(isolate, type).type_class()); | |
| 4483 defining_function = Dart_LookupFunction(cls, | |
| 4484 NewString("getInstanceClosure")); | |
| 4485 EXPECT(Dart_IdentityEquals(owner, defining_function)); | |
| 4486 // -999: We want to distinguish between a non-answer and a wrong answer, and | |
| 4487 // -1 has been a previous wrong answer | |
| 4488 fixed_param_count = -999; | |
| 4489 opt_param_count = -999; | |
| 4490 result = Dart_FunctionParameterCounts(result, | |
| 4491 &fixed_param_count, | |
| 4492 &opt_param_count); | |
| 4493 EXPECT_VALID(result); | |
| 4494 EXPECT_EQ(0, fixed_param_count); | |
| 4495 EXPECT_EQ(0, opt_param_count); | |
| 4496 | |
| 4497 // Invoke a function which returns an "instance" closure with arguments. | |
| 4498 retobj = Dart_Invoke(lib, | |
| 4499 NewString("getInstanceClosureWithArgs"), | |
| 4500 0, | |
| 4501 NULL); | |
| 4502 EXPECT_VALID(retobj); | |
| 4503 EXPECT(Dart_IsClosure(retobj)); | |
| 4504 | |
| 4505 // Retrieve the closure's function | |
| 4506 result = Dart_ClosureFunction(retobj); | |
| 4507 EXPECT_VALID(result); | |
| 4508 EXPECT(Dart_IsFunction(result)); | |
| 4509 owner = Dart_FunctionOwner(result); | |
| 4510 EXPECT_VALID(owner); | |
| 4511 defining_function = | |
| 4512 Dart_LookupFunction(cls, NewString("getInstanceClosureWithArgs")); | |
| 4513 EXPECT(Dart_IdentityEquals(owner, defining_function)); | |
| 4514 // -999: We want to distinguish between a non-answer and a wrong answer, and | |
| 4515 // -1 has been a previous wrong answer | |
| 4516 fixed_param_count = -999; | |
| 4517 opt_param_count = -999; | |
| 4518 result = Dart_FunctionParameterCounts(result, | |
| 4519 &fixed_param_count, | |
| 4520 &opt_param_count); | |
| 4521 EXPECT_VALID(result); | |
| 4522 EXPECT_EQ(2, fixed_param_count); | |
| 4523 EXPECT_EQ(1, opt_param_count); | |
| 4524 | |
| 4525 // Invoke a function which returns a "static" closure. | |
| 4526 retobj = Dart_Invoke(lib, NewString("getStaticClosure"), 0, NULL); | |
| 4527 EXPECT_VALID(retobj); | |
| 4528 EXPECT(Dart_IsClosure(retobj)); | |
| 4529 | |
| 4530 // Retrieve the closure's function | |
| 4531 result = Dart_ClosureFunction(retobj); | |
| 4532 EXPECT_VALID(result); | |
| 4533 EXPECT(Dart_IsFunction(result)); | |
| 4534 owner = Dart_FunctionOwner(result); | |
| 4535 EXPECT_VALID(owner); | |
| 4536 defining_function = Dart_LookupFunction(cls, | |
| 4537 NewString("getStaticClosure")); | |
| 4538 EXPECT(Dart_IdentityEquals(owner, defining_function)); | |
| 4539 // -999: We want to distinguish between a non-answer and a wrong answer, and | |
| 4540 // -1 has been a previous wrong answer | |
| 4541 fixed_param_count = -999; | |
| 4542 opt_param_count = -999; | |
| 4543 result = Dart_FunctionParameterCounts(result, | |
| 4544 &fixed_param_count, | |
| 4545 &opt_param_count); | |
| 4546 EXPECT_VALID(result); | |
| 4547 EXPECT_EQ(0, fixed_param_count); | |
| 4548 EXPECT_EQ(0, opt_param_count); | |
| 4549 | |
| 4550 | |
| 4551 // Invoke a function which returns a "static" closure with arguments. | |
| 4552 retobj = Dart_Invoke(lib, | |
| 4553 NewString("getStaticClosureWithArgs"), | |
| 4554 0, | |
| 4555 NULL); | |
| 4556 EXPECT_VALID(retobj); | |
| 4557 EXPECT(Dart_IsClosure(retobj)); | |
| 4558 | |
| 4559 // Retrieve the closure's function | |
| 4560 result = Dart_ClosureFunction(retobj); | |
| 4561 EXPECT_VALID(result); | |
| 4562 EXPECT(Dart_IsFunction(result)); | |
| 4563 owner = Dart_FunctionOwner(result); | |
| 4564 EXPECT_VALID(owner); | |
| 4565 defining_function = | |
| 4566 Dart_LookupFunction(cls, NewString("getStaticClosureWithArgs")); | |
| 4567 EXPECT(Dart_IdentityEquals(owner, defining_function)); | |
| 4568 // -999: We want to distinguish between a non-answer and a wrong answer, and | |
| 4569 // -1 has been a previous wrong answer | |
| 4570 fixed_param_count = -999; | |
| 4571 opt_param_count = -999; | |
| 4572 result = Dart_FunctionParameterCounts(result, | |
| 4573 &fixed_param_count, | |
| 4574 &opt_param_count); | |
| 4575 EXPECT_VALID(result); | |
| 4576 EXPECT_EQ(2, fixed_param_count); | |
| 4577 EXPECT_EQ(1, opt_param_count); | |
| 4578 } | |
| 4579 | 4284 |
| 4580 TEST_CASE(InvokeClosure) { | 4285 TEST_CASE(InvokeClosure) { |
| 4581 const char* kScriptChars = | 4286 const char* kScriptChars = |
| 4582 "class InvokeClosure {\n" | 4287 "class InvokeClosure {\n" |
| 4583 " InvokeClosure(int i, int j) : fld1 = i, fld2 = j {}\n" | 4288 " InvokeClosure(int i, int j) : fld1 = i, fld2 = j {}\n" |
| 4584 " Function method1(int i) {\n" | 4289 " Function method1(int i) {\n" |
| 4585 " f(int j) => j + i + fld1 + fld2 + fld4; \n" | 4290 " f(int j) => j + i + fld1 + fld2 + fld4; \n" |
| 4586 " return f;\n" | 4291 " return f;\n" |
| 4587 " }\n" | 4292 " }\n" |
| 4588 " static Function method2(int i) {\n" | 4293 " static Function method2(int i) {\n" |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4765 EXPECT(Dart_IsError(type)); | 4470 EXPECT(Dart_IsError(type)); |
| 4766 EXPECT_STREQ("myerror", Dart_GetError(type)); | 4471 EXPECT_STREQ("myerror", Dart_GetError(type)); |
| 4767 | 4472 |
| 4768 // Lookup a type using an error class name. The error propagates. | 4473 // Lookup a type using an error class name. The error propagates. |
| 4769 type = Dart_GetType(lib, Api::NewError("myerror"), 0, NULL); | 4474 type = Dart_GetType(lib, Api::NewError("myerror"), 0, NULL); |
| 4770 EXPECT(Dart_IsError(type)); | 4475 EXPECT(Dart_IsError(type)); |
| 4771 EXPECT_STREQ("myerror", Dart_GetError(type)); | 4476 EXPECT_STREQ("myerror", Dart_GetError(type)); |
| 4772 } | 4477 } |
| 4773 | 4478 |
| 4774 | 4479 |
| 4775 static void BuildFunctionDescription(TextBuffer* buffer, Dart_Handle func) { | |
| 4776 buffer->Clear(); | |
| 4777 if (Dart_IsNull(func)) { | |
| 4778 WARN("Function not found"); | |
| 4779 return; | |
| 4780 } | |
| 4781 Dart_Handle name = Dart_FunctionName(func); | |
| 4782 EXPECT_VALID(name); | |
| 4783 const char* name_cstr = ""; | |
| 4784 EXPECT_VALID(Dart_StringToCString(name, &name_cstr)); | |
| 4785 bool is_abstract = false; | |
| 4786 bool is_static = false; | |
| 4787 bool is_getter = false; | |
| 4788 bool is_setter = false; | |
| 4789 bool is_constructor = false; | |
| 4790 int64_t fixed_param_count = -1; | |
| 4791 int64_t opt_param_count = -1; | |
| 4792 EXPECT_VALID(Dart_FunctionIsAbstract(func, &is_abstract)); | |
| 4793 EXPECT_VALID(Dart_FunctionIsStatic(func, &is_static)); | |
| 4794 EXPECT_VALID(Dart_FunctionIsGetter(func, &is_getter)); | |
| 4795 EXPECT_VALID(Dart_FunctionIsSetter(func, &is_setter)); | |
| 4796 EXPECT_VALID(Dart_FunctionIsConstructor(func, &is_constructor)); | |
| 4797 EXPECT_VALID(Dart_FunctionParameterCounts(func, | |
| 4798 &fixed_param_count, &opt_param_count)); | |
| 4799 | |
| 4800 buffer->Printf("%s %"Pd64" %"Pd64"", | |
| 4801 name_cstr, | |
| 4802 fixed_param_count, | |
| 4803 opt_param_count); | |
| 4804 if (is_abstract) { | |
| 4805 buffer->Printf(" abstract"); | |
| 4806 } | |
| 4807 if (is_static) { | |
| 4808 buffer->Printf(" static"); | |
| 4809 } | |
| 4810 if (is_getter) { | |
| 4811 buffer->Printf(" getter"); | |
| 4812 } | |
| 4813 if (is_setter) { | |
| 4814 buffer->Printf(" setter"); | |
| 4815 } | |
| 4816 if (is_constructor) { | |
| 4817 buffer->Printf(" constructor"); | |
| 4818 } | |
| 4819 } | |
| 4820 | |
| 4821 | |
| 4822 TEST_CASE(FunctionReflection) { | |
| 4823 const char* kScriptChars = | |
| 4824 "a() => 'a';\n" | |
| 4825 "_b() => '_b';\n" | |
| 4826 "get c => 'bar';\n" | |
| 4827 "set d(x) {}\n" | |
| 4828 "get _e => 'bar';\n" | |
| 4829 "set _f(x) {}\n" | |
| 4830 "class MyClass {\n" | |
| 4831 " MyClass() {}\n" | |
| 4832 " MyClass.named() {}\n" | |
| 4833 " a() => 'a';\n" | |
| 4834 " _b() => '_b';\n" | |
| 4835 " get c => 'bar';\n" | |
| 4836 " set d(x) {}\n" | |
| 4837 " get _e => 'bar';\n" | |
| 4838 " set _f(x) {}\n" | |
| 4839 " static g() => 'g';\n" | |
| 4840 " static _h() => '_h';\n" | |
| 4841 " static get i => 'i';\n" | |
| 4842 " static set j(x) {}\n" | |
| 4843 " static get _k => 'k';\n" | |
| 4844 " static set _l(x) {}\n" | |
| 4845 " m();\n" | |
| 4846 " _n();\n" | |
| 4847 " get o;\n" | |
| 4848 " set p(x);\n" | |
| 4849 " get _q;\n" | |
| 4850 " set _r(x);\n" | |
| 4851 " s(x, [y, z]) {}\n" | |
| 4852 " t([x, y, z]) {}\n" | |
| 4853 " operator ==(x) {}\n" | |
| 4854 "}\n" | |
| 4855 "class _PrivateClass {\n" | |
| 4856 " _PrivateClass() {}\n" | |
| 4857 " _PrivateClass.named() {}\n" | |
| 4858 "}\n"; | |
| 4859 | |
| 4860 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 4861 EXPECT_VALID(lib); | |
| 4862 Dart_Handle cls = Dart_GetClass(lib, NewString("MyClass")); | |
| 4863 EXPECT_VALID(cls); | |
| 4864 Dart_Handle private_cls = Dart_GetClass(lib, NewString("_PrivateClass")); | |
| 4865 EXPECT_VALID(private_cls); | |
| 4866 TextBuffer buffer(128); | |
| 4867 | |
| 4868 // Lookup a top-level function. | |
| 4869 Dart_Handle func = Dart_LookupFunction(lib, NewString("a")); | |
| 4870 EXPECT_VALID(func); | |
| 4871 EXPECT(Dart_IsFunction(func)); | |
| 4872 BuildFunctionDescription(&buffer, func); | |
| 4873 EXPECT_STREQ("a 0 0 static", buffer.buf()); | |
| 4874 EXPECT(Dart_IsLibrary(Dart_FunctionOwner(func))); | |
| 4875 Dart_Handle owner = Dart_FunctionOwner(func); | |
| 4876 EXPECT_VALID(owner); | |
| 4877 EXPECT(Dart_IdentityEquals(owner, lib)); | |
| 4878 | |
| 4879 // Lookup a private top-level function. | |
| 4880 func = Dart_LookupFunction(lib, NewString("_b")); | |
| 4881 EXPECT_VALID(func); | |
| 4882 EXPECT(Dart_IsFunction(func)); | |
| 4883 BuildFunctionDescription(&buffer, func); | |
| 4884 EXPECT_STREQ("_b 0 0 static", buffer.buf()); | |
| 4885 owner = Dart_FunctionOwner(func); | |
| 4886 EXPECT_VALID(owner); | |
| 4887 EXPECT(Dart_IdentityEquals(owner, lib)); | |
| 4888 | |
| 4889 // Lookup a top-level getter. | |
| 4890 func = Dart_LookupFunction(lib, NewString("c")); | |
| 4891 EXPECT_VALID(func); | |
| 4892 EXPECT(Dart_IsFunction(func)); | |
| 4893 BuildFunctionDescription(&buffer, func); | |
| 4894 EXPECT_STREQ("c 0 0 static getter", buffer.buf()); | |
| 4895 owner = Dart_FunctionOwner(func); | |
| 4896 EXPECT_VALID(owner); | |
| 4897 EXPECT(Dart_IdentityEquals(owner, lib)); | |
| 4898 | |
| 4899 // Lookup a top-level setter. | |
| 4900 func = Dart_LookupFunction(lib, NewString("d=")); | |
| 4901 EXPECT_VALID(func); | |
| 4902 EXPECT(Dart_IsFunction(func)); | |
| 4903 BuildFunctionDescription(&buffer, func); | |
| 4904 EXPECT_STREQ("d= 1 0 static setter", buffer.buf()); | |
| 4905 owner = Dart_FunctionOwner(func); | |
| 4906 EXPECT_VALID(owner); | |
| 4907 EXPECT(Dart_IdentityEquals(owner, lib)); | |
| 4908 | |
| 4909 // Lookup a private top-level getter. | |
| 4910 func = Dart_LookupFunction(lib, NewString("_e")); | |
| 4911 EXPECT_VALID(func); | |
| 4912 EXPECT(Dart_IsFunction(func)); | |
| 4913 BuildFunctionDescription(&buffer, func); | |
| 4914 EXPECT_STREQ("_e 0 0 static getter", buffer.buf()); | |
| 4915 owner = Dart_FunctionOwner(func); | |
| 4916 EXPECT_VALID(owner); | |
| 4917 EXPECT(Dart_IdentityEquals(owner, lib)); | |
| 4918 | |
| 4919 // Lookup a private top-level setter. | |
| 4920 func = Dart_LookupFunction(lib, NewString("_f=")); | |
| 4921 EXPECT_VALID(func); | |
| 4922 EXPECT(Dart_IsFunction(func)); | |
| 4923 BuildFunctionDescription(&buffer, func); | |
| 4924 EXPECT_STREQ("_f= 1 0 static setter", buffer.buf()); | |
| 4925 owner = Dart_FunctionOwner(func); | |
| 4926 EXPECT_VALID(owner); | |
| 4927 EXPECT(Dart_IdentityEquals(owner, lib)); | |
| 4928 | |
| 4929 // Lookup an unnamed constructor | |
| 4930 func = Dart_LookupFunction(cls, NewString("MyClass")); | |
| 4931 EXPECT_VALID(func); | |
| 4932 EXPECT(Dart_IsFunction(func)); | |
| 4933 BuildFunctionDescription(&buffer, func); | |
| 4934 EXPECT_STREQ("MyClass 0 0 constructor", buffer.buf()); | |
| 4935 owner = Dart_FunctionOwner(func); | |
| 4936 EXPECT_VALID(owner); | |
| 4937 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 4938 | |
| 4939 // Lookup a named constructor | |
| 4940 func = Dart_LookupFunction(cls, NewString("MyClass.named")); | |
| 4941 EXPECT_VALID(func); | |
| 4942 EXPECT(Dart_IsFunction(func)); | |
| 4943 BuildFunctionDescription(&buffer, func); | |
| 4944 EXPECT_STREQ("MyClass.named 0 0 constructor", buffer.buf()); | |
| 4945 owner = Dart_FunctionOwner(func); | |
| 4946 EXPECT_VALID(owner); | |
| 4947 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 4948 | |
| 4949 // Lookup an private unnamed constructor | |
| 4950 func = Dart_LookupFunction(private_cls, NewString("_PrivateClass")); | |
| 4951 EXPECT_VALID(func); | |
| 4952 EXPECT(Dart_IsFunction(func)); | |
| 4953 BuildFunctionDescription(&buffer, func); | |
| 4954 EXPECT_STREQ("_PrivateClass 0 0 constructor", buffer.buf()); | |
| 4955 owner = Dart_FunctionOwner(func); | |
| 4956 EXPECT_VALID(owner); | |
| 4957 EXPECT(Dart_IdentityEquals(owner, private_cls)); | |
| 4958 | |
| 4959 // Lookup a private named constructor | |
| 4960 func = Dart_LookupFunction(private_cls, | |
| 4961 NewString("_PrivateClass.named")); | |
| 4962 EXPECT_VALID(func); | |
| 4963 EXPECT(Dart_IsFunction(func)); | |
| 4964 BuildFunctionDescription(&buffer, func); | |
| 4965 EXPECT_STREQ("_PrivateClass.named 0 0 constructor", buffer.buf()); | |
| 4966 owner = Dart_FunctionOwner(func); | |
| 4967 EXPECT_VALID(owner); | |
| 4968 EXPECT(Dart_IdentityEquals(owner, private_cls)); | |
| 4969 | |
| 4970 // Lookup a method. | |
| 4971 func = Dart_LookupFunction(cls, NewString("a")); | |
| 4972 EXPECT_VALID(func); | |
| 4973 EXPECT(Dart_IsFunction(func)); | |
| 4974 BuildFunctionDescription(&buffer, func); | |
| 4975 EXPECT_STREQ("a 0 0", buffer.buf()); | |
| 4976 owner = Dart_FunctionOwner(func); | |
| 4977 EXPECT_VALID(owner); | |
| 4978 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 4979 | |
| 4980 // Lookup a private method. | |
| 4981 func = Dart_LookupFunction(cls, NewString("_b")); | |
| 4982 EXPECT_VALID(func); | |
| 4983 EXPECT(Dart_IsFunction(func)); | |
| 4984 BuildFunctionDescription(&buffer, func); | |
| 4985 EXPECT_STREQ("_b 0 0", buffer.buf()); | |
| 4986 owner = Dart_FunctionOwner(func); | |
| 4987 EXPECT_VALID(owner); | |
| 4988 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 4989 | |
| 4990 // Lookup a instance getter. | |
| 4991 func = Dart_LookupFunction(cls, NewString("c")); | |
| 4992 EXPECT_VALID(func); | |
| 4993 EXPECT(Dart_IsFunction(func)); | |
| 4994 BuildFunctionDescription(&buffer, func); | |
| 4995 EXPECT_STREQ("c 0 0 getter", buffer.buf()); | |
| 4996 owner = Dart_FunctionOwner(func); | |
| 4997 EXPECT_VALID(owner); | |
| 4998 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 4999 | |
| 5000 // Lookup a instance setter. | |
| 5001 func = Dart_LookupFunction(cls, NewString("d=")); | |
| 5002 EXPECT_VALID(func); | |
| 5003 EXPECT(Dart_IsFunction(func)); | |
| 5004 BuildFunctionDescription(&buffer, func); | |
| 5005 EXPECT_STREQ("d= 1 0 setter", buffer.buf()); | |
| 5006 owner = Dart_FunctionOwner(func); | |
| 5007 EXPECT_VALID(owner); | |
| 5008 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5009 | |
| 5010 // Lookup a private instance getter. | |
| 5011 func = Dart_LookupFunction(cls, NewString("_e")); | |
| 5012 EXPECT_VALID(func); | |
| 5013 EXPECT(Dart_IsFunction(func)); | |
| 5014 BuildFunctionDescription(&buffer, func); | |
| 5015 EXPECT_STREQ("_e 0 0 getter", buffer.buf()); | |
| 5016 owner = Dart_FunctionOwner(func); | |
| 5017 EXPECT_VALID(owner); | |
| 5018 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5019 | |
| 5020 // Lookup a private instance setter. | |
| 5021 func = Dart_LookupFunction(cls, NewString("_f=")); | |
| 5022 EXPECT_VALID(func); | |
| 5023 EXPECT(Dart_IsFunction(func)); | |
| 5024 BuildFunctionDescription(&buffer, func); | |
| 5025 EXPECT_STREQ("_f= 1 0 setter", buffer.buf()); | |
| 5026 owner = Dart_FunctionOwner(func); | |
| 5027 EXPECT_VALID(owner); | |
| 5028 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5029 | |
| 5030 // Lookup a static method. | |
| 5031 func = Dart_LookupFunction(cls, NewString("g")); | |
| 5032 EXPECT_VALID(func); | |
| 5033 EXPECT(Dart_IsFunction(func)); | |
| 5034 BuildFunctionDescription(&buffer, func); | |
| 5035 EXPECT_STREQ("g 0 0 static", buffer.buf()); | |
| 5036 owner = Dart_FunctionOwner(func); | |
| 5037 EXPECT_VALID(owner); | |
| 5038 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5039 | |
| 5040 // Lookup a private static method. | |
| 5041 func = Dart_LookupFunction(cls, NewString("_h")); | |
| 5042 EXPECT_VALID(func); | |
| 5043 EXPECT(Dart_IsFunction(func)); | |
| 5044 BuildFunctionDescription(&buffer, func); | |
| 5045 EXPECT_STREQ("_h 0 0 static", buffer.buf()); | |
| 5046 owner = Dart_FunctionOwner(func); | |
| 5047 EXPECT_VALID(owner); | |
| 5048 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5049 | |
| 5050 // Lookup a static getter. | |
| 5051 func = Dart_LookupFunction(cls, NewString("i")); | |
| 5052 EXPECT_VALID(func); | |
| 5053 EXPECT(Dart_IsFunction(func)); | |
| 5054 BuildFunctionDescription(&buffer, func); | |
| 5055 EXPECT_STREQ("i 0 0 static getter", buffer.buf()); | |
| 5056 owner = Dart_FunctionOwner(func); | |
| 5057 EXPECT_VALID(owner); | |
| 5058 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5059 | |
| 5060 // Lookup a static setter. | |
| 5061 func = Dart_LookupFunction(cls, NewString("j=")); | |
| 5062 EXPECT_VALID(func); | |
| 5063 EXPECT(Dart_IsFunction(func)); | |
| 5064 BuildFunctionDescription(&buffer, func); | |
| 5065 EXPECT_STREQ("j= 1 0 static setter", buffer.buf()); | |
| 5066 owner = Dart_FunctionOwner(func); | |
| 5067 EXPECT_VALID(owner); | |
| 5068 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5069 | |
| 5070 // Lookup a private static getter. | |
| 5071 func = Dart_LookupFunction(cls, NewString("_k")); | |
| 5072 EXPECT_VALID(func); | |
| 5073 EXPECT(Dart_IsFunction(func)); | |
| 5074 BuildFunctionDescription(&buffer, func); | |
| 5075 EXPECT_STREQ("_k 0 0 static getter", buffer.buf()); | |
| 5076 owner = Dart_FunctionOwner(func); | |
| 5077 EXPECT_VALID(owner); | |
| 5078 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5079 | |
| 5080 // Lookup a private static setter. | |
| 5081 func = Dart_LookupFunction(cls, NewString("_l=")); | |
| 5082 EXPECT_VALID(func); | |
| 5083 EXPECT(Dart_IsFunction(func)); | |
| 5084 BuildFunctionDescription(&buffer, func); | |
| 5085 EXPECT_STREQ("_l= 1 0 static setter", buffer.buf()); | |
| 5086 owner = Dart_FunctionOwner(func); | |
| 5087 EXPECT_VALID(owner); | |
| 5088 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5089 | |
| 5090 // Lookup an abstract method. | |
| 5091 func = Dart_LookupFunction(cls, NewString("m")); | |
| 5092 EXPECT_VALID(func); | |
| 5093 EXPECT(Dart_IsFunction(func)); | |
| 5094 BuildFunctionDescription(&buffer, func); | |
| 5095 EXPECT_STREQ("m 0 0 abstract", buffer.buf()); | |
| 5096 owner = Dart_FunctionOwner(func); | |
| 5097 EXPECT_VALID(owner); | |
| 5098 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5099 | |
| 5100 // Lookup a private abstract method. | |
| 5101 func = Dart_LookupFunction(cls, NewString("_n")); | |
| 5102 EXPECT_VALID(func); | |
| 5103 EXPECT(Dart_IsFunction(func)); | |
| 5104 BuildFunctionDescription(&buffer, func); | |
| 5105 EXPECT_STREQ("_n 0 0 abstract", buffer.buf()); | |
| 5106 owner = Dart_FunctionOwner(func); | |
| 5107 EXPECT_VALID(owner); | |
| 5108 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5109 | |
| 5110 // Lookup a abstract getter. | |
| 5111 func = Dart_LookupFunction(cls, NewString("o")); | |
| 5112 EXPECT_VALID(func); | |
| 5113 EXPECT(Dart_IsFunction(func)); | |
| 5114 BuildFunctionDescription(&buffer, func); | |
| 5115 EXPECT_STREQ("o 0 0 abstract getter", buffer.buf()); | |
| 5116 owner = Dart_FunctionOwner(func); | |
| 5117 EXPECT_VALID(owner); | |
| 5118 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5119 | |
| 5120 // Lookup a abstract setter. | |
| 5121 func = Dart_LookupFunction(cls, NewString("p=")); | |
| 5122 EXPECT_VALID(func); | |
| 5123 EXPECT(Dart_IsFunction(func)); | |
| 5124 BuildFunctionDescription(&buffer, func); | |
| 5125 EXPECT_STREQ("p= 1 0 abstract setter", buffer.buf()); | |
| 5126 owner = Dart_FunctionOwner(func); | |
| 5127 EXPECT_VALID(owner); | |
| 5128 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5129 | |
| 5130 // Lookup a private abstract getter. | |
| 5131 func = Dart_LookupFunction(cls, NewString("_q")); | |
| 5132 EXPECT_VALID(func); | |
| 5133 EXPECT(Dart_IsFunction(func)); | |
| 5134 BuildFunctionDescription(&buffer, func); | |
| 5135 EXPECT_STREQ("_q 0 0 abstract getter", buffer.buf()); | |
| 5136 owner = Dart_FunctionOwner(func); | |
| 5137 EXPECT_VALID(owner); | |
| 5138 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5139 | |
| 5140 // Lookup a private abstract setter. | |
| 5141 func = Dart_LookupFunction(cls, NewString("_r=")); | |
| 5142 EXPECT_VALID(func); | |
| 5143 EXPECT(Dart_IsFunction(func)); | |
| 5144 BuildFunctionDescription(&buffer, func); | |
| 5145 EXPECT_STREQ("_r= 1 0 abstract setter", buffer.buf()); | |
| 5146 owner = Dart_FunctionOwner(func); | |
| 5147 EXPECT_VALID(owner); | |
| 5148 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5149 | |
| 5150 // Lookup a method with fixed and optional parameters. | |
| 5151 func = Dart_LookupFunction(cls, NewString("s")); | |
| 5152 EXPECT_VALID(func); | |
| 5153 EXPECT(Dart_IsFunction(func)); | |
| 5154 BuildFunctionDescription(&buffer, func); | |
| 5155 EXPECT_STREQ("s 1 2", buffer.buf()); | |
| 5156 owner = Dart_FunctionOwner(func); | |
| 5157 EXPECT_VALID(owner); | |
| 5158 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5159 | |
| 5160 // Lookup a method with only optional parameters. | |
| 5161 func = Dart_LookupFunction(cls, NewString("t")); | |
| 5162 EXPECT_VALID(func); | |
| 5163 EXPECT(Dart_IsFunction(func)); | |
| 5164 BuildFunctionDescription(&buffer, func); | |
| 5165 EXPECT_STREQ("t 0 3", buffer.buf()); | |
| 5166 owner = Dart_FunctionOwner(func); | |
| 5167 EXPECT_VALID(owner); | |
| 5168 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5169 | |
| 5170 // Lookup an operator | |
| 5171 func = Dart_LookupFunction(cls, NewString("==")); | |
| 5172 EXPECT_VALID(func); | |
| 5173 EXPECT(Dart_IsFunction(func)); | |
| 5174 BuildFunctionDescription(&buffer, func); | |
| 5175 EXPECT_STREQ("== 1 0", buffer.buf()); | |
| 5176 owner = Dart_FunctionOwner(func); | |
| 5177 EXPECT_VALID(owner); | |
| 5178 EXPECT(Dart_IdentityEquals(owner, cls)); | |
| 5179 | |
| 5180 // Lookup a function that does not exist from a library. | |
| 5181 func = Dart_LookupFunction(lib, NewString("DoesNotExist")); | |
| 5182 EXPECT(Dart_IsNull(func)); | |
| 5183 | |
| 5184 // Lookup a function that does not exist from a class. | |
| 5185 func = Dart_LookupFunction(cls, NewString("DoesNotExist")); | |
| 5186 EXPECT(Dart_IsNull(func)); | |
| 5187 | |
| 5188 // Lookup a class using an error class name. The error propagates. | |
| 5189 func = Dart_LookupFunction(cls, Api::NewError("myerror")); | |
| 5190 EXPECT_ERROR(func, "myerror"); | |
| 5191 | |
| 5192 // Lookup a class from an error library. The error propagates. | |
| 5193 func = Dart_LookupFunction(Api::NewError("myerror"), NewString("foo")); | |
| 5194 EXPECT_ERROR(func, "myerror"); | |
| 5195 } | |
| 5196 | |
| 5197 | |
| 5198 TEST_CASE(TypeReflection) { | |
| 5199 const char* kScriptChars = | |
| 5200 "void func(String a, int b) {}\n" | |
| 5201 "int variable;\n"; | |
| 5202 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 5203 EXPECT_VALID(lib); | |
| 5204 | |
| 5205 Dart_Handle func = Dart_LookupFunction(lib, NewString("func")); | |
| 5206 EXPECT_VALID(func); | |
| 5207 EXPECT(Dart_IsFunction(func)); | |
| 5208 | |
| 5209 // Make sure parameter counts are right. | |
| 5210 int64_t fixed_params = -1; | |
| 5211 int64_t opt_params = -1; | |
| 5212 EXPECT_VALID(Dart_FunctionParameterCounts(func, &fixed_params, &opt_params)); | |
| 5213 EXPECT_EQ(2, fixed_params); | |
| 5214 EXPECT_EQ(0, opt_params); | |
| 5215 | |
| 5216 // Check the return type. | |
| 5217 Dart_Handle type = Dart_FunctionReturnType(func); | |
| 5218 EXPECT_VALID(type); | |
| 5219 Dart_Handle cls_name = Dart_ClassName(type); | |
| 5220 EXPECT_VALID(cls_name); | |
| 5221 const char* cls_name_cstr = ""; | |
| 5222 EXPECT_VALID(Dart_StringToCString(cls_name, &cls_name_cstr)); | |
| 5223 EXPECT_STREQ("void", cls_name_cstr); | |
| 5224 | |
| 5225 // Check a parameter type. | |
| 5226 type = Dart_FunctionParameterType(func, 0); | |
| 5227 EXPECT_VALID(type); | |
| 5228 cls_name = Dart_ClassName(type); | |
| 5229 EXPECT_VALID(cls_name); | |
| 5230 cls_name_cstr = ""; | |
| 5231 EXPECT_VALID(Dart_StringToCString(cls_name, &cls_name_cstr)); | |
| 5232 EXPECT_STREQ("String", cls_name_cstr); | |
| 5233 | |
| 5234 Dart_Handle var = Dart_LookupVariable(lib, NewString("variable")); | |
| 5235 EXPECT_VALID(var); | |
| 5236 EXPECT(Dart_IsVariable(var)); | |
| 5237 | |
| 5238 // Check the variable type. | |
| 5239 type = Dart_VariableType(var); | |
| 5240 EXPECT_VALID(type); | |
| 5241 cls_name = Dart_ClassName(type); | |
| 5242 EXPECT_VALID(cls_name); | |
| 5243 cls_name_cstr = ""; | |
| 5244 EXPECT_VALID(Dart_StringToCString(cls_name, &cls_name_cstr)); | |
| 5245 if (FLAG_enable_type_checks) { | |
| 5246 EXPECT_STREQ("int", cls_name_cstr); | |
| 5247 } else { | |
| 5248 EXPECT_STREQ("dynamic", cls_name_cstr); | |
| 5249 } | |
| 5250 } | |
| 5251 | |
| 5252 | |
| 5253 static void BuildVariableDescription(TextBuffer* buffer, Dart_Handle var) { | |
| 5254 buffer->Clear(); | |
| 5255 Dart_Handle name = Dart_VariableName(var); | |
| 5256 EXPECT_VALID(name); | |
| 5257 const char* name_cstr = ""; | |
| 5258 EXPECT_VALID(Dart_StringToCString(name, &name_cstr)); | |
| 5259 bool is_static = false; | |
| 5260 bool is_final = false; | |
| 5261 EXPECT_VALID(Dart_VariableIsStatic(var, &is_static)); | |
| 5262 EXPECT_VALID(Dart_VariableIsFinal(var, &is_final)); | |
| 5263 buffer->Printf("%s", name_cstr); | |
| 5264 if (is_static) { | |
| 5265 buffer->Printf(" static"); | |
| 5266 } | |
| 5267 if (is_final) { | |
| 5268 buffer->Printf(" final"); | |
| 5269 } | |
| 5270 } | |
| 5271 | |
| 5272 | |
| 5273 TEST_CASE(VariableReflection) { | |
| 5274 const char* kScriptChars = | |
| 5275 "var a = 'a';\n" | |
| 5276 "var _b = '_b';\n" | |
| 5277 "final c = 'c';\n" | |
| 5278 "final _d = '_d';\n" | |
| 5279 "class MyClass {\n" | |
| 5280 " var a = 'a';\n" | |
| 5281 " var _b = '_b';\n" | |
| 5282 " final c = 'c';\n" | |
| 5283 " final _d = '_d';\n" | |
| 5284 " static var e = 'e';\n" | |
| 5285 " static var _f = '_f';\n" | |
| 5286 " static const g = 'g';\n" | |
| 5287 " static const _h = '_h';\n" | |
| 5288 "}\n"; | |
| 5289 | |
| 5290 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 5291 EXPECT_VALID(lib); | |
| 5292 Dart_Handle cls = Dart_GetClass(lib, NewString("MyClass")); | |
| 5293 EXPECT_VALID(cls); | |
| 5294 TextBuffer buffer(128); | |
| 5295 | |
| 5296 // Lookup a top-level variable. | |
| 5297 Dart_Handle var = Dart_LookupVariable(lib, NewString("a")); | |
| 5298 EXPECT_VALID(var); | |
| 5299 EXPECT(Dart_IsVariable(var)); | |
| 5300 BuildVariableDescription(&buffer, var); | |
| 5301 EXPECT_STREQ("a static", buffer.buf()); | |
| 5302 | |
| 5303 // Lookup a private top-level variable. | |
| 5304 var = Dart_LookupVariable(lib, NewString("_b")); | |
| 5305 EXPECT_VALID(var); | |
| 5306 EXPECT(Dart_IsVariable(var)); | |
| 5307 BuildVariableDescription(&buffer, var); | |
| 5308 EXPECT_STREQ("_b static", buffer.buf()); | |
| 5309 | |
| 5310 // Lookup a const top-level variable. | |
| 5311 var = Dart_LookupVariable(lib, NewString("c")); | |
| 5312 EXPECT_VALID(var); | |
| 5313 EXPECT(Dart_IsVariable(var)); | |
| 5314 BuildVariableDescription(&buffer, var); | |
| 5315 EXPECT_STREQ("c static final", buffer.buf()); | |
| 5316 | |
| 5317 // Lookup a private const top-level variable. | |
| 5318 var = Dart_LookupVariable(lib, NewString("_d")); | |
| 5319 EXPECT_VALID(var); | |
| 5320 EXPECT(Dart_IsVariable(var)); | |
| 5321 BuildVariableDescription(&buffer, var); | |
| 5322 EXPECT_STREQ("_d static final", buffer.buf()); | |
| 5323 | |
| 5324 // Lookup a instance variable. | |
| 5325 var = Dart_LookupVariable(cls, NewString("a")); | |
| 5326 EXPECT_VALID(var); | |
| 5327 EXPECT(Dart_IsVariable(var)); | |
| 5328 BuildVariableDescription(&buffer, var); | |
| 5329 EXPECT_STREQ("a", buffer.buf()); | |
| 5330 | |
| 5331 // Lookup a private instance variable. | |
| 5332 var = Dart_LookupVariable(cls, NewString("_b")); | |
| 5333 EXPECT_VALID(var); | |
| 5334 EXPECT(Dart_IsVariable(var)); | |
| 5335 BuildVariableDescription(&buffer, var); | |
| 5336 EXPECT_STREQ("_b", buffer.buf()); | |
| 5337 | |
| 5338 // Lookup a final instance variable. | |
| 5339 var = Dart_LookupVariable(cls, NewString("c")); | |
| 5340 EXPECT_VALID(var); | |
| 5341 EXPECT(Dart_IsVariable(var)); | |
| 5342 BuildVariableDescription(&buffer, var); | |
| 5343 EXPECT_STREQ("c final", buffer.buf()); | |
| 5344 | |
| 5345 // Lookup a private final instance variable. | |
| 5346 var = Dart_LookupVariable(cls, NewString("_d")); | |
| 5347 EXPECT_VALID(var); | |
| 5348 EXPECT(Dart_IsVariable(var)); | |
| 5349 BuildVariableDescription(&buffer, var); | |
| 5350 EXPECT_STREQ("_d final", buffer.buf()); | |
| 5351 | |
| 5352 // Lookup a static variable. | |
| 5353 var = Dart_LookupVariable(cls, NewString("e")); | |
| 5354 EXPECT_VALID(var); | |
| 5355 EXPECT(Dart_IsVariable(var)); | |
| 5356 BuildVariableDescription(&buffer, var); | |
| 5357 EXPECT_STREQ("e static", buffer.buf()); | |
| 5358 | |
| 5359 // Lookup a private static variable. | |
| 5360 var = Dart_LookupVariable(cls, NewString("_f")); | |
| 5361 EXPECT_VALID(var); | |
| 5362 EXPECT(Dart_IsVariable(var)); | |
| 5363 BuildVariableDescription(&buffer, var); | |
| 5364 EXPECT_STREQ("_f static", buffer.buf()); | |
| 5365 | |
| 5366 // Lookup a const static variable. | |
| 5367 var = Dart_LookupVariable(cls, NewString("g")); | |
| 5368 EXPECT_VALID(var); | |
| 5369 EXPECT(Dart_IsVariable(var)); | |
| 5370 BuildVariableDescription(&buffer, var); | |
| 5371 EXPECT_STREQ("g static final", buffer.buf()); | |
| 5372 | |
| 5373 // Lookup a private const static variable. | |
| 5374 var = Dart_LookupVariable(cls, NewString("_h")); | |
| 5375 EXPECT_VALID(var); | |
| 5376 EXPECT(Dart_IsVariable(var)); | |
| 5377 BuildVariableDescription(&buffer, var); | |
| 5378 EXPECT_STREQ("_h static final", buffer.buf()); | |
| 5379 | |
| 5380 // Lookup a variable that does not exist from a library. | |
| 5381 var = Dart_LookupVariable(lib, NewString("DoesNotExist")); | |
| 5382 EXPECT(Dart_IsNull(var)); | |
| 5383 | |
| 5384 // Lookup a variable that does not exist from a class. | |
| 5385 var = Dart_LookupVariable(cls, NewString("DoesNotExist")); | |
| 5386 EXPECT(Dart_IsNull(var)); | |
| 5387 | |
| 5388 // Lookup a class from an error library. The error propagates. | |
| 5389 var = Dart_LookupVariable(Api::NewError("myerror"), NewString("foo")); | |
| 5390 EXPECT_ERROR(var, "myerror"); | |
| 5391 | |
| 5392 // Lookup a class using an error class name. The error propagates. | |
| 5393 var = Dart_LookupVariable(lib, Api::NewError("myerror")); | |
| 5394 EXPECT_ERROR(var, "myerror"); | |
| 5395 } | |
| 5396 | |
| 5397 | |
| 5398 TEST_CASE(TypeVariableReflection) { | |
| 5399 const char* kScriptChars = | |
| 5400 "abstract class UpperBound {}\n" | |
| 5401 "class GenericClass<U, T extends UpperBound> {\n" | |
| 5402 " T func1() { return null; }\n" | |
| 5403 " U func2() { return null; }\n" | |
| 5404 "}\n"; | |
| 5405 | |
| 5406 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 5407 Dart_Handle cls = Dart_GetClass(lib, NewString("GenericClass")); | |
| 5408 EXPECT_VALID(cls); | |
| 5409 | |
| 5410 // Test Dart_GetTypeVariableNames. | |
| 5411 Dart_Handle names = Dart_GetTypeVariableNames(cls); | |
| 5412 EXPECT_VALID(names); | |
| 5413 Dart_Handle names_str = Dart_ToString(names); | |
| 5414 EXPECT_VALID(names_str); | |
| 5415 const char* cstr = ""; | |
| 5416 EXPECT_VALID(Dart_StringToCString(names_str, &cstr)); | |
| 5417 EXPECT_STREQ("[U, T]", cstr); | |
| 5418 | |
| 5419 // Test variable U. | |
| 5420 Dart_Handle type_var = Dart_LookupTypeVariable(cls, NewString("U")); | |
| 5421 EXPECT_VALID(type_var); | |
| 5422 EXPECT(Dart_IsTypeVariable(type_var)); | |
| 5423 Dart_Handle type_var_name = Dart_TypeVariableName(type_var); | |
| 5424 EXPECT_VALID(type_var_name); | |
| 5425 EXPECT_VALID(Dart_StringToCString(type_var_name, &cstr)); | |
| 5426 EXPECT_STREQ("U", cstr); | |
| 5427 Dart_Handle type_var_owner = Dart_TypeVariableOwner(type_var); | |
| 5428 EXPECT_VALID(type_var_owner); | |
| 5429 EXPECT(Dart_IdentityEquals(cls, type_var_owner)); | |
| 5430 Dart_Handle type_var_bound = Dart_TypeVariableUpperBound(type_var); | |
| 5431 Dart_Handle bound_name = Dart_ClassName(type_var_bound); | |
| 5432 EXPECT_VALID(Dart_StringToCString(bound_name, &cstr)); | |
| 5433 EXPECT_STREQ("Object", cstr); | |
| 5434 | |
| 5435 // Test variable T. | |
| 5436 type_var = Dart_LookupTypeVariable(cls, NewString("T")); | |
| 5437 EXPECT_VALID(type_var); | |
| 5438 EXPECT(Dart_IsTypeVariable(type_var)); | |
| 5439 type_var_name = Dart_TypeVariableName(type_var); | |
| 5440 EXPECT_VALID(type_var_name); | |
| 5441 EXPECT_VALID(Dart_StringToCString(type_var_name, &cstr)); | |
| 5442 EXPECT_STREQ("T", cstr); | |
| 5443 type_var_owner = Dart_TypeVariableOwner(type_var); | |
| 5444 EXPECT_VALID(type_var_owner); | |
| 5445 EXPECT(Dart_IdentityEquals(cls, type_var_owner)); | |
| 5446 type_var_bound = Dart_TypeVariableUpperBound(type_var); | |
| 5447 bound_name = Dart_ClassName(type_var_bound); | |
| 5448 EXPECT_VALID(Dart_StringToCString(bound_name, &cstr)); | |
| 5449 EXPECT_STREQ("UpperBound", cstr); | |
| 5450 } | |
| 5451 | |
| 5452 | |
| 5453 TEST_CASE(InstanceOf) { | 4480 TEST_CASE(InstanceOf) { |
| 5454 const char* kScriptChars = | 4481 const char* kScriptChars = |
| 5455 "class OtherClass {\n" | 4482 "class OtherClass {\n" |
| 5456 " static returnNull() { return null; }\n" | 4483 " static returnNull() { return null; }\n" |
| 5457 "}\n" | 4484 "}\n" |
| 5458 "class InstanceOfTest {\n" | 4485 "class InstanceOfTest {\n" |
| 5459 " InstanceOfTest() {}\n" | 4486 " InstanceOfTest() {}\n" |
| 5460 " static InstanceOfTest testMain() {\n" | 4487 " static InstanceOfTest testMain() {\n" |
| 5461 " return new InstanceOfTest();\n" | 4488 " return new InstanceOfTest();\n" |
| 5462 " }\n" | 4489 " }\n" |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5880 | 4907 |
| 5881 // Check list contents. | 4908 // Check list contents. |
| 5882 list_string = Dart_ToString(list); | 4909 list_string = Dart_ToString(list); |
| 5883 EXPECT_VALID(list_string); | 4910 EXPECT_VALID(list_string); |
| 5884 list_cstr = ""; | 4911 list_cstr = ""; |
| 5885 EXPECT_VALID(Dart_StringToCString(list_string, &list_cstr)); | 4912 EXPECT_VALID(Dart_StringToCString(list_string, &list_cstr)); |
| 5886 EXPECT_STREQ("[A2, B2, C2=, MyClass, _A2, _B2, _C2=]", list_cstr); | 4913 EXPECT_STREQ("[A2, B2, C2=, MyClass, _A2, _B2, _C2=]", list_cstr); |
| 5887 } | 4914 } |
| 5888 | 4915 |
| 5889 | 4916 |
| 5890 TEST_CASE(GetVariableNames) { | |
| 5891 const char* kLibraryChars = | |
| 5892 "library library_name;\n" | |
| 5893 "\n" | |
| 5894 "var A;\n" | |
| 5895 "get B => 12;\n" | |
| 5896 "set C(x) { }\n" | |
| 5897 "D(x) => (x + 1);\n" | |
| 5898 "var _A;\n" | |
| 5899 "get _B => 12;\n" | |
| 5900 "set _C(x) { }\n" | |
| 5901 "_D(x) => (x + 1);\n" | |
| 5902 "\n" | |
| 5903 "class MyClass {\n" | |
| 5904 " var A2;\n" | |
| 5905 " var _A2;\n" | |
| 5906 "}\n" | |
| 5907 "\n" | |
| 5908 "_compare(String a, String b) => a.compareTo(b);\n" | |
| 5909 "sort(list) => list.sort(_compare);\n"; | |
| 5910 | |
| 5911 // Get the variables from a library. | |
| 5912 Dart_Handle url = NewString("library_url"); | |
| 5913 Dart_Handle source = NewString(kLibraryChars); | |
| 5914 Dart_Handle lib = Dart_LoadLibrary(url, source); | |
| 5915 EXPECT_VALID(lib); | |
| 5916 | |
| 5917 Dart_Handle list = Dart_GetVariableNames(lib); | |
| 5918 EXPECT_VALID(list); | |
| 5919 EXPECT(Dart_IsList(list)); | |
| 5920 | |
| 5921 // Sort the list. | |
| 5922 const int kNumArgs = 1; | |
| 5923 Dart_Handle args[1]; | |
| 5924 args[0] = list; | |
| 5925 EXPECT_VALID(Dart_Invoke(lib, NewString("sort"), kNumArgs, args)); | |
| 5926 | |
| 5927 // Check list contents. | |
| 5928 Dart_Handle list_string = Dart_ToString(list); | |
| 5929 EXPECT_VALID(list_string); | |
| 5930 const char* list_cstr = ""; | |
| 5931 EXPECT_VALID(Dart_StringToCString(list_string, &list_cstr)); | |
| 5932 EXPECT_STREQ("[A, _A]", list_cstr); | |
| 5933 | |
| 5934 // Get the variables from a class. | |
| 5935 Dart_Handle cls = Dart_GetClass(lib, NewString("MyClass")); | |
| 5936 EXPECT_VALID(cls); | |
| 5937 | |
| 5938 list = Dart_GetVariableNames(cls); | |
| 5939 EXPECT_VALID(list); | |
| 5940 EXPECT(Dart_IsList(list)); | |
| 5941 | |
| 5942 // Sort the list. | |
| 5943 args[0] = list; | |
| 5944 EXPECT_VALID(Dart_Invoke(lib, NewString("sort"), kNumArgs, args)); | |
| 5945 | |
| 5946 // Check list contents. | |
| 5947 list_string = Dart_ToString(list); | |
| 5948 EXPECT_VALID(list_string); | |
| 5949 list_cstr = ""; | |
| 5950 EXPECT_VALID(Dart_StringToCString(list_string, &list_cstr)); | |
| 5951 EXPECT_STREQ("[A2, _A2]", list_cstr); | |
| 5952 } | |
| 5953 | |
| 5954 | |
| 5955 TEST_CASE(LibraryImportLibrary) { | 4917 TEST_CASE(LibraryImportLibrary) { |
| 5956 const char* kLibrary1Chars = | 4918 const char* kLibrary1Chars = |
| 5957 "library library1_name;"; | 4919 "library library1_name;"; |
| 5958 const char* kLibrary2Chars = | 4920 const char* kLibrary2Chars = |
| 5959 "library library2_name;"; | 4921 "library library2_name;"; |
| 5960 Dart_Handle error = Dart_Error("incoming error"); | 4922 Dart_Handle error = Dart_Error("incoming error"); |
| 5961 Dart_Handle result; | 4923 Dart_Handle result; |
| 5962 | 4924 |
| 5963 Dart_Handle url = NewString("library1_url"); | 4925 Dart_Handle url = NewString("library1_url"); |
| 5964 Dart_Handle source = NewString(kLibrary1Chars); | 4926 Dart_Handle source = NewString(kLibrary1Chars); |
| (...skipping 2241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8206 Dart_Handle result = Dart_Invoke(lib, | 7168 Dart_Handle result = Dart_Invoke(lib, |
| 8207 NewString("main"), | 7169 NewString("main"), |
| 8208 0, | 7170 0, |
| 8209 NULL); | 7171 NULL); |
| 8210 int64_t value = 0; | 7172 int64_t value = 0; |
| 8211 result = Dart_IntegerToInt64(result, &value); | 7173 result = Dart_IntegerToInt64(result, &value); |
| 8212 EXPECT_VALID(result); | 7174 EXPECT_VALID(result); |
| 8213 EXPECT_EQ(8, value); | 7175 EXPECT_EQ(8, value); |
| 8214 } | 7176 } |
| 8215 | 7177 |
| 8216 | |
| 8217 TEST_CASE(AmbiguousReference) { | |
| 8218 const char* kImportedScript1Chars = | |
| 8219 "library library1.dart;\n" | |
| 8220 "var foo;\n" | |
| 8221 "var foo1;\n" | |
| 8222 "bar() => 'library1.dart bar()';\n" | |
| 8223 "bar1() => 'library1.dart bar1()';\n" | |
| 8224 "baz() => 'library1.dart baz()';\n" | |
| 8225 "var bay;\n" | |
| 8226 "typedef int bax(int a, int b);\n" | |
| 8227 "class baw {}\n"; | |
| 8228 const char* kImportedScript2Chars = | |
| 8229 "library library2.dart;\n" | |
| 8230 "var foo;\n" | |
| 8231 "var foo2;\n" | |
| 8232 "bar() => 'library2.dart bar()';\n" | |
| 8233 "bar2() => 'library2.dart bar2()';\n" | |
| 8234 "var baz;\n" | |
| 8235 "bay() => 'library2.dart bay()';\n" | |
| 8236 "typedef double bax(int a, int b);\n" | |
| 8237 "var baw;\n"; | |
| 8238 const char* kScriptChars = | |
| 8239 "import 'library1.dart';\n" | |
| 8240 "import 'library2.dart';\n"; | |
| 8241 | |
| 8242 // Load imported libs. | |
| 8243 Dart_Handle url1 = NewString("library1.dart"); | |
| 8244 Dart_Handle source1 = NewString(kImportedScript1Chars); | |
| 8245 Dart_Handle imported_lib1 = Dart_LoadLibrary(url1, source1); | |
| 8246 EXPECT_VALID(imported_lib1); | |
| 8247 Dart_Handle url2 = NewString("library2.dart"); | |
| 8248 Dart_Handle source2 = NewString(kImportedScript2Chars); | |
| 8249 Dart_Handle imported_lib2 = Dart_LoadLibrary(url2, source2); | |
| 8250 EXPECT_VALID(imported_lib2); | |
| 8251 // Load main script. | |
| 8252 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 8253 EXPECT_VALID(lib); | |
| 8254 // Expect an error for ambiguous references. | |
| 8255 EXPECT(Dart_IsError(Dart_LookupVariable(lib, NewString("foo")))); | |
| 8256 EXPECT(Dart_IsError(Dart_LookupFunction(lib, NewString("bar")))); | |
| 8257 EXPECT(Dart_IsError(Dart_LookupFunction(lib, NewString("baz")))); | |
| 8258 EXPECT(Dart_IsError(Dart_LookupVariable(lib, NewString("bay")))); | |
| 8259 EXPECT(Dart_IsError(Dart_GetClass(lib, NewString("bax")))); | |
| 8260 EXPECT(Dart_IsError(Dart_GetClass(lib, NewString("baw")))); | |
| 8261 // Variables foo1 and foo2 are unambiguous. | |
| 8262 EXPECT(!Dart_IsError(Dart_LookupVariable(lib, NewString("foo1")))); | |
| 8263 EXPECT(!Dart_IsError(Dart_LookupVariable(lib, NewString("foo2")))); | |
| 8264 // Functions bar1 and bar2 are unambiguous. | |
| 8265 EXPECT(!Dart_IsError(Dart_LookupFunction(lib, NewString("bar1")))); | |
| 8266 EXPECT(!Dart_IsError(Dart_LookupFunction(lib, NewString("bar2")))); | |
| 8267 } | |
| 8268 | |
| 8269 } // namespace dart | 7178 } // namespace dart |
| OLD | NEW |