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

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

Issue 10878010: Restore js interop code (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Restore js function passing code 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/src/Isolates.dart ('k') | tests/html/html.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Patch file for the dart:isolate library.
6
7 /********************************************************
8 Inserted from lib/isolate/serialization.dart
9 ********************************************************/
10
11 class _MessageTraverserVisitedMap {
12
13 operator[](var object) => null;
14 void operator[]=(var object, var info) { }
15
16 void reset() { }
17 void cleanup() { }
18
19 }
20
21 /** Abstract visitor for dart objects that can be sent as isolate messages. */
22 class _MessageTraverser {
23
24 _MessageTraverserVisitedMap _visited;
25 _MessageTraverser() : _visited = new _MessageTraverserVisitedMap();
26
27 /** Visitor's entry point. */
28 traverse(var x) {
29 if (isPrimitive(x)) return visitPrimitive(x);
30 _visited.reset();
31 var result;
32 try {
33 result = _dispatch(x);
34 } finally {
35 _visited.cleanup();
36 }
37 return result;
38 }
39
40 _dispatch(var x) {
41 if (isPrimitive(x)) return visitPrimitive(x);
42 if (x is List) return visitList(x);
43 if (x is Map) return visitMap(x);
44 if (x is SendPort) return visitSendPort(x);
45 if (x is SendPortSync) return visitSendPortSync(x);
46
47 // Overridable fallback.
48 return visitObject(x);
49 }
50
51 abstract visitPrimitive(x);
52 abstract visitList(List x);
53 abstract visitMap(Map x);
54 abstract visitSendPort(SendPort x);
55 abstract visitSendPortSync(SendPortSync x);
56
57 visitObject(Object x) {
58 // TODO(floitsch): make this a real exception. (which one)?
59 throw "Message serialization: Illegal value $x passed";
60 }
61
62 static bool isPrimitive(x) {
63 return (x === null) || (x is String) || (x is num) || (x is bool);
64 }
65 }
66
67
68 /** A visitor that recursively copies a message. */
69 class _Copier extends _MessageTraverser {
70
71 visitPrimitive(x) => x;
72
73 List visitList(List list) {
74 List copy = _visited[list];
75 if (copy !== null) return copy;
76
77 int len = list.length;
78
79 // TODO(floitsch): we loose the generic type of the List.
80 copy = new List(len);
81 _visited[list] = copy;
82 for (int i = 0; i < len; i++) {
83 copy[i] = _dispatch(list[i]);
84 }
85 return copy;
86 }
87
88 Map visitMap(Map map) {
89 Map copy = _visited[map];
90 if (copy !== null) return copy;
91
92 // TODO(floitsch): we loose the generic type of the map.
93 copy = new Map();
94 _visited[map] = copy;
95 map.forEach((key, val) {
96 copy[_dispatch(key)] = _dispatch(val);
97 });
98 return copy;
99 }
100
101 }
102
103 /** Visitor that serializes a message as a JSON array. */
104 class _Serializer extends _MessageTraverser {
105 int _nextFreeRefId = 0;
106
107 visitPrimitive(x) => x;
108
109 visitList(List list) {
110 int copyId = _visited[list];
111 if (copyId !== null) return ['ref', copyId];
112
113 int id = _nextFreeRefId++;
114 _visited[list] = id;
115 var jsArray = _serializeList(list);
116 // TODO(floitsch): we are losing the generic type.
117 return ['list', id, jsArray];
118 }
119
120 visitMap(Map map) {
121 int copyId = _visited[map];
122 if (copyId !== null) return ['ref', copyId];
123
124 int id = _nextFreeRefId++;
125 _visited[map] = id;
126 var keys = _serializeList(map.getKeys());
127 var values = _serializeList(map.getValues());
128 // TODO(floitsch): we are losing the generic type.
129 return ['map', id, keys, values];
130 }
131
132 _serializeList(List list) {
133 int len = list.length;
134 var result = new List(len);
135 for (int i = 0; i < len; i++) {
136 result[i] = _dispatch(list[i]);
137 }
138 return result;
139 }
140 }
141
142 /** Deserializes arrays created with [_Serializer]. */
143 class _Deserializer {
144 Map<int, Dynamic> _deserialized;
145
146 _Deserializer();
147
148 static bool isPrimitive(x) {
149 return (x === null) || (x is String) || (x is num) || (x is bool);
150 }
151
152 deserialize(x) {
153 if (isPrimitive(x)) return x;
154 // TODO(floitsch): this should be new HashMap<int, var|Dynamic>()
155 _deserialized = new HashMap();
156 return _deserializeHelper(x);
157 }
158
159 _deserializeHelper(x) {
160 if (isPrimitive(x)) return x;
161 assert(x is List);
162 switch (x[0]) {
163 case 'ref': return _deserializeRef(x);
164 case 'list': return _deserializeList(x);
165 case 'map': return _deserializeMap(x);
166 case 'sendport': return deserializeSendPort(x);
167 default: return deserializeObject(x);
168 }
169 }
170
171 _deserializeRef(List x) {
172 int id = x[1];
173 var result = _deserialized[id];
174 assert(result !== null);
175 return result;
176 }
177
178 List _deserializeList(List x) {
179 int id = x[1];
180 // We rely on the fact that Dart-lists are directly mapped to Js-arrays.
181 List dartList = x[2];
182 _deserialized[id] = dartList;
183 int len = dartList.length;
184 for (int i = 0; i < len; i++) {
185 dartList[i] = _deserializeHelper(dartList[i]);
186 }
187 return dartList;
188 }
189
190 Map _deserializeMap(List x) {
191 Map result = new Map();
192 int id = x[1];
193 _deserialized[id] = result;
194 List keys = x[2];
195 List values = x[3];
196 int len = keys.length;
197 assert(len == values.length);
198 for (int i = 0; i < len; i++) {
199 var key = _deserializeHelper(keys[i]);
200 var value = _deserializeHelper(values[i]);
201 result[key] = value;
202 }
203 return result;
204 }
205
206 abstract deserializeSendPort(List x);
207
208 deserializeObject(List x) {
209 // TODO(floitsch): Use real exception (which one?).
210 throw "Unexpected serialized object";
211 }
212 }
213
OLDNEW
« no previous file with comments | « lib/html/src/Isolates.dart ('k') | tests/html/html.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698