Created
August 15, 2018 10:09
-
-
Save richard-uk1/b0e9c5a807e29d0b5f89f8f064880676 to your computer and use it in GitHub Desktop.
Experiments handling errors in wasm-bindgen.
This file contains hidden or 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(use_extern_macros, wasm_custom_section, wasm_import_module, proc_macro_mod)] | |
extern crate wasm_bindgen; | |
extern crate web_sys; | |
use wasm_bindgen::prelude::*; | |
use web_sys::{console, Node}; | |
macro_rules! try_web { | |
($e:expr) => { | |
match $e { | |
Ok(inner) => inner, | |
Err(e) => { | |
console::error(e.clone()); | |
panic!(e); | |
} | |
} | |
}; | |
} | |
#[wasm_bindgen] | |
extern { | |
static window: web_sys::Window; | |
} | |
#[wasm_bindgen] | |
pub fn run() { | |
std::panic::set_hook(Box::new(|info| { console::error(info.to_string().into()); })); | |
let doc = window.document().unwrap(); | |
let el = try_web!(doc.create_element_using_local_name("p")); | |
let text = doc.create_text_node("content"); | |
try_web!(<AsRef<Node>>::as_ref(&el).append_child(<AsRef<Node>>::as_ref(&text))); | |
try_web!(<AsRef<Node>>::as_ref(&doc).append_child(<AsRef<Node>>::as_ref(&el))); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment