OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 class ReceivePortFactory { | |
6 factory ReceivePort() { | |
7 return new ReceivePortImpl(); | |
8 } | |
9 | |
10 factory ReceivePort.singleShot() { | |
11 return new ReceivePortSingleShotImpl(); | |
12 } | |
13 } | |
14 | |
15 | |
16 class ReceivePortImpl implements ReceivePort { | |
17 /*--- public interface ---*/ | |
18 factory ReceivePortImpl() native "ReceivePortImpl_factory"; | |
19 | |
20 receive(void onMessage(var message, SendPort replyTo)) { | |
21 _onMessage = onMessage; | |
22 } | |
23 | |
24 close() { | |
25 _portMap.remove(_id); | |
26 _closeInternal(_id); | |
27 } | |
28 | |
29 SendPort toSendPort() { | |
30 return new SendPortImpl(_id); | |
31 } | |
32 | |
33 /**** Internal implementation details ****/ | |
34 // Called from the VM to create a new ReceivePort instance. | |
35 static ReceivePortImpl _get_or_create(int id) { | |
36 if (_portMap !== null) { | |
37 ReceivePortImpl port = _portMap[id]; | |
38 if (port !== null) { | |
39 return port; | |
40 } | |
41 } | |
42 return new ReceivePortImpl._internal(id); | |
43 } | |
44 ReceivePortImpl._internal(int id) : _id = id { | |
45 if (_portMap === null) { | |
46 _portMap = new Map(); | |
47 } | |
48 _portMap[id] = this; | |
49 } | |
50 | |
51 // Called from the VM to dispatch to the handler. | |
52 static void _handleMessage(int id, int replyId, var message) { | |
53 assert(_portMap !== null); | |
54 ReceivePort port = _portMap[id]; | |
55 SendPort replyTo = (replyId == 0) ? null : new SendPortImpl(replyId); | |
56 (port._onMessage)(message, replyTo); | |
57 } | |
58 | |
59 // Call into the VM to close the VM maintained mappings. | |
60 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; | |
61 | |
62 final int _id; | |
63 var _onMessage; | |
64 | |
65 // id to ReceivePort mapping. | |
66 static Map _portMap; | |
67 } | |
68 | |
69 | |
70 class ReceivePortSingleShotImpl implements ReceivePort { | |
71 | |
72 ReceivePortSingleShotImpl() : _port = new ReceivePortImpl() { } | |
73 | |
74 void receive(void callback(var message, SendPort replyTo)) { | |
75 _port.receive((var message, SendPort replyTo) { | |
76 _port.close(); | |
77 callback(message, replyTo); | |
78 }); | |
79 } | |
80 | |
81 void close() { | |
82 _port.close(); | |
83 } | |
84 | |
85 SendPort toSendPort() { | |
86 return _port.toSendPort(); | |
87 } | |
88 | |
89 final ReceivePortImpl _port; | |
90 | |
91 } | |
92 | |
93 | |
94 class SendPortImpl implements SendPort { | |
95 /*--- public interface ---*/ | |
96 void send(var message, [SendPort replyTo = null]) { | |
97 this._sendNow(message, replyTo); | |
98 } | |
99 | |
100 void _sendNow(var message, SendPort replyTo) { | |
101 int replyId = (replyTo === null) ? 0 : replyTo._id; | |
102 _sendInternal(_id, replyId, message); | |
103 } | |
104 | |
105 ReceivePortSingleShotImpl call(var message) { | |
106 final result = new ReceivePortSingleShotImpl(); | |
107 this.send(message, result.toSendPort()); | |
108 return result; | |
109 } | |
110 | |
111 ReceivePortSingleShotImpl _callNow(var message) { | |
112 final result = new ReceivePortSingleShotImpl(); | |
113 this._sendNow(message, result.toSendPort()); | |
114 return result; | |
115 } | |
116 | |
117 bool operator==(var other) { | |
118 return (other is SendPortImpl) && _id == other._id; | |
119 } | |
120 | |
121 int hashCode() { | |
122 return _id; | |
123 } | |
124 | |
125 /*--- private implementation ---*/ | |
126 const SendPortImpl(int id) : _id = id; | |
127 | |
128 // SendPortImpl._create is called from the VM when a new SendPort instance is | |
129 // needed by the VM code. | |
130 static SendPort _create(int id) { | |
131 return new SendPortImpl(id); | |
132 } | |
133 | |
134 // Forward the implementation of sending messages to the VM. Only port ids | |
135 // are being handed to the VM. | |
136 static _sendInternal(int sendId, int replyId, var message) | |
137 native "SendPortImpl_sendInternal_"; | |
138 | |
139 final int _id; | |
140 } | |
141 | |
142 | |
143 class IsolateNatives { | |
144 static Future<SendPort> spawn(Isolate isolate, bool isLight) { | |
145 Completer<SendPort> completer = new Completer<SendPort>(); | |
146 SendPort port = _start(isolate, isLight); | |
147 completer.complete(port); | |
148 return completer.future; | |
149 } | |
150 | |
151 // Starts a new isolate calling the run method on a new instance of the | |
152 // remote class's type. | |
153 // Returns the send port which is passed to the newly created isolate. | |
154 // This method is being dispatched to from the public core library code. | |
155 static SendPort _start(Isolate isolate, bool light) | |
156 native "IsolateNatives_start"; | |
157 } | |
OLD | NEW |