lib: short-circuit WebIDL BufferSource SAB check by panva · Pull Request #62833 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions benchmark/misc/webidl-buffer-source.js
28 changes: 21 additions & 7 deletions lib/internal/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ArrayBufferIsView,
ArrayBufferPrototypeGetByteLength,
ArrayPrototypePush,
ArrayPrototypeToSorted,
DataViewPrototypeGetBuffer,
Expand Down Expand Up @@ -33,8 +34,7 @@ const {
const { kEmptyObject } = require('internal/util');
const {
isArrayBuffer,
isDataView,
isSharedArrayBuffer,
isTypedArray,
} = require('internal/util/types');

const converters = { __proto__: null };
Expand Down Expand Up @@ -390,9 +390,23 @@ function createInterfaceConverter(name, I) {
};
}

function getDataViewOrTypedArrayBuffer(V) {
return isDataView(V) ?
DataViewPrototypeGetBuffer(V) : TypedArrayPrototypeGetBuffer(V);
// Returns the [[ViewedArrayBuffer]] of an ArrayBufferView without leaving JS.
function getViewedArrayBuffer(V) {
return isTypedArray(V) ?
TypedArrayPrototypeGetBuffer(V) : DataViewPrototypeGetBuffer(V);
}

// Returns `true` if `buffer` is a `SharedArrayBuffer`. Uses a brand check via
// the `ArrayBuffer.prototype.byteLength` getter, which succeeds only on real
// (non-shared) ArrayBuffers and throws on SharedArrayBuffers — independent
// of the receiver's prototype chain.
function isSharedArrayBufferBacking(buffer) {
Comment thread
MattiasBuelens marked this conversation as resolved.
try {
ArrayBufferPrototypeGetByteLength(buffer);
Comment thread
MattiasBuelens marked this conversation as resolved.
return false;
} catch {
return true;
}
}

// https://webidl.spec.whatwg.org/#ArrayBufferView
Expand All @@ -402,7 +416,7 @@ converters.ArrayBufferView = (V, opts = kEmptyObject) => {
'is not an ArrayBufferView.',
opts);
}
if (isSharedArrayBuffer(getDataViewOrTypedArrayBuffer(V))) {
if (isSharedArrayBufferBacking(getViewedArrayBuffer(V))) {
throw makeException(
'is a view on a SharedArrayBuffer, which is not allowed.',
opts);
Expand All @@ -414,7 +428,7 @@ converters.ArrayBufferView = (V, opts = kEmptyObject) => {
// https://webidl.spec.whatwg.org/#BufferSource
converters.BufferSource = (V, opts = kEmptyObject) => {
if (ArrayBufferIsView(V)) {
if (isSharedArrayBuffer(getDataViewOrTypedArrayBuffer(V))) {
if (isSharedArrayBufferBacking(getViewedArrayBuffer(V))) {
throw makeException(
'is a view on a SharedArrayBuffer, which is not allowed.',
opts);
Expand Down
208 changes: 208 additions & 0 deletions test/parallel/test-internal-webidl-buffer-source.js
Loading