| OLD | NEW |
| 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 // Patch file for the dart:isolate library. | 5 // Patch file for the dart:isolate library. |
| 6 | 6 |
| 7 /******************************************************** | 7 /******************************************************** |
| 8 Inserted from lib/isolate/serialization.dart | 8 Inserted from lib/isolate/serialization.dart |
| 9 ********************************************************/ | 9 ********************************************************/ |
| 10 | 10 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 abstract visitMap(Map x); | 53 abstract visitMap(Map x); |
| 54 abstract visitSendPort(SendPort x); | 54 abstract visitSendPort(SendPort x); |
| 55 abstract visitSendPortSync(SendPortSync x); | 55 abstract visitSendPortSync(SendPortSync x); |
| 56 | 56 |
| 57 visitObject(Object x) { | 57 visitObject(Object x) { |
| 58 // TODO(floitsch): make this a real exception. (which one)? | 58 // TODO(floitsch): make this a real exception. (which one)? |
| 59 throw "Message serialization: Illegal value $x passed"; | 59 throw "Message serialization: Illegal value $x passed"; |
| 60 } | 60 } |
| 61 | 61 |
| 62 static bool isPrimitive(x) { | 62 static bool isPrimitive(x) { |
| 63 return (x === null) || (x is String) || (x is num) || (x is bool); | 63 return (x == null) || (x is String) || (x is num) || (x is bool); |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 | 67 |
| 68 /** A visitor that recursively copies a message. */ | 68 /** A visitor that recursively copies a message. */ |
| 69 class _Copier extends _MessageTraverser { | 69 class _Copier extends _MessageTraverser { |
| 70 | 70 |
| 71 visitPrimitive(x) => x; | 71 visitPrimitive(x) => x; |
| 72 | 72 |
| 73 List visitList(List list) { | 73 List visitList(List list) { |
| 74 List copy = _visited[list]; | 74 List copy = _visited[list]; |
| 75 if (copy !== null) return copy; | 75 if (copy != null) return copy; |
| 76 | 76 |
| 77 int len = list.length; | 77 int len = list.length; |
| 78 | 78 |
| 79 // TODO(floitsch): we loose the generic type of the List. | 79 // TODO(floitsch): we loose the generic type of the List. |
| 80 copy = new List(len); | 80 copy = new List(len); |
| 81 _visited[list] = copy; | 81 _visited[list] = copy; |
| 82 for (int i = 0; i < len; i++) { | 82 for (int i = 0; i < len; i++) { |
| 83 copy[i] = _dispatch(list[i]); | 83 copy[i] = _dispatch(list[i]); |
| 84 } | 84 } |
| 85 return copy; | 85 return copy; |
| 86 } | 86 } |
| 87 | 87 |
| 88 Map visitMap(Map map) { | 88 Map visitMap(Map map) { |
| 89 Map copy = _visited[map]; | 89 Map copy = _visited[map]; |
| 90 if (copy !== null) return copy; | 90 if (copy != null) return copy; |
| 91 | 91 |
| 92 // TODO(floitsch): we loose the generic type of the map. | 92 // TODO(floitsch): we loose the generic type of the map. |
| 93 copy = new Map(); | 93 copy = new Map(); |
| 94 _visited[map] = copy; | 94 _visited[map] = copy; |
| 95 map.forEach((key, val) { | 95 map.forEach((key, val) { |
| 96 copy[_dispatch(key)] = _dispatch(val); | 96 copy[_dispatch(key)] = _dispatch(val); |
| 97 }); | 97 }); |
| 98 return copy; | 98 return copy; |
| 99 } | 99 } |
| 100 | 100 |
| 101 } | 101 } |
| 102 | 102 |
| 103 /** Visitor that serializes a message as a JSON array. */ | 103 /** Visitor that serializes a message as a JSON array. */ |
| 104 class _Serializer extends _MessageTraverser { | 104 class _Serializer extends _MessageTraverser { |
| 105 int _nextFreeRefId = 0; | 105 int _nextFreeRefId = 0; |
| 106 | 106 |
| 107 visitPrimitive(x) => x; | 107 visitPrimitive(x) => x; |
| 108 | 108 |
| 109 visitList(List list) { | 109 visitList(List list) { |
| 110 int copyId = _visited[list]; | 110 int copyId = _visited[list]; |
| 111 if (copyId !== null) return ['ref', copyId]; | 111 if (copyId != null) return ['ref', copyId]; |
| 112 | 112 |
| 113 int id = _nextFreeRefId++; | 113 int id = _nextFreeRefId++; |
| 114 _visited[list] = id; | 114 _visited[list] = id; |
| 115 var jsArray = _serializeList(list); | 115 var jsArray = _serializeList(list); |
| 116 // TODO(floitsch): we are losing the generic type. | 116 // TODO(floitsch): we are losing the generic type. |
| 117 return ['list', id, jsArray]; | 117 return ['list', id, jsArray]; |
| 118 } | 118 } |
| 119 | 119 |
| 120 visitMap(Map map) { | 120 visitMap(Map map) { |
| 121 int copyId = _visited[map]; | 121 int copyId = _visited[map]; |
| 122 if (copyId !== null) return ['ref', copyId]; | 122 if (copyId != null) return ['ref', copyId]; |
| 123 | 123 |
| 124 int id = _nextFreeRefId++; | 124 int id = _nextFreeRefId++; |
| 125 _visited[map] = id; | 125 _visited[map] = id; |
| 126 var keys = _serializeList(map.getKeys()); | 126 var keys = _serializeList(map.getKeys()); |
| 127 var values = _serializeList(map.getValues()); | 127 var values = _serializeList(map.getValues()); |
| 128 // TODO(floitsch): we are losing the generic type. | 128 // TODO(floitsch): we are losing the generic type. |
| 129 return ['map', id, keys, values]; | 129 return ['map', id, keys, values]; |
| 130 } | 130 } |
| 131 | 131 |
| 132 _serializeList(List list) { | 132 _serializeList(List list) { |
| 133 int len = list.length; | 133 int len = list.length; |
| 134 var result = new List(len); | 134 var result = new List(len); |
| 135 for (int i = 0; i < len; i++) { | 135 for (int i = 0; i < len; i++) { |
| 136 result[i] = _dispatch(list[i]); | 136 result[i] = _dispatch(list[i]); |
| 137 } | 137 } |
| 138 return result; | 138 return result; |
| 139 } | 139 } |
| 140 } | 140 } |
| 141 | 141 |
| 142 /** Deserializes arrays created with [_Serializer]. */ | 142 /** Deserializes arrays created with [_Serializer]. */ |
| 143 class _Deserializer { | 143 class _Deserializer { |
| 144 Map<int, Dynamic> _deserialized; | 144 Map<int, Dynamic> _deserialized; |
| 145 | 145 |
| 146 _Deserializer(); | 146 _Deserializer(); |
| 147 | 147 |
| 148 static bool isPrimitive(x) { | 148 static bool isPrimitive(x) { |
| 149 return (x === null) || (x is String) || (x is num) || (x is bool); | 149 return (x == null) || (x is String) || (x is num) || (x is bool); |
| 150 } | 150 } |
| 151 | 151 |
| 152 deserialize(x) { | 152 deserialize(x) { |
| 153 if (isPrimitive(x)) return x; | 153 if (isPrimitive(x)) return x; |
| 154 // TODO(floitsch): this should be new HashMap<int, var|Dynamic>() | 154 // TODO(floitsch): this should be new HashMap<int, var|Dynamic>() |
| 155 _deserialized = new HashMap(); | 155 _deserialized = new HashMap(); |
| 156 return _deserializeHelper(x); | 156 return _deserializeHelper(x); |
| 157 } | 157 } |
| 158 | 158 |
| 159 _deserializeHelper(x) { | 159 _deserializeHelper(x) { |
| 160 if (isPrimitive(x)) return x; | 160 if (isPrimitive(x)) return x; |
| 161 assert(x is List); | 161 assert(x is List); |
| 162 switch (x[0]) { | 162 switch (x[0]) { |
| 163 case 'ref': return _deserializeRef(x); | 163 case 'ref': return _deserializeRef(x); |
| 164 case 'list': return _deserializeList(x); | 164 case 'list': return _deserializeList(x); |
| 165 case 'map': return _deserializeMap(x); | 165 case 'map': return _deserializeMap(x); |
| 166 case 'sendport': return deserializeSendPort(x); | 166 case 'sendport': return deserializeSendPort(x); |
| 167 default: return deserializeObject(x); | 167 default: return deserializeObject(x); |
| 168 } | 168 } |
| 169 } | 169 } |
| 170 | 170 |
| 171 _deserializeRef(List x) { | 171 _deserializeRef(List x) { |
| 172 int id = x[1]; | 172 int id = x[1]; |
| 173 var result = _deserialized[id]; | 173 var result = _deserialized[id]; |
| 174 assert(result !== null); | 174 assert(result != null); |
| 175 return result; | 175 return result; |
| 176 } | 176 } |
| 177 | 177 |
| 178 List _deserializeList(List x) { | 178 List _deserializeList(List x) { |
| 179 int id = x[1]; | 179 int id = x[1]; |
| 180 // We rely on the fact that Dart-lists are directly mapped to Js-arrays. | 180 // We rely on the fact that Dart-lists are directly mapped to Js-arrays. |
| 181 List dartList = x[2]; | 181 List dartList = x[2]; |
| 182 _deserialized[id] = dartList; | 182 _deserialized[id] = dartList; |
| 183 int len = dartList.length; | 183 int len = dartList.length; |
| 184 for (int i = 0; i < len; i++) { | 184 for (int i = 0; i < len; i++) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 204 } | 204 } |
| 205 | 205 |
| 206 abstract deserializeSendPort(List x); | 206 abstract deserializeSendPort(List x); |
| 207 | 207 |
| 208 deserializeObject(List x) { | 208 deserializeObject(List x) { |
| 209 // TODO(floitsch): Use real exception (which one?). | 209 // TODO(floitsch): Use real exception (which one?). |
| 210 throw "Unexpected serialized object"; | 210 throw "Unexpected serialized object"; |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 | 213 |
| OLD | NEW |