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

Side by Side Diff: pkg/dev_compiler/tool/input_sdk/lib/html/html_common/conversions.dart

Issue 2752163002: Format all dart dev compiler files (Closed)
Patch Set: Created 3 years, 9 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
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
6 // Conversions for IDBKey. 5 // Conversions for IDBKey.
7 // 6 //
8 // Per http://www.w3.org/TR/IndexedDB/#key-construct 7 // Per http://www.w3.org/TR/IndexedDB/#key-construct
9 // 8 //
10 // "A value is said to be a valid key if it is one of the following types: Array 9 // "A value is said to be a valid key if it is one of the following types: Array
11 // JavaScript objects [ECMA-262], DOMString [WEBIDL], Date [ECMA-262] or float 10 // JavaScript objects [ECMA-262], DOMString [WEBIDL], Date [ECMA-262] or float
12 // [WEBIDL]. However Arrays are only valid keys if every item in the array is 11 // [WEBIDL]. However Arrays are only valid keys if every item in the array is
13 // defined and is a valid key (i.e. sparse arrays can not be valid keys) and if 12 // defined and is a valid key (i.e. sparse arrays can not be valid keys) and if
14 // the Array doesn't directly or indirectly contain itself. Any non-numeric 13 // the Array doesn't directly or indirectly contain itself. Any non-numeric
15 // properties are ignored, and thus does not affect whether the Array is a valid 14 // properties are ignored, and thus does not affect whether the Array is a valid
(...skipping 16 matching lines...) Expand all
32 convertDartToNative_SerializedScriptValue(value) { 31 convertDartToNative_SerializedScriptValue(value) {
33 return convertDartToNative_PrepareForStructuredClone(value); 32 return convertDartToNative_PrepareForStructuredClone(value);
34 } 33 }
35 34
36 /// Since the source object may be viewed via a JavaScript event listener the 35 /// Since the source object may be viewed via a JavaScript event listener the
37 /// original may not be modified. 36 /// original may not be modified.
38 convertNativeToDart_SerializedScriptValue(object) { 37 convertNativeToDart_SerializedScriptValue(object) {
39 return convertNativeToDart_AcceptStructuredClone(object, mustCopy: true); 38 return convertNativeToDart_AcceptStructuredClone(object, mustCopy: true);
40 } 39 }
41 40
42
43 /** 41 /**
44 * Converts a Dart value into a JavaScript SerializedScriptValue. Returns the 42 * Converts a Dart value into a JavaScript SerializedScriptValue. Returns the
45 * original input or a functional 'copy'. Does not mutate the original. 43 * original input or a functional 'copy'. Does not mutate the original.
46 * 44 *
47 * The main transformation is the translation of Dart Maps are converted to 45 * The main transformation is the translation of Dart Maps are converted to
48 * JavaScript Objects. 46 * JavaScript Objects.
49 * 47 *
50 * The algorithm is essentially a dry-run of the structured clone algorithm 48 * The algorithm is essentially a dry-run of the structured clone algorithm
51 * described at 49 * described at
52 * http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interf aces.html#structured-clone 50 * http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interf aces.html#structured-clone
53 * https://www.khronos.org/registry/typedarray/specs/latest/#9 51 * https://www.khronos.org/registry/typedarray/specs/latest/#9
54 * 52 *
55 * Since the result of this function is expected to be passed only to JavaScript 53 * Since the result of this function is expected to be passed only to JavaScript
56 * operations that perform the structured clone algorithm which does not mutate 54 * operations that perform the structured clone algorithm which does not mutate
57 * its output, the result may share structure with the input [value]. 55 * its output, the result may share structure with the input [value].
58 */ 56 */
59 abstract class _StructuredClone { 57 abstract class _StructuredClone {
60
61 // TODO(sra): Replace slots with identity hash table. 58 // TODO(sra): Replace slots with identity hash table.
62 var values = []; 59 var values = [];
63 var copies = []; // initially 'null', 'true' during initial DFS, then a copy. 60 var copies = []; // initially 'null', 'true' during initial DFS, then a copy.
64 61
65 int findSlot(value) { 62 int findSlot(value) {
66 int length = values.length; 63 int length = values.length;
67 for (int i = 0; i < length; i++) { 64 for (int i = 0; i < length; i++) {
68 if (identical(values[i], value)) return i; 65 if (identical(values[i], value)) return i;
69 } 66 }
70 values.add(value); 67 values.add(value);
71 copies.add(null); 68 copies.add(null);
72 return length; 69 return length;
73 } 70 }
71
74 readSlot(int i) => copies[i]; 72 readSlot(int i) => copies[i];
75 writeSlot(int i, x) { copies[i] = x; } 73 writeSlot(int i, x) {
76 cleanupSlots() {} // Will be needed if we mark objects with a property. 74 copies[i] = x;
75 }
76
77 cleanupSlots() {} // Will be needed if we mark objects with a property.
77 bool cloneNotRequired(object); 78 bool cloneNotRequired(object);
78 newJsMap(); 79 newJsMap();
79 List newJsList(length); 80 List newJsList(length);
80 void putIntoMap(map, key, value); 81 void putIntoMap(map, key, value);
81 82
82 // Returns the input, or a clone of the input. 83 // Returns the input, or a clone of the input.
83 walk(e) { 84 walk(e) {
84 if (e == null) return e; 85 if (e == null) return e;
85 if (e is bool) return e; 86 if (e is bool) return e;
86 if (e is num) return e; 87 if (e is num) return e;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 } 135 }
135 136
136 throw new UnimplementedError('structured clone of other type'); 137 throw new UnimplementedError('structured clone of other type');
137 } 138 }
138 139
139 List copyList(List e, int slot) { 140 List copyList(List e, int slot) {
140 int i = 0; 141 int i = 0;
141 int length = e.length; 142 int length = e.length;
142 var copy = newJsList(length); 143 var copy = newJsList(length);
143 writeSlot(slot, copy); 144 writeSlot(slot, copy);
144 for ( ; i < length; i++) { 145 for (; i < length; i++) {
145 copy[i] = walk(e[i]); 146 copy[i] = walk(e[i]);
146 } 147 }
147 return copy; 148 return copy;
148 } 149 }
149 150
150 convertDartToNative_PrepareForStructuredClone(value) { 151 convertDartToNative_PrepareForStructuredClone(value) {
151 var copy = walk(value); 152 var copy = walk(value);
152 cleanupSlots(); 153 cleanupSlots();
153 return copy; 154 return copy;
154 } 155 }
(...skipping 11 matching lines...) Expand all
166 * 167 *
167 * If necessary, JavaScript Dates are converted into Dart Dates. 168 * If necessary, JavaScript Dates are converted into Dart Dates.
168 * 169 *
169 * If [mustCopy] is [:true:], the entire object is copied and the original input 170 * If [mustCopy] is [:true:], the entire object is copied and the original input
170 * is not mutated. This should be the case where Dart and JavaScript code can 171 * is not mutated. This should be the case where Dart and JavaScript code can
171 * access the value, for example, via multiple event listeners for 172 * access the value, for example, via multiple event listeners for
172 * MessageEvents. Mutating the object to make it more 'Dart-like' would corrupt 173 * MessageEvents. Mutating the object to make it more 'Dart-like' would corrupt
173 * the value as seen from the JavaScript listeners. 174 * the value as seen from the JavaScript listeners.
174 */ 175 */
175 abstract class _AcceptStructuredClone { 176 abstract class _AcceptStructuredClone {
176
177 // TODO(sra): Replace slots with identity hash table. 177 // TODO(sra): Replace slots with identity hash table.
178 var values = []; 178 var values = [];
179 var copies = []; // initially 'null', 'true' during initial DFS, then a copy. 179 var copies = []; // initially 'null', 'true' during initial DFS, then a copy.
180 bool mustCopy = false; 180 bool mustCopy = false;
181 181
182 int findSlot(value) { 182 int findSlot(value) {
183 int length = values.length; 183 int length = values.length;
184 for (int i = 0; i < length; i++) { 184 for (int i = 0; i < length; i++) {
185 if (identicalInJs(values[i], value)) return i; 185 if (identicalInJs(values[i], value)) return i;
186 } 186 }
187 values.add(value); 187 values.add(value);
188 copies.add(null); 188 copies.add(null);
189 return length; 189 return length;
190 } 190 }
191 191
192 /// Are the two objects identical, but taking into account that two JsObject 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. 193 /// wrappers may not be identical, but their underlying Js Object might be.
194 bool identicalInJs(a, b); 194 bool identicalInJs(a, b);
195 readSlot(int i) => copies[i]; 195 readSlot(int i) => copies[i];
196 writeSlot(int i, x) { copies[i] = x; } 196 writeSlot(int i, x) {
197 copies[i] = x;
198 }
197 199
198 /// Iterate over the JS properties. 200 /// Iterate over the JS properties.
199 forEachJsField(object, action(key, value)); 201 forEachJsField(object, action(key, value));
200 202
201 /// Create a new Dart list of the given length. May create a native List or 203 /// 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. 204 /// a JsArray, depending if we're in Dartium or dart2js.
203 List newDartList(length); 205 List newDartList(length);
204 206
205 walk(e) { 207 walk(e) {
206 if (e == null) return e; 208 if (e == null) return e;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 // On Firefox, the returned ContextAttributes is a plain object. 271 // On Firefox, the returned ContextAttributes is a plain object.
270 class ContextAttributes { 272 class ContextAttributes {
271 bool alpha; 273 bool alpha;
272 bool antialias; 274 bool antialias;
273 bool depth; 275 bool depth;
274 bool premultipliedAlpha; 276 bool premultipliedAlpha;
275 bool preserveDrawingBuffer; 277 bool preserveDrawingBuffer;
276 bool stencil; 278 bool stencil;
277 bool failIfMajorPerformanceCaveat; 279 bool failIfMajorPerformanceCaveat;
278 280
279 ContextAttributes(this.alpha, this.antialias, this.depth, 281 ContextAttributes(
280 this.failIfMajorPerformanceCaveat, this.premultipliedAlpha, 282 this.alpha,
281 this.preserveDrawingBuffer, this.stencil); 283 this.antialias,
284 this.depth,
285 this.failIfMajorPerformanceCaveat,
286 this.premultipliedAlpha,
287 this.preserveDrawingBuffer,
288 this.stencil);
282 } 289 }
283 290
284 convertNativeToDart_ContextAttributes(nativeContextAttributes) { 291 convertNativeToDart_ContextAttributes(nativeContextAttributes) {
285 // On Firefox the above test fails because ContextAttributes is a plain 292 // On Firefox the above test fails because ContextAttributes is a plain
286 // object so we create a _TypedContextAttributes. 293 // object so we create a _TypedContextAttributes.
287 294
288 return new ContextAttributes( 295 return new ContextAttributes(
289 JS('var', '#.alpha', nativeContextAttributes), 296 JS('var', '#.alpha', nativeContextAttributes),
290 JS('var', '#.antialias', nativeContextAttributes), 297 JS('var', '#.antialias', nativeContextAttributes),
291 JS('var', '#.depth', nativeContextAttributes), 298 JS('var', '#.depth', nativeContextAttributes),
292 JS('var', '#.failIfMajorPerformanceCaveat', nativeContextAttributes), 299 JS('var', '#.failIfMajorPerformanceCaveat', nativeContextAttributes),
293 JS('var', '#.premultipliedAlpha', nativeContextAttributes), 300 JS('var', '#.premultipliedAlpha', nativeContextAttributes),
294 JS('var', '#.preserveDrawingBuffer', nativeContextAttributes), 301 JS('var', '#.preserveDrawingBuffer', nativeContextAttributes),
295 JS('var', '#.stencil', nativeContextAttributes)); 302 JS('var', '#.stencil', nativeContextAttributes));
296 } 303 }
297 304
298 // Conversions for ImageData 305 // Conversions for ImageData
299 // 306 //
300 // On Firefox, the returned ImageData is a plain object. 307 // On Firefox, the returned ImageData is a plain object.
301 308
302 class _TypedImageData implements ImageData { 309 class _TypedImageData implements ImageData {
303 final Uint8ClampedList data; 310 final Uint8ClampedList data;
304 final int height; 311 final int height;
305 final int width; 312 final int width;
306 313
307 _TypedImageData(this.data, this.height, this.width); 314 _TypedImageData(this.data, this.height, this.width);
308 } 315 }
309 316
310 ImageData convertNativeToDart_ImageData(nativeImageData) { 317 ImageData convertNativeToDart_ImageData(nativeImageData) {
311
312 // None of the native getters that return ImageData are declared as returning 318 // None of the native getters that return ImageData are declared as returning
313 // [ImageData] since that is incorrect for FireFox, which returns a plain 319 // [ImageData] since that is incorrect for FireFox, which returns a plain
314 // Object. So we need something that tells the compiler that the ImageData 320 // Object. So we need something that tells the compiler that the ImageData
315 // class has been instantiated. 321 // class has been instantiated.
316 // TODO(sra): Remove this when all the ImageData returning APIs have been 322 // TODO(sra): Remove this when all the ImageData returning APIs have been
317 // annotated as returning the union ImageData + Object. 323 // annotated as returning the union ImageData + Object.
318 JS('ImageData', '0'); 324 JS('ImageData', '0');
319 325
320 if (nativeImageData is ImageData) { 326 if (nativeImageData is ImageData) {
321
322 // Fix for Issue 16069: on IE, the `data` field is a CanvasPixelArray which 327 // Fix for Issue 16069: on IE, the `data` field is a CanvasPixelArray which
323 // has Array as the constructor property. This interferes with finding the 328 // has Array as the constructor property. This interferes with finding the
324 // correct interceptor. Fix it by overwriting the constructor property. 329 // correct interceptor. Fix it by overwriting the constructor property.
325 var data = nativeImageData.data; 330 var data = nativeImageData.data;
326 if (JS('bool', '#.constructor === Array', data)) { 331 if (JS('bool', '#.constructor === Array', data)) {
327 if (JS('bool', 'typeof CanvasPixelArray !== "undefined"')) { 332 if (JS('bool', 'typeof CanvasPixelArray !== "undefined"')) {
328 JS('void', '#.constructor = CanvasPixelArray', data); 333 JS('void', '#.constructor = CanvasPixelArray', data);
329 // This TypedArray property is missing from CanvasPixelArray. 334 // This TypedArray property is missing from CanvasPixelArray.
330 JS('void', '#.BYTES_PER_ELEMENT = 1', data); 335 JS('void', '#.BYTES_PER_ELEMENT = 1', data);
331 } 336 }
332 } 337 }
333 338
334 return nativeImageData; 339 return nativeImageData;
335 } 340 }
336 341
337 // On Firefox the above test fails because [nativeImageData] is a plain 342 // On Firefox the above test fails because [nativeImageData] is a plain
338 // object. So we create a _TypedImageData. 343 // object. So we create a _TypedImageData.
339 344
340 return new _TypedImageData( 345 return new _TypedImageData(
341 JS('NativeUint8ClampedList', '#.data', nativeImageData), 346 JS('NativeUint8ClampedList', '#.data', nativeImageData),
342 JS('var', '#.height', nativeImageData), 347 JS('var', '#.height', nativeImageData),
343 JS('var', '#.width', nativeImageData)); 348 JS('var', '#.width', nativeImageData));
344 } 349 }
345 350
346 // We can get rid of this conversion if _TypedImageData implements the fields 351 // We can get rid of this conversion if _TypedImageData implements the fields
347 // with native names. 352 // with native names.
348 convertDartToNative_ImageData(ImageData imageData) { 353 convertDartToNative_ImageData(ImageData imageData) {
349 if (imageData is _TypedImageData) { 354 if (imageData is _TypedImageData) {
350 return JS('', '{data: #, height: #, width: #}', 355 return JS('', '{data: #, height: #, width: #}', imageData.data,
351 imageData.data, imageData.height, imageData.width); 356 imageData.height, imageData.width);
352 } 357 }
353 return imageData; 358 return imageData;
354 } 359 }
355 360
356 const String _serializedScriptValue = 361 const String _serializedScriptValue = 'num|String|bool|'
357 'num|String|bool|'
358 'JSExtendableArray|=Object|' 362 'JSExtendableArray|=Object|'
359 'Blob|File|NativeByteBuffer|NativeTypedData' 363 'Blob|File|NativeByteBuffer|NativeTypedData'
360 // TODO(sra): Add Date, RegExp. 364 // TODO(sra): Add Date, RegExp.
361 ; 365 ;
362 const annotation_Creates_SerializedScriptValue = 366 const annotation_Creates_SerializedScriptValue =
363 const Creates(_serializedScriptValue); 367 const Creates(_serializedScriptValue);
364 const annotation_Returns_SerializedScriptValue = 368 const annotation_Returns_SerializedScriptValue =
365 const Returns(_serializedScriptValue); 369 const Returns(_serializedScriptValue);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698