mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-28 12:32:37 +03:00
29771b574c
This commit migrates from `wasm_bindgen!`-the-macro to `#[wasm_bindgen]`-the-attribute. The actual mechanics of the macro are relatively simple in just generating some shims here and there, but wrapping everything in one huge macro invocation can often seem intimidating as it gives off this feeling of "oh dear anything can happen here!" Using an attribute should curb expectations much more greatly of "oh there's just some extra stuff happening behind the scenes". The usage is otherwise relatively straightforward and close to what it was before, but check out the DESIGN.md/README.md changes for more info!
160 lines
4.7 KiB
Rust
160 lines
4.7 KiB
Rust
extern crate test_support;
|
|
|
|
#[test]
|
|
fn works() {
|
|
test_support::project()
|
|
.file("src/lib.rs", r#"
|
|
#![feature(proc_macro)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn foo() -> JsValue {
|
|
JsValue::from("foo")
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn bar(s: &str) -> JsValue {
|
|
JsValue::from(s)
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn baz() -> JsValue {
|
|
JsValue::from(1.0)
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn baz2(a: &JsValue, b: &JsValue) {
|
|
assert_eq!(a.as_f64(), Some(2.0));
|
|
assert_eq!(b.as_f64(), None);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn js_null() -> JsValue {
|
|
JsValue::null()
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn js_undefined() -> JsValue {
|
|
JsValue::undefined()
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn test_is_null_undefined(
|
|
a: &JsValue,
|
|
b: &JsValue,
|
|
c: &JsValue,
|
|
) {
|
|
assert!(a.is_null());
|
|
assert!(!a.is_undefined());
|
|
|
|
assert!(!b.is_null());
|
|
assert!(b.is_undefined());
|
|
|
|
assert!(!c.is_null());
|
|
assert!(!c.is_undefined());
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn get_true() -> JsValue {
|
|
JsValue::from(true)
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn get_false() -> JsValue {
|
|
JsValue::from(false)
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn test_bool(
|
|
a: &JsValue,
|
|
b: &JsValue,
|
|
c: &JsValue,
|
|
) {
|
|
assert_eq!(a.as_bool(), Some(true));
|
|
assert_eq!(b.as_bool(), Some(false));
|
|
assert_eq!(c.as_bool(), None);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn mk_symbol() -> JsValue {
|
|
let a = JsValue::symbol(None);
|
|
assert!(a.is_symbol());
|
|
return a
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn mk_symbol2(s: &str) -> JsValue {
|
|
let a = JsValue::symbol(Some(s));
|
|
assert!(a.is_symbol());
|
|
return a
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn assert_symbols(a: &JsValue, b: &JsValue) {
|
|
assert!(a.is_symbol());
|
|
assert!(!b.is_symbol());
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn acquire_string(a: &JsValue, b: &JsValue) {
|
|
assert_eq!(a.as_string().unwrap(), "foo");
|
|
assert_eq!(b.as_string(), None);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn acquire_string2(a: &JsValue) -> String {
|
|
a.as_string().unwrap_or("wrong".to_string())
|
|
}
|
|
"#)
|
|
.file("test.ts", r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
assert.strictEqual(wasm.foo(), 'foo');
|
|
assert.strictEqual(wasm.bar('a'), 'a');
|
|
assert.strictEqual(wasm.baz(), 1);
|
|
wasm.baz2(2, 'a');
|
|
|
|
assert.strictEqual(wasm.js_null(), null);
|
|
assert.strictEqual(wasm.js_undefined(), undefined);
|
|
|
|
wasm.test_is_null_undefined(null, undefined, 1.0);
|
|
|
|
assert.strictEqual(wasm.get_true(), true);
|
|
assert.strictEqual(wasm.get_false(), false);
|
|
wasm.test_bool(true, false, 1.0);
|
|
|
|
assert.strictEqual(typeof(wasm.mk_symbol()), 'symbol');
|
|
assert.strictEqual(typeof(wasm.mk_symbol2('a')), 'symbol');
|
|
assert.strictEqual((Symbol as any).keyFor(wasm.mk_symbol()), undefined);
|
|
assert.strictEqual((Symbol as any).keyFor(wasm.mk_symbol2('b')), undefined);
|
|
|
|
wasm.assert_symbols((Symbol as any)(), 'a');
|
|
wasm.acquire_string('foo', null)
|
|
assert.strictEqual(wasm.acquire_string2(''), '');
|
|
assert.strictEqual(wasm.acquire_string2('a'), 'a');
|
|
}
|
|
"#)
|
|
.test();
|
|
}
|
|
|