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 "final RawReceivePort mainPort = new RawReceivePort();\n" | 29 "final RawReceivePort mainPort = new RawReceivePort();\n" |
| 30 "final SendPort mainSendPort = mainPort.sendPort;\n" |
30 "\n" | 31 "\n" |
31 "echo(arg) native \"native_echo\";\n" | 32 "echo(arg) native \"native_echo\";\n" |
32 "\n" | 33 "\n" |
33 "class CustomIsolateImpl implements CustomIsolate {\n" | 34 "class CustomIsolateImpl implements CustomIsolate {\n" |
34 " CustomIsolateImpl(String entry) : _entry = entry{\n" | 35 " CustomIsolateImpl(String entry) : _entry = entry{\n" |
35 " echo('Constructing isolate');\n" | 36 " echo('Constructing isolate');\n" |
36 " }\n" | 37 " }\n" |
37 "\n" | 38 "\n" |
38 " SendPort spawn() {\n" | 39 " SendPort spawn() {\n" |
39 " return _start(_entry);\n" | 40 " return _start(_entry);\n" |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 EXPECT(new_isolate != NULL); | 280 EXPECT(new_isolate != NULL); |
280 Dart_SetMessageNotifyCallback(&NotifyMessage); | 281 Dart_SetMessageNotifyCallback(&NotifyMessage); |
281 Dart_EnterScope(); | 282 Dart_EnterScope(); |
282 // Reload all the test classes here. | 283 // Reload all the test classes here. |
283 // | 284 // |
284 // TODO(turnidge): Use the create isolate callback instead? | 285 // TODO(turnidge): Use the create isolate callback instead? |
285 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, | 286 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, |
286 NativeLookup); | 287 NativeLookup); |
287 EXPECT_VALID(lib); | 288 EXPECT_VALID(lib); |
288 | 289 |
289 Dart_Handle main_port = Dart_GetField(lib, NewString("mainPort")); | 290 Dart_Handle main_send_port = Dart_GetField(lib, NewString("mainSendPort")); |
290 EXPECT_VALID(main_port); | 291 EXPECT_VALID(main_send_port); |
291 Dart_Port main_port_id; | 292 Dart_Port main_port_id; |
292 Dart_Handle err = Dart_ReceivePortGetId(main_port, &main_port_id); | 293 Dart_Handle err = Dart_SendPortGetId(main_send_port, &main_port_id); |
293 EXPECT_VALID(err); | 294 EXPECT_VALID(err); |
294 | 295 |
295 OS::Print("-- Adding StartEvent to queue --\n"); | 296 OS::Print("-- Adding StartEvent to queue --\n"); |
296 event_queue->Add(new StartEvent(new_isolate, isolate_main)); | 297 event_queue->Add(new StartEvent(new_isolate, isolate_main)); |
297 | 298 |
298 // Restore the original isolate. | 299 // Restore the original isolate. |
299 Dart_ExitScope(); | 300 Dart_ExitScope(); |
300 Dart_ExitIsolate(); | 301 Dart_ExitIsolate(); |
301 Dart_EnterIsolate(saved_isolate); | 302 Dart_EnterIsolate(saved_isolate); |
302 Dart_EnterScope(); | 303 Dart_EnterScope(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 event = event_queue->Get(); | 344 event = event_queue->Get(); |
344 } | 345 } |
345 OS::Print("-- Finished event loop --\n"); | 346 OS::Print("-- Finished event loop --\n"); |
346 EXPECT_STREQ("Received: 43", saved_echo); | 347 EXPECT_STREQ("Received: 43", saved_echo); |
347 free(const_cast<char*>(saved_echo)); | 348 free(const_cast<char*>(saved_echo)); |
348 | 349 |
349 delete event_queue; | 350 delete event_queue; |
350 } | 351 } |
351 | 352 |
352 } // namespace dart | 353 } // namespace dart |
OLD | NEW |