Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(399)

Side by Side Diff: lib/html/src/Isolates.dart

Issue 10834426: Support general serializing of functions between JS and Dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « lib/html/dartium/html_dartium.dart ('k') | lib/isolate/serialization.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 return ([arg0 = _UNSPECIFIED, arg1 = _UNSPECIFIED,
126 arg2 = _UNSPECIFIED, arg3 = _UNSPECIFIED]) {
127 var args = [arg0, arg1, arg2, arg3];
128 var last = args.indexOf(_UNSPECIFIED);
129 if (last >= 0) args = args.getRange(0, last);
130 var message = [id, args];
131 return port.callSync(message);
132 };
133 }
105 } 134 }
106 135
107 // The receiver is JS. 136 // The receiver is JS.
108 class _JsSendPortSync implements SendPortSync { 137 class _JsSendPortSync implements SendPortSync {
109 138
110 num _id; 139 num _id;
111 _JsSendPortSync(this._id); 140 _JsSendPortSync(this._id);
112 141
113 callSync(var message) { 142 callSync(var message) {
114 var serialized = _serialize(message); 143 var serialized = _serialize(message);
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 return new _RemoteSendPortSync(isolateId, portId); 266 return new _RemoteSendPortSync(isolateId, portId);
238 } 267 }
239 } 268 }
240 } 269 }
241 270
242 void _dispatchEvent(String receiver, var message) { 271 void _dispatchEvent(String receiver, var message) {
243 var event = document.$dom_createEvent('TextEvent'); 272 var event = document.$dom_createEvent('TextEvent');
244 event.initTextEvent(receiver, false, false, window, JSON.stringify(message)); 273 event.initTextEvent(receiver, false, false, window, JSON.stringify(message));
245 window.$dom_dispatchEvent(event); 274 window.$dom_dispatchEvent(event);
246 } 275 }
OLDNEW
« no previous file with comments | « lib/html/dartium/html_dartium.dart ('k') | lib/isolate/serialization.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698