Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Unified Diff: lib/html/dart2js/html_dart2js.dart

Side-by-side diff isn't available for this file because of its large size.
Issue 10868067: dart2js dart:html conversions for serialized script values. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
Download patch
« no previous file with comments | « lib/dom/scripts/generator.py ('k') | lib/html/src/dart2js_Conversions.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/html/dart2js/html_dart2js.dart
diff --git a/lib/html/dart2js/html_dart2js.dart b/lib/html/dart2js/html_dart2js.dart
index 9d726eb5daff1085ba65b2a98368d99191c58d0f..d260a5c029e2835b9fd9bfdf3ab4924b47ae9beb 100644
--- a/lib/html/dart2js/html_dart2js.dart
+++ b/lib/html/dart2js/html_dart2js.dart
@@ -7747,12 +7747,17 @@ class _IDBCursorImpl implements IDBCursor native "*IDBCursor" {
_IDBRequestImpl delete() native;
- _IDBRequestImpl update(value) native;
+ _IDBRequestImpl update(value) {
+ var value_1 = _convertDartToNative_SerializedScriptValue(value);
+ return _update_1(value_1);
+ }
+ _IDBRequestImpl _update_1(value) native "update";
}
class _IDBCursorWithValueImpl extends _IDBCursorImpl implements IDBCursorWithValue native "*IDBCursorWithValue" {
- final Dynamic value;
+ Dynamic get value() => _convertNativeToDart_IDBAny(this._value);
+ Dynamic get _value() native "return this.value;";
}
// 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
@@ -8090,10 +8095,12 @@ class _IDBObjectStoreImpl implements IDBObjectStore native "*IDBObjectStore" {
_IDBRequestImpl add(value, [key = _default]) {
if (_default != key) {
- var key_1 = _convertDartToNative_IDBKey(key);
- return _add_1(value, key_1);
+ var value_1 = _convertDartToNative_SerializedScriptValue(value);
+ var key_2 = _convertDartToNative_IDBKey(key);
+ return _add_1(value_1, key_2);
}
- return _add_2(value);
+ var value_3 = _convertDartToNative_SerializedScriptValue(value);
+ return _add_2(value_3);
}
_IDBRequestImpl _add_1(value, key) native "add";
_IDBRequestImpl _add_2(value) native "add";
@@ -8229,10 +8236,12 @@ class _IDBObjectStoreImpl implements IDBObjectStore native "*IDBObjectStore" {
_IDBRequestImpl put(value, [key = _default]) {
if (_default != key) {
- var key_1 = _convertDartToNative_IDBKey(key);
- return _put_1(value, key_1);
+ var value_1 = _convertDartToNative_SerializedScriptValue(value);
+ var key_2 = _convertDartToNative_IDBKey(key);
+ return _put_1(value_1, key_2);
}
- return _put_2(value);
+ var value_3 = _convertDartToNative_SerializedScriptValue(value);
+ return _put_2(value_3);
}
_IDBRequestImpl _put_1(value, key) native "put";
_IDBRequestImpl _put_2(value) native "put";
@@ -8271,7 +8280,8 @@ class _IDBRequestImpl extends _EventTargetImpl implements IDBRequest native "*ID
final String readyState;
- final Dynamic result;
+ Dynamic get result() => _convertNativeToDart_IDBAny(this._result);
+ Dynamic get _result() native "return this.result;";
final Dynamic source;
@@ -38420,6 +38430,7 @@ class _SVGSVGElementFactoryProvider {
// 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
@@ -38437,37 +38448,6 @@ class _SVGSVGElementFactoryProvider {
// 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.
@@ -38504,7 +38484,7 @@ _convertDartToNative_ImageData(ImageData imageData) {
/// Converts a JavaScript object with properties into a Dart Map.
/// Not suitable for nested objects.
Map _convertNativeToDart_Dictionary(object) {
- // TODO: Implement.
+ if (object == null) return null;
var dict = {};
for (final key in JS('List', 'Object.getOwnPropertyNames(#)', object)) {
dict[key] = JS('var', '#[#]', object, key);
@@ -38514,6 +38494,7 @@ Map _convertNativeToDart_Dictionary(object) {
/// Converts a flat Dart map into a JavaScript object with properties.
_convertDartToNative_Dictionary(Map dict) {
+ if (dict == null) return null;
var object = JS('var', '{}');
dict.forEach((String key, value) {
JS('void', '#[#] = #', object, key, value);
@@ -38528,8 +38509,320 @@ _convertDartToNative_Dictionary(Map dict) {
* Creates a new JavaScript array if necessary, otherwise returns the original.
*/
List _convertDartToNative_StringArray(List<String> input) {
+ // TODO(sra). Implement this.
return input;
}
+
+
+// -----------------------------------------------------------------------------
+
+/**
+ * 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) {
+ containsDate(object) {
+ if (_isJavaScriptDate(object)) return true;
+ if (object is List) {
+ for (int i = 0; i < object.length; i++) {
+ if (containsDate(object[i])) return true;
+ }
+ }
+ return false; // number, string.
+ }
+ if (containsDate(nativeKey)) {
+ throw const NotImplementedException('IDBKey containing Date');
+ }
+ // 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.
+ return dartKey;
+}
+
+
+
+// May modify original. If so, action is idempotent.
+_convertNativeToDart_IDBAny(object) {
+ return _convertNativeToDart_AcceptStructuredClone(object);
+}
+
+/// Converts a Dart value into
+_convertDartToNative_SerializedScriptValue(value) {
+ return _convertDartToNative_PrepareForStructuredClone(value);
+}
+
+
+/**
+ * Converts a Dart value into a JavaScript SerializedScriptValue. Returns the
+ * original input or a functional 'copy'. Does not mutate the original.
+ *
+ * The main transformation is the translation of Dart Maps are converted to
+ * JavaScript Objects.
+ *
+ * The algorithm is essentially a dry-run of the structured clone algorithm
+ * described at
+ * http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#structured-clone
+ * https://www.khronos.org/registry/typedarray/specs/latest/#9
+ *
+ */
+_convertDartToNative_PrepareForStructuredClone(value) {
+
+ // TODO(sra): Replace slots with identity hash table.
+ var values = [];
+ var copies = []; // initially 'null', 'true' during initial DFS, then a copy.
+
+ int findSlot(value) {
+ int length = values.length;
+ for (int i = 0; i < length; i++) {
+ if (values[i] === value) return i;
+ }
+ values.add(value);
+ copies.add(null);
+ return length;
+ }
+ readSlot(int i) => copies[i];
+ writeSlot(int i, x) { copies[i] = x; }
+ cleanupSlots() {} // Will be needed if we mark objects with a property.
+
+ // Returns the input, or a clone of the input.
+ walk(e) {
+ if (e == null) return e;
+ if (e is bool) return e;
+ if (e is num) return e;
+ if (e is String) return e;
+ if (e is Date) {
+ // TODO(sra).
+ throw const NotImplementedException('structured clone of Date');
+ }
+ if (e is RegExp) {
+ // TODO(sra).
+ throw const NotImplementedException('structured clone of RegExp');
+ }
+
+ // The browser's internal structured cloning algorithm will copy certain
+ // types of object, but it will copy only its own implementations and not
+ // just any Dart implementations of the interface.
+
+ // TODO(sra): The JavaScript objects suitable for direct cloning by the
+ // structured clone algorithm could be tagged with an private interface.
+
+ if (e is _FileImpl) return e;
+ if (e is File) {
+ throw const NotImplementedException('structured clone of File');
+ }
+
+ if (e is _BlobImpl) return e;
+ if (e is Blob) {
+ throw const NotImplementedException('structured clone of Blob');
+ }
+
+ if (e is _FileListImpl) return e;
+ if (e is FileList) {
+ throw const NotImplementedException('structured clone of FileList');
+ }
+
+ // TODO(sra): Firefox: How to convert _TypedImageData on the other end?
+ if (e is _ImageDataImpl) return e;
+ if (e is ImageData) {
+ throw const NotImplementedException('structured clone of FileList');
+ }
+
+ if (e is _ArrayBufferImpl) return e;
+ if (e is ArrayBuffer) {
+ throw const NotImplementedException('structured clone of ArrayBuffer');
+ }
+
+ if (e is _ArrayBufferViewImpl) return e;
+ if (e is ArrayBufferView) {
+ throw const NotImplementedException('structured clone of ArrayBufferView');
+ }
+
+ if (e is Map) {
+ var slot = findSlot(e);
+ var copy = readSlot(slot);
+ if (copy != null) return copy;
+ copy = JS('var', '{}');
+ writeSlot(slot, copy);
+ e.forEach((key, value) {
+ JS('void', '#[#] = #', copy, key, walk(value));
+ });
+ return copy;
+ }
+
+ if (e is List) {
+ // Since a JavaScript Array is an instance of Dart List it is possible to
+ // avoid making a copy of the list if there is no need to copy anything
+ // reachable from the array. We defer creating a new array until a cycle
+ // is detected or a subgraph was copied.
+ int length = e.length;
+ var slot = findSlot(e);
+ var copy = readSlot(slot);
+ if (copy != null) {
+ if (true == copy) { // Cycle, so commit to making a copy.
+ copy = JS('List', 'new Array(#)', length);
+ writeSlot(slot, copy);
+ }
+ return copy;
+ }
+
+ int i = 0;
+
+ if (_isJavaScriptArray(e) &&
+ // We have to copy immutable lists, otherwise the structured clone
+ // algorithm will copy the .immutable$list marker property, making the
+ // list immutable when received!
+ !_isImmutableJavaScriptArray(e)) {
+ writeSlot(slot, true); // Deferred copy.
+ for ( ; i < length; i++) {
+ var element = e[i];
+ var elementCopy = walk(element);
+ if (elementCopy !== element) {
+ copy = readSlot(slot); // Cyclic reference may have created it.
+ if (true == copy) {
+ copy = JS('List', 'new Array(#)', length);
+ writeSlot(slot, copy);
+ }
+ for (int j = 0; j < i; j++) {
+ copy[j] = e[j];
+ }
+ copy[i] = elementCopy;
+ i++;
+ break;
+ }
+ }
+ if (copy == null) {
+ copy = e;
+ writeSlot(slot, copy);
+ }
+ } else {
+ // Not a JavaScript Array. We are forced to make a copy.
+ copy = JS('List', 'new Array(#)', length);
+ writeSlot(slot, copy);
+ }
+
+ for ( ; i < length; i++) {
+ copy[i] = walk(e[i]);
+ }
+ return copy;
+ }
+
+ throw const NotImplementedException('structured clone of other type');
+ }
+
+ var copy = walk(value);
+ cleanupSlots();
+ return copy;
+}
+
+/**
+ * Converts a native value 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 serializable script values such values from native DOM calls.
+ *
+ * [object] is the result of a structured clone operation.
+ *
+ * If necessary, JavaScript Dates are converted into Dart Dates.
+ */
+_convertNativeToDart_AcceptStructuredClone(object) {
+
+ // TODO(sra): Replace slots with identity hash table that works on non-dart
+ // objects.
+ var values = [];
+ var copies = [];
+
+ int findSlot(value) {
+ int length = values.length;
+ for (int i = 0; i < length; i++) {
+ if (values[i] === value) return i;
+ }
+ values.add(value);
+ copies.add(null);
+ return length;
+ }
+ readSlot(int i) => copies[i];
+ writeSlot(int i, x) { copies[i] = x; }
+
+ walk(e) {
+ if (e == null) return e;
+ if (e is bool) return e;
+ if (e is num) return e;
+ if (e is String) return e;
+
+ if (_isJavaScriptDate(e)) {
+ // TODO(sra).
+ throw const NotImplementedException('structured clone of Date');
+ }
+
+ if (_isJavaScriptRegExp(e)) {
+ // TODO(sra).
+ throw const NotImplementedException('structured clone of RegExp');
+ }
+
+ if (_isJavaScriptSimpleObject(e)) {
+ // TODO(sra): Swizzle the prototype for one of a Map implementation that
+ // uses the properies as storage.
+ var slot = findSlot(e);
+ var copy = readSlot(slot);
+ if (copy != null) return copy;
+ copy = {};
+
+ writeSlot(slot, copy);
+ for (final key in JS('List', 'Object.keys(#)', e)) {
+ copy[key] = walk(JS('var', '#[#]', e, key));
+ }
+ return copy;
+ }
+
+ if (_isJavaScriptArray(e)) {
+ // Since a JavaScript Array is an instance of Dart List, we can modify it
+ // in-place.
+ var slot = findSlot(e);
+ var copy = readSlot(slot);
+ if (copy != null) return copy;
+ writeSlot(slot, e);
+
+ int length = e.length;
+ for (int i = 0; i < length; i++) {
+ e[i] = walk(e[i]);
+ }
+ return e;
+ }
+
+ // Assume anything else is already a valid Dart object, either by having
+ // already been processed, or e.g. a clonable native class.
+ return e;
+ }
+
+ var copy = walk(object);
+ return copy;
+}
+
+
+bool _isJavaScriptDate(value) => JS('bool', '# instanceof Date', value);
+bool _isJavaScriptRegExp(value) => JS('bool', '# instanceof RegExp', value);
+bool _isJavaScriptArray(value) => JS('bool', '# instanceof Array', value);
+bool _isJavaScriptSimpleObject(value) =>
+ JS('bool', 'Object.getPrototypeOf(#) === Object.prototype', value);
+bool _isImmutableJavaScriptArray(value) =>
+ JS('bool', @'!!(#.immutable$list)', value);
// 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.
« no previous file with comments | « lib/dom/scripts/generator.py ('k') | lib/html/src/dart2js_Conversions.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698