| 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 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 | 292 |
| 293 /* patch */ factory Float64x2.fromFloat32x4(Float32x4 v) { | 293 /* patch */ factory Float64x2.fromFloat32x4(Float32x4 v) { |
| 294 return new _Float64x2.fromFloat32x4(v); | 294 return new _Float64x2.fromFloat32x4(v); |
| 295 } | 295 } |
| 296 } | 296 } |
| 297 | 297 |
| 298 | 298 |
| 299 patch class ByteData { | 299 patch class ByteData { |
| 300 /* patch */ factory ByteData(int length) { | 300 /* patch */ factory ByteData(int length) { |
| 301 var list = new _Uint8Array(length); | 301 var list = new _Uint8Array(length); |
| 302 return new _ByteDataView(list.buffer, 0, length); | 302 return new _ByteDataView(list, 0, length); |
| 303 } | 303 } |
| 304 | 304 |
| 305 /* patch */ factory ByteData.view(ByteBuffer buffer, | 305 /* patch */ factory ByteData.view(ByteBuffer buffer, |
| 306 [int offsetInBytes = 0, int length]) { | 306 [int offsetInBytes = 0, int length]) { |
| 307 if (length == null) { | 307 if (length == null) { |
| 308 length = buffer.lengthInBytes - offsetInBytes; | 308 length = buffer.lengthInBytes - offsetInBytes; |
| 309 } | 309 } |
| 310 return new _ByteDataView(buffer, offsetInBytes, length); | 310 return new _ByteDataView(buffer._data, offsetInBytes, length); |
| 311 } |
| 312 |
| 313 // Called directly from C code. |
| 314 factory ByteData._view(TypedData typedData, int offsetInBytes, int length) { |
| 315 return new _ByteDataView(typedData, offsetInBytes, length); |
| 311 } | 316 } |
| 312 } | 317 } |
| 313 | 318 |
| 314 | 319 |
| 315 // Based class for _TypedList that provides common methods for implementing | 320 // Based class for _TypedList that provides common methods for implementing |
| 316 // the collection and list interfaces. | 321 // the collection and list interfaces. |
| 317 | 322 |
| 318 abstract class _TypedListBase { | 323 abstract class _TypedListBase { |
| 319 | 324 |
| 320 // Method(s) implementing the Collection interface. | 325 // Method(s) implementing the Collection interface. |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 final count = end - start; | 549 final count = end - start; |
| 545 if ((from.length - skipCount) < count) { | 550 if ((from.length - skipCount) < count) { |
| 546 throw new StateError("Not enough elements"); | 551 throw new StateError("Not enough elements"); |
| 547 } | 552 } |
| 548 | 553 |
| 549 if (from is _TypedListBase) { | 554 if (from is _TypedListBase) { |
| 550 if (this.elementSizeInBytes == from.elementSizeInBytes) { | 555 if (this.elementSizeInBytes == from.elementSizeInBytes) { |
| 551 if ((count < 10) && (from.buffer != this.buffer)) { | 556 if ((count < 10) && (from.buffer != this.buffer)) { |
| 552 Lists.copy(from, skipCount, this, start, count); | 557 Lists.copy(from, skipCount, this, start, count); |
| 553 return; | 558 return; |
| 554 } else if (this.buffer._setRange( | 559 } else if (this.buffer._data._setRange( |
| 555 start * elementSizeInBytes + this.offsetInBytes, | 560 start * elementSizeInBytes + this.offsetInBytes, |
| 556 count * elementSizeInBytes, | 561 count * elementSizeInBytes, |
| 557 from.buffer, | 562 from.buffer._data, |
| 558 skipCount * elementSizeInBytes + from.offsetInBytes, | 563 skipCount * elementSizeInBytes + from.offsetInBytes, |
| 559 this._cid, from._cid)) { | 564 this._cid, from._cid)) { |
| 560 return; | 565 return; |
| 561 } | 566 } |
| 562 } else if (from.buffer == this.buffer) { | 567 } else if (from.buffer == this.buffer) { |
| 563 // Different element sizes, but same buffer means that we need | 568 // Different element sizes, but same buffer means that we need |
| 564 // an intermediate structure. | 569 // an intermediate structure. |
| 565 // TODO(srdjan): Optimize to skip copying if the range does not overlap. | 570 // TODO(srdjan): Optimize to skip copying if the range does not overlap. |
| 566 final temp_buffer = new List(count); | 571 final temp_buffer = new List(count); |
| 567 for (int i = 0; i < count; i++) { | 572 for (int i = 0; i < count; i++) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 // Element size of toCid and fromCid must match (test at caller). | 606 // Element size of toCid and fromCid must match (test at caller). |
| 602 bool _setRange(int startInBytes, int lengthInBytes, | 607 bool _setRange(int startInBytes, int lengthInBytes, |
| 603 _TypedListBase from, int startFromInBytes, | 608 _TypedListBase from, int startFromInBytes, |
| 604 int toCid, int fromCid) | 609 int toCid, int fromCid) |
| 605 native "TypedData_setRange"; | 610 native "TypedData_setRange"; |
| 606 | 611 |
| 607 int get _cid native "Object_cid"; | 612 int get _cid native "Object_cid"; |
| 608 } | 613 } |
| 609 | 614 |
| 610 | 615 |
| 611 abstract class _TypedList extends _TypedListBase implements ByteBuffer { | 616 class _ByteBuffer implements ByteBuffer { |
| 617 final _TypedList _data; |
| 618 |
| 619 _ByteBuffer(this._data); |
| 620 |
| 621 // Forward calls to _data. |
| 622 int get lengthInBytes => _data.lengthInBytes; |
| 623 int get hashCode => _data.hashCode; |
| 624 bool operator==(Object other) => |
| 625 (other is _ByteBuffer) && identical(_data, other._data); |
| 626 } |
| 627 |
| 628 |
| 629 abstract class _TypedList extends _TypedListBase { |
| 612 // Default method implementing parts of the TypedData interface. | 630 // Default method implementing parts of the TypedData interface. |
| 613 int get offsetInBytes { | 631 int get offsetInBytes { |
| 614 return 0; | 632 return 0; |
| 615 } | 633 } |
| 616 | 634 |
| 617 int get lengthInBytes { | 635 int get lengthInBytes { |
| 618 return length * elementSizeInBytes; | 636 return length * elementSizeInBytes; |
| 619 } | 637 } |
| 620 | 638 |
| 621 ByteBuffer get buffer { | 639 ByteBuffer get buffer => new _ByteBuffer(this); |
| 622 return this; | |
| 623 } | |
| 624 | 640 |
| 625 // Methods implementing the collection interface. | 641 // Methods implementing the collection interface. |
| 626 | 642 |
| 627 int get length native "TypedData_length"; | 643 int get length native "TypedData_length"; |
| 628 | 644 |
| 629 // Internal utility methods. | 645 // Internal utility methods. |
| 630 | 646 |
| 631 int _getInt8(int offsetInBytes) native "TypedData_GetInt8"; | 647 int _getInt8(int offsetInBytes) native "TypedData_GetInt8"; |
| 632 void _setInt8(int offsetInBytes, int value) native "TypedData_SetInt8"; | 648 void _setInt8(int offsetInBytes, int value) native "TypedData_SetInt8"; |
| 633 | 649 |
| (...skipping 1873 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2507 _position = _length; | 2523 _position = _length; |
| 2508 _current = null; | 2524 _current = null; |
| 2509 return false; | 2525 return false; |
| 2510 } | 2526 } |
| 2511 | 2527 |
| 2512 E get current => _current; | 2528 E get current => _current; |
| 2513 } | 2529 } |
| 2514 | 2530 |
| 2515 | 2531 |
| 2516 class _TypedListView extends _TypedListBase implements TypedData { | 2532 class _TypedListView extends _TypedListBase implements TypedData { |
| 2517 _TypedListView(ByteBuffer _buffer, int _offset, int _length) | 2533 _TypedListView(_ByteBuffer _buffer, int _offset, int _length) |
| 2518 : _typedData = _buffer, // This assignment is type safe. | 2534 : _typedData = _buffer._data, |
| 2519 offsetInBytes = _offset, | 2535 offsetInBytes = _offset, |
| 2520 length = _length { | 2536 length = _length { |
| 2521 } | 2537 } |
| 2522 | 2538 |
| 2523 // Method(s) implementing the TypedData interface. | 2539 // Method(s) implementing the TypedData interface. |
| 2524 | 2540 |
| 2525 int get lengthInBytes { | 2541 int get lengthInBytes { |
| 2526 return length * elementSizeInBytes; | 2542 return length * elementSizeInBytes; |
| 2527 } | 2543 } |
| 2528 | 2544 |
| (...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3256 | 3272 |
| 3257 // Internal utility methods. | 3273 // Internal utility methods. |
| 3258 | 3274 |
| 3259 Float64x2List _createList(int length) { | 3275 Float64x2List _createList(int length) { |
| 3260 return new Float64x2List(length); | 3276 return new Float64x2List(length); |
| 3261 } | 3277 } |
| 3262 } | 3278 } |
| 3263 | 3279 |
| 3264 | 3280 |
| 3265 class _ByteDataView implements ByteData { | 3281 class _ByteDataView implements ByteData { |
| 3266 _ByteDataView(ByteBuffer _buffer, int _offsetInBytes, int _lengthInBytes) | 3282 _ByteDataView(TypedData typedData, int _offsetInBytes, int _lengthInBytes) |
| 3267 : _typedData = _buffer, // _buffer is guaranteed to be a TypedData here. | 3283 : _typedData = typedData, |
| 3268 _offset = _offsetInBytes, | 3284 _offset = _offsetInBytes, |
| 3269 length = _lengthInBytes { | 3285 length = _lengthInBytes { |
| 3270 _rangeCheck(_buffer.lengthInBytes, _offset, length); | 3286 _rangeCheck(_typedData.lengthInBytes, _offset, length); |
| 3271 } | 3287 } |
| 3272 | 3288 |
| 3273 | 3289 |
| 3274 // Method(s) implementing TypedData interface. | 3290 // Method(s) implementing TypedData interface. |
| 3275 | 3291 |
| 3276 ByteBuffer get buffer { | 3292 ByteBuffer get buffer { |
| 3277 return _typedData.buffer; | 3293 return _typedData.buffer; |
| 3278 } | 3294 } |
| 3279 | 3295 |
| 3280 int get lengthInBytes { | 3296 int get lengthInBytes { |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3630 return value; | 3646 return value; |
| 3631 } | 3647 } |
| 3632 return object; | 3648 return object; |
| 3633 } | 3649 } |
| 3634 | 3650 |
| 3635 | 3651 |
| 3636 void _throwRangeError(int index, int length) { | 3652 void _throwRangeError(int index, int length) { |
| 3637 String message = "$index must be in the range [0..$length)"; | 3653 String message = "$index must be in the range [0..$length)"; |
| 3638 throw new RangeError(message); | 3654 throw new RangeError(message); |
| 3639 } | 3655 } |
| OLD | NEW |