- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 33.6k
util: add isArrayBufferDetached method #45512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,11 +1,15 @@ | ||
| 'use strict'; | ||
|  | ||
| const { | ||
| ArrayBuffer, | ||
| ArrayBufferIsView, | ||
| ArrayBufferPrototypeGetByteLength, | ||
| ObjectDefineProperties, | ||
| TypedArrayPrototypeGetSymbolToStringTag, | ||
| } = primordials; | ||
|  | ||
| const { isArrayBufferDetached: _isArrayBufferDetached, ...internalTypes } = internalBinding('types'); | ||
|  | ||
| function isTypedArray(value) { | ||
| return TypedArrayPrototypeGetSymbolToStringTag(value) !== undefined; | ||
| } | ||
|  | @@ -54,8 +58,16 @@ function isBigUint64Array(value) { | |
| return TypedArrayPrototypeGetSymbolToStringTag(value) === 'BigUint64Array'; | ||
| } | ||
|  | ||
| function isArrayBufferDetached(value) { | ||
| if (value instanceof ArrayBuffer && ArrayBufferPrototypeGetByteLength(value) === 0) { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't be surprised if that  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Referencing: https://github.com/RafaelGSS/nodejs-bench-operations/blob/main/RESULTS-v19.md#comparison-using-instanceof I can change it to  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the best course of action is to make an internal  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, instanceof is brittle and can be forged, and doesn’t work cross-realm. In JS the way to check this is using the byteLength getter in a try/catch. | ||
| return _isArrayBufferDetached(value); | ||
| } | ||
| return false; | ||
| } | ||
|  | ||
| module.exports = { | ||
| ...internalBinding('types'), | ||
| ...internalTypes, | ||
| isArrayBufferDetached, | ||
| isArrayBufferView: ArrayBufferIsView, | ||
| isTypedArray, | ||
| isUint8Array, | ||
|  | ||
Uh oh!
There was an error while loading. Please reload this page.