| 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 "include/dart_api.h" | 5 #include "include/dart_api.h" |
| 6 #include "platform/assert.h" | 6 #include "platform/assert.h" |
| 7 #include "platform/json.h" | 7 #include "platform/json.h" |
| 8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
| 9 #include "vm/class_finalizer.h" | 9 #include "vm/class_finalizer.h" |
| 10 #include "vm/dart_api_impl.h" | 10 #include "vm/dart_api_impl.h" |
| (...skipping 3698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3709 // We can only invoke non-private imported functions. | 3709 // We can only invoke non-private imported functions. |
| 3710 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("imported"), 0, NULL)); | 3710 EXPECT_VALID(Dart_Invoke(lib1, Dart_NewString("imported"), 0, NULL)); |
| 3711 EXPECT_ERROR(Dart_Invoke(lib1, Dart_NewString("_imported"), 0, NULL), | 3711 EXPECT_ERROR(Dart_Invoke(lib1, Dart_NewString("_imported"), 0, NULL), |
| 3712 "did not find top-level function '_imported'"); | 3712 "did not find top-level function '_imported'"); |
| 3713 } | 3713 } |
| 3714 | 3714 |
| 3715 TEST_CASE(ClosureFunction) { | 3715 TEST_CASE(ClosureFunction) { |
| 3716 const char* kScriptChars = | 3716 const char* kScriptChars = |
| 3717 "Function getClosure() {\n" | 3717 "Function getClosure() {\n" |
| 3718 " return (x, y, [z]) => x + y + z;\n" | 3718 " return (x, y, [z]) => x + y + z;\n" |
| 3719 "}\n" |
| 3720 "class Foo {\n" |
| 3721 " getInstanceClosure() {\n" |
| 3722 " return () { return this; };\n" |
| 3723 " }\n" |
| 3724 " getInstanceClosureWithArgs() {\n" |
| 3725 " return (x, y, [z]) { return this; };\n" |
| 3726 " }\n" |
| 3727 " static getStaticClosure() {\n" |
| 3728 " return () { return 42; };\n" |
| 3729 " }\n" |
| 3730 " static getStaticClosureWithArgs() {\n" |
| 3731 " return (x, y, [z]) { return 42; };\n" |
| 3732 " }\n" |
| 3733 "}\n" |
| 3734 "Function getInstanceClosure() {\n" |
| 3735 " return new Foo().getInstanceClosure();\n" |
| 3736 "}\n" |
| 3737 "Function getInstanceClosureWithArgs() {\n" |
| 3738 " return new Foo().getInstanceClosureWithArgs();\n" |
| 3739 "}\n" |
| 3740 "Function getStaticClosure() {\n" |
| 3741 " return Foo.getStaticClosure();\n" |
| 3742 "}\n" |
| 3743 "Function getStaticClosureWithArgs() {\n" |
| 3744 " return Foo.getStaticClosureWithArgs();\n" |
| 3719 "}\n"; | 3745 "}\n"; |
| 3720 Dart_Handle result; | 3746 Dart_Handle result; |
| 3721 DARTSCOPE_NOCHECKS(Isolate::Current()); | 3747 DARTSCOPE_NOCHECKS(Isolate::Current()); |
| 3722 | 3748 |
| 3723 // Create a test library and Load up a test script in it. | 3749 // Create a test library and Load up a test script in it. |
| 3724 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 3750 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 3725 | 3751 |
| 3726 // Invoke a function which returns a closure. | 3752 // Invoke a function which returns a closure. |
| 3727 Dart_Handle retobj = Dart_Invoke(lib, Dart_NewString("getClosure"), 0, NULL); | 3753 Dart_Handle retobj = Dart_Invoke(lib, Dart_NewString("getClosure"), 0, NULL); |
| 3728 EXPECT_VALID(retobj); | 3754 EXPECT_VALID(retobj); |
| 3729 | 3755 |
| 3730 EXPECT(Dart_IsClosure(retobj)); | 3756 EXPECT(Dart_IsClosure(retobj)); |
| 3731 EXPECT(!Dart_IsClosure(Dart_NewInteger(101))); | 3757 EXPECT(!Dart_IsClosure(Dart_NewInteger(101))); |
| 3732 | 3758 |
| 3733 // Retrieve the closure's function | 3759 // Retrieve the closure's function |
| 3734 result = Dart_ClosureFunction(retobj); | 3760 result = Dart_ClosureFunction(retobj); |
| 3735 EXPECT_VALID(result); | 3761 EXPECT_VALID(result); |
| 3736 EXPECT(Dart_IsFunction(result)); | 3762 EXPECT(Dart_IsFunction(result)); |
| 3737 int64_t fixed_param_count = -1; | 3763 int64_t fixed_param_count = -999; |
| 3738 int64_t opt_param_count = -1; | 3764 int64_t opt_param_count = -999; |
| 3739 result = Dart_FunctionParameterCounts(result, | 3765 result = Dart_FunctionParameterCounts(result, |
| 3740 &fixed_param_count, | 3766 &fixed_param_count, |
| 3741 &opt_param_count); | 3767 &opt_param_count); |
| 3742 EXPECT_VALID(result); | 3768 EXPECT_VALID(result); |
| 3743 EXPECT_EQ(2, fixed_param_count); | 3769 EXPECT_EQ(2, fixed_param_count); |
| 3744 EXPECT_EQ(1, opt_param_count); | 3770 EXPECT_EQ(1, opt_param_count); |
| 3745 | 3771 |
| 3746 // Try to retrieve function from a non-closure object | 3772 // Try to retrieve function from a non-closure object |
| 3747 result = Dart_ClosureFunction(Dart_NewInteger(1)); | 3773 result = Dart_ClosureFunction(Dart_NewInteger(1)); |
| 3748 EXPECT(Dart_IsError(result)); | 3774 EXPECT(Dart_IsError(result)); |
| 3775 |
| 3776 // Invoke a function which returns an "instance" closure. |
| 3777 retobj = Dart_Invoke(lib, Dart_NewString("getInstanceClosure"), 0, NULL); |
| 3778 EXPECT_VALID(retobj); |
| 3779 EXPECT(Dart_IsClosure(retobj)); |
| 3780 |
| 3781 // Retrieve the closure's function |
| 3782 result = Dart_ClosureFunction(retobj); |
| 3783 EXPECT_VALID(result); |
| 3784 EXPECT(Dart_IsFunction(result)); |
| 3785 // -999: We want to distinguish between a non-answer and a wrong answer, and |
| 3786 // -1 has been a previous wrong answer |
| 3787 fixed_param_count = -999; |
| 3788 opt_param_count = -999; |
| 3789 result = Dart_FunctionParameterCounts(result, |
| 3790 &fixed_param_count, |
| 3791 &opt_param_count); |
| 3792 EXPECT_VALID(result); |
| 3793 EXPECT_EQ(0, fixed_param_count); |
| 3794 EXPECT_EQ(0, opt_param_count); |
| 3795 |
| 3796 // Invoke a function which returns an "instance" closure with arguments. |
| 3797 retobj = Dart_Invoke(lib, |
| 3798 Dart_NewString("getInstanceClosureWithArgs"), |
| 3799 0, |
| 3800 NULL); |
| 3801 EXPECT_VALID(retobj); |
| 3802 EXPECT(Dart_IsClosure(retobj)); |
| 3803 |
| 3804 // Retrieve the closure's function |
| 3805 result = Dart_ClosureFunction(retobj); |
| 3806 EXPECT_VALID(result); |
| 3807 EXPECT(Dart_IsFunction(result)); |
| 3808 // -999: We want to distinguish between a non-answer and a wrong answer, and |
| 3809 // -1 has been a previous wrong answer |
| 3810 fixed_param_count = -999; |
| 3811 opt_param_count = -999; |
| 3812 result = Dart_FunctionParameterCounts(result, |
| 3813 &fixed_param_count, |
| 3814 &opt_param_count); |
| 3815 EXPECT_VALID(result); |
| 3816 EXPECT_EQ(2, fixed_param_count); |
| 3817 EXPECT_EQ(1, opt_param_count); |
| 3818 |
| 3819 // Invoke a function which returns a "static" closure. |
| 3820 retobj = Dart_Invoke(lib, Dart_NewString("getStaticClosure"), 0, NULL); |
| 3821 EXPECT_VALID(retobj); |
| 3822 EXPECT(Dart_IsClosure(retobj)); |
| 3823 |
| 3824 // Retrieve the closure's function |
| 3825 result = Dart_ClosureFunction(retobj); |
| 3826 EXPECT_VALID(result); |
| 3827 EXPECT(Dart_IsFunction(result)); |
| 3828 // -999: We want to distinguish between a non-answer and a wrong answer, and |
| 3829 // -1 has been a previous wrong answer |
| 3830 fixed_param_count = -999; |
| 3831 opt_param_count = -999; |
| 3832 result = Dart_FunctionParameterCounts(result, |
| 3833 &fixed_param_count, |
| 3834 &opt_param_count); |
| 3835 EXPECT_VALID(result); |
| 3836 EXPECT_EQ(0, fixed_param_count); |
| 3837 EXPECT_EQ(0, opt_param_count); |
| 3838 |
| 3839 |
| 3840 // Invoke a function which returns a "static" closure with arguments. |
| 3841 retobj = Dart_Invoke(lib, |
| 3842 Dart_NewString("getStaticClosureWithArgs"), |
| 3843 0, |
| 3844 NULL); |
| 3845 EXPECT_VALID(retobj); |
| 3846 EXPECT(Dart_IsClosure(retobj)); |
| 3847 |
| 3848 // Retrieve the closure's function |
| 3849 result = Dart_ClosureFunction(retobj); |
| 3850 EXPECT_VALID(result); |
| 3851 EXPECT(Dart_IsFunction(result)); |
| 3852 // -999: We want to distinguish between a non-answer and a wrong answer, and |
| 3853 // -1 has been a previous wrong answer |
| 3854 fixed_param_count = -999; |
| 3855 opt_param_count = -999; |
| 3856 result = Dart_FunctionParameterCounts(result, |
| 3857 &fixed_param_count, |
| 3858 &opt_param_count); |
| 3859 EXPECT_VALID(result); |
| 3860 EXPECT_EQ(2, fixed_param_count); |
| 3861 EXPECT_EQ(1, opt_param_count); |
| 3749 } | 3862 } |
| 3750 | 3863 |
| 3751 TEST_CASE(InvokeClosure) { | 3864 TEST_CASE(InvokeClosure) { |
| 3752 const char* kScriptChars = | 3865 const char* kScriptChars = |
| 3753 "class InvokeClosure {\n" | 3866 "class InvokeClosure {\n" |
| 3754 " InvokeClosure(int i, int j) : fld1 = i, fld2 = j {}\n" | 3867 " InvokeClosure(int i, int j) : fld1 = i, fld2 = j {}\n" |
| 3755 " Function method1(int i) {\n" | 3868 " Function method1(int i) {\n" |
| 3756 " f(int j) => j + i + fld1 + fld2 + fld4; \n" | 3869 " f(int j) => j + i + fld1 + fld2 + fld4; \n" |
| 3757 " return f;\n" | 3870 " return f;\n" |
| 3758 " }\n" | 3871 " }\n" |
| (...skipping 2460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6219 EXPECT_VALID(result); | 6332 EXPECT_VALID(result); |
| 6220 EXPECT(Dart_IsInteger(result)); | 6333 EXPECT(Dart_IsInteger(result)); |
| 6221 int64_t value = 0; | 6334 int64_t value = 0; |
| 6222 EXPECT_VALID(Dart_IntegerToInt64(result, &value)); | 6335 EXPECT_VALID(Dart_IntegerToInt64(result, &value)); |
| 6223 EXPECT_EQ(0, value); | 6336 EXPECT_EQ(0, value); |
| 6224 } | 6337 } |
| 6225 | 6338 |
| 6226 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 6339 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 6227 | 6340 |
| 6228 } // namespace dart | 6341 } // namespace dart |
| OLD | NEW |