Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Unified Diff: sdk/lib/_internal/js_runtime/lib/native_typed_data.dart

Issue 1318943005: Update range errors to agree on the numbers. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: More tests Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/js_runtime/lib/native_typed_data.dart
diff --git a/sdk/lib/_internal/js_runtime/lib/native_typed_data.dart b/sdk/lib/_internal/js_runtime/lib/native_typed_data.dart
index d134c6db81766fb4a62ebd0f0d3d10207c0f48b9..ec50a86191f7a59ca4fb777009a699fb88df9e2e 100644
--- a/sdk/lib/_internal/js_runtime/lib/native_typed_data.dart
+++ b/sdk/lib/_internal/js_runtime/lib/native_typed_data.dart
@@ -13,7 +13,7 @@ import 'dart:_internal';
import 'dart:_interceptors' show JSIndexable, JSUInt32, JSUInt31;
import 'dart:_js_helper' show
Creates, JavaScriptIndexingBehavior, JSName, Native, Null, Returns,
- diagnoseIndexError;
+ diagnoseIndexError, diagnoseRangeError;
import 'dart:_foreign_helper' show JS;
import 'dart:math' as Math;
@@ -148,39 +148,10 @@ class NativeFloat32x4List
int get elementSizeInBytes => Float32x4List.BYTES_PER_ELEMENT;
- void _invalidIndex(int index, int length) {
- if (index < 0 || index >= length) {
- if (length == this.length) {
- throw new RangeError.index(index, this);
- }
- throw new RangeError.range(index, 0, length - 1);
- } else {
- throw new ArgumentError('Invalid list index $index');
- }
- }
-
- void _checkIndex(int index, int length) {
- if (JS('bool', '(# >>> 0 != #)', index, index) || index >= length) {
- _invalidIndex(index, length);
- }
- }
-
- int _checkSublistArguments(int start, int end, int length) {
- // For `sublist` the [start] and [end] indices are allowed to be equal to
- // [length]. However, [_checkIndex] only allows indices in the range
- // 0 .. length - 1. We therefore increment the [length] argument by one
- // for the [_checkIndex] checks.
- _checkIndex(start, length + 1);
- if (end == null) return length;
- _checkIndex(end, length + 1);
- if (start > end) throw new RangeError.range(start, 0, end);
- return end;
- }
-
int get length => _storage.length ~/ 4;
Float32x4 operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
double _x = _storage[(index * 4) + 0];
double _y = _storage[(index * 4) + 1];
double _z = _storage[(index * 4) + 2];
@@ -189,7 +160,7 @@ class NativeFloat32x4List
}
void operator[]=(int index, Float32x4 value) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
_storage[(index * 4) + 0] = value.x;
_storage[(index * 4) + 1] = value.y;
_storage[(index * 4) + 2] = value.z;
@@ -197,7 +168,7 @@ class NativeFloat32x4List
}
List<Float32x4> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
return new NativeFloat32x4List._externalStorage(
_storage.sublist(start * 4, end * 4));
}
@@ -257,40 +228,10 @@ class NativeInt32x4List
int get elementSizeInBytes => Int32x4List.BYTES_PER_ELEMENT;
- void _invalidIndex(int index, int length) {
- if (index < 0 || index >= length) {
- if (length == this.length) {
- throw new RangeError.index(index, this);
- }
- throw new RangeError.range(index, 0, length - 1);
- } else {
- throw new ArgumentError('Invalid list index $index');
- }
- }
-
- void _checkIndex(int index, int length) {
- if (JS('bool', '(# >>> 0 != #)', index, index)
- || JS('bool', '# >= #', index, length)) {
- _invalidIndex(index, length);
- }
- }
-
- int _checkSublistArguments(int start, int end, int length) {
- // For `sublist` the [start] and [end] indices are allowed to be equal to
- // [length]. However, [_checkIndex] only allows indices in the range
- // 0 .. length - 1. We therefore increment the [length] argument by one
- // for the [_checkIndex] checks.
- _checkIndex(start, length + 1);
- if (end == null) return length;
- _checkIndex(end, length + 1);
- if (start > end) throw new RangeError.range(start, 0, end);
- return end;
- }
-
int get length => _storage.length ~/ 4;
Int32x4 operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
int _x = _storage[(index * 4) + 0];
int _y = _storage[(index * 4) + 1];
int _z = _storage[(index * 4) + 2];
@@ -299,7 +240,7 @@ class NativeInt32x4List
}
void operator[]=(int index, Int32x4 value) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
_storage[(index * 4) + 0] = value.x;
_storage[(index * 4) + 1] = value.y;
_storage[(index * 4) + 2] = value.z;
@@ -307,7 +248,7 @@ class NativeInt32x4List
}
List<Int32x4> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
return new NativeInt32x4List._externalStorage(
_storage.sublist(start * 4, end * 4));
}
@@ -366,52 +307,23 @@ class NativeFloat64x2List
int get elementSizeInBytes => Float64x2List.BYTES_PER_ELEMENT;
- void _invalidIndex(int index, int length) {
- if (index < 0 || index >= length) {
- if (length == this.length) {
- throw new RangeError.index(index, this);
- }
- throw new RangeError.range(index, 0, length - 1);
- } else {
- throw new ArgumentError('Invalid list index $index');
- }
- }
-
- void _checkIndex(int index, int length) {
- if (JS('bool', '(# >>> 0 != #)', index, index) || index >= length) {
- _invalidIndex(index, length);
- }
- }
-
- int _checkSublistArguments(int start, int end, int length) {
- // For `sublist` the [start] and [end] indices are allowed to be equal to
- // [length]. However, [_checkIndex] only allows indices in the range
- // 0 .. length - 1. We therefore increment the [length] argument by one
- // for the [_checkIndex] checks.
- _checkIndex(start, length + 1);
- if (end == null) return length;
- _checkIndex(end, length + 1);
- if (start > end) throw new RangeError.range(start, 0, end);
- return end;
- }
-
int get length => _storage.length ~/ 2;
Float64x2 operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
double _x = _storage[(index * 2) + 0];
double _y = _storage[(index * 2) + 1];
return new Float64x2(_x, _y);
}
void operator[]=(int index, Float64x2 value) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
_storage[(index * 2) + 0] = value.x;
_storage[(index * 2) + 1] = value.y;
}
List<Float64x2> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
return new NativeFloat64x2List._externalStorage(
_storage.sublist(start * 2, end * 2));
}
@@ -446,37 +358,20 @@ class NativeTypedData implements TypedData {
@JSName('BYTES_PER_ELEMENT')
final int elementSizeInBytes;
- void _checkIndex(int index, int length) {
- if (JS('bool', '(# >>> 0) !== #', index, index) ||
- JS('int', '#', index) >= length) { // 'int' guaranteed by above test.
- throw diagnoseIndexError(this, index);
- }
- }
-
- void _invalidPosition(int position, int length) {
+ void _invalidPosition(int position, int length, String name) {
if (position is !int) {
- throw new ArgumentError.value(position, null, 'Invalid list position');
+ throw new ArgumentError.value(position, name, 'Invalid list position');
} else {
- throw new RangeError.range(position, 0, length);
+ throw new RangeError.range(position, 0, length, name);
}
}
- void _checkPosition(int position, int length) {
+ void _checkPosition(int position, int length, String name) {
if (JS('bool', '(# >>> 0) !== #', position, position) ||
JS('int', '#', position) > length) { // 'int' guaranteed by above test.
- _invalidPosition(position, length);
+ _invalidPosition(position, length, name);
}
}
-
- int _checkSublistArguments(int start, int end, int length) {
- // For `sublist` the [start] and [end] indices are allowed to be equal to
- // [length].
- _checkPosition(start, length);
- if (end == null) return length;
- _checkPosition(end, length);
- if (start > end) throw new RangeError.range(start, 0, end);
- return end;
- }
}
@@ -862,8 +757,8 @@ abstract class NativeTypedArray extends NativeTypedData
void _setRangeFast(int start, int end,
NativeTypedArray source, int skipCount) {
int targetLength = this.length;
- _checkPosition(start, targetLength);
- _checkPosition(end, targetLength);
+ _checkPosition(start, targetLength, "start");
+ _checkPosition(end, targetLength, "end");
if (start > end) throw new RangeError.range(start, 0, end);
int count = end - start;
@@ -888,12 +783,12 @@ abstract class NativeTypedArrayOfDouble
with ListMixin<double>, FixedLengthListMixin<double> {
num operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
return JS('num', '#[#]', this, index);
}
void operator[]=(int index, num value) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
JS('void', '#[#] = #', this, index, value);
}
@@ -916,7 +811,7 @@ abstract class NativeTypedArrayOfInt
// types
void operator[]=(int index, int value) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
JS('void', '#[#] = #', this, index, value);
}
@@ -952,7 +847,7 @@ class NativeFloat32List
Type get runtimeType => Float32List;
List<double> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
var source = JS('NativeFloat32List', '#.subarray(#, #)', this, start, end);
return _create1(source);
}
@@ -989,7 +884,7 @@ class NativeFloat64List
Type get runtimeType => Float64List;
List<double> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
var source = JS('NativeFloat64List', '#.subarray(#, #)', this, start, end);
return _create1(source);
}
@@ -1026,12 +921,12 @@ class NativeInt16List
Type get runtimeType => Int16List;
int operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
return JS('int', '#[#]', this, index);
}
List<int> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
var source = JS('NativeInt16List', '#.subarray(#, #)', this, start, end);
return _create1(source);
}
@@ -1066,12 +961,12 @@ class NativeInt32List extends NativeTypedArrayOfInt implements Int32List {
Type get runtimeType => Int32List;
int operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
return JS('int', '#[#]', this, index);
}
List<int> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
var source = JS('NativeInt32List', '#.subarray(#, #)', this, start, end);
return _create1(source);
}
@@ -1106,12 +1001,12 @@ class NativeInt8List extends NativeTypedArrayOfInt implements Int8List {
Type get runtimeType => Int8List;
int operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
return JS('int', '#[#]', this, index);
}
List<int> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
var source = JS('NativeInt8List', '#.subarray(#, #)', this, start, end);
return _create1(source);
}
@@ -1146,12 +1041,12 @@ class NativeUint16List extends NativeTypedArrayOfInt implements Uint16List {
Type get runtimeType => Uint16List;
int operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
return JS('JSUInt31', '#[#]', this, index);
}
List<int> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
var source = JS('NativeUint16List', '#.subarray(#, #)', this, start, end);
return _create1(source);
}
@@ -1186,12 +1081,12 @@ class NativeUint32List extends NativeTypedArrayOfInt implements Uint32List {
Type get runtimeType => Uint32List;
int operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
return JS('JSUInt32', '#[#]', this, index);
}
List<int> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
var source = JS('NativeUint32List', '#.subarray(#, #)', this, start, end);
return _create1(source);
}
@@ -1230,12 +1125,12 @@ class NativeUint8ClampedList
int get length => JS('JSUInt32', '#.length', this);
int operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
return JS('JSUInt31', '#[#]', this, index);
}
List<int> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
var source = JS('NativeUint8ClampedList', '#.subarray(#, #)',
this, start, end);
return _create1(source);
@@ -1278,12 +1173,12 @@ class NativeUint8List extends NativeTypedArrayOfInt implements Uint8List {
int get length => JS('JSUInt32', '#.length', this);
int operator[](int index) {
- _checkIndex(index, length);
+ _checkValidIndex(index, this, this.length);
return JS('JSUInt31', '#[#]', this, index);
}
List<int> sublist(int start, [int end]) {
- end = _checkSublistArguments(start, end, length);
+ end = _checkValidRange(start, end, this.length);
var source = JS('NativeUint8List', '#.subarray(#, #)', this, start, end);
return _create1(source);
}
@@ -2006,3 +1901,37 @@ class NativeFloat64x2 implements Float64x2 {
return new NativeFloat64x2._doubles(Math.sqrt(x), Math.sqrt(y));
}
}
+
+/// Checks that the value is a Uint32. If not, it's not valid as an array
+/// index or offset. Also ensures that the value is non-negative.
+bool _isInvalidArrayIndex(int index) {
+ return (JS('bool', '(# >>> 0 !== #)', index, index));
+}
+
+/// Checks that [index] is a valid index into [list] which has length [length].
+///
+/// That is, [index] is an insteger in the range `0..length - 1`.
+void _checkValidIndex(int index, List list, int length) {
+ if (_isInvalidArrayIndex(index) || JS('int', '#', index) >= length) {
+ throw diagnoseIndexError(list, index);
+ }
+}
+
+/// Checks that [start] and [end] form a range of a list of length [length].
+///
+/// That is: `start` and `end` are integers with `0 <= start <= end <= length`.
+/// If `end` is `null` in which case it is considered to be `length`
+///
+/// Returns the actual value of `end`, which is `length` if `end` is `null`, and
+/// the original value of `end` otherwise.
+int _checkValidRange(int start, int end, int length) {
+ if (_isInvalidArrayIndex(start) || // Ensures start is non-negative int.
+ ((end == null) ? start > length
+ : (_isInvalidArrayIndex(end) ||
+ start > end ||
+ end > length))) {
+ throw diagnoseRangeError(start, end, length);
+ }
+ if (end == null) return length;
+ return end;
+}

Powered by Google App Engine
This is Rietveld 408576698