| 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 | 6 |
| 7 #include "vm/unit_test.h" | 7 #include "vm/unit_test.h" |
| 8 | 8 |
| 9 // Custom Isolate Test. | 9 // Custom Isolate Test. |
| 10 // | 10 // |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 " echo('Received: ' + message);\n" | 73 " echo('Received: ' + message);\n" |
| 74 " });\n" | 74 " });\n" |
| 75 " });\n" | 75 " });\n" |
| 76 " return 'success';\n" | 76 " return 'success';\n" |
| 77 "}\n"; | 77 "}\n"; |
| 78 | 78 |
| 79 | 79 |
| 80 // An entry in our event queue. | 80 // An entry in our event queue. |
| 81 class Event { | 81 class Event { |
| 82 protected: | 82 protected: |
| 83 Event() : next_(NULL) {} | 83 explicit Event(Dart_Isolate isolate) : isolate_(isolate), next_(NULL) {} |
| 84 | 84 |
| 85 public: | 85 public: |
| 86 virtual ~Event() {} | 86 virtual ~Event() {} |
| 87 virtual void Process() = 0; | 87 virtual void Process() = 0; |
| 88 | 88 |
| 89 virtual bool IsShutdownEvent(Dart_Isolate isolate) { | 89 Dart_Isolate isolate() const { return isolate_; } |
| 90 return false; | 90 |
| 91 private: |
| 92 friend class EventQueue; |
| 93 Dart_Isolate isolate_; |
| 94 Event* next_; |
| 95 }; |
| 96 |
| 97 |
| 98 // A simple event queue for our test. |
| 99 class EventQueue { |
| 100 public: |
| 101 EventQueue() { |
| 102 head_ = NULL; |
| 91 } | 103 } |
| 92 virtual bool IsMessageEvent(Dart_Isolate isolate, Dart_Port port) { | 104 |
| 93 return false; | 105 void Add(Event* event) { |
| 106 if (head_ == NULL) { |
| 107 head_ = event; |
| 108 tail_ = event; |
| 109 } else { |
| 110 tail_->next_ = event; |
| 111 tail_ = event; |
| 112 } |
| 113 } |
| 114 |
| 115 Event* Get() { |
| 116 if (head_ == NULL) { |
| 117 return NULL; |
| 118 } |
| 119 Event* tmp = head_; |
| 120 head_ = head_->next_; |
| 121 if (head_ == NULL) { |
| 122 // Not necessary, but why not. |
| 123 tail_ = NULL; |
| 124 } |
| 125 |
| 126 return tmp; |
| 127 } |
| 128 |
| 129 void RemoveEventsForIsolate(Dart_Isolate isolate) { |
| 130 Event* cur = head_; |
| 131 Event* prev = NULL; |
| 132 while (cur != NULL) { |
| 133 Event* next = cur->next_; |
| 134 if (cur->isolate() == isolate) { |
| 135 // Remove matching event. |
| 136 if (prev != NULL) { |
| 137 prev->next_ = next; |
| 138 } else { |
| 139 head_ = next; |
| 140 } |
| 141 delete cur; |
| 142 } else { |
| 143 // Advance. |
| 144 prev = cur; |
| 145 } |
| 146 cur = next; |
| 147 } |
| 148 tail_ = prev; |
| 94 } | 149 } |
| 95 | 150 |
| 96 private: | 151 private: |
| 97 friend class EventQueue; | 152 Event* head_; |
| 98 Event* next_; | 153 Event* tail_; |
| 99 }; | 154 }; |
| 155 EventQueue* event_queue; |
| 100 | 156 |
| 101 | 157 |
| 102 // Start an isolate. | 158 // Start an isolate. |
| 103 class StartEvent : public Event { | 159 class StartEvent : public Event { |
| 104 public: | 160 public: |
| 105 StartEvent(Dart_Isolate isolate, const char* main) | 161 StartEvent(Dart_Isolate isolate, const char* main) |
| 106 : isolate_(isolate), main_(main) {} | 162 : Event(isolate), main_(main) {} |
| 107 | 163 |
| 108 virtual void Process(); | 164 virtual void Process(); |
| 109 private: | 165 private: |
| 110 Dart_Isolate isolate_; | |
| 111 const char* main_; | 166 const char* main_; |
| 112 }; | 167 }; |
| 113 | 168 |
| 114 | 169 |
| 115 void StartEvent::Process() { | 170 void StartEvent::Process() { |
| 116 OS::Print(">> StartEvent with isolate(%p)--\n", isolate_); | 171 OS::Print(">> StartEvent with isolate(%p)--\n", isolate()); |
| 117 Dart_EnterIsolate(isolate_); | 172 Dart_EnterIsolate(isolate()); |
| 118 Dart_EnterScope(); | 173 Dart_EnterScope(); |
| 119 Dart_Handle result; | 174 Dart_Handle result; |
| 120 | 175 |
| 121 // Reload all the test classes here. | 176 // Reload all the test classes here. |
| 122 // | 177 // |
| 123 // TODO(turnidge): Use the create isolate callback instead? | 178 // TODO(turnidge): Use the create isolate callback instead? |
| 124 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, | 179 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, |
| 125 NativeLookup); | 180 NativeLookup); |
| 126 EXPECT_VALID(lib); | 181 EXPECT_VALID(lib); |
| 127 EXPECT_VALID(Dart_CompileAll()); | 182 EXPECT_VALID(Dart_CompileAll()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 144 NULL); | 199 NULL); |
| 145 EXPECT_VALID(result); | 200 EXPECT_VALID(result); |
| 146 free(const_cast<char*>(main_)); | 201 free(const_cast<char*>(main_)); |
| 147 main_ = NULL; | 202 main_ = NULL; |
| 148 | 203 |
| 149 Dart_ExitScope(); | 204 Dart_ExitScope(); |
| 150 Dart_ExitIsolate(); | 205 Dart_ExitIsolate(); |
| 151 } | 206 } |
| 152 | 207 |
| 153 | 208 |
| 154 // Shutdown an isolate. | 209 // Notify an isolate of a pending message. |
| 155 class ShutdownEvent : public Event { | 210 class MessageEvent : public Event { |
| 156 public: | 211 public: |
| 157 explicit ShutdownEvent(Dart_Isolate isolate) : isolate_(isolate) {} | 212 explicit MessageEvent(Dart_Isolate isolate) : Event(isolate) {} |
| 158 | 213 |
| 159 virtual bool IsShutdownEvent(Dart_Isolate isolate) { | 214 ~MessageEvent() { |
| 160 return isolate == isolate_; | |
| 161 } | 215 } |
| 162 | 216 |
| 163 virtual void Process(); | 217 virtual void Process(); |
| 164 private: | |
| 165 Dart_Isolate isolate_; | |
| 166 }; | |
| 167 | |
| 168 | |
| 169 void ShutdownEvent::Process() { | |
| 170 OS::Print("<< ShutdownEvent with isolate(%p)--\n", isolate_); | |
| 171 Dart_EnterIsolate(isolate_); | |
| 172 Dart_ShutdownIsolate(); | |
| 173 } | |
| 174 | |
| 175 | |
| 176 // Deliver a message to an isolate. | |
| 177 class MessageEvent : public Event { | |
| 178 public: | |
| 179 MessageEvent(Dart_Isolate isolate, Dart_Port dest, Dart_Port reply, | |
| 180 Dart_Message msg) | |
| 181 : isolate_(isolate), dest_(dest), reply_(reply), msg_(msg) {} | |
| 182 | |
| 183 ~MessageEvent() { | |
| 184 free(msg_); | |
| 185 msg_ = NULL; | |
| 186 } | |
| 187 | |
| 188 virtual bool IsMessageEvent(Dart_Isolate isolate, Dart_Port port) { | |
| 189 return isolate == isolate_ && (port == kCloseAllPorts || port == dest_); | |
| 190 } | |
| 191 | |
| 192 virtual void Process(); | |
| 193 private: | |
| 194 Dart_Isolate isolate_; | |
| 195 Dart_Port dest_; | |
| 196 Dart_Port reply_; | |
| 197 Dart_Message msg_; | |
| 198 }; | 218 }; |
| 199 | 219 |
| 200 | 220 |
| 201 void MessageEvent::Process() { | 221 void MessageEvent::Process() { |
| 202 OS::Print("$$ MessageEvent with dest port %lld--\n", dest_); | 222 OS::Print("$$ MessageEvent with isolate(%p)\n", isolate()); |
| 203 Dart_EnterIsolate(isolate_); | 223 Dart_EnterIsolate(isolate()); |
| 204 Dart_EnterScope(); | 224 Dart_EnterScope(); |
| 205 | 225 |
| 206 Dart_Handle result = Dart_HandleMessage(dest_, reply_, msg_); | 226 Dart_Handle result = Dart_HandleMessage(); |
| 207 EXPECT_VALID(result); | 227 EXPECT_VALID(result); |
| 208 | 228 |
| 209 Dart_ExitScope(); | 229 if (!Dart_HasLivePorts()) { |
| 210 Dart_ExitIsolate(); | 230 OS::Print("<< Shutting down isolate(%p)\n", isolate()); |
| 231 event_queue->RemoveEventsForIsolate(isolate()); |
| 232 Dart_ShutdownIsolate(); |
| 233 } else { |
| 234 Dart_ExitScope(); |
| 235 Dart_ExitIsolate(); |
| 236 } |
| 237 ASSERT(Dart_CurrentIsolate() == NULL); |
| 211 } | 238 } |
| 212 | 239 |
| 213 | 240 |
| 214 // A simple event queue for our test. | 241 static void NotifyMessage(Dart_Isolate dest_isolate) { |
| 215 class EventQueue { | 242 OS::Print("-- Notify isolate(%p) of pending message --\n", dest_isolate); |
| 216 public: | |
| 217 EventQueue() { | |
| 218 head_ = NULL; | |
| 219 } | |
| 220 | |
| 221 void Add(Event* event) { | |
| 222 if (head_ == NULL) { | |
| 223 head_ = event; | |
| 224 tail_ = event; | |
| 225 } else { | |
| 226 tail_->next_ = event; | |
| 227 tail_ = event; | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 Event* Get() { | |
| 232 if (head_ == NULL) { | |
| 233 return NULL; | |
| 234 } | |
| 235 Event* tmp = head_; | |
| 236 head_ = head_->next_; | |
| 237 if (head_ == NULL) { | |
| 238 tail_ = NULL; | |
| 239 } | |
| 240 | |
| 241 return tmp; | |
| 242 } | |
| 243 | |
| 244 void ClosePort(Dart_Isolate isolate, Dart_Port port) { | |
| 245 Event* cur = head_; | |
| 246 Event* prev = NULL; | |
| 247 while (cur != NULL) { | |
| 248 Event* next = cur->next_; | |
| 249 if (cur->IsMessageEvent(isolate, port)) { | |
| 250 // Remove matching event. | |
| 251 if (prev != NULL) { | |
| 252 prev->next_ = next; | |
| 253 } else { | |
| 254 head_ = next; | |
| 255 } | |
| 256 delete cur; | |
| 257 } else { | |
| 258 // Advance. | |
| 259 prev = cur; | |
| 260 } | |
| 261 cur = next; | |
| 262 } | |
| 263 tail_ = prev; | |
| 264 } | |
| 265 | |
| 266 private: | |
| 267 Event* head_; | |
| 268 Event* tail_; | |
| 269 }; | |
| 270 EventQueue* event_queue; | |
| 271 Event* current_event; | |
| 272 | |
| 273 static bool PostMessage(Dart_Isolate dest_isolate, | |
| 274 Dart_Port dest_port, | |
| 275 Dart_Port reply_port, | |
| 276 Dart_Message message) { | |
| 277 OS::Print("-- Posting message dest(%d) reply(%d) --\n", | |
| 278 dest_port, reply_port); | |
| 279 OS::Print("-- Adding MessageEvent to queue --\n"); | 243 OS::Print("-- Adding MessageEvent to queue --\n"); |
| 280 event_queue->Add( | 244 event_queue->Add(new MessageEvent(dest_isolate)); |
| 281 new MessageEvent(dest_isolate, dest_port, reply_port, message)); | |
| 282 return true; | |
| 283 } | |
| 284 | |
| 285 | |
| 286 static void ClosePort(Dart_Isolate isolate, | |
| 287 Dart_Port port) { | |
| 288 OS::Print("-- Closing port (%lld) for isolate(%p) --\n", | |
| 289 port, isolate); | |
| 290 | |
| 291 // Remove any pending events for the isolate/port. | |
| 292 event_queue->ClosePort(isolate, port); | |
| 293 | |
| 294 Dart_Isolate current = Dart_CurrentIsolate(); | |
| 295 if (current) { | |
| 296 Dart_ExitIsolate(); | |
| 297 } | |
| 298 Dart_EnterIsolate(isolate); | |
| 299 if (!Dart_HasLivePorts() && | |
| 300 (current_event == NULL || !current_event->IsShutdownEvent(isolate))) { | |
| 301 OS::Print("-- Adding ShutdownEvent to queue --\n"); | |
| 302 event_queue->Add(new ShutdownEvent(isolate)); | |
| 303 } | |
| 304 Dart_ExitIsolate(); | |
| 305 if (current) { | |
| 306 Dart_EnterIsolate(current); | |
| 307 } | |
| 308 } | 245 } |
| 309 | 246 |
| 310 | 247 |
| 311 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc) { | 248 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc) { |
| 312 const char* name_str = NULL; | 249 const char* name_str = NULL; |
| 313 EXPECT(Dart_IsString(name)); | 250 EXPECT(Dart_IsString(name)); |
| 314 EXPECT_VALID(Dart_StringToCString(name, &name_str)); | 251 EXPECT_VALID(Dart_StringToCString(name, &name_str)); |
| 315 if (strcmp(name_str, "native_echo") == 0) { | 252 if (strcmp(name_str, "native_echo") == 0) { |
| 316 return &native_echo; | 253 return &native_echo; |
| 317 } else if (strcmp(name_str, "CustomIsolateImpl_start") == 0) { | 254 } else if (strcmp(name_str, "CustomIsolateImpl_start") == 0) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 EXPECT_VALID(Dart_StringToCString(param, &isolate_main)); | 289 EXPECT_VALID(Dart_StringToCString(param, &isolate_main)); |
| 353 isolate_main = strdup(isolate_main); | 290 isolate_main = strdup(isolate_main); |
| 354 | 291 |
| 355 // Save current isolate. | 292 // Save current isolate. |
| 356 Dart_Isolate saved_isolate = Dart_CurrentIsolate(); | 293 Dart_Isolate saved_isolate = Dart_CurrentIsolate(); |
| 357 Dart_ExitIsolate(); | 294 Dart_ExitIsolate(); |
| 358 | 295 |
| 359 // Create a new Dart_Isolate. | 296 // Create a new Dart_Isolate. |
| 360 Dart_Isolate new_isolate = TestCase::CreateTestIsolate(); | 297 Dart_Isolate new_isolate = TestCase::CreateTestIsolate(); |
| 361 EXPECT(new_isolate != NULL); | 298 EXPECT(new_isolate != NULL); |
| 362 Dart_SetMessageCallbacks(&PostMessage, &ClosePort); | 299 Dart_SetMessageNotifyCallback(&NotifyMessage); |
| 363 Dart_Port new_port = Dart_GetMainPortId(); | 300 Dart_Port new_port = Dart_GetMainPortId(); |
| 364 | 301 |
| 365 OS::Print("-- Adding StartEvent to queue --\n"); | 302 OS::Print("-- Adding StartEvent to queue --\n"); |
| 366 event_queue->Add(new StartEvent(new_isolate, isolate_main)); | 303 event_queue->Add(new StartEvent(new_isolate, isolate_main)); |
| 367 | 304 |
| 368 // Restore the original isolate. | 305 // Restore the original isolate. |
| 369 Dart_ExitIsolate(); | 306 Dart_ExitIsolate(); |
| 370 Dart_EnterIsolate(saved_isolate); | 307 Dart_EnterIsolate(saved_isolate); |
| 371 Dart_EnterScope(); | 308 Dart_EnterScope(); |
| 372 | 309 |
| 373 Dart_Handle send_port = Dart_NewSendPort(new_port); | 310 Dart_Handle send_port = Dart_NewSendPort(new_port); |
| 374 EXPECT_VALID(send_port); | 311 EXPECT_VALID(send_port); |
| 375 Dart_SetReturnValue(args, send_port); | 312 Dart_SetReturnValue(args, send_port); |
| 376 | 313 |
| 377 OS::Print("-- Exit: CustomIsolateImpl_start --\n"); | 314 OS::Print("-- Exit: CustomIsolateImpl_start --\n"); |
| 378 Dart_ExitScope(); | 315 Dart_ExitScope(); |
| 379 } | 316 } |
| 380 | 317 |
| 381 | 318 |
| 382 UNIT_TEST_CASE(CustomIsolates) { | 319 UNIT_TEST_CASE(CustomIsolates) { |
| 383 event_queue = new EventQueue(); | 320 event_queue = new EventQueue(); |
| 384 current_event = NULL; | |
| 385 | 321 |
| 386 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate(); | 322 Dart_Isolate dart_isolate = TestCase::CreateTestIsolate(); |
| 387 EXPECT(dart_isolate != NULL); | 323 EXPECT(dart_isolate != NULL); |
| 388 Dart_SetMessageCallbacks(&PostMessage, &ClosePort); | 324 Dart_SetMessageNotifyCallback(&NotifyMessage); |
| 389 Dart_EnterScope(); | 325 Dart_EnterScope(); |
| 390 Dart_Handle result; | 326 Dart_Handle result; |
| 391 | 327 |
| 392 // Create a test library. | 328 // Create a test library. |
| 393 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, | 329 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, |
| 394 NativeLookup); | 330 NativeLookup); |
| 395 EXPECT_VALID(lib); | 331 EXPECT_VALID(lib); |
| 396 | 332 |
| 397 // Run main. | 333 // Run main. |
| 398 result = Dart_InvokeStatic(lib, | 334 result = Dart_InvokeStatic(lib, |
| 399 Dart_NewString(""), | 335 Dart_NewString(""), |
| 400 Dart_NewString("main"), | 336 Dart_NewString("main"), |
| 401 0, | 337 0, |
| 402 NULL); | 338 NULL); |
| 403 EXPECT_VALID(result); | 339 EXPECT_VALID(result); |
| 404 EXPECT(Dart_IsString(result)); | 340 EXPECT(Dart_IsString(result)); |
| 405 const char* result_str = NULL; | 341 const char* result_str = NULL; |
| 406 EXPECT_VALID(Dart_StringToCString(result, &result_str)); | 342 EXPECT_VALID(Dart_StringToCString(result, &result_str)); |
| 407 EXPECT_STREQ("success", result_str); | 343 EXPECT_STREQ("success", result_str); |
| 408 | 344 |
| 409 Dart_ExitScope(); | 345 Dart_ExitScope(); |
| 410 Dart_ExitIsolate(); | 346 Dart_ExitIsolate(); |
| 411 | 347 |
| 412 OS::Print("-- Starting event loop --\n"); | 348 OS::Print("-- Starting event loop --\n"); |
| 413 Event* event = event_queue->Get(); | 349 Event* event = event_queue->Get(); |
| 414 while (event) { | 350 while (event) { |
| 415 current_event = event; | |
| 416 event->Process(); | 351 event->Process(); |
| 417 current_event = NULL; | |
| 418 delete event; | 352 delete event; |
| 419 event = event_queue->Get(); | 353 event = event_queue->Get(); |
| 420 } | 354 } |
| 421 OS::Print("-- Finished event loop --\n"); | 355 OS::Print("-- Finished event loop --\n"); |
| 422 EXPECT_STREQ("Received: 43", saved_echo); | 356 EXPECT_STREQ("Received: 43", saved_echo); |
| 423 free(const_cast<char*>(saved_echo)); | 357 free(const_cast<char*>(saved_echo)); |
| 424 | 358 |
| 425 delete event_queue; | 359 delete event_queue; |
| 426 } | 360 } |
| 427 | 361 |
| 428 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 362 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 429 | 363 |
| 430 } // namespace dart | 364 } // namespace dart |
| OLD | NEW |