mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-26 19:45:54 +03:00
02b7021053
This commit leverages two new attributes in the Rust compiler, `#[wasm_custom_section]` and `#[wasm_import_module]`. These two attributes allow removing a lot of hacks found in wasm-bindgen and also allows removing the requirement of `wasm-opt` to remove the unused data sections. This does require two new nightly features but we already required the `proc_macro` nightly feature and these will hopefully be stabilized before that feature!
213 lines
5.6 KiB
Rust
213 lines
5.6 KiB
Rust
extern crate test_support;
|
|
|
|
#[test]
|
|
fn simple() {
|
|
test_support::project()
|
|
.file("src/lib.rs", r#"
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
extern {
|
|
fn foo(s: &JsValue);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub 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, wasm_custom_section, wasm_import_module)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
extern {
|
|
fn foo(s: JsValue);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub 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, wasm_custom_section, wasm_import_module)]
|
|
|
|
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]
|
|
pub 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, wasm_custom_section, wasm_import_module)]
|
|
|
|
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]
|
|
pub 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();
|
|
}
|
|
|
|
#[test]
|
|
fn returning_vector() {
|
|
test_support::project()
|
|
.file("src/lib.rs", r#"
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
extern {
|
|
fn foo() -> JsValue;
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn bar() -> Vec<JsValue> {
|
|
let mut res = Vec::new();
|
|
for _ in 0..10 {
|
|
res.push(foo())
|
|
}
|
|
res
|
|
}
|
|
"#)
|
|
.file("test.ts", r#"
|
|
import * as wasm from "./out";
|
|
import * as assert from "assert";
|
|
|
|
export function foo(): any { return { "foo": "bar" }; }
|
|
|
|
export function test() {
|
|
const result = wasm.bar();
|
|
assert.strictEqual(result.length, 10);
|
|
}
|
|
"#)
|
|
.test();
|
|
}
|