mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
lib,src: use built-in array buffer detach, transfer
PR-URL: https://github.com/nodejs/node/pull/54837 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
This commit is contained in:
parent
e4692eee75
commit
9195210399
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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<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);
|
||||
|
|
|
|||
2
typings/primordials.d.ts
vendored
2
typings/primordials.d.ts
vendored
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user