| 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 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 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 |
| 32 visitFunction(Function func) { |
| 33 return [ 'funcref', |
| 34 _makeFunctionRef(func), visitSendPortSync(_sendPort()), null ]; |
| 35 } |
| 31 } | 36 } |
| 32 | 37 |
| 38 // Leaking implementation. Later will be backend specific and hopefully |
| 39 // not leaking (at least in most of the cases.) |
| 40 // TODO: provide better, backend specific implementation. |
| 41 class _FunctionRegistry { |
| 42 final ReceivePortSync _port; |
| 43 int _nextId; |
| 44 final Map<String, Function> _registry; |
| 45 |
| 46 _FunctionRegistry() : |
| 47 _port = new ReceivePortSync(), |
| 48 _nextId = 0, |
| 49 _registry = <Function>{} { |
| 50 _port.receive((msg) { |
| 51 final id = msg[0]; |
| 52 final args = msg[1]; |
| 53 final f = _registry[id]; |
| 54 switch (args.length) { |
| 55 case 0: return f(); |
| 56 case 1: return f(args[0]); |
| 57 case 2: return f(args[0], args[1]); |
| 58 case 3: return f(args[0], args[1], args[2]); |
| 59 case 4: return f(args[0], args[1], args[2], args[3]); |
| 60 default: throw 'Unsupported number of arguments.'; |
| 61 } |
| 62 }); |
| 63 } |
| 64 |
| 65 String _add(Function f) { |
| 66 final id = 'func-ref-${_nextId++}'; |
| 67 _registry[id] = f; |
| 68 return id; |
| 69 } |
| 70 |
| 71 get _sendPort() => _port.toSendPort(); |
| 72 } |
| 73 |
| 74 _FunctionRegistry __functionRegistry; |
| 75 get _functionRegistry() { |
| 76 if (__functionRegistry === null) __functionRegistry = new _FunctionRegistry(); |
| 77 return __functionRegistry; |
| 78 } |
| 79 |
| 80 _makeFunctionRef(f) => _functionRegistry._add(f); |
| 81 _sendPort() => _functionRegistry._sendPort; |
| 82 /// End of function serialization implementation. |
| 83 |
| 33 _deserialize(var message) { | 84 _deserialize(var message) { |
| 34 return new _JsDeserializer().deserialize(message); | 85 return new _JsDeserializer().deserialize(message); |
| 35 } | 86 } |
| 36 | 87 |
| 37 class _JsDeserializer extends _Deserializer { | 88 class _JsDeserializer extends _Deserializer { |
| 38 | 89 |
| 39 deserializeSendPort(List x) { | 90 deserializeSendPort(List x) { |
| 40 String tag = x[1]; | 91 String tag = x[1]; |
| 41 switch (tag) { | 92 switch (tag) { |
| 42 case 'nativejs': | 93 case 'nativejs': |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 return new _RemoteSendPortSync(isolateId, portId); | 237 return new _RemoteSendPortSync(isolateId, portId); |
| 187 } | 238 } |
| 188 } | 239 } |
| 189 } | 240 } |
| 190 | 241 |
| 191 void _dispatchEvent(String receiver, var message) { | 242 void _dispatchEvent(String receiver, var message) { |
| 192 var event = document.$dom_createEvent('TextEvent'); | 243 var event = document.$dom_createEvent('TextEvent'); |
| 193 event.initTextEvent(receiver, false, false, window, JSON.stringify(message)); | 244 event.initTextEvent(receiver, false, false, window, JSON.stringify(message)); |
| 194 window.$dom_dispatchEvent(event); | 245 window.$dom_dispatchEvent(event); |
| 195 } | 246 } |
| OLD | NEW |