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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ Conversions for all of the basic types from the Web IDL specification are implem
- [`DOMString`](https://heycam.github.io/webidl/#es-DOMString), which can additionally be provided the boolean option `{ treatNullAsEmptyString }` as a second parameter
- [`ByteString`](https://heycam.github.io/webidl/#es-ByteString), [`USVString`](https://heycam.github.io/webidl/#es-USVString)
- [`object`](https://heycam.github.io/webidl/#es-object)
- [Buffer source types](https://heycam.github.io/webidl/#es-buffer-source-types), which can additionally be provided with the boolean option `{ allowShared }` as a second parameter
- [Buffer source types](https://heycam.github.io/webidl/#es-buffer-source-types), which can additionally be provided with the boolean option bag `{ allowShared, allowResizable }` as a second parameter

Additionally, for convenience, the following derived type definitions are implemented:

- [`ArrayBufferView`](https://heycam.github.io/webidl/#ArrayBufferView), which can additionally be provided with the boolean option `{ allowShared }` as a second parameter
- [`ArrayBufferView`](https://heycam.github.io/webidl/#ArrayBufferView), which can additionally be provided with the boolean option bag `{ allowShared, allowResizable }` as a second parameter
- [`BufferSource`](https://heycam.github.io/webidl/#BufferSource)
- [`DOMTimeStamp`](https://heycam.github.io/webidl/#DOMTimeStamp)

Expand Down
34 changes: 32 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,25 @@ function isSharedArrayBuffer(value) {
}
}

const abResizableGetter = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "resizable").get;
const sabGrowableGetter = Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, "growable").get;

function isNonSharedArrayBufferResizable(value) {
try {
return abResizableGetter.call(value);
} catch {
return false;
}
}

function isSharedArrayBufferGrowable(value) {
try {
return sabGrowableGetter.call(value);
} catch {
return false;
}
}

function isArrayBufferDetached(value) {
try {
// eslint-disable-next-line no-new
Expand All @@ -336,6 +355,9 @@ exports.ArrayBuffer = (value, options = {}) => {
if (!isNonSharedArrayBuffer(value)) {
throw makeException(TypeError, "is not an ArrayBuffer", options);
}
if (!options.allowResizable && isNonSharedArrayBufferResizable(value)) {
throw makeException(TypeError, "is a resizable ArrayBuffer", options);
}
if (isArrayBufferDetached(value)) {
throw makeException(TypeError, "is a detached ArrayBuffer", options);
}
Expand All @@ -347,8 +369,8 @@ exports.SharedArrayBuffer = (value, options = {}) => {
if (!isSharedArrayBuffer(value)) {
throw makeException(TypeError, "is not a SharedArrayBuffer", options);
}
if (isArrayBufferDetached(value)) {
throw makeException(TypeError, "is a detached SharedArrayBuffer", options);
if (!options.allowResizable && isSharedArrayBufferGrowable(value)) {
throw makeException(TypeError, "is a growable SharedArrayBuffer", options);
}

return value;
Expand Down Expand Up @@ -405,6 +427,14 @@ exports.ArrayBufferView = (value, options = {}) => {
throw makeException(TypeError, "is a view on a SharedArrayBuffer, which is not allowed", options);
}

if (!options.allowResizable) {
if (isNonSharedArrayBufferResizable(value.buffer)) {
throw makeException(TypeError, "is a view on a resizable ArrayBuffer, which is not allowed", options);
} else if (isSharedArrayBufferGrowable(value.buffer)) {
throw makeException(TypeError, "is a view on a growable SharedArrayBuffer, which is not allowed", options);
}
}

if (isArrayBufferDetached(value.buffer)) {
throw makeException(TypeError, "is a view on a detached ArrayBuffer", options);
}
Expand Down
Loading