Chromium Code Reviews| 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 | 5 |
| 6 // Conversions for IDBKey. | 6 // Conversions for IDBKey. |
| 7 // | 7 // |
| 8 // Per http://www.w3.org/TR/IndexedDB/#key-construct | 8 // Per http://www.w3.org/TR/IndexedDB/#key-construct |
| 9 // | 9 // |
| 10 // "A value is said to be a valid key if it is one of the following types: Array | 10 // "A value is said to be a valid key if it is one of the following types: Array |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 } | 69 } |
| 70 values.add(value); | 70 values.add(value); |
| 71 copies.add(null); | 71 copies.add(null); |
| 72 return length; | 72 return length; |
| 73 } | 73 } |
| 74 readSlot(int i) => copies[i]; | 74 readSlot(int i) => copies[i]; |
| 75 writeSlot(int i, x) { copies[i] = x; } | 75 writeSlot(int i, x) { copies[i] = x; } |
| 76 cleanupSlots() {} // Will be needed if we mark objects with a property. | 76 cleanupSlots() {} // Will be needed if we mark objects with a property. |
| 77 bool cloneNotRequired(object); | 77 bool cloneNotRequired(object); |
| 78 newJsMap(); | 78 newJsMap(); |
| 79 newJsList(length); | 79 List newJsList(length); |
| 80 void putIntoMap(map, key, value); | 80 void putIntoMap(map, key, value); |
| 81 | 81 |
| 82 // Returns the input, or a clone of the input. | 82 // Returns the input, or a clone of the input. |
| 83 walk(e) { | 83 walk(e) { |
| 84 if (e == null) return e; | 84 if (e == null) return e; |
| 85 if (e is bool) return e; | 85 if (e is bool) return e; |
| 86 if (e is num) return e; | 86 if (e is num) return e; |
| 87 if (e is String) return e; | 87 if (e is String) return e; |
| 88 if (e is DateTime) { | 88 if (e is DateTime) { |
| 89 return convertDartToNative_DateTime(e); | 89 return convertDartToNative_DateTime(e); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 120 return copy; | 120 return copy; |
| 121 } | 121 } |
| 122 | 122 |
| 123 if (e is List) { | 123 if (e is List) { |
| 124 // Since a JavaScript Array is an instance of Dart List it is tempting | 124 // Since a JavaScript Array is an instance of Dart List it is tempting |
| 125 // in dart2js to avoid making a copy of the list if there is no need | 125 // in dart2js to avoid making a copy of the list if there is no need |
| 126 // to copy anything reachable from the array. However, the list may have | 126 // to copy anything reachable from the array. However, the list may have |
| 127 // non-native properties or methods from interceptors and such, e.g. | 127 // non-native properties or methods from interceptors and such, e.g. |
| 128 // an immutability marker. So we had to stop doing that. | 128 // an immutability marker. So we had to stop doing that. |
| 129 var slot = findSlot(e); | 129 var slot = findSlot(e); |
| 130 var copy = readSlot(slot); | 130 var copy = JS('List', '#', readSlot(slot)); |
| 131 if (copy != null) return copy; | 131 if (copy != null) return copy; |
| 132 copy = copyList(e, slot); | 132 copy = copyList(e, slot); |
| 133 return copy; | 133 return copy; |
| 134 } | 134 } |
| 135 | 135 |
| 136 throw new UnimplementedError('structured clone of other type'); | 136 throw new UnimplementedError('structured clone of other type'); |
| 137 } | 137 } |
| 138 | 138 |
| 139 copyList(List e, int slot) { | 139 List copyList(List e, int slot) { |
| 140 int i = 0; | 140 int i = 0; |
| 141 int length = e.length; | 141 int length = e.length; |
| 142 var copy = newJsList(length); | 142 var copy = newJsList(length); |
| 143 writeSlot(slot, copy); | 143 writeSlot(slot, copy); |
| 144 for ( ; i < length; i++) { | 144 for ( ; i < length; i++) { |
| 145 copy[i] = walk(e[i]); | 145 copy[i] = walk(e[i]); |
| 146 } | 146 } |
| 147 return copy; | 147 return copy; |
| 148 } | 148 } |
| 149 | 149 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 return length; | 189 return length; |
| 190 } | 190 } |
| 191 | 191 |
| 192 /// Are the two objects identical, but taking into account that two JsObject | 192 /// Are the two objects identical, but taking into account that two JsObject |
| 193 /// wrappers may not be identical, but their underlying Js Object might be. | 193 /// wrappers may not be identical, but their underlying Js Object might be. |
| 194 bool identicalInJs(a, b); | 194 bool identicalInJs(a, b); |
| 195 readSlot(int i) => copies[i]; | 195 readSlot(int i) => copies[i]; |
| 196 writeSlot(int i, x) { copies[i] = x; } | 196 writeSlot(int i, x) { copies[i] = x; } |
| 197 | 197 |
| 198 /// Iterate over the JS properties. | 198 /// Iterate over the JS properties. |
| 199 forEachJsField(object, action); | 199 forEachJsField(object, action(key, value)); |
| 200 | 200 |
| 201 /// Create a new Dart list of the given length. May create a native List or | 201 /// Create a new Dart list of the given length. May create a native List or |
| 202 /// a JsArray, depending if we're in Dartium or dart2js. | 202 /// a JsArray, depending if we're in Dartium or dart2js. |
| 203 newDartList(length); | 203 List newDartList(length); |
| 204 | 204 |
| 205 walk(e) { | 205 walk(e) { |
| 206 if (e == null) return e; | 206 if (e == null) return e; |
| 207 if (e is bool) return e; | 207 if (e is bool) return e; |
| 208 if (e is num) return e; | 208 if (e is num) return e; |
| 209 if (e is String) return e; | 209 if (e is String) return e; |
| 210 | 210 |
| 211 if (isJavaScriptDate(e)) { | 211 if (isJavaScriptDate(e)) { |
| 212 return convertNativeToDart_DateTime(e); | 212 return convertNativeToDart_DateTime(e); |
| 213 } | 213 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 228 var copy = readSlot(slot); | 228 var copy = readSlot(slot); |
| 229 if (copy != null) return copy; | 229 if (copy != null) return copy; |
| 230 copy = {}; | 230 copy = {}; |
| 231 | 231 |
| 232 writeSlot(slot, copy); | 232 writeSlot(slot, copy); |
| 233 forEachJsField(e, (key, value) => copy[key] = walk(value)); | 233 forEachJsField(e, (key, value) => copy[key] = walk(value)); |
| 234 return copy; | 234 return copy; |
| 235 } | 235 } |
| 236 | 236 |
| 237 if (isJavaScriptArray(e)) { | 237 if (isJavaScriptArray(e)) { |
| 238 var slot = findSlot(e); | 238 List l = JS('', '#', e); |
|
vsm
2017/02/01 20:59:43
should be able to move the type to the right here
Jacob
2017/02/01 21:26:45
Done.
| |
| 239 var copy = readSlot(slot); | 239 var slot = findSlot(l); |
| 240 var copy = JS('List', '#', readSlot(slot)); | |
| 240 if (copy != null) return copy; | 241 if (copy != null) return copy; |
| 241 | 242 |
| 242 int length = e.length; | 243 int length = l.length; |
| 243 // Since a JavaScript Array is an instance of Dart List, we can modify it | 244 // Since a JavaScript Array is an instance of Dart List, we can modify it |
| 244 // in-place unless we must copy. | 245 // in-place unless we must copy. |
| 245 copy = mustCopy ? newDartList(length) : e; | 246 copy = mustCopy ? newDartList(length) : l; |
| 246 writeSlot(slot, copy); | 247 writeSlot(slot, copy); |
| 247 | 248 |
| 248 for (int i = 0; i < length; i++) { | 249 for (int i = 0; i < length; i++) { |
| 249 copy[i] = walk(e[i]); | 250 copy[i] = walk(l[i]); |
| 250 } | 251 } |
| 251 return copy; | 252 return copy; |
| 252 } | 253 } |
| 253 | 254 |
| 254 // Assume anything else is already a valid Dart object, either by having | 255 // Assume anything else is already a valid Dart object, either by having |
| 255 // already been processed, or e.g. a clonable native class. | 256 // already been processed, or e.g. a clonable native class. |
| 256 return e; | 257 return e; |
| 257 } | 258 } |
| 258 | 259 |
| 259 convertNativeToDart_AcceptStructuredClone(object, {mustCopy: false}) { | 260 convertNativeToDart_AcceptStructuredClone(object, {mustCopy: false}) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 const String _serializedScriptValue = | 356 const String _serializedScriptValue = |
| 356 'num|String|bool|' | 357 'num|String|bool|' |
| 357 'JSExtendableArray|=Object|' | 358 'JSExtendableArray|=Object|' |
| 358 'Blob|File|NativeByteBuffer|NativeTypedData' | 359 'Blob|File|NativeByteBuffer|NativeTypedData' |
| 359 // TODO(sra): Add Date, RegExp. | 360 // TODO(sra): Add Date, RegExp. |
| 360 ; | 361 ; |
| 361 const annotation_Creates_SerializedScriptValue = | 362 const annotation_Creates_SerializedScriptValue = |
| 362 const Creates(_serializedScriptValue); | 363 const Creates(_serializedScriptValue); |
| 363 const annotation_Returns_SerializedScriptValue = | 364 const annotation_Returns_SerializedScriptValue = |
| 364 const Returns(_serializedScriptValue); | 365 const Returns(_serializedScriptValue); |
| OLD | NEW |