Skip to content

Commit 612790f

Browse files
Add allowResizable to (Shared)ArrayBuffer conversions
Implements the [AllowResizable] attribute introduced in whatwg/webidl#982.
1 parent 3f59834 commit 612790f

File tree

3 files changed

+242
-12
lines changed

3 files changed

+242
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ Conversions for all of the basic types from the Web IDL specification are implem
5656
- [`DOMString`](https://heycam.github.io/webidl/#es-DOMString), which can additionally be provided the boolean option `{ treatNullAsEmptyString }` as a second parameter
5757
- [`ByteString`](https://heycam.github.io/webidl/#es-ByteString), [`USVString`](https://heycam.github.io/webidl/#es-USVString)
5858
- [`object`](https://heycam.github.io/webidl/#es-object)
59-
- [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
59+
- [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
6060

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

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

lib/index.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,25 @@ function isSharedArrayBuffer(value) {
322322
}
323323
}
324324

325+
const abResizableGetter = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "resizable").get;
326+
const sabGrowableGetter = Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, "growable").get;
327+
328+
function isNonSharedArrayBufferResizable(value) {
329+
try {
330+
return abResizableGetter.call(value);
331+
} catch {
332+
return false;
333+
}
334+
}
335+
336+
function isSharedArrayBufferGrowable(value) {
337+
try {
338+
return sabGrowableGetter.call(value);
339+
} catch {
340+
return false;
341+
}
342+
}
343+
325344
function isArrayBufferDetached(value) {
326345
try {
327346
// eslint-disable-next-line no-new
@@ -336,6 +355,9 @@ exports.ArrayBuffer = (value, options = {}) => {
336355
if (!isNonSharedArrayBuffer(value)) {
337356
throw makeException(TypeError, "is not an ArrayBuffer", options);
338357
}
358+
if (!options.allowResizable && isNonSharedArrayBufferResizable(value)) {
359+
throw makeException(TypeError, "is a resizable ArrayBuffer", options);
360+
}
339361
if (isArrayBufferDetached(value)) {
340362
throw makeException(TypeError, "is a detached ArrayBuffer", options);
341363
}
@@ -347,8 +369,8 @@ exports.SharedArrayBuffer = (value, options = {}) => {
347369
if (!isSharedArrayBuffer(value)) {
348370
throw makeException(TypeError, "is not a SharedArrayBuffer", options);
349371
}
350-
if (isArrayBufferDetached(value)) {
351-
throw makeException(TypeError, "is a detached SharedArrayBuffer", options);
372+
if (!options.allowResizable && isSharedArrayBufferGrowable(value)) {
373+
throw makeException(TypeError, "is a growable SharedArrayBuffer", options);
352374
}
353375

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

430+
if (!options.allowResizable) {
431+
if (isNonSharedArrayBufferResizable(value.buffer)) {
432+
throw makeException(TypeError, "is a view on a resizable ArrayBuffer, which is not allowed", options);
433+
} else if (isSharedArrayBufferGrowable(value.buffer)) {
434+
throw makeException(TypeError, "is a view on a growable SharedArrayBuffer, which is not allowed", options);
435+
}
436+
}
437+
408438
if (isArrayBufferDetached(value.buffer)) {
409439
throw makeException(TypeError, "is a view on a detached ArrayBuffer", options);
410440
}

0 commit comments

Comments
 (0)