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 10 matching lines...) Expand all Loading... |
21 // JavaScript arrays, and any Dates are JavaScript Dates. | 21 // JavaScript arrays, and any Dates are JavaScript Dates. |
22 | 22 |
23 // Conversions for Window. These check if the window is the local | 23 // Conversions for Window. These check if the window is the local |
24 // window, and if it's not, wraps or unwraps it with a secure wrapper. | 24 // window, and if it's not, wraps or unwraps it with a secure wrapper. |
25 // We need to test for EventTarget here as well as it's a base type. | 25 // We need to test for EventTarget here as well as it's a base type. |
26 // We omit an unwrapper for Window as no methods take a non-local | 26 // We omit an unwrapper for Window as no methods take a non-local |
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 | |
32 /// Converts a JavaScript object with properties into a Dart Map. | |
33 /// Not suitable for nested objects. | |
34 Map convertNativeToDart_Dictionary(object) { | |
35 if (object == null) return null; | |
36 var dict = {}; | |
37 var keys = JS('JSExtendableArray', 'Object.getOwnPropertyNames(#)', object); | |
38 for (final key in keys) { | |
39 dict[key] = JS('var', '#[#]', object, key); | |
40 } | |
41 return dict; | |
42 } | |
43 | |
44 /// Converts a flat Dart map into a JavaScript object with properties. | |
45 convertDartToNative_Dictionary(Map dict) { | |
46 if (dict == null) return null; | |
47 var object = JS('var', '{}'); | |
48 dict.forEach((String key, value) { | |
49 JS('void', '#[#] = #', object, key, value); | |
50 }); | |
51 return object; | |
52 } | |
53 | |
54 | |
55 /** | |
56 * Ensures that the input is a JavaScript Array. | |
57 * | |
58 * Creates a new JavaScript array if necessary, otherwise returns the original. | |
59 */ | |
60 List convertDartToNative_StringArray(List<String> input) { | |
61 // TODO(sra). Implement this. | |
62 return input; | |
63 } | |
64 | |
65 DateTime convertNativeToDart_DateTime(date) { | |
66 var millisSinceEpoch = JS('int', '#.getTime()', date); | |
67 return new DateTime.fromMillisecondsSinceEpoch(millisSinceEpoch, isUtc: true); | |
68 } | |
69 | |
70 convertDartToNative_DateTime(DateTime date) { | |
71 return JS('', 'new Date(#)', date.millisecondsSinceEpoch); | |
72 } | |
73 | |
74 | |
75 // ----------------------------------------------------------------------------- | |
76 | |
77 /// Converts a Dart value into a JavaScript SerializedScriptValue. | 31 /// Converts a Dart value into a JavaScript SerializedScriptValue. |
78 convertDartToNative_SerializedScriptValue(value) { | 32 convertDartToNative_SerializedScriptValue(value) { |
79 return _convertDartToNative_PrepareForStructuredClone(value); | 33 return convertDartToNative_PrepareForStructuredClone(value); |
80 } | 34 } |
81 | 35 |
82 /// Since the source object may be viewed via a JavaScript event listener the | 36 /// Since the source object may be viewed via a JavaScript event listener the |
83 /// original may not be modified. | 37 /// original may not be modified. |
84 convertNativeToDart_SerializedScriptValue(object) { | 38 convertNativeToDart_SerializedScriptValue(object) { |
85 return convertNativeToDart_AcceptStructuredClone(object, mustCopy: true); | 39 return convertNativeToDart_AcceptStructuredClone(object, mustCopy: true); |
86 } | 40 } |
87 | 41 |
88 | 42 |
89 /** | 43 /** |
90 * Converts a Dart value into a JavaScript SerializedScriptValue. Returns the | 44 * Converts a Dart value into a JavaScript SerializedScriptValue. Returns the |
91 * original input or a functional 'copy'. Does not mutate the original. | 45 * original input or a functional 'copy'. Does not mutate the original. |
92 * | 46 * |
93 * The main transformation is the translation of Dart Maps are converted to | 47 * The main transformation is the translation of Dart Maps are converted to |
94 * JavaScript Objects. | 48 * JavaScript Objects. |
95 * | 49 * |
96 * The algorithm is essentially a dry-run of the structured clone algorithm | 50 * The algorithm is essentially a dry-run of the structured clone algorithm |
97 * described at | 51 * described at |
98 * http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interf
aces.html#structured-clone | 52 * http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interf
aces.html#structured-clone |
99 * https://www.khronos.org/registry/typedarray/specs/latest/#9 | 53 * https://www.khronos.org/registry/typedarray/specs/latest/#9 |
100 * | 54 * |
101 * Since the result of this function is expected to be passed only to JavaScript | 55 * Since the result of this function is expected to be passed only to JavaScript |
102 * operations that perform the structured clone algorithm which does not mutate | 56 * operations that perform the structured clone algorithm which does not mutate |
103 * its output, the result may share structure with the input [value]. | 57 * its output, the result may share structure with the input [value]. |
104 */ | 58 */ |
105 _convertDartToNative_PrepareForStructuredClone(value) { | 59 abstract class _StructuredClone { |
106 | 60 |
107 // TODO(sra): Replace slots with identity hash table. | 61 // TODO(sra): Replace slots with identity hash table. |
108 var values = []; | 62 var values = []; |
109 var copies = []; // initially 'null', 'true' during initial DFS, then a copy. | 63 var copies = []; // initially 'null', 'true' during initial DFS, then a copy. |
110 | 64 |
111 int findSlot(value) { | 65 int findSlot(value) { |
112 int length = values.length; | 66 int length = values.length; |
113 for (int i = 0; i < length; i++) { | 67 for (int i = 0; i < length; i++) { |
114 if (identical(values[i], value)) return i; | 68 if (identical(values[i], value)) return i; |
115 } | 69 } |
116 values.add(value); | 70 values.add(value); |
117 copies.add(null); | 71 copies.add(null); |
118 return length; | 72 return length; |
119 } | 73 } |
120 readSlot(int i) => copies[i]; | 74 readSlot(int i) => copies[i]; |
121 writeSlot(int i, x) { copies[i] = x; } | 75 writeSlot(int i, x) { copies[i] = x; } |
122 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); |
| 78 newJsMap(); |
| 79 newJsList(length); |
| 80 void putIntoMap(map, key, value); |
123 | 81 |
124 // Returns the input, or a clone of the input. | 82 // Returns the input, or a clone of the input. |
125 walk(e) { | 83 walk(e) { |
126 if (e == null) return e; | 84 if (e == null) return e; |
127 if (e is bool) return e; | 85 if (e is bool) return e; |
128 if (e is num) return e; | 86 if (e is num) return e; |
129 if (e is String) return e; | 87 if (e is String) return e; |
130 if (e is DateTime) { | 88 if (e is DateTime) { |
131 return convertDartToNative_DateTime(e); | 89 return convertDartToNative_DateTime(e); |
132 } | 90 } |
133 if (e is RegExp) { | 91 if (e is RegExp) { |
134 // TODO(sra). | 92 // TODO(sra). |
135 throw new UnimplementedError('structured clone of RegExp'); | 93 throw new UnimplementedError('structured clone of RegExp'); |
136 } | 94 } |
137 | 95 |
138 // The browser's internal structured cloning algorithm will copy certain | 96 // The browser's internal structured cloning algorithm will copy certain |
139 // types of object, but it will copy only its own implementations and not | 97 // types of object, but it will copy only its own implementations and not |
140 // just any Dart implementations of the interface. | 98 // just any Dart implementations of the interface. |
141 | 99 |
142 // TODO(sra): The JavaScript objects suitable for direct cloning by the | 100 // TODO(sra): The JavaScript objects suitable for direct cloning by the |
143 // structured clone algorithm could be tagged with an private interface. | 101 // structured clone algorithm could be tagged with an private interface. |
144 | 102 |
145 if (e is File) return e; | 103 if (e is File) return e; |
146 if (e is Blob) return e; | 104 if (e is Blob) return e; |
147 if (e is FileList) return e; | 105 if (e is FileList) return e; |
148 | 106 |
149 // TODO(sra): Firefox: How to convert _TypedImageData on the other end? | 107 // TODO(sra): Firefox: How to convert _TypedImageData on the other end? |
150 if (e is ImageData) return e; | 108 if (e is ImageData) return e; |
151 if (e is NativeByteBuffer) return e; | 109 if (cloneNotRequired(e)) return e; |
152 | |
153 if (e is NativeTypedData) return e; | |
154 | 110 |
155 if (e is Map) { | 111 if (e is Map) { |
156 var slot = findSlot(e); | 112 var slot = findSlot(e); |
157 var copy = readSlot(slot); | 113 var copy = readSlot(slot); |
158 if (copy != null) return copy; | 114 if (copy != null) return copy; |
159 copy = JS('var', '{}'); | 115 copy = newJsMap(); |
160 writeSlot(slot, copy); | 116 writeSlot(slot, copy); |
161 e.forEach((key, value) { | 117 e.forEach((key, value) { |
162 JS('void', '#[#] = #', copy, key, walk(value)); | 118 putIntoMap(copy, key, walk(value)); |
163 }); | 119 }); |
164 return copy; | 120 return copy; |
165 } | 121 } |
166 | 122 |
167 if (e is List) { | 123 if (e is List) { |
168 // Since a JavaScript Array is an instance of Dart List it is possible to | 124 // Since a JavaScript Array is an instance of Dart List it is tempting |
169 // avoid making a copy of the list if there is no need to copy anything | 125 // in dart2js to avoid making a copy of the list if there is no need |
170 // reachable from the array. We defer creating a new array until a cycle | 126 // to copy anything reachable from the array. However, the list may have |
171 // is detected or a subgraph was copied. | 127 // non-native properties or methods from interceptors and such, e.g. |
172 int length = e.length; | 128 // an immutability marker. So we had to stop doing that. |
173 var slot = findSlot(e); | 129 var slot = findSlot(e); |
174 var copy = readSlot(slot); | 130 var copy = readSlot(slot); |
175 if (copy != null) { | 131 if (copy != null) return copy; |
176 if (true == copy) { // Cycle, so commit to making a copy. | 132 copy = copyList(e, slot); |
177 copy = JS('JSExtendableArray', 'new Array(#)', length); | |
178 writeSlot(slot, copy); | |
179 } | |
180 return copy; | |
181 } | |
182 | |
183 int i = 0; | |
184 | |
185 // Always clone the list, as it may have non-native properties or methods | |
186 // from interceptors and such. | |
187 copy = JS('JSExtendableArray', 'new Array(#)', length); | |
188 writeSlot(slot, copy); | |
189 | |
190 for ( ; i < length; i++) { | |
191 copy[i] = walk(e[i]); | |
192 } | |
193 return copy; | 133 return copy; |
194 } | 134 } |
195 | 135 |
196 throw new UnimplementedError('structured clone of other type'); | 136 throw new UnimplementedError('structured clone of other type'); |
197 } | 137 } |
198 | 138 |
199 var copy = walk(value); | 139 copyList(List e, int slot) { |
200 cleanupSlots(); | 140 int i = 0; |
201 return copy; | 141 int length = e.length; |
| 142 var copy = newJsList(length); |
| 143 writeSlot(slot, copy); |
| 144 for ( ; i < length; i++) { |
| 145 copy[i] = walk(e[i]); |
| 146 } |
| 147 return copy; |
| 148 } |
| 149 |
| 150 convertDartToNative_PrepareForStructuredClone(value) { |
| 151 var copy = walk(value); |
| 152 cleanupSlots(); |
| 153 return copy; |
| 154 } |
202 } | 155 } |
203 | 156 |
204 /** | 157 /** |
205 * Converts a native value into a Dart object. | 158 * Converts a native value into a Dart object. |
206 * | 159 * |
207 * If [mustCopy] is [:false:], may return the original input. May mutate the | 160 * If [mustCopy] is [:false:], may return the original input. May mutate the |
208 * original input (but will be idempotent if mutation occurs). It is assumed | 161 * original input (but will be idempotent if mutation occurs). It is assumed |
209 * that this conversion happens on native serializable script values such values | 162 * that this conversion happens on native serializable script values such values |
210 * from native DOM calls. | 163 * from native DOM calls. |
211 * | 164 * |
212 * [object] is the result of a structured clone operation. | 165 * [object] is the result of a structured clone operation. |
213 * | 166 * |
214 * If necessary, JavaScript Dates are converted into Dart Dates. | 167 * If necessary, JavaScript Dates are converted into Dart Dates. |
215 * | 168 * |
216 * If [mustCopy] is [:true:], the entire object is copied and the original input | 169 * If [mustCopy] is [:true:], the entire object is copied and the original input |
217 * is not mutated. This should be the case where Dart and JavaScript code can | 170 * is not mutated. This should be the case where Dart and JavaScript code can |
218 * access the value, for example, via multiple event listeners for | 171 * access the value, for example, via multiple event listeners for |
219 * MessageEvents. Mutating the object to make it more 'Dart-like' would corrupt | 172 * MessageEvents. Mutating the object to make it more 'Dart-like' would corrupt |
220 * the value as seen from the JavaScript listeners. | 173 * the value as seen from the JavaScript listeners. |
221 */ | 174 */ |
222 convertNativeToDart_AcceptStructuredClone(object, {mustCopy: false}) { | 175 abstract class _AcceptStructuredClone { |
223 | 176 |
224 // TODO(sra): Replace slots with identity hash table that works on non-dart | 177 // TODO(sra): Replace slots with identity hash table. |
225 // objects. | |
226 var values = []; | 178 var values = []; |
227 var copies = []; | 179 var copies = []; // initially 'null', 'true' during initial DFS, then a copy. |
| 180 bool mustCopy = false; |
228 | 181 |
229 int findSlot(value) { | 182 int findSlot(value) { |
230 int length = values.length; | 183 int length = values.length; |
231 for (int i = 0; i < length; i++) { | 184 for (int i = 0; i < length; i++) { |
232 if (identical(values[i], value)) return i; | 185 if (identicalInJs(values[i], value)) return i; |
233 } | 186 } |
234 values.add(value); | 187 values.add(value); |
235 copies.add(null); | 188 copies.add(null); |
236 return length; | 189 return length; |
237 } | 190 } |
| 191 |
| 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. |
| 194 bool identicalInJs(a, b); |
238 readSlot(int i) => copies[i]; | 195 readSlot(int i) => copies[i]; |
239 writeSlot(int i, x) { copies[i] = x; } | 196 writeSlot(int i, x) { copies[i] = x; } |
240 | 197 |
| 198 /// Iterate over the JS properties. |
| 199 forEachJsField(object, action); |
| 200 |
| 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. |
| 203 newDartList(length); |
| 204 |
241 walk(e) { | 205 walk(e) { |
242 if (e == null) return e; | 206 if (e == null) return e; |
243 if (e is bool) return e; | 207 if (e is bool) return e; |
244 if (e is num) return e; | 208 if (e is num) return e; |
245 if (e is String) return e; | 209 if (e is String) return e; |
246 | 210 |
247 if (isJavaScriptDate(e)) { | 211 if (isJavaScriptDate(e)) { |
248 return convertNativeToDart_DateTime(e); | 212 return convertNativeToDart_DateTime(e); |
249 } | 213 } |
250 | 214 |
251 if (isJavaScriptRegExp(e)) { | 215 if (isJavaScriptRegExp(e)) { |
252 // TODO(sra). | 216 // TODO(sra). |
253 throw new UnimplementedError('structured clone of RegExp'); | 217 throw new UnimplementedError('structured clone of RegExp'); |
254 } | 218 } |
255 | 219 |
| 220 if (isJavaScriptPromise(e)) { |
| 221 return convertNativePromiseToDartFuture(e); |
| 222 } |
| 223 |
256 if (isJavaScriptSimpleObject(e)) { | 224 if (isJavaScriptSimpleObject(e)) { |
257 // TODO(sra): If mustCopy is false, swizzle the prototype for one of a Map | 225 // TODO(sra): If mustCopy is false, swizzle the prototype for one of a Map |
258 // implementation that uses the properies as storage. | 226 // implementation that uses the properies as storage. |
259 var slot = findSlot(e); | 227 var slot = findSlot(e); |
260 var copy = readSlot(slot); | 228 var copy = readSlot(slot); |
261 if (copy != null) return copy; | 229 if (copy != null) return copy; |
262 copy = {}; | 230 copy = {}; |
263 | 231 |
264 writeSlot(slot, copy); | 232 writeSlot(slot, copy); |
265 for (final key in JS('JSExtendableArray', 'Object.keys(#)', e)) { | 233 forEachJsField(e, (key, value) => copy[key] = walk(value)); |
266 copy[key] = walk(JS('var', '#[#]', e, key)); | |
267 } | |
268 return copy; | 234 return copy; |
269 } | 235 } |
270 | 236 |
271 if (isJavaScriptArray(e)) { | 237 if (isJavaScriptArray(e)) { |
272 var slot = findSlot(e); | 238 var slot = findSlot(e); |
273 var copy = readSlot(slot); | 239 var copy = readSlot(slot); |
274 if (copy != null) return copy; | 240 if (copy != null) return copy; |
275 | 241 |
276 int length = e.length; | 242 int length = e.length; |
277 // Since a JavaScript Array is an instance of Dart List, we can modify it | 243 // Since a JavaScript Array is an instance of Dart List, we can modify it |
278 // in-place unless we must copy. | 244 // in-place unless we must copy. |
279 copy = mustCopy ? JS('JSExtendableArray', 'new Array(#)', length) : e; | 245 copy = mustCopy ? newDartList(length) : e; |
280 writeSlot(slot, copy); | 246 writeSlot(slot, copy); |
281 | 247 |
282 for (int i = 0; i < length; i++) { | 248 for (int i = 0; i < length; i++) { |
283 copy[i] = walk(e[i]); | 249 copy[i] = walk(e[i]); |
284 } | 250 } |
285 return copy; | 251 return copy; |
286 } | 252 } |
287 | 253 |
288 // Assume anything else is already a valid Dart object, either by having | 254 // Assume anything else is already a valid Dart object, either by having |
289 // already been processed, or e.g. a clonable native class. | 255 // already been processed, or e.g. a clonable native class. |
290 return e; | 256 return e; |
291 } | 257 } |
292 | 258 |
293 var copy = walk(object); | 259 convertNativeToDart_AcceptStructuredClone(object, {mustCopy: false}) { |
294 return copy; | 260 this.mustCopy = mustCopy; |
| 261 var copy = walk(object); |
| 262 return copy; |
| 263 } |
295 } | 264 } |
296 | 265 |
297 // Conversions for ContextAttributes. | 266 // Conversions for ContextAttributes. |
298 // | 267 // |
299 // On Firefox, the returned ContextAttributes is a plain object. | 268 // On Firefox, the returned ContextAttributes is a plain object. |
300 class _TypedContextAttributes implements gl.ContextAttributes { | 269 class _TypedContextAttributes implements gl.ContextAttributes { |
301 bool alpha; | 270 bool alpha; |
302 bool antialias; | 271 bool antialias; |
303 bool depth; | 272 bool depth; |
304 bool premultipliedAlpha; | 273 bool premultipliedAlpha; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 // We can get rid of this conversion if _TypedImageData implements the fields | 350 // We can get rid of this conversion if _TypedImageData implements the fields |
382 // with native names. | 351 // with native names. |
383 convertDartToNative_ImageData(ImageData imageData) { | 352 convertDartToNative_ImageData(ImageData imageData) { |
384 if (imageData is _TypedImageData) { | 353 if (imageData is _TypedImageData) { |
385 return JS('', '{data: #, height: #, width: #}', | 354 return JS('', '{data: #, height: #, width: #}', |
386 imageData.data, imageData.height, imageData.width); | 355 imageData.data, imageData.height, imageData.width); |
387 } | 356 } |
388 return imageData; | 357 return imageData; |
389 } | 358 } |
390 | 359 |
391 | |
392 bool isJavaScriptDate(value) => JS('bool', '# instanceof Date', value); | |
393 bool isJavaScriptRegExp(value) => JS('bool', '# instanceof RegExp', value); | |
394 bool isJavaScriptArray(value) => JS('bool', '# instanceof Array', value); | |
395 bool isJavaScriptSimpleObject(value) { | |
396 var proto = JS('', 'Object.getPrototypeOf(#)', value); | |
397 return JS('bool', '# === Object.prototype', proto) || | |
398 JS('bool', '# === null', proto); | |
399 } | |
400 bool isImmutableJavaScriptArray(value) => | |
401 JS('bool', r'!!(#.immutable$list)', value); | |
402 | |
403 | |
404 | |
405 const String _serializedScriptValue = | 360 const String _serializedScriptValue = |
406 'num|String|bool|' | 361 'num|String|bool|' |
407 'JSExtendableArray|=Object|' | 362 'JSExtendableArray|=Object|' |
408 'Blob|File|NativeByteBuffer|NativeTypedData' | 363 'Blob|File|NativeByteBuffer|NativeTypedData' |
409 // TODO(sra): Add Date, RegExp. | 364 // TODO(sra): Add Date, RegExp. |
410 ; | 365 ; |
411 const annotation_Creates_SerializedScriptValue = | 366 const annotation_Creates_SerializedScriptValue = |
412 const Creates(_serializedScriptValue); | 367 const Creates(_serializedScriptValue); |
413 const annotation_Returns_SerializedScriptValue = | 368 const annotation_Returns_SerializedScriptValue = |
414 const Returns(_serializedScriptValue); | 369 const Returns(_serializedScriptValue); |
OLD | NEW |