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