Skip to content

Commit 6f0ac74

Browse files
anonrigErickWendel
authored andcommitted
src: add internal isArrayBufferDetached
PR-URL: nodejs#45568 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
1 parent 4948a82 commit 6f0ac74

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

lib/internal/util.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ArrayBufferPrototypeGetByteLength,
45
ArrayFrom,
56
ArrayIsArray,
67
ArrayPrototypePush,
@@ -42,6 +43,7 @@ const {
4243
} = require('internal/errors');
4344
const { signals } = internalBinding('constants').os;
4445
const {
46+
isArrayBufferDetached: _isArrayBufferDetached,
4547
privateSymbols: {
4648
arrow_message_private_symbol,
4749
decorated_private_symbol,
@@ -560,6 +562,14 @@ function SideEffectFreeRegExpPrototypeExec(regex, string) {
560562
return FunctionPrototypeCall(RegExpFromAnotherRealm.prototype.exec, regex, string);
561563
}
562564

565+
function isArrayBufferDetached(value) {
566+
if (ArrayBufferPrototypeGetByteLength(value) === 0) {
567+
return _isArrayBufferDetached(value);
568+
}
569+
570+
return false;
571+
}
572+
563573
module.exports = {
564574
assertCrypto,
565575
cachedResult,
@@ -577,6 +587,7 @@ module.exports = {
577587
getInternalGlobal,
578588
getSystemErrorMap,
579589
getSystemErrorName,
590+
isArrayBufferDetached,
580591
isError,
581592
isInsideNodeModules,
582593
join,

lib/internal/webstreams/readablestream.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const {
5151
const {
5252
createDeferredPromise,
5353
customInspectSymbol: kInspect,
54+
isArrayBufferDetached,
5455
kEmptyObject,
5556
kEnumerableProperty,
5657
} = require('internal/util');
@@ -104,7 +105,6 @@ const {
104105
extractHighWaterMark,
105106
extractSizeAlgorithm,
106107
lazyTransfer,
107-
isDetachedBuffer,
108108
isViewedArrayBufferDetached,
109109
isBrandCheck,
110110
resetQueue,
@@ -669,7 +669,7 @@ class ReadableStreamBYOBRequest {
669669
const viewBuffer = ArrayBufferViewGetBuffer(view);
670670
const viewBufferByteLength = ArrayBufferPrototypeGetByteLength(viewBuffer);
671671

672-
if (isDetachedBuffer(viewBuffer)) {
672+
if (isArrayBufferDetached(viewBuffer)) {
673673
throw new ERR_INVALID_STATE.TypeError('Viewed ArrayBuffer is detached');
674674
}
675675

@@ -2643,7 +2643,7 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
26432643
if (pendingPullIntos.length) {
26442644
const firstPendingPullInto = pendingPullIntos[0];
26452645

2646-
if (isDetachedBuffer(firstPendingPullInto.buffer)) {
2646+
if (isArrayBufferDetached(firstPendingPullInto.buffer)) {
26472647
throw new ERR_INVALID_STATE.TypeError(
26482648
'Destination ArrayBuffer is detached',
26492649
);

lib/internal/webstreams/util.js

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {
4-
ArrayBufferPrototypeGetByteLength,
54
ArrayBufferPrototypeSlice,
65
ArrayPrototypePush,
76
ArrayPrototypeShift,
@@ -47,6 +46,7 @@ const {
4746
} = internalBinding('util');
4847

4948
const assert = require('internal/assert');
49+
const { isArrayBufferDetached } = require('internal/util');
5050

5151
const kState = Symbol('kState');
5252
const kType = Symbol('kType');
@@ -137,23 +137,10 @@ function transferArrayBuffer(buffer) {
137137
return res;
138138
}
139139

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-
153140
function isViewedArrayBufferDetached(view) {
154141
return (
155142
ArrayBufferViewGetByteLength(view) === 0 &&
156-
isDetachedBuffer(ArrayBufferViewGetBuffer(view))
143+
isArrayBufferDetached(ArrayBufferViewGetBuffer(view))
157144
);
158145
}
159146

@@ -253,7 +240,6 @@ module.exports = {
253240
extractSizeAlgorithm,
254241
lazyTransfer,
255242
isBrandCheck,
256-
isDetachedBuffer,
257243
isPromisePending,
258244
isViewedArrayBufferDetached,
259245
peekQueueValue,

src/node_util.cc

+12
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
138138
}
139139
}
140140

141+
static void IsArrayBufferDetached(const FunctionCallbackInfo<Value>& args) {
142+
if (args[0]->IsArrayBuffer()) {
143+
auto buffer = args[0].As<v8::ArrayBuffer>();
144+
args.GetReturnValue().Set(buffer->WasDetached());
145+
return;
146+
}
147+
args.GetReturnValue().Set(false);
148+
}
149+
141150
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
142151
if (!args[0]->IsObject())
143152
return;
@@ -343,6 +352,7 @@ static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
343352
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
344353
registry->Register(GetPromiseDetails);
345354
registry->Register(GetProxyDetails);
355+
registry->Register(IsArrayBufferDetached);
346356
registry->Register(PreviewEntries);
347357
registry->Register(GetOwnNonIndexProperties);
348358
registry->Register(GetConstructorName);
@@ -427,6 +437,8 @@ void Initialize(Local<Object> target,
427437
SetMethodNoSideEffect(
428438
context, target, "getPromiseDetails", GetPromiseDetails);
429439
SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails);
440+
SetMethodNoSideEffect(
441+
context, target, "isArrayBufferDetached", IsArrayBufferDetached);
430442
SetMethodNoSideEffect(context, target, "previewEntries", PreviewEntries);
431443
SetMethodNoSideEffect(
432444
context, target, "getOwnNonIndexProperties", GetOwnNonIndexProperties);

0 commit comments

Comments
 (0)