| 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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 containsDate(object) { | 129 containsDate(object) { |
| 130 if (_isJavaScriptDate(object)) return true; | 130 if (_isJavaScriptDate(object)) return true; |
| 131 if (object is List) { | 131 if (object is List) { |
| 132 for (int i = 0; i < object.length; i++) { | 132 for (int i = 0; i < object.length; i++) { |
| 133 if (containsDate(object[i])) return true; | 133 if (containsDate(object[i])) return true; |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 return false; // number, string. | 136 return false; // number, string. |
| 137 } | 137 } |
| 138 if (containsDate(nativeKey)) { | 138 if (containsDate(nativeKey)) { |
| 139 throw const NotImplementedException('IDBKey containing Date'); | 139 throw new UnimplementedError('IDBKey containing Date'); |
| 140 } | 140 } |
| 141 // TODO: Cache conversion somewhere? | 141 // TODO: Cache conversion somewhere? |
| 142 return nativeKey; | 142 return nativeKey; |
| 143 } | 143 } |
| 144 | 144 |
| 145 /** | 145 /** |
| 146 * Converts a Dart object into a valid IDBKey. | 146 * Converts a Dart object into a valid IDBKey. |
| 147 * | 147 * |
| 148 * May return the original input. Does not mutate input. | 148 * May return the original input. Does not mutate input. |
| 149 * | 149 * |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 cleanupSlots() {} // Will be needed if we mark objects with a property. | 210 cleanupSlots() {} // Will be needed if we mark objects with a property. |
| 211 | 211 |
| 212 // Returns the input, or a clone of the input. | 212 // Returns the input, or a clone of the input. |
| 213 walk(e) { | 213 walk(e) { |
| 214 if (e == null) return e; | 214 if (e == null) return e; |
| 215 if (e is bool) return e; | 215 if (e is bool) return e; |
| 216 if (e is num) return e; | 216 if (e is num) return e; |
| 217 if (e is String) return e; | 217 if (e is String) return e; |
| 218 if (e is Date) { | 218 if (e is Date) { |
| 219 // TODO(sra). | 219 // TODO(sra). |
| 220 throw const NotImplementedException('structured clone of Date'); | 220 throw new UnimplementedError('structured clone of Date'); |
| 221 } | 221 } |
| 222 if (e is RegExp) { | 222 if (e is RegExp) { |
| 223 // TODO(sra). | 223 // TODO(sra). |
| 224 throw const NotImplementedException('structured clone of RegExp'); | 224 throw new UnimplementedError('structured clone of RegExp'); |
| 225 } | 225 } |
| 226 | 226 |
| 227 // The browser's internal structured cloning algorithm will copy certain | 227 // The browser's internal structured cloning algorithm will copy certain |
| 228 // types of object, but it will copy only its own implementations and not | 228 // types of object, but it will copy only its own implementations and not |
| 229 // just any Dart implementations of the interface. | 229 // just any Dart implementations of the interface. |
| 230 | 230 |
| 231 // TODO(sra): The JavaScript objects suitable for direct cloning by the | 231 // TODO(sra): The JavaScript objects suitable for direct cloning by the |
| 232 // structured clone algorithm could be tagged with an private interface. | 232 // structured clone algorithm could be tagged with an private interface. |
| 233 | 233 |
| 234 if (e is _FileImpl) return e; | 234 if (e is _FileImpl) return e; |
| 235 if (e is File) { | 235 if (e is File) { |
| 236 throw const NotImplementedException('structured clone of File'); | 236 throw new UnimplementedError('structured clone of File'); |
| 237 } | 237 } |
| 238 | 238 |
| 239 if (e is _BlobImpl) return e; | 239 if (e is _BlobImpl) return e; |
| 240 if (e is Blob) { | 240 if (e is Blob) { |
| 241 throw const NotImplementedException('structured clone of Blob'); | 241 throw new UnimplementedError('structured clone of Blob'); |
| 242 } | 242 } |
| 243 | 243 |
| 244 if (e is _FileListImpl) return e; | 244 if (e is _FileListImpl) return e; |
| 245 | 245 |
| 246 // TODO(sra): Firefox: How to convert _TypedImageData on the other end? | 246 // TODO(sra): Firefox: How to convert _TypedImageData on the other end? |
| 247 if (e is _ImageDataImpl) return e; | 247 if (e is _ImageDataImpl) return e; |
| 248 if (e is ImageData) { | 248 if (e is ImageData) { |
| 249 throw const NotImplementedException('structured clone of ImageData'); | 249 throw new UnimplementedError('structured clone of ImageData'); |
| 250 } | 250 } |
| 251 | 251 |
| 252 if (e is _ArrayBufferImpl) return e; | 252 if (e is _ArrayBufferImpl) return e; |
| 253 if (e is ArrayBuffer) { | 253 if (e is ArrayBuffer) { |
| 254 throw const NotImplementedException('structured clone of ArrayBuffer'); | 254 throw new UnimplementedError('structured clone of ArrayBuffer'); |
| 255 } | 255 } |
| 256 | 256 |
| 257 if (e is _ArrayBufferViewImpl) return e; | 257 if (e is _ArrayBufferViewImpl) return e; |
| 258 if (e is ArrayBufferView) { | 258 if (e is ArrayBufferView) { |
| 259 throw const NotImplementedException('structured clone of ArrayBufferView')
; | 259 throw new UnimplementedError('structured clone of ArrayBufferView'); |
| 260 } | 260 } |
| 261 | 261 |
| 262 if (e is Map) { | 262 if (e is Map) { |
| 263 var slot = findSlot(e); | 263 var slot = findSlot(e); |
| 264 var copy = readSlot(slot); | 264 var copy = readSlot(slot); |
| 265 if (copy != null) return copy; | 265 if (copy != null) return copy; |
| 266 copy = JS('var', '{}'); | 266 copy = JS('var', '{}'); |
| 267 writeSlot(slot, copy); | 267 writeSlot(slot, copy); |
| 268 e.forEach((key, value) { | 268 e.forEach((key, value) { |
| 269 JS('void', '#[#] = #', copy, key, walk(value)); | 269 JS('void', '#[#] = #', copy, key, walk(value)); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 copy = JS('List', 'new Array(#)', length); | 321 copy = JS('List', 'new Array(#)', length); |
| 322 writeSlot(slot, copy); | 322 writeSlot(slot, copy); |
| 323 } | 323 } |
| 324 | 324 |
| 325 for ( ; i < length; i++) { | 325 for ( ; i < length; i++) { |
| 326 copy[i] = walk(e[i]); | 326 copy[i] = walk(e[i]); |
| 327 } | 327 } |
| 328 return copy; | 328 return copy; |
| 329 } | 329 } |
| 330 | 330 |
| 331 throw const NotImplementedException('structured clone of other type'); | 331 throw new UnimplementedError('structured clone of other type'); |
| 332 } | 332 } |
| 333 | 333 |
| 334 var copy = walk(value); | 334 var copy = walk(value); |
| 335 cleanupSlots(); | 335 cleanupSlots(); |
| 336 return copy; | 336 return copy; |
| 337 } | 337 } |
| 338 | 338 |
| 339 /** | 339 /** |
| 340 * Converts a native value into a Dart object. | 340 * Converts a native value into a Dart object. |
| 341 * | 341 * |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 writeSlot(int i, x) { copies[i] = x; } | 374 writeSlot(int i, x) { copies[i] = x; } |
| 375 | 375 |
| 376 walk(e) { | 376 walk(e) { |
| 377 if (e == null) return e; | 377 if (e == null) return e; |
| 378 if (e is bool) return e; | 378 if (e is bool) return e; |
| 379 if (e is num) return e; | 379 if (e is num) return e; |
| 380 if (e is String) return e; | 380 if (e is String) return e; |
| 381 | 381 |
| 382 if (_isJavaScriptDate(e)) { | 382 if (_isJavaScriptDate(e)) { |
| 383 // TODO(sra). | 383 // TODO(sra). |
| 384 throw const NotImplementedException('structured clone of Date'); | 384 throw new UnimplementedError('structured clone of Date'); |
| 385 } | 385 } |
| 386 | 386 |
| 387 if (_isJavaScriptRegExp(e)) { | 387 if (_isJavaScriptRegExp(e)) { |
| 388 // TODO(sra). | 388 // TODO(sra). |
| 389 throw const NotImplementedException('structured clone of RegExp'); | 389 throw new UnimplementedError('structured clone of RegExp'); |
| 390 } | 390 } |
| 391 | 391 |
| 392 if (_isJavaScriptSimpleObject(e)) { | 392 if (_isJavaScriptSimpleObject(e)) { |
| 393 // TODO(sra): If mustCopy is false, swizzle the prototype for one of a Map | 393 // TODO(sra): If mustCopy is false, swizzle the prototype for one of a Map |
| 394 // implementation that uses the properies as storage. | 394 // implementation that uses the properies as storage. |
| 395 var slot = findSlot(e); | 395 var slot = findSlot(e); |
| 396 var copy = readSlot(slot); | 396 var copy = readSlot(slot); |
| 397 if (copy != null) return copy; | 397 if (copy != null) return copy; |
| 398 copy = {}; | 398 copy = {}; |
| 399 | 399 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 } | 431 } |
| 432 | 432 |
| 433 | 433 |
| 434 bool _isJavaScriptDate(value) => JS('bool', '# instanceof Date', value); | 434 bool _isJavaScriptDate(value) => JS('bool', '# instanceof Date', value); |
| 435 bool _isJavaScriptRegExp(value) => JS('bool', '# instanceof RegExp', value); | 435 bool _isJavaScriptRegExp(value) => JS('bool', '# instanceof RegExp', value); |
| 436 bool _isJavaScriptArray(value) => JS('bool', '# instanceof Array', value); | 436 bool _isJavaScriptArray(value) => JS('bool', '# instanceof Array', value); |
| 437 bool _isJavaScriptSimpleObject(value) => | 437 bool _isJavaScriptSimpleObject(value) => |
| 438 JS('bool', 'Object.getPrototypeOf(#) === Object.prototype', value); | 438 JS('bool', 'Object.getPrototypeOf(#) === Object.prototype', value); |
| 439 bool _isImmutableJavaScriptArray(value) => | 439 bool _isImmutableJavaScriptArray(value) => |
| 440 JS('bool', r'!!(#.immutable$list)', value); | 440 JS('bool', r'!!(#.immutable$list)', value); |
| OLD | NEW |