1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-02 23:57:25 +02:00
2023-07-21 18:11:48 +02:00

786 B

Expose a method

The first thing you will want to do with WebAssembly is expose your methods to Javascript. This is straightforward using the #[wasm_bindgen] procedural macro.

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn set_panic_hook() {
    // Generates better error messages if our code ever panics.
    //
    // For more details see
    // https://github.com/rustwasm/console_error_panic_hook#readme
    console_error_panic_hook::set_once();
}

// Exposes the `add` method from the previous slide
#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}
  • set_panic_hook is a convenient setup method that adds debug information to stack traces when a WASM module panics. Don't use it in prod builds because it is rather