From 8d9073370d4ac5179de3213596be4895223a502e Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Sun, 19 Feb 2023 17:14:46 +0900 Subject: [PATCH] Add isResizableArrayBuffer --- src/_util/is.mjs | 17 +++++++++++++++++ src/_util/primordials.mjs | 3 +++ 2 files changed, 20 insertions(+) diff --git a/src/_util/is.mjs b/src/_util/is.mjs index 9699cff8..6c162589 100644 --- a/src/_util/is.mjs +++ b/src/_util/is.mjs @@ -1,5 +1,6 @@ import { ArrayBufferPrototypeGetByteLength, + ArrayBufferPrototypeGetResizable, ArrayIsArray, ArrayIteratorPrototype, ArrayIteratorPrototypeNext, @@ -97,6 +98,22 @@ export function isAnyArrayBuffer(value) { return isArrayBuffer(value) || isSharedArrayBuffer(value); } +/** + * @param {unknown} value + * @returns {boolean} + */ +export function isResizableArrayBuffer(value) { + if (ArrayBufferPrototypeGetResizable === null) { + return false; + } + + try { + return ArrayBufferPrototypeGetResizable(/** @type {any} */ (value)); + } catch (e) { + return false; + } +} + /** * @param {unknown} value * @returns {value is unknown[]} diff --git a/src/_util/primordials.mjs b/src/_util/primordials.mjs index 72c8bc70..633355f7 100644 --- a/src/_util/primordials.mjs +++ b/src/_util/primordials.mjs @@ -118,6 +118,9 @@ const ArrayBufferPrototype = NativeArrayBuffer.prototype; export const ArrayBufferPrototypeSlice = uncurryThis(ArrayBufferPrototype.slice); /** @type {(buffer: ArrayBuffer) => ArrayBuffer} */ export const ArrayBufferPrototypeGetByteLength = uncurryThisGetter(ArrayBufferPrototype, "byteLength"); +/** @type {null | ((buffer: ArrayBuffer) => boolean)} */ +export const ArrayBufferPrototypeGetResizable = typeof ArrayBufferPrototype["resize"] !== "undefined" ? + uncurryThisGetter(ArrayBufferPrototype, "resizable") : null; // SharedArrayBuffer export const NativeSharedArrayBuffer = typeof SharedArrayBuffer !== "undefined" ? SharedArrayBuffer : null;