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/utils.h" | 7 #include "platform/utils.h" |
8 #include "vm/dart_api_impl.h" | 8 #include "vm/dart_api_impl.h" |
9 #include "vm/dart_api_state.h" | 9 #include "vm/dart_api_state.h" |
10 #include "vm/thread.h" | 10 #include "vm/thread.h" |
(...skipping 2783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2794 | 2794 |
2795 result = Dart_InvokeStatic(result, | 2795 result = Dart_InvokeStatic(result, |
2796 Dart_NewString(""), | 2796 Dart_NewString(""), |
2797 Dart_NewString("main"), | 2797 Dart_NewString("main"), |
2798 0, | 2798 0, |
2799 NULL); | 2799 NULL); |
2800 EXPECT_VALID(result); | 2800 EXPECT_VALID(result); |
2801 } | 2801 } |
2802 | 2802 |
2803 | 2803 |
| 2804 void NewNativePort_send123(Dart_Port dest_port_id, |
| 2805 Dart_Port reply_port_id, |
| 2806 uint8_t* data) { |
| 2807 intptr_t response = 123; |
| 2808 Dart_PostIntArray(reply_port_id, 1, &response); |
| 2809 } |
| 2810 |
| 2811 |
| 2812 void NewNativePort_send321(Dart_Port dest_port_id, |
| 2813 Dart_Port reply_port_id, |
| 2814 uint8_t* data) { |
| 2815 intptr_t response = 321; |
| 2816 Dart_PostIntArray(reply_port_id, 1, &response); |
| 2817 } |
| 2818 |
| 2819 |
| 2820 UNIT_TEST_CASE(NewNativePort) { |
| 2821 // Create a port with a bogus handler. |
| 2822 Dart_Port error_port = Dart_NewNativePort("Foo", NULL, true); |
| 2823 EXPECT_EQ(kIllegalPort, error_port); |
| 2824 |
| 2825 // Create the port w/o a current isolate, just to make sure that works. |
| 2826 Dart_Port port_id1 = |
| 2827 Dart_NewNativePort("Port123", NewNativePort_send123, true); |
| 2828 |
| 2829 TestIsolateScope __test_isolate__; |
| 2830 const char* kScriptChars = |
| 2831 "void callPort(SendPort port) {\n" |
| 2832 " port.call(null).receive((message, replyTo) {\n" |
| 2833 " throw new Exception(message[0]);\n" |
| 2834 " });\n" |
| 2835 "}\n"; |
| 2836 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 2837 Dart_EnterScope(); |
| 2838 |
| 2839 // Create a port w/ a current isolate, to make sure that works too. |
| 2840 Dart_Port port_id2 = |
| 2841 Dart_NewNativePort("Port321", NewNativePort_send321, true); |
| 2842 |
| 2843 Dart_Handle send_port1 = Dart_NewSendPort(port_id1); |
| 2844 EXPECT_VALID(send_port1); |
| 2845 Dart_Handle send_port2 = Dart_NewSendPort(port_id2); |
| 2846 EXPECT_VALID(send_port2); |
| 2847 |
| 2848 // Test first port. |
| 2849 Dart_Handle dart_args[1]; |
| 2850 dart_args[0] = send_port1; |
| 2851 Dart_Handle result = Dart_InvokeStatic(lib, |
| 2852 Dart_NewString(""), |
| 2853 Dart_NewString("callPort"), |
| 2854 1, |
| 2855 dart_args); |
| 2856 EXPECT_VALID(result); |
| 2857 result = Dart_RunLoop(); |
| 2858 EXPECT(Dart_IsError(result)); |
| 2859 EXPECT(Dart_ErrorHasException(result)); |
| 2860 EXPECT_SUBSTRING("Exception: 123\n", Dart_GetError(result)); |
| 2861 |
| 2862 // result second port. |
| 2863 dart_args[0] = send_port2; |
| 2864 result = Dart_InvokeStatic(lib, |
| 2865 Dart_NewString(""), |
| 2866 Dart_NewString("callPort"), |
| 2867 1, |
| 2868 dart_args); |
| 2869 EXPECT_VALID(result); |
| 2870 result = Dart_RunLoop(); |
| 2871 EXPECT(Dart_IsError(result)); |
| 2872 EXPECT(Dart_ErrorHasException(result)); |
| 2873 EXPECT_SUBSTRING("Exception: 321\n", Dart_GetError(result)); |
| 2874 |
| 2875 Dart_ExitScope(); |
| 2876 |
| 2877 // Delete the native ports. |
| 2878 EXPECT(Dart_CloseNativePort(port_id1)); |
| 2879 EXPECT(Dart_CloseNativePort(port_id2)); |
| 2880 } |
| 2881 |
| 2882 |
2804 static bool RunLoopTestCallback(const char* name_prefix, | 2883 static bool RunLoopTestCallback(const char* name_prefix, |
2805 void* data, char** error) { | 2884 void* data, char** error) { |
2806 const char* kScriptChars = | 2885 const char* kScriptChars = |
2807 "#import('builtin');\n" | 2886 "#import('builtin');\n" |
2808 "class MyIsolate extends Isolate {\n" | 2887 "class MyIsolate extends Isolate {\n" |
2809 " MyIsolate() : super() { }\n" | 2888 " MyIsolate() : super() { }\n" |
2810 " void main() {\n" | 2889 " void main() {\n" |
2811 " port.receive((message, replyTo) {\n" | 2890 " port.receive((message, replyTo) {\n" |
2812 " if (message) {\n" | 2891 " if (message) {\n" |
2813 " throw new Exception('MakeChildExit');\n" | 2892 " throw new Exception('MakeChildExit');\n" |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3052 // We should have received the expected number of interrupts. | 3131 // We should have received the expected number of interrupts. |
3053 EXPECT_EQ(kInterruptCount, interrupt_count); | 3132 EXPECT_EQ(kInterruptCount, interrupt_count); |
3054 | 3133 |
3055 // Give the spawned thread enough time to properly exit. | 3134 // Give the spawned thread enough time to properly exit. |
3056 Isolate::SetInterruptCallback(saved); | 3135 Isolate::SetInterruptCallback(saved); |
3057 } | 3136 } |
3058 | 3137 |
3059 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 3138 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
3060 | 3139 |
3061 } // namespace dart | 3140 } // namespace dart |
OLD | NEW |