Skip to content

Commit 7d64817

Browse files
committed
src: add internal isArrayBufferDetached
1 parent f8ce911 commit 7d64817

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,
@@ -45,6 +44,7 @@ const {
4544
} = internalBinding('util');
4645

4746
const assert = require('internal/assert');
47+
const { isArrayBufferDetached } = require("internal/util");
4848

4949
const kState = Symbol('kState');
5050
const kType = Symbol('kType');
@@ -135,23 +135,10 @@ function transferArrayBuffer(buffer) {
135135
return res;
136136
}
137137

138-
function isDetachedBuffer(buffer) {
139-
if (ArrayBufferPrototypeGetByteLength(buffer) === 0) {
140-
// TODO(daeyeon): Consider using C++ builtin to improve performance.
141-
try {
142-
new Uint8Array(buffer);
143-
} catch (error) {
144-
assert(error.name === 'TypeError');
145-
return true;
146-
}
147-
}
148-
return false;
149-
}
150-
151138
function isViewedArrayBufferDetached(view) {
152139
return (
153140
ArrayBufferViewGetByteLength(view) === 0 &&
154-
isDetachedBuffer(ArrayBufferViewGetBuffer(view))
141+
isArrayBufferDetached(ArrayBufferViewGetBuffer(view))
155142
);
156143
}
157144

@@ -251,7 +238,6 @@ module.exports = {
251238
extractSizeAlgorithm,
252239
lazyTransfer,
253240
isBrandCheck,
254-
isDetachedBuffer,
255241
isPromisePending,
256242
isViewedArrayBufferDetached,
257243
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;
@@ -342,6 +351,7 @@ static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
342351
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
343352
registry->Register(GetPromiseDetails);
344353
registry->Register(GetProxyDetails);
354+
registry->Register(IsArrayBufferDetached);
345355
registry->Register(PreviewEntries);
346356
registry->Register(GetOwnNonIndexProperties);
347357
registry->Register(GetConstructorName);
@@ -403,6 +413,8 @@ void Initialize(Local<Object> target,
403413
SetMethodNoSideEffect(
404414
context, target, "getPromiseDetails", GetPromiseDetails);
405415
SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails);
416+
SetMethodNoSideEffect(
417+
context, target, "isArrayBufferDetached", IsArrayBufferDetached);
406418
SetMethodNoSideEffect(context, target, "previewEntries", PreviewEntries);
407419
SetMethodNoSideEffect(
408420
context, target, "getOwnNonIndexProperties", GetOwnNonIndexProperties);

0 commit comments

Comments
 (0)