OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import "dart:isolate"; |
| 6 import "dart:async"; |
| 7 import "package:expect/expect.dart"; |
| 8 import "package:async_helper/async_helper.dart"; |
| 9 |
| 10 void toplevel(port, message) { port.send("toplevel:$message"); } |
| 11 Function createFuncToplevel() => (p, m) { p.send(m); }; |
| 12 class C { |
| 13 Function initializer; |
| 14 Function body; |
| 15 C() : initializer = ((p, m) { throw "initializer"; }) { |
| 16 body = (p, m) { throw "body"; }; |
| 17 } |
| 18 static void staticFunc(port, message) { port.send("static:$message"); } |
| 19 static Function createFuncStatic() => (p, m) { throw "static expr"; }; |
| 20 void instanceMethod(p, m) { throw "instanceMethod"; } |
| 21 Function createFuncMember() => (p, m) { throw "instance expr"; }; |
| 22 void call(n, p) { throw "C"; } |
| 23 } |
| 24 |
| 25 class Callable { |
| 26 void call(p, m) { p.send(["callable", m]); } |
| 27 } |
| 28 |
| 29 |
| 30 void main() { |
| 31 asyncStart(); |
| 32 |
| 33 // top-level functions, static functions, closures, instance methods |
| 34 // or function expressions are not sendable to an isolate spawned using |
| 35 // spawnUri. |
| 36 testUnsendable("toplevel", toplevel); |
| 37 testUnsendable("static", C.staticFunc); |
| 38 var c = new C(); |
| 39 testUnsendable("instance method", c.instanceMethod); |
| 40 testUnsendable("static context expression", createFuncToplevel()); |
| 41 testUnsendable("static context expression", C.createFuncStatic()); |
| 42 testUnsendable("initializer context expression", c.initializer); |
| 43 testUnsendable("constructor context expression", c.body); |
| 44 testUnsendable("instance method context expression", c.createFuncMember()); |
| 45 testUnsendable("toplevel", toplevel.call); |
| 46 testUnsendable("static", C.staticFunc.call); |
| 47 testUnsendable("callable object", new Callable().call); |
| 48 |
| 49 asyncEnd(); |
| 50 return; |
| 51 } |
| 52 |
| 53 // Create a receive port that expects exactly one message. |
| 54 // Pass the message to `callback` and return the sendPort. |
| 55 SendPort singleMessagePort(callback) { |
| 56 var p; |
| 57 p = new RawReceivePort((v) { p.close(); callback(v); }); |
| 58 return p.sendPort; |
| 59 } |
| 60 |
| 61 // A singleMessagePort that expects the message to be a specific value. |
| 62 SendPort expectMessagePort(message) { |
| 63 asyncStart(); |
| 64 return singleMessagePort((v) { |
| 65 Expect.equals(message, v); |
| 66 asyncEnd(); |
| 67 }); |
| 68 } |
| 69 |
| 70 // Creates a new isolate and a pair of ports that expect a single message |
| 71 // to be sent to the other isolate and back to the callback function. |
| 72 Future<SendPort> echoPort(callback(value)) { |
| 73 Completer completer = new Completer<SendPort>(); |
| 74 SendPort replyPort = singleMessagePort(callback); |
| 75 RawReceivePort initPort; |
| 76 initPort = new RawReceivePort((p) { |
| 77 completer.complete(p); |
| 78 initPort.close(); |
| 79 }); |
| 80 return Isolate.spawn(_echo, [replyPort, initPort.sendPort]) |
| 81 .then((isolate) => completer.future); |
| 82 } |
| 83 |
| 84 void _echo(msg) { |
| 85 var replyPort = msg[0]; |
| 86 RawReceivePort requestPort; |
| 87 requestPort = new RawReceivePort((msg) { |
| 88 replyPort.send(msg); |
| 89 requestPort.close(); // Single echo only. |
| 90 }); |
| 91 msg[1].send(requestPort.sendPort); |
| 92 } |
| 93 |
| 94 // Creates other isolate that waits for a single message, `msg`, on the returned |
| 95 // port, and executes it as `msg[0](msg[1],msg[2])` in the other isolate. |
| 96 Future<SendPort> callPort() { |
| 97 Completer completer = new Completer<SendPort>(); |
| 98 SendPort initPort = singleMessagePort(completer.complete); |
| 99 return Isolate.spawn(_call, initPort) |
| 100 .then((_) => completer.future); |
| 101 } |
| 102 |
| 103 void _call(initPort) { |
| 104 initPort.send(singleMessagePort(callFunc)); |
| 105 } |
| 106 |
| 107 void testUnsendable(name, func) { |
| 108 asyncStart(); |
| 109 Isolate.spawnUri(Uri.parse("function_send_test.dart"), [], func) |
| 110 .then((v) => throw "allowed spawn direct?", onError: (e,s){ asyncEnd(); }); |
| 111 asyncStart(); |
| 112 Isolate.spawnUri(Uri.parse("function_send_test.dart"), [], [func]) |
| 113 .then((v) => throw "allowed spawn wrapped?", onError: (e,s){ asyncEnd(); }); |
| 114 } |
| 115 |
| 116 void callFunc(message) { |
| 117 message[0](message[1], message[2]); |
| 118 } |
OLD | NEW |