Browse Source

Return undefined for negative TypedArray indexes

Mihail Stoykov 5 years ago
parent
commit
ab4920bc03
2 changed files with 9 additions and 1 deletions
  1. 8 0
      builtin_typedarrays_test.go
  2. 1 1
      typedarrays.go

+ 8 - 0
builtin_typedarrays_test.go

@@ -296,3 +296,11 @@ func TestTypedArraySortComparatorReturnValueNegZero(t *testing.T) {
 	`
 	testScript1(SCRIPT, _undefined, t)
 }
+
+func TestInt32ArrayNegativeIndex(t *testing.T) {
+	const SCRIPT = `
+	new Int32Array()[-1] === undefined;
+	`
+
+	testScript1(SCRIPT, valueTrue, t)
+}

+ 1 - 1
typedarrays.go

@@ -454,7 +454,7 @@ func (a *float64Array) typeMatch(v Value) bool {
 
 func (a *typedArrayObject) _getIdx(idx int) Value {
 	a.viewedArrayBuf.ensureNotDetached()
-	if idx < a.length {
+	if 0 <= idx && idx < a.length {
 		return a.typedArray.get(idx + a.offset)
 	}
 	return nil