| 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 16 matching lines...) Expand all Loading... |
| 27 // window as a parameter. | 27 // window as a parameter. |
| 28 | 28 |
| 29 part of html_common; | 29 part of html_common; |
| 30 | 30 |
| 31 | 31 |
| 32 /// Converts a JavaScript object with properties into a Dart Map. | 32 /// Converts a JavaScript object with properties into a Dart Map. |
| 33 /// Not suitable for nested objects. | 33 /// Not suitable for nested objects. |
| 34 Map convertNativeToDart_Dictionary(object) { | 34 Map convertNativeToDart_Dictionary(object) { |
| 35 if (object == null) return null; | 35 if (object == null) return null; |
| 36 var dict = {}; | 36 var dict = {}; |
| 37 for (final key in JS('=List', 'Object.getOwnPropertyNames(#)', object)) { | 37 var keys = JS('JSExtendableArray', 'Object.getOwnPropertyNames(#)', object); |
| 38 for (final key in keys) { |
| 38 dict[key] = JS('var', '#[#]', object, key); | 39 dict[key] = JS('var', '#[#]', object, key); |
| 39 } | 40 } |
| 40 return dict; | 41 return dict; |
| 41 } | 42 } |
| 42 | 43 |
| 43 /// Converts a flat Dart map into a JavaScript object with properties. | 44 /// Converts a flat Dart map into a JavaScript object with properties. |
| 44 convertDartToNative_Dictionary(Map dict) { | 45 convertDartToNative_Dictionary(Map dict) { |
| 45 if (dict == null) return null; | 46 if (dict == null) return null; |
| 46 var object = JS('var', '{}'); | 47 var object = JS('var', '{}'); |
| 47 dict.forEach((String key, value) { | 48 dict.forEach((String key, value) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 if (e is List) { | 159 if (e is List) { |
| 159 // Since a JavaScript Array is an instance of Dart List it is possible to | 160 // Since a JavaScript Array is an instance of Dart List it is possible to |
| 160 // avoid making a copy of the list if there is no need to copy anything | 161 // avoid making a copy of the list if there is no need to copy anything |
| 161 // reachable from the array. We defer creating a new array until a cycle | 162 // reachable from the array. We defer creating a new array until a cycle |
| 162 // is detected or a subgraph was copied. | 163 // is detected or a subgraph was copied. |
| 163 int length = e.length; | 164 int length = e.length; |
| 164 var slot = findSlot(e); | 165 var slot = findSlot(e); |
| 165 var copy = readSlot(slot); | 166 var copy = readSlot(slot); |
| 166 if (copy != null) { | 167 if (copy != null) { |
| 167 if (true == copy) { // Cycle, so commit to making a copy. | 168 if (true == copy) { // Cycle, so commit to making a copy. |
| 168 copy = JS('=List', 'new Array(#)', length); | 169 copy = JS('JSExtendableArray', 'new Array(#)', length); |
| 169 writeSlot(slot, copy); | 170 writeSlot(slot, copy); |
| 170 } | 171 } |
| 171 return copy; | 172 return copy; |
| 172 } | 173 } |
| 173 | 174 |
| 174 int i = 0; | 175 int i = 0; |
| 175 | 176 |
| 176 // Always clone the list, as it may have non-native properties or methods | 177 // Always clone the list, as it may have non-native properties or methods |
| 177 // from interceptors and such. | 178 // from interceptors and such. |
| 178 copy = JS('=List', 'new Array(#)', length); | 179 copy = JS('JSExtendableArray', 'new Array(#)', length); |
| 179 writeSlot(slot, copy); | 180 writeSlot(slot, copy); |
| 180 | 181 |
| 181 for ( ; i < length; i++) { | 182 for ( ; i < length; i++) { |
| 182 copy[i] = walk(e[i]); | 183 copy[i] = walk(e[i]); |
| 183 } | 184 } |
| 184 return copy; | 185 return copy; |
| 185 } | 186 } |
| 186 | 187 |
| 187 throw new UnimplementedError('structured clone of other type'); | 188 throw new UnimplementedError('structured clone of other type'); |
| 188 } | 189 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 248 |
| 248 if (isJavaScriptSimpleObject(e)) { | 249 if (isJavaScriptSimpleObject(e)) { |
| 249 // TODO(sra): If mustCopy is false, swizzle the prototype for one of a Map | 250 // TODO(sra): If mustCopy is false, swizzle the prototype for one of a Map |
| 250 // implementation that uses the properies as storage. | 251 // implementation that uses the properies as storage. |
| 251 var slot = findSlot(e); | 252 var slot = findSlot(e); |
| 252 var copy = readSlot(slot); | 253 var copy = readSlot(slot); |
| 253 if (copy != null) return copy; | 254 if (copy != null) return copy; |
| 254 copy = {}; | 255 copy = {}; |
| 255 | 256 |
| 256 writeSlot(slot, copy); | 257 writeSlot(slot, copy); |
| 257 for (final key in JS('=List', 'Object.keys(#)', e)) { | 258 for (final key in JS('JSExtendableArray', 'Object.keys(#)', e)) { |
| 258 copy[key] = walk(JS('var', '#[#]', e, key)); | 259 copy[key] = walk(JS('var', '#[#]', e, key)); |
| 259 } | 260 } |
| 260 return copy; | 261 return copy; |
| 261 } | 262 } |
| 262 | 263 |
| 263 if (isJavaScriptArray(e)) { | 264 if (isJavaScriptArray(e)) { |
| 264 var slot = findSlot(e); | 265 var slot = findSlot(e); |
| 265 var copy = readSlot(slot); | 266 var copy = readSlot(slot); |
| 266 if (copy != null) return copy; | 267 if (copy != null) return copy; |
| 267 | 268 |
| 268 int length = e.length; | 269 int length = e.length; |
| 269 // Since a JavaScript Array is an instance of Dart List, we can modify it | 270 // Since a JavaScript Array is an instance of Dart List, we can modify it |
| 270 // in-place unless we must copy. | 271 // in-place unless we must copy. |
| 271 copy = mustCopy ? JS('=List', 'new Array(#)', length) : e; | 272 copy = mustCopy ? JS('JSExtendableArray', 'new Array(#)', length) : e; |
| 272 writeSlot(slot, copy); | 273 writeSlot(slot, copy); |
| 273 | 274 |
| 274 for (int i = 0; i < length; i++) { | 275 for (int i = 0; i < length; i++) { |
| 275 copy[i] = walk(e[i]); | 276 copy[i] = walk(e[i]); |
| 276 } | 277 } |
| 277 return copy; | 278 return copy; |
| 278 } | 279 } |
| 279 | 280 |
| 280 // Assume anything else is already a valid Dart object, either by having | 281 // Assume anything else is already a valid Dart object, either by having |
| 281 // already been processed, or e.g. a clonable native class. | 282 // already been processed, or e.g. a clonable native class. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 bool isJavaScriptArray(value) => JS('bool', '# instanceof Array', value); | 336 bool isJavaScriptArray(value) => JS('bool', '# instanceof Array', value); |
| 336 bool isJavaScriptSimpleObject(value) => | 337 bool isJavaScriptSimpleObject(value) => |
| 337 JS('bool', 'Object.getPrototypeOf(#) === Object.prototype', value); | 338 JS('bool', 'Object.getPrototypeOf(#) === Object.prototype', value); |
| 338 bool isImmutableJavaScriptArray(value) => | 339 bool isImmutableJavaScriptArray(value) => |
| 339 JS('bool', r'!!(#.immutable$list)', value); | 340 JS('bool', r'!!(#.immutable$list)', value); |
| 340 | 341 |
| 341 | 342 |
| 342 | 343 |
| 343 const String _serializedScriptValue = | 344 const String _serializedScriptValue = |
| 344 'num|String|bool|' | 345 'num|String|bool|' |
| 345 '=List|=Object|' | 346 'JSExtendableArray|=Object|' |
| 346 'Blob|File|ByteBuffer|TypedData' | 347 'Blob|File|ByteBuffer|TypedData' |
| 347 // TODO(sra): Add Date, RegExp. | 348 // TODO(sra): Add Date, RegExp. |
| 348 ; | 349 ; |
| 349 const annotation_Creates_SerializedScriptValue = | 350 const annotation_Creates_SerializedScriptValue = |
| 350 const Creates(_serializedScriptValue); | 351 const Creates(_serializedScriptValue); |
| 351 const annotation_Returns_SerializedScriptValue = | 352 const annotation_Returns_SerializedScriptValue = |
| 352 const Returns(_serializedScriptValue); | 353 const Returns(_serializedScriptValue); |
| OLD | NEW |