| 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. |
| 11 // | 11 // |
| 12 // This mid-size test uses the Dart Embedding Api to create a custom | 12 // This mid-size test uses the Dart Embedding Api to create a custom |
| 13 // isolate abstraction. Instead of having a dedicated thread for each | 13 // isolate abstraction. Instead of having a dedicated thread for each |
| 14 // isolate, as is the case normally, this implementation shares a | 14 // isolate, as is the case normally, this implementation shares a |
| 15 // single thread among the isolates using an event queue. | 15 // single thread among the isolates using an event queue. |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 | 18 |
| 19 static void native_echo(Dart_NativeArguments args); | 19 static void native_echo(Dart_NativeArguments args); |
| 20 static void CustomIsolateImpl_start(Dart_NativeArguments args); | 20 static void CustomIsolateImpl_start(Dart_NativeArguments args); |
| 21 static Dart_NativeFunction NativeLookup(Dart_Handle name, | 21 static Dart_NativeFunction NativeLookup(Dart_Handle name, |
| 22 int argc, | 22 int argc, |
| 23 bool* auto_setup_scope); | 23 bool* auto_setup_scope); |
| 24 | 24 |
| 25 | 25 |
| 26 static const char* kCustomIsolateScriptChars = | 26 static const char* kCustomIsolateScriptChars = |
| 27 "import 'dart:isolate';\n" | 27 "import 'dart:isolate';\n" |
| 28 "\n" | 28 "\n" |
| 29 "RawReceivePort mainPort;\n" | 29 "final RawReceivePort mainPort = new RawReceivePort();\n" |
| 30 "\n" | 30 "\n" |
| 31 "echo(arg) native \"native_echo\";\n" | 31 "echo(arg) native \"native_echo\";\n" |
| 32 "\n" | 32 "\n" |
| 33 "class CustomIsolateImpl implements CustomIsolate {\n" | 33 "class CustomIsolateImpl implements CustomIsolate {\n" |
| 34 " CustomIsolateImpl(String entry) : _entry = entry{\n" | 34 " CustomIsolateImpl(String entry) : _entry = entry{\n" |
| 35 " echo('Constructing isolate');\n" | 35 " echo('Constructing isolate');\n" |
| 36 " }\n" | 36 " }\n" |
| 37 "\n" | 37 "\n" |
| 38 " SendPort spawn() {\n" | 38 " SendPort spawn() {\n" |
| 39 " return _start(_entry);\n" | 39 " return _start(_entry);\n" |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 const char* main_; | 163 const char* main_; |
| 164 }; | 164 }; |
| 165 | 165 |
| 166 | 166 |
| 167 void StartEvent::Process() { | 167 void StartEvent::Process() { |
| 168 OS::Print(">> StartEvent with isolate(%p)--\n", isolate()); | 168 OS::Print(">> StartEvent with isolate(%p)--\n", isolate()); |
| 169 Dart_EnterIsolate(isolate()); | 169 Dart_EnterIsolate(isolate()); |
| 170 Dart_EnterScope(); | 170 Dart_EnterScope(); |
| 171 Dart_Handle result; | 171 Dart_Handle result; |
| 172 | 172 |
| 173 // Reload all the test classes here. | 173 Dart_Handle lib = Dart_LookupLibrary(NewString(TestCase::url())); |
| 174 // | |
| 175 // TODO(turnidge): Use the create isolate callback instead? | |
| 176 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, | |
| 177 NativeLookup); | |
| 178 EXPECT_VALID(lib); | 174 EXPECT_VALID(lib); |
| 179 | 175 |
| 180 Dart_Handle recv_port = Dart_GetReceivePort(Dart_GetMainPortId()); | |
| 181 EXPECT_VALID(recv_port); | |
| 182 result = Dart_SetField(lib, NewString("mainPort"), recv_port); | |
| 183 EXPECT_VALID(result); | |
| 184 | |
| 185 result = Dart_Invoke(lib, NewString(main_), 0, NULL); | 176 result = Dart_Invoke(lib, NewString(main_), 0, NULL); |
| 186 EXPECT_VALID(result); | 177 EXPECT_VALID(result); |
| 187 free(const_cast<char*>(main_)); | 178 free(const_cast<char*>(main_)); |
| 188 main_ = NULL; | 179 main_ = NULL; |
| 189 | 180 |
| 190 Dart_ExitScope(); | 181 Dart_ExitScope(); |
| 191 Dart_ExitIsolate(); | 182 Dart_ExitIsolate(); |
| 192 } | 183 } |
| 193 | 184 |
| 194 | 185 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 isolate_main = strdup(isolate_main); | 271 isolate_main = strdup(isolate_main); |
| 281 | 272 |
| 282 // Save current isolate. | 273 // Save current isolate. |
| 283 Dart_Isolate saved_isolate = Dart_CurrentIsolate(); | 274 Dart_Isolate saved_isolate = Dart_CurrentIsolate(); |
| 284 Dart_ExitIsolate(); | 275 Dart_ExitIsolate(); |
| 285 | 276 |
| 286 // Create a new Dart_Isolate. | 277 // Create a new Dart_Isolate. |
| 287 Dart_Isolate new_isolate = TestCase::CreateTestIsolate(); | 278 Dart_Isolate new_isolate = TestCase::CreateTestIsolate(); |
| 288 EXPECT(new_isolate != NULL); | 279 EXPECT(new_isolate != NULL); |
| 289 Dart_SetMessageNotifyCallback(&NotifyMessage); | 280 Dart_SetMessageNotifyCallback(&NotifyMessage); |
| 290 Dart_Port new_port = Dart_GetMainPortId(); | 281 Dart_EnterScope(); |
| 282 // Reload all the test classes here. |
| 283 // |
| 284 // TODO(turnidge): Use the create isolate callback instead? |
| 285 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, |
| 286 NativeLookup); |
| 287 EXPECT_VALID(lib); |
| 288 |
| 289 Dart_Handle main_port = Dart_GetField(lib, NewString("mainPort")); |
| 290 EXPECT_VALID(main_port); |
| 291 Dart_Port main_port_id; |
| 292 Dart_Handle err = Dart_PortGetId(main_port, &main_port_id); |
| 293 EXPECT_VALID(err); |
| 291 | 294 |
| 292 OS::Print("-- Adding StartEvent to queue --\n"); | 295 OS::Print("-- Adding StartEvent to queue --\n"); |
| 293 event_queue->Add(new StartEvent(new_isolate, isolate_main)); | 296 event_queue->Add(new StartEvent(new_isolate, isolate_main)); |
| 294 | 297 |
| 295 // Restore the original isolate. | 298 // Restore the original isolate. |
| 299 Dart_ExitScope(); |
| 296 Dart_ExitIsolate(); | 300 Dart_ExitIsolate(); |
| 297 Dart_EnterIsolate(saved_isolate); | 301 Dart_EnterIsolate(saved_isolate); |
| 298 Dart_EnterScope(); | 302 Dart_EnterScope(); |
| 299 | 303 |
| 300 Dart_Handle send_port = Dart_NewSendPort(new_port); | 304 Dart_Handle send_port = Dart_NewSendPort(main_port_id); |
| 301 EXPECT_VALID(send_port); | 305 EXPECT_VALID(send_port); |
| 302 Dart_SetReturnValue(args, send_port); | 306 Dart_SetReturnValue(args, send_port); |
| 303 | 307 |
| 304 OS::Print("-- Exit: CustomIsolateImpl_start --\n"); | 308 OS::Print("-- Exit: CustomIsolateImpl_start --\n"); |
| 305 Dart_ExitScope(); | 309 Dart_ExitScope(); |
| 306 } | 310 } |
| 307 | 311 |
| 308 | 312 |
| 309 UNIT_TEST_CASE(CustomIsolates) { | 313 UNIT_TEST_CASE(CustomIsolates) { |
| 310 event_queue = new EventQueue(); | 314 event_queue = new EventQueue(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 339 event = event_queue->Get(); | 343 event = event_queue->Get(); |
| 340 } | 344 } |
| 341 OS::Print("-- Finished event loop --\n"); | 345 OS::Print("-- Finished event loop --\n"); |
| 342 EXPECT_STREQ("Received: 43", saved_echo); | 346 EXPECT_STREQ("Received: 43", saved_echo); |
| 343 free(const_cast<char*>(saved_echo)); | 347 free(const_cast<char*>(saved_echo)); |
| 344 | 348 |
| 345 delete event_queue; | 349 delete event_queue; |
| 346 } | 350 } |
| 347 | 351 |
| 348 } // namespace dart | 352 } // namespace dart |
| OLD | NEW |