| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 part of dart._isolate_helper; | 5 part of dart._isolate_helper; |
| 6 | 6 |
| 7 /// Serialize [message]. | 7 /// Serialize [message]. |
| 8 _serializeMessage(message) { | 8 _serializeMessage(message) { |
| 9 return new _Serializer().serialize(message); | 9 return new _Serializer().serialize(message); |
| 10 } | 10 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 109 |
| 110 serializeArrayInPlace(JSArray x) { | 110 serializeArrayInPlace(JSArray x) { |
| 111 for (int i = 0; i < x.length; i++) { | 111 for (int i = 0; i < x.length; i++) { |
| 112 x[i] = serialize(x[i]); | 112 x[i] = serialize(x[i]); |
| 113 } | 113 } |
| 114 return x; | 114 return x; |
| 115 } | 115 } |
| 116 | 116 |
| 117 serializeMap(InternalMap x) { | 117 serializeMap(InternalMap x) { |
| 118 Function serializeTearOff = serialize; | 118 Function serializeTearOff = serialize; |
| 119 return ['map', | 119 return [ |
| 120 x.keys.map(serializeTearOff).toList(), | 120 'map', |
| 121 x.values.map(serializeTearOff).toList()]; | 121 x.keys.map(serializeTearOff).toList(), |
| 122 x.values.map(serializeTearOff).toList() |
| 123 ]; |
| 122 } | 124 } |
| 123 | 125 |
| 124 serializeJSObject(JSObject x) { | 126 serializeJSObject(JSObject x) { |
| 125 // Don't serialize objects if their `constructor` property isn't `Object` | 127 // Don't serialize objects if their `constructor` property isn't `Object` |
| 126 // or undefined/null. | 128 // or undefined/null. |
| 127 // A different constructor is taken as a sign that the object has complex | 129 // A different constructor is taken as a sign that the object has complex |
| 128 // internal state, or that it is a function, and won't be serialized. | 130 // internal state, or that it is a function, and won't be serialized. |
| 129 if (JS('bool', '!!(#.constructor)', x) && | 131 if (JS('bool', '!!(#.constructor)', x) && |
| 130 JS('bool', 'x.constructor !== Object')) { | 132 JS('bool', 'x.constructor !== Object')) { |
| 131 unsupported(x, "Only plain JS Objects are supported:"); | 133 unsupported(x, "Only plain JS Objects are supported:"); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 | 186 |
| 185 _Deserializer({adjustSendPorts: true}) : _adjustSendPorts = adjustSendPorts; | 187 _Deserializer({adjustSendPorts: true}) : _adjustSendPorts = adjustSendPorts; |
| 186 | 188 |
| 187 /// Returns a message that can be transmitted through web-worker channels. | 189 /// Returns a message that can be transmitted through web-worker channels. |
| 188 deserialize(x) { | 190 deserialize(x) { |
| 189 if (isPrimitive(x)) return deserializePrimitive(x); | 191 if (isPrimitive(x)) return deserializePrimitive(x); |
| 190 | 192 |
| 191 if (x is! JSArray) throw new ArgumentError("Bad serialized message: $x"); | 193 if (x is! JSArray) throw new ArgumentError("Bad serialized message: $x"); |
| 192 | 194 |
| 193 switch (x.first) { | 195 switch (x.first) { |
| 194 case "ref": return deserializeRef(x); | 196 case "ref": |
| 195 case "buffer": return deserializeByteBuffer(x); | 197 return deserializeRef(x); |
| 196 case "typed": return deserializeTypedData(x); | 198 case "buffer": |
| 197 case "fixed": return deserializeFixed(x); | 199 return deserializeByteBuffer(x); |
| 198 case "extendable": return deserializeExtendable(x); | 200 case "typed": |
| 199 case "mutable": return deserializeMutable(x); | 201 return deserializeTypedData(x); |
| 200 case "const": return deserializeConst(x); | 202 case "fixed": |
| 201 case "map": return deserializeMap(x); | 203 return deserializeFixed(x); |
| 202 case "sendport": return deserializeSendPort(x); | 204 case "extendable": |
| 203 case "raw sendport": return deserializeRawSendPort(x); | 205 return deserializeExtendable(x); |
| 204 case "js-object": return deserializeJSObject(x); | 206 case "mutable": |
| 205 case "function": return deserializeClosure(x); | 207 return deserializeMutable(x); |
| 206 case "dart": return deserializeDartObject(x); | 208 case "const": |
| 207 default: throw "couldn't deserialize: $x"; | 209 return deserializeConst(x); |
| 210 case "map": |
| 211 return deserializeMap(x); |
| 212 case "sendport": |
| 213 return deserializeSendPort(x); |
| 214 case "raw sendport": |
| 215 return deserializeRawSendPort(x); |
| 216 case "js-object": |
| 217 return deserializeJSObject(x); |
| 218 case "function": |
| 219 return deserializeClosure(x); |
| 220 case "dart": |
| 221 return deserializeDartObject(x); |
| 222 default: |
| 223 throw "couldn't deserialize: $x"; |
| 208 } | 224 } |
| 209 } | 225 } |
| 210 | 226 |
| 211 bool isPrimitive(x) => x == null || x is String || x is num || x is bool; | 227 bool isPrimitive(x) => x == null || x is String || x is num || x is bool; |
| 212 deserializePrimitive(x) => x; | 228 deserializePrimitive(x) => x; |
| 213 | 229 |
| 214 // ['ref', id]. | 230 // ['ref', id]. |
| 215 deserializeRef(x) { | 231 deserializeRef(x) { |
| 216 assert(x[0] == 'ref'); | 232 assert(x[0] == 'ref'); |
| 217 int serializationId = x[1]; | 233 int serializationId = x[1]; |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 deserializeDartObject(x) { | 364 deserializeDartObject(x) { |
| 349 assert(x[0] == 'dart'); | 365 assert(x[0] == 'dart'); |
| 350 String classId = x[1]; | 366 String classId = x[1]; |
| 351 List fields = x[2]; | 367 List fields = x[2]; |
| 352 var instanceFromClassId = JS_EMBEDDED_GLOBAL('', INSTANCE_FROM_CLASS_ID); | 368 var instanceFromClassId = JS_EMBEDDED_GLOBAL('', INSTANCE_FROM_CLASS_ID); |
| 353 var initializeObject = JS_EMBEDDED_GLOBAL('', INITIALIZE_EMPTY_INSTANCE); | 369 var initializeObject = JS_EMBEDDED_GLOBAL('', INITIALIZE_EMPTY_INSTANCE); |
| 354 | 370 |
| 355 var emptyInstance = JS('', '#(#)', instanceFromClassId, classId); | 371 var emptyInstance = JS('', '#(#)', instanceFromClassId, classId); |
| 356 deserializedObjects.add(emptyInstance); | 372 deserializedObjects.add(emptyInstance); |
| 357 deserializeArrayInPlace(fields); | 373 deserializeArrayInPlace(fields); |
| 358 return JS('', '#(#, #, #)', | 374 return JS( |
| 359 initializeObject, classId, emptyInstance, fields); | 375 '', '#(#, #, #)', initializeObject, classId, emptyInstance, fields); |
| 360 } | 376 } |
| 361 } | 377 } |
| OLD | NEW |