#emscripten #bindings #api-bindings

emscripten-functions

Rust-friendly bindings to various emscripten system functions

3 unstable releases

0.2.1 Oct 2, 2023
0.2.0 Sep 6, 2023
0.1.0 Aug 26, 2023

#521 in WebAssembly

Download history 4/week @ 2024-02-14 37/week @ 2024-02-21 4/week @ 2024-02-28 2/week @ 2024-03-06 15/week @ 2024-03-13 31/week @ 2024-03-20 32/week @ 2024-03-27 32/week @ 2024-04-03 8/week @ 2024-04-10

83 downloads per month

MIT license

2.5MB
4.5K SLoC

emscripten-functions

This crate contains various emscripten system functions (made with rust-native parameter and return value types) that make programming in rust for emscripten targets easier. Functions based on ones from the following emscripten headers are available:

  • emscripten
  • console

Examples

Run javascript from rust

Using the emscripten_functions::emscripten::run_script family of functions you can run the javascript you need in your web app.

Example

// The `.escape_unicode()` method makes it safe to pass untrusted user input.
run_script(
    format!(
        r##"
            document.querySelector("#this-is-secure").innerHTML = "{}"
        "##, 
        "untrusted user input".escape_unicode()
    )
);

Main loop control

If you need to run a loop function over and over, emscripten has its own main loop managing system. Using the emscripten_functions::emscripten::set_main_loop and emscripten_functions::emscripten::set_main_loop_with_arg functions you can run your rust functions as main loops, with full control over the main loop running parameters.

Example

struct GameData {
    level: u32,
    score: u32
}
let mut game_data = GameData {
    level: 1,
    score: 0
}

set_main_loop_with_arg(|data| {
    if data.score < data.level {
        data.score += 1;
    } else {
        data.score = 0;
        data.level += 1;
    }

    // Here you call your display to screen functions.
    // For demonstration purposes I chose `println!`.
    println!("Score {}, level {}", data.score, data.level);
}, game_data, 0, true);

Dependencies