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 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) => identical(_data, other._data); | |
|
siva
2014/06/17 01:09:57
shouldn't this say
other is _ByteBuffer && identic
Cutch
2014/06/17 14:31:17
Done.
| |
| 625 } | |
| 626 | |
| 627 | |
| 628 abstract class _TypedList extends _TypedListBase { | |
| 612 // Default method implementing parts of the TypedData interface. | 629 // Default method implementing parts of the TypedData interface. |
| 613 int get offsetInBytes { | 630 int get offsetInBytes { |
| 614 return 0; | 631 return 0; |
| 615 } | 632 } |
| 616 | 633 |
| 617 int get lengthInBytes { | 634 int get lengthInBytes { |
| 618 return length * elementSizeInBytes; | 635 return length * elementSizeInBytes; |
| 619 } | 636 } |
| 620 | 637 |
| 621 ByteBuffer get buffer { | 638 ByteBuffer get buffer => new _ByteBuffer(this); |
| 622 return this; | |
| 623 } | |
| 624 | 639 |
| 625 // Methods implementing the collection interface. | 640 // Methods implementing the collection interface. |
| 626 | 641 |
| 627 int get length native "TypedData_length"; | 642 int get length native "TypedData_length"; |
| 628 | 643 |
| 629 // Internal utility methods. | 644 // Internal utility methods. |
| 630 | 645 |
| 631 int _getInt8(int offsetInBytes) native "TypedData_GetInt8"; | 646 int _getInt8(int offsetInBytes) native "TypedData_GetInt8"; |
| 632 void _setInt8(int offsetInBytes, int value) native "TypedData_SetInt8"; | 647 void _setInt8(int offsetInBytes, int value) native "TypedData_SetInt8"; |
| 633 | 648 |
| (...skipping 1873 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2507 _position = _length; | 2522 _position = _length; |
| 2508 _current = null; | 2523 _current = null; |
| 2509 return false; | 2524 return false; |
| 2510 } | 2525 } |
| 2511 | 2526 |
| 2512 E get current => _current; | 2527 E get current => _current; |
| 2513 } | 2528 } |
| 2514 | 2529 |
| 2515 | 2530 |
| 2516 class _TypedListView extends _TypedListBase implements TypedData { | 2531 class _TypedListView extends _TypedListBase implements TypedData { |
| 2517 _TypedListView(ByteBuffer _buffer, int _offset, int _length) | 2532 _TypedListView(_ByteBuffer _buffer, int _offset, int _length) |
| 2518 : _typedData = _buffer, // This assignment is type safe. | 2533 : _typedData = _buffer._data, // This assignment is type safe. |
|
Ivan Posva
2014/06/17 01:07:02
You can drop the assignment comment now.
Cutch
2014/06/17 14:31:17
Done.
| |
| 2519 offsetInBytes = _offset, | 2534 offsetInBytes = _offset, |
| 2520 length = _length { | 2535 length = _length { |
| 2521 } | 2536 } |
| 2522 | 2537 |
| 2523 // Method(s) implementing the TypedData interface. | 2538 // Method(s) implementing the TypedData interface. |
| 2524 | 2539 |
| 2525 int get lengthInBytes { | 2540 int get lengthInBytes { |
| 2526 return length * elementSizeInBytes; | 2541 return length * elementSizeInBytes; |
| 2527 } | 2542 } |
| 2528 | 2543 |
| (...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3256 | 3271 |
| 3257 // Internal utility methods. | 3272 // Internal utility methods. |
| 3258 | 3273 |
| 3259 Float64x2List _createList(int length) { | 3274 Float64x2List _createList(int length) { |
| 3260 return new Float64x2List(length); | 3275 return new Float64x2List(length); |
| 3261 } | 3276 } |
| 3262 } | 3277 } |
| 3263 | 3278 |
| 3264 | 3279 |
| 3265 class _ByteDataView implements ByteData { | 3280 class _ByteDataView implements ByteData { |
| 3266 _ByteDataView(ByteBuffer _buffer, int _offsetInBytes, int _lengthInBytes) | 3281 _ByteDataView(TypedData typedData, int _offsetInBytes, int _lengthInBytes) |
| 3267 : _typedData = _buffer, // _buffer is guaranteed to be a TypedData here. | 3282 : _typedData = typedData, |
| 3268 _offset = _offsetInBytes, | 3283 _offset = _offsetInBytes, |
| 3269 length = _lengthInBytes { | 3284 length = _lengthInBytes { |
| 3270 _rangeCheck(_buffer.lengthInBytes, _offset, length); | 3285 _rangeCheck(_typedData.lengthInBytes, _offset, length); |
| 3271 } | 3286 } |
| 3272 | 3287 |
| 3273 | 3288 |
| 3274 // Method(s) implementing TypedData interface. | 3289 // Method(s) implementing TypedData interface. |
| 3275 | 3290 |
| 3276 ByteBuffer get buffer { | 3291 ByteBuffer get buffer { |
| 3277 return _typedData.buffer; | 3292 return _typedData.buffer; |
| 3278 } | 3293 } |
| 3279 | 3294 |
| 3280 int get lengthInBytes { | 3295 int get lengthInBytes { |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3630 return value; | 3645 return value; |
| 3631 } | 3646 } |
| 3632 return object; | 3647 return object; |
| 3633 } | 3648 } |
| 3634 | 3649 |
| 3635 | 3650 |
| 3636 void _throwRangeError(int index, int length) { | 3651 void _throwRangeError(int index, int length) { |
| 3637 String message = "$index must be in the range [0..$length)"; | 3652 String message = "$index must be in the range [0..$length)"; |
| 3638 throw new RangeError(message); | 3653 throw new RangeError(message); |
| 3639 } | 3654 } |
| OLD | NEW |