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