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