Skip to content

Commit

Permalink
lib,src: use built-in array buffer detach, transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 7, 2024
1 parent 8882a21 commit 6723407
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 68 deletions.
17 changes: 1 addition & 16 deletions lib/internal/modules/esm/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ const { clearImmediate, setImmediate } = require('timers');
const {
hasUncaughtExceptionCaptureCallback,
} = require('internal/process/execution');
const {
isArrayBuffer,
isDataView,
isTypedArray,
} = require('util/types');

const { receiveMessageOnPort } = require('internal/worker/io');
const {
Expand All @@ -35,17 +30,7 @@ const { isMarkedAsUntransferable } = require('internal/buffer');
*/
function transferArrayBuffer(hasError, source) {
if (hasError || source == null) { return; }
let arrayBuffer;
if (isArrayBuffer(source)) {
arrayBuffer = source;
} else if (isTypedArray(source)) {
arrayBuffer = TypedArrayPrototypeGetBuffer(source);
} else if (isDataView(source)) {
arrayBuffer = DataViewPrototypeGetBuffer(source);
}
if (arrayBuffer && !isMarkedAsUntransferable(arrayBuffer)) {
return [arrayBuffer];
}
return ArrayBufferPrototypeTransfer(source);

Check failure on line 33 in lib/internal/modules/esm/worker.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'ArrayBufferPrototypeTransfer' is not defined
}

/**
Expand Down
12 changes: 1 addition & 11 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ArrayBufferPrototypeGetByteLength,

Check failure on line 4 in lib/internal/util.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'ArrayBufferPrototypeGetByteLength' is assigned a value but never used
ArrayBufferPrototypeDetached,

Check failure on line 5 in lib/internal/util.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Out of ASCIIbetical order - ArrayBufferPrototypeGetByteLength >= ArrayBufferPrototypeDetached

Check failure on line 5 in lib/internal/util.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'ArrayBufferPrototypeDetached' is assigned a value but never used
ArrayFrom,
ArrayIsArray,
ArrayPrototypePush,
Expand Down Expand Up @@ -57,7 +58,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 +798,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 +908,6 @@ module.exports = {
getSystemErrorMap,
getSystemErrorName,
guessHandleType,
isArrayBufferDetached,
isError,
isInsideNodeModules,
isMacOS,
Expand Down
20 changes: 10 additions & 10 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const {
ArrayBuffer,
ArrayBufferPrototypeGetByteLength,
ArrayBufferPrototypeSlice,
ArrayBufferPrototypeDetached,

Check failure on line 7 in lib/internal/webstreams/readablestream.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Out of ASCIIbetical order - ArrayBufferPrototypeSlice >= ArrayBufferPrototypeDetached
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 @@ -107,7 +108,6 @@ const {
isBrandCheck,
resetQueue,
setPromiseHandled,
transferArrayBuffer,
nonOpCancel,
nonOpPull,
nonOpStart,
Expand Down Expand Up @@ -698,7 +698,7 @@ class ReadableStreamBYOBRequest {
const viewBuffer = ArrayBufferViewGetBuffer(view);
const viewBufferByteLength = ArrayBufferPrototypeGetByteLength(viewBuffer);

if (isArrayBufferDetached(viewBuffer)) {
if (ArrayBufferPrototypeDetached(viewBuffer)) {
throw new ERR_INVALID_STATE.TypeError('Viewed ArrayBuffer is detached');
}

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

let transferredBuffer;
try {
transferredBuffer = transferArrayBuffer(buffer);
transferredBuffer = ArrayBufferPrototypeTransfer(buffer);
} catch (error) {
readIntoRequest[kError](error);
return;
Expand Down Expand Up @@ -2743,7 +2743,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 +2793,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 (ArrayBufferPrototypeDetached(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 +3104,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
20 changes: 1 addition & 19 deletions lib/internal/webstreams/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ArrayBufferPrototypeSlice,
ArrayBufferPrototypeDetached,

Check failure on line 5 in lib/internal/webstreams/util.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Out of ASCIIbetical order - ArrayBufferPrototypeSlice >= ArrayBufferPrototypeDetached

Check failure on line 5 in lib/internal/webstreams/util.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'ArrayBufferPrototypeDetached' is assigned a value but never used
ArrayPrototypePush,
ArrayPrototypeShift,
AsyncIteratorPrototype,
Expand Down Expand Up @@ -42,7 +43,6 @@ const {
} = internalBinding('util');

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

const {
validateFunction,
Expand Down Expand Up @@ -118,22 +118,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 +275,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 @@ -151,6 +151,8 @@ declare namespace primordials {
export const ArrayBufferPrototype: typeof ArrayBuffer.prototype
export const ArrayBufferIsView: typeof ArrayBuffer.isView
export const ArrayBufferPrototypeSlice: UncurryThis<typeof ArrayBuffer.prototype.slice>
export const ArrayBufferPrototypeDetached: UncurryThis<typeof ArrayBuffer.prototype.detached>
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

0 comments on commit 6723407

Please sign in to comment.