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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 * If necessary, [dartKey] may be copied to ensure all lists are converted into | 122 * If necessary, [dartKey] may be copied to ensure all lists are converted into |
123 * JavaScript Arrays and Dart Dates into JavaScript Dates. | 123 * JavaScript Arrays and Dart Dates into JavaScript Dates. |
124 */ | 124 */ |
125 _convertDartToNative_IDBKey(dartKey) { | 125 _convertDartToNative_IDBKey(dartKey) { |
126 // TODO: Implement. | 126 // TODO: Implement. |
127 return dartKey; | 127 return dartKey; |
128 } | 128 } |
129 | 129 |
130 | 130 |
131 | 131 |
132 // May modify original. If so, action is idempotent. | 132 /// May modify original. If so, action is idempotent. |
133 _convertNativeToDart_IDBAny(object) { | 133 _convertNativeToDart_IDBAny(object) { |
134 return _convertNativeToDart_AcceptStructuredClone(object); | 134 return _convertNativeToDart_AcceptStructuredClone(object, mustCopy: false); |
135 } | 135 } |
136 | 136 |
137 /// Converts a Dart value into | 137 /// Converts a Dart value into a JavaScript SerializedScriptValue. |
138 _convertDartToNative_SerializedScriptValue(value) { | 138 _convertDartToNative_SerializedScriptValue(value) { |
139 return _convertDartToNative_PrepareForStructuredClone(value); | 139 return _convertDartToNative_PrepareForStructuredClone(value); |
140 } | 140 } |
141 | 141 |
| 142 /// Since the source object may be viewed via a JavaScript event listener the |
| 143 /// original may not be modified. |
| 144 _convertNativeToDart_SerializedScriptValue(object) { |
| 145 return _convertNativeToDart_AcceptStructuredClone(object, mustCopy: true); |
| 146 } |
| 147 |
142 | 148 |
143 /** | 149 /** |
144 * Converts a Dart value into a JavaScript SerializedScriptValue. Returns the | 150 * Converts a Dart value into a JavaScript SerializedScriptValue. Returns the |
145 * original input or a functional 'copy'. Does not mutate the original. | 151 * original input or a functional 'copy'. Does not mutate the original. |
146 * | 152 * |
147 * The main transformation is the translation of Dart Maps are converted to | 153 * The main transformation is the translation of Dart Maps are converted to |
148 * JavaScript Objects. | 154 * JavaScript Objects. |
149 * | 155 * |
150 * The algorithm is essentially a dry-run of the structured clone algorithm | 156 * The algorithm is essentially a dry-run of the structured clone algorithm |
151 * described at | 157 * described at |
152 * http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interf
aces.html#structured-clone | 158 * http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interf
aces.html#structured-clone |
153 * https://www.khronos.org/registry/typedarray/specs/latest/#9 | 159 * https://www.khronos.org/registry/typedarray/specs/latest/#9 |
154 * | 160 * |
| 161 * Since the result of this function is expected to be passed only to JavaScript |
| 162 * operations that perform the structured clone algorithm which does not mutate |
| 163 * its output, the result may share structure with the input [value]. |
155 */ | 164 */ |
156 _convertDartToNative_PrepareForStructuredClone(value) { | 165 _convertDartToNative_PrepareForStructuredClone(value) { |
157 | 166 |
158 // TODO(sra): Replace slots with identity hash table. | 167 // TODO(sra): Replace slots with identity hash table. |
159 var values = []; | 168 var values = []; |
160 var copies = []; // initially 'null', 'true' during initial DFS, then a copy. | 169 var copies = []; // initially 'null', 'true' during initial DFS, then a copy. |
161 | 170 |
162 int findSlot(value) { | 171 int findSlot(value) { |
163 int length = values.length; | 172 int length = values.length; |
164 for (int i = 0; i < length; i++) { | 173 for (int i = 0; i < length; i++) { |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 } | 304 } |
296 | 305 |
297 var copy = walk(value); | 306 var copy = walk(value); |
298 cleanupSlots(); | 307 cleanupSlots(); |
299 return copy; | 308 return copy; |
300 } | 309 } |
301 | 310 |
302 /** | 311 /** |
303 * Converts a native value into a Dart object. | 312 * Converts a native value into a Dart object. |
304 * | 313 * |
305 * May return the original input. May mutate the original input (but will be | 314 * If [mustCopy] is [:false:], may return the original input. May mutate the |
306 * idempotent if mutation occurs). It is assumed that this conversion happens | 315 * original input (but will be idempotent if mutation occurs). It is assumed |
307 * on native serializable script values such values from native DOM calls. | 316 * that this conversion happens on native serializable script values such values |
| 317 * from native DOM calls. |
308 * | 318 * |
309 * [object] is the result of a structured clone operation. | 319 * [object] is the result of a structured clone operation. |
310 * | 320 * |
311 * If necessary, JavaScript Dates are converted into Dart Dates. | 321 * If necessary, JavaScript Dates are converted into Dart Dates. |
| 322 * |
| 323 * If [mustCopy] is [:true:], the entire object is copied and the original input |
| 324 * is not mutated. This should be the case where Dart and JavaScript code can |
| 325 * access the value, for example, via multiple event listeners for |
| 326 * MessageEvents. Mutating the object to make it more 'Dart-like' would corrupt |
| 327 * the value as seen from the JavaScript listeners. |
312 */ | 328 */ |
313 _convertNativeToDart_AcceptStructuredClone(object) { | 329 _convertNativeToDart_AcceptStructuredClone(object, {mustCopy = false}) { |
314 | 330 |
315 // TODO(sra): Replace slots with identity hash table that works on non-dart | 331 // TODO(sra): Replace slots with identity hash table that works on non-dart |
316 // objects. | 332 // objects. |
317 var values = []; | 333 var values = []; |
318 var copies = []; | 334 var copies = []; |
319 | 335 |
320 int findSlot(value) { | 336 int findSlot(value) { |
321 int length = values.length; | 337 int length = values.length; |
322 for (int i = 0; i < length; i++) { | 338 for (int i = 0; i < length; i++) { |
323 if (values[i] === value) return i; | 339 if (values[i] === value) return i; |
(...skipping 15 matching lines...) Expand all Loading... |
339 // TODO(sra). | 355 // TODO(sra). |
340 throw const NotImplementedException('structured clone of Date'); | 356 throw const NotImplementedException('structured clone of Date'); |
341 } | 357 } |
342 | 358 |
343 if (_isJavaScriptRegExp(e)) { | 359 if (_isJavaScriptRegExp(e)) { |
344 // TODO(sra). | 360 // TODO(sra). |
345 throw const NotImplementedException('structured clone of RegExp'); | 361 throw const NotImplementedException('structured clone of RegExp'); |
346 } | 362 } |
347 | 363 |
348 if (_isJavaScriptSimpleObject(e)) { | 364 if (_isJavaScriptSimpleObject(e)) { |
349 // TODO(sra): Swizzle the prototype for one of a Map implementation that | 365 // TODO(sra): If mustCopy is false, swizzle the prototype for one of a Map |
350 // uses the properies as storage. | 366 // implementation that uses the properies as storage. |
351 var slot = findSlot(e); | 367 var slot = findSlot(e); |
352 var copy = readSlot(slot); | 368 var copy = readSlot(slot); |
353 if (copy != null) return copy; | 369 if (copy != null) return copy; |
354 copy = {}; | 370 copy = {}; |
355 | 371 |
356 writeSlot(slot, copy); | 372 writeSlot(slot, copy); |
357 for (final key in JS('List', 'Object.keys(#)', e)) { | 373 for (final key in JS('List', 'Object.keys(#)', e)) { |
358 copy[key] = walk(JS('var', '#[#]', e, key)); | 374 copy[key] = walk(JS('var', '#[#]', e, key)); |
359 } | 375 } |
360 return copy; | 376 return copy; |
361 } | 377 } |
362 | 378 |
363 if (_isJavaScriptArray(e)) { | 379 if (_isJavaScriptArray(e)) { |
364 // Since a JavaScript Array is an instance of Dart List, we can modify it | |
365 // in-place. | |
366 var slot = findSlot(e); | 380 var slot = findSlot(e); |
367 var copy = readSlot(slot); | 381 var copy = readSlot(slot); |
368 if (copy != null) return copy; | 382 if (copy != null) return copy; |
369 writeSlot(slot, e); | |
370 | 383 |
371 int length = e.length; | 384 int length = e.length; |
| 385 // Since a JavaScript Array is an instance of Dart List, we can modify it |
| 386 // in-place unless we must copy. |
| 387 copy = mustCopy ? JS('List', 'new Array(#)', length) : e; |
| 388 writeSlot(slot, copy); |
| 389 |
372 for (int i = 0; i < length; i++) { | 390 for (int i = 0; i < length; i++) { |
373 e[i] = walk(e[i]); | 391 copy[i] = walk(e[i]); |
374 } | 392 } |
375 return e; | 393 return copy; |
376 } | 394 } |
377 | 395 |
378 // Assume anything else is already a valid Dart object, either by having | 396 // Assume anything else is already a valid Dart object, either by having |
379 // already been processed, or e.g. a clonable native class. | 397 // already been processed, or e.g. a clonable native class. |
380 return e; | 398 return e; |
381 } | 399 } |
382 | 400 |
383 var copy = walk(object); | 401 var copy = walk(object); |
384 return copy; | 402 return copy; |
385 } | 403 } |
386 | 404 |
387 | 405 |
388 bool _isJavaScriptDate(value) => JS('bool', '# instanceof Date', value); | 406 bool _isJavaScriptDate(value) => JS('bool', '# instanceof Date', value); |
389 bool _isJavaScriptRegExp(value) => JS('bool', '# instanceof RegExp', value); | 407 bool _isJavaScriptRegExp(value) => JS('bool', '# instanceof RegExp', value); |
390 bool _isJavaScriptArray(value) => JS('bool', '# instanceof Array', value); | 408 bool _isJavaScriptArray(value) => JS('bool', '# instanceof Array', value); |
391 bool _isJavaScriptSimpleObject(value) => | 409 bool _isJavaScriptSimpleObject(value) => |
392 JS('bool', 'Object.getPrototypeOf(#) === Object.prototype', value); | 410 JS('bool', 'Object.getPrototypeOf(#) === Object.prototype', value); |
393 bool _isImmutableJavaScriptArray(value) => | 411 bool _isImmutableJavaScriptArray(value) => |
394 JS('bool', r'!!(#.immutable$list)', value); | 412 JS('bool', r'!!(#.immutable$list)', value); |
OLD | NEW |