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

Side by Side Diff: lib/isolate/serialization.dart

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

Powered by Google App Engine
This is Rietveld 408576698