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