1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/web): use primordials of ES2024 ArrayBuffer transfer (#24396)

Ref: https://github.com/denoland/deno_core/issues/135
This commit is contained in:
Kenta Moriuchi 2024-07-03 05:27:01 +09:00 committed by GitHub
parent f78a60e882
commit d379c0b299
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 35 deletions

View file

@ -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");
}
}
firstDescriptor.buffer = ArrayBufferPrototypeTransferToFixedLength(
// deno-lint-ignore prefer-primordials
firstDescriptor.buffer = transferArrayBuffer(firstDescriptor.buffer);
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);
const buffer = ArrayBufferPrototypeTransferToFixedLength(
// deno-lint-ignore prefer-primordials
const buffer = transferArrayBuffer(pullIntoDescriptor.buffer);
pullIntoDescriptor.buffer,
);
return new pullIntoDescriptor.viewConstructor(
buffer,
// deno-lint-ignore prefer-primordials

View file

@ -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

View file

@ -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,
@ -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")
}