| 1 |
# `js-sys` |
| 2 |
|
| 3 |
The [`js-sys` crate][js-sys] provides raw bindings to all the global APIs |
| 4 |
guaranteed to exist in every JavaScript environment by the ECMAScript standard, |
| 5 |
and its source lives at [`wasm-bindgen/crates/js-sys`][src]. With the `js-sys` |
| 6 |
crate, we can work with `Object`s, `Array`s, `Function`s, `Map`s, `Set`s, |
| 7 |
etc... without writing the `#[wasm_bindgen]` imports by hand. |
| 8 |
|
| 9 |
Documentation for the published version of this crate is available on |
| 10 |
[docs.rs][docsrs] but you can also check out the [master branch |
| 11 |
documentation][masterdoc] for the crate. |
| 12 |
|
| 13 |
[docsrs]: https://docs.rs/js-sys |
| 14 |
[masterdoc]: https://rustwasm.github.io/wasm-bindgen/api/js_sys/ |
| 15 |
[src]: https://github.com/rustwasm/wasm-bindgen/tree/master/crates/js-sys |
| 16 |
|
| 17 |
For example, we can invoke JavaScript [`Function`][mdn-function] callbacks and |
| 18 |
time how long they take to execute with [`Date.now()`][mdn-date-now], and we |
| 19 |
don't need to write any JS imports ourselves: |
| 20 |
|
| 21 |
```rust |
| 22 |
use wasm_bindgen::prelude::*; |
| 23 |
|
| 24 |
#[wasm_bindgen] |
| 25 |
pub fn timed(callback: &js_sys::Function) -> f64 { |
| 26 |
let then = js_sys::Date::now(); |
| 27 |
callback.apply(JsValue::null(), &js_sys::Array::new()).unwrap(); |
| 28 |
let now = js_sys::Date::now(); |
| 29 |
now - then |
| 30 |
} |
| 31 |
``` |
| 32 |
|
| 33 |
The `js-sys` crate doesn't contain bindings to any Web APIs like |
| 34 |
[`document.querySelectorAll`][mdn-qsa]. These will be part of the |
| 35 |
[`web-sys`][web-sys] crate. |
| 36 |
|
| 37 |
[MDN]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects |
| 38 |
[js-sys]: https://crates.io/crates/js-sys |
| 39 |
[issue]: https://github.com/rustwasm/wasm-bindgen/issues/275 |
| 40 |
[mdn-function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function |
| 41 |
[mdn-qsa]: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll |
| 42 |
[web-sys]: https://crates.io/crates/web-sys |
| 43 |
[web-sys-contributing]: https://rustwasm.github.io/wasm-bindgen/web-sys.html |
| 44 |
[web-sys-issues]: https://github.com/rustwasm/wasm-bindgen/issues?q=is%3Aissue+is%3Aopen+label%3Aweb-sys |
| 45 |
[mdn-date-now]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now |