@@ -11,8 +11,9 @@ library dart.typed_data.implementation;
1111import 'dart:collection' ;
1212import 'dart:_internal' ;
1313import 'dart:_interceptors' show JSIndexable, JSUInt32, JSUInt31;
14- import 'dart:_js_helper'
15- show Creates, JavaScriptIndexingBehavior, JSName, Native, Null, Returns;
14+ import 'dart:_js_helper' show
15+ Creates, JavaScriptIndexingBehavior, JSName, Native, Null, Returns,
16+ diagnoseIndexError;
1617import 'dart:_foreign_helper' show JS;
1718import 'dart:math' as Math;
1819
@@ -445,35 +446,34 @@ class NativeTypedData implements TypedData {
445446 @JSName ('BYTES_PER_ELEMENT' )
446447 final int elementSizeInBytes;
447448
448- void _invalidIndex (int index, int length) {
449- if (index < 0 || index >= length) {
450- if (this is List ) {
451- var list = this ; // Typed as dynamic to avoid warning.
452- if (length == list.length) {
453- throw new RangeError .index (index, this );
454- }
455- }
456- throw new RangeError .range (index, 0 , length - 1 );
449+ void _checkIndex (int index, int length) {
450+ if (JS ('bool' , '(# >>> 0) !== #' , index, index) ||
451+ JS ('int' , '#' , index) >= length) { // 'int' guaranteed by above test.
452+ throw diagnoseIndexError (this , index);
453+ }
454+ }
455+
456+ void _invalidPosition (int position, int length) {
457+ if (position is ! int ) {
458+ throw new ArgumentError .value (position, null , 'Invalid list position' );
457459 } else {
458- throw new ArgumentError ( 'Invalid list index $ index ' );
460+ throw new RangeError . range (position, 0 , length );
459461 }
460462 }
461463
462- void _checkIndex (int index , int length) {
463- if (JS ('bool' , '(# >>> 0) !== #' , index, index ) ||
464- JS ('int' , '#' , index ) >= length) { // 'int' guaranteed by above test.
465- _invalidIndex (index , length);
464+ void _checkPosition (int position , int length) {
465+ if (JS ('bool' , '(# >>> 0) !== #' , position, position ) ||
466+ JS ('int' , '#' , position ) > length) { // 'int' guaranteed by above test.
467+ _invalidPosition (position , length);
466468 }
467469 }
468470
469471 int _checkSublistArguments (int start, int end, int length) {
470472 // For `sublist` the [start] and [end] indices are allowed to be equal to
471- // [length]. However, [_checkIndex] only allows indices in the range
472- // 0 .. length - 1. We therefore increment the [length] argument by one
473- // for the [_checkIndex] checks.
474- _checkIndex (start, length + 1 );
473+ // [length].
474+ _checkPosition (start, length);
475475 if (end == null ) return length;
476- _checkIndex (end, length + 1 );
476+ _checkPosition (end, length);
477477 if (start > end) throw new RangeError .range (start, 0 , end);
478478 return end;
479479 }
@@ -862,8 +862,8 @@ abstract class NativeTypedArray extends NativeTypedData
862862 void _setRangeFast (int start, int end,
863863 NativeTypedArray source, int skipCount) {
864864 int targetLength = this .length;
865- _checkIndex (start, targetLength + 1 );
866- _checkIndex (end, targetLength + 1 );
865+ _checkPosition (start, targetLength);
866+ _checkPosition (end, targetLength);
867867 if (start > end) throw new RangeError .range (start, 0 , end);
868868 int count = end - start;
869869
0 commit comments