Skip to content

Commit ae61740

Browse files
anonrigdanielleadams
authored andcommitted
src: add internal isArrayBufferDetached
PR-URL: #45568 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
1 parent 90e8418 commit ae61740

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,
@@ -557,6 +559,14 @@ function SideEffectFreeRegExpPrototypeExec(regex, string) {
557559
return FunctionPrototypeCall(RegExpFromAnotherRealm.prototype.exec, regex, string);
558560
}
559561

562+
function isArrayBufferDetached(value) {
563+
if (ArrayBufferPrototypeGetByteLength(value) === 0) {
564+
return _isArrayBufferDetached(value);
565+
}
566+
567+
return false;
568+
}
569+
560570
module.exports = {
561571
assertCrypto,
562572
cachedResult,
@@ -574,6 +584,7 @@ module.exports = {
574584
getInternalGlobal,
575585
getSystemErrorMap,
576586
getSystemErrorName,
587+
isArrayBufferDetached,
577588
isError,
578589
isInsideNodeModules,
579590
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');
@@ -103,7 +104,6 @@ const {
103104
extractHighWaterMark,
104105
extractSizeAlgorithm,
105106
lazyTransfer,
106-
isDetachedBuffer,
107107
isViewedArrayBufferDetached,
108108
isBrandCheck,
109109
resetQueue,
@@ -668,7 +668,7 @@ class ReadableStreamBYOBRequest {
668668
const viewBuffer = ArrayBufferViewGetBuffer(view);
669669
const viewBufferByteLength = ArrayBufferPrototypeGetByteLength(viewBuffer);
670670

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

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

2644-
if (isDetachedBuffer(firstPendingPullInto.buffer)) {
2644+
if (isArrayBufferDetached(firstPendingPullInto.buffer)) {
26452645
throw new ERR_INVALID_STATE.TypeError(
26462646
'Destination ArrayBuffer is detached',
26472647
);

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
@@ -137,6 +137,15 @@ static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
137137
}
138138
}
139139

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+
140149
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
141150
if (!args[0]->IsObject())
142151
return;
@@ -349,6 +358,7 @@ static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
349358
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
350359
registry->Register(GetPromiseDetails);
351360
registry->Register(GetProxyDetails);
361+
registry->Register(IsArrayBufferDetached);
352362
registry->Register(PreviewEntries);
353363
registry->Register(GetOwnNonIndexProperties);
354364
registry->Register(GetConstructorName);
@@ -422,6 +432,8 @@ void Initialize(Local<Object> target,
422432
SetMethodNoSideEffect(
423433
context, target, "getPromiseDetails", GetPromiseDetails);
424434
SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails);
435+
SetMethodNoSideEffect(
436+
context, target, "isArrayBufferDetached", IsArrayBufferDetached);
425437
SetMethodNoSideEffect(context, target, "previewEntries", PreviewEntries);
426438
SetMethodNoSideEffect(
427439
context, target, "getOwnNonIndexProperties", GetOwnNonIndexProperties);

0 commit comments

Comments
 (0)