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 SendPortImpl implements SendPort { | |
6 | |
7 const SendPortImpl(this._workerId, this._isolateId, this._receivePortId); | |
8 | |
9 void send(var message, [SendPort replyTo = null]) { | |
10 // TODO(kasperl): get rid of _sendNow. | |
11 this._sendNow(message, replyTo); | |
12 } | |
13 | |
14 void _sendNow(var message, SendPort replyTo) native; | |
15 | |
16 ReceivePortSingleShotImpl call(var message) { | |
17 final result = new ReceivePortSingleShotImpl(); | |
18 this.send(message, result.toSendPort()); | |
19 return result; | |
20 } | |
21 | |
22 ReceivePortSingleShotImpl _callNow(var message) { | |
23 final result = new ReceivePortSingleShotImpl(); | |
24 this._sendNow(message, result.toSendPort()); | |
25 return result; | |
26 } | |
27 | |
28 bool operator==(var other) { | |
29 return (other is SendPortImpl) && | |
30 (_workerId == other._workerId) && | |
31 (_isolateId == other._isolateId) && | |
32 (_receivePortId == other._receivePortId); | |
33 } | |
34 | |
35 int hashCode() { | |
36 return (_workerId << 16) ^ (_isolateId << 8) ^ _receivePortId; | |
37 } | |
38 | |
39 final int _receivePortId; | |
40 final int _isolateId; | |
41 final int _workerId; | |
42 | |
43 static _create(int workerId, int isolateId, int receivePortId) native { | |
44 return new SendPortImpl(workerId, isolateId, receivePortId); | |
45 } | |
46 static _getReceivePortId(SendPortImpl port) native { | |
47 return port._receivePortId; | |
48 } | |
49 static _getIsolateId(SendPortImpl port) native { | |
50 return port._isolateId; | |
51 } | |
52 static _getWorkerId(SendPortImpl port) native { | |
53 return port._workerId; | |
54 } | |
55 } | |
56 | |
57 | |
58 class ReceivePortFactory { | |
59 | |
60 factory ReceivePort() { | |
61 return new ReceivePortImpl(); | |
62 } | |
63 | |
64 factory ReceivePort.singleShot() { | |
65 return new ReceivePortSingleShotImpl(); | |
66 } | |
67 | |
68 } | |
69 | |
70 | |
71 class ReceivePortImpl implements ReceivePort { | |
72 ReceivePortImpl() | |
73 : _id = _nextFreeId++ { | |
74 _register(_id); | |
75 } | |
76 | |
77 void receive(void onMessage(var message, SendPort replyTo)) { | |
78 _callback = onMessage; | |
79 } | |
80 | |
81 void close() { | |
82 _callback = null; | |
83 _unregister(_id); | |
84 } | |
85 | |
86 SendPort toSendPort() { | |
87 return _toNewSendPort(); | |
88 } | |
89 | |
90 /** | |
91 * Returns a fresh [SendPort]. The implementation is not allowed to cache | |
92 * existing ports. | |
93 */ | |
94 SendPort _toNewSendPort() { | |
95 return new SendPortImpl(_currentWorkerId(), _currentIsolateId(), _id); | |
96 } | |
97 | |
98 int _id; | |
99 Function _callback = null; | |
100 | |
101 static int _nextFreeId = 1; | |
102 | |
103 void _register(int id) native; | |
104 void _unregister(int id) native; | |
105 | |
106 static int _currentWorkerId() native; | |
107 static int _currentIsolateId() native; | |
108 | |
109 static void _invokeCallback(ReceivePortImpl port, message, replyTo) native { | |
110 if (port._callback !== null) (port._callback)(message, replyTo); | |
111 } | |
112 | |
113 static int _getId(ReceivePortImpl port) native { | |
114 return port._id; | |
115 } | |
116 static Function _getCallback(ReceivePortImpl port) native { | |
117 return port._callback; | |
118 } | |
119 } | |
120 | |
121 | |
122 class ReceivePortSingleShotImpl implements ReceivePort { | |
123 | |
124 ReceivePortSingleShotImpl() : _port = new ReceivePortImpl() { } | |
125 | |
126 void receive(void callback(var message, SendPort replyTo)) { | |
127 _port.receive((var message, SendPort replyTo) { | |
128 _port.close(); | |
129 callback(message, replyTo); | |
130 }); | |
131 } | |
132 | |
133 void close() { | |
134 _port.close(); | |
135 } | |
136 | |
137 SendPort toSendPort() { | |
138 return _toNewSendPort(); | |
139 } | |
140 | |
141 /** | |
142 * Returns a fresh [SendPort]. The implementation is not allowed to cache | |
143 * existing ports. | |
144 */ | |
145 SendPort _toNewSendPort() { | |
146 return _port._toNewSendPort(); | |
147 } | |
148 | |
149 final ReceivePortImpl _port; | |
150 | |
151 } | |
152 | |
153 final String _SPAWNED_SIGNAL = "spawned"; | |
154 | |
155 class IsolateNatives { | |
156 static Future<SendPort> spawn(Isolate isolate, bool isLight) { | |
157 Completer<SendPort> result = new Completer<SendPort>(); | |
158 ReceivePort port = new ReceivePort.singleShot(); | |
159 port.receive((msg, SendPort replyPort) { | |
160 assert(msg == _SPAWNED_SIGNAL); | |
161 result.complete(replyPort); | |
162 }); | |
163 _spawn(isolate, isLight, port.toSendPort()); | |
164 return result.future; | |
165 } | |
166 | |
167 static SendPort _spawn(Isolate isolate, bool light, SendPort port) native; | |
168 } | |
169 | |
170 | |
171 class _IsolateJsUtil { | |
172 static void _startIsolate(Isolate isolate, SendPort replyTo) native { | |
173 ReceivePort port = new ReceivePort(); | |
174 replyTo.send(_SPAWNED_SIGNAL, port.toSendPort()); | |
175 isolate._run(port); | |
176 } | |
177 | |
178 static SendPort _toSendPort(port) native { | |
179 return port.toSendPort(); | |
180 } | |
181 | |
182 static void _print(String msg) native { | |
183 print(msg); | |
184 } | |
185 | |
186 static _copyObject(obj) native { | |
187 return new Copier().traverse(obj); | |
188 } | |
189 | |
190 static _serializeObject(obj) native { | |
191 return new Serializer().traverse(obj); | |
192 } | |
193 | |
194 static _deserializeMessage(message) native { | |
195 return new Deserializer().deserialize(message); | |
196 } | |
197 } | |
OLD | NEW |