Type Translations in web-sys
Most of the types specified in WebIDL (the interface definition language for
all Web APIs) have relatively straightforward translations into
web-sys
, but it's worth calling out a few in particular:
-
BufferSource
andArrayBufferView
- these two types show up in a number of APIs that generally deal with a buffer of bytes. We bind them inweb-sys
with two different types,js_sys::Object
and&mut [u8]
. Usingjs_sys::Object
allows passing in arbitrary JS values which represent a view of bytes (like any typed array object), and&mut [u8]
allows using a raw slice in Rust. Unfortunately we must pessimistically assume that JS will modify all slices as we don't currently have information of whether they're modified or not. -
Callbacks are all represented as
js_sys::Function
. This means that all callbacks going throughweb-sys
are a raw JS value. You can work with this by either juggling actualjs_sys::Function
instances or you can create aClosure<dyn FnMut(...)>
, extract the underlyingJsValue
withas_ref
, and then useJsCast::unchecked_ref
to convert it to ajs_sys::Function
.