Chromium Code Reviews| 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" | |
|
Ivan Posva
2012/07/26 01:01:09
Please add spaces between (), { and }.
| |
| 3723 " }\n" | |
| 3724 "}\n" | |
| 3725 "Function getInstanceClosure() {\n" | |
| 3726 " var f = new Foo();\n" | |
| 3727 " return f.getInstanceClosure();\n" | |
| 3719 "}\n"; | 3728 "}\n"; |
| 3720 Dart_Handle result; | 3729 Dart_Handle result; |
| 3721 DARTSCOPE_NOCHECKS(Isolate::Current()); | 3730 DARTSCOPE_NOCHECKS(Isolate::Current()); |
| 3722 | 3731 |
| 3723 // Create a test library and Load up a test script in it. | 3732 // Create a test library and Load up a test script in it. |
| 3724 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 3733 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 3725 | 3734 |
| 3726 // Invoke a function which returns a closure. | 3735 // Invoke a function which returns a closure. |
| 3727 Dart_Handle retobj = Dart_Invoke(lib, Dart_NewString("getClosure"), 0, NULL); | 3736 Dart_Handle retobj = Dart_Invoke(lib, Dart_NewString("getClosure"), 0, NULL); |
| 3728 EXPECT_VALID(retobj); | 3737 EXPECT_VALID(retobj); |
| 3729 | 3738 |
| 3730 EXPECT(Dart_IsClosure(retobj)); | 3739 EXPECT(Dart_IsClosure(retobj)); |
| 3731 EXPECT(!Dart_IsClosure(Dart_NewInteger(101))); | 3740 EXPECT(!Dart_IsClosure(Dart_NewInteger(101))); |
| 3732 | 3741 |
| 3733 // Retrieve the closure's function | 3742 // Retrieve the closure's function |
| 3734 result = Dart_ClosureFunction(retobj); | 3743 result = Dart_ClosureFunction(retobj); |
| 3735 EXPECT_VALID(result); | 3744 EXPECT_VALID(result); |
| 3736 EXPECT(Dart_IsFunction(result)); | 3745 EXPECT(Dart_IsFunction(result)); |
| 3737 int64_t fixed_param_count = -1; | 3746 int64_t fixed_param_count = -999; |
| 3738 int64_t opt_param_count = -1; | 3747 int64_t opt_param_count = -999; |
| 3739 result = Dart_FunctionParameterCounts(result, | 3748 result = Dart_FunctionParameterCounts(result, |
| 3740 &fixed_param_count, | 3749 &fixed_param_count, |
| 3741 &opt_param_count); | 3750 &opt_param_count); |
| 3742 EXPECT_VALID(result); | 3751 EXPECT_VALID(result); |
| 3743 EXPECT_EQ(2, fixed_param_count); | 3752 EXPECT_EQ(2, fixed_param_count); |
| 3744 EXPECT_EQ(1, opt_param_count); | 3753 EXPECT_EQ(1, opt_param_count); |
| 3745 | 3754 |
| 3746 // Try to retrieve function from a non-closure object | 3755 // Try to retrieve function from a non-closure object |
| 3747 result = Dart_ClosureFunction(Dart_NewInteger(1)); | 3756 result = Dart_ClosureFunction(Dart_NewInteger(1)); |
| 3748 EXPECT(Dart_IsError(result)); | 3757 EXPECT(Dart_IsError(result)); |
| 3758 | |
| 3759 // Invoke a function which returns an "instance" closure. | |
| 3760 retobj = Dart_Invoke(lib, Dart_NewString("getInstanceClosure"), 0, NULL); | |
| 3761 EXPECT_VALID(retobj); | |
| 3762 EXPECT(Dart_IsClosure(retobj)); | |
| 3763 | |
| 3764 // Retrieve the closure's function | |
| 3765 result = Dart_ClosureFunction(retobj); | |
| 3766 EXPECT_VALID(result); | |
| 3767 EXPECT(Dart_IsFunction(result)); | |
| 3768 // -999: We want to distinguish between a non-answer and a wrong answer, and | |
| 3769 // -1 has been a previous wrong answer | |
| 3770 fixed_param_count = -999; | |
| 3771 opt_param_count = -999; | |
| 3772 result = Dart_FunctionParameterCounts(result, | |
| 3773 &fixed_param_count, | |
| 3774 &opt_param_count); | |
| 3775 EXPECT_VALID(result); | |
| 3776 EXPECT_EQ(0, fixed_param_count); | |
|
Ivan Posva
2012/07/26 01:01:09
We should also test with closures in statics and w
| |
| 3777 EXPECT_EQ(0, opt_param_count); | |
| 3749 } | 3778 } |
| 3750 | 3779 |
| 3751 TEST_CASE(InvokeClosure) { | 3780 TEST_CASE(InvokeClosure) { |
| 3752 const char* kScriptChars = | 3781 const char* kScriptChars = |
| 3753 "class InvokeClosure {\n" | 3782 "class InvokeClosure {\n" |
| 3754 " InvokeClosure(int i, int j) : fld1 = i, fld2 = j {}\n" | 3783 " InvokeClosure(int i, int j) : fld1 = i, fld2 = j {}\n" |
| 3755 " Function method1(int i) {\n" | 3784 " Function method1(int i) {\n" |
| 3756 " f(int j) => j + i + fld1 + fld2 + fld4; \n" | 3785 " f(int j) => j + i + fld1 + fld2 + fld4; \n" |
| 3757 " return f;\n" | 3786 " return f;\n" |
| 3758 " }\n" | 3787 " }\n" |
| (...skipping 2460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 6219 EXPECT_VALID(result); | 6248 EXPECT_VALID(result); |
| 6220 EXPECT(Dart_IsInteger(result)); | 6249 EXPECT(Dart_IsInteger(result)); |
| 6221 int64_t value = 0; | 6250 int64_t value = 0; |
| 6222 EXPECT_VALID(Dart_IntegerToInt64(result, &value)); | 6251 EXPECT_VALID(Dart_IntegerToInt64(result, &value)); |
| 6223 EXPECT_EQ(0, value); | 6252 EXPECT_EQ(0, value); |
| 6224 } | 6253 } |
| 6225 | 6254 |
| 6226 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 6255 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 6227 | 6256 |
| 6228 } // namespace dart | 6257 } // namespace dart |
| OLD | NEW |