| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 // patch classes for Int8List ..... Float64List and ByteData implementations. | 5 // patch classes for Int8List ..... Float64List and ByteData implementations. |
| 6 | 6 |
| 7 patch class Int8List { | 7 patch class Int8List { |
| 8 /* patch */ factory Int8List(int length) { | 8 /* patch */ factory Int8List(int length) { |
| 9 return new _Int8Array(length); | 9 return new _Int8Array(length); |
| 10 } | 10 } |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 } | 291 } |
| 292 /* patch */ factory Int32x4.fromFloat32x4Bits(Float32x4 x) { | 292 /* patch */ factory Int32x4.fromFloat32x4Bits(Float32x4 x) { |
| 293 return new _Int32x4.fromFloat32x4Bits(x); | 293 return new _Int32x4.fromFloat32x4Bits(x); |
| 294 } | 294 } |
| 295 } | 295 } |
| 296 | 296 |
| 297 | 297 |
| 298 patch class ByteData { | 298 patch class ByteData { |
| 299 /* patch */ factory ByteData(int length) { | 299 /* patch */ factory ByteData(int length) { |
| 300 var list = new _Uint8Array(length); | 300 var list = new _Uint8Array(length); |
| 301 return new _ByteDataView(list, 0, length); | 301 return new _ByteDataView(list.buffer, 0, length); |
| 302 } | 302 } |
| 303 | 303 |
| 304 /* patch */ factory ByteData.view(ByteBuffer buffer, | 304 /* patch */ factory ByteData.view(ByteBuffer buffer, |
| 305 [int offsetInBytes = 0, int length]) { | 305 [int offsetInBytes = 0, int length]) { |
| 306 if (length == null) { | 306 if (length == null) { |
| 307 length = buffer.lengthInBytes - offsetInBytes; | 307 length = buffer.lengthInBytes - offsetInBytes; |
| 308 } | 308 } |
| 309 _ByteBuffer internalBuffer = buffer; | 309 return new _ByteDataView(buffer, offsetInBytes, length); |
| 310 return new _ByteDataView(internalBuffer._typedData, offsetInBytes, length); | |
| 311 } | 310 } |
| 312 | |
| 313 // Called directly from C code. | |
| 314 factory ByteData._view(TypedData typedData, int offsetInBytes, int length) | |
| 315 => new _ByteDataView(typedData, offsetInBytes, length); | |
| 316 } | 311 } |
| 317 | 312 |
| 318 | 313 |
| 319 // Based class for _TypedList that provides common methods for implementing | 314 // Based class for _TypedList that provides common methods for implementing |
| 320 // the collection and list interfaces. | 315 // the collection and list interfaces. |
| 321 | 316 |
| 322 abstract class _TypedListBase { | 317 abstract class _TypedListBase { |
| 323 | 318 |
| 324 // Method(s) implementing the Collection interface. | 319 // Method(s) implementing the Collection interface. |
| 325 bool contains(element) => IterableMixinWorkaround.contains(this, element); | 320 bool contains(element) => IterableMixinWorkaround.contains(this, element); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 throw new StateError("Not enough elements"); | 547 throw new StateError("Not enough elements"); |
| 553 } | 548 } |
| 554 | 549 |
| 555 if (from is _TypedListBase) { | 550 if (from is _TypedListBase) { |
| 556 final needsClamping = | 551 final needsClamping = |
| 557 this._isClamped() && (this._isClamped() != from._isClamped()); | 552 this._isClamped() && (this._isClamped() != from._isClamped()); |
| 558 if (this.elementSizeInBytes == from.elementSizeInBytes) { | 553 if (this.elementSizeInBytes == from.elementSizeInBytes) { |
| 559 if (needsClamping) { | 554 if (needsClamping) { |
| 560 Lists.copy(from, skipCount, this, start, count); | 555 Lists.copy(from, skipCount, this, start, count); |
| 561 return; | 556 return; |
| 562 } | 557 } else if (this.buffer._setRange( |
| 563 _ByteBuffer buffer = this.buffer; | 558 start * elementSizeInBytes + this.offsetInBytes, |
| 564 _ByteBuffer fromBuffer = from.buffer; | 559 count * elementSizeInBytes, |
| 565 if (buffer._typedData._setRange( | 560 from.buffer, |
| 566 start * elementSizeInBytes + this.offsetInBytes, | 561 skipCount * elementSizeInBytes + from.offsetInBytes)) { |
| 567 count * elementSizeInBytes, | |
| 568 fromBuffer._typedData, | |
| 569 skipCount * elementSizeInBytes + from.offsetInBytes)) { | |
| 570 return; | 562 return; |
| 571 } | 563 } |
| 572 } else if (from.buffer == this.buffer) { | 564 } else if (from.buffer == this.buffer) { |
| 573 // Different element sizes, but same buffer means that we need | 565 // Different element sizes, but same buffer means that we need |
| 574 // an intermediate structure. | 566 // an intermediate structure. |
| 575 // TODO(srdjan): Optimize to skip copying if the range does not overlap. | 567 // TODO(srdjan): Optimize to skip copying if the range does not overlap. |
| 576 final temp_buffer = new List(count); | 568 final temp_buffer = new List(count); |
| 577 for (int i = 0; i < count; i++) { | 569 for (int i = 0; i < count; i++) { |
| 578 temp_buffer[i] = from[skipCount + i]; | 570 temp_buffer[i] = from[skipCount + i]; |
| 579 } | 571 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 608 | 600 |
| 609 // Returns true if operation succeeds. | 601 // Returns true if operation succeeds. |
| 610 // Returns false if 'from' and 'this' do not have the same element types. | 602 // Returns false if 'from' and 'this' do not have the same element types. |
| 611 // The copy occurs using a memory copy (no clamping, conversion, etc). | 603 // The copy occurs using a memory copy (no clamping, conversion, etc). |
| 612 bool _setRange(int startInBytes, int lengthInBytes, | 604 bool _setRange(int startInBytes, int lengthInBytes, |
| 613 _TypedListBase from, int startFromInBytes) | 605 _TypedListBase from, int startFromInBytes) |
| 614 native "TypedData_setRange"; | 606 native "TypedData_setRange"; |
| 615 } | 607 } |
| 616 | 608 |
| 617 | 609 |
| 618 abstract class _TypedList extends _TypedListBase { | 610 abstract class _TypedList extends _TypedListBase implements ByteBuffer { |
| 619 // Default method implementing parts of the TypedData interface. | 611 // Default method implementing parts of the TypedData interface. |
| 620 int get offsetInBytes { | 612 int get offsetInBytes { |
| 621 return 0; | 613 return 0; |
| 622 } | 614 } |
| 623 | 615 |
| 624 int get lengthInBytes { | 616 int get lengthInBytes { |
| 625 return length * elementSizeInBytes; | 617 return length * elementSizeInBytes; |
| 626 } | 618 } |
| 627 | 619 |
| 628 ByteBuffer get buffer => new _ByteBuffer(this); | 620 ByteBuffer get buffer { |
| 621 return this; |
| 622 } |
| 629 | 623 |
| 630 // Methods implementing the collection interface. | 624 // Methods implementing the collection interface. |
| 631 | 625 |
| 632 int get length native "TypedData_length"; | 626 int get length native "TypedData_length"; |
| 633 | 627 |
| 634 // Internal utility methods. | 628 // Internal utility methods. |
| 635 | 629 |
| 636 int _getInt8(int offsetInBytes) native "TypedData_GetInt8"; | 630 int _getInt8(int offsetInBytes) native "TypedData_GetInt8"; |
| 637 void _setInt8(int offsetInBytes, int value) native "TypedData_SetInt8"; | 631 void _setInt8(int offsetInBytes, int value) native "TypedData_SetInt8"; |
| 638 | 632 |
| (...skipping 1694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2333 _position = _length; | 2327 _position = _length; |
| 2334 _current = null; | 2328 _current = null; |
| 2335 return false; | 2329 return false; |
| 2336 } | 2330 } |
| 2337 | 2331 |
| 2338 E get current => _current; | 2332 E get current => _current; |
| 2339 } | 2333 } |
| 2340 | 2334 |
| 2341 | 2335 |
| 2342 class _TypedListView extends _TypedListBase implements TypedData { | 2336 class _TypedListView extends _TypedListBase implements TypedData { |
| 2343 final TypedData _typedData; | |
| 2344 final int offsetInBytes; | |
| 2345 final int length; | |
| 2346 | |
| 2347 _TypedListView(ByteBuffer _buffer, int _offset, int _length) | 2337 _TypedListView(ByteBuffer _buffer, int _offset, int _length) |
| 2348 : _typedData = (_buffer as _ByteBuffer)._typedData, | 2338 : _typedData = _buffer, // This assignment is type safe. |
| 2349 offsetInBytes = _offset, | 2339 offsetInBytes = _offset, |
| 2350 length = _length; | 2340 length = _length { |
| 2341 } |
| 2351 | 2342 |
| 2352 // Method(s) implementing the TypedData interface. | 2343 // Method(s) implementing the TypedData interface. |
| 2353 | 2344 |
| 2354 int get lengthInBytes { | 2345 int get lengthInBytes { |
| 2355 return length * elementSizeInBytes; | 2346 return length * elementSizeInBytes; |
| 2356 } | 2347 } |
| 2357 | 2348 |
| 2358 ByteBuffer get buffer { | 2349 ByteBuffer get buffer { |
| 2359 return _typedData.buffer; | 2350 return _typedData.buffer; |
| 2360 } | 2351 } |
| 2352 |
| 2353 final TypedData _typedData; |
| 2354 final int offsetInBytes; |
| 2355 final int length; |
| 2361 } | 2356 } |
| 2362 | 2357 |
| 2363 | 2358 |
| 2364 class _Int8ArrayView extends _TypedListView implements Int8List { | 2359 class _Int8ArrayView extends _TypedListView implements Int8List { |
| 2365 // Constructor. | 2360 // Constructor. |
| 2366 _Int8ArrayView(ByteBuffer buffer, [int _offsetInBytes = 0, int _length]) | 2361 _Int8ArrayView(ByteBuffer buffer, [int _offsetInBytes = 0, int _length]) |
| 2367 : super(buffer, _offsetInBytes, | 2362 : super(buffer, _offsetInBytes, |
| 2368 _defaultIfNull(_length, | 2363 _defaultIfNull(_length, |
| 2369 ((buffer.lengthInBytes - _offsetInBytes) ~/ | 2364 ((buffer.lengthInBytes - _offsetInBytes) ~/ |
| 2370 Int8List.BYTES_PER_ELEMENT))) { | 2365 Int8List.BYTES_PER_ELEMENT))) { |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3031 | 3026 |
| 3032 // Internal utility methods. | 3027 // Internal utility methods. |
| 3033 | 3028 |
| 3034 Int32x4List _createList(int length) { | 3029 Int32x4List _createList(int length) { |
| 3035 return new Int32x4List(length); | 3030 return new Int32x4List(length); |
| 3036 } | 3031 } |
| 3037 } | 3032 } |
| 3038 | 3033 |
| 3039 | 3034 |
| 3040 class _ByteDataView implements ByteData { | 3035 class _ByteDataView implements ByteData { |
| 3041 final TypedData _typedData; | 3036 _ByteDataView(ByteBuffer _buffer, int _offsetInBytes, int _lengthInBytes) |
| 3042 final int _offset; | 3037 : _typedData = _buffer, // _buffer is guaranteed to be a TypedData here. |
| 3043 final int length; | |
| 3044 | |
| 3045 _ByteDataView(TypedData typedData, int _offsetInBytes, int _lengthInBytes) | |
| 3046 : _typedData = typedData, | |
| 3047 _offset = _offsetInBytes, | 3038 _offset = _offsetInBytes, |
| 3048 length = _lengthInBytes { | 3039 length = _lengthInBytes { |
| 3049 _rangeCheck(typedData.lengthInBytes, _offset, length); | 3040 _rangeCheck(_buffer.lengthInBytes, _offset, length); |
| 3050 } | 3041 } |
| 3051 | 3042 |
| 3043 |
| 3052 // Method(s) implementing TypedData interface. | 3044 // Method(s) implementing TypedData interface. |
| 3053 | 3045 |
| 3054 ByteBuffer get buffer { | 3046 ByteBuffer get buffer { |
| 3055 return _typedData.buffer; | 3047 return _typedData.buffer; |
| 3056 } | 3048 } |
| 3057 | 3049 |
| 3058 int get lengthInBytes { | 3050 int get lengthInBytes { |
| 3059 return length; | 3051 return length; |
| 3060 } | 3052 } |
| 3061 | 3053 |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3312 static int _toEndianUint32(int host_value, bool little_endian) | 3304 static int _toEndianUint32(int host_value, bool little_endian) |
| 3313 native "ByteData_ToEndianUint32"; | 3305 native "ByteData_ToEndianUint32"; |
| 3314 static int _toEndianInt64(int host_value, bool little_endian) | 3306 static int _toEndianInt64(int host_value, bool little_endian) |
| 3315 native "ByteData_ToEndianInt64"; | 3307 native "ByteData_ToEndianInt64"; |
| 3316 static int _toEndianUint64(int host_value, bool little_endian) | 3308 static int _toEndianUint64(int host_value, bool little_endian) |
| 3317 native "ByteData_ToEndianUint64"; | 3309 native "ByteData_ToEndianUint64"; |
| 3318 static double _toEndianFloat32(double host_value, bool little_endian) | 3310 static double _toEndianFloat32(double host_value, bool little_endian) |
| 3319 native "ByteData_ToEndianFloat32"; | 3311 native "ByteData_ToEndianFloat32"; |
| 3320 static double _toEndianFloat64(double host_value, bool little_endian) | 3312 static double _toEndianFloat64(double host_value, bool little_endian) |
| 3321 native "ByteData_ToEndianFloat64"; | 3313 native "ByteData_ToEndianFloat64"; |
| 3314 |
| 3315 |
| 3316 final TypedData _typedData; |
| 3317 final int _offset; |
| 3318 final int length; |
| 3322 } | 3319 } |
| 3323 | 3320 |
| 3324 | 3321 |
| 3325 // Top level utility methods. | 3322 // Top level utility methods. |
| 3326 int _toInt(int value, int mask) { | 3323 int _toInt(int value, int mask) { |
| 3327 value &= mask; | 3324 value &= mask; |
| 3328 if (value > (mask >> 1)) value -= mask + 1; | 3325 if (value > (mask >> 1)) value -= mask + 1; |
| 3329 return value; | 3326 return value; |
| 3330 } | 3327 } |
| 3331 | 3328 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3403 return value; | 3400 return value; |
| 3404 } | 3401 } |
| 3405 return object; | 3402 return object; |
| 3406 } | 3403 } |
| 3407 | 3404 |
| 3408 | 3405 |
| 3409 void _throwRangeError(int index, int length) { | 3406 void _throwRangeError(int index, int length) { |
| 3410 String message = "$index must be in the range [0..$length)"; | 3407 String message = "$index must be in the range [0..$length)"; |
| 3411 throw new RangeError(message); | 3408 throw new RangeError(message); |
| 3412 } | 3409 } |
| 3413 | |
| 3414 /** | |
| 3415 * Internal implementation of [ByteBuffer]. | |
| 3416 */ | |
| 3417 class _ByteBuffer implements ByteBuffer { | |
| 3418 final _TypedList _typedData; | |
| 3419 | |
| 3420 _ByteBuffer(this._typedData); | |
| 3421 | |
| 3422 int get lengthInBytes => _typedData.lengthInBytes; | |
| 3423 | |
| 3424 int get hashCode => _typedData.hashCode; | |
| 3425 bool operator==(Object other) => | |
| 3426 other is _ByteBuffer && identical(_typedData, other._typedData); | |
| 3427 } | |
| OLD | NEW |