From dde23d03cef08f153676c76c58a2bc10744993f5 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 15 Sep 2024 20:32:04 -0400 Subject: [PATCH] lib,src: use built-in array buffer detach, transfer PR-URL: https://github.com/nodejs/node/pull/54837 Reviewed-By: James M Snell Reviewed-By: Chengzhong Wu Reviewed-By: Antoine du Hamel Reviewed-By: Stephen Belanger --- lib/internal/util.js | 12 ------------ lib/internal/webstreams/readablestream.js | 23 +++++++++++------------ lib/internal/webstreams/util.js | 21 --------------------- src/node_util.cc | 12 ------------ typings/primordials.d.ts | 2 ++ 5 files changed, 13 insertions(+), 57 deletions(-) diff --git a/lib/internal/util.js b/lib/internal/util.js index 37d6ef7a93e477..9141811afa23e2 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -1,7 +1,6 @@ 'use strict'; const { - ArrayBufferPrototypeGetByteLength, ArrayFrom, ArrayIsArray, ArrayPrototypePush, @@ -57,7 +56,6 @@ const { } = require('internal/errors'); const { signals } = internalBinding('constants').os; const { - isArrayBufferDetached: _isArrayBufferDetached, guessHandleType: _guessHandleType, privateSymbols: { arrow_message_private_symbol, @@ -798,15 +796,6 @@ function SideEffectFreeRegExpPrototypeSymbolSplit(regex, string, limit = undefin return getCrossRelmRegex(regex)[SymbolSplit](string, limit); } - -function isArrayBufferDetached(value) { - if (ArrayBufferPrototypeGetByteLength(value) === 0) { - return _isArrayBufferDetached(value); - } - - return false; -} - /** * Helper function to lazy-load an initialize-once value. * @template T Return value of initializer @@ -917,7 +906,6 @@ module.exports = { getSystemErrorMap, getSystemErrorName, guessHandleType, - isArrayBufferDetached, isError, isInsideNodeModules, isMacOS, diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index 68ef663c530fe6..94c76a83898222 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -3,7 +3,9 @@ const { ArrayBuffer, ArrayBufferPrototypeGetByteLength, + ArrayBufferPrototypeGetDetached, ArrayBufferPrototypeSlice, + ArrayBufferPrototypeTransfer, ArrayPrototypePush, ArrayPrototypeShift, DataView, @@ -49,7 +51,6 @@ const { const { createDeferredPromise, customInspectSymbol: kInspect, - isArrayBufferDetached, kEmptyObject, kEnumerableProperty, SideEffectFreeRegExpPrototypeSymbolReplace, @@ -103,11 +104,9 @@ const { extractHighWaterMark, extractSizeAlgorithm, lazyTransfer, - isViewedArrayBufferDetached, isBrandCheck, resetQueue, setPromiseHandled, - transferArrayBuffer, nonOpCancel, nonOpPull, nonOpStart, @@ -698,7 +697,7 @@ class ReadableStreamBYOBRequest { const viewBuffer = ArrayBufferViewGetBuffer(view); const viewBufferByteLength = ArrayBufferPrototypeGetByteLength(viewBuffer); - if (isArrayBufferDetached(viewBuffer)) { + if (ArrayBufferPrototypeGetDetached(viewBuffer)) { throw new ERR_INVALID_STATE.TypeError('Viewed ArrayBuffer is detached'); } @@ -725,7 +724,7 @@ class ReadableStreamBYOBRequest { validateBuffer(view, 'view'); - if (isViewedArrayBufferDetached(view)) { + if (ArrayBufferPrototypeGetDetached(view.buffer)) { throw new ERR_INVALID_STATE.TypeError('Viewed ArrayBuffer is detached'); } @@ -1981,7 +1980,7 @@ function readableByteStreamControllerConvertPullIntoDescriptor(desc) { if (bytesFilled > byteLength) throw new ERR_INVALID_STATE.RangeError('The buffer size is invalid'); assert(!(bytesFilled % elementSize)); - const transferredBuffer = transferArrayBuffer(buffer); + const transferredBuffer = ArrayBufferPrototypeTransfer(buffer); if (ctor === Buffer) { return Buffer.from(transferredBuffer, byteOffset, bytesFilled / elementSize); @@ -2650,7 +2649,7 @@ function readableByteStreamControllerPullInto( let transferredBuffer; try { - transferredBuffer = transferArrayBuffer(buffer); + transferredBuffer = ArrayBufferPrototypeTransfer(buffer); } catch (error) { readIntoRequest[kError](error); return; @@ -2743,7 +2742,7 @@ function readableByteStreamControllerRespond(controller, bytesWritten) { throw new ERR_INVALID_ARG_VALUE.RangeError('bytesWritten', bytesWritten); } - desc.buffer = transferArrayBuffer(desc.buffer); + desc.buffer = ArrayBufferPrototypeTransfer(desc.buffer); readableByteStreamControllerRespondInternal(controller, bytesWritten); } @@ -2793,12 +2792,12 @@ function readableByteStreamControllerEnqueue(controller, chunk) { if (closeRequested || stream[kState].state !== 'readable') return; - const transferredBuffer = transferArrayBuffer(buffer); + const transferredBuffer = ArrayBufferPrototypeTransfer(buffer); if (pendingPullIntos.length) { const firstPendingPullInto = pendingPullIntos[0]; - if (isArrayBufferDetached(firstPendingPullInto.buffer)) { + if (ArrayBufferPrototypeGetDetached(firstPendingPullInto.buffer)) { throw new ERR_INVALID_STATE.TypeError( 'Destination ArrayBuffer is detached', ); @@ -2806,7 +2805,7 @@ function readableByteStreamControllerEnqueue(controller, chunk) { readableByteStreamControllerInvalidateBYOBRequest(controller); - firstPendingPullInto.buffer = transferArrayBuffer( + firstPendingPullInto.buffer = ArrayBufferPrototypeTransfer( firstPendingPullInto.buffer, ); @@ -3104,7 +3103,7 @@ function readableByteStreamControllerRespondWithNewView(controller, view) { if (bufferByteLength !== viewBufferByteLength) throw new ERR_INVALID_ARG_VALUE.RangeError('view', view); - desc.buffer = transferArrayBuffer(viewBuffer); + desc.buffer = ArrayBufferPrototypeTransfer(viewBuffer); readableByteStreamControllerRespondInternal(controller, viewByteLength); } diff --git a/lib/internal/webstreams/util.js b/lib/internal/webstreams/util.js index 76e58d642c8d9f..cace607a077618 100644 --- a/lib/internal/webstreams/util.js +++ b/lib/internal/webstreams/util.js @@ -21,13 +21,11 @@ const { ERR_ARG_NOT_ITERABLE, ERR_INVALID_ARG_VALUE, ERR_INVALID_STATE, - ERR_OPERATION_FAILED, }, } = require('internal/errors'); const { copyArrayBuffer, - detachArrayBuffer, } = internalBinding('buffer'); const { @@ -42,7 +40,6 @@ const { } = internalBinding('util'); const assert = require('internal/assert'); -const { isArrayBufferDetached } = require('internal/util'); const { validateFunction, @@ -118,22 +115,6 @@ function isBrandCheck(brand) { }; } -function transferArrayBuffer(buffer) { - const res = detachArrayBuffer(buffer); - if (res === undefined) { - throw new ERR_OPERATION_FAILED.TypeError( - 'The ArrayBuffer could not be transferred'); - } - return res; -} - -function isViewedArrayBufferDetached(view) { - return ( - ArrayBufferViewGetByteLength(view) === 0 && - isArrayBufferDetached(ArrayBufferViewGetBuffer(view)) - ); -} - function dequeueValue(controller) { assert(controller[kState].queue !== undefined); assert(controller[kState].queueTotalSize !== undefined); @@ -291,11 +272,9 @@ module.exports = { invokePromiseCallback, isBrandCheck, isPromisePending, - isViewedArrayBufferDetached, peekQueueValue, resetQueue, setPromiseHandled, - transferArrayBuffer, nonOpCancel, nonOpFlush, nonOpPull, diff --git a/src/node_util.cc b/src/node_util.cc index 680fb10c0ec51d..ea16a472c27c67 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -162,15 +162,6 @@ static void GetCallerLocation(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(Array::New(args.GetIsolate(), ret, arraysize(ret))); } -static void IsArrayBufferDetached(const FunctionCallbackInfo& args) { - if (args[0]->IsArrayBuffer()) { - auto buffer = args[0].As(); - args.GetReturnValue().Set(buffer->WasDetached()); - return; - } - args.GetReturnValue().Set(false); -} - static void PreviewEntries(const FunctionCallbackInfo& args) { if (!args[0]->IsObject()) return; @@ -306,7 +297,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(GetPromiseDetails); registry->Register(GetProxyDetails); registry->Register(GetCallerLocation); - registry->Register(IsArrayBufferDetached); registry->Register(PreviewEntries); registry->Register(GetCallSite); registry->Register(GetOwnNonIndexProperties); @@ -406,8 +396,6 @@ void Initialize(Local target, SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails); SetMethodNoSideEffect( context, target, "getCallerLocation", GetCallerLocation); - SetMethodNoSideEffect( - context, target, "isArrayBufferDetached", IsArrayBufferDetached); SetMethodNoSideEffect(context, target, "previewEntries", PreviewEntries); SetMethodNoSideEffect( context, target, "getOwnNonIndexProperties", GetOwnNonIndexProperties); diff --git a/typings/primordials.d.ts b/typings/primordials.d.ts index 987239c01245a7..e53ea514a9b249 100644 --- a/typings/primordials.d.ts +++ b/typings/primordials.d.ts @@ -150,7 +150,9 @@ declare namespace primordials { export import ArrayBuffer = globalThis.ArrayBuffer; export const ArrayBufferPrototype: typeof ArrayBuffer.prototype export const ArrayBufferIsView: typeof ArrayBuffer.isView + export const ArrayBufferPrototypeGetDetached: UncurryThis export const ArrayBufferPrototypeSlice: UncurryThis + export const ArrayBufferPrototypeTransfer: UncurryThis export const AsyncIteratorPrototype: AsyncIterable; export import BigInt = globalThis.BigInt; export const BigIntPrototype: typeof BigInt.prototype