OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 /** | |
6 * Specialized integers and floating point numbers, | |
7 * with SIMD support and efficient lists. | |
8 */ | |
9 library dart.typed_data.implementation; | |
10 | |
11 import 'dart:collection'; | |
12 import 'dart:_internal'; | |
13 import 'dart:_interceptors' show JSIndexable, JSUInt32, JSUInt31; | |
14 import 'dart:_js_helper' show | |
15 Creates, JavaScriptIndexingBehavior, JSName, Native, Null, Returns, | |
16 diagnoseIndexError; | |
17 import 'dart:_foreign_helper' show JS; | |
18 import 'dart:math' as Math; | |
19 | |
20 import 'dart:typed_data'; | |
21 | |
22 @Native("ArrayBuffer") | |
23 class NativeByteBuffer implements ByteBuffer { | |
24 @JSName('byteLength') | |
25 final int lengthInBytes; | |
26 | |
27 Type get runtimeType => ByteBuffer; | |
28 | |
29 Uint8List asUint8List([int offsetInBytes = 0, int length]) { | |
30 return new NativeUint8List.view(this, offsetInBytes, length); | |
31 } | |
32 | |
33 Int8List asInt8List([int offsetInBytes = 0, int length]) { | |
34 return new NativeInt8List.view(this, offsetInBytes, length); | |
35 } | |
36 | |
37 Uint8ClampedList asUint8ClampedList([int offsetInBytes = 0, int length]) { | |
38 return new NativeUint8ClampedList.view(this, offsetInBytes, length); | |
39 } | |
40 | |
41 Uint16List asUint16List([int offsetInBytes = 0, int length]) { | |
42 return new NativeUint16List.view(this, offsetInBytes, length); | |
43 } | |
44 Int16List asInt16List([int offsetInBytes = 0, int length]) { | |
45 return new NativeInt16List.view(this, offsetInBytes, length); | |
46 } | |
47 | |
48 Uint32List asUint32List([int offsetInBytes = 0, int length]) { | |
49 return new NativeUint32List.view(this, offsetInBytes, length); | |
50 } | |
51 | |
52 Int32List asInt32List([int offsetInBytes = 0, int length]) { | |
53 return new NativeInt32List.view(this, offsetInBytes, length); | |
54 } | |
55 | |
56 Uint64List asUint64List([int offsetInBytes = 0, int length]) { | |
57 throw new UnsupportedError("Uint64List not supported by dart2js."); | |
58 } | |
59 | |
60 Int64List asInt64List([int offsetInBytes = 0, int length]) { | |
61 throw new UnsupportedError("Int64List not supported by dart2js."); | |
62 } | |
63 | |
64 Int32x4List asInt32x4List([int offsetInBytes = 0, int length]) { | |
65 NativeInt32List storage = | |
66 this.asInt32List(offsetInBytes, length != null ? length * 4 : null); | |
67 return new NativeInt32x4List._externalStorage(storage); | |
68 } | |
69 | |
70 Float32List asFloat32List([int offsetInBytes = 0, int length]) { | |
71 return new NativeFloat32List.view(this, offsetInBytes, length); | |
72 } | |
73 | |
74 Float64List asFloat64List([int offsetInBytes = 0, int length]) { | |
75 return new NativeFloat64List.view(this, offsetInBytes, length); | |
76 } | |
77 | |
78 Float32x4List asFloat32x4List([int offsetInBytes = 0, int length]) { | |
79 NativeFloat32List storage = | |
80 this.asFloat32List(offsetInBytes, length != null ? length * 4 : null); | |
81 return new NativeFloat32x4List._externalStorage(storage); | |
82 } | |
83 | |
84 Float64x2List asFloat64x2List([int offsetInBytes = 0, int length]) { | |
85 NativeFloat64List storage = | |
86 this.asFloat64List(offsetInBytes, length != null ? length * 2 : null); | |
87 return new NativeFloat64x2List._externalStorage(storage); | |
88 } | |
89 | |
90 ByteData asByteData([int offsetInBytes = 0, int length]) { | |
91 return new NativeByteData.view(this, offsetInBytes, length); | |
92 } | |
93 } | |
94 | |
95 | |
96 | |
97 /** | |
98 * A fixed-length list of Float32x4 numbers that is viewable as a | |
99 * [TypedData]. For long lists, this implementation will be considerably more | |
100 * space- and time-efficient than the default [List] implementation. | |
101 */ | |
102 class NativeFloat32x4List | |
103 extends Object with ListMixin<Float32x4>, FixedLengthListMixin<Float32x4> | |
104 implements Float32x4List { | |
105 | |
106 final NativeFloat32List _storage; | |
107 | |
108 /** | |
109 * Creates a [Float32x4List] of the specified length (in elements), | |
110 * all of whose elements are initially zero. | |
111 */ | |
112 NativeFloat32x4List(int length) | |
113 : _storage = new NativeFloat32List(length * 4); | |
114 | |
115 NativeFloat32x4List._externalStorage(this._storage); | |
116 | |
117 NativeFloat32x4List._slowFromList(List<Float32x4> list) | |
118 : _storage = new NativeFloat32List(list.length * 4) { | |
119 for (int i = 0; i < list.length; i++) { | |
120 var e = list[i]; | |
121 _storage[(i * 4) + 0] = e.x; | |
122 _storage[(i * 4) + 1] = e.y; | |
123 _storage[(i * 4) + 2] = e.z; | |
124 _storage[(i * 4) + 3] = e.w; | |
125 } | |
126 } | |
127 | |
128 Type get runtimeType => Float32x4List; | |
129 | |
130 /** | |
131 * Creates a [Float32x4List] with the same size as the [elements] list | |
132 * and copies over the elements. | |
133 */ | |
134 factory NativeFloat32x4List.fromList(List<Float32x4> list) { | |
135 if (list is NativeFloat32x4List) { | |
136 return new NativeFloat32x4List._externalStorage( | |
137 new NativeFloat32List.fromList(list._storage)); | |
138 } else { | |
139 return new NativeFloat32x4List._slowFromList(list); | |
140 } | |
141 } | |
142 | |
143 ByteBuffer get buffer => _storage.buffer; | |
144 | |
145 int get lengthInBytes => _storage.lengthInBytes; | |
146 | |
147 int get offsetInBytes => _storage.offsetInBytes; | |
148 | |
149 int get elementSizeInBytes => Float32x4List.BYTES_PER_ELEMENT; | |
150 | |
151 void _invalidIndex(int index, int length) { | |
152 if (index < 0 || index >= length) { | |
153 if (length == this.length) { | |
154 throw new RangeError.index(index, this); | |
155 } | |
156 throw new RangeError.range(index, 0, length - 1); | |
157 } else { | |
158 throw new ArgumentError('Invalid list index $index'); | |
159 } | |
160 } | |
161 | |
162 void _checkIndex(int index, int length) { | |
163 if (JS('bool', '(# >>> 0 != #)', index, index) || index >= length) { | |
164 _invalidIndex(index, length); | |
165 } | |
166 } | |
167 | |
168 int _checkSublistArguments(int start, int end, int length) { | |
169 // For `sublist` the [start] and [end] indices are allowed to be equal to | |
170 // [length]. However, [_checkIndex] only allows indices in the range | |
171 // 0 .. length - 1. We therefore increment the [length] argument by one | |
172 // for the [_checkIndex] checks. | |
173 _checkIndex(start, length + 1); | |
174 if (end == null) return length; | |
175 _checkIndex(end, length + 1); | |
176 if (start > end) throw new RangeError.range(start, 0, end); | |
177 return end; | |
178 } | |
179 | |
180 int get length => _storage.length ~/ 4; | |
181 | |
182 Float32x4 operator[](int index) { | |
183 _checkIndex(index, length); | |
184 double _x = _storage[(index * 4) + 0]; | |
185 double _y = _storage[(index * 4) + 1]; | |
186 double _z = _storage[(index * 4) + 2]; | |
187 double _w = _storage[(index * 4) + 3]; | |
188 return new NativeFloat32x4._truncated(_x, _y, _z, _w); | |
189 } | |
190 | |
191 void operator[]=(int index, Float32x4 value) { | |
192 _checkIndex(index, length); | |
193 _storage[(index * 4) + 0] = value.x; | |
194 _storage[(index * 4) + 1] = value.y; | |
195 _storage[(index * 4) + 2] = value.z; | |
196 _storage[(index * 4) + 3] = value.w; | |
197 } | |
198 | |
199 List<Float32x4> sublist(int start, [int end]) { | |
200 end = _checkSublistArguments(start, end, length); | |
201 return new NativeFloat32x4List._externalStorage( | |
202 _storage.sublist(start * 4, end * 4)); | |
203 } | |
204 } | |
205 | |
206 | |
207 /** | |
208 * A fixed-length list of Int32x4 numbers that is viewable as a | |
209 * [TypedData]. For long lists, this implementation will be considerably more | |
210 * space- and time-efficient than the default [List] implementation. | |
211 */ | |
212 class NativeInt32x4List | |
213 extends Object with ListMixin<Int32x4>, FixedLengthListMixin<Int32x4> | |
214 implements Int32x4List { | |
215 | |
216 final Int32List _storage; | |
217 | |
218 /** | |
219 * Creates a [Int32x4List] of the specified length (in elements), | |
220 * all of whose elements are initially zero. | |
221 */ | |
222 NativeInt32x4List(int length) : _storage = new NativeInt32List(length * 4); | |
223 | |
224 NativeInt32x4List._externalStorage(Int32List storage) : _storage = storage; | |
225 | |
226 NativeInt32x4List._slowFromList(List<Int32x4> list) | |
227 : _storage = new NativeInt32List(list.length * 4) { | |
228 for (int i = 0; i < list.length; i++) { | |
229 var e = list[i]; | |
230 _storage[(i * 4) + 0] = e.x; | |
231 _storage[(i * 4) + 1] = e.y; | |
232 _storage[(i * 4) + 2] = e.z; | |
233 _storage[(i * 4) + 3] = e.w; | |
234 } | |
235 } | |
236 | |
237 Type get runtimeType => Int32x4List; | |
238 | |
239 /** | |
240 * Creates a [Int32x4List] with the same size as the [elements] list | |
241 * and copies over the elements. | |
242 */ | |
243 factory NativeInt32x4List.fromList(List<Int32x4> list) { | |
244 if (list is NativeInt32x4List) { | |
245 return new NativeInt32x4List._externalStorage( | |
246 new NativeInt32List.fromList(list._storage)); | |
247 } else { | |
248 return new NativeInt32x4List._slowFromList(list); | |
249 } | |
250 } | |
251 | |
252 ByteBuffer get buffer => _storage.buffer; | |
253 | |
254 int get lengthInBytes => _storage.lengthInBytes; | |
255 | |
256 int get offsetInBytes => _storage.offsetInBytes; | |
257 | |
258 int get elementSizeInBytes => Int32x4List.BYTES_PER_ELEMENT; | |
259 | |
260 void _invalidIndex(int index, int length) { | |
261 if (index < 0 || index >= length) { | |
262 if (length == this.length) { | |
263 throw new RangeError.index(index, this); | |
264 } | |
265 throw new RangeError.range(index, 0, length - 1); | |
266 } else { | |
267 throw new ArgumentError('Invalid list index $index'); | |
268 } | |
269 } | |
270 | |
271 void _checkIndex(int index, int length) { | |
272 if (JS('bool', '(# >>> 0 != #)', index, index) | |
273 || JS('bool', '# >= #', index, length)) { | |
274 _invalidIndex(index, length); | |
275 } | |
276 } | |
277 | |
278 int _checkSublistArguments(int start, int end, int length) { | |
279 // For `sublist` the [start] and [end] indices are allowed to be equal to | |
280 // [length]. However, [_checkIndex] only allows indices in the range | |
281 // 0 .. length - 1. We therefore increment the [length] argument by one | |
282 // for the [_checkIndex] checks. | |
283 _checkIndex(start, length + 1); | |
284 if (end == null) return length; | |
285 _checkIndex(end, length + 1); | |
286 if (start > end) throw new RangeError.range(start, 0, end); | |
287 return end; | |
288 } | |
289 | |
290 int get length => _storage.length ~/ 4; | |
291 | |
292 Int32x4 operator[](int index) { | |
293 _checkIndex(index, length); | |
294 int _x = _storage[(index * 4) + 0]; | |
295 int _y = _storage[(index * 4) + 1]; | |
296 int _z = _storage[(index * 4) + 2]; | |
297 int _w = _storage[(index * 4) + 3]; | |
298 return new NativeInt32x4._truncated(_x, _y, _z, _w); | |
299 } | |
300 | |
301 void operator[]=(int index, Int32x4 value) { | |
302 _checkIndex(index, length); | |
303 _storage[(index * 4) + 0] = value.x; | |
304 _storage[(index * 4) + 1] = value.y; | |
305 _storage[(index * 4) + 2] = value.z; | |
306 _storage[(index * 4) + 3] = value.w; | |
307 } | |
308 | |
309 List<Int32x4> sublist(int start, [int end]) { | |
310 end = _checkSublistArguments(start, end, length); | |
311 return new NativeInt32x4List._externalStorage( | |
312 _storage.sublist(start * 4, end * 4)); | |
313 } | |
314 } | |
315 | |
316 | |
317 /** | |
318 * A fixed-length list of Float64x2 numbers that is viewable as a | |
319 * [TypedData]. For long lists, this implementation will be considerably more | |
320 * space- and time-efficient than the default [List] implementation. | |
321 */ | |
322 class NativeFloat64x2List | |
323 extends Object with ListMixin<Float64x2>, FixedLengthListMixin<Float64x2> | |
324 implements Float64x2List { | |
325 | |
326 final NativeFloat64List _storage; | |
327 | |
328 /** | |
329 * Creates a [Float64x2List] of the specified length (in elements), | |
330 * all of whose elements are initially zero. | |
331 */ | |
332 NativeFloat64x2List(int length) | |
333 : _storage = new NativeFloat64List(length * 2); | |
334 | |
335 NativeFloat64x2List._externalStorage(this._storage); | |
336 | |
337 NativeFloat64x2List._slowFromList(List<Float64x2> list) | |
338 : _storage = new NativeFloat64List(list.length * 2) { | |
339 for (int i = 0; i < list.length; i++) { | |
340 var e = list[i]; | |
341 _storage[(i * 2) + 0] = e.x; | |
342 _storage[(i * 2) + 1] = e.y; | |
343 } | |
344 } | |
345 | |
346 /** | |
347 * Creates a [Float64x2List] with the same size as the [elements] list | |
348 * and copies over the elements. | |
349 */ | |
350 factory NativeFloat64x2List.fromList(List<Float64x2> list) { | |
351 if (list is NativeFloat64x2List) { | |
352 return new NativeFloat64x2List._externalStorage( | |
353 new NativeFloat64List.fromList(list._storage)); | |
354 } else { | |
355 return new NativeFloat64x2List._slowFromList(list); | |
356 } | |
357 } | |
358 | |
359 Type get runtimeType => Float64x2List; | |
360 | |
361 ByteBuffer get buffer => _storage.buffer; | |
362 | |
363 int get lengthInBytes => _storage.lengthInBytes; | |
364 | |
365 int get offsetInBytes => _storage.offsetInBytes; | |
366 | |
367 int get elementSizeInBytes => Float64x2List.BYTES_PER_ELEMENT; | |
368 | |
369 void _invalidIndex(int index, int length) { | |
370 if (index < 0 || index >= length) { | |
371 if (length == this.length) { | |
372 throw new RangeError.index(index, this); | |
373 } | |
374 throw new RangeError.range(index, 0, length - 1); | |
375 } else { | |
376 throw new ArgumentError('Invalid list index $index'); | |
377 } | |
378 } | |
379 | |
380 void _checkIndex(int index, int length) { | |
381 if (JS('bool', '(# >>> 0 != #)', index, index) || index >= length) { | |
382 _invalidIndex(index, length); | |
383 } | |
384 } | |
385 | |
386 int _checkSublistArguments(int start, int end, int length) { | |
387 // For `sublist` the [start] and [end] indices are allowed to be equal to | |
388 // [length]. However, [_checkIndex] only allows indices in the range | |
389 // 0 .. length - 1. We therefore increment the [length] argument by one | |
390 // for the [_checkIndex] checks. | |
391 _checkIndex(start, length + 1); | |
392 if (end == null) return length; | |
393 _checkIndex(end, length + 1); | |
394 if (start > end) throw new RangeError.range(start, 0, end); | |
395 return end; | |
396 } | |
397 | |
398 int get length => _storage.length ~/ 2; | |
399 | |
400 Float64x2 operator[](int index) { | |
401 _checkIndex(index, length); | |
402 double _x = _storage[(index * 2) + 0]; | |
403 double _y = _storage[(index * 2) + 1]; | |
404 return new Float64x2(_x, _y); | |
405 } | |
406 | |
407 void operator[]=(int index, Float64x2 value) { | |
408 _checkIndex(index, length); | |
409 _storage[(index * 2) + 0] = value.x; | |
410 _storage[(index * 2) + 1] = value.y; | |
411 } | |
412 | |
413 List<Float64x2> sublist(int start, [int end]) { | |
414 end = _checkSublistArguments(start, end, length); | |
415 return new NativeFloat64x2List._externalStorage( | |
416 _storage.sublist(start * 2, end * 2)); | |
417 } | |
418 } | |
419 | |
420 @Native("ArrayBufferView") | |
421 class NativeTypedData implements TypedData { | |
422 /** | |
423 * Returns the byte buffer associated with this object. | |
424 */ | |
425 @Creates('NativeByteBuffer') | |
426 // May be Null for IE's CanvasPixelArray. | |
427 @Returns('NativeByteBuffer|Null') | |
428 final ByteBuffer buffer; | |
429 | |
430 /** | |
431 * Returns the length of this view, in bytes. | |
432 */ | |
433 @JSName('byteLength') | |
434 final int lengthInBytes; | |
435 | |
436 /** | |
437 * Returns the offset in bytes into the underlying byte buffer of this view. | |
438 */ | |
439 @JSName('byteOffset') | |
440 final int offsetInBytes; | |
441 | |
442 /** | |
443 * Returns the number of bytes in the representation of each element in this | |
444 * list. | |
445 */ | |
446 @JSName('BYTES_PER_ELEMENT') | |
447 final int elementSizeInBytes; | |
448 | |
449 void _checkIndex(int index, int length) { | |
450 if (JS('bool', '(# >>> 0) !== #', index, index) || | |
451 JS('int', '#', index) >= length) { // 'int' guaranteed by above test. | |
452 throw diagnoseIndexError(this, index); | |
453 } | |
454 } | |
455 | |
456 void _invalidPosition(int position, int length) { | |
457 if (position is !int) { | |
458 throw new ArgumentError.value(position, null, 'Invalid list position'); | |
459 } else { | |
460 throw new RangeError.range(position, 0, length); | |
461 } | |
462 } | |
463 | |
464 void _checkPosition(int position, int length) { | |
465 if (JS('bool', '(# >>> 0) !== #', position, position) || | |
466 JS('int', '#', position) > length) { // 'int' guaranteed by above test. | |
467 _invalidPosition(position, length); | |
468 } | |
469 } | |
470 | |
471 int _checkSublistArguments(int start, int end, int length) { | |
472 // For `sublist` the [start] and [end] indices are allowed to be equal to | |
473 // [length]. | |
474 _checkPosition(start, length); | |
475 if (end == null) return length; | |
476 _checkPosition(end, length); | |
477 if (start > end) throw new RangeError.range(start, 0, end); | |
478 return end; | |
479 } | |
480 } | |
481 | |
482 | |
483 // Validates the unnamed constructor length argument. Checking is necessary | |
484 // because passing unvalidated values to the native constructors can cause | |
485 // conversions or create views. | |
486 int _checkLength(length) { | |
487 if (length is! int) throw new ArgumentError('Invalid length $length'); | |
488 return length; | |
489 } | |
490 | |
491 // Validates `.view` constructor arguments. Checking is necessary because | |
492 // passing unvalidated values to the native constructors can cause conversions | |
493 // (e.g. String arguments) or create typed data objects that are not actually | |
494 // views of the input. | |
495 void _checkViewArguments(buffer, offsetInBytes, length) { | |
496 if (buffer is! NativeByteBuffer) { | |
497 throw new ArgumentError('Invalid view buffer'); | |
498 } | |
499 if (offsetInBytes is! int) { | |
500 throw new ArgumentError('Invalid view offsetInBytes $offsetInBytes'); | |
501 } | |
502 if (length != null && length is! int) { | |
503 throw new ArgumentError('Invalid view length $length'); | |
504 } | |
505 } | |
506 | |
507 // Ensures that [list] is a JavaScript Array or a typed array. If necessary, | |
508 // returns a copy of the list. | |
509 List _ensureNativeList(List list) { | |
510 if (list is JSIndexable) return list; | |
511 List result = new List(list.length); | |
512 for (int i = 0; i < list.length; i++) { | |
513 result[i] = list[i]; | |
514 } | |
515 return result; | |
516 } | |
517 | |
518 | |
519 @Native("DataView") | |
520 class NativeByteData extends NativeTypedData implements ByteData { | |
521 /** | |
522 * Creates a [ByteData] of the specified length (in elements), all of | |
523 * whose elements are initially zero. | |
524 */ | |
525 factory NativeByteData(int length) => _create1(_checkLength(length)); | |
526 | |
527 /** | |
528 * Creates an [ByteData] _view_ of the specified region in the specified | |
529 * byte buffer. Changes in the [ByteData] will be visible in the byte | |
530 * buffer and vice versa. If the [offsetInBytes] index of the region is not | |
531 * specified, it defaults to zero (the first byte in the byte buffer). | |
532 * If the length is not specified, it defaults to null, which indicates | |
533 * that the view extends to the end of the byte buffer. | |
534 * | |
535 * Throws [RangeError] if [offsetInBytes] or [length] are negative, or | |
536 * if [offsetInBytes] + ([length] * elementSizeInBytes) is greater than | |
537 * the length of [buffer]. | |
538 */ | |
539 factory NativeByteData.view(ByteBuffer buffer, | |
540 int offsetInBytes, int length) { | |
541 _checkViewArguments(buffer, offsetInBytes, length); | |
542 return length == null | |
543 ? _create2(buffer, offsetInBytes) | |
544 : _create3(buffer, offsetInBytes, length); | |
545 } | |
546 | |
547 Type get runtimeType => ByteData; | |
548 | |
549 int get elementSizeInBytes => 1; | |
550 | |
551 /** | |
552 * Returns the floating point number represented by the four bytes at | |
553 * the specified [byteOffset] in this object, in IEEE 754 | |
554 * single-precision binary floating-point format (binary32). | |
555 * | |
556 * Throws [RangeError] if [byteOffset] is negative, or | |
557 * `byteOffset + 4` is greater than the length of this object. | |
558 */ | |
559 num getFloat32(int byteOffset, [Endianness endian=Endianness.BIG_ENDIAN]) => | |
560 _getFloat32(byteOffset, Endianness.LITTLE_ENDIAN == endian); | |
561 | |
562 @JSName('getFloat32') | |
563 @Returns('num') | |
564 num _getFloat32(int byteOffset, [bool littleEndian]) native; | |
565 | |
566 /** | |
567 * Returns the floating point number represented by the eight bytes at | |
568 * the specified [byteOffset] in this object, in IEEE 754 | |
569 * double-precision binary floating-point format (binary64). | |
570 * | |
571 * Throws [RangeError] if [byteOffset] is negative, or | |
572 * `byteOffset + 8` is greater than the length of this object. | |
573 */ | |
574 num getFloat64(int byteOffset, [Endianness endian=Endianness.BIG_ENDIAN]) => | |
575 _getFloat64(byteOffset, Endianness.LITTLE_ENDIAN == endian); | |
576 | |
577 @JSName('getFloat64') | |
578 @Returns('num') | |
579 num _getFloat64(int byteOffset, [bool littleEndian]) native; | |
580 | |
581 /** | |
582 * Returns the (possibly negative) integer represented by the two bytes at | |
583 * the specified [byteOffset] in this object, in two's complement binary | |
584 * form. | |
585 * The return value will be between 2<sup>15</sup> and 2<sup>15</sup> - 1, | |
586 * inclusive. | |
587 * | |
588 * Throws [RangeError] if [byteOffset] is negative, or | |
589 * `byteOffset + 2` is greater than the length of this object. | |
590 */ | |
591 int getInt16(int byteOffset, [Endianness endian=Endianness.BIG_ENDIAN]) => | |
592 _getInt16(byteOffset, Endianness.LITTLE_ENDIAN == endian); | |
593 | |
594 @JSName('getInt16') | |
595 @Returns('int') | |
596 int _getInt16(int byteOffset, [bool littleEndian]) native; | |
597 | |
598 /** | |
599 * Returns the (possibly negative) integer represented by the four bytes at | |
600 * the specified [byteOffset] in this object, in two's complement binary | |
601 * form. | |
602 * The return value will be between 2<sup>31</sup> and 2<sup>31</sup> - 1, | |
603 * inclusive. | |
604 * | |
605 * Throws [RangeError] if [byteOffset] is negative, or | |
606 * `byteOffset + 4` is greater than the length of this object. | |
607 */ | |
608 int getInt32(int byteOffset, [Endianness endian=Endianness.BIG_ENDIAN]) => | |
609 _getInt32(byteOffset, Endianness.LITTLE_ENDIAN == endian); | |
610 | |
611 @JSName('getInt32') | |
612 @Returns('int') | |
613 int _getInt32(int byteOffset, [bool littleEndian]) native; | |
614 | |
615 /** | |
616 * Returns the (possibly negative) integer represented by the eight bytes at | |
617 * the specified [byteOffset] in this object, in two's complement binary | |
618 * form. | |
619 * The return value will be between 2<sup>63</sup> and 2<sup>63</sup> - 1, | |
620 * inclusive. | |
621 * | |
622 * Throws [RangeError] if [byteOffset] is negative, or | |
623 * `byteOffset + 8` is greater than the length of this object. | |
624 */ | |
625 int getInt64(int byteOffset, [Endianness endian=Endianness.BIG_ENDIAN]) { | |
626 throw new UnsupportedError('Int64 accessor not supported by dart2js.'); | |
627 } | |
628 | |
629 /** | |
630 * Returns the (possibly negative) integer represented by the byte at the | |
631 * specified [byteOffset] in this object, in two's complement binary | |
632 * representation. The return value will be between -128 and 127, inclusive. | |
633 * | |
634 * Throws [RangeError] if [byteOffset] is negative, or | |
635 * greater than or equal to the length of this object. | |
636 */ | |
637 int getInt8(int byteOffset) native; | |
638 | |
639 /** | |
640 * Returns the positive integer represented by the two bytes starting | |
641 * at the specified [byteOffset] in this object, in unsigned binary | |
642 * form. | |
643 * The return value will be between 0 and 2<sup>16</sup> - 1, inclusive. | |
644 * | |
645 * Throws [RangeError] if [byteOffset] is negative, or | |
646 * `byteOffset + 2` is greater than the length of this object. | |
647 */ | |
648 int getUint16(int byteOffset, [Endianness endian=Endianness.BIG_ENDIAN]) => | |
649 _getUint16(byteOffset, Endianness.LITTLE_ENDIAN == endian); | |
650 | |
651 @JSName('getUint16') | |
652 @Returns('JSUInt31') | |
653 int _getUint16(int byteOffset, [bool littleEndian]) native; | |
654 | |
655 /** | |
656 * Returns the positive integer represented by the four bytes starting | |
657 * at the specified [byteOffset] in this object, in unsigned binary | |
658 * form. | |
659 * The return value will be between 0 and 2<sup>32</sup> - 1, inclusive. | |
660 * | |
661 * Throws [RangeError] if [byteOffset] is negative, or | |
662 * `byteOffset + 4` is greater than the length of this object. | |
663 */ | |
664 int getUint32(int byteOffset, [Endianness endian=Endianness.BIG_ENDIAN]) => | |
665 _getUint32(byteOffset, Endianness.LITTLE_ENDIAN == endian); | |
666 | |
667 @JSName('getUint32') | |
668 @Returns('JSUInt32') | |
669 int _getUint32(int byteOffset, [bool littleEndian]) native; | |
670 | |
671 /** | |
672 * Returns the positive integer represented by the eight bytes starting | |
673 * at the specified [byteOffset] in this object, in unsigned binary | |
674 * form. | |
675 * The return value will be between 0 and 2<sup>64</sup> - 1, inclusive. | |
676 * | |
677 * Throws [RangeError] if [byteOffset] is negative, or | |
678 * `byteOffset + 8` is greater than the length of this object. | |
679 */ | |
680 int getUint64(int byteOffset, [Endianness endian=Endianness.BIG_ENDIAN]) { | |
681 throw new UnsupportedError('Uint64 accessor not supported by dart2js.'); | |
682 } | |
683 | |
684 /** | |
685 * Returns the positive integer represented by the byte at the specified | |
686 * [byteOffset] in this object, in unsigned binary form. The | |
687 * return value will be between 0 and 255, inclusive. | |
688 * | |
689 * Throws [RangeError] if [byteOffset] is negative, or | |
690 * greater than or equal to the length of this object. | |
691 */ | |
692 int getUint8(int byteOffset) native; | |
693 | |
694 /** | |
695 * Sets the four bytes starting at the specified [byteOffset] in this | |
696 * object to the IEEE 754 single-precision binary floating-point | |
697 * (binary32) representation of the specified [value]. | |
698 * | |
699 * **Note that this method can lose precision.** The input [value] is | |
700 * a 64-bit floating point value, which will be converted to 32-bit | |
701 * floating point value by IEEE 754 rounding rules before it is stored. | |
702 * If [value] cannot be represented exactly as a binary32, it will be | |
703 * converted to the nearest binary32 value. If two binary32 values are | |
704 * equally close, the one whose least significant bit is zero will be used. | |
705 * Note that finite (but large) values can be converted to infinity, and | |
706 * small non-zero values can be converted to zero. | |
707 * | |
708 * Throws [RangeError] if [byteOffset] is negative, or | |
709 * `byteOffset + 4` is greater than the length of this object. | |
710 */ | |
711 void setFloat32(int byteOffset, num value, | |
712 [Endianness endian=Endianness.BIG_ENDIAN]) => | |
713 _setFloat32(byteOffset, value, Endianness.LITTLE_ENDIAN == endian); | |
714 | |
715 @JSName('setFloat32') | |
716 void _setFloat32(int byteOffset, num value, [bool littleEndian]) native; | |
717 | |
718 /** | |
719 * Sets the eight bytes starting at the specified [byteOffset] in this | |
720 * object to the IEEE 754 double-precision binary floating-point | |
721 * (binary64) representation of the specified [value]. | |
722 * | |
723 * Throws [RangeError] if [byteOffset] is negative, or | |
724 * `byteOffset + 8` is greater than the length of this object. | |
725 */ | |
726 void setFloat64(int byteOffset, num value, | |
727 [Endianness endian=Endianness.BIG_ENDIAN]) => | |
728 _setFloat64(byteOffset, value, Endianness.LITTLE_ENDIAN == endian); | |
729 | |
730 @JSName('setFloat64') | |
731 void _setFloat64(int byteOffset, num value, [bool littleEndian]) native; | |
732 | |
733 /** | |
734 * Sets the two bytes starting at the specified [byteOffset] in this | |
735 * object to the two's complement binary representation of the specified | |
736 * [value], which must fit in two bytes. In other words, [value] must lie | |
737 * between 2<sup>15</sup> and 2<sup>15</sup> - 1, inclusive. | |
738 * | |
739 * Throws [RangeError] if [byteOffset] is negative, or | |
740 * `byteOffset + 2` is greater than the length of this object. | |
741 */ | |
742 void setInt16(int byteOffset, int value, | |
743 [Endianness endian=Endianness.BIG_ENDIAN]) => | |
744 _setInt16(byteOffset, value, Endianness.LITTLE_ENDIAN == endian); | |
745 | |
746 @JSName('setInt16') | |
747 void _setInt16(int byteOffset, int value, [bool littleEndian]) native; | |
748 | |
749 /** | |
750 * Sets the four bytes starting at the specified [byteOffset] in this | |
751 * object to the two's complement binary representation of the specified | |
752 * [value], which must fit in four bytes. In other words, [value] must lie | |
753 * between 2<sup>31</sup> and 2<sup>31</sup> - 1, inclusive. | |
754 * | |
755 * Throws [RangeError] if [byteOffset] is negative, or | |
756 * `byteOffset + 4` is greater than the length of this object. | |
757 */ | |
758 void setInt32(int byteOffset, int value, | |
759 [Endianness endian=Endianness.BIG_ENDIAN]) => | |
760 _setInt32(byteOffset, value, Endianness.LITTLE_ENDIAN == endian); | |
761 | |
762 @JSName('setInt32') | |
763 void _setInt32(int byteOffset, int value, [bool littleEndian]) native; | |
764 | |
765 /** | |
766 * Sets the eight bytes starting at the specified [byteOffset] in this | |
767 * object to the two's complement binary representation of the specified | |
768 * [value], which must fit in eight bytes. In other words, [value] must lie | |
769 * between 2<sup>63</sup> and 2<sup>63</sup> - 1, inclusive. | |
770 * | |
771 * Throws [RangeError] if [byteOffset] is negative, or | |
772 * `byteOffset + 8` is greater than the length of this object. | |
773 */ | |
774 void setInt64(int byteOffset, int value, | |
775 [Endianness endian=Endianness.BIG_ENDIAN]) { | |
776 throw new UnsupportedError('Int64 accessor not supported by dart2js.'); | |
777 } | |
778 | |
779 /** | |
780 * Sets the byte at the specified [byteOffset] in this object to the | |
781 * two's complement binary representation of the specified [value], which | |
782 * must fit in a single byte. In other words, [value] must be between | |
783 * -128 and 127, inclusive. | |
784 * | |
785 * Throws [RangeError] if [byteOffset] is negative, or | |
786 * greater than or equal to the length of this object. | |
787 */ | |
788 void setInt8(int byteOffset, int value) native; | |
789 | |
790 /** | |
791 * Sets the two bytes starting at the specified [byteOffset] in this object | |
792 * to the unsigned binary representation of the specified [value], | |
793 * which must fit in two bytes. in other words, [value] must be between | |
794 * 0 and 2<sup>16</sup> - 1, inclusive. | |
795 * | |
796 * Throws [RangeError] if [byteOffset] is negative, or | |
797 * `byteOffset + 2` is greater than the length of this object. | |
798 */ | |
799 void setUint16(int byteOffset, int value, | |
800 [Endianness endian=Endianness.BIG_ENDIAN]) => | |
801 _setUint16(byteOffset, value, Endianness.LITTLE_ENDIAN == endian); | |
802 | |
803 @JSName('setUint16') | |
804 void _setUint16(int byteOffset, int value, [bool littleEndian]) native; | |
805 | |
806 /** | |
807 * Sets the four bytes starting at the specified [byteOffset] in this object | |
808 * to the unsigned binary representation of the specified [value], | |
809 * which must fit in four bytes. in other words, [value] must be between | |
810 * 0 and 2<sup>32</sup> - 1, inclusive. | |
811 * | |
812 * Throws [RangeError] if [byteOffset] is negative, or | |
813 * `byteOffset + 4` is greater than the length of this object. | |
814 */ | |
815 void setUint32(int byteOffset, int value, | |
816 [Endianness endian=Endianness.BIG_ENDIAN]) => | |
817 _setUint32(byteOffset, value, Endianness.LITTLE_ENDIAN == endian); | |
818 | |
819 @JSName('setUint32') | |
820 void _setUint32(int byteOffset, int value, [bool littleEndian]) native; | |
821 | |
822 /** | |
823 * Sets the eight bytes starting at the specified [byteOffset] in this object | |
824 * to the unsigned binary representation of the specified [value], | |
825 * which must fit in eight bytes. in other words, [value] must be between | |
826 * 0 and 2<sup>64</sup> - 1, inclusive. | |
827 * | |
828 * Throws [RangeError] if [byteOffset] is negative, or | |
829 * `byteOffset + 8` is greater than the length of this object. | |
830 */ | |
831 void setUint64(int byteOffset, int value, | |
832 [Endianness endian=Endianness.BIG_ENDIAN]) { | |
833 throw new UnsupportedError('Uint64 accessor not supported by dart2js.'); | |
834 } | |
835 | |
836 /** | |
837 * Sets the byte at the specified [byteOffset] in this object to the | |
838 * unsigned binary representation of the specified [value], which must fit | |
839 * in a single byte. in other words, [value] must be between 0 and 255, | |
840 * inclusive. | |
841 * | |
842 * Throws [RangeError] if [byteOffset] is negative, | |
843 * or greater than or equal to the length of this object. | |
844 */ | |
845 void setUint8(int byteOffset, int value) native; | |
846 | |
847 static NativeByteData _create1(arg) => | |
848 JS('NativeByteData', 'new DataView(new ArrayBuffer(#))', arg); | |
849 | |
850 static NativeByteData _create2(arg1, arg2) => | |
851 JS('NativeByteData', 'new DataView(#, #)', arg1, arg2); | |
852 | |
853 static NativeByteData _create3(arg1, arg2, arg3) => | |
854 JS('NativeByteData', 'new DataView(#, #, #)', arg1, arg2, arg3); | |
855 } | |
856 | |
857 | |
858 abstract class NativeTypedArray extends NativeTypedData | |
859 implements JavaScriptIndexingBehavior { | |
860 int get length => JS('JSUInt32', '#.length', this); | |
861 | |
862 void _setRangeFast(int start, int end, | |
863 NativeTypedArray source, int skipCount) { | |
864 int targetLength = this.length; | |
865 _checkPosition(start, targetLength); | |
866 _checkPosition(end, targetLength); | |
867 if (start > end) throw new RangeError.range(start, 0, end); | |
868 int count = end - start; | |
869 | |
870 if (skipCount < 0) throw new ArgumentError(skipCount); | |
871 | |
872 int sourceLength = source.length; | |
873 if (sourceLength - skipCount < count) { | |
874 throw new StateError('Not enough elements'); | |
875 } | |
876 | |
877 if (skipCount != 0 || sourceLength != count) { | |
878 // Create a view of the exact subrange that is copied from the source. | |
879 source = JS('', '#.subarray(#, #)', | |
880 source, skipCount, skipCount + count); | |
881 } | |
882 JS('void', '#.set(#, #)', this, source, start); | |
883 } | |
884 } | |
885 | |
886 abstract class NativeTypedArrayOfDouble | |
887 extends NativeTypedArray | |
888 with ListMixin<double>, FixedLengthListMixin<double> { | |
889 | |
890 num operator[](int index) { | |
891 _checkIndex(index, length); | |
892 return JS('num', '#[#]', this, index); | |
893 } | |
894 | |
895 void operator[]=(int index, num value) { | |
896 _checkIndex(index, length); | |
897 JS('void', '#[#] = #', this, index, value); | |
898 } | |
899 | |
900 void setRange(int start, int end, Iterable<double> iterable, | |
901 [int skipCount = 0]) { | |
902 if (iterable is NativeTypedArrayOfDouble) { | |
903 _setRangeFast(start, end, iterable, skipCount); | |
904 return; | |
905 } | |
906 super.setRange(start, end, iterable, skipCount); | |
907 } | |
908 } | |
909 | |
910 abstract class NativeTypedArrayOfInt | |
911 extends NativeTypedArray | |
912 with ListMixin<int>, FixedLengthListMixin<int> | |
913 implements List<int> { | |
914 | |
915 // operator[]() is not here since different versions have different return | |
916 // types | |
917 | |
918 void operator[]=(int index, int value) { | |
919 _checkIndex(index, length); | |
920 JS('void', '#[#] = #', this, index, value); | |
921 } | |
922 | |
923 void setRange(int start, int end, Iterable<int> iterable, | |
924 [int skipCount = 0]) { | |
925 if (iterable is NativeTypedArrayOfInt) { | |
926 _setRangeFast(start, end, iterable, skipCount); | |
927 return; | |
928 } | |
929 super.setRange(start, end, iterable, skipCount); | |
930 } | |
931 } | |
932 | |
933 | |
934 @Native("Float32Array") | |
935 class NativeFloat32List | |
936 extends NativeTypedArrayOfDouble | |
937 implements Float32List { | |
938 | |
939 factory NativeFloat32List(int length) => _create1(_checkLength(length)); | |
940 | |
941 factory NativeFloat32List.fromList(List<double> elements) => | |
942 _create1(_ensureNativeList(elements)); | |
943 | |
944 factory NativeFloat32List.view(ByteBuffer buffer, | |
945 int offsetInBytes, int length) { | |
946 _checkViewArguments(buffer, offsetInBytes, length); | |
947 return length == null | |
948 ? _create2(buffer, offsetInBytes) | |
949 : _create3(buffer, offsetInBytes, length); | |
950 } | |
951 | |
952 Type get runtimeType => Float32List; | |
953 | |
954 List<double> sublist(int start, [int end]) { | |
955 end = _checkSublistArguments(start, end, length); | |
956 var source = JS('NativeFloat32List', '#.subarray(#, #)', this, start, end); | |
957 return _create1(source); | |
958 } | |
959 | |
960 static NativeFloat32List _create1(arg) => | |
961 JS('NativeFloat32List', 'new Float32Array(#)', arg); | |
962 | |
963 static NativeFloat32List _create2(arg1, arg2) => | |
964 JS('NativeFloat32List', 'new Float32Array(#, #)', arg1, arg2); | |
965 | |
966 static NativeFloat32List _create3(arg1, arg2, arg3) => | |
967 JS('NativeFloat32List', 'new Float32Array(#, #, #)', arg1, arg2, arg3); | |
968 } | |
969 | |
970 | |
971 @Native("Float64Array") | |
972 class NativeFloat64List | |
973 extends NativeTypedArrayOfDouble | |
974 implements Float64List { | |
975 | |
976 factory NativeFloat64List(int length) => _create1(_checkLength(length)); | |
977 | |
978 factory NativeFloat64List.fromList(List<double> elements) => | |
979 _create1(_ensureNativeList(elements)); | |
980 | |
981 factory NativeFloat64List.view(ByteBuffer buffer, | |
982 int offsetInBytes, int length) { | |
983 _checkViewArguments(buffer, offsetInBytes, length); | |
984 return length == null | |
985 ? _create2(buffer, offsetInBytes) | |
986 : _create3(buffer, offsetInBytes, length); | |
987 } | |
988 | |
989 Type get runtimeType => Float64List; | |
990 | |
991 List<double> sublist(int start, [int end]) { | |
992 end = _checkSublistArguments(start, end, length); | |
993 var source = JS('NativeFloat64List', '#.subarray(#, #)', this, start, end); | |
994 return _create1(source); | |
995 } | |
996 | |
997 static NativeFloat64List _create1(arg) => | |
998 JS('NativeFloat64List', 'new Float64Array(#)', arg); | |
999 | |
1000 static NativeFloat64List _create2(arg1, arg2) => | |
1001 JS('NativeFloat64List', 'new Float64Array(#, #)', arg1, arg2); | |
1002 | |
1003 static NativeFloat64List _create3(arg1, arg2, arg3) => | |
1004 JS('NativeFloat64List', 'new Float64Array(#, #, #)', arg1, arg2, arg3); | |
1005 } | |
1006 | |
1007 | |
1008 @Native("Int16Array") | |
1009 class NativeInt16List | |
1010 extends NativeTypedArrayOfInt | |
1011 implements Int16List { | |
1012 | |
1013 factory NativeInt16List(int length) => _create1(_checkLength(length)); | |
1014 | |
1015 factory NativeInt16List.fromList(List<int> elements) => | |
1016 _create1(_ensureNativeList(elements)); | |
1017 | |
1018 factory NativeInt16List.view(NativeByteBuffer buffer, | |
1019 int offsetInBytes, int length) { | |
1020 _checkViewArguments(buffer, offsetInBytes, length); | |
1021 return length == null | |
1022 ? _create2(buffer, offsetInBytes) | |
1023 : _create3(buffer, offsetInBytes, length); | |
1024 } | |
1025 | |
1026 Type get runtimeType => Int16List; | |
1027 | |
1028 int operator[](int index) { | |
1029 _checkIndex(index, length); | |
1030 return JS('int', '#[#]', this, index); | |
1031 } | |
1032 | |
1033 List<int> sublist(int start, [int end]) { | |
1034 end = _checkSublistArguments(start, end, length); | |
1035 var source = JS('NativeInt16List', '#.subarray(#, #)', this, start, end); | |
1036 return _create1(source); | |
1037 } | |
1038 | |
1039 static NativeInt16List _create1(arg) => | |
1040 JS('NativeInt16List', 'new Int16Array(#)', arg); | |
1041 | |
1042 static NativeInt16List _create2(arg1, arg2) => | |
1043 JS('NativeInt16List', 'new Int16Array(#, #)', arg1, arg2); | |
1044 | |
1045 static NativeInt16List _create3(arg1, arg2, arg3) => | |
1046 JS('NativeInt16List', 'new Int16Array(#, #, #)', arg1, arg2, arg3); | |
1047 } | |
1048 | |
1049 | |
1050 @Native("Int32Array") | |
1051 class NativeInt32List extends NativeTypedArrayOfInt implements Int32List { | |
1052 | |
1053 factory NativeInt32List(int length) => _create1(_checkLength(length)); | |
1054 | |
1055 factory NativeInt32List.fromList(List<int> elements) => | |
1056 _create1(_ensureNativeList(elements)); | |
1057 | |
1058 factory NativeInt32List.view(ByteBuffer buffer, | |
1059 int offsetInBytes, int length) { | |
1060 _checkViewArguments(buffer, offsetInBytes, length); | |
1061 return length == null | |
1062 ? _create2(buffer, offsetInBytes) | |
1063 : _create3(buffer, offsetInBytes, length); | |
1064 } | |
1065 | |
1066 Type get runtimeType => Int32List; | |
1067 | |
1068 int operator[](int index) { | |
1069 _checkIndex(index, length); | |
1070 return JS('int', '#[#]', this, index); | |
1071 } | |
1072 | |
1073 List<int> sublist(int start, [int end]) { | |
1074 end = _checkSublistArguments(start, end, length); | |
1075 var source = JS('NativeInt32List', '#.subarray(#, #)', this, start, end); | |
1076 return _create1(source); | |
1077 } | |
1078 | |
1079 static NativeInt32List _create1(arg) => | |
1080 JS('NativeInt32List', 'new Int32Array(#)', arg); | |
1081 | |
1082 static NativeInt32List _create2(arg1, arg2) => | |
1083 JS('NativeInt32List', 'new Int32Array(#, #)', arg1, arg2); | |
1084 | |
1085 static NativeInt32List _create3(arg1, arg2, arg3) => | |
1086 JS('NativeInt32List', 'new Int32Array(#, #, #)', arg1, arg2, arg3); | |
1087 } | |
1088 | |
1089 | |
1090 @Native("Int8Array") | |
1091 class NativeInt8List extends NativeTypedArrayOfInt implements Int8List { | |
1092 | |
1093 factory NativeInt8List(int length) => _create1(_checkLength(length)); | |
1094 | |
1095 factory NativeInt8List.fromList(List<int> elements) => | |
1096 _create1(_ensureNativeList(elements)); | |
1097 | |
1098 factory NativeInt8List.view(ByteBuffer buffer, | |
1099 int offsetInBytes, int length) { | |
1100 _checkViewArguments(buffer, offsetInBytes, length); | |
1101 return length == null | |
1102 ? _create2(buffer, offsetInBytes) | |
1103 : _create3(buffer, offsetInBytes, length); | |
1104 } | |
1105 | |
1106 Type get runtimeType => Int8List; | |
1107 | |
1108 int operator[](int index) { | |
1109 _checkIndex(index, length); | |
1110 return JS('int', '#[#]', this, index); | |
1111 } | |
1112 | |
1113 List<int> sublist(int start, [int end]) { | |
1114 end = _checkSublistArguments(start, end, length); | |
1115 var source = JS('NativeInt8List', '#.subarray(#, #)', this, start, end); | |
1116 return _create1(source); | |
1117 } | |
1118 | |
1119 static NativeInt8List _create1(arg) => | |
1120 JS('NativeInt8List', 'new Int8Array(#)', arg); | |
1121 | |
1122 static NativeInt8List _create2(arg1, arg2) => | |
1123 JS('NativeInt8List', 'new Int8Array(#, #)', arg1, arg2); | |
1124 | |
1125 static Int8List _create3(arg1, arg2, arg3) => | |
1126 JS('NativeInt8List', 'new Int8Array(#, #, #)', arg1, arg2, arg3); | |
1127 } | |
1128 | |
1129 | |
1130 @Native("Uint16Array") | |
1131 class NativeUint16List extends NativeTypedArrayOfInt implements Uint16List { | |
1132 | |
1133 factory NativeUint16List(int length) => _create1(_checkLength(length)); | |
1134 | |
1135 factory NativeUint16List.fromList(List<int> list) => | |
1136 _create1(_ensureNativeList(list)); | |
1137 | |
1138 factory NativeUint16List.view(ByteBuffer buffer, | |
1139 int offsetInBytes, int length) { | |
1140 _checkViewArguments(buffer, offsetInBytes, length); | |
1141 return length == null | |
1142 ? _create2(buffer, offsetInBytes) | |
1143 : _create3(buffer, offsetInBytes, length); | |
1144 } | |
1145 | |
1146 Type get runtimeType => Uint16List; | |
1147 | |
1148 int operator[](int index) { | |
1149 _checkIndex(index, length); | |
1150 return JS('JSUInt31', '#[#]', this, index); | |
1151 } | |
1152 | |
1153 List<int> sublist(int start, [int end]) { | |
1154 end = _checkSublistArguments(start, end, length); | |
1155 var source = JS('NativeUint16List', '#.subarray(#, #)', this, start, end); | |
1156 return _create1(source); | |
1157 } | |
1158 | |
1159 static NativeUint16List _create1(arg) => | |
1160 JS('NativeUint16List', 'new Uint16Array(#)', arg); | |
1161 | |
1162 static NativeUint16List _create2(arg1, arg2) => | |
1163 JS('NativeUint16List', 'new Uint16Array(#, #)', arg1, arg2); | |
1164 | |
1165 static NativeUint16List _create3(arg1, arg2, arg3) => | |
1166 JS('NativeUint16List', 'new Uint16Array(#, #, #)', arg1, arg2, arg3); | |
1167 } | |
1168 | |
1169 | |
1170 @Native("Uint32Array") | |
1171 class NativeUint32List extends NativeTypedArrayOfInt implements Uint32List { | |
1172 | |
1173 factory NativeUint32List(int length) => _create1(_checkLength(length)); | |
1174 | |
1175 factory NativeUint32List.fromList(List<int> elements) => | |
1176 _create1(_ensureNativeList(elements)); | |
1177 | |
1178 factory NativeUint32List.view(ByteBuffer buffer, | |
1179 int offsetInBytes, int length) { | |
1180 _checkViewArguments(buffer, offsetInBytes, length); | |
1181 return length == null | |
1182 ? _create2(buffer, offsetInBytes) | |
1183 : _create3(buffer, offsetInBytes, length); | |
1184 } | |
1185 | |
1186 Type get runtimeType => Uint32List; | |
1187 | |
1188 int operator[](int index) { | |
1189 _checkIndex(index, length); | |
1190 return JS('JSUInt32', '#[#]', this, index); | |
1191 } | |
1192 | |
1193 List<int> sublist(int start, [int end]) { | |
1194 end = _checkSublistArguments(start, end, length); | |
1195 var source = JS('NativeUint32List', '#.subarray(#, #)', this, start, end); | |
1196 return _create1(source); | |
1197 } | |
1198 | |
1199 static NativeUint32List _create1(arg) => | |
1200 JS('NativeUint32List', 'new Uint32Array(#)', arg); | |
1201 | |
1202 static NativeUint32List _create2(arg1, arg2) => | |
1203 JS('NativeUint32List', 'new Uint32Array(#, #)', arg1, arg2); | |
1204 | |
1205 static NativeUint32List _create3(arg1, arg2, arg3) => | |
1206 JS('NativeUint32List', 'new Uint32Array(#, #, #)', arg1, arg2, arg3); | |
1207 } | |
1208 | |
1209 | |
1210 @Native("Uint8ClampedArray,CanvasPixelArray") | |
1211 class NativeUint8ClampedList | |
1212 extends NativeTypedArrayOfInt | |
1213 implements Uint8ClampedList { | |
1214 | |
1215 factory NativeUint8ClampedList(int length) => _create1(_checkLength(length)); | |
1216 | |
1217 factory NativeUint8ClampedList.fromList(List<int> elements) => | |
1218 _create1(_ensureNativeList(elements)); | |
1219 | |
1220 factory NativeUint8ClampedList.view(ByteBuffer buffer, | |
1221 int offsetInBytes, int length) { | |
1222 _checkViewArguments(buffer, offsetInBytes, length); | |
1223 return length == null | |
1224 ? _create2(buffer, offsetInBytes) | |
1225 : _create3(buffer, offsetInBytes, length); | |
1226 } | |
1227 | |
1228 Type get runtimeType => Uint8ClampedList; | |
1229 | |
1230 int get length => JS('JSUInt32', '#.length', this); | |
1231 | |
1232 int operator[](int index) { | |
1233 _checkIndex(index, length); | |
1234 return JS('JSUInt31', '#[#]', this, index); | |
1235 } | |
1236 | |
1237 List<int> sublist(int start, [int end]) { | |
1238 end = _checkSublistArguments(start, end, length); | |
1239 var source = JS('NativeUint8ClampedList', '#.subarray(#, #)', | |
1240 this, start, end); | |
1241 return _create1(source); | |
1242 } | |
1243 | |
1244 static NativeUint8ClampedList _create1(arg) => | |
1245 JS('NativeUint8ClampedList', 'new Uint8ClampedArray(#)', arg); | |
1246 | |
1247 static NativeUint8ClampedList _create2(arg1, arg2) => | |
1248 JS('NativeUint8ClampedList', 'new Uint8ClampedArray(#, #)', arg1, arg2); | |
1249 | |
1250 static NativeUint8ClampedList _create3(arg1, arg2, arg3) => | |
1251 JS('NativeUint8ClampedList', 'new Uint8ClampedArray(#, #, #)', | |
1252 arg1, arg2, arg3); | |
1253 } | |
1254 | |
1255 | |
1256 // On some browsers Uint8ClampedArray is a subtype of Uint8Array. Marking | |
1257 // Uint8List as !nonleaf ensures that the native dispatch correctly handles | |
1258 // the potential for Uint8ClampedArray to 'accidentally' pick up the | |
1259 // dispatch record for Uint8List. | |
1260 @Native("Uint8Array,!nonleaf") | |
1261 class NativeUint8List extends NativeTypedArrayOfInt implements Uint8List { | |
1262 | |
1263 factory NativeUint8List(int length) => _create1(_checkLength(length)); | |
1264 | |
1265 factory NativeUint8List.fromList(List<int> elements) => | |
1266 _create1(_ensureNativeList(elements)); | |
1267 | |
1268 factory NativeUint8List.view(ByteBuffer buffer, | |
1269 int offsetInBytes, int length) { | |
1270 _checkViewArguments(buffer, offsetInBytes, length); | |
1271 return length == null | |
1272 ? _create2(buffer, offsetInBytes) | |
1273 : _create3(buffer, offsetInBytes, length); | |
1274 } | |
1275 | |
1276 Type get runtimeType => Uint8List; | |
1277 | |
1278 int get length => JS('JSUInt32', '#.length', this); | |
1279 | |
1280 int operator[](int index) { | |
1281 _checkIndex(index, length); | |
1282 return JS('JSUInt31', '#[#]', this, index); | |
1283 } | |
1284 | |
1285 List<int> sublist(int start, [int end]) { | |
1286 end = _checkSublistArguments(start, end, length); | |
1287 var source = JS('NativeUint8List', '#.subarray(#, #)', this, start, end); | |
1288 return _create1(source); | |
1289 } | |
1290 | |
1291 static NativeUint8List _create1(arg) => | |
1292 JS('NativeUint8List', 'new Uint8Array(#)', arg); | |
1293 | |
1294 static NativeUint8List _create2(arg1, arg2) => | |
1295 JS('NativeUint8List', 'new Uint8Array(#, #)', arg1, arg2); | |
1296 | |
1297 static NativeUint8List _create3(arg1, arg2, arg3) => | |
1298 JS('NativeUint8List', 'new Uint8Array(#, #, #)', arg1, arg2, arg3); | |
1299 } | |
1300 | |
1301 | |
1302 /** | |
1303 * Implementation of Dart Float32x4 immutable value type and operations. | |
1304 * Float32x4 stores 4 32-bit floating point values in "lanes". | |
1305 * The lanes are "x", "y", "z", and "w" respectively. | |
1306 */ | |
1307 class NativeFloat32x4 implements Float32x4 { | |
1308 final double x; | |
1309 final double y; | |
1310 final double z; | |
1311 final double w; | |
1312 | |
1313 static final NativeFloat32List _list = new NativeFloat32List(4); | |
1314 static final Uint32List _uint32view = _list.buffer.asUint32List(); | |
1315 | |
1316 static _truncate(x) { | |
1317 _list[0] = x; | |
1318 return _list[0]; | |
1319 } | |
1320 | |
1321 NativeFloat32x4(double x, double y, double z, double w) | |
1322 : this.x = _truncate(x), | |
1323 this.y = _truncate(y), | |
1324 this.z = _truncate(z), | |
1325 this.w = _truncate(w) { | |
1326 // We would prefer to check for `double` but in dart2js we can't see the | |
1327 // difference anyway. | |
1328 if (x is! num) throw new ArgumentError(x); | |
1329 if (y is! num) throw new ArgumentError(y); | |
1330 if (z is! num) throw new ArgumentError(z); | |
1331 if (w is! num) throw new ArgumentError(w); | |
1332 } | |
1333 | |
1334 NativeFloat32x4.splat(double v) : this(v, v, v, v); | |
1335 NativeFloat32x4.zero() : this._truncated(0.0, 0.0, 0.0, 0.0); | |
1336 | |
1337 /// Returns a bit-wise copy of [i] as a Float32x4. | |
1338 factory NativeFloat32x4.fromInt32x4Bits(Int32x4 i) { | |
1339 _uint32view[0] = i.x; | |
1340 _uint32view[1] = i.y; | |
1341 _uint32view[2] = i.z; | |
1342 _uint32view[3] = i.w; | |
1343 return new NativeFloat32x4._truncated(_list[0], _list[1], _list[2], _list[3]
); | |
1344 } | |
1345 | |
1346 NativeFloat32x4.fromFloat64x2(Float64x2 v) | |
1347 : this._truncated(_truncate(v.x), _truncate(v.y), 0.0, 0.0); | |
1348 | |
1349 /// Creates a new NativeFloat32x4. | |
1350 /// | |
1351 /// Does not verify if the given arguments are non-null. | |
1352 NativeFloat32x4._doubles(double x, double y, double z, double w) | |
1353 : this.x = _truncate(x), | |
1354 this.y = _truncate(y), | |
1355 this.z = _truncate(z), | |
1356 this.w = _truncate(w); | |
1357 | |
1358 /// Creates a new NativeFloat32x4. | |
1359 /// | |
1360 /// The constructor does not truncate the arguments. They must already be in | |
1361 /// the correct range. It does not verify the type of the given arguments, | |
1362 /// either. | |
1363 NativeFloat32x4._truncated(this.x, this.y, this.z, this.w); | |
1364 | |
1365 String toString() { | |
1366 return '[$x, $y, $z, $w]'; | |
1367 } | |
1368 | |
1369 /// Addition operator. | |
1370 Float32x4 operator+(Float32x4 other) { | |
1371 double _x = x + other.x; | |
1372 double _y = y + other.y; | |
1373 double _z = z + other.z; | |
1374 double _w = w + other.w; | |
1375 return new NativeFloat32x4._doubles(_x, _y, _z, _w); | |
1376 } | |
1377 | |
1378 /// Negate operator. | |
1379 Float32x4 operator-() { | |
1380 return new NativeFloat32x4._truncated(-x, -y, -z, -w); | |
1381 } | |
1382 | |
1383 /// Subtraction operator. | |
1384 Float32x4 operator-(Float32x4 other) { | |
1385 double _x = x - other.x; | |
1386 double _y = y - other.y; | |
1387 double _z = z - other.z; | |
1388 double _w = w - other.w; | |
1389 return new NativeFloat32x4._doubles(_x, _y, _z, _w); | |
1390 } | |
1391 | |
1392 /// Multiplication operator. | |
1393 Float32x4 operator*(Float32x4 other) { | |
1394 double _x = x * other.x; | |
1395 double _y = y * other.y; | |
1396 double _z = z * other.z; | |
1397 double _w = w * other.w; | |
1398 return new NativeFloat32x4._doubles(_x, _y, _z, _w); | |
1399 } | |
1400 | |
1401 /// Division operator. | |
1402 Float32x4 operator/(Float32x4 other) { | |
1403 double _x = x / other.x; | |
1404 double _y = y / other.y; | |
1405 double _z = z / other.z; | |
1406 double _w = w / other.w; | |
1407 return new NativeFloat32x4._doubles(_x, _y, _z, _w); | |
1408 } | |
1409 | |
1410 /// Relational less than. | |
1411 Int32x4 lessThan(Float32x4 other) { | |
1412 bool _cx = x < other.x; | |
1413 bool _cy = y < other.y; | |
1414 bool _cz = z < other.z; | |
1415 bool _cw = w < other.w; | |
1416 return new NativeInt32x4._truncated(_cx ? -1 : 0, | |
1417 _cy ? -1 : 0, | |
1418 _cz ? -1 : 0, | |
1419 _cw ? -1 : 0); | |
1420 } | |
1421 | |
1422 /// Relational less than or equal. | |
1423 Int32x4 lessThanOrEqual(Float32x4 other) { | |
1424 bool _cx = x <= other.x; | |
1425 bool _cy = y <= other.y; | |
1426 bool _cz = z <= other.z; | |
1427 bool _cw = w <= other.w; | |
1428 return new NativeInt32x4._truncated(_cx ? -1 : 0, | |
1429 _cy ? -1 : 0, | |
1430 _cz ? -1 : 0, | |
1431 _cw ? -1 : 0); | |
1432 } | |
1433 | |
1434 /// Relational greater than. | |
1435 Int32x4 greaterThan(Float32x4 other) { | |
1436 bool _cx = x > other.x; | |
1437 bool _cy = y > other.y; | |
1438 bool _cz = z > other.z; | |
1439 bool _cw = w > other.w; | |
1440 return new NativeInt32x4._truncated(_cx ? -1 : 0, | |
1441 _cy ? -1 : 0, | |
1442 _cz ? -1 : 0, | |
1443 _cw ? -1 : 0); | |
1444 } | |
1445 | |
1446 /// Relational greater than or equal. | |
1447 Int32x4 greaterThanOrEqual(Float32x4 other) { | |
1448 bool _cx = x >= other.x; | |
1449 bool _cy = y >= other.y; | |
1450 bool _cz = z >= other.z; | |
1451 bool _cw = w >= other.w; | |
1452 return new NativeInt32x4._truncated(_cx ? -1 : 0, | |
1453 _cy ? -1 : 0, | |
1454 _cz ? -1 : 0, | |
1455 _cw ? -1 : 0); | |
1456 } | |
1457 | |
1458 /// Relational equal. | |
1459 Int32x4 equal(Float32x4 other) { | |
1460 bool _cx = x == other.x; | |
1461 bool _cy = y == other.y; | |
1462 bool _cz = z == other.z; | |
1463 bool _cw = w == other.w; | |
1464 return new NativeInt32x4._truncated(_cx ? -1 : 0, | |
1465 _cy ? -1 : 0, | |
1466 _cz ? -1 : 0, | |
1467 _cw ? -1 : 0); | |
1468 } | |
1469 | |
1470 /// Relational not-equal. | |
1471 Int32x4 notEqual(Float32x4 other) { | |
1472 bool _cx = x != other.x; | |
1473 bool _cy = y != other.y; | |
1474 bool _cz = z != other.z; | |
1475 bool _cw = w != other.w; | |
1476 return new NativeInt32x4._truncated(_cx ? -1 : 0, | |
1477 _cy ? -1 : 0, | |
1478 _cz ? -1 : 0, | |
1479 _cw ? -1 : 0); | |
1480 } | |
1481 | |
1482 /// Returns a copy of [this] each lane being scaled by [s]. | |
1483 Float32x4 scale(double s) { | |
1484 double _x = s * x; | |
1485 double _y = s * y; | |
1486 double _z = s * z; | |
1487 double _w = s * w; | |
1488 return new NativeFloat32x4._doubles(_x, _y, _z, _w); | |
1489 } | |
1490 | |
1491 /// Returns the absolute value of this [Float32x4]. | |
1492 Float32x4 abs() { | |
1493 double _x = x.abs(); | |
1494 double _y = y.abs(); | |
1495 double _z = z.abs(); | |
1496 double _w = w.abs(); | |
1497 return new NativeFloat32x4._truncated(_x, _y, _z, _w); | |
1498 } | |
1499 | |
1500 /// Clamps [this] to be in the range [lowerLimit]-[upperLimit]. | |
1501 Float32x4 clamp(Float32x4 lowerLimit, Float32x4 upperLimit) { | |
1502 double _lx = lowerLimit.x; | |
1503 double _ly = lowerLimit.y; | |
1504 double _lz = lowerLimit.z; | |
1505 double _lw = lowerLimit.w; | |
1506 double _ux = upperLimit.x; | |
1507 double _uy = upperLimit.y; | |
1508 double _uz = upperLimit.z; | |
1509 double _uw = upperLimit.w; | |
1510 double _x = x; | |
1511 double _y = y; | |
1512 double _z = z; | |
1513 double _w = w; | |
1514 // MAX(MIN(self, upper), lower). | |
1515 _x = _x > _ux ? _ux : _x; | |
1516 _y = _y > _uy ? _uy : _y; | |
1517 _z = _z > _uz ? _uz : _z; | |
1518 _w = _w > _uw ? _uw : _w; | |
1519 _x = _x < _lx ? _lx : _x; | |
1520 _y = _y < _ly ? _ly : _y; | |
1521 _z = _z < _lz ? _lz : _z; | |
1522 _w = _w < _lw ? _lw : _w; | |
1523 return new NativeFloat32x4._truncated(_x, _y, _z, _w); | |
1524 } | |
1525 | |
1526 /// Extract the sign bit from each lane return them in the first 4 bits. | |
1527 int get signMask { | |
1528 var view = _uint32view; | |
1529 var mx, my, mz, mw; | |
1530 _list[0] = x; | |
1531 _list[1] = y; | |
1532 _list[2] = z; | |
1533 _list[3] = w; | |
1534 // This is correct because dart2js uses the unsigned right shift. | |
1535 mx = (view[0] & 0x80000000) >> 31; | |
1536 my = (view[1] & 0x80000000) >> 30; | |
1537 mz = (view[2] & 0x80000000) >> 29; | |
1538 mw = (view[3] & 0x80000000) >> 28; | |
1539 return mx | my | mz | mw; | |
1540 } | |
1541 | |
1542 /// Shuffle the lane values. [mask] must be one of the 256 shuffle constants. | |
1543 Float32x4 shuffle(int mask) { | |
1544 if ((mask < 0) || (mask > 255)) { | |
1545 throw new RangeError.range(mask, 0, 255, "mask"); | |
1546 } | |
1547 _list[0] = x; | |
1548 _list[1] = y; | |
1549 _list[2] = z; | |
1550 _list[3] = w; | |
1551 | |
1552 double _x = _list[mask & 0x3]; | |
1553 double _y = _list[(mask >> 2) & 0x3]; | |
1554 double _z = _list[(mask >> 4) & 0x3]; | |
1555 double _w = _list[(mask >> 6) & 0x3]; | |
1556 return new NativeFloat32x4._truncated(_x, _y, _z, _w); | |
1557 } | |
1558 | |
1559 /// Shuffle the lane values in [this] and [other]. The returned | |
1560 /// Float32x4 will have XY lanes from [this] and ZW lanes from [other]. | |
1561 /// Uses the same [mask] as [shuffle]. | |
1562 Float32x4 shuffleMix(Float32x4 other, int mask) { | |
1563 if ((mask < 0) || (mask > 255)) { | |
1564 throw new RangeError.range(mask, 0, 255, "mask"); | |
1565 } | |
1566 _list[0] = x; | |
1567 _list[1] = y; | |
1568 _list[2] = z; | |
1569 _list[3] = w; | |
1570 double _x = _list[mask & 0x3]; | |
1571 double _y = _list[(mask >> 2) & 0x3]; | |
1572 | |
1573 _list[0] = other.x; | |
1574 _list[1] = other.y; | |
1575 _list[2] = other.z; | |
1576 _list[3] = other.w; | |
1577 double _z = _list[(mask >> 4) & 0x3]; | |
1578 double _w = _list[(mask >> 6) & 0x3]; | |
1579 return new NativeFloat32x4._truncated(_x, _y, _z, _w); | |
1580 } | |
1581 | |
1582 /// Copy [this] and replace the [x] lane. | |
1583 Float32x4 withX(double newX) { | |
1584 return new NativeFloat32x4._truncated(_truncate(newX), y, z, w); | |
1585 } | |
1586 | |
1587 /// Copy [this] and replace the [y] lane. | |
1588 Float32x4 withY(double newY) { | |
1589 return new NativeFloat32x4._truncated(x, _truncate(newY), z, w); | |
1590 } | |
1591 | |
1592 /// Copy [this] and replace the [z] lane. | |
1593 Float32x4 withZ(double newZ) { | |
1594 return new NativeFloat32x4._truncated(x, y, _truncate(newZ), w); | |
1595 } | |
1596 | |
1597 /// Copy [this] and replace the [w] lane. | |
1598 Float32x4 withW(double newW) { | |
1599 return new NativeFloat32x4._truncated(x, y, z, _truncate(newW)); | |
1600 } | |
1601 | |
1602 /// Returns the lane-wise minimum value in [this] or [other]. | |
1603 Float32x4 min(Float32x4 other) { | |
1604 double _x = x < other.x ? x : other.x; | |
1605 double _y = y < other.y ? y : other.y; | |
1606 double _z = z < other.z ? z : other.z; | |
1607 double _w = w < other.w ? w : other.w; | |
1608 return new NativeFloat32x4._truncated(_x, _y, _z, _w); | |
1609 } | |
1610 | |
1611 /// Returns the lane-wise maximum value in [this] or [other]. | |
1612 Float32x4 max(Float32x4 other) { | |
1613 double _x = x > other.x ? x : other.x; | |
1614 double _y = y > other.y ? y : other.y; | |
1615 double _z = z > other.z ? z : other.z; | |
1616 double _w = w > other.w ? w : other.w; | |
1617 return new NativeFloat32x4._truncated(_x, _y, _z, _w); | |
1618 } | |
1619 | |
1620 /// Returns the square root of [this]. | |
1621 Float32x4 sqrt() { | |
1622 double _x = Math.sqrt(x); | |
1623 double _y = Math.sqrt(y); | |
1624 double _z = Math.sqrt(z); | |
1625 double _w = Math.sqrt(w); | |
1626 return new NativeFloat32x4._doubles(_x, _y, _z, _w); | |
1627 } | |
1628 | |
1629 /// Returns the reciprocal of [this]. | |
1630 Float32x4 reciprocal() { | |
1631 double _x = 1.0 / x; | |
1632 double _y = 1.0 / y; | |
1633 double _z = 1.0 / z; | |
1634 double _w = 1.0 / w; | |
1635 return new NativeFloat32x4._doubles(_x, _y, _z, _w); | |
1636 } | |
1637 | |
1638 /// Returns the square root of the reciprocal of [this]. | |
1639 Float32x4 reciprocalSqrt() { | |
1640 double _x = Math.sqrt(1.0 / x); | |
1641 double _y = Math.sqrt(1.0 / y); | |
1642 double _z = Math.sqrt(1.0 / z); | |
1643 double _w = Math.sqrt(1.0 / w); | |
1644 return new NativeFloat32x4._doubles(_x, _y, _z, _w); | |
1645 } | |
1646 } | |
1647 | |
1648 | |
1649 /** | |
1650 * Interface of Dart Int32x4 and operations. | |
1651 * Int32x4 stores 4 32-bit bit-masks in "lanes". | |
1652 * The lanes are "x", "y", "z", and "w" respectively. | |
1653 */ | |
1654 class NativeInt32x4 implements Int32x4 { | |
1655 final int x; | |
1656 final int y; | |
1657 final int z; | |
1658 final int w; | |
1659 | |
1660 static final _list = new NativeInt32List(4); | |
1661 | |
1662 static _truncate(x) { | |
1663 _list[0] = x; | |
1664 return _list[0]; | |
1665 } | |
1666 | |
1667 NativeInt32x4(int x, int y, int z, int w) | |
1668 : this.x = _truncate(x), | |
1669 this.y = _truncate(y), | |
1670 this.z = _truncate(z), | |
1671 this.w = _truncate(w) { | |
1672 if (x != this.x && x is! int) throw new ArgumentError(x); | |
1673 if (y != this.y && y is! int) throw new ArgumentError(y); | |
1674 if (z != this.z && z is! int) throw new ArgumentError(z); | |
1675 if (w != this.w && w is! int) throw new ArgumentError(w); | |
1676 } | |
1677 | |
1678 NativeInt32x4.bool(bool x, bool y, bool z, bool w) | |
1679 : this.x = x ? -1 : 0, | |
1680 this.y = y ? -1 : 0, | |
1681 this.z = z ? -1 : 0, | |
1682 this.w = w ? -1 : 0; | |
1683 | |
1684 /// Returns a bit-wise copy of [f] as a Int32x4. | |
1685 factory NativeInt32x4.fromFloat32x4Bits(Float32x4 f) { | |
1686 NativeFloat32List floatList = NativeFloat32x4._list; | |
1687 floatList[0] = f.x; | |
1688 floatList[1] = f.y; | |
1689 floatList[2] = f.z; | |
1690 floatList[3] = f.w; | |
1691 NativeInt32List view = floatList.buffer.asInt32List(); | |
1692 return new NativeInt32x4._truncated(view[0], view[1], view[2], view[3]); | |
1693 } | |
1694 | |
1695 NativeInt32x4._truncated(this.x, this.y, this.z, this.w); | |
1696 | |
1697 String toString() => '[$x, $y, $z, $w]'; | |
1698 | |
1699 | |
1700 /// The bit-wise or operator. | |
1701 Int32x4 operator|(Int32x4 other) { | |
1702 // Dart2js uses unsigned results for bit-operations. | |
1703 // We use "JS" to fall back to the signed versions. | |
1704 return new NativeInt32x4._truncated(JS("int", "# | #", x, other.x), | |
1705 JS("int", "# | #", y, other.y), | |
1706 JS("int", "# | #", z, other.z), | |
1707 JS("int", "# | #", w, other.w)); | |
1708 } | |
1709 | |
1710 /// The bit-wise and operator. | |
1711 Int32x4 operator&(Int32x4 other) { | |
1712 // Dart2js uses unsigned results for bit-operations. | |
1713 // We use "JS" to fall back to the signed versions. | |
1714 return new NativeInt32x4._truncated(JS("int", "# & #", x, other.x), | |
1715 JS("int", "# & #", y, other.y), | |
1716 JS("int", "# & #", z, other.z), | |
1717 JS("int", "# & #", w, other.w)); | |
1718 } | |
1719 | |
1720 /// The bit-wise xor operator. | |
1721 Int32x4 operator^(Int32x4 other) { | |
1722 // Dart2js uses unsigned results for bit-operations. | |
1723 // We use "JS" to fall back to the signed versions. | |
1724 return new NativeInt32x4._truncated(JS("int", "# ^ #", x, other.x), | |
1725 JS("int", "# ^ #", y, other.y), | |
1726 JS("int", "# ^ #", z, other.z), | |
1727 JS("int", "# ^ #", w, other.w)); | |
1728 } | |
1729 | |
1730 Int32x4 operator+(Int32x4 other) { | |
1731 // Avoid going through the typed array by "| 0" the result. | |
1732 return new NativeInt32x4._truncated(JS("int", "(# + #) | 0", x, other.x), | |
1733 JS("int", "(# + #) | 0", y, other.y), | |
1734 JS("int", "(# + #) | 0", z, other.z), | |
1735 JS("int", "(# + #) | 0", w, other.w)); | |
1736 } | |
1737 | |
1738 Int32x4 operator-(Int32x4 other) { | |
1739 // Avoid going through the typed array by "| 0" the result. | |
1740 return new NativeInt32x4._truncated(JS("int", "(# - #) | 0", x, other.x), | |
1741 JS("int", "(# - #) | 0", y, other.y), | |
1742 JS("int", "(# - #) | 0", z, other.z), | |
1743 JS("int", "(# - #) | 0", w, other.w)); | |
1744 } | |
1745 | |
1746 Int32x4 operator-() { | |
1747 // Avoid going through the typed array by "| 0" the result. | |
1748 return new NativeInt32x4._truncated(JS("int", "(-#) | 0", x), | |
1749 JS("int", "(-#) | 0", y), | |
1750 JS("int", "(-#) | 0", z), | |
1751 JS("int", "(-#) | 0", w)); | |
1752 } | |
1753 | |
1754 /// Extract the top bit from each lane return them in the first 4 bits. | |
1755 int get signMask { | |
1756 int mx = (x & 0x80000000) >> 31; | |
1757 int my = (y & 0x80000000) >> 31; | |
1758 int mz = (z & 0x80000000) >> 31; | |
1759 int mw = (w & 0x80000000) >> 31; | |
1760 return mx | my << 1 | mz << 2 | mw << 3; | |
1761 } | |
1762 | |
1763 /// Shuffle the lane values. [mask] must be one of the 256 shuffle constants. | |
1764 Int32x4 shuffle(int mask) { | |
1765 if ((mask < 0) || (mask > 255)) { | |
1766 throw new RangeError.range(mask, 0, 255, "mask"); | |
1767 } | |
1768 _list[0] = x; | |
1769 _list[1] = y; | |
1770 _list[2] = z; | |
1771 _list[3] = w; | |
1772 int _x = _list[mask & 0x3]; | |
1773 int _y = _list[(mask >> 2) & 0x3]; | |
1774 int _z = _list[(mask >> 4) & 0x3]; | |
1775 int _w = _list[(mask >> 6) & 0x3]; | |
1776 return new NativeInt32x4._truncated(_x, _y, _z, _w); | |
1777 } | |
1778 | |
1779 /// Shuffle the lane values in [this] and [other]. The returned | |
1780 /// Int32x4 will have XY lanes from [this] and ZW lanes from [other]. | |
1781 /// Uses the same [mask] as [shuffle]. | |
1782 Int32x4 shuffleMix(Int32x4 other, int mask) { | |
1783 if ((mask < 0) || (mask > 255)) { | |
1784 throw new RangeError.range(mask, 0, 255, "mask"); | |
1785 } | |
1786 _list[0] = x; | |
1787 _list[1] = y; | |
1788 _list[2] = z; | |
1789 _list[3] = w; | |
1790 int _x = _list[mask & 0x3]; | |
1791 int _y = _list[(mask >> 2) & 0x3]; | |
1792 | |
1793 _list[0] = other.x; | |
1794 _list[1] = other.y; | |
1795 _list[2] = other.z; | |
1796 _list[3] = other.w; | |
1797 int _z = _list[(mask >> 4) & 0x3]; | |
1798 int _w = _list[(mask >> 6) & 0x3]; | |
1799 return new NativeInt32x4._truncated(_x, _y, _z, _w); | |
1800 } | |
1801 | |
1802 /// Returns a new [Int32x4] copied from [this] with a new x value. | |
1803 Int32x4 withX(int x) { | |
1804 int _x = _truncate(x); | |
1805 return new NativeInt32x4._truncated(_x, y, z, w); | |
1806 } | |
1807 | |
1808 /// Returns a new [Int32x4] copied from [this] with a new y value. | |
1809 Int32x4 withY(int y) { | |
1810 int _y = _truncate(y); | |
1811 return new NativeInt32x4._truncated(x, _y, z, w); | |
1812 } | |
1813 | |
1814 /// Returns a new [Int32x4] copied from [this] with a new z value. | |
1815 Int32x4 withZ(int z) { | |
1816 int _z = _truncate(z); | |
1817 return new NativeInt32x4._truncated(x, y, _z, w); | |
1818 } | |
1819 | |
1820 /// Returns a new [Int32x4] copied from [this] with a new w value. | |
1821 Int32x4 withW(int w) { | |
1822 int _w = _truncate(w); | |
1823 return new NativeInt32x4._truncated(x, y, z, _w); | |
1824 } | |
1825 | |
1826 /// Extracted x value. Returns `false` for 0, `true` for any other value. | |
1827 bool get flagX => x != 0; | |
1828 /// Extracted y value. Returns `false` for 0, `true` for any other value. | |
1829 bool get flagY => y != 0; | |
1830 /// Extracted z value. Returns `false` for 0, `true` for any other value. | |
1831 bool get flagZ => z != 0; | |
1832 /// Extracted w value. Returns `false` for 0, `true` for any other value. | |
1833 bool get flagW => w != 0; | |
1834 | |
1835 /// Returns a new [Int32x4] copied from [this] with a new x value. | |
1836 Int32x4 withFlagX(bool flagX) { | |
1837 int _x = flagX ? -1 : 0; | |
1838 return new NativeInt32x4._truncated(_x, y, z, w); | |
1839 } | |
1840 | |
1841 /// Returns a new [Int32x4] copied from [this] with a new y value. | |
1842 Int32x4 withFlagY(bool flagY) { | |
1843 int _y = flagY ? -1 : 0; | |
1844 return new NativeInt32x4._truncated(x, _y, z, w); | |
1845 } | |
1846 | |
1847 /// Returns a new [Int32x4] copied from [this] with a new z value. | |
1848 Int32x4 withFlagZ(bool flagZ) { | |
1849 int _z = flagZ ? -1 : 0; | |
1850 return new NativeInt32x4._truncated(x, y, _z, w); | |
1851 } | |
1852 | |
1853 /// Returns a new [Int32x4] copied from [this] with a new w value. | |
1854 Int32x4 withFlagW(bool flagW) { | |
1855 int _w = flagW ? -1 : 0; | |
1856 return new NativeInt32x4._truncated(x, y, z, _w); | |
1857 } | |
1858 | |
1859 /// Merge [trueValue] and [falseValue] based on [this]' bit mask: | |
1860 /// Select bit from [trueValue] when bit in [this] is on. | |
1861 /// Select bit from [falseValue] when bit in [this] is off. | |
1862 Float32x4 select(Float32x4 trueValue, Float32x4 falseValue) { | |
1863 var floatList = NativeFloat32x4._list; | |
1864 var intView = NativeFloat32x4._uint32view; | |
1865 | |
1866 floatList[0] = trueValue.x; | |
1867 floatList[1] = trueValue.y; | |
1868 floatList[2] = trueValue.z; | |
1869 floatList[3] = trueValue.w; | |
1870 int stx = intView[0]; | |
1871 int sty = intView[1]; | |
1872 int stz = intView[2]; | |
1873 int stw = intView[3]; | |
1874 | |
1875 floatList[0] = falseValue.x; | |
1876 floatList[1] = falseValue.y; | |
1877 floatList[2] = falseValue.z; | |
1878 floatList[3] = falseValue.w; | |
1879 int sfx = intView[0]; | |
1880 int sfy = intView[1]; | |
1881 int sfz = intView[2]; | |
1882 int sfw = intView[3]; | |
1883 int _x = (x & stx) | (~x & sfx); | |
1884 int _y = (y & sty) | (~y & sfy); | |
1885 int _z = (z & stz) | (~z & sfz); | |
1886 int _w = (w & stw) | (~w & sfw); | |
1887 intView[0] = _x; | |
1888 intView[1] = _y; | |
1889 intView[2] = _z; | |
1890 intView[3] = _w; | |
1891 return new NativeFloat32x4._truncated( | |
1892 floatList[0], floatList[1], floatList[2], floatList[3]); | |
1893 } | |
1894 } | |
1895 | |
1896 class NativeFloat64x2 implements Float64x2 { | |
1897 final double x; | |
1898 final double y; | |
1899 | |
1900 static NativeFloat64List _list = new NativeFloat64List(2); | |
1901 static NativeUint32List _uint32View = _list.buffer.asUint32List(); | |
1902 | |
1903 NativeFloat64x2(this.x, this.y) { | |
1904 if (x is! num) throw new ArgumentError(x); | |
1905 if (y is! num) throw new ArgumentError(y); | |
1906 } | |
1907 | |
1908 NativeFloat64x2.splat(double v) : this(v, v); | |
1909 | |
1910 NativeFloat64x2.zero() : this.splat(0.0); | |
1911 | |
1912 NativeFloat64x2.fromFloat32x4(Float32x4 v) : this(v.x, v.y); | |
1913 | |
1914 /// Arguments [x] and [y] must be doubles. | |
1915 NativeFloat64x2._doubles(this.x, this.y); | |
1916 | |
1917 String toString() => '[$x, $y]'; | |
1918 | |
1919 /// Addition operator. | |
1920 Float64x2 operator+(Float64x2 other) { | |
1921 return new NativeFloat64x2._doubles(x + other.x, y + other.y); | |
1922 } | |
1923 | |
1924 /// Negate operator. | |
1925 Float64x2 operator-() { | |
1926 return new NativeFloat64x2._doubles(-x, -y); | |
1927 } | |
1928 | |
1929 /// Subtraction operator. | |
1930 Float64x2 operator-(Float64x2 other) { | |
1931 return new NativeFloat64x2._doubles(x - other.x, y - other.y); | |
1932 } | |
1933 /// Multiplication operator. | |
1934 Float64x2 operator*(Float64x2 other) { | |
1935 return new NativeFloat64x2._doubles(x * other.x, y * other.y); | |
1936 } | |
1937 /// Division operator. | |
1938 Float64x2 operator/(Float64x2 other) { | |
1939 return new NativeFloat64x2._doubles(x / other.x, y / other.y); | |
1940 } | |
1941 | |
1942 /// Returns a copy of [this] each lane being scaled by [s]. | |
1943 Float64x2 scale(double s) { | |
1944 return new NativeFloat64x2._doubles(x * s, y * s); | |
1945 } | |
1946 | |
1947 /// Returns the absolute value of this [Float64x2]. | |
1948 Float64x2 abs() { | |
1949 return new NativeFloat64x2._doubles(x.abs(), y.abs()); | |
1950 } | |
1951 | |
1952 /// Clamps [this] to be in the range [lowerLimit]-[upperLimit]. | |
1953 Float64x2 clamp(Float64x2 lowerLimit, | |
1954 Float64x2 upperLimit) { | |
1955 double _lx = lowerLimit.x; | |
1956 double _ly = lowerLimit.y; | |
1957 double _ux = upperLimit.x; | |
1958 double _uy = upperLimit.y; | |
1959 double _x = x; | |
1960 double _y = y; | |
1961 // MAX(MIN(self, upper), lower). | |
1962 _x = _x > _ux ? _ux : _x; | |
1963 _y = _y > _uy ? _uy : _y; | |
1964 _x = _x < _lx ? _lx : _x; | |
1965 _y = _y < _ly ? _ly : _y; | |
1966 return new NativeFloat64x2._doubles(_x, _y); | |
1967 } | |
1968 | |
1969 /// Extract the sign bits from each lane return them in the first 2 bits. | |
1970 int get signMask { | |
1971 var view = _uint32View; | |
1972 _list[0] = x; | |
1973 _list[1] = y; | |
1974 var mx = (view[1] & 0x80000000) >> 31; | |
1975 var my = (view[3] & 0x80000000) >> 31; | |
1976 return mx | my << 1; | |
1977 } | |
1978 | |
1979 /// Returns a new [Float64x2] copied from [this] with a new x value. | |
1980 Float64x2 withX(double x) { | |
1981 if (x is! num) throw new ArgumentError(x); | |
1982 return new NativeFloat64x2._doubles(x, y); | |
1983 } | |
1984 | |
1985 /// Returns a new [Float64x2] copied from [this] with a new y value. | |
1986 Float64x2 withY(double y) { | |
1987 if (y is! num) throw new ArgumentError(y); | |
1988 return new NativeFloat64x2._doubles(x, y); | |
1989 } | |
1990 | |
1991 /// Returns the lane-wise minimum value in [this] or [other]. | |
1992 Float64x2 min(Float64x2 other) { | |
1993 return new NativeFloat64x2._doubles(x < other.x ? x : other.x, | |
1994 y < other.y ? y : other.y); | |
1995 | |
1996 } | |
1997 | |
1998 /// Returns the lane-wise maximum value in [this] or [other]. | |
1999 Float64x2 max(Float64x2 other) { | |
2000 return new NativeFloat64x2._doubles(x > other.x ? x : other.x, | |
2001 y > other.y ? y : other.y); | |
2002 } | |
2003 | |
2004 /// Returns the lane-wise square root of [this]. | |
2005 Float64x2 sqrt() { | |
2006 return new NativeFloat64x2._doubles(Math.sqrt(x), Math.sqrt(y)); | |
2007 } | |
2008 } | |
OLD | NEW |