| 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 // |
| 11 // This mid-size test uses the Dart Embedding Api to create a custom | 11 // This mid-size test uses the Dart Embedding Api to create a custom |
| 12 // isolate abstraction. Instead of having a dedicated thread for each | 12 // isolate abstraction. Instead of having a dedicated thread for each |
| 13 // isolate, as is the case normally, this implementation shares a | 13 // isolate, as is the case normally, this implementation shares a |
| 14 // single thread among the isolates using an event queue. | 14 // single thread among the isolates using an event queue. |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 | 17 |
| 18 // Only ia32 and x64 can run execution tests. | 18 // Only ia32 and x64 can run execution tests. |
| 19 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) | 19 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) |
| 20 | 20 |
| 21 static void native_echo(Dart_NativeArguments args); | 21 static void native_echo(Dart_NativeArguments args); |
| 22 static void CustomIsolateImpl_start(Dart_NativeArguments args); | 22 static void CustomIsolateImpl_start(Dart_NativeArguments args); |
| 23 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc); | 23 static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc); |
| 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 "class GlobalsHack {\n" | 29 "ReceivePort mainPort;\n" |
| 30 " static ReceivePort _receivePort;\n" | |
| 31 "}\n" | |
| 32 "\n" | |
| 33 "ReceivePort get receivePort() {\n" | |
| 34 " return GlobalsHack._receivePort;\n" | |
| 35 "}\n" | |
| 36 "\n" | 30 "\n" |
| 37 "echo(arg) native \"native_echo\";\n" | 31 "echo(arg) native \"native_echo\";\n" |
| 38 "\n" | 32 "\n" |
| 39 "class CustomIsolateImpl implements CustomIsolate {\n" | 33 "class CustomIsolateImpl implements CustomIsolate {\n" |
| 40 " CustomIsolateImpl(String entry) : _entry = entry{\n" | 34 " CustomIsolateImpl(String entry) : _entry = entry{\n" |
| 41 " echo('Constructing isolate');\n" | 35 " echo('Constructing isolate');\n" |
| 42 " }\n" | 36 " }\n" |
| 43 "\n" | 37 "\n" |
| 44 " Future<SendPort> spawn() {\n" | 38 " Future<SendPort> spawn() {\n" |
| 45 " Completer<SendPort> completer = new Completer<SendPort>();\n" | 39 " Completer<SendPort> completer = new Completer<SendPort>();\n" |
| 46 " SendPort port = _start(_entry);\n" | 40 " SendPort port = _start(_entry);\n" |
| 47 " completer.complete(port);\n" | 41 " completer.complete(port);\n" |
| 48 " return completer.future;\n" | 42 " return completer.future;\n" |
| 49 " }\n" | 43 " }\n" |
| 50 "\n" | 44 "\n" |
| 51 " static SendPort _start(entry)\n" | 45 " static SendPort _start(entry)\n" |
| 52 " native \"CustomIsolateImpl_start\";\n" | 46 " native \"CustomIsolateImpl_start\";\n" |
| 53 "\n" | 47 "\n" |
| 54 " String _entry;\n" | 48 " String _entry;\n" |
| 55 "}\n" | 49 "}\n" |
| 56 "\n" | 50 "\n" |
| 57 "interface CustomIsolate default CustomIsolateImpl {\n" | 51 "interface CustomIsolate default CustomIsolateImpl {\n" |
| 58 " CustomIsolate(String entry);\n" | 52 " CustomIsolate(String entry);\n" |
| 59 "\n" | 53 "\n" |
| 60 " Future<SendPort> spawn();\n" | 54 " Future<SendPort> spawn();\n" |
| 61 "}\n" | 55 "}\n" |
| 62 "\n" | 56 "\n" |
| 63 "isolateMain() {\n" | 57 "isolateMain() {\n" |
| 64 " echo('Running isolateMain');\n" | 58 " echo('Running isolateMain');\n" |
| 65 " receivePort.receive((message, SendPort replyTo) {\n" | 59 " mainPort.receive((message, SendPort replyTo) {\n" |
| 66 " echo('Received: ' + message);\n" | 60 " echo('Received: ' + message);\n" |
| 67 " replyTo.send((message + 1), null);\n" | 61 " replyTo.send((message + 1), null);\n" |
| 68 " });\n" | 62 " });\n" |
| 69 "}\n" | 63 "}\n" |
| 70 "\n" | 64 "\n" |
| 71 "main() {\n" | 65 "main() {\n" |
| 72 " var isolate = new CustomIsolate(\"isolateMain\");\n" | 66 " var isolate = new CustomIsolate(\"isolateMain\");\n" |
| 73 " isolate.spawn().then((SendPort port) {\n" | 67 " isolate.spawn().then((SendPort port) {\n" |
| 74 " port.call(42).receive((message, replyTo) {\n" | 68 " port.call(42).receive((message, replyTo) {\n" |
| 75 " echo('Received: ' + message);\n" | 69 " echo('Received: ' + message);\n" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 // Reload all the test classes here. | 172 // Reload all the test classes here. |
| 179 // | 173 // |
| 180 // TODO(turnidge): Use the create isolate callback instead? | 174 // TODO(turnidge): Use the create isolate callback instead? |
| 181 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, | 175 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, |
| 182 NativeLookup); | 176 NativeLookup); |
| 183 EXPECT_VALID(lib); | 177 EXPECT_VALID(lib); |
| 184 EXPECT_VALID(Dart_CompileAll()); | 178 EXPECT_VALID(Dart_CompileAll()); |
| 185 | 179 |
| 186 Dart_Handle recv_port = Dart_GetReceivePort(Dart_GetMainPortId()); | 180 Dart_Handle recv_port = Dart_GetReceivePort(Dart_GetMainPortId()); |
| 187 EXPECT_VALID(recv_port); | 181 EXPECT_VALID(recv_port); |
| 188 | 182 result = Dart_SetField(lib, Dart_NewString("mainPort"), recv_port); |
| 189 // TODO(turnidge): Provide a way to set a top-level variable from | |
| 190 // the dart embedding api. | |
| 191 Dart_Handle hidden = Dart_GetClass(lib, Dart_NewString("GlobalsHack")); | |
| 192 EXPECT_VALID(hidden); | |
| 193 result = Dart_SetStaticField(hidden, Dart_NewString("_receivePort"), | |
| 194 recv_port); | |
| 195 EXPECT_VALID(result); | 183 EXPECT_VALID(result); |
| 196 | 184 |
| 197 result = Dart_InvokeStatic(lib, | 185 result = Dart_InvokeStatic(lib, |
| 198 Dart_NewString(""), | 186 Dart_NewString(""), |
| 199 Dart_NewString(main_), | 187 Dart_NewString(main_), |
| 200 0, | 188 0, |
| 201 NULL); | 189 NULL); |
| 202 EXPECT_VALID(result); | 190 EXPECT_VALID(result); |
| 203 free(const_cast<char*>(main_)); | 191 free(const_cast<char*>(main_)); |
| 204 main_ = NULL; | 192 main_ = NULL; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 OS::Print("-- Finished event loop --\n"); | 345 OS::Print("-- Finished event loop --\n"); |
| 358 EXPECT_STREQ("Received: 43", saved_echo); | 346 EXPECT_STREQ("Received: 43", saved_echo); |
| 359 free(const_cast<char*>(saved_echo)); | 347 free(const_cast<char*>(saved_echo)); |
| 360 | 348 |
| 361 delete event_queue; | 349 delete event_queue; |
| 362 } | 350 } |
| 363 | 351 |
| 364 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 352 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 365 | 353 |
| 366 } // namespace dart | 354 } // namespace dart |
| OLD | NEW |