Chromium Code Reviews| Index: lib/html/src/dart2js_Conversions.dart |
| diff --git a/lib/html/src/dart2js_Conversions.dart b/lib/html/src/dart2js_Conversions.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fa913073d437a847c6f1e0ef5d3337e86f8a10a7 |
| --- /dev/null |
| +++ b/lib/html/src/dart2js_Conversions.dart |
| @@ -0,0 +1,107 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +// Conversions for IDBKey. |
| +// |
| +// Per http://www.w3.org/TR/IndexedDB/#key-construct |
| +// |
| +// "A value is said to be a valid key if it is one of the following types: Array |
| +// JavaScript objects [ECMA-262], DOMString [WEBIDL], Date [ECMA-262] or float |
| +// [WEBIDL]. However Arrays are only valid keys if every item in the array is |
| +// defined and is a valid key (i.e. sparse arrays can not be valid keys) and if |
| +// the Array doesn't directly or indirectly contain itself. Any non-numeric |
| +// properties are ignored, and thus does not affect whether the Array is a valid |
| +// key. Additionally, if the value is of type float, it is only a valid key if |
| +// it is not NaN, and if the value is of type Date it is only a valid key if its |
| +// [[PrimitiveValue]] internal property, as defined by [ECMA-262], is not NaN." |
| + |
| +// What is required is to ensure that an Lists in the key are actually |
| +// JavaScript arrays, and any Dates are JavaScript Dates. |
| + |
| +/** |
| + * Converts a native IDBKey into a Dart object. |
| + * |
| + * May return the original input. May mutate the original input (but will be |
| + * idempotent if mutation occurs). It is assumed that this conversion happens |
| + * on native IDBKeys on all paths that return IDBKeys from native DOM calls. |
| + * |
| + * If necessary, JavaScript Dates are converted into Dart Dates. |
| + */ |
| +_convertNativeToDart_IDBKey(nativeKey) { |
| + // TODO: Implement. |
| + // TODO: Cache conversion somewhere. |
| + return nativeKey; |
| +} |
| + |
| +/** |
| + * Converts a Dart object into a valid IDBKey. |
| + * |
| + * May return the original input. Does not mutate input. |
| + * |
| + * If necessary, [dartKey] may be copied to ensure all lists are converted into |
| + * JavaScript Arrays and Dart Dates into JavaScript Dates. |
| + */ |
| + |
| +_convertDartToNative_IDBKey(dartKey) { |
| + // TODO: Implement. |
| + // TODO: Cache conversion on object. |
| + return dartKey; |
| +} |
| + |
| + |
| + |
| +// Conversions for ImageData |
| +// |
| +// On Firefox, the returned ImageData is a plain object. |
| + |
| +class FakeImageData implements ImageData { |
|
vsm
2012/08/22 18:49:36
Nit: I'd use a different name than 'Fake'. E.g.,
|
| + final Uint8ClampedArray data; |
| + final int height; |
| + final int width; |
| + |
| + FakeImageData._internal(this.data, this.height, this.width); |
| +} |
| + |
| +ImageData _convertNativeToDart_ImageData(Object nativeImageData) { |
| + if (nativeImageData is ImageData) return nativeImageData; |
| + |
| + // On Firefox the above test fails because imagedata is a plain object. |
| + // So we create a FakeImageData. |
| + |
| + return new FakeImageData._internal( |
| + JS('var', '#.data', nativeImageData), |
| + JS('var', '#.height', nativeImageData), |
| + JS('var', '#.width', nativeImageData)); |
| +} |
| + |
| +Object _convertDartToNative_ImageData(ImageData imageData) { |
| + if (imageData is _ImageDataImpl) return imageData; |
| + |
| + // We can get rid of this conversion if FakeImageData implements the fields |
| + // with native names. |
| + return JS('Object', '{data: #, height: #, width: #}', |
| + imageData.data, imageData.height, imageData.width); |
| +} |
| + |
| + |
| +/// Converts a JavaScript object with properties into a Dart Map. |
| +Map _convertNativeToDart_Dictionary(Object object) { |
| + // TODO: Implement. |
| + return object; |
| +} |
| + |
| +/// Converts a Dart map into a JavaScript object with properties. |
| +Object _convertDartToNative_Dictionary(Map dict) { |
| + // TODO: Implement. |
| + return dict; |
| +} |
| + |
| + |
| +/** |
| + * Ensures that the input is a JavaScript Array. |
| + * |
| + * Creates a new JavaScript array if necessary, otherwise returns the original. |
| + */ |
| +Object _ensureStringArray(List<String> input) { |
|
vsm
2012/08/22 18:49:36
Rename to convertDartToNative for consistency?
|
| +} |