Skip to content

Commit

Permalink
fix(ext/web): use primordials of ES2024 ArrayBuffer transfer (#24396)
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Jul 2, 2024
1 parent f78a60e commit d379c0b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 35 deletions.
35 changes: 15 additions & 20 deletions ext/web/06_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 14,6 @@ const {
isTypedArray,
} = core;
import {
op_arraybuffer_was_detached,
// TODO(mmastrac): use readAll
op_read_all,
op_readable_stream_resource_allocate,
Expand All @@ -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,
Expand Down Expand Up @@ -279,8 279,7 @@ function isDetachedBuffer(O) {
if (isSharedArrayBuffer(O)) {
return false;
}
return ArrayBufferPrototypeGetByteLength(O) === 0 &&
op_arraybuffer_was_detached(O);
return ArrayBufferPrototypeGetDetached(O);
}

/**
Expand All @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -1369,7 1360,7 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
);
}
readableByteStreamControllerInvalidateBYOBRequest(controller);
firstPendingPullInto.buffer = transferArrayBuffer(
firstPendingPullInto.buffer = ArrayBufferPrototypeTransferToFixedLength(
// deno-lint-ignore prefer-primordials
firstPendingPullInto.buffer,
);
Expand Down Expand Up @@ -2029,7 2020,7 @@ function readableByteStreamControllerPullInto(
assert(minimumFill % elementSize === 0);

try {
buffer = transferArrayBuffer(buffer);
buffer = ArrayBufferPrototypeTransferToFixedLength(buffer);
} catch (e) {
readIntoRequest.errorSteps(e);
return;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion ext/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions ext/web/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 87,6 @@ deno_core::extension!(deno_web,
compression::op_compression_finish,
op_now<P>,
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,
Expand Down Expand Up @@ -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<v8::Local<'a, v8::ArrayBuffer>, 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")
}
Expand Down

0 comments on commit d379c0b

Please sign in to comment.