mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-13 13:01:22 +00:00
Reorganize and rewrite examples
This commit is a large-ish scale reorganization of our examples. The main goal here is to have a dedicated section of the guide for example, and all examples will be listed there. Each example's `README` is now just boilerplate pointing at the guide along with a blurb about how to run it. Some examples like `math` and `smorgasboard` have been deleted as they didn't really serve much purpose, and others like `closures` have been rewritten with `web-sys` instead of hand-bound bindings. Overall it's hoped that this puts us in a good and consistent state for our examples, with all of them being described in the guide, excerpts are in the guide, and they're all relatively idiomatically using `web-sys`.
This commit is contained in:
@ -1,21 +1,69 @@
|
||||
extern crate wasm_bindgen;
|
||||
extern crate web_sys;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
// ... or you can manually write the bindings yourself
|
||||
#[wasm_bindgen]
|
||||
pub fn run() {
|
||||
bare_bones();
|
||||
using_a_macro();
|
||||
using_web_sys();
|
||||
}
|
||||
|
||||
// First up let's take a look of binding `console.log` manually, without the
|
||||
// help of `web_sys`. Here we're writing the `#[wasm_bindgen]` annotations
|
||||
// manually ourselves, and the correctness of our program relies on the
|
||||
// correctness of these annotations!
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
// Use `js_namespace` here to bind `console.log(..)` instead of just
|
||||
// `log(..)`
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn log(s: &str);
|
||||
|
||||
// The `console.log` is quite polymorphic, so we can bind it with multiple
|
||||
// signatures. Note that we need to use `js_name` to ensure we always call
|
||||
// `log` in JS.
|
||||
#[wasm_bindgen(js_namespace = console, js_name = log)]
|
||||
fn log_u32(a: u32);
|
||||
|
||||
// Multiple arguments too!
|
||||
#[wasm_bindgen(js_namespace = console, js_name = log)]
|
||||
fn log_many(a: &str, b: &str);
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn run() {
|
||||
fn bare_bones() {
|
||||
log("Hello from Rust!");
|
||||
log_u32(42);
|
||||
log_many("Logging", "many values!");
|
||||
}
|
||||
|
||||
// Next let's define a macro that's like `println!`, only it works for
|
||||
// `console.log`. Note that `println!` doesn't actually work on the wasm target
|
||||
// because the standard library currently just eats all output. To get
|
||||
// `println!`-like behavior in your app you'll likely want a macro like this.
|
||||
|
||||
macro_rules! console_log {
|
||||
// Note that this is using the `log` function imported above during
|
||||
// `bare_bones`
|
||||
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
|
||||
}
|
||||
|
||||
fn using_a_macro() {
|
||||
console_log!("Hello {}!", "world");
|
||||
console_log!("Let's print some numbers...");
|
||||
console_log!("1 + 3 = {}", 1 + 3);
|
||||
}
|
||||
|
||||
// And finally, we don't even have to define the `log` function ourselves! The
|
||||
// `web_sys` crate already has it defined for us.
|
||||
|
||||
fn using_web_sys() {
|
||||
use web_sys::console;
|
||||
|
||||
console::log_1(&"Hello using web-sys".into());
|
||||
|
||||
let js: JsValue = 4.into();
|
||||
console::log_2(&"Logging arbitrary values looks like".into(), &js);
|
||||
}
|
||||
|
Reference in New Issue
Block a user