Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib,src: use built-in array buffer detach, transfer #54837

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
ArrayBufferPrototypeGetByteLength,
ArrayFrom,
ArrayIsArray,
ArrayPrototypePush,
Expand Down Expand Up @@ -57,7 +56,6 @@ const {
} = require('internal/errors');
const { signals } = internalBinding('constants').os;
const {
isArrayBufferDetached: _isArrayBufferDetached,
guessHandleType: _guessHandleType,
privateSymbols: {
arrow_message_private_symbol,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -917,7 +906,6 @@ module.exports = {
getSystemErrorMap,
getSystemErrorName,
guessHandleType,
isArrayBufferDetached,
isError,
isInsideNodeModules,
isMacOS,
Expand Down
23 changes: 11 additions & 12 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const {
ArrayBuffer,
ArrayBufferPrototypeGetByteLength,
ArrayBufferPrototypeGetDetached,
ArrayBufferPrototypeSlice,
ArrayBufferPrototypeTransfer,
ArrayPrototypePush,
ArrayPrototypeShift,
DataView,
Expand Down Expand Up @@ -50,7 +52,6 @@ const {
const {
createDeferredPromise,
customInspectSymbol: kInspect,
isArrayBufferDetached,
kEmptyObject,
kEnumerableProperty,
SideEffectFreeRegExpPrototypeSymbolReplace,
Expand Down Expand Up @@ -103,11 +104,9 @@ const {
extractHighWaterMark,
extractSizeAlgorithm,
lazyTransfer,
isViewedArrayBufferDetached,
isBrandCheck,
resetQueue,
setPromiseHandled,
transferArrayBuffer,
nonOpCancel,
nonOpPull,
nonOpStart,
Expand Down Expand Up @@ -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');
}

Expand All @@ -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');
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -2650,7 +2649,7 @@ function readableByteStreamControllerPullInto(

let transferredBuffer;
try {
transferredBuffer = transferArrayBuffer(buffer);
transferredBuffer = ArrayBufferPrototypeTransfer(buffer);
} catch (error) {
readIntoRequest[kError](error);
return;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -2793,20 +2792,20 @@ 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',
);
}

readableByteStreamControllerInvalidateBYOBRequest(controller);

firstPendingPullInto.buffer = transferArrayBuffer(
firstPendingPullInto.buffer = ArrayBufferPrototypeTransfer(
firstPendingPullInto.buffer,
);

Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 0 additions & 21 deletions lib/internal/webstreams/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -42,7 +40,6 @@ const {
} = internalBinding('util');

const assert = require('internal/assert');
const { isArrayBufferDetached } = require('internal/util');

const {
validateFunction,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -291,11 +272,9 @@ module.exports = {
invokePromiseCallback,
isBrandCheck,
isPromisePending,
isViewedArrayBufferDetached,
peekQueueValue,
resetQueue,
setPromiseHandled,
transferArrayBuffer,
nonOpCancel,
nonOpFlush,
nonOpPull,
Expand Down
12 changes: 0 additions & 12 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,6 @@ static void GetCallerLocation(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(Array::New(args.GetIsolate(), ret, arraysize(ret)));
}

static void IsArrayBufferDetached(const FunctionCallbackInfo<Value>& args) {
if (args[0]->IsArrayBuffer()) {
auto buffer = args[0].As<v8::ArrayBuffer>();
args.GetReturnValue().Set(buffer->WasDetached());
return;
}
args.GetReturnValue().Set(false);
}

static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
if (!args[0]->IsObject())
return;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -406,8 +396,6 @@ void Initialize(Local<Object> 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);
Expand Down
2 changes: 2 additions & 0 deletions typings/primordials.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof ArrayBuffer.prototype.detached>
export const ArrayBufferPrototypeSlice: UncurryThis<typeof ArrayBuffer.prototype.slice>
export const ArrayBufferPrototypeTransfer: UncurryThis<typeof ArrayBuffer.prototype.transfer>
export const AsyncIteratorPrototype: AsyncIterable<any>;
export import BigInt = globalThis.BigInt;
export const BigIntPrototype: typeof BigInt.prototype
Expand Down
Loading