Skip to content

Commit

Permalink
Bug 1763606 - Part 7: Remove detached ArrayBuffer checks from TypedAr…
Browse files Browse the repository at this point in the history
…ray.prototype.sort comparator. r=tcampbell

Remove the check per <tc39/ecma262#2723>.

Depends on D143290

Differential Revision: https://phabricator.services.mozilla.com/D143291
  • Loading branch information
anba committed Jun 8, 2022
1 parent 854e73f commit 5a1d24e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 31 deletions.
18 changes: 1 addition & 17 deletions js/src/builtin/TypedArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -1018,26 +1018,10 @@ function TypedArraySort(comparefn) {
var v = +comparefn(x, y);

// Step b.
var length;
if (isTypedArray) {
length = TypedArrayLength(obj);
} else {
length = callFunction(CallTypedArrayMethodIfWrapped, obj, "TypedArrayLengthMethod");
}

// It's faster for us to check the typed array's length than to check
// for detached buffers.
if (length === 0) {
assert(PossiblyWrappedTypedArrayHasDetachedBuffer(obj),
"Length can only change from non-zero to zero when the buffer was detached");
ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
}

// Step c.
if (v !== v)
return 0;

// Step d.
// Step c.
return v;
};

Expand Down
28 changes: 14 additions & 14 deletions js/src/tests/non262/TypedArray/sort_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ if (typeof detachArrayBuffer === "function") {
}, TypeError);
}

// Ensure detachment check works when buffer is detached in comparator.
// Ensure detaching buffer in comparator doesn't throw an error.
if (typeof detachArrayBuffer === "function") {
let detached = false;
let ta = new Int32Array(3);
assertThrowsInstanceOf(() => {
ta.sort(function(a, b) {
assertEq(detached, false);
ta.sort(function(a, b) {
if (!detached) {
detached = true;
detachArrayBuffer(ta.buffer);
return a - b;
});
}, TypeError);
}
return a - b;
});
assertEq(detached, true);
}

// Ensure detachment check doesn't choke on wrapped typed array.
Expand All @@ -31,19 +31,19 @@ if (typeof newGlobal === "function") {
});
}

// Ensure detachment check works for wrapped typed arrays.
// Ensure detaching buffer in comparator doesn't throw an error when the typed array is wrapped.
if (typeof newGlobal === "function" && typeof detachArrayBuffer === "function") {
let detached = false;
let ta = new Int32Array(3);
let otherGlobal = newGlobal();
assertThrowsInstanceOf(() => {
otherGlobal.Int32Array.prototype.sort.call(ta, function(a,b) {
assertEq(detached, false);
otherGlobal.Int32Array.prototype.sort.call(ta, function(a,b) {
if (!detached) {
detached = true;
detachArrayBuffer(ta.buffer);
return a - b;
});
}, otherGlobal.TypeError);
}
return a - b;
});
assertEq(detached, true);
}

// Ensure that TypedArray.prototype.sort will not sort non-TypedArrays
Expand Down

0 comments on commit 5a1d24e

Please sign in to comment.