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:
-
BufferSourceandArrayBufferView- these two types show up in a number of APIs that generally deal with a buffer of bytes. We bind them inweb-syswith two different types,js_sys::Objectand&mut [u8]. Usingjs_sys::Objectallows 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-sysare a raw JS value. You can work with this by either juggling actualjs_sys::Functioninstances or you can create aClosure<dyn FnMut(...)>, extract the underlyingJsValuewithas_ref, and then useJsCast::unchecked_refto convert it to ajs_sys::Function.