OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 _serialize(var message) { | 5 _serialize(var message) { |
6 return new _JsSerializer().traverse(message); | 6 return new _JsSerializer().traverse(message); |
7 } | 7 } |
8 | 8 |
9 class _JsSerializer extends _Serializer { | 9 class _JsSerializer extends _Serializer { |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... | |
22 visitLocalSendPortSync(_LocalSendPortSync x) { | 22 visitLocalSendPortSync(_LocalSendPortSync x) { |
23 return [ 'sendport', 'dart', | 23 return [ 'sendport', 'dart', |
24 ReceivePortSync._isolateId, x._receivePort._portId ]; | 24 ReceivePortSync._isolateId, x._receivePort._portId ]; |
25 } | 25 } |
26 | 26 |
27 visitRemoteSendPortSync(_RemoteSendPortSync x) { | 27 visitRemoteSendPortSync(_RemoteSendPortSync x) { |
28 return [ 'sendport', 'dart', | 28 return [ 'sendport', 'dart', |
29 x._receivePort._isolateId, x._receivePort._portId ]; | 29 x._receivePort._isolateId, x._receivePort._portId ]; |
30 } | 30 } |
31 | 31 |
32 visitObject(Object x) { | |
33 if (x is Function) return visitFunction(x); | |
34 // TODO: Handle DOM elements and proxy other objects. | |
35 throw "Unserializable object $x"; | |
36 } | |
37 | |
32 visitFunction(Function func) { | 38 visitFunction(Function func) { |
33 return [ 'funcref', | 39 return [ 'funcref', |
34 _makeFunctionRef(func), visitSendPortSync(_sendPort()), null ]; | 40 _makeFunctionRef(func), visitSendPortSync(_sendPort()), null ]; |
35 } | 41 } |
36 } | 42 } |
37 | 43 |
38 // Leaking implementation. Later will be backend specific and hopefully | 44 // Leaking implementation. Later will be backend specific and hopefully |
39 // not leaking (at least in most of the cases.) | 45 // not leaking (at least in most of the cases.) |
40 // TODO: provide better, backend specific implementation. | 46 // TODO: provide better, backend specific implementation. |
41 class _FunctionRegistry { | 47 class _FunctionRegistry { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 _makeFunctionRef(f) => _functionRegistry._add(f); | 86 _makeFunctionRef(f) => _functionRegistry._add(f); |
81 _sendPort() => _functionRegistry._sendPort; | 87 _sendPort() => _functionRegistry._sendPort; |
82 /// End of function serialization implementation. | 88 /// End of function serialization implementation. |
83 | 89 |
84 _deserialize(var message) { | 90 _deserialize(var message) { |
85 return new _JsDeserializer().deserialize(message); | 91 return new _JsDeserializer().deserialize(message); |
86 } | 92 } |
87 | 93 |
88 class _JsDeserializer extends _Deserializer { | 94 class _JsDeserializer extends _Deserializer { |
89 | 95 |
96 static final _UNSPECIFIED = const Object(); | |
97 | |
90 deserializeSendPort(List x) { | 98 deserializeSendPort(List x) { |
91 String tag = x[1]; | 99 String tag = x[1]; |
92 switch (tag) { | 100 switch (tag) { |
93 case 'nativejs': | 101 case 'nativejs': |
94 num id = x[2]; | 102 num id = x[2]; |
95 return new _JsSendPortSync(id); | 103 return new _JsSendPortSync(id); |
96 case 'dart': | 104 case 'dart': |
97 num isolateId = x[2]; | 105 num isolateId = x[2]; |
98 num portId = x[3]; | 106 num portId = x[3]; |
99 return ReceivePortSync._lookup(isolateId, portId); | 107 return ReceivePortSync._lookup(isolateId, portId); |
100 default: | 108 default: |
101 throw 'Illegal SendPortSync type: $tag'; | 109 throw 'Illegal SendPortSync type: $tag'; |
102 } | 110 } |
103 } | 111 } |
104 | 112 |
113 deserializeObject(List x) { | |
114 String tag = x[0]; | |
115 switch (tag) { | |
116 case 'funcref': return deserializeFunction(x); | |
117 default: throw 'Illegal object type: $x'; | |
118 } | |
119 } | |
120 | |
121 deserializeFunction(List x) { | |
122 var id = x[1]; | |
123 SendPortSync port = deserializeSendPort(x[2]); | |
124 // TODO: Support varargs when there is support in the language. | |
125 var f = ([arg0 = _UNSPECIFIED, arg1 = _UNSPECIFIED, | |
Anton Muhin
2012/08/21 11:27:35
nit: I'd not introduce f local and would just retu
vsm
2012/08/21 15:59:26
Done.
| |
126 arg2 = _UNSPECIFIED, arg3 = _UNSPECIFIED]) { | |
127 var args = [arg0, arg1, arg2, arg3]; | |
128 var last = args.indexOf(_UNSPECIFIED); | |
129 if (last >= 0) args.removeRange(last, args.length - 1); | |
Anton Muhin
2012/08/21 11:27:35
getRange(0, last) is slightly more readable, but Y
vsm
2012/08/21 15:59:26
Done.
| |
130 var message = [id, args]; | |
131 return port.callSync(message); | |
132 }; | |
133 return f; | |
134 } | |
105 } | 135 } |
106 | 136 |
107 // The receiver is JS. | 137 // The receiver is JS. |
108 class _JsSendPortSync implements SendPortSync { | 138 class _JsSendPortSync implements SendPortSync { |
109 | 139 |
110 num _id; | 140 num _id; |
111 _JsSendPortSync(this._id); | 141 _JsSendPortSync(this._id); |
112 | 142 |
113 callSync(var message) { | 143 callSync(var message) { |
114 var serialized = _serialize(message); | 144 var serialized = _serialize(message); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 return new _RemoteSendPortSync(isolateId, portId); | 267 return new _RemoteSendPortSync(isolateId, portId); |
238 } | 268 } |
239 } | 269 } |
240 } | 270 } |
241 | 271 |
242 void _dispatchEvent(String receiver, var message) { | 272 void _dispatchEvent(String receiver, var message) { |
243 var event = document.$dom_createEvent('TextEvent'); | 273 var event = document.$dom_createEvent('TextEvent'); |
244 event.initTextEvent(receiver, false, false, window, JSON.stringify(message)); | 274 event.initTextEvent(receiver, false, false, window, JSON.stringify(message)); |
245 window.$dom_dispatchEvent(event); | 275 window.$dom_dispatchEvent(event); |
246 } | 276 } |
OLD | NEW |