Add an example of importing non-browser functions

Closes #208
This commit is contained in:
Alex Crichton
2018-05-23 12:06:49 -07:00
parent 4ddd93d75d
commit 151acf8eb3
12 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,39 @@
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./defined-in-js")]
extern {
fn name() -> String;
type MyClass;
#[wasm_bindgen(constructor)]
fn new() -> MyClass;
#[wasm_bindgen(method, getter)]
fn number(this: &MyClass) -> u32;
#[wasm_bindgen(method, setter)]
fn set_number(this: &MyClass, number: u32) -> MyClass;
#[wasm_bindgen(method)]
fn render(this: &MyClass) -> String;
}
// Import `console.log` so we can log something to the console
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub fn run() {
log(&format!("Hello, {}!", name()));
let x = MyClass::new();
assert_eq!(x.number(), 42);
x.set_number(10);
log(&x.render());
}