GeezerVison is showing github.com — src: add internal isArrayBufferDetached · nodejs/node@ae61740
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | | 4 | + ArrayBufferPrototypeGetByteLength, | 4 | 5 | ArrayFrom, | 5 | 6 | ArrayIsArray, | 6 | 7 | ArrayPrototypePush, | @@ -42,6 +43,7 @@ const { | 42 | 43 | } = require('internal/errors'); | 43 | 44 | const { signals } = internalBinding('constants').os; | 44 | 45 | const { | | 46 | + isArrayBufferDetached: _isArrayBufferDetached, | 45 | 47 | privateSymbols: { | 46 | 48 | arrow_message_private_symbol, | 47 | 49 | decorated_private_symbol, | @@ -557,6 +559,14 @@ function SideEffectFreeRegExpPrototypeExec(regex, string) { | 557 | 559 | return FunctionPrototypeCall(RegExpFromAnotherRealm.prototype.exec, regex, string); | 558 | 560 | } | 559 | 561 |
| | 562 | +function isArrayBufferDetached(value) { | | 563 | + if (ArrayBufferPrototypeGetByteLength(value) === 0) { | | 564 | + return _isArrayBufferDetached(value); | | 565 | + } | | 566 | + | | 567 | + return false; | | 568 | +} | | 569 | + | 560 | 570 | module.exports = { | 561 | 571 | assertCrypto, | 562 | 572 | cachedResult, | @@ -574,6 +584,7 @@ module.exports = { | 574 | 584 | getInternalGlobal, | 575 | 585 | getSystemErrorMap, | 576 | 586 | getSystemErrorName, | | 587 | + isArrayBufferDetached, | 577 | 588 | isError, | 578 | 589 | isInsideNodeModules, | 579 | 590 | join, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstreams/readablestream.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,6 +51,7 @@ const { | 51 | 51 | const { | 52 | 52 | createDeferredPromise, | 53 | 53 | customInspectSymbol: kInspect, | | 54 | + isArrayBufferDetached, | 54 | 55 | kEmptyObject, | 55 | 56 | kEnumerableProperty, | 56 | 57 | } = require('internal/util'); | @@ -103,7 +104,6 @@ const { | 103 | 104 | extractHighWaterMark, | 104 | 105 | extractSizeAlgorithm, | 105 | 106 | lazyTransfer, | 106 | | - isDetachedBuffer, | 107 | 107 | isViewedArrayBufferDetached, | 108 | 108 | isBrandCheck, | 109 | 109 | resetQueue, | @@ -668,7 +668,7 @@ class ReadableStreamBYOBRequest { | 668 | 668 | const viewBuffer = ArrayBufferViewGetBuffer(view); | 669 | 669 | const viewBufferByteLength = ArrayBufferPrototypeGetByteLength(viewBuffer); | 670 | 670 |
| 671 | | - if (isDetachedBuffer(viewBuffer)) { | | 671 | + if (isArrayBufferDetached(viewBuffer)) { | 672 | 672 | throw new ERR_INVALID_STATE.TypeError('Viewed ArrayBuffer is detached'); | 673 | 673 | } | 674 | 674 |
| @@ -2641,7 +2641,7 @@ function readableByteStreamControllerEnqueue(controller, chunk) { | 2641 | 2641 | if (pendingPullIntos.length) { | 2642 | 2642 | const firstPendingPullInto = pendingPullIntos[0]; | 2643 | 2643 |
| 2644 | | - if (isDetachedBuffer(firstPendingPullInto.buffer)) { | | 2644 | + if (isArrayBufferDetached(firstPendingPullInto.buffer)) { | 2645 | 2645 | throw new ERR_INVALID_STATE.TypeError( | 2646 | 2646 | 'Destination ArrayBuffer is detached', | 2647 | 2647 | ); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstreams/util.js +2-16Lines changed: 2 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | 4 | | - ArrayBufferPrototypeGetByteLength, | 5 | 4 | ArrayBufferPrototypeSlice, | 6 | 5 | ArrayPrototypePush, | 7 | 6 | ArrayPrototypeShift, | @@ -47,6 +46,7 @@ const { | 47 | 46 | } = internalBinding('util'); | 48 | 47 |
| 49 | 48 | const assert = require('internal/assert'); | | 49 | +const { isArrayBufferDetached } = require('internal/util'); | 50 | 50 |
| 51 | 51 | const kState = Symbol('kState'); | 52 | 52 | const kType = Symbol('kType'); | @@ -137,23 +137,10 @@ function transferArrayBuffer(buffer) { | 137 | 137 | return res; | 138 | 138 | } | 139 | 139 |
| 140 | | -function isDetachedBuffer(buffer) { | 141 | | - if (ArrayBufferPrototypeGetByteLength(buffer) === 0) { | 142 | | - // TODO(daeyeon): Consider using C++ builtin to improve performance. | 143 | | - try { | 144 | | - new Uint8Array(buffer); | 145 | | - } catch (error) { | 146 | | - assert(error.name === 'TypeError'); | 147 | | - return true; | 148 | | - } | 149 | | - } | 150 | | - return false; | 151 | | -} | 152 | | - | 153 | 140 | function isViewedArrayBufferDetached(view) { | 154 | 141 | return ( | 155 | 142 | ArrayBufferViewGetByteLength(view) === 0 && | 156 | | - isDetachedBuffer(ArrayBufferViewGetBuffer(view)) | | 143 | + isArrayBufferDetached(ArrayBufferViewGetBuffer(view)) | 157 | 144 | ); | 158 | 145 | } | 159 | 146 |
| @@ -253,7 +240,6 @@ module.exports = { | 253 | 240 | extractSizeAlgorithm, | 254 | 241 | lazyTransfer, | 255 | 242 | isBrandCheck, | 256 | | - isDetachedBuffer, | 257 | 243 | isPromisePending, | 258 | 244 | isViewedArrayBufferDetached, | 259 | 245 | peekQueueValue, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_util.cc +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -137,6 +137,15 @@ static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) { | 137 | 137 | } | 138 | 138 | } | 139 | 139 |
| | 140 | +static void IsArrayBufferDetached(const FunctionCallbackInfo<Value>& args) { | | 141 | + if (args[0]->IsArrayBuffer()) { | | 142 | + auto buffer = args[0].As<v8::ArrayBuffer>(); | | 143 | + args.GetReturnValue().Set(buffer->WasDetached()); | | 144 | + return; | | 145 | + } | | 146 | + args.GetReturnValue().Set(false); | | 147 | +} | | 148 | + | 140 | 149 | static void PreviewEntries(const FunctionCallbackInfo<Value>& args) { | 141 | 150 | if (!args[0]->IsObject()) | 142 | 151 | return; | @@ -349,6 +358,7 @@ static void ToUSVString(const FunctionCallbackInfo<Value>& args) { | 349 | 358 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | 350 | 359 | registry->Register(GetPromiseDetails); | 351 | 360 | registry->Register(GetProxyDetails); | | 361 | + registry->Register(IsArrayBufferDetached); | 352 | 362 | registry->Register(PreviewEntries); | 353 | 363 | registry->Register(GetOwnNonIndexProperties); | 354 | 364 | registry->Register(GetConstructorName); | @@ -422,6 +432,8 @@ void Initialize(Local<Object> target, | 422 | 432 | SetMethodNoSideEffect( | 423 | 433 | context, target, "getPromiseDetails", GetPromiseDetails); | 424 | 434 | SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails); | | 435 | + SetMethodNoSideEffect( | | 436 | + context, target, "isArrayBufferDetached", IsArrayBufferDetached); | 425 | 437 | SetMethodNoSideEffect(context, target, "previewEntries", PreviewEntries); | 426 | 438 | SetMethodNoSideEffect( | 427 | 439 | context, target, "getOwnNonIndexProperties", GetOwnNonIndexProperties); | |
|