From d379c0b299411a847765e2879f8ed14bdb2d0298 Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Wed, 3 Jul 2024 05:27:01 +0900 Subject: [PATCH] fix(ext/web): use primordials of ES2024 ArrayBuffer transfer (#24396) Ref: https://github.com/denoland/deno_core/issues/135 --- ext/web/06_streams.js | 35 +++++++++++++++-------------------- ext/web/README.md | 1 - ext/web/lib.rs | 14 -------------- 3 files changed, 15 insertions(+), 35 deletions(-) diff --git a/ext/web/06_streams.js b/ext/web/06_streams.js index 4ab1c3b5b0..d3de243637 100644 --- a/ext/web/06_streams.js +++ b/ext/web/06_streams.js @@ -14,7 +14,6 @@ const { isTypedArray, } = core; import { - op_arraybuffer_was_detached, // TODO(mmastrac): use readAll op_read_all, op_readable_stream_resource_allocate, @@ -25,13 +24,14 @@ import { op_readable_stream_resource_write_buf, op_readable_stream_resource_write_error, op_readable_stream_resource_write_sync, - op_transfer_arraybuffer, } from "ext:core/ops"; const { ArrayBuffer, ArrayBufferIsView, ArrayBufferPrototypeGetByteLength, + ArrayBufferPrototypeGetDetached, ArrayBufferPrototypeSlice, + ArrayBufferPrototypeTransferToFixedLength, ArrayPrototypeMap, ArrayPrototypePush, ArrayPrototypeShift, @@ -279,8 +279,7 @@ function isDetachedBuffer(O) { if (isSharedArrayBuffer(O)) { return false; } - return ArrayBufferPrototypeGetByteLength(O) === 0 && - op_arraybuffer_was_detached(O); + return ArrayBufferPrototypeGetDetached(O); } /** @@ -297,14 +296,6 @@ function canTransferArrayBuffer(O) { return true; } -/** - * @param {ArrayBufferLike} O - * @returns {ArrayBufferLike} - */ -function transferArrayBuffer(O) { - return op_transfer_arraybuffer(O); -} - /** * @param {ArrayBufferLike} O * @returns {number} @@ -1359,7 +1350,7 @@ function readableByteStreamControllerEnqueue(controller, chunk) { "chunk's buffer is detached and so cannot be enqueued", ); } - const transferredBuffer = transferArrayBuffer(buffer); + const transferredBuffer = ArrayBufferPrototypeTransferToFixedLength(buffer); if (controller[_pendingPullIntos].length !== 0) { const firstPendingPullInto = controller[_pendingPullIntos][0]; // deno-lint-ignore prefer-primordials @@ -1369,7 +1360,7 @@ function readableByteStreamControllerEnqueue(controller, chunk) { ); } readableByteStreamControllerInvalidateBYOBRequest(controller); - firstPendingPullInto.buffer = transferArrayBuffer( + firstPendingPullInto.buffer = ArrayBufferPrototypeTransferToFixedLength( // deno-lint-ignore prefer-primordials firstPendingPullInto.buffer, ); @@ -2029,7 +2020,7 @@ function readableByteStreamControllerPullInto( assert(minimumFill % elementSize === 0); try { - buffer = transferArrayBuffer(buffer); + buffer = ArrayBufferPrototypeTransferToFixedLength(buffer); } catch (e) { readIntoRequest.errorSteps(e); return; @@ -2122,8 +2113,10 @@ function readableByteStreamControllerRespond(controller, bytesWritten) { throw new RangeError("bytesWritten out of range"); } } - // deno-lint-ignore prefer-primordials - firstDescriptor.buffer = transferArrayBuffer(firstDescriptor.buffer); + firstDescriptor.buffer = ArrayBufferPrototypeTransferToFixedLength( + // deno-lint-ignore prefer-primordials + firstDescriptor.buffer, + ); readableByteStreamControllerRespondInternal(controller, bytesWritten); } @@ -2340,7 +2333,7 @@ function readableByteStreamControllerRespondWithNewView(controller, view) { "The region specified by view is larger than byobRequest", ); } - firstDescriptor.buffer = transferArrayBuffer(buffer); + firstDescriptor.buffer = ArrayBufferPrototypeTransferToFixedLength(buffer); readableByteStreamControllerRespondInternal(controller, byteLength); } @@ -2484,8 +2477,10 @@ function readableByteStreamControllerConvertPullIntoDescriptor( // deno-lint-ignore prefer-primordials assert(bytesFilled <= pullIntoDescriptor.byteLength); assert((bytesFilled % elementSize) === 0); - // deno-lint-ignore prefer-primordials - const buffer = transferArrayBuffer(pullIntoDescriptor.buffer); + const buffer = ArrayBufferPrototypeTransferToFixedLength( + // deno-lint-ignore prefer-primordials + pullIntoDescriptor.buffer, + ); return new pullIntoDescriptor.viewConstructor( buffer, // deno-lint-ignore prefer-primordials diff --git a/ext/web/README.md b/ext/web/README.md index 12e49416e7..81dc8dab23 100644 --- a/ext/web/README.md +++ b/ext/web/README.md @@ -143,7 +143,6 @@ Following ops are provided, which can be accessed through `Deno.ops`: - op_compression_finish - op_now - op_defer -- op_transfer_arraybuffer - op_readable_stream_resource_allocate - op_readable_stream_resource_allocate_sized - op_readable_stream_resource_get_sink diff --git a/ext/web/lib.rs b/ext/web/lib.rs index 554bad1de0..89f1197e71 100644 --- a/ext/web/lib.rs +++ b/ext/web/lib.rs @@ -87,7 +87,6 @@ deno_core::extension!(deno_web, compression::op_compression_finish, op_now

, op_defer, - op_transfer_arraybuffer, stream_resource::op_readable_stream_resource_allocate, stream_resource::op_readable_stream_resource_allocate_sized, stream_resource::op_readable_stream_resource_get_sink, @@ -424,19 +423,6 @@ fn op_encoding_encode_into_fast( out_buf[1] = boundary as u32; } -#[op2] -fn op_transfer_arraybuffer<'a>( - scope: &mut v8::HandleScope<'a>, - ab: &v8::ArrayBuffer, -) -> Result, AnyError> { - if !ab.is_detachable() { - return Err(type_error("ArrayBuffer is not detachable")); - } - let bs = ab.get_backing_store(); - ab.detach(None); - Ok(v8::ArrayBuffer::with_backing_store(scope, &bs)) -} - pub fn get_declaration() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_web.d.ts") }