OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 "include/dart_native_api.h" | 6 #include "include/dart_native_api.h" |
7 | 7 |
8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
9 | 9 |
10 // Custom Isolate Test. | 10 // Custom Isolate Test. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 " SendPort spawn();\n" | 52 " SendPort spawn();\n" |
53 "}\n" | 53 "}\n" |
54 "\n" | 54 "\n" |
55 "isolateMain() {\n" | 55 "isolateMain() {\n" |
56 " echo('Running isolateMain');\n" | 56 " echo('Running isolateMain');\n" |
57 " mainPort.handler = (message) {\n" | 57 " mainPort.handler = (message) {\n" |
58 " var data = message[0];\n" | 58 " var data = message[0];\n" |
59 " var replyTo = message[1];\n" | 59 " var replyTo = message[1];\n" |
60 " echo('Received: $data');\n" | 60 " echo('Received: $data');\n" |
61 " replyTo.send(data + 1);\n" | 61 " replyTo.send(data + 1);\n" |
62 " mainPort.close();\n" | |
63 " };\n" | 62 " };\n" |
64 "}\n" | 63 "}\n" |
65 "\n" | 64 "\n" |
66 "main() {\n" | 65 "main() {\n" |
67 " var isolate = new CustomIsolate(\"isolateMain\");\n" | 66 " var isolate = new CustomIsolate(\"isolateMain\");\n" |
68 " var receivePort = new RawReceivePort();\n" | 67 " var receivePort = new RawReceivePort();\n" |
69 " SendPort port = isolate.spawn();\n" | 68 " SendPort port = isolate.spawn();\n" |
70 " port.send([42, receivePort.sendPort]);\n" | 69 " port.send([42, receivePort.sendPort]);\n" |
71 " receivePort.handler = (message) {\n" | 70 " receivePort.handler = (message) {\n" |
72 " receivePort.close();\n" | 71 " receivePort.close();\n" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 Dart_Handle result; | 172 Dart_Handle result; |
174 | 173 |
175 Dart_Handle lib = Dart_LookupLibrary(NewString(TestCase::url())); | 174 Dart_Handle lib = Dart_LookupLibrary(NewString(TestCase::url())); |
176 EXPECT_VALID(lib); | 175 EXPECT_VALID(lib); |
177 | 176 |
178 result = Dart_Invoke(lib, NewString(main_), 0, NULL); | 177 result = Dart_Invoke(lib, NewString(main_), 0, NULL); |
179 EXPECT_VALID(result); | 178 EXPECT_VALID(result); |
180 free(const_cast<char*>(main_)); | 179 free(const_cast<char*>(main_)); |
181 main_ = NULL; | 180 main_ = NULL; |
182 | 181 |
183 Dart_SetMessageNotifyCallback(NULL); | |
184 Dart_ExitScope(); | 182 Dart_ExitScope(); |
185 Dart_ExitIsolate(); | 183 Dart_ExitIsolate(); |
186 } | 184 } |
187 | 185 |
188 | 186 |
189 // Notify an isolate of a pending message. | 187 // Notify an isolate of a pending message. |
190 class MessageEvent : public Event { | 188 class MessageEvent : public Event { |
191 public: | 189 public: |
192 explicit MessageEvent(Dart_Isolate isolate) : Event(isolate) {} | 190 explicit MessageEvent(Dart_Isolate isolate) : Event(isolate) {} |
193 | 191 |
194 ~MessageEvent() { | 192 ~MessageEvent() { |
195 } | 193 } |
196 | 194 |
197 virtual void Process(); | 195 virtual void Process(); |
198 }; | 196 }; |
199 | 197 |
200 | 198 |
201 void MessageEvent::Process() { | 199 void MessageEvent::Process() { |
202 OS::Print("$$ MessageEvent with isolate(%p)\n", isolate()); | 200 OS::Print("$$ MessageEvent with isolate(%p)\n", isolate()); |
203 Dart_EnterIsolate(isolate()); | 201 Dart_EnterIsolate(isolate()); |
204 Dart_EnterScope(); | 202 Dart_EnterScope(); |
205 | 203 |
206 Dart_Handle result = Dart_HandleMessage(); | 204 Dart_Handle result = Dart_HandleMessage(); |
207 EXPECT_VALID(result); | 205 EXPECT_VALID(result); |
208 | 206 |
209 if (!Dart_HasLivePorts()) { | 207 if (!Dart_HasLivePorts()) { |
210 OS::Print("<< Shutting down isolate(%p)\n", isolate()); | 208 OS::Print("<< Shutting down isolate(%p)\n", isolate()); |
211 event_queue->RemoveEventsForIsolate(isolate()); | 209 event_queue->RemoveEventsForIsolate(isolate()); |
212 Dart_SetMessageNotifyCallback(NULL); | |
213 Dart_ShutdownIsolate(); | 210 Dart_ShutdownIsolate(); |
214 } else { | 211 } else { |
215 Dart_ExitScope(); | 212 Dart_ExitScope(); |
216 Dart_ExitIsolate(); | 213 Dart_ExitIsolate(); |
217 } | 214 } |
218 ASSERT(Dart_CurrentIsolate() == NULL); | 215 ASSERT(Dart_CurrentIsolate() == NULL); |
219 } | 216 } |
220 | 217 |
221 | 218 |
222 static void NotifyMessage(Dart_Isolate dest_isolate) { | 219 static void NotifyMessage(Dart_Isolate dest_isolate) { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 while (event) { | 343 while (event) { |
347 event->Process(); | 344 event->Process(); |
348 delete event; | 345 delete event; |
349 event = event_queue->Get(); | 346 event = event_queue->Get(); |
350 } | 347 } |
351 OS::Print("-- Finished event loop --\n"); | 348 OS::Print("-- Finished event loop --\n"); |
352 EXPECT_STREQ("Received: 43", saved_echo); | 349 EXPECT_STREQ("Received: 43", saved_echo); |
353 free(const_cast<char*>(saved_echo)); | 350 free(const_cast<char*>(saved_echo)); |
354 | 351 |
355 delete event_queue; | 352 delete event_queue; |
356 event_queue = NULL; | |
357 } | 353 } |
358 | 354 |
359 } // namespace dart | 355 } // namespace dart |
OLD | NEW |