Browse Source

Fix the subarray set method

ankur22 3 weeks ago
parent
commit
c665ba5341
2 changed files with 18 additions and 2 deletions
  1. 2 2
      builtin_typedarrays.go
  2. 16 0
      builtin_typedarrays_test.go

+ 2 - 2
builtin_typedarrays.go

@@ -1060,8 +1060,8 @@ func (r *Runtime) typedArrayProto_set(call FunctionCall) Value {
 			}
 			for i := 0; i < srcLen; i++ {
 				val := nilSafe(srcObj.self.getIdx(valueInt(i), nil))
-				if ta.isValidIntegerIndex(i) {
-					ta.typedArray.set(targetOffset+i, val)
+				if ta.isValidIntegerIndex(targetOffset + i) {
+					ta.typedArray.set(ta.offset+targetOffset+i, val)
 				}
 			}
 		}

+ 16 - 0
builtin_typedarrays_test.go

@@ -367,3 +367,19 @@ func TestDataViewExportToBytes(t *testing.T) {
 		t.Fatal("unexpected value", b)
 	}
 }
+
+func TestTypedArraySubarraySet(t *testing.T) {
+	const SCRIPT = `
+	const u = new Uint8Array(4)
+	const s = u.subarray(1, 4);
+	s.set([1,2,3], 0);
+	if (s.length !== 3) {
+		throw new Error("s.length=" + s.length + ", expected 3");
+	}
+	if (s[0] !== 1 || s[1] !== 2 || s[2] !== 3) {
+		throw new Error("s=[" + s.join(",") + "], expected [1,2,3]");
+	}
+	`
+
+	testScript(SCRIPT, _undefined, t)
+}