Last active
May 9, 2024 13:27
-
-
Save steveklabnik/d86491646b9e3e420f8806a286ec8e92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(lang_items)] | |
#![no_std] | |
#[no_mangle] | |
pub fn add_one(x: i32) -> i32 { | |
x + 1 | |
} | |
// needed for no_std | |
#[lang = "panic_fmt"] | |
#[no_mangle] | |
pub extern fn rust_begin_panic(_msg: core::fmt::Arguments, | |
_file: &'static str, | |
_line: u32, | |
_column: u32) -> ! { | |
loop {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rustup update nightly | |
$ rustup target add wasm32-unknown-unknown --toolchain nightly | |
$ rustc +nightly --target wasm32-unknown-unknown -O .\add.rs --crate-type=cdylib | |
$ cargo install --git https://github.com/alexcrichton/wasm-gc | |
$ wasm-gc .\add.wasm small-add.wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>wasm wasm wasm</title> | |
<script> | |
fetch('small-add.wasm').then(response => | |
response.arrayBuffer() | |
).then(bytes => | |
WebAssembly.instantiate(bytes, {}) | |
).then(results => { | |
alert(results.instance.exports.add_one(41)); | |
}); | |
</script> | |
</head> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment