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 b1e1e32
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 74 deletions.
21 changes: 2 additions & 19 deletions lib/internal/modules/esm/worker.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
'use strict';

const {
ArrayBufferPrototypeTransfer,
AtomicsAdd,
AtomicsNotify,
DataViewPrototypeGetBuffer,
Int32Array,
PromisePrototypeThen,
ReflectApply,
SafeSet,
TypedArrayPrototypeGetBuffer,
} = primordials;
const assert = require('internal/assert');
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 {
WORKER_TO_MAIN_THREAD_NOTIFICATION,
} = require('internal/modules/esm/shared_constants');
const { initializeHooks } = require('internal/modules/esm/utils');
const { isMarkedAsUntransferable } = require('internal/buffer');

/**
* Transfers an ArrayBuffer, TypedArray, or DataView to a worker thread.
Expand All @@ -35,17 +28,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);
}

/**
Expand Down
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
20 changes: 10 additions & 10 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

const {
ArrayBuffer,
ArrayBufferPrototypeDetached,
ArrayBufferPrototypeGetByteLength,
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 @@ -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
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 @@ -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 b1e1e32

Please sign in to comment.