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

Side by Side Diff: lib/html/src/dart2js_Conversions.dart

Issue 11065003: Convert between Dart and JavaScript SerializedScriptValues on postMessage. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « lib/html/scripts/generator.py ('k') | tests/html/postmessage_structured_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 May modify original. If so, action is idempotent.
vsm 2012/10/05 17:22:17 This comment looks incomplete. Can you fix?
143 _convertNativeToDart_SerializedScriptValue(object) {
144 return _convertNativeToDart_AcceptStructuredClone(object, mustCopy: true);
145 }
146
142 147
143 /** 148 /**
144 * Converts a Dart value into a JavaScript SerializedScriptValue. Returns the 149 * Converts a Dart value into a JavaScript SerializedScriptValue. Returns the
145 * original input or a functional 'copy'. Does not mutate the original. 150 * original input or a functional 'copy'. Does not mutate the original.
146 * 151 *
147 * The main transformation is the translation of Dart Maps are converted to 152 * The main transformation is the translation of Dart Maps are converted to
148 * JavaScript Objects. 153 * JavaScript Objects.
149 * 154 *
150 * The algorithm is essentially a dry-run of the structured clone algorithm 155 * The algorithm is essentially a dry-run of the structured clone algorithm
151 * described at 156 * described at
152 * http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interf aces.html#structured-clone 157 * 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 158 * https://www.khronos.org/registry/typedarray/specs/latest/#9
154 * 159 *
160 * Since the result of this function is expected to be passed only to JavaScript
161 * operations that perform the structured clone algorithm which does not mutate
162 * its output, the result may share structure with the input [value].
155 */ 163 */
156 _convertDartToNative_PrepareForStructuredClone(value) { 164 _convertDartToNative_PrepareForStructuredClone(value) {
157 165
158 // TODO(sra): Replace slots with identity hash table. 166 // TODO(sra): Replace slots with identity hash table.
159 var values = []; 167 var values = [];
160 var copies = []; // initially 'null', 'true' during initial DFS, then a copy. 168 var copies = []; // initially 'null', 'true' during initial DFS, then a copy.
161 169
162 int findSlot(value) { 170 int findSlot(value) {
163 int length = values.length; 171 int length = values.length;
164 for (int i = 0; i < length; i++) { 172 for (int i = 0; i < length; i++) {
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 } 303 }
296 304
297 var copy = walk(value); 305 var copy = walk(value);
298 cleanupSlots(); 306 cleanupSlots();
299 return copy; 307 return copy;
300 } 308 }
301 309
302 /** 310 /**
303 * Converts a native value into a Dart object. 311 * Converts a native value into a Dart object.
304 * 312 *
305 * May return the original input. May mutate the original input (but will be 313 * If [mustCopy] is [:false:], may return the original input. May mutate the
306 * idempotent if mutation occurs). It is assumed that this conversion happens 314 * original input (but will be idempotent if mutation occurs). It is assumed
307 * on native serializable script values such values from native DOM calls. 315 * that this conversion happens on native serializable script values such values
316 * from native DOM calls.
308 * 317 *
309 * [object] is the result of a structured clone operation. 318 * [object] is the result of a structured clone operation.
310 * 319 *
311 * If necessary, JavaScript Dates are converted into Dart Dates. 320 * If necessary, JavaScript Dates are converted into Dart Dates.
321 *
322 * If [mustCopy] is [:true:], the entire object is copied and the original input
323 * is not mutated. This should be the case where Dart and JavaScript code can
324 * access the value, for example, via multiple event listeners for
325 * MessageEvents. Mutating the object to make it more 'Dart-like' would corrupt
326 * the value as seen from the JavaScript listeners.
312 */ 327 */
313 _convertNativeToDart_AcceptStructuredClone(object) { 328 _convertNativeToDart_AcceptStructuredClone(object, {mustCopy = false}) {
314 329
315 // TODO(sra): Replace slots with identity hash table that works on non-dart 330 // TODO(sra): Replace slots with identity hash table that works on non-dart
316 // objects. 331 // objects.
317 var values = []; 332 var values = [];
318 var copies = []; 333 var copies = [];
319 334
320 int findSlot(value) { 335 int findSlot(value) {
321 int length = values.length; 336 int length = values.length;
322 for (int i = 0; i < length; i++) { 337 for (int i = 0; i < length; i++) {
323 if (values[i] === value) return i; 338 if (values[i] === value) return i;
(...skipping 15 matching lines...) Expand all
339 // TODO(sra). 354 // TODO(sra).
340 throw const NotImplementedException('structured clone of Date'); 355 throw const NotImplementedException('structured clone of Date');
341 } 356 }
342 357
343 if (_isJavaScriptRegExp(e)) { 358 if (_isJavaScriptRegExp(e)) {
344 // TODO(sra). 359 // TODO(sra).
345 throw const NotImplementedException('structured clone of RegExp'); 360 throw const NotImplementedException('structured clone of RegExp');
346 } 361 }
347 362
348 if (_isJavaScriptSimpleObject(e)) { 363 if (_isJavaScriptSimpleObject(e)) {
349 // TODO(sra): Swizzle the prototype for one of a Map implementation that 364 // TODO(sra): If mustCopy is false, swizzle the prototype for one of a Map
350 // uses the properies as storage. 365 // implementation that uses the properies as storage.
351 var slot = findSlot(e); 366 var slot = findSlot(e);
352 var copy = readSlot(slot); 367 var copy = readSlot(slot);
353 if (copy != null) return copy; 368 if (copy != null) return copy;
354 copy = {}; 369 copy = {};
355 370
356 writeSlot(slot, copy); 371 writeSlot(slot, copy);
357 for (final key in JS('List', 'Object.keys(#)', e)) { 372 for (final key in JS('List', 'Object.keys(#)', e)) {
358 copy[key] = walk(JS('var', '#[#]', e, key)); 373 copy[key] = walk(JS('var', '#[#]', e, key));
359 } 374 }
360 return copy; 375 return copy;
361 } 376 }
362 377
363 if (_isJavaScriptArray(e)) { 378 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); 379 var slot = findSlot(e);
367 var copy = readSlot(slot); 380 var copy = readSlot(slot);
368 if (copy != null) return copy; 381 if (copy != null) return copy;
369 writeSlot(slot, e);
370 382
371 int length = e.length; 383 int length = e.length;
384 // Since a JavaScript Array is an instance of Dart List, we can modify it
385 // in-place unless we must copy.
386 copy = mustCopy ? JS('List', 'new Array(#)', length) : e;
387 writeSlot(slot, copy);
388
372 for (int i = 0; i < length; i++) { 389 for (int i = 0; i < length; i++) {
373 e[i] = walk(e[i]); 390 copy[i] = walk(e[i]);
374 } 391 }
375 return e; 392 return copy;
376 } 393 }
377 394
378 // Assume anything else is already a valid Dart object, either by having 395 // Assume anything else is already a valid Dart object, either by having
379 // already been processed, or e.g. a clonable native class. 396 // already been processed, or e.g. a clonable native class.
380 return e; 397 return e;
381 } 398 }
382 399
383 var copy = walk(object); 400 var copy = walk(object);
384 return copy; 401 return copy;
385 } 402 }
386 403
387 404
388 bool _isJavaScriptDate(value) => JS('bool', '# instanceof Date', value); 405 bool _isJavaScriptDate(value) => JS('bool', '# instanceof Date', value);
389 bool _isJavaScriptRegExp(value) => JS('bool', '# instanceof RegExp', value); 406 bool _isJavaScriptRegExp(value) => JS('bool', '# instanceof RegExp', value);
390 bool _isJavaScriptArray(value) => JS('bool', '# instanceof Array', value); 407 bool _isJavaScriptArray(value) => JS('bool', '# instanceof Array', value);
391 bool _isJavaScriptSimpleObject(value) => 408 bool _isJavaScriptSimpleObject(value) =>
392 JS('bool', 'Object.getPrototypeOf(#) === Object.prototype', value); 409 JS('bool', 'Object.getPrototypeOf(#) === Object.prototype', value);
393 bool _isImmutableJavaScriptArray(value) => 410 bool _isImmutableJavaScriptArray(value) =>
394 JS('bool', r'!!(#.immutable$list)', value); 411 JS('bool', r'!!(#.immutable$list)', value);
OLDNEW
« no previous file with comments | « lib/html/scripts/generator.py ('k') | tests/html/postmessage_structured_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698