Skip to content

Commit

Permalink
[Fix] ES2015+: ValidateTypedArray: proper detachment check and re…
Browse files Browse the repository at this point in the history
…turn value
  • Loading branch information
ljharb committed Jan 31, 2024
1 parent 48b2d9f commit e239ab2
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 86 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,6 @@
/2022/UTF16EncodeCodePoint.js spackled linguist-generated=true
/2022/UTF16SurrogatePairToCodePoint.js spackled linguist-generated=true
/2022/UnicodeEscape.js spackled linguist-generated=true
/2022/ValidateTypedArray.js spackled linguist-generated=true
/2022/WeakRefDeref.js spackled linguist-generated=true
/2022/WeekDay.js spackled linguist-generated=true
/2022/WordCharacters.js spackled linguist-generated=true
Expand Down
18 changes: 9 additions & 9 deletions 2015/ValidateTypedArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ var GetIntrinsic = require('get-intrinsic');

var $TypeError = GetIntrinsic('%TypeError%');

var callBound = require('call-bind/callBound');
var $taSome = callBound('TypedArray.prototype.some', true);

var IsDetachedBuffer = require('./IsDetachedBuffer');
var Type = require('./Type');

var isTypedArray = require('is-typed-array');
var typedArrayBuffer = require('typed-array-buffer');

// https://262.ecma-international.org/6.0/#sec-validatetypedarray

module.exports = function ValidateTypedArray(O) {
if (Type(O) !== 'Object') {
throw new $TypeError('Assertion failed: O must be an Object');
throw new $TypeError('Assertion failed: `O` must be an Object'); // step 1
}
if (!isTypedArray(O)) {
throw new $TypeError('Assertion failed: O must be a TypedArray');
throw new $TypeError('Assertion failed: `O` must be a Typed Array'); // steps 2 - 3
}

// without `.some` (like in node 0.10), there's no way to check buffer detachment, but also no way to be detached
if ($taSome) {
$taSome(O, function () { return true; });
var buffer = typedArrayBuffer(O); // step 4

if (IsDetachedBuffer(buffer)) {
throw new $TypeError('`O` must be backed by a non-detached buffer'); // step 5
}

return O.buffer;
return buffer; // step 6
};
18 changes: 9 additions & 9 deletions 2016/ValidateTypedArray.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions 2017/ValidateTypedArray.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions 2018/ValidateTypedArray.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions 2019/ValidateTypedArray.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions 2020/ValidateTypedArray.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions 2021/ValidateTypedArray.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions 2022/ValidateTypedArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@ var GetIntrinsic = require('get-intrinsic');

var $TypeError = GetIntrinsic('%TypeError%');

var callBound = require('call-bind/callBound');
var $taSome = callBound('TypedArray.prototype.some', true);

var IsDetachedBuffer = require('./IsDetachedBuffer');
var Type = require('./Type');

var isTypedArray = require('is-typed-array');
var typedArrayBuffer = require('typed-array-buffer');

// https://262.ecma-international.org/6.0/#sec-validatetypedarray
// https://262.ecma-international.org/13.0/#sec-validatetypedarray

module.exports = function ValidateTypedArray(O) {
if (Type(O) !== 'Object') {
throw new $TypeError('Assertion failed: O must be an Object');
throw new $TypeError('Assertion failed: `O` must be an Object'); // step 1
}
if (!isTypedArray(O)) {
throw new $TypeError('Assertion failed: O must be a TypedArray');
throw new $TypeError('Assertion failed: `O` must be a Typed Array'); // steps 1 - 2
}

// without `.some` (like in node 0.10), there's no way to check buffer detachment, but also no way to be detached
if ($taSome) {
$taSome(O, function () { return true; });
}
var buffer = typedArrayBuffer(O); // step 3

return O.buffer;
if (IsDetachedBuffer(buffer)) {
throw new $TypeError('`O` must be backed by a non-detached buffer'); // step 4
}
};
20 changes: 9 additions & 11 deletions 2023/ValidateTypedArray.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5526,6 +5526,18 @@ var es2015 = function ES2015(ES, ops, expectedMissing, skips) {
function () { ES.ValidateTypedArray(ta); },
debug(ta) + ' is a TypedArray'
);

st.test('can detach', { skip: !canDetach }, function (s2t) {
ES.DetachArrayBuffer(ta.buffer);

s2t['throws'](
function () { ES.ValidateTypedArray(ta); },
TypeError,
debug(ta) + ' is a detached TypedArray'
);

s2t.end();
});
});

st.end();
Expand Down

0 comments on commit e239ab2

Please sign in to comment.