Chromium Code Reviews| 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, int argc); | 21 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc); |
| 22 | 22 |
| 23 | 23 |
| 24 static const char* kCustomIsolateScriptChars = | 24 static const char* kCustomIsolateScriptChars = |
| 25 "import 'dart:async';\n" | |
| 26 "import 'dart:isolate';\n" | 25 "import 'dart:isolate';\n" |
| 27 "\n" | 26 "\n" |
| 28 "ReceivePort mainPort;\n" | 27 "ReceivePort mainPort;\n" |
| 29 "\n" | 28 "\n" |
| 30 "echo(arg) native \"native_echo\";\n" | 29 "echo(arg) native \"native_echo\";\n" |
| 31 "\n" | 30 "\n" |
| 32 "class CustomIsolateImpl implements CustomIsolate {\n" | 31 "class CustomIsolateImpl implements CustomIsolate {\n" |
| 33 " CustomIsolateImpl(String entry) : _entry = entry{\n" | 32 " CustomIsolateImpl(String entry) : _entry = entry{\n" |
| 34 " echo('Constructing isolate');\n" | 33 " echo('Constructing isolate');\n" |
| 35 " }\n" | 34 " }\n" |
| 36 "\n" | 35 "\n" |
| 37 " Future<SendPort> spawn() {\n" | 36 " SendPort spawn() {\n" |
| 38 " Completer<SendPort> completer = new Completer<SendPort>();\n" | 37 " return _start(_entry);\n" |
| 39 " SendPort port = _start(_entry);\n" | |
| 40 " completer.complete(port);\n" | |
| 41 " return completer.future;\n" | |
| 42 " }\n" | 38 " }\n" |
| 43 "\n" | 39 "\n" |
| 44 " static SendPort _start(entry)\n" | 40 " static SendPort _start(entry)\n" |
| 45 " native \"CustomIsolateImpl_start\";\n" | 41 " native \"CustomIsolateImpl_start\";\n" |
| 46 "\n" | 42 "\n" |
| 47 " String _entry;\n" | 43 " String _entry;\n" |
| 48 "}\n" | 44 "}\n" |
| 49 "\n" | 45 "\n" |
| 50 "abstract class CustomIsolate {\n" | 46 "abstract class CustomIsolate {\n" |
| 51 " factory CustomIsolate(String entry) = CustomIsolateImpl;\n" | 47 " factory CustomIsolate(String entry) = CustomIsolateImpl;\n" |
| 52 "\n" | 48 "\n" |
| 53 " Future<SendPort> spawn();\n" | 49 " SendPort spawn();\n" |
| 54 "}\n" | 50 "}\n" |
| 55 "\n" | 51 "\n" |
| 56 "isolateMain() {\n" | 52 "isolateMain() {\n" |
| 57 " echo('Running isolateMain');\n" | 53 " echo('Running isolateMain');\n" |
| 58 " mainPort.receive((message, SendPort replyTo) {\n" | 54 " mainPort.receive((message, SendPort replyTo) {\n" |
| 59 " echo('Received: $message');\n" | 55 " echo('Received: $message');\n" |
| 60 " replyTo.send((message + 1), null);\n" | 56 " replyTo.send((message + 1), null);\n" |
| 61 " });\n" | 57 " });\n" |
| 62 "}\n" | 58 "}\n" |
| 63 "\n" | 59 "\n" |
| 64 "main() {\n" | 60 "main() {\n" |
| 65 " var isolate = new CustomIsolate(\"isolateMain\");\n" | 61 " var isolate = new CustomIsolate(\"isolateMain\");\n" |
| 66 " isolate.spawn().then((SendPort port) {\n" | 62 " var receivePort = new ReceivePort();\n" |
| 67 " port.call(42).then((message) {\n" | 63 " SendPort port = isolate.spawn();\n" |
| 68 " echo('Received: $message');\n" | 64 " port.send(42, receivePort.toSendPort());\n" |
| 69 " });\n" | 65 " receivePort.receive((message, _) {\n" |
| 66 " receivePort.close();\n" | |
| 67 " echo('Received: $message');\n" | |
| 70 " });\n" | 68 " });\n" |
| 71 " return 'success';\n" | 69 " return 'success';\n" |
| 72 "}\n"; | 70 "}\n"; |
| 73 | 71 |
| 74 | 72 |
| 75 // An entry in our event queue. | 73 // An entry in our event queue. |
| 76 class Event { | 74 class Event { |
| 77 protected: | 75 protected: |
| 78 explicit Event(Dart_Isolate isolate) : isolate_(isolate), next_(NULL) {} | 76 explicit Event(Dart_Isolate isolate) : isolate_(isolate), next_(NULL) {} |
| 79 | 77 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 Dart_EnterIsolate(isolate()); | 165 Dart_EnterIsolate(isolate()); |
| 168 Dart_EnterScope(); | 166 Dart_EnterScope(); |
| 169 Dart_Handle result; | 167 Dart_Handle result; |
| 170 | 168 |
| 171 // Reload all the test classes here. | 169 // Reload all the test classes here. |
| 172 // | 170 // |
| 173 // TODO(turnidge): Use the create isolate callback instead? | 171 // TODO(turnidge): Use the create isolate callback instead? |
| 174 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, | 172 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, |
| 175 NativeLookup); | 173 NativeLookup); |
| 176 EXPECT_VALID(lib); | 174 EXPECT_VALID(lib); |
| 177 EXPECT_VALID(Dart_CompileAll()); | |
|
Ivan Posva
2013/09/25 20:23:46
Why did you remove the Dart_CompileAll call?
We d
Anders Johnsen
2013/09/26 14:19:33
I removed it because it complained about the nativ
| |
| 178 | 175 |
| 179 Dart_Handle recv_port = Dart_GetReceivePort(Dart_GetMainPortId()); | 176 Dart_Handle recv_port = Dart_GetReceivePort(Dart_GetMainPortId()); |
| 180 EXPECT_VALID(recv_port); | 177 EXPECT_VALID(recv_port); |
| 181 result = Dart_SetField(lib, NewString("mainPort"), recv_port); | 178 result = Dart_SetField(lib, NewString("mainPort"), recv_port); |
| 182 EXPECT_VALID(result); | 179 EXPECT_VALID(result); |
| 183 | 180 |
| 184 result = Dart_Invoke(lib, NewString(main_), 0, NULL); | 181 result = Dart_Invoke(lib, NewString(main_), 0, NULL); |
| 185 EXPECT_VALID(result); | 182 EXPECT_VALID(result); |
| 186 free(const_cast<char*>(main_)); | 183 free(const_cast<char*>(main_)); |
| 187 main_ = NULL; | 184 main_ = NULL; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 event = event_queue->Get(); | 331 event = event_queue->Get(); |
| 335 } | 332 } |
| 336 OS::Print("-- Finished event loop --\n"); | 333 OS::Print("-- Finished event loop --\n"); |
| 337 EXPECT_STREQ("Received: 43", saved_echo); | 334 EXPECT_STREQ("Received: 43", saved_echo); |
| 338 free(const_cast<char*>(saved_echo)); | 335 free(const_cast<char*>(saved_echo)); |
| 339 | 336 |
| 340 delete event_queue; | 337 delete event_queue; |
| 341 } | 338 } |
| 342 | 339 |
| 343 } // namespace dart | 340 } // namespace dart |
| OLD | NEW |