mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-13 04:51:23 +00:00
Reorganize the import_js example
This commit is contained in:
11
examples/import_js/crate/Cargo.toml
Normal file
11
examples/import_js/crate/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "import_js"
|
||||
version = "0.1.0"
|
||||
authors = ["The wasm-bindgen Developers"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
wasm-bindgen = "0.2.40"
|
21
examples/import_js/crate/defined-in-js.js
Normal file
21
examples/import_js/crate/defined-in-js.js
Normal file
@ -0,0 +1,21 @@
|
||||
export function name() {
|
||||
return 'World';
|
||||
}
|
||||
|
||||
export class MyClass {
|
||||
constructor() {
|
||||
this._number = 42;
|
||||
}
|
||||
|
||||
get number() {
|
||||
return this._number;
|
||||
}
|
||||
|
||||
set number(n) {
|
||||
return this._number = n;
|
||||
}
|
||||
|
||||
render() {
|
||||
return `My number is: ${this.number}`;
|
||||
}
|
||||
}
|
35
examples/import_js/crate/src/lib.rs
Normal file
35
examples/import_js/crate/src/lib.rs
Normal file
@ -0,0 +1,35 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen(module = "/defined-in-js.js")]
|
||||
extern "C" {
|
||||
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;
|
||||
}
|
||||
|
||||
// lifted from the `console_log` example
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn log(s: &str);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn run() {
|
||||
log(&format!("Hello, {}!", name()));
|
||||
|
||||
let x = MyClass::new();
|
||||
assert_eq!(x.number(), 42);
|
||||
x.set_number(10);
|
||||
log(&x.render());
|
||||
}
|
Reference in New Issue
Block a user