mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-30 16:12:16 +00:00
This commit switches all imports of JS methods to `structural` by default. Proposed in [RFC 5] this should increase the performance of bindings today while also providing future-proofing for possible confusion with the recent addition of the `Deref` trait for all imported types by default as well. A new attribute, `host_binding`, is introduced in this PR as well to recover the old behavior of binding directly to an imported function which will one day be the precise function on the prototype. Eventually `web-sys` will switcsh over entirely to being driven via `host_binding` methods, but for now it's been measured to be not quite as fast so we're not making that switch yet. Note that `host_binding` differs from the proposed name of `final` due to the controversy, and its hoped that `host_binding` is a good middle-ground! [RFC 5]: https://rustwasm.github.io/rfcs/005-structural-and-deref.html
41 lines
987 B
Rust
41 lines
987 B
Rust
use wasm_bindgen::prelude::*;
|
|
use wasm_bindgen_test::*;
|
|
|
|
#[wasm_bindgen]
|
|
extern "C" {
|
|
type Math;
|
|
#[wasm_bindgen(static_method_of = Math, host_binding)]
|
|
fn log(f: f32) -> f32;
|
|
}
|
|
|
|
#[wasm_bindgen(module = "tests/wasm/host_binding.js")]
|
|
extern "C" {
|
|
type MyType;
|
|
#[wasm_bindgen(constructor, host_binding)]
|
|
fn new(x: u32) -> MyType;
|
|
#[wasm_bindgen(static_method_of = MyType, host_binding)]
|
|
fn foo(a: &str) -> String;
|
|
#[wasm_bindgen(method, host_binding)]
|
|
fn bar(this: &MyType, arg: bool) -> f32;
|
|
|
|
#[wasm_bindgen(method, getter, host_binding)]
|
|
fn a(this: &MyType) -> u32;
|
|
#[wasm_bindgen(method, setter, host_binding)]
|
|
fn set_a(this: &MyType, a: u32);
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn simple() {
|
|
assert_eq!(Math::log(1.0), 0.0);
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn classes() {
|
|
assert_eq!(MyType::foo("x"), "xy");
|
|
let x = MyType::new(2);
|
|
assert_eq!(x.bar(true), 3.2);
|
|
assert_eq!(x.a(), 1);
|
|
x.set_a(3);
|
|
assert_eq!(x.a(), 3);
|
|
}
|