Skip to content

Commit

Permalink
Show how to use web-sys::console::log from the console_log example
Browse files Browse the repository at this point in the history
  • Loading branch information
eminence committed Aug 18, 2018
1 parent 7a08da9 commit 4a994da
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/console_log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = { path = "../.." }
web-sys = { path = "../../crates/web-sys" }
6 changes: 4 additions & 2 deletions examples/console_log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

[View this example online](https://webassembly.studio/?f=ppd7u8k9i9)

This directory is an example of using the `#[wasm_bindgen]` macro to import the
`console.log` function and call it

This directory is an example of two ways to get access to the `console.log` function.
The first way uses the `#[wasm_bindgen]` macro to import the function and call it.
The second way uses the binding from the `web-sys` crate.

You can build the example with:

Expand Down
8 changes: 8 additions & 0 deletions examples/console_log/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#![feature(use_extern_macros)]

extern crate wasm_bindgen;
extern crate web_sys;

use wasm_bindgen::prelude::*;

// You can use the console bindings from web-sys...
use web_sys::console;

// ... or you can manually write the bindings yourself
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
Expand All @@ -19,4 +24,7 @@ pub fn run() {
log("Hello from Rust!");
log_u32(42);
log_many("Logging", "many values!");

console::log(JsValue::from("Another message from rust!"));
console::log(JsValue::from(56u32));
}

0 comments on commit 4a994da

Please sign in to comment.