mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-27 23:41:05 +00:00
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!
179 lines
4.6 KiB
Rust
179 lines
4.6 KiB
Rust
extern crate test_support;
|
|
|
|
#[test]
|
|
fn simple() {
|
|
test_support::project()
|
|
.file("src/lib.rs", r#"
|
|
#![feature(proc_macro)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
extern {
|
|
fn foo(s: &JsValue);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn bar(s: &JsValue) {
|
|
foo(s);
|
|
}
|
|
"#)
|
|
.file("test.ts", r#"
|
|
import * as wasm from "./out";
|
|
import * as assert from "assert";
|
|
|
|
let ARG: string | null = null;
|
|
|
|
export function foo(s: any): void {
|
|
assert.strictEqual(ARG, null);
|
|
ARG = s;
|
|
}
|
|
|
|
export function test() {
|
|
assert.strictEqual(ARG, null);
|
|
let sym = (Symbol as any)('test');
|
|
wasm.bar(sym);
|
|
assert.strictEqual(ARG, sym);
|
|
}
|
|
"#)
|
|
.test();
|
|
}
|
|
|
|
#[test]
|
|
fn owned() {
|
|
test_support::project()
|
|
.file("src/lib.rs", r#"
|
|
#![feature(proc_macro)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
extern {
|
|
fn foo(s: JsValue);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn bar(s: JsValue) {
|
|
foo(s);
|
|
}
|
|
"#)
|
|
.file("test.ts", r#"
|
|
import * as wasm from "./out";
|
|
import * as assert from "assert";
|
|
|
|
let ARG: any = null;
|
|
|
|
export function foo(s: any): void {
|
|
assert.strictEqual(ARG, null);
|
|
ARG = s;
|
|
}
|
|
|
|
export function test() {
|
|
assert.strictEqual(ARG, null);
|
|
let sym = (Symbol as any)('test');
|
|
wasm.bar(sym);
|
|
assert.strictEqual(ARG, sym);
|
|
}
|
|
"#)
|
|
.test();
|
|
}
|
|
|
|
#[test]
|
|
fn clone() {
|
|
test_support::project()
|
|
.file("src/lib.rs", r#"
|
|
#![feature(proc_macro)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
extern {
|
|
fn foo1(s: JsValue);
|
|
fn foo2(s: &JsValue);
|
|
fn foo3(s: JsValue);
|
|
fn foo4(s: &JsValue);
|
|
fn foo5(s: JsValue);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn bar(s: JsValue) {
|
|
foo1(s.clone());
|
|
foo2(&s);
|
|
foo3(s.clone());
|
|
foo4(&s);
|
|
foo5(s);
|
|
}
|
|
"#)
|
|
.file("test.ts", r#"
|
|
import * as wasm from "./out";
|
|
import * as assert from "assert";
|
|
|
|
let ARG = (Symbol as any)('test');
|
|
|
|
export function foo1(s: any): void { assert.strictEqual(s, ARG); }
|
|
export function foo2(s: any): void { assert.strictEqual(s, ARG); }
|
|
export function foo3(s: any): void { assert.strictEqual(s, ARG); }
|
|
export function foo4(s: any): void { assert.strictEqual(s, ARG); }
|
|
export function foo5(s: any): void { assert.strictEqual(s, ARG); }
|
|
|
|
export function test() {
|
|
wasm.bar(ARG);
|
|
}
|
|
"#)
|
|
.test();
|
|
}
|
|
|
|
#[test]
|
|
fn promote() {
|
|
test_support::project()
|
|
.file("src/lib.rs", r#"
|
|
#![feature(proc_macro)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
extern {
|
|
fn foo1(s: &JsValue);
|
|
fn foo2(s: JsValue);
|
|
fn foo3(s: &JsValue);
|
|
fn foo4(s: JsValue);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[no_mangle]
|
|
pub extern fn bar(s: &JsValue) {
|
|
foo1(s);
|
|
foo2(s.clone());
|
|
foo3(s);
|
|
foo4(s.clone());
|
|
}
|
|
"#)
|
|
.file("test.ts", r#"
|
|
import * as wasm from "./out";
|
|
import * as assert from "assert";
|
|
|
|
let ARG = (Symbol as any)('test');
|
|
|
|
export function foo1(s: any): void { assert.strictEqual(s, ARG); }
|
|
export function foo2(s: any): void { assert.strictEqual(s, ARG); }
|
|
export function foo3(s: any): void { assert.strictEqual(s, ARG); }
|
|
export function foo4(s: any): void { assert.strictEqual(s, ARG); }
|
|
|
|
export function test() {
|
|
wasm.bar(ARG);
|
|
}
|
|
"#)
|
|
.test();
|
|
}
|