wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

web-sys: unnecessary &mut requirement in WebUSB functions, potentially others

Open spookyvision opened this issue 1 year ago • 3 comments

several WebUSB functions that take a [u8] slice, e.g., transfer_out_with_u8_array, require it be passed as &mut. As far as I can tell this is not required since USB transfers are unidirectional.

full list of affected functions:

other APIs might also be affected - I searched the docs for with_u8_array and found e.g. ReadableByteStreamController::enqueue_with_u8_array taking a &mut buffer which at first glance is only used for reading.

spookyvision avatar May 15 '24 21:05 spookyvision

This is the case because web-sys is auto-generated from WebIDL, which doesn't include any information about whether or not buffers passed to functions are mutable.

Because of that, it conservatively assumes that all buffers are mutable, since it can't result in UB if a web API tries to mutate an immutable buffer and it's backwards-compatible to change a slice from mutable to immutable.

That change is made by adding the functions whose slices should be immutable to a big list: https://github.com/rustwasm/wasm-bindgen/blob/fa6d2bc726813c9f5370f07c64a4b60f0ac1edd5/crates/webidl/src/constants.rs#L43-L105.

Liamolucko avatar May 16 '24 06:05 Liamolucko

@spookyvision Do you want to see if WebIDL is aware of the limitation? Solve the problem upstream?

xpe avatar May 27 '24 12:05 xpe

I did a quick scan of the WebIDL spec and it doesn't seem they have a concept of (im)mutability, sadly. I might be wrong, but since the spec refers JavaScript a lot, it's probably closely modeled based on JS semantics.

spookyvision avatar May 27 '24 14:05 spookyvision