Importing non-browser JS
View full source code or view the compiled example online
The #[wasm_bindgen]
attribute can be used on extern "C" { .. }
blocks to import
functionality from JS. This is how the js-sys
and the web-sys
crates are
built, but you can also use it in your own crate!
For example if you're working with this JS file:
// defined-in-js.js
export function name() {
return 'Rust';
}
export class MyClass {
constructor() {
this._number = 42;
}
get number() {
return this._number;
}
set number(n) {
return this._number = n;
}
render() {
return `My number is: ${this.number}`;
}
}
you can use it in Rust with:
You can also explore the full list of ways to configure imports