Port tests that use only basic features

This commit is contained in:
Anton Danilkin
2018-08-05 05:00:16 +03:00
committed by Alex Crichton
parent 25a1bcb5be
commit 654bb9b683
41 changed files with 1938 additions and 2466 deletions

View File

@ -1,573 +0,0 @@
use super::project;
#[test]
fn works() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use std::cell::Cell;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn call(a: &Fn());
fn thread(a: &Fn(u32) -> u32) -> u32;
}
#[wasm_bindgen]
pub fn run() {
let a = Cell::new(false);
call(&|| a.set(true));
assert!(a.get());
assert_eq!(thread(&|a| a + 1), 3);
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function call(a) {
a();
}
export function thread(a) {
return a(2);
}
export function test() {
run();
}
"#,
)
.test();
}
#[test]
fn cannot_reuse() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn call(a: &Fn());
#[wasm_bindgen(catch)]
fn call_again() -> Result<(), JsValue>;
}
#[wasm_bindgen]
pub fn run() {
call(&|| {});
assert!(call_again().is_err());
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
let CACHE = null;
export function call(a) {
CACHE = a;
}
export function call_again() {
CACHE();
}
export function test() {
run();
}
"#,
)
.test();
}
#[test]
fn long_lived() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use std::cell::Cell;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn call1(a: &Closure<Fn()>);
fn call2(a: &Closure<FnMut(u32) -> u32>) -> u32;
}
#[wasm_bindgen]
pub fn run() {
let hit = Rc::new(Cell::new(false));
let hit2 = hit.clone();
let a = Closure::new(move || hit2.set(true));
assert!(!hit.get());
call1(&a);
assert!(hit.get());
let hit = Rc::new(Cell::new(false));
{
let hit = hit.clone();
let a = Closure::new(move |x| {
hit.set(true);
x + 3
});
assert_eq!(call2(&a), 5);
}
assert!(hit.get());
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function call1(a) {
a();
}
export function call2(a) {
return a(2);
}
export function test() {
run();
}
"#,
)
.test();
}
#[test]
fn many_arity() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn call1(a: &Closure<Fn()>);
fn call2(a: &Closure<Fn(u32)>);
fn call3(a: &Closure<Fn(u32, u32)>);
fn call4(a: &Closure<Fn(u32, u32, u32)>);
fn call5(a: &Closure<Fn(u32, u32, u32, u32)>);
fn call6(a: &Closure<Fn(u32, u32, u32, u32, u32)>);
fn call7(a: &Closure<Fn(u32, u32, u32, u32, u32, u32)>);
fn call8(a: &Closure<Fn(u32, u32, u32, u32, u32, u32, u32)>);
#[wasm_bindgen(js_name = call1)]
fn stack1(a: &Fn());
#[wasm_bindgen(js_name = call2)]
fn stack2(a: &Fn(u32));
#[wasm_bindgen(js_name = call3)]
fn stack3(a: &Fn(u32, u32));
#[wasm_bindgen(js_name = call4)]
fn stack4(a: &Fn(u32, u32, u32));
#[wasm_bindgen(js_name = call5)]
fn stack5(a: &Fn(u32, u32, u32, u32));
#[wasm_bindgen(js_name = call6)]
fn stack6(a: &Fn(u32, u32, u32, u32, u32));
#[wasm_bindgen(js_name = call7)]
fn stack7(a: &Fn(u32, u32, u32, u32, u32, u32));
#[wasm_bindgen(js_name = call8)]
fn stack8(a: &Fn(u32, u32, u32, u32, u32, u32, u32));
}
#[wasm_bindgen]
pub fn run() {
call1(&Closure::new(|| {}));
call2(&Closure::new(|a| assert_eq!(a, 1)));
call3(&Closure::new(|a, b| assert_eq!((a, b), (1, 2))));
call4(&Closure::new(|a, b, c| assert_eq!((a, b, c), (1, 2, 3))));
call5(&Closure::new(|a, b, c, d| {
assert_eq!((a, b, c, d), (1, 2, 3, 4))
}));
call6(&Closure::new(|a, b, c, d, e| {
assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5))
}));
call7(&Closure::new(|a, b, c, d, e, f| {
assert_eq!((a, b, c, d, e, f), (1, 2, 3, 4, 5, 6))
}));
call8(&Closure::new(|a, b, c, d, e, f, g| {
assert_eq!((a, b, c, d, e, f, g), (1, 2, 3, 4, 5, 6, 7))
}));
stack1(&(|| {}));
stack2(&(|a| assert_eq!(a, 1)));
stack3(&(|a, b| assert_eq!((a, b), (1, 2))));
stack4(&(|a, b, c| assert_eq!((a, b, c), (1, 2, 3))));
stack5(&(|a, b, c, d| {
assert_eq!((a, b, c, d), (1, 2, 3, 4))
}));
stack6(&(|a, b, c, d, e| {
assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5))
}));
stack7(&(|a, b, c, d, e, f| {
assert_eq!((a, b, c, d, e, f), (1, 2, 3, 4, 5, 6))
}));
stack8(&(|a, b, c, d, e, f, g| {
assert_eq!((a, b, c, d, e, f, g), (1, 2, 3, 4, 5, 6, 7))
}));
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function call1(a) { a() }
export function call2(a) { a(1) }
export function call3(a) { a(1, 2) }
export function call4(a) { a(1, 2, 3) }
export function call5(a) { a(1, 2, 3, 4) }
export function call6(a) { a(1, 2, 3, 4, 5) }
export function call7(a) { a(1, 2, 3, 4, 5, 6) }
export function call8(a) { a(1, 2, 3, 4, 5, 6, 7) }
export function test() {
run();
}
"#,
)
.test();
}
#[test]
fn long_lived_dropping() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use std::cell::Cell;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn cache(a: &Closure<Fn()>);
#[wasm_bindgen(catch)]
fn call() -> Result<(), JsValue>;
}
#[wasm_bindgen]
pub fn run() {
let hit = Rc::new(Cell::new(false));
let hit2 = hit.clone();
let a = Closure::new(move || hit2.set(true));
cache(&a);
assert!(!hit.get());
assert!(call().is_ok());
assert!(hit.get());
drop(a);
assert!(call().is_err());
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
let CACHE = null;
export function cache(a) { CACHE = a; }
export function call() { CACHE() }
export function test() {
run();
}
"#,
)
.test();
}
#[test]
fn long_fnmut_recursive() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn cache(a: &Closure<FnMut()>);
#[wasm_bindgen(catch)]
fn call() -> Result<(), JsValue>;
}
#[wasm_bindgen]
pub fn run() {
let a = Closure::new(|| {
assert!(call().is_err());
});
cache(&a);
assert!(call().is_ok());
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
let CACHE = null;
export function cache(a) { CACHE = a; }
export function call() { CACHE() }
export function test() {
run();
}
"#,
)
.test();
}
#[test]
fn fnmut() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn call(a: &mut FnMut());
fn thread(a: &mut FnMut(u32) -> u32) -> u32;
}
#[wasm_bindgen]
pub fn run() {
let mut a = false;
call(&mut || a = true);
assert!(a);
let mut x = false;
assert_eq!(thread(&mut |a| {
x = true;
a + 1
}), 3);
assert!(x);
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function call(a) {
a();
}
export function thread(a) {
return a(2);
}
export function test() {
run();
}
"#,
)
.test();
}
#[test]
fn fnmut_bad() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn call(a: &mut FnMut());
#[wasm_bindgen(catch)]
fn again(a: bool) -> Result<(), JsValue>;
}
#[wasm_bindgen]
pub fn run() {
let mut x = true;
let mut hits = 0;
call(&mut || {
hits += 1;
if again(hits == 1).is_err() {
return
}
x = false;
});
assert!(hits == 1);
assert!(x);
assert!(again(true).is_err());
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
let F = null;
export function call(a) {
F = a;
a();
}
export function again(x) {
if (x) F();
}
export function test() {
run();
}
"#,
)
.test();
}
#[test]
fn string_arguments() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn call(a: &mut FnMut(String));
}
#[wasm_bindgen]
pub fn run() {
let mut x = false;
call(&mut |s| {
assert_eq!(s, "foo");
x = true;
});
assert!(x);
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function call(a) {
a("foo")
}
export function test() {
run();
}
"#,
)
.test();
}
#[test]
fn string_ret() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn call(a: &mut FnMut(String) -> String);
}
#[wasm_bindgen]
pub fn run() {
let mut x = false;
call(&mut |mut s| {
assert_eq!(s, "foo");
s.push_str("bar");
x = true;
s
});
assert!(x);
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
import * as assert from "assert";
export function call(a) {
const s = a("foo");
assert.strictEqual(s, "foobar");
}
export function test() {
run();
}
"#,
)
.test();
}

View File

@ -1,78 +0,0 @@
use super::project;
#[test]
fn same_function_different_locations() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod a {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./foo")]
extern {
pub fn foo();
}
}
pub mod b {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./foo")]
extern {
pub fn foo();
}
}
#[wasm_bindgen]
pub fn test() {
a::foo();
b::foo();
}
"#,
)
.file("foo.js", "export function foo() {}")
.test();
}
#[test]
fn same_function_different_modules() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod a {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./foo")]
extern {
pub fn foo() -> bool;
}
}
pub mod b {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./bar")]
extern {
pub fn foo() -> bool;
}
}
#[wasm_bindgen]
pub fn test() {
assert!(a::foo());
assert!(!b::foo());
}
"#,
)
.file("foo.js", "export function foo() { return true; } ")
.file("bar.js", "export function foo() { return false; } ")
.test();
}

View File

@ -1,103 +0,0 @@
use super::project;
#[test]
fn c_style_enum() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub enum Color {
Green,
Yellow,
Red,
}
#[wasm_bindgen]
pub fn cycle(color: Color) -> Color {
match color {
Color::Green => Color::Yellow,
Color::Yellow => Color::Red,
Color::Red => Color::Green,
}
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
assert.strictEqual(wasm.Color.Green, 0);
assert.strictEqual(wasm.Color.Yellow, 1);
assert.strictEqual(wasm.Color.Red, 2);
assert.strictEqual(Object.keys(wasm.Color).length, 3);
assert.strictEqual(wasm.cycle(wasm.Color.Green), wasm.Color.Yellow);
}
"#,
)
.test();
}
#[test]
fn c_style_enum_with_custom_values() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod inner {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub enum Color {
Green = 21,
Yellow = 34,
Red,
}
}
use inner::Color;
#[wasm_bindgen]
pub fn cycle(color: Color) -> Color {
match color {
Color::Green => Color::Yellow,
Color::Yellow => Color::Red,
Color::Red => Color::Green,
}
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
assert.strictEqual(wasm.Color.Green, 21);
assert.strictEqual(wasm.Color.Yellow, 34);
assert.strictEqual(wasm.Color.Red, 2);
assert.strictEqual(Object.keys(wasm.Color).length, 3);
assert.strictEqual(wasm.cycle(wasm.Color.Green), wasm.Color.Yellow);
}
"#,
)
.test();
}

94
tests/all/js_objects.rs Normal file
View File

@ -0,0 +1,94 @@
use super::project;
#[test]
fn serde() {
project()
.serde(true)
.depend("serde = '1.0'")
.depend("serde_derive = '1.0'")
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
#[macro_use]
extern crate serde_derive;
use wasm_bindgen::prelude::*;
#[derive(Deserialize, Serialize)]
pub struct Foo {
a: u32,
b: String,
c: Option<Bar>,
d: Bar,
}
#[derive(Deserialize, Serialize)]
pub struct Bar {
a: u32,
}
#[wasm_bindgen(module = "./test")]
extern {
fn verify(a: JsValue) -> JsValue;
}
#[wasm_bindgen]
pub fn run() {
let js = JsValue::from_serde("foo").unwrap();
assert_eq!(js.as_string(), Some("foo".to_string()));
let ret = verify(JsValue::from_serde(&Foo {
a: 0,
b: "foo".to_string(),
c: None,
d: Bar { a: 1 },
}).unwrap());
let foo = ret.into_serde::<Foo>().unwrap();
assert_eq!(foo.a, 2);
assert_eq!(foo.b, "bar");
assert!(foo.c.is_some());
assert_eq!(foo.c.as_ref().unwrap().a, 3);
assert_eq!(foo.d.a, 4);
}
#[wasm_bindgen]
pub fn parse(j: &JsValue) {
let s = j.into_serde::<String>().unwrap();
assert_eq!(s, "bar");
}
"#,
)
.file(
"test.js",
r#"
import { run, parse } from "./out";
import * as assert from "assert";
export function verify(a) {
assert.deepStrictEqual(a, {
a: 0,
b: 'foo',
c: null,
d: { a: 1 }
});
return {
a: 2,
b: 'bar',
c: { a: 3 },
d: { a: 4 },
}
}
export function test() {
run();
parse('bar');
}
"#,
)
.test();
}

View File

@ -1,375 +0,0 @@
use super::project;
#[test]
fn simple() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
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.js",
r#"
import * as wasm from "./out";
import * as assert from "assert";
let ARG = null;
export function foo(s) {
assert.strictEqual(ARG, null);
ARG = s;
}
export function test() {
assert.strictEqual(ARG, null);
let sym = Symbol('test');
wasm.bar(sym);
assert.strictEqual(ARG, sym);
}
"#,
)
.test();
}
#[test]
fn owned() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
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.js",
r#"
import * as wasm from "./out";
import * as assert from "assert";
let ARG = null;
export function foo(s) {
assert.strictEqual(ARG, null);
ARG = s;
}
export function test() {
assert.strictEqual(ARG, null);
let sym = Symbol('test');
wasm.bar(sym);
assert.strictEqual(ARG, sym);
}
"#,
)
.test();
}
#[test]
fn clone() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
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.js",
r#"
import * as wasm from "./out";
import * as assert from "assert";
let ARG = Symbol('test');
export function foo1(s) { assert.strictEqual(s, ARG); }
export function foo2(s) { assert.strictEqual(s, ARG); }
export function foo3(s) { assert.strictEqual(s, ARG); }
export function foo4(s) { assert.strictEqual(s, ARG); }
export function foo5(s) { assert.strictEqual(s, ARG); }
export function test() {
wasm.bar(ARG);
}
"#,
)
.test();
}
#[test]
fn promote() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
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.js",
r#"
import * as wasm from "./out";
import * as assert from "assert";
let ARG = Symbol('test');
export function foo1(s) { assert.strictEqual(s, ARG); }
export function foo2(s) { assert.strictEqual(s, ARG); }
export function foo3(s) { assert.strictEqual(s, ARG); }
export function foo4(s) { assert.strictEqual(s, ARG); }
export function test() {
wasm.bar(ARG);
}
"#,
)
.test();
}
#[test]
fn returning_vector() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
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.js",
r#"
import * as wasm from "./out";
import * as assert from "assert";
export function foo() { return { "foo": "bar" }; }
export function test() {
const result = wasm.bar();
assert.strictEqual(result.length, 10);
}
"#,
)
.test();
}
#[test]
fn another_vector_return() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn get_array() -> Vec<JsValue> {
vec![
JsValue::from(1),
JsValue::from(2),
JsValue::from(3),
JsValue::from(4),
JsValue::from(5),
JsValue::from(6),
]
}
"#,
)
.file(
"test.js",
r#"
import { get_array } from "./out";
import * as assert from "assert";
export function test() {
assert.deepStrictEqual(get_array(), [1, 2, 3, 4, 5, 6]);
}
"#,
)
.test();
}
#[test]
fn serde() {
project()
.serde(true)
.depend("serde = '1.0'")
.depend("serde_derive = '1.0'")
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
#[macro_use]
extern crate serde_derive;
use wasm_bindgen::prelude::*;
#[derive(Deserialize, Serialize)]
pub struct Foo {
a: u32,
b: String,
c: Option<Bar>,
d: Bar,
}
#[derive(Deserialize, Serialize)]
pub struct Bar {
a: u32,
}
#[wasm_bindgen(module = "./test")]
extern {
fn verify(a: JsValue) -> JsValue;
}
#[wasm_bindgen]
pub fn run() {
let js = JsValue::from_serde("foo").unwrap();
assert_eq!(js.as_string(), Some("foo".to_string()));
let ret = verify(JsValue::from_serde(&Foo {
a: 0,
b: "foo".to_string(),
c: None,
d: Bar { a: 1 },
}).unwrap());
let foo = ret.into_serde::<Foo>().unwrap();
assert_eq!(foo.a, 2);
assert_eq!(foo.b, "bar");
assert!(foo.c.is_some());
assert_eq!(foo.c.as_ref().unwrap().a, 3);
assert_eq!(foo.d.a, 4);
}
#[wasm_bindgen]
pub fn parse(j: &JsValue) {
let s = j.into_serde::<String>().unwrap();
assert_eq!(s, "bar");
}
"#,
)
.file(
"test.js",
r#"
import { run, parse } from "./out";
import * as assert from "assert";
export function verify(a) {
assert.deepStrictEqual(a, {
a: 0,
b: 'foo',
c: null,
d: { a: 1 }
});
return {
a: 2,
b: 'bar',
c: { a: 3 },
d: { a: 4 },
}
}
export function test() {
run();
parse('bar');
}
"#,
)
.test();
}

View File

@ -4,21 +4,13 @@ extern crate wasm_bindgen_test_project_builder as project_builder;
use project_builder::{project, run};
mod closures;
mod comments;
mod dependencies;
mod duplicates;
mod enums;
mod import_class;
mod imports;
mod jsobjects;
mod math;
mod js_objects;
mod node;
mod non_debug;
mod non_wasm;
mod simple;
mod slice;
mod structural;
mod typescript;
mod u64;
mod validate_prt;

View File

@ -1,82 +0,0 @@
use super::project;
#[test]
fn auto_bind_math() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn math(a: f32, b: f64) -> f64 {
b.acos() +
b.asin() +
b.atan() +
b.atan2(b) +
b.cbrt() +
b.cosh() +
b.exp_m1() +
b.ln_1p() +
b.sinh() +
b.tan() +
b.tanh() +
b.hypot(b) +
b.cos() +
b.exp() +
b.exp2() +
b.mul_add(b, b) +
b.ln() +
b.log(b) +
b.log10() +
b.log2() +
b.powi(8) +
b.powf(b) +
b.round() +
b.sin() +
b.abs() +
b.signum() +
b.floor() +
b.ceil() +
b.trunc() +
b.sqrt() +
(b % (a as f64)) +
((a.cos() +
a.exp() +
a.exp2() +
a.mul_add(a, a) +
a.ln() +
a.log(a) +
a.log10() +
a.log2() +
a.powi(8) +
a.powf(a) +
a.round() +
a.sin() +
a.abs() +
a.signum() +
a.floor() +
a.ceil() +
a.trunc() +
a.sqrt() +
(a % (b as f32))) as f64) +
(b + 2.0f64.powf(a as f64))
}
"#,
)
.file(
"test.js",
r#"
import { math } from "./out";
export function test() {
math(1.0, 2.0);
}
"#,
)
.test();
}

View File

@ -1,526 +0,0 @@
use super::project;
#[test]
fn export() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
macro_rules! doit {
($($i:ident)*) => ($(
#[wasm_bindgen]
pub fn $i(a: &[$i]) -> Vec<$i> {
assert_eq!(a.len(), 2);
assert_eq!(a[0], 1 as $i);
assert_eq!(a[1], 2 as $i);
a.to_vec()
}
)*)
}
doit! { i8 u8 i16 u16 i32 u32 f32 f64 }
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
function assert_arrays_equal(a, b) {
console.log(a, b);
assert.strictEqual(a.length, b.length);
assert.strictEqual(a.byteLength, b.byteLength);
for (let i = 0; i < a.length; i++) {
assert.strictEqual(a[i], b[i]);
}
}
export function test() {
const i8 = new Int8Array(2);
i8[0] = 1;
i8[1] = 2;
assert_arrays_equal(wasm.i8(i8), i8);
const u8 = new Uint8Array(2);
u8[0] = 1;
u8[1] = 2;
assert_arrays_equal(wasm.u8(u8), u8);
const i16 = new Int16Array(2);
i16[0] = 1;
i16[1] = 2;
assert_arrays_equal(wasm.i16(i16), i16);
const u16 = new Uint16Array(2);
u16[0] = 1;
u16[1] = 2;
assert_arrays_equal(wasm.u16(u16), u16);
const i32 = new Int32Array(2);
i32[0] = 1;
i32[1] = 2;
wasm.i32(i32);
assert_arrays_equal(wasm.i32(i32), i32);
const u32 = new Uint32Array(2);
u32[0] = 1;
u32[1] = 2;
assert_arrays_equal(wasm.u32(u32), u32);
const f32 = new Float32Array(2);
f32[0] = 1;
f32[1] = 2;
assert_arrays_equal(wasm.f32(f32), f32);
const f64 = new Float64Array(2);
f64[0] = 1;
f64[1] = 2;
assert_arrays_equal(wasm.f64(f64), f64);
}
"#,
)
.test();
}
#[test]
fn import() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
macro_rules! doit {
($(($rust:ident, $js:ident, $i:ident))*) => ($(
#[wasm_bindgen(module = "./test")]
extern {
fn $js(a: &[$i]) -> Vec<$i>;
}
#[wasm_bindgen]
pub fn $rust(a: &[$i]) -> Vec<$i> {
assert_eq!(a.len(), 2);
assert_eq!(a[0], 1 as $i);
assert_eq!(a[1], 2 as $i);
$js(a)
}
)*)
}
doit! {
(rust_i8, js_i8, i8)
(rust_u8, js_u8, u8)
(rust_i16, js_i16, i16)
(rust_u16, js_u16, u16)
(rust_i32, js_i32, i32)
(rust_u32, js_u32, u32)
(rust_f32, js_f32, f32)
(rust_f64, js_f64, f64)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function js_i8(a) {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
}
export function js_u8(a) {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
}
export function js_i16(a) {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
}
export function js_u16(a) {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
}
export function js_i32(a) {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
}
export function js_u32(a) {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
}
export function js_f32(a) {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
}
export function js_f64(a) {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
}
function assert_arrays_equal(a, b) {
console.log(a, b);
assert.strictEqual(a.length, b.length);
assert.strictEqual(a.byteLength, b.byteLength);
for (let i = 0; i < a.length; i++) {
assert.strictEqual(a[i], b[i]);
}
}
export function test() {
const i8 = new Int8Array(2);
i8[0] = 1;
i8[1] = 2;
assert_arrays_equal(wasm.rust_i8(i8), i8);
const u8 = new Uint8Array(2);
u8[0] = 1;
u8[1] = 2;
assert_arrays_equal(wasm.rust_u8(u8), u8);
const i16 = new Int16Array(2);
i16[0] = 1;
i16[1] = 2;
assert_arrays_equal(wasm.rust_i16(i16), i16);
const u16 = new Uint16Array(2);
u16[0] = 1;
u16[1] = 2;
assert_arrays_equal(wasm.rust_u16(u16), u16);
const i32 = new Int32Array(2);
i32[0] = 1;
i32[1] = 2;
assert_arrays_equal(wasm.rust_i32(i32), i32);
const u32 = new Uint32Array(2);
u32[0] = 1;
u32[1] = 2;
assert_arrays_equal(wasm.rust_u32(u32), u32);
const f32 = new Float32Array(2);
f32[0] = 1;
f32[1] = 2;
assert_arrays_equal(wasm.rust_f32(f32), f32);
const f64 = new Float64Array(2);
f64[0] = 1;
f64[1] = 2;
assert_arrays_equal(wasm.rust_f64(f64), f64);
}
"#,
)
.test();
}
#[test]
fn pass_array_works() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
macro_rules! doit {
($(($rust:ident, $i:ident))*) => ($(
#[wasm_bindgen]
pub fn $rust(a: &[$i]) {
assert_eq!(a.len(), 2);
assert_eq!(a[0], 1 as $i);
assert_eq!(a[1], 2 as $i);
}
)*)
}
doit! {
(rust_i8, i8)
(rust_u8, u8)
(rust_i16, i16)
(rust_u16, u16)
(rust_i32, i32)
(rust_u32, u32)
(rust_f32, f32)
(rust_f64, f64)
}
"#,
)
.file(
"test.js",
r#"
import * as wasm from "./out";
export function test() {
wasm.rust_i8([1, 2]);
wasm.rust_u8([1, 2]);
wasm.rust_i16([1, 2]);
wasm.rust_u16([1, 2]);
wasm.rust_i32([1, 2]);
wasm.rust_u32([1, 2]);
wasm.rust_f32([1, 2]);
wasm.rust_f64([1, 2]);
}
"#,
)
.test();
}
#[test]
fn import_mut() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
macro_rules! doit {
($(($rust:ident, $js:ident, $i:ident))*) => (
$(
#[wasm_bindgen(module = "./test")]
extern {
fn $js(a: &mut [$i]);
}
fn $rust() {
let mut buf = [
1 as $i,
2 as $i,
3 as $i,
];
$js(&mut buf);
assert_eq!(buf[0], 4 as $i);
assert_eq!(buf[1], 5 as $i);
assert_eq!(buf[2], 3 as $i);
}
)*
#[wasm_bindgen]
pub fn run() {
$($rust();)*
}
)
}
doit! {
(rust_i8, js_i8, i8)
(rust_u8, js_u8, u8)
(rust_i16, js_i16, i16)
(rust_u16, js_u16, u16)
(rust_i32, js_i32, i32)
(rust_u32, js_u32, u32)
(rust_f32, js_f32, f32)
(rust_f64, js_f64, f64)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
function foo(a) {
assert.strictEqual(a.length, 3);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
a[0] = 4;
a[1] = 5;
}
export const js_i8 = foo;
export const js_u8 = foo;
export const js_i16 = foo;
export const js_u16 = foo;
export const js_i32 = foo;
export const js_u32 = foo;
export const js_f32 = foo;
export const js_f64 = foo;
export function test() {
wasm.run();
}
"#,
)
.test();
}
#[test]
fn export_mut() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
macro_rules! doit {
($($i:ident)*) => ($(
#[wasm_bindgen]
pub fn $i(a: &mut [$i]) {
assert_eq!(a.len(), 3);
assert_eq!(a[0], 1 as $i);
assert_eq!(a[1], 2 as $i);
assert_eq!(a[2], 3 as $i);
a[0] = 4 as $i;
a[1] = 5 as $i;
}
)*)
}
doit! { i8 u8 i16 u16 i32 u32 f32 f64 }
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
function run(a, rust) {
assert.strictEqual(a.length, 3);
a[0] = 1;
a[1] = 2;
a[2] = 3;
console.log(a);
rust(a);
console.log(a);
assert.strictEqual(a.length, 3);
assert.strictEqual(a[0], 4);
assert.strictEqual(a[1], 5);
assert.strictEqual(a[2], 3);
}
export function test() {
run(new Int8Array(3), wasm.i8);
run(new Uint8Array(3), wasm.u8);
run(new Int16Array(3), wasm.i16);
run(new Uint16Array(3), wasm.u16);
run(new Int32Array(3), wasm.i32);
run(new Uint32Array(3), wasm.u32);
run(new Float32Array(3), wasm.f32);
run(new Float64Array(3), wasm.f64);
}
"#,
)
.test();
}
#[test]
fn return_vec_ok() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn broken_vec() -> Vec<u32> {
vec![1, 2, 3, 4, 5, 6, 7, 8, 9]
}
#[wasm_bindgen]
pub fn web_main() -> Application {
Application::new()
}
#[wasm_bindgen]
pub struct Application {
thing: Vec<u32>,
}
#[wasm_bindgen]
impl Application {
pub fn new() -> Application {
let mut thing = vec![];
thing.push(0);
thing.push(0);
thing.push(0);
thing.push(0);
thing.push(0);
Application {
thing: thing
}
}
pub fn tick(&mut self) {
self.thing = self.thing.clone();
}
}
pub fn main() {
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let app = wasm.web_main();
for (let i = 0; i < 10; i++) {
app.tick();
let bad = wasm.broken_vec();
console.log("Received from rust:", i, bad);
assert.strictEqual(bad[0], 1);
assert.strictEqual(bad[1], 2);
assert.strictEqual(bad[2], 3);
assert.strictEqual(bad[3], 4);
assert.strictEqual(bad[4], 5);
assert.strictEqual(bad[5], 6);
assert.strictEqual(bad[6], 7);
assert.strictEqual(bad[7], 8);
assert.strictEqual(bad[8], 9);
}
}
"#,
)
.test();
}

View File

@ -1,52 +0,0 @@
use super::project;
#[test]
fn works() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
pub type Foo;
#[wasm_bindgen(method, structural)]
fn bar(this: &Foo);
#[wasm_bindgen(method, getter, structural)]
fn baz(this: &Foo) -> u32;
#[wasm_bindgen(method, setter, structural)]
fn set_baz(this: &Foo, val: u32);
}
#[wasm_bindgen]
pub fn run(a: &Foo) {
a.bar();
assert_eq!(a.baz(), 1);
a.set_baz(2);
assert_eq!(a.baz(), 2);
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import { run } from "./out";
export function test() {
let called = false;
run({
bar() { called = true; },
baz: 1,
});
assert.strictEqual(called, true);
}
"#,
)
.test();
}

View File

@ -1,106 +0,0 @@
use super::project;
#[test]
fn works() {
project()
.requires_bigint()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn js_i64_round(a: i64) -> i64;
fn js_u64_round(a: u64) -> u64;
}
#[wasm_bindgen]
pub fn zero() -> u64 { 0 }
#[wasm_bindgen]
pub fn one() -> u64 { 1 }
#[wasm_bindgen]
pub fn neg_one() -> i64 { -1 }
#[wasm_bindgen]
pub fn u32_max() -> u64 { u32::max_value() as u64 }
#[wasm_bindgen]
pub fn i32_min() -> i64 { i32::min_value() as i64 }
#[wasm_bindgen]
pub fn u64_max() -> u64 { u64::max_value() }
#[wasm_bindgen]
pub fn i64_min() -> i64 { i64::min_value() }
#[wasm_bindgen]
pub fn i64_round(a: i64) -> i64 { js_i64_round(a) }
#[wasm_bindgen]
pub fn u64_round(a: u64) -> u64 { js_u64_round(a) }
#[wasm_bindgen]
pub fn i64_slice(a: &[i64]) -> Vec<i64> { a.to_vec() }
#[wasm_bindgen]
pub fn u64_slice(a: &[u64]) -> Vec<u64> { a.to_vec() }
"#,
)
.file(
"test.js",
r#"
import * as wasm from './out';
function assertEq(a, b) {
console.log(a, '?=', b);
if (a === b)
return;
throw new Error('not equal');
}
function assertArrayEq(a, b) {
console.log(a, '?=', b);
if (a.length !== b.length)
throw new Error('not equal');
for (let i = 0; i < a.length; i++)
assertEq(a[i], b[i]);
}
export function test() {
assertEq(wasm.zero(), BigInt(0));
assertEq(wasm.one(), BigInt(1));
assertEq(wasm.neg_one(), BigInt(-1));
assertEq(wasm.u32_max(), BigInt(4294967295));
assertEq(wasm.i32_min(), BigInt(-2147483648));
assertEq(wasm.u64_max(), BigInt('18446744073709551615'));
assertEq(wasm.i64_min(), BigInt('-9223372036854775808'));
assertEq(wasm.i64_round(BigInt(0)), BigInt(0));
assertEq(wasm.i64_round(BigInt(1)), BigInt(1));
assertEq(wasm.i64_round(BigInt(-1)), BigInt(-1));
assertEq(wasm.u64_round(BigInt(0)), BigInt(0));
assertEq(wasm.u64_round(BigInt(1)), BigInt(1));
assertEq(wasm.u64_round(BigInt(1) << BigInt(64)), BigInt(0));
const u64_max = BigInt('18446744073709551615');
const i64_min = BigInt('-9223372036854775808');
assertEq(wasm.i64_round(i64_min), i64_min);
assertEq(wasm.u64_round(u64_max), u64_max);
assertArrayEq(wasm.u64_slice([]), new BigUint64Array());
assertArrayEq(wasm.i64_slice([]), new BigInt64Array());
const arr1 = new BigUint64Array([BigInt(1), BigInt(2)]);
assertArrayEq(wasm.u64_slice([BigInt(1), BigInt(2)]), arr1);
const arr2 = new BigInt64Array([BigInt(1), BigInt(2)]);
assertArrayEq(wasm.i64_slice([BigInt(1), BigInt(2)]), arr2);
assertArrayEq(wasm.i64_slice([i64_min]), new BigInt64Array([i64_min]));
assertArrayEq(wasm.u64_slice([u64_max]), new BigUint64Array([u64_max]));
}
export function js_i64_round(a) { return a; }
export function js_u64_round(a) { return a; }
"#,
)
.test();
}

View File

@ -1,75 +0,0 @@
use super::project;
#[test]
fn works() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct Fruit {
name: String,
}
#[wasm_bindgen]
impl Fruit {
#[wasm_bindgen(method)]
pub fn name(&self) -> String {
self.name.clone()
}
#[wasm_bindgen(constructor)]
pub fn new(name: String) -> Self {
Fruit {
name,
}
}
}
#[wasm_bindgen]
pub fn eat(_fruit: Fruit) { }
"#,
)
.file(
"test.js",
r#"
import * as wasm from './out';
const targetMessage = 'Attempt to use a moved value';
function assertEq(a, b) {
console.log(a, '?=', b);
if (a === b)
return;
throw new Error('not equal');
}
export function test() {
useMoved();
moveMoved();
}
export function useMoved() {
// create a new struct
let apple = new wasm.Fruit('apple');
// sanity check that this method works
let name = apple.name();
// consume the struct
wasm.eat(apple);
// try and use the moved apple again
try {
let movedName = apple.name();
} catch (e) {
assertEq(e.message, targetMessage);
}
}
export function moveMoved() {
let pear = new wasm.Fruit('pear');
let name = pear.name();
wasm.eat(pear);
try {
wasm.eat(pear);
} catch (e) {
assertEq(e.message, targetMessage);
}
}
"#,
)
.test();
}

View File

@ -1,43 +1,43 @@
const assert = require('assert');
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
exports.test_works = function() {
assert.strictEqual(wasm.api_foo(), 'foo');
assert.strictEqual(wasm.api_bar('a'), 'a');
assert.strictEqual(wasm.api_baz(), 1);
wasm.api_baz2(2, 'a');
assert.strictEqual(wasm.api_js_null(), null);
assert.strictEqual(wasm.api_js_undefined(), undefined);
wasm.api_test_is_null_undefined(null, undefined, 1.0);
assert.strictEqual(wasm.api_get_true(), true);
assert.strictEqual(wasm.api_get_false(), false);
wasm.api_test_bool(true, false, 1.0);
assert.strictEqual(typeof(wasm.api_mk_symbol()), 'symbol');
assert.strictEqual(typeof(wasm.api_mk_symbol2('a')), 'symbol');
assert.strictEqual(Symbol.keyFor(wasm.api_mk_symbol()), undefined);
assert.strictEqual(Symbol.keyFor(wasm.api_mk_symbol2('b')), undefined);
wasm.api_assert_symbols(Symbol(), 'a');
wasm.api_acquire_string('foo', null);
assert.strictEqual(wasm.api_acquire_string2(''), '');
assert.strictEqual(wasm.api_acquire_string2('a'), 'a');
exports.assert_null = x => {
assert.strictEqual(x, null);
};
exports.test_eq_works = function() {
assert.strictEqual(wasm.eq_test('a', 'a'), true);
assert.strictEqual(wasm.eq_test('a', 'b'), false);
assert.strictEqual(wasm.eq_test(NaN, NaN), false);
assert.strictEqual(wasm.eq_test({a: 'a'}, {a: 'a'}), false);
assert.strictEqual(wasm.eq_test1(NaN), false);
let x = {a: 'a'};
assert.strictEqual(wasm.eq_test(x, x), true);
assert.strictEqual(wasm.eq_test1(x), true);
exports.js_works = () => {
assert.strictEqual(wasm.api_foo(), 'foo');
assert.strictEqual(wasm.api_bar('a'), 'a');
assert.strictEqual(wasm.api_baz(), 1);
wasm.api_baz2(2, 'a');
assert.strictEqual(wasm.api_js_null(), null);
assert.strictEqual(wasm.api_js_undefined(), undefined);
wasm.api_test_is_null_undefined(null, undefined, 1.0);
assert.strictEqual(wasm.api_get_true(), true);
assert.strictEqual(wasm.api_get_false(), false);
wasm.api_test_bool(true, false, 1.0);
assert.strictEqual(typeof(wasm.api_mk_symbol()), 'symbol');
assert.strictEqual(typeof(wasm.api_mk_symbol2('a')), 'symbol');
assert.strictEqual(Symbol.keyFor(wasm.api_mk_symbol()), undefined);
assert.strictEqual(Symbol.keyFor(wasm.api_mk_symbol2('b')), undefined);
wasm.api_assert_symbols(Symbol(), 'a');
wasm.api_acquire_string('foo', null);
assert.strictEqual(wasm.api_acquire_string2(''), '');
assert.strictEqual(wasm.api_acquire_string2('a'), 'a');
};
exports.assert_null = function(x) {
assert.strictEqual(x, null);
exports.js_eq_works = () => {
assert.strictEqual(wasm.eq_test('a', 'a'), true);
assert.strictEqual(wasm.eq_test('a', 'b'), false);
assert.strictEqual(wasm.eq_test(NaN, NaN), false);
assert.strictEqual(wasm.eq_test({a: 'a'}, {a: 'a'}), false);
assert.strictEqual(wasm.eq_test1(NaN), false);
let x = {a: 'a'};
assert.strictEqual(wasm.eq_test(x, x), true);
assert.strictEqual(wasm.eq_test1(x), true);
};

View File

@ -3,14 +3,14 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/api.js", version = "*")]
extern {
fn test_works();
fn test_eq_works();
fn js_works();
fn js_eq_works();
fn assert_null(v: JsValue);
}
#[wasm_bindgen_test]
fn works() {
test_works();
js_works();
}
#[wasm_bindgen]
@ -87,14 +87,14 @@ pub fn api_mk_symbol() -> JsValue {
let a = JsValue::symbol(None);
assert!(a.is_symbol());
assert_eq!(format!("{:?}", a), "Symbol(..)");
return a
return a;
}
#[wasm_bindgen]
pub fn api_mk_symbol2(s: &str) -> JsValue {
let a = JsValue::symbol(Some(s));
assert!(a.is_symbol());
return a
return a;
}
#[wasm_bindgen]
@ -117,7 +117,7 @@ pub fn api_acquire_string2(a: &JsValue) -> String {
#[wasm_bindgen_test]
fn eq_works() {
test_eq_works();
js_eq_works();
}
#[wasm_bindgen]

View File

@ -1,23 +1,17 @@
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
function assertEq(a, b) {
console.log(a, '?=', b);
if (a === b)
return;
throw new Error('not equal');
}
exports.js_identity = a => a;
exports.char_works = function() {
assertEq(wasm.char_single_char(), 'a');
assertEq(wasm.char_wide_char(), '💩');
assertEq(wasm.char_parrot('Ղ'), 'Ղ');
assertEq(wasm.char_parrot('ҝ'), 'ҝ');
assertEq(wasm.char_parrot('Δ'), 'Δ');
assertEq(wasm.char_parrot('䉨'), '䉨');
assertEq(wasm.char_round('a'), 'a');
assertEq(wasm.char_round('㊻'), '㊻');
wasm.char_short_test('a');
wasm.char_wide_test('💩');
exports.js_works = () => {
assert.strictEqual(wasm.letter(), 'a');
assert.strictEqual(wasm.face(), '😀');
assert.strictEqual(wasm.rust_identity('Ղ'), 'Ղ');
assert.strictEqual(wasm.rust_identity('ҝ'), 'ҝ');
assert.strictEqual(wasm.rust_identity('Δ'), 'Δ');
assert.strictEqual(wasm.rust_identity('䉨'), '䉨');
assert.strictEqual(wasm.rust_js_identity('a'), 'a');
assert.strictEqual(wasm.rust_js_identity('㊻'), '㊻');
wasm.rust_letter('a');
wasm.rust_face('😀');
};
exports.js_parrot = function(a) { return a; };

View File

@ -3,27 +3,29 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/char.js", version = "*")]
extern {
fn char_works();
fn js_parrot(c: char) -> char;
fn js_identity(c: char) -> char;
fn js_works();
}
#[wasm_bindgen]
pub fn rust_identity(c: char) -> char { c }
#[wasm_bindgen]
pub fn rust_js_identity(c: char) -> char { js_identity(c) }
#[wasm_bindgen]
pub fn letter() -> char { 'a' }
#[wasm_bindgen]
pub fn face() -> char { '😀' }
#[wasm_bindgen]
pub fn rust_letter(a: char) { assert_eq!(a, 'a'); }
#[wasm_bindgen]
pub fn rust_face(p: char) { assert_eq!(p, '😀'); }
#[wasm_bindgen_test]
fn works() {
char_works();
js_works();
}
#[wasm_bindgen]
pub fn char_single_char() -> char { 'a' }
#[wasm_bindgen]
pub fn char_wide_char() -> char { '💩' }
#[wasm_bindgen]
pub fn char_parrot(c: char) -> char { c }
#[wasm_bindgen]
pub fn char_short_test(a: char) { assert_eq!(a, 'a'); }
#[wasm_bindgen]
pub fn char_wide_test(p: char) { assert_eq!(p, '💩'); }
#[wasm_bindgen]
pub fn char_round(c: char)-> char { js_parrot(c) }

View File

@ -1,128 +1,128 @@
const assert = require('assert');
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
exports.test_simple = function() {
const r = wasm.ClassesSimple.new();
assert.strictEqual(r.add(0), 0);
assert.strictEqual(r.add(1), 1);
assert.strictEqual(r.add(1), 2);
r.add(2);
assert.strictEqual(r.consume(), 4);
assert.throws(() => r.free(), /null pointer passed to rust/);
exports.js_simple = () => {
const r = wasm.ClassesSimple.new();
assert.strictEqual(r.add(0), 0);
assert.strictEqual(r.add(1), 1);
assert.strictEqual(r.add(1), 2);
r.add(2);
assert.strictEqual(r.consume(), 4);
assert.throws(() => r.free(), /null pointer passed to rust/);
const r2 = wasm.ClassesSimple.with_contents(10);
assert.strictEqual(r2.add(1), 11);
assert.strictEqual(r2.add(2), 13);
assert.strictEqual(r2.add(3), 16);
r2.free();
const r2 = wasm.ClassesSimple.with_contents(10);
assert.strictEqual(r2.add(1), 11);
assert.strictEqual(r2.add(2), 13);
assert.strictEqual(r2.add(3), 16);
r2.free();
const r3 = new wasm.ClassesSimple();
assert.strictEqual(r3.add(42), 42);
r3.free();
const r3 = new wasm.ClassesSimple();
assert.strictEqual(r3.add(42), 42);
r3.free();
};
exports.test_strings = function() {
const r = wasm.ClassesStrings1.new();
r.set(3);
let bar = r.bar('baz');
r.free();
assert.strictEqual(bar.name(), "foo-baz-3");
bar.free();
exports.js_strings = () => {
const r = wasm.ClassesStrings1.new();
r.set(3);
let bar = r.bar('baz');
r.free();
assert.strictEqual(bar.name(), 'foo-baz-3');
bar.free();
};
exports.test_exceptions = function() {
assert.throws(() => new wasm.ClassesExceptions1(), /cannot invoke `new` directly/);
let a = wasm.ClassesExceptions1.new();
a.free();
assert.throws(() => a.free(), /null pointer passed to rust/);
exports.js_exceptions = () => {
assert.throws(() => new wasm.ClassesExceptions1(), /cannot invoke `new` directly/);
let a = wasm.ClassesExceptions1.new();
a.free();
assert.throws(() => a.free(), /null pointer passed to rust/);
let b = wasm.ClassesExceptions1.new();
b.foo(b);
assert.throws(() => b.bar(b), /recursive use of an object/);
let b = wasm.ClassesExceptions1.new();
b.foo(b);
assert.throws(() => b.bar(b), /recursive use of an object/);
let c = wasm.ClassesExceptions1.new();
let d = wasm.ClassesExceptions2.new();
assert.throws(() => c.foo(d), /expected instance of ClassesExceptions1/);
d.free();
c.free();
let c = wasm.ClassesExceptions1.new();
let d = wasm.ClassesExceptions2.new();
assert.throws(() => c.foo(d), /expected instance of ClassesExceptions1/);
d.free();
c.free();
};
exports.test_pass_one_to_another = function() {
let a = wasm.ClassesPassA.new();
let b = wasm.ClassesPassB.new();
a.foo(b);
a.bar(b);
a.free();
exports.js_pass_one_to_another = () => {
let a = wasm.ClassesPassA.new();
let b = wasm.ClassesPassB.new();
a.foo(b);
a.bar(b);
a.free();
};
exports.take_class = function(foo) {
assert.strictEqual(foo.inner(), 13);
foo.free();
assert.throws(() => foo.free(), /null pointer passed to rust/);
exports.take_class = foo => {
assert.strictEqual(foo.inner(), 13);
foo.free();
assert.throws(() => foo.free(), /null pointer passed to rust/);
};
exports.test_constructors = function() {
const foo = new wasm.ConstructorsFoo(1);
assert.strictEqual(foo.get_number(), 1);
foo.free();
exports.js_constructors = () => {
const foo = new wasm.ConstructorsFoo(1);
assert.strictEqual(foo.get_number(), 1);
foo.free();
const foo2 = wasm.ConstructorsFoo.new(2);
assert.strictEqual(foo2.get_number(), 2);
foo2.free();
const foo2 = wasm.ConstructorsFoo.new(2);
assert.strictEqual(foo2.get_number(), 2);
foo2.free();
const bar = new wasm.ConstructorsBar(3, 4);
assert.strictEqual(bar.get_sum(), 7);
bar.free();
const bar = new wasm.ConstructorsBar(3, 4);
assert.strictEqual(bar.get_sum(), 7);
bar.free();
const bar2 = wasm.ConstructorsBar.other_name(5, 6);
assert.strictEqual(bar2.get_sum(), 11);
bar2.free();
const bar2 = wasm.ConstructorsBar.other_name(5, 6);
assert.strictEqual(bar2.get_sum(), 11);
bar2.free();
assert.strictEqual(wasm.cross_item_construction().get_sum(), 15);
assert.strictEqual(wasm.cross_item_construction().get_sum(), 15);
};
exports.test_empty_structs = function() {
wasm.OtherEmpty.return_a_value();
exports.js_empty_structs = () => {
wasm.OtherEmpty.return_a_value();
};
exports.test_public_fields = function() {
const a = wasm.PublicFields.new();
assert.strictEqual(a.a, 0);
a.a = 3;
assert.strictEqual(a.a, 3);
exports.js_public_fields = () => {
const a = wasm.PublicFields.new();
assert.strictEqual(a.a, 0);
a.a = 3;
assert.strictEqual(a.a, 3);
assert.strictEqual(a.b, 0);
a.b = 7;
assert.strictEqual(a.b, 7);
assert.strictEqual(a.b, 0);
a.b = 7;
assert.strictEqual(a.b, 7);
assert.strictEqual(a.c, 0);
a.c = 8;
assert.strictEqual(a.c, 8);
assert.strictEqual(a.c, 0);
a.c = 8;
assert.strictEqual(a.c, 8);
assert.strictEqual(a.d, 0);
a.d = 3.3;
assert.strictEqual(a.d, 3);
assert.strictEqual(a.d, 0);
a.d = 3.3;
assert.strictEqual(a.d, 3);
};
exports.test_using_self = function() {
wasm.UseSelf.new().free();
exports.js_using_self = () => {
wasm.UseSelf.new().free();
};
exports.test_readonly_fields = function() {
const a = wasm.Readonly.new();
assert.strictEqual(a.a, 0);
a.a = 3;
assert.strictEqual(a.a, 0);
a.free();
exports.js_readonly_fields = () => {
const a = wasm.Readonly.new();
assert.strictEqual(a.a, 0);
a.a = 3;
assert.strictEqual(a.a, 0);
a.free();
};
exports.test_double_consume = function() {
const r = wasm.DoubleConsume.new();
assert.throws(() => r.consume(r), /Attempt to use a moved value/);
}
exports.test_js_rename = function() {
wasm.JsRename.new().bar();
wasm.classes_foo();
exports.js_double_consume = () => {
const r = wasm.DoubleConsume.new();
assert.throws(() => r.consume(r), /Attempt to use a moved value/);
};
exports.js_js_rename = () => {
wasm.JsRename.new().bar();
wasm.classes_foo();
};

View File

@ -3,25 +3,25 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/classes.js", version = "*")]
extern {
fn test_simple();
fn test_strings();
fn test_exceptions();
fn test_pass_one_to_another();
fn js_simple();
fn js_strings();
fn js_exceptions();
fn js_pass_one_to_another();
fn take_class(foo: ClassesIntoJs);
#[wasm_bindgen(js_name = take_class)]
fn take_class_as_jsvalue(foo: JsValue);
fn test_constructors();
fn test_empty_structs();
fn test_public_fields();
fn test_using_self();
fn test_readonly_fields();
fn test_double_consume();
fn test_js_rename();
fn js_constructors();
fn js_empty_structs();
fn js_public_fields();
fn js_using_self();
fn js_readonly_fields();
fn js_double_consume();
fn js_js_rename();
}
#[wasm_bindgen_test]
fn simple() {
test_simple();
js_simple();
}
#[wasm_bindgen]
@ -52,7 +52,7 @@ impl ClassesSimple {
#[wasm_bindgen_test]
fn strings() {
test_strings()
js_strings()
}
#[wasm_bindgen]
@ -89,11 +89,11 @@ impl ClassesStrings2 {
#[wasm_bindgen_test]
fn exceptions() {
test_exceptions();
js_exceptions();
}
#[wasm_bindgen]
pub struct ClassesExceptions1 {
}
pub struct ClassesExceptions1 {}
#[wasm_bindgen]
impl ClassesExceptions1 {
@ -101,16 +101,13 @@ impl ClassesExceptions1 {
ClassesExceptions1 {}
}
pub fn foo(&self, _: &ClassesExceptions1) {
}
pub fn foo(&self, _: &ClassesExceptions1) {}
pub fn bar(&mut self, _: &mut ClassesExceptions1) {
}
pub fn bar(&mut self, _: &mut ClassesExceptions1) {}
}
#[wasm_bindgen]
pub struct ClassesExceptions2 {
}
pub struct ClassesExceptions2 {}
#[wasm_bindgen]
impl ClassesExceptions2 {
@ -121,7 +118,7 @@ impl ClassesExceptions2 {
#[wasm_bindgen_test]
fn pass_one_to_another() {
test_pass_one_to_another();
js_pass_one_to_another();
}
#[wasm_bindgen]
@ -133,11 +130,9 @@ impl ClassesPassA {
ClassesPassA {}
}
pub fn foo(&self, _other: &ClassesPassB) {
}
pub fn foo(&self, _other: &ClassesPassB) {}
pub fn bar(&self, _other: ClassesPassB) {
}
pub fn bar(&self, _other: ClassesPassB) {}
}
#[wasm_bindgen]
@ -191,7 +186,7 @@ fn pass_into_js_as_js_class() {
#[wasm_bindgen_test]
fn constructors() {
test_constructors();
js_constructors();
}
#[wasm_bindgen]
@ -236,7 +231,7 @@ impl ConstructorsBar {
#[wasm_bindgen_test]
fn empty_structs() {
test_empty_structs();
js_empty_structs();
}
#[wasm_bindgen]
@ -252,7 +247,7 @@ impl OtherEmpty {
#[wasm_bindgen_test]
fn public_fields() {
test_public_fields();
js_public_fields();
}
#[wasm_bindgen]
@ -273,12 +268,11 @@ impl PublicFields {
#[wasm_bindgen_test]
fn using_self() {
test_using_self();
js_using_self();
}
#[wasm_bindgen]
pub struct UseSelf {
}
pub struct UseSelf {}
#[wasm_bindgen]
impl UseSelf {
@ -289,7 +283,7 @@ impl UseSelf {
#[wasm_bindgen_test]
fn readonly_fields() {
test_readonly_fields();
js_readonly_fields();
}
#[wasm_bindgen]
@ -308,11 +302,11 @@ impl Readonly {
#[wasm_bindgen_test]
fn double_consume() {
test_double_consume();
js_double_consume();
}
#[wasm_bindgen]
pub struct DoubleConsume { }
pub struct DoubleConsume {}
#[wasm_bindgen]
impl DoubleConsume {
@ -328,12 +322,12 @@ impl DoubleConsume {
#[wasm_bindgen_test]
fn rename_function_for_js() {
test_js_rename();
js_js_rename();
foo();
}
#[wasm_bindgen]
pub struct JsRename { }
pub struct JsRename {}
#[wasm_bindgen]
impl JsRename {
@ -341,13 +335,11 @@ impl JsRename {
pub fn new() -> JsRename {
let f = JsRename {};
f.foo();
return f
f
}
#[wasm_bindgen(js_name = bar)]
pub fn foo(&self) {
}
pub fn foo(&self) {}
}
#[wasm_bindgen(js_name = classes_foo)]

93
tests/wasm/closures.js Normal file
View File

@ -0,0 +1,93 @@
const assert = require('assert');
exports.works_call = a => {
a();
};
exports.works_thread = a => a(2);
let CANNOT_REUSE_CACHE = null;
exports.cannot_reuse_call = a => {
CANNOT_REUSE_CACHE = a;
};
exports.cannot_reuse_call_again = () => {
CANNOT_REUSE_CACHE();
};
exports.long_lived_call1 = a => {
a();
};
exports.long_lived_call2 = a => a(2);
exports.many_arity_call1 = a => {
a();
};
exports.many_arity_call2 = a => {
a(1);
};
exports.many_arity_call3 = a => {
a(1, 2);
};
exports.many_arity_call4 = a => {
a(1, 2, 3);
};
exports.many_arity_call5 = a => {
a(1, 2, 3, 4);
};
exports.many_arity_call6 = a => {
a(1, 2, 3, 4, 5);
};
exports.many_arity_call7 = a => {
a(1, 2, 3, 4, 5, 6);
};
exports.many_arity_call8 = a => {
a(1, 2, 3, 4, 5, 6, 7);
};
let LONG_LIVED_DROPPING_CACHE = null;
exports.long_lived_dropping_cache = a => {
LONG_LIVED_DROPPING_CACHE = a;
};
exports.long_lived_dropping_call = () => {
LONG_LIVED_DROPPING_CACHE();
};
let LONG_FNMUT_RECURSIVE_CACHE = null;
exports.long_fnmut_recursive_cache = a => {
LONG_FNMUT_RECURSIVE_CACHE = a;
};
exports.long_fnmut_recursive_call = () => {
LONG_FNMUT_RECURSIVE_CACHE();
};
exports.fnmut_call = a => {
a();
};
exports.fnmut_thread = a => a(2);
let FNMUT_BAD_F = null;
exports.fnmut_bad_call = a => {
FNMUT_BAD_F = a;
a();
};
exports.fnmut_bad_again = x => {
if (x) {
FNMUT_BAD_F();
}
};
exports.string_arguments_call = a => {
a('foo');
};
exports.string_ret_call = a => {
assert.strictEqual(a('foo'), 'foobar');
};

209
tests/wasm/closures.rs Normal file
View File

@ -0,0 +1,209 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use std::cell::Cell;
use std::rc::Rc;
#[wasm_bindgen(module = "tests/wasm/closures.js", version = "*")]
extern {
fn works_call(a: &Fn());
fn works_thread(a: &Fn(u32) -> u32) -> u32;
fn cannot_reuse_call(a: &Fn());
#[wasm_bindgen(catch)]
fn cannot_reuse_call_again() -> Result<(), JsValue>;
fn long_lived_call1(a: &Closure<Fn()>);
fn long_lived_call2(a: &Closure<FnMut(u32) -> u32>) -> u32;
fn many_arity_call1(a: &Closure<Fn()>);
fn many_arity_call2(a: &Closure<Fn(u32)>);
fn many_arity_call3(a: &Closure<Fn(u32, u32)>);
fn many_arity_call4(a: &Closure<Fn(u32, u32, u32)>);
fn many_arity_call5(a: &Closure<Fn(u32, u32, u32, u32)>);
fn many_arity_call6(a: &Closure<Fn(u32, u32, u32, u32, u32)>);
fn many_arity_call7(a: &Closure<Fn(u32, u32, u32, u32, u32, u32)>);
fn many_arity_call8(a: &Closure<Fn(u32, u32, u32, u32, u32, u32, u32)>);
#[wasm_bindgen(js_name = many_arity_call1)]
fn many_arity_stack1(a: &Fn());
#[wasm_bindgen(js_name = many_arity_call2)]
fn many_arity_stack2(a: &Fn(u32));
#[wasm_bindgen(js_name = many_arity_call3)]
fn many_arity_stack3(a: &Fn(u32, u32));
#[wasm_bindgen(js_name = many_arity_call4)]
fn many_arity_stack4(a: &Fn(u32, u32, u32));
#[wasm_bindgen(js_name = many_arity_call5)]
fn many_arity_stack5(a: &Fn(u32, u32, u32, u32));
#[wasm_bindgen(js_name = many_arity_call6)]
fn many_arity_stack6(a: &Fn(u32, u32, u32, u32, u32));
#[wasm_bindgen(js_name = many_arity_call7)]
fn many_arity_stack7(a: &Fn(u32, u32, u32, u32, u32, u32));
#[wasm_bindgen(js_name = many_arity_call8)]
fn many_arity_stack8(a: &Fn(u32, u32, u32, u32, u32, u32, u32));
fn long_lived_dropping_cache(a: &Closure<Fn()>);
#[wasm_bindgen(catch)]
fn long_lived_dropping_call() -> Result<(), JsValue>;
fn long_fnmut_recursive_cache(a: &Closure<FnMut()>);
#[wasm_bindgen(catch)]
fn long_fnmut_recursive_call() -> Result<(), JsValue>;
fn fnmut_call(a: &mut FnMut());
fn fnmut_thread(a: &mut FnMut(u32) -> u32) -> u32;
fn fnmut_bad_call(a: &mut FnMut());
#[wasm_bindgen(catch)]
fn fnmut_bad_again(a: bool) -> Result<(), JsValue>;
fn string_arguments_call(a: &mut FnMut(String));
fn string_ret_call(a: &mut FnMut(String) -> String);
}
#[wasm_bindgen_test]
fn works() {
let a = Cell::new(false);
works_call(&|| a.set(true));
assert!(a.get());
assert_eq!(works_thread(&|a| a + 1), 3);
}
#[wasm_bindgen_test]
fn cannot_reuse() {
cannot_reuse_call(&|| {});
assert!(cannot_reuse_call_again().is_err());
}
#[wasm_bindgen_test]
fn long_lived() {
let hit = Rc::new(Cell::new(false));
let hit2 = hit.clone();
let a = Closure::new(move || hit2.set(true));
assert!(!hit.get());
long_lived_call1(&a);
assert!(hit.get());
let hit = Rc::new(Cell::new(false));
{
let hit = hit.clone();
let a = Closure::new(move |x| {
hit.set(true);
x + 3
});
assert_eq!(long_lived_call2(&a), 5);
}
assert!(hit.get());
}
#[wasm_bindgen_test]
fn many_arity() {
many_arity_call1(&Closure::new(|| {}));
many_arity_call2(&Closure::new(|a| assert_eq!(a, 1)));
many_arity_call3(&Closure::new(|a, b| assert_eq!((a, b), (1, 2))));
many_arity_call4(&Closure::new(|a, b, c| assert_eq!((a, b, c), (1, 2, 3))));
many_arity_call5(&Closure::new(|a, b, c, d| {
assert_eq!((a, b, c, d), (1, 2, 3, 4))
}));
many_arity_call6(&Closure::new(|a, b, c, d, e| {
assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5))
}));
many_arity_call7(&Closure::new(|a, b, c, d, e, f| {
assert_eq!((a, b, c, d, e, f), (1, 2, 3, 4, 5, 6))
}));
many_arity_call8(&Closure::new(|a, b, c, d, e, f, g| {
assert_eq!((a, b, c, d, e, f, g), (1, 2, 3, 4, 5, 6, 7))
}));
many_arity_stack1(&(|| {}));
many_arity_stack2(&(|a| assert_eq!(a, 1)));
many_arity_stack3(&(|a, b| assert_eq!((a, b), (1, 2))));
many_arity_stack4(&(|a, b, c| assert_eq!((a, b, c), (1, 2, 3))));
many_arity_stack5(&(|a, b, c, d| {
assert_eq!((a, b, c, d), (1, 2, 3, 4))
}));
many_arity_stack6(&(|a, b, c, d, e| {
assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5))
}));
many_arity_stack7(&(|a, b, c, d, e, f| {
assert_eq!((a, b, c, d, e, f), (1, 2, 3, 4, 5, 6))
}));
many_arity_stack8(&(|a, b, c, d, e, f, g| {
assert_eq!((a, b, c, d, e, f, g), (1, 2, 3, 4, 5, 6, 7))
}));
}
#[wasm_bindgen_test]
fn long_lived_dropping() {
let hit = Rc::new(Cell::new(false));
let hit2 = hit.clone();
let a = Closure::new(move || hit2.set(true));
long_lived_dropping_cache(&a);
assert!(!hit.get());
assert!(long_lived_dropping_call().is_ok());
assert!(hit.get());
drop(a);
assert!(long_lived_dropping_call().is_err());
}
#[wasm_bindgen_test]
fn long_fnmut_recursive() {
let a = Closure::new(|| {
assert!(long_fnmut_recursive_call().is_err());
});
long_fnmut_recursive_cache(&a);
assert!(long_fnmut_recursive_call().is_ok());
}
#[wasm_bindgen_test]
fn fnmut() {
let mut a = false;
fnmut_call(&mut || a = true);
assert!(a);
let mut x = false;
assert_eq!(fnmut_thread(&mut |a| {
x = true;
a + 1
}), 3);
assert!(x);
}
#[wasm_bindgen_test]
fn fnmut_bad() {
let mut x = true;
let mut hits = 0;
fnmut_bad_call(&mut || {
hits += 1;
if fnmut_bad_again(hits == 1).is_err() {
return;
}
x = false;
});
assert_eq!(hits, 1);
assert!(x);
assert!(fnmut_bad_again(true).is_err());
}
#[wasm_bindgen_test]
fn string_arguments() {
let mut x = false;
string_arguments_call(&mut |s| {
assert_eq!(s, "foo");
x = true;
});
assert!(x);
}
#[wasm_bindgen_test]
fn string_ret() {
let mut x = false;
string_ret_call(&mut |mut s| {
assert_eq!(s, "foo");
s.push_str("bar");
x = true;
s
});
assert!(x);
}

49
tests/wasm/duplicates.rs Normal file
View File

@ -0,0 +1,49 @@
use wasm_bindgen_test::*;
pub mod same_function_different_locations_a {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/duplicates_a.js", version = "*")]
extern {
pub fn foo();
}
}
pub mod same_function_different_locations_b {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/duplicates_a.js", version = "*")]
extern {
pub fn foo();
}
}
#[wasm_bindgen_test]
fn same_function_different_locations() {
same_function_different_locations_a::foo();
same_function_different_locations_b::foo();
}
pub mod same_function_different_modules_a {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/duplicates_b.js", version = "*")]
extern {
pub fn foo() -> bool;
}
}
pub mod same_function_different_modules_b {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/duplicates_c.js", version = "*")]
extern {
pub fn foo() -> bool;
}
}
#[wasm_bindgen_test]
fn same_function_different_modules() {
assert!(same_function_different_modules_a::foo());
assert!(!same_function_different_modules_b::foo());
}

View File

@ -0,0 +1 @@
exports.foo = () => {};

View File

@ -0,0 +1 @@
exports.foo = () => true;

View File

@ -0,0 +1 @@
exports.foo = () => false;

20
tests/wasm/enums.js Normal file
View File

@ -0,0 +1,20 @@
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
exports.js_c_style_enum = () => {
assert.strictEqual(wasm.Color.Green, 0);
assert.strictEqual(wasm.Color.Yellow, 1);
assert.strictEqual(wasm.Color.Red, 2);
assert.strictEqual(Object.keys(wasm.Color).length, 3);
assert.strictEqual(wasm.enum_cycle(wasm.Color.Green), wasm.Color.Yellow);
};
exports.js_c_style_enum_with_custom_values = () => {
assert.strictEqual(wasm.ColorWithCustomValues.Green, 21);
assert.strictEqual(wasm.ColorWithCustomValues.Yellow, 34);
assert.strictEqual(wasm.ColorWithCustomValues.Red, 2);
assert.strictEqual(Object.keys(wasm.ColorWithCustomValues).length, 3);
assert.strictEqual(wasm.enum_with_custom_values_cycle(wasm.ColorWithCustomValues.Green), wasm.ColorWithCustomValues.Yellow);
};

55
tests/wasm/enums.rs Normal file
View File

@ -0,0 +1,55 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use self::inner::ColorWithCustomValues;
#[wasm_bindgen(module = "tests/wasm/enums.js", version = "*")]
extern {
fn js_c_style_enum();
fn js_c_style_enum_with_custom_values();
}
#[wasm_bindgen]
pub enum Color {
Green,
Yellow,
Red,
}
pub mod inner {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub enum ColorWithCustomValues {
Green = 21,
Yellow = 34,
Red,
}
}
#[wasm_bindgen]
pub fn enum_cycle(color: Color) -> Color {
match color {
Color::Green => Color::Yellow,
Color::Yellow => Color::Red,
Color::Red => Color::Green,
}
}
#[wasm_bindgen]
pub fn enum_with_custom_values_cycle(color: ColorWithCustomValues) -> ColorWithCustomValues {
match color {
ColorWithCustomValues::Green => ColorWithCustomValues::Yellow,
ColorWithCustomValues::Yellow => ColorWithCustomValues::Red,
ColorWithCustomValues::Red => ColorWithCustomValues::Green,
}
}
#[wasm_bindgen_test]
fn c_style_enum() {
js_c_style_enum();
}
#[wasm_bindgen_test]
fn c_style_enum_with_custom_values() {
js_c_style_enum_with_custom_values();
}

84
tests/wasm/js_objects.js Normal file
View File

@ -0,0 +1,84 @@
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
let SIMPLE_ARG = null;
exports.simple_foo = s => {
assert.strictEqual(SIMPLE_ARG, null);
SIMPLE_ARG = s;
};
exports.js_simple = () => {
assert.strictEqual(SIMPLE_ARG, null);
let sym = Symbol('test');
wasm.simple_bar(sym);
assert.strictEqual(SIMPLE_ARG, sym);
};
let OWNED_ARG = null;
exports.owned_foo = s => {
assert.strictEqual(OWNED_ARG, null);
OWNED_ARG = s;
};
exports.js_owned = () => {
assert.strictEqual(OWNED_ARG, null);
let sym = Symbol('test');
wasm.owned_bar(sym);
assert.strictEqual(OWNED_ARG, sym);
};
let CLONE_ARG = Symbol('test');
exports.clone_foo1 = s => {
assert.strictEqual(s, CLONE_ARG);
};
exports.clone_foo2 = s => {
assert.strictEqual(s, CLONE_ARG);
};
exports.clone_foo3 = s => {
assert.strictEqual(s, CLONE_ARG);
};
exports.clone_foo4 = s => {
assert.strictEqual(s, CLONE_ARG);
};
exports.clone_foo5 = s => {
assert.strictEqual(s, CLONE_ARG);
};
exports.js_clone = () => {
wasm.clone_bar(CLONE_ARG);
};
let PROMOTE_ARG = Symbol('test');
exports.promote_foo1 = s => {
assert.strictEqual(s, PROMOTE_ARG);
};
exports.promote_foo2 = s => {
assert.strictEqual(s, PROMOTE_ARG);
};
exports.promote_foo3 = s => {
assert.strictEqual(s, PROMOTE_ARG);
};
exports.promote_foo4 = s => {
assert.strictEqual(s, PROMOTE_ARG);
};
exports.js_promote = () => {
wasm.promote_bar(PROMOTE_ARG);
};
exports.returning_vector_foo = () => {
return {'foo': 'bar'};
};
exports.js_returning_vector = () => {
assert.strictEqual(wasm.returning_vector_bar().length, 10);
};
exports.js_another_vector_return = () => {
assert.deepStrictEqual(wasm.another_vector_return_get_array(), [1, 2, 3, 4, 5, 6]);
};

107
tests/wasm/js_objects.rs Normal file
View File

@ -0,0 +1,107 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/js_objects.js", version = "*")]
extern {
fn simple_foo(s: &JsValue);
fn js_simple();
fn owned_foo(s: JsValue);
fn js_owned();
fn clone_foo1(s: JsValue);
fn clone_foo2(s: &JsValue);
fn clone_foo3(s: JsValue);
fn clone_foo4(s: &JsValue);
fn clone_foo5(s: JsValue);
fn js_clone();
fn promote_foo1(s: &JsValue);
fn promote_foo2(s: JsValue);
fn promote_foo3(s: &JsValue);
fn promote_foo4(s: JsValue);
fn js_promote();
fn returning_vector_foo() -> JsValue;
fn js_returning_vector();
fn js_another_vector_return();
}
#[wasm_bindgen]
pub fn simple_bar(s: &JsValue) {
simple_foo(s);
}
#[wasm_bindgen_test]
fn simple() {
js_simple();
}
#[wasm_bindgen]
pub fn owned_bar(s: JsValue) {
owned_foo(s);
}
#[wasm_bindgen_test]
fn owned() {
js_owned();
}
#[wasm_bindgen]
pub fn clone_bar(s: JsValue) {
clone_foo1(s.clone());
clone_foo2(&s);
clone_foo3(s.clone());
clone_foo4(&s);
clone_foo5(s);
}
#[wasm_bindgen_test]
fn clone() {
js_clone();
}
#[wasm_bindgen]
pub fn promote_bar(s: &JsValue) {
promote_foo1(s);
promote_foo2(s.clone());
promote_foo3(s);
promote_foo4(s.clone());
}
#[wasm_bindgen_test]
fn promote() {
js_promote();
}
#[wasm_bindgen]
pub fn returning_vector_bar() -> Vec<JsValue> {
let mut res = Vec::new();
for _ in 0..10 {
res.push(returning_vector_foo())
}
res
}
#[wasm_bindgen_test]
fn returning_vector() {
js_returning_vector();
}
#[wasm_bindgen]
pub fn another_vector_return_get_array() -> Vec<JsValue> {
vec![
JsValue::from(1),
JsValue::from(2),
JsValue::from(3),
JsValue::from(4),
JsValue::from(5),
JsValue::from(6),
]
}
#[wasm_bindgen_test]
fn another_vector_return() {
js_another_vector_return();
}

View File

@ -7,5 +7,14 @@ extern crate wasm_bindgen;
pub mod api;
pub mod char;
pub mod classes;
pub mod closures;
pub mod duplicates;
pub mod enums;
pub mod js_objects;
pub mod math;
pub mod option;
pub mod optional_primitives;
pub mod slice;
pub mod structural;
pub mod u64;
pub mod validate_prt;

5
tests/wasm/math.js Normal file
View File

@ -0,0 +1,5 @@
const wasm = require('wasm-bindgen-test.js');
exports.js_auto_bind_math = () => {
wasm.math(1.0, 2.0);
};

67
tests/wasm/math.rs Normal file
View File

@ -0,0 +1,67 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/math.js", version = "*")]
extern {
fn js_auto_bind_math();
}
#[wasm_bindgen]
pub fn math(a: f32, b: f64) -> f64 {
b.acos() +
b.asin() +
b.atan() +
b.atan2(b) +
b.cbrt() +
b.cosh() +
b.exp_m1() +
b.ln_1p() +
b.sinh() +
b.tan() +
b.tanh() +
b.hypot(b) +
b.cos() +
b.exp() +
b.exp2() +
b.mul_add(b, b) +
b.ln() +
b.log(b) +
b.log10() +
b.log2() +
b.powi(8) +
b.powf(b) +
b.round() +
b.sin() +
b.abs() +
b.signum() +
b.floor() +
b.ceil() +
b.trunc() +
b.sqrt() +
(b % (a as f64)) +
((a.cos() +
a.exp() +
a.exp2() +
a.mul_add(a, a) +
a.ln() +
a.log(a) +
a.log10() +
a.log2() +
a.powi(8) +
a.powf(a) +
a.round() +
a.sin() +
a.abs() +
a.signum() +
a.floor() +
a.ceil() +
a.trunc() +
a.sqrt() +
(a % (b as f32))) as f64) +
(b + 2.0f64.powf(a as f64))
}
#[wasm_bindgen_test]
fn auto_bind_math() {
js_auto_bind_math();
}

View File

@ -1,29 +1,28 @@
const assert = require('assert');
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
class MyType {}
class MyType {
}
exports.MyType = MyType;
exports.take_none_byval = function(x) {
assert.strictEqual(x, undefined);
exports.take_none_byval = x => {
assert.strictEqual(x, undefined);
};
exports.take_some_byval = function(x) {
assert.ok(x !== null && x !== undefined);
assert.ok(x instanceof MyType);
};
exports.return_undef_byval = function() { return undefined; };
exports.return_null_byval = function() { return null; };
exports.return_some_byval = function(x) {
return new MyType();
exports.take_some_byval = x => {
assert.ok(x !== null && x !== undefined);
assert.ok(x instanceof MyType);
};
exports.return_undef_byval = () => undefined;
exports.return_null_byval = () => null;
exports.return_some_byval = () => new MyType();
exports.test_option_values = function() {
wasm.rust_take_none_byval(null);
wasm.rust_take_none_byval(undefined);
wasm.rust_take_some_byval(new MyType());
assert.strictEqual(wasm.rust_return_none_byval(), undefined);
const x = wasm.rust_return_some_byval();
assert.ok(x !== null && x !== undefined);
assert.ok(x instanceof MyType);
exports.test_option_values = () => {
wasm.rust_take_none_byval(null);
wasm.rust_take_none_byval(undefined);
wasm.rust_take_some_byval(new MyType());
assert.strictEqual(wasm.rust_return_none_byval(), undefined);
const x = wasm.rust_return_some_byval();
assert.ok(x !== null && x !== undefined);
assert.ok(x instanceof MyType);
};

View File

@ -1,102 +1,102 @@
const assert = require('assert');
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
exports.test_works = function() {
assert.strictEqual(wasm.i32_identity(wasm.i32_none()), undefined);
assert.strictEqual(wasm.i32_identity(wasm.i32_zero()), 0);
assert.strictEqual(wasm.i32_identity(wasm.i32_one()), 1);
assert.strictEqual(wasm.i32_identity(wasm.i32_neg_one()), -1);
assert.strictEqual(wasm.i32_identity(wasm.i32_max()), 2147483647);
assert.strictEqual(wasm.i32_identity(wasm.i32_min()), -2147483648);
exports.optional_i32_js_identity = a => a;
exports.optional_u32_js_identity = a => a;
exports.optional_isize_js_identity = a => a;
exports.optional_usize_js_identity = a => a;
exports.optional_f32_js_identity = a => a;
exports.optional_f64_js_identity = a => a;
exports.optional_i8_js_identity = a => a;
exports.optional_u8_js_identity = a => a;
exports.optional_i16_js_identity = a => a;
exports.optional_u16_js_identity = a => a;
exports.optional_i64_js_identity = a => a;
exports.optional_u64_js_identity = a => a;
exports.optional_bool_js_identity = a => a;
exports.optional_char_js_identity = a => a;
assert.strictEqual(wasm.u32_identity(wasm.u32_none()), undefined);
assert.strictEqual(wasm.u32_identity(wasm.u32_zero()), 0);
assert.strictEqual(wasm.u32_identity(wasm.u32_one()), 1);
assert.strictEqual(wasm.u32_identity(wasm.u32_max()), 4294967295);
assert.strictEqual(wasm.u32_identity(wasm.u32_min()), 0);
exports.js_works = () => {
assert.strictEqual(wasm.optional_i32_identity(wasm.optional_i32_none()), undefined);
assert.strictEqual(wasm.optional_i32_identity(wasm.optional_i32_zero()), 0);
assert.strictEqual(wasm.optional_i32_identity(wasm.optional_i32_one()), 1);
assert.strictEqual(wasm.optional_i32_identity(wasm.optional_i32_neg_one()), -1);
assert.strictEqual(wasm.optional_i32_identity(wasm.optional_i32_min()), -2147483648);
assert.strictEqual(wasm.optional_i32_identity(wasm.optional_i32_max()), 2147483647);
assert.strictEqual(wasm.isize_identity(wasm.isize_none()), undefined);
assert.strictEqual(wasm.isize_identity(wasm.isize_zero()), 0);
assert.strictEqual(wasm.isize_identity(wasm.isize_one()), 1);
assert.strictEqual(wasm.isize_identity(wasm.isize_neg_one()), -1);
assert.strictEqual(wasm.isize_identity(wasm.isize_max()), 2147483647);
assert.strictEqual(wasm.isize_identity(wasm.isize_min()), -2147483648);
assert.strictEqual(wasm.optional_u32_identity(wasm.optional_u32_none()), undefined);
assert.strictEqual(wasm.optional_u32_identity(wasm.optional_u32_zero()), 0);
assert.strictEqual(wasm.optional_u32_identity(wasm.optional_u32_one()), 1);
assert.strictEqual(wasm.optional_u32_identity(wasm.optional_u32_min()), 0);
assert.strictEqual(wasm.optional_u32_identity(wasm.optional_u32_max()), 4294967295);
assert.strictEqual(wasm.usize_identity(wasm.usize_none()), undefined);
assert.strictEqual(wasm.usize_identity(wasm.usize_zero()), 0);
assert.strictEqual(wasm.usize_identity(wasm.usize_one()), 1);
assert.strictEqual(wasm.usize_identity(wasm.usize_max()), 4294967295);
assert.strictEqual(wasm.usize_identity(wasm.usize_min()), 0);
assert.strictEqual(wasm.optional_isize_identity(wasm.optional_isize_none()), undefined);
assert.strictEqual(wasm.optional_isize_identity(wasm.optional_isize_zero()), 0);
assert.strictEqual(wasm.optional_isize_identity(wasm.optional_isize_one()), 1);
assert.strictEqual(wasm.optional_isize_identity(wasm.optional_isize_neg_one()), -1);
assert.strictEqual(wasm.optional_isize_identity(wasm.optional_isize_min()), -2147483648);
assert.strictEqual(wasm.optional_isize_identity(wasm.optional_isize_max()), 2147483647);
assert.strictEqual(wasm.f32_identity(wasm.f32_none()), undefined);
assert.strictEqual(wasm.f32_identity(wasm.f32_zero()), 0);
assert.strictEqual(wasm.f32_identity(wasm.f32_one()), 1);
assert.strictEqual(wasm.f32_identity(wasm.f32_neg_one()), -1);
assert.strictEqual(wasm.optional_usize_identity(wasm.optional_usize_none()), undefined);
assert.strictEqual(wasm.optional_usize_identity(wasm.optional_usize_zero()), 0);
assert.strictEqual(wasm.optional_usize_identity(wasm.optional_usize_one()), 1);
assert.strictEqual(wasm.optional_usize_identity(wasm.optional_usize_min()), 0);
assert.strictEqual(wasm.optional_usize_identity(wasm.optional_usize_max()), 4294967295);
assert.strictEqual(wasm.f64_identity(wasm.f64_none()), undefined);
assert.strictEqual(wasm.f64_identity(wasm.f64_zero()), 0);
assert.strictEqual(wasm.f64_identity(wasm.f64_one()), 1);
assert.strictEqual(wasm.f64_identity(wasm.f64_neg_one()), -1);
assert.strictEqual(wasm.optional_f32_identity(wasm.optional_f32_none()), undefined);
assert.strictEqual(wasm.optional_f32_identity(wasm.optional_f32_zero()), 0);
assert.strictEqual(wasm.optional_f32_identity(wasm.optional_f32_one()), 1);
assert.strictEqual(wasm.optional_f32_identity(wasm.optional_f32_neg_one()), -1);
assert.strictEqual(wasm.i8_identity(wasm.i8_none()), undefined);
assert.strictEqual(wasm.i8_identity(wasm.i8_zero()), 0);
assert.strictEqual(wasm.i8_identity(wasm.i8_one()), 1);
assert.strictEqual(wasm.i8_identity(wasm.i8_neg_one()), -1);
assert.strictEqual(wasm.i8_identity(wasm.i8_max()), 127);
assert.strictEqual(wasm.i8_identity(wasm.i8_min()), -128);
assert.strictEqual(wasm.optional_f64_identity(wasm.optional_f64_none()), undefined);
assert.strictEqual(wasm.optional_f64_identity(wasm.optional_f64_zero()), 0);
assert.strictEqual(wasm.optional_f64_identity(wasm.optional_f64_one()), 1);
assert.strictEqual(wasm.optional_f64_identity(wasm.optional_f64_neg_one()), -1);
assert.strictEqual(wasm.u8_identity(wasm.u8_none()), undefined);
assert.strictEqual(wasm.u8_identity(wasm.u8_zero()), 0);
assert.strictEqual(wasm.u8_identity(wasm.u8_one()), 1);
assert.strictEqual(wasm.u8_identity(wasm.u8_max()), 255);
assert.strictEqual(wasm.u8_identity(wasm.u8_min()), 0);
assert.strictEqual(wasm.optional_i8_identity(wasm.optional_i8_none()), undefined);
assert.strictEqual(wasm.optional_i8_identity(wasm.optional_i8_zero()), 0);
assert.strictEqual(wasm.optional_i8_identity(wasm.optional_i8_one()), 1);
assert.strictEqual(wasm.optional_i8_identity(wasm.optional_i8_neg_one()), -1);
assert.strictEqual(wasm.optional_i8_identity(wasm.optional_i8_min()), -128);
assert.strictEqual(wasm.optional_i8_identity(wasm.optional_i8_max()), 127);
assert.strictEqual(wasm.i16_identity(wasm.i16_none()), undefined);
assert.strictEqual(wasm.i16_identity(wasm.i16_zero()), 0);
assert.strictEqual(wasm.i16_identity(wasm.i16_one()), 1);
assert.strictEqual(wasm.i16_identity(wasm.i16_neg_one()), -1);
assert.strictEqual(wasm.i16_identity(wasm.i16_max()), 32767);
assert.strictEqual(wasm.i16_identity(wasm.i16_min()), -32768);
assert.strictEqual(wasm.optional_u8_identity(wasm.optional_u8_none()), undefined);
assert.strictEqual(wasm.optional_u8_identity(wasm.optional_u8_zero()), 0);
assert.strictEqual(wasm.optional_u8_identity(wasm.optional_u8_one()), 1);
assert.strictEqual(wasm.optional_u8_identity(wasm.optional_u8_min()), 0);
assert.strictEqual(wasm.optional_u8_identity(wasm.optional_u8_max()), 255);
assert.strictEqual(wasm.u16_identity(wasm.u16_none()), undefined);
assert.strictEqual(wasm.u16_identity(wasm.u16_zero()), 0);
assert.strictEqual(wasm.u16_identity(wasm.u16_one()), 1);
assert.strictEqual(wasm.u16_identity(wasm.u16_max()), 65535);
assert.strictEqual(wasm.u16_identity(wasm.u16_min()), 0);
assert.strictEqual(wasm.optional_i16_identity(wasm.optional_i16_none()), undefined);
assert.strictEqual(wasm.optional_i16_identity(wasm.optional_i16_zero()), 0);
assert.strictEqual(wasm.optional_i16_identity(wasm.optional_i16_one()), 1);
assert.strictEqual(wasm.optional_i16_identity(wasm.optional_i16_neg_one()), -1);
assert.strictEqual(wasm.optional_i16_identity(wasm.optional_i16_min()), -32768);
assert.strictEqual(wasm.optional_i16_identity(wasm.optional_i16_max()), 32767);
assert.strictEqual(wasm.i64_identity(wasm.i64_none()), undefined);
assert.strictEqual(wasm.i64_identity(wasm.i64_zero()), BigInt('0'));
assert.strictEqual(wasm.i64_identity(wasm.i64_one()), BigInt('1'));
assert.strictEqual(wasm.i64_identity(wasm.i64_neg_one()), BigInt('-1'));
assert.strictEqual(wasm.i64_identity(wasm.i64_max()), BigInt('9223372036854775807'));
assert.strictEqual(wasm.i64_identity(wasm.i64_min()), BigInt('-9223372036854775808'));
assert.strictEqual(wasm.optional_u16_identity(wasm.optional_u16_none()), undefined);
assert.strictEqual(wasm.optional_u16_identity(wasm.optional_u16_zero()), 0);
assert.strictEqual(wasm.optional_u16_identity(wasm.optional_u16_one()), 1);
assert.strictEqual(wasm.optional_u16_identity(wasm.optional_u16_min()), 0);
assert.strictEqual(wasm.optional_u16_identity(wasm.optional_u16_max()), 65535);
assert.strictEqual(wasm.u64_identity(wasm.u64_none()), undefined);
assert.strictEqual(wasm.u64_identity(wasm.u64_zero()), BigInt('0'));
assert.strictEqual(wasm.u64_identity(wasm.u64_one()), BigInt('1'));
assert.strictEqual(wasm.u64_identity(wasm.u64_max()), BigInt('18446744073709551615'));
assert.strictEqual(wasm.u64_identity(wasm.u64_min()), BigInt('0'));
assert.strictEqual(wasm.optional_i64_identity(wasm.optional_i64_none()), undefined);
assert.strictEqual(wasm.optional_i64_identity(wasm.optional_i64_zero()), BigInt('0'));
assert.strictEqual(wasm.optional_i64_identity(wasm.optional_i64_one()), BigInt('1'));
assert.strictEqual(wasm.optional_i64_identity(wasm.optional_i64_neg_one()), BigInt('-1'));
assert.strictEqual(wasm.optional_i64_identity(wasm.optional_i64_min()), BigInt('-9223372036854775808'));
assert.strictEqual(wasm.optional_i64_identity(wasm.optional_i64_max()), BigInt('9223372036854775807'));
assert.strictEqual(wasm.bool_identity(wasm.bool_none()), undefined);
assert.strictEqual(wasm.bool_identity(wasm.bool_false()), false);
assert.strictEqual(wasm.bool_identity(wasm.bool_true()), true);
assert.strictEqual(wasm.optional_u64_identity(wasm.optional_u64_none()), undefined);
assert.strictEqual(wasm.optional_u64_identity(wasm.optional_u64_zero()), BigInt('0'));
assert.strictEqual(wasm.optional_u64_identity(wasm.optional_u64_one()), BigInt('1'));
assert.strictEqual(wasm.optional_u64_identity(wasm.optional_u64_min()), BigInt('0'));
assert.strictEqual(wasm.optional_u64_identity(wasm.optional_u64_max()), BigInt('18446744073709551615'));
assert.strictEqual(wasm.char_identity(wasm.char_none()), undefined);
assert.strictEqual(wasm.char_identity(wasm.char_letter()), 'a');
assert.strictEqual(wasm.char_identity(wasm.char_face()), '😀');
assert.strictEqual(wasm.optional_bool_identity(wasm.optional_bool_none()), undefined);
assert.strictEqual(wasm.optional_bool_identity(wasm.optional_bool_false()), false);
assert.strictEqual(wasm.optional_bool_identity(wasm.optional_bool_true()), true);
assert.strictEqual(wasm.optional_char_identity(wasm.optional_char_none()), undefined);
assert.strictEqual(wasm.optional_char_identity(wasm.optional_char_letter()), 'a');
assert.strictEqual(wasm.optional_char_identity(wasm.optional_char_face()), '😀');
};
exports.i32_js_identity = function(a) { return a; };
exports.u32_js_identity = function(a) { return a; };
exports.isize_js_identity = function(a) { return a; };
exports.usize_js_identity = function(a) { return a; };
exports.f32_js_identity = function(a) { return a; };
exports.f64_js_identity = function(a) { return a; };
exports.i8_js_identity = function(a) { return a; };
exports.u8_js_identity = function(a) { return a; };
exports.i16_js_identity = function(a) { return a; };
exports.u16_js_identity = function(a) { return a; };
exports.i64_js_identity = function(a) { return a; };
exports.u64_js_identity = function(a) { return a; };
exports.bool_js_identity = function(a) { return a; };
exports.char_js_identity = function(a) { return a; };

View File

@ -3,205 +3,274 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/optional_primitives.js", version = "*")]
extern {
fn i32_js_identity(a: Option<i32>) -> Option<i32>;
fn u32_js_identity(a: Option<u32>) -> Option<u32>;
fn isize_js_identity(a: Option<isize>) -> Option<isize>;
fn usize_js_identity(a: Option<usize>) -> Option<usize>;
fn f32_js_identity(a: Option<f32>) -> Option<f32>;
fn f64_js_identity(a: Option<f64>) -> Option<f64>;
fn i8_js_identity(a: Option<i8>) -> Option<i8>;
fn u8_js_identity(a: Option<u8>) -> Option<u8>;
fn i16_js_identity(a: Option<i16>) -> Option<i16>;
fn u16_js_identity(a: Option<u16>) -> Option<u16>;
fn i64_js_identity(a: Option<i64>) -> Option<i64>;
fn u64_js_identity(a: Option<u64>) -> Option<u64>;
fn bool_js_identity(a: Option<bool>) -> Option<bool>;
fn char_js_identity(a: Option<char>) -> Option<char>;
fn optional_i32_js_identity(a: Option<i32>) -> Option<i32>;
fn optional_u32_js_identity(a: Option<u32>) -> Option<u32>;
fn optional_isize_js_identity(a: Option<isize>) -> Option<isize>;
fn optional_usize_js_identity(a: Option<usize>) -> Option<usize>;
fn optional_f32_js_identity(a: Option<f32>) -> Option<f32>;
fn optional_f64_js_identity(a: Option<f64>) -> Option<f64>;
fn optional_i8_js_identity(a: Option<i8>) -> Option<i8>;
fn optional_u8_js_identity(a: Option<u8>) -> Option<u8>;
fn optional_i16_js_identity(a: Option<i16>) -> Option<i16>;
fn optional_u16_js_identity(a: Option<u16>) -> Option<u16>;
fn optional_i64_js_identity(a: Option<i64>) -> Option<i64>;
fn optional_u64_js_identity(a: Option<u64>) -> Option<u64>;
fn optional_bool_js_identity(a: Option<bool>) -> Option<bool>;
fn optional_char_js_identity(a: Option<char>) -> Option<char>;
fn test_works();
fn js_works();
}
#[wasm_bindgen]
pub fn i32_none() -> Option<i32> { None }
#[wasm_bindgen]
pub fn i32_zero() -> Option<i32> { Some(0) }
#[wasm_bindgen]
pub fn i32_one() -> Option<i32> { Some(1) }
#[wasm_bindgen]
pub fn i32_neg_one() -> Option<i32> { Some(-1) }
#[wasm_bindgen]
pub fn i32_max() -> Option<i32> { Some(i32::max_value()) }
#[wasm_bindgen]
pub fn i32_min() -> Option<i32> { Some(i32::min_value()) }
#[wasm_bindgen]
pub fn i32_identity(a: Option<i32>) -> Option<i32> { i32_js_identity(a) }
pub fn optional_i32_none() -> Option<i32> { None }
#[wasm_bindgen]
pub fn u32_none() -> Option<u32> { None }
#[wasm_bindgen]
pub fn u32_zero() -> Option<u32> { Some(0) }
#[wasm_bindgen]
pub fn u32_one() -> Option<u32> { Some(1) }
#[wasm_bindgen]
pub fn u32_max() -> Option<u32> { Some(u32::max_value()) }
#[wasm_bindgen]
pub fn u32_min() -> Option<u32> { Some(u32::min_value()) }
#[wasm_bindgen]
pub fn u32_identity(a: Option<u32>) -> Option<u32> { u32_js_identity(a) }
pub fn optional_i32_zero() -> Option<i32> { Some(0) }
#[wasm_bindgen]
pub fn isize_none() -> Option<isize> { None }
#[wasm_bindgen]
pub fn isize_zero() -> Option<isize> { Some(0) }
#[wasm_bindgen]
pub fn isize_one() -> Option<isize> { Some(1) }
#[wasm_bindgen]
pub fn isize_neg_one() -> Option<isize> { Some(-1) }
#[wasm_bindgen]
pub fn isize_max() -> Option<isize> { Some(isize::max_value()) }
#[wasm_bindgen]
pub fn isize_min() -> Option<isize> { Some(isize::min_value()) }
#[wasm_bindgen]
pub fn isize_identity(a: Option<isize>) -> Option<isize> { isize_js_identity(a) }
pub fn optional_i32_one() -> Option<i32> { Some(1) }
#[wasm_bindgen]
pub fn usize_none() -> Option<usize> { None }
#[wasm_bindgen]
pub fn usize_zero() -> Option<usize> { Some(0) }
#[wasm_bindgen]
pub fn usize_one() -> Option<usize> { Some(1) }
#[wasm_bindgen]
pub fn usize_max() -> Option<usize> { Some(usize::max_value()) }
#[wasm_bindgen]
pub fn usize_min() -> Option<usize> { Some(usize::min_value()) }
#[wasm_bindgen]
pub fn usize_identity(a: Option<usize>) -> Option<usize> { usize_js_identity(a) }
pub fn optional_i32_neg_one() -> Option<i32> { Some(-1) }
#[wasm_bindgen]
pub fn f32_none() -> Option<f32> { None }
#[wasm_bindgen]
pub fn f32_zero() -> Option<f32> { Some(0f32) }
#[wasm_bindgen]
pub fn f32_one() -> Option<f32> { Some(1f32) }
#[wasm_bindgen]
pub fn f32_neg_one() -> Option<f32> { Some(-1f32) }
#[wasm_bindgen]
pub fn f32_identity(a: Option<f32>) -> Option<f32> { f32_js_identity(a) }
pub fn optional_i32_min() -> Option<i32> { Some(i32::min_value()) }
#[wasm_bindgen]
pub fn f64_none() -> Option<f64> { None }
#[wasm_bindgen]
pub fn f64_zero() -> Option<f64> { Some(0f64) }
#[wasm_bindgen]
pub fn f64_one() -> Option<f64> { Some(1f64) }
#[wasm_bindgen]
pub fn f64_neg_one() -> Option<f64> { Some(-1f64) }
#[wasm_bindgen]
pub fn f64_identity(a: Option<f64>) -> Option<f64> { f64_js_identity(a) }
pub fn optional_i32_max() -> Option<i32> { Some(i32::max_value()) }
#[wasm_bindgen]
pub fn i8_none() -> Option<i8> { None }
#[wasm_bindgen]
pub fn i8_zero() -> Option<i8> { Some(0) }
#[wasm_bindgen]
pub fn i8_one() -> Option<i8> { Some(1) }
#[wasm_bindgen]
pub fn i8_neg_one() -> Option<i8> { Some(-1) }
#[wasm_bindgen]
pub fn i8_max() -> Option<i8> { Some(i8::max_value()) }
#[wasm_bindgen]
pub fn i8_min() -> Option<i8> { Some(i8::min_value()) }
#[wasm_bindgen]
pub fn i8_identity(a: Option<i8>) -> Option<i8> { i8_js_identity(a) }
pub fn optional_i32_identity(a: Option<i32>) -> Option<i32> { optional_i32_js_identity(a) }
#[wasm_bindgen]
pub fn u8_none() -> Option<u8> { None }
#[wasm_bindgen]
pub fn u8_zero() -> Option<u8> { Some(0) }
#[wasm_bindgen]
pub fn u8_one() -> Option<u8> { Some(1) }
#[wasm_bindgen]
pub fn u8_max() -> Option<u8> { Some(u8::max_value()) }
#[wasm_bindgen]
pub fn u8_min() -> Option<u8> { Some(u8::min_value()) }
#[wasm_bindgen]
pub fn u8_identity(a: Option<u8>) -> Option<u8> { u8_js_identity(a) }
pub fn optional_u32_none() -> Option<u32> { None }
#[wasm_bindgen]
pub fn i16_none() -> Option<i16> { None }
#[wasm_bindgen]
pub fn i16_zero() -> Option<i16> { Some(0) }
#[wasm_bindgen]
pub fn i16_one() -> Option<i16> { Some(1) }
#[wasm_bindgen]
pub fn i16_neg_one() -> Option<i16> { Some(-1) }
#[wasm_bindgen]
pub fn i16_max() -> Option<i16> { Some(i16::max_value()) }
#[wasm_bindgen]
pub fn i16_min() -> Option<i16> { Some(i16::min_value()) }
#[wasm_bindgen]
pub fn i16_identity(a: Option<i16>) -> Option<i16> { i16_js_identity(a) }
pub fn optional_u32_zero() -> Option<u32> { Some(0) }
#[wasm_bindgen]
pub fn u16_none() -> Option<u16> { None }
#[wasm_bindgen]
pub fn u16_zero() -> Option<u16> { Some(0) }
#[wasm_bindgen]
pub fn u16_one() -> Option<u16> { Some(1) }
#[wasm_bindgen]
pub fn u16_max() -> Option<u16> { Some(u16::max_value()) }
#[wasm_bindgen]
pub fn u16_min() -> Option<u16> { Some(u16::min_value()) }
#[wasm_bindgen]
pub fn u16_identity(a: Option<u16>) -> Option<u16> { u16_js_identity(a) }
pub fn optional_u32_one() -> Option<u32> { Some(1) }
#[wasm_bindgen]
pub fn i64_none() -> Option<i64> { None }
#[wasm_bindgen]
pub fn i64_zero() -> Option<i64> { Some(0) }
#[wasm_bindgen]
pub fn i64_one() -> Option<i64> { Some(1) }
#[wasm_bindgen]
pub fn i64_neg_one() -> Option<i64> { Some(-1) }
#[wasm_bindgen]
pub fn i64_max() -> Option<i64> { Some(i64::max_value()) }
#[wasm_bindgen]
pub fn i64_min() -> Option<i64> { Some(i64::min_value()) }
#[wasm_bindgen]
pub fn i64_identity(a: Option<i64>) -> Option<i64> { i64_js_identity(a) }
pub fn optional_u32_min() -> Option<u32> { Some(u32::min_value()) }
#[wasm_bindgen]
pub fn u64_none() -> Option<u64> { None }
#[wasm_bindgen]
pub fn u64_zero() -> Option<u64> { Some(0) }
#[wasm_bindgen]
pub fn u64_one() -> Option<u64> { Some(1) }
#[wasm_bindgen]
pub fn u64_max() -> Option<u64> { Some(u64::max_value()) }
#[wasm_bindgen]
pub fn u64_min() -> Option<u64> { Some(u64::min_value()) }
#[wasm_bindgen]
pub fn u64_identity(a: Option<u64>) -> Option<u64> { u64_js_identity(a) }
pub fn optional_u32_max() -> Option<u32> { Some(u32::max_value()) }
#[wasm_bindgen]
pub fn bool_none() -> Option<bool> { None }
#[wasm_bindgen]
pub fn bool_false() -> Option<bool> { Some(false) }
#[wasm_bindgen]
pub fn bool_true() -> Option<bool> { Some(true) }
#[wasm_bindgen]
pub fn bool_identity(a: Option<bool>) -> Option<bool> { bool_js_identity(a) }
pub fn optional_u32_identity(a: Option<u32>) -> Option<u32> { optional_u32_js_identity(a) }
#[wasm_bindgen]
pub fn char_none() -> Option<char> { None }
pub fn optional_isize_none() -> Option<isize> { None }
#[wasm_bindgen]
pub fn char_letter() -> Option<char> { Some('a') }
pub fn optional_isize_zero() -> Option<isize> { Some(0) }
#[wasm_bindgen]
pub fn char_face() -> Option<char> { Some('😀') }
pub fn optional_isize_one() -> Option<isize> { Some(1) }
#[wasm_bindgen]
pub fn char_identity(a: Option<char>) -> Option<char> { char_js_identity(a) }
pub fn optional_isize_neg_one() -> Option<isize> { Some(-1) }
#[wasm_bindgen]
pub fn optional_isize_min() -> Option<isize> { Some(isize::min_value()) }
#[wasm_bindgen]
pub fn optional_isize_max() -> Option<isize> { Some(isize::max_value()) }
#[wasm_bindgen]
pub fn optional_isize_identity(a: Option<isize>) -> Option<isize> { optional_isize_js_identity(a) }
#[wasm_bindgen]
pub fn optional_usize_none() -> Option<usize> { None }
#[wasm_bindgen]
pub fn optional_usize_zero() -> Option<usize> { Some(0) }
#[wasm_bindgen]
pub fn optional_usize_one() -> Option<usize> { Some(1) }
#[wasm_bindgen]
pub fn optional_usize_min() -> Option<usize> { Some(usize::min_value()) }
#[wasm_bindgen]
pub fn optional_usize_max() -> Option<usize> { Some(usize::max_value()) }
#[wasm_bindgen]
pub fn optional_usize_identity(a: Option<usize>) -> Option<usize> { optional_usize_js_identity(a) }
#[wasm_bindgen]
pub fn optional_f32_none() -> Option<f32> { None }
#[wasm_bindgen]
pub fn optional_f32_zero() -> Option<f32> { Some(0f32) }
#[wasm_bindgen]
pub fn optional_f32_one() -> Option<f32> { Some(1f32) }
#[wasm_bindgen]
pub fn optional_f32_neg_one() -> Option<f32> { Some(-1f32) }
#[wasm_bindgen]
pub fn optional_f32_identity(a: Option<f32>) -> Option<f32> { optional_f32_js_identity(a) }
#[wasm_bindgen]
pub fn optional_f64_none() -> Option<f64> { None }
#[wasm_bindgen]
pub fn optional_f64_zero() -> Option<f64> { Some(0f64) }
#[wasm_bindgen]
pub fn optional_f64_one() -> Option<f64> { Some(1f64) }
#[wasm_bindgen]
pub fn optional_f64_neg_one() -> Option<f64> { Some(-1f64) }
#[wasm_bindgen]
pub fn optional_f64_identity(a: Option<f64>) -> Option<f64> { optional_f64_js_identity(a) }
#[wasm_bindgen]
pub fn optional_i8_none() -> Option<i8> { None }
#[wasm_bindgen]
pub fn optional_i8_zero() -> Option<i8> { Some(0) }
#[wasm_bindgen]
pub fn optional_i8_one() -> Option<i8> { Some(1) }
#[wasm_bindgen]
pub fn optional_i8_neg_one() -> Option<i8> { Some(-1) }
#[wasm_bindgen]
pub fn optional_i8_min() -> Option<i8> { Some(i8::min_value()) }
#[wasm_bindgen]
pub fn optional_i8_max() -> Option<i8> { Some(i8::max_value()) }
#[wasm_bindgen]
pub fn optional_i8_identity(a: Option<i8>) -> Option<i8> { optional_i8_js_identity(a) }
#[wasm_bindgen]
pub fn optional_u8_none() -> Option<u8> { None }
#[wasm_bindgen]
pub fn optional_u8_zero() -> Option<u8> { Some(0) }
#[wasm_bindgen]
pub fn optional_u8_one() -> Option<u8> { Some(1) }
#[wasm_bindgen]
pub fn optional_u8_min() -> Option<u8> { Some(u8::min_value()) }
#[wasm_bindgen]
pub fn optional_u8_max() -> Option<u8> { Some(u8::max_value()) }
#[wasm_bindgen]
pub fn optional_u8_identity(a: Option<u8>) -> Option<u8> { optional_u8_js_identity(a) }
#[wasm_bindgen]
pub fn optional_i16_none() -> Option<i16> { None }
#[wasm_bindgen]
pub fn optional_i16_zero() -> Option<i16> { Some(0) }
#[wasm_bindgen]
pub fn optional_i16_one() -> Option<i16> { Some(1) }
#[wasm_bindgen]
pub fn optional_i16_neg_one() -> Option<i16> { Some(-1) }
#[wasm_bindgen]
pub fn optional_i16_min() -> Option<i16> { Some(i16::min_value()) }
#[wasm_bindgen]
pub fn optional_i16_max() -> Option<i16> { Some(i16::max_value()) }
#[wasm_bindgen]
pub fn optional_i16_identity(a: Option<i16>) -> Option<i16> { optional_i16_js_identity(a) }
#[wasm_bindgen]
pub fn optional_u16_none() -> Option<u16> { None }
#[wasm_bindgen]
pub fn optional_u16_zero() -> Option<u16> { Some(0) }
#[wasm_bindgen]
pub fn optional_u16_one() -> Option<u16> { Some(1) }
#[wasm_bindgen]
pub fn optional_u16_min() -> Option<u16> { Some(u16::min_value()) }
#[wasm_bindgen]
pub fn optional_u16_max() -> Option<u16> { Some(u16::max_value()) }
#[wasm_bindgen]
pub fn optional_u16_identity(a: Option<u16>) -> Option<u16> { optional_u16_js_identity(a) }
#[wasm_bindgen]
pub fn optional_i64_none() -> Option<i64> { None }
#[wasm_bindgen]
pub fn optional_i64_zero() -> Option<i64> { Some(0) }
#[wasm_bindgen]
pub fn optional_i64_one() -> Option<i64> { Some(1) }
#[wasm_bindgen]
pub fn optional_i64_neg_one() -> Option<i64> { Some(-1) }
#[wasm_bindgen]
pub fn optional_i64_min() -> Option<i64> { Some(i64::min_value()) }
#[wasm_bindgen]
pub fn optional_i64_max() -> Option<i64> { Some(i64::max_value()) }
#[wasm_bindgen]
pub fn optional_i64_identity(a: Option<i64>) -> Option<i64> { optional_i64_js_identity(a) }
#[wasm_bindgen]
pub fn optional_u64_none() -> Option<u64> { None }
#[wasm_bindgen]
pub fn optional_u64_zero() -> Option<u64> { Some(0) }
#[wasm_bindgen]
pub fn optional_u64_one() -> Option<u64> { Some(1) }
#[wasm_bindgen]
pub fn optional_u64_min() -> Option<u64> { Some(u64::min_value()) }
#[wasm_bindgen]
pub fn optional_u64_max() -> Option<u64> { Some(u64::max_value()) }
#[wasm_bindgen]
pub fn optional_u64_identity(a: Option<u64>) -> Option<u64> { optional_u64_js_identity(a) }
#[wasm_bindgen]
pub fn optional_bool_none() -> Option<bool> { None }
#[wasm_bindgen]
pub fn optional_bool_false() -> Option<bool> { Some(false) }
#[wasm_bindgen]
pub fn optional_bool_true() -> Option<bool> { Some(true) }
#[wasm_bindgen]
pub fn optional_bool_identity(a: Option<bool>) -> Option<bool> { optional_bool_js_identity(a) }
#[wasm_bindgen]
pub fn optional_char_none() -> Option<char> { None }
#[wasm_bindgen]
pub fn optional_char_letter() -> Option<char> { Some('a') }
#[wasm_bindgen]
pub fn optional_char_face() -> Option<char> { Some('😀') }
#[wasm_bindgen]
pub fn optional_char_identity(a: Option<char>) -> Option<char> { optional_char_js_identity(a) }
#[wasm_bindgen_test]
fn works() {
test_works();
js_works();
}

206
tests/wasm/slice.js Normal file
View File

@ -0,0 +1,206 @@
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
exports.js_export = () => {
const i8 = new Int8Array(2);
i8[0] = 1;
i8[1] = 2;
assert.deepStrictEqual(wasm.export_i8(i8), i8);
const u8 = new Uint8Array(2);
u8[0] = 1;
u8[1] = 2;
assert.deepStrictEqual(wasm.export_u8(u8), u8);
const i16 = new Int16Array(2);
i16[0] = 1;
i16[1] = 2;
assert.deepStrictEqual(wasm.export_i16(i16), i16);
const u16 = new Uint16Array(2);
u16[0] = 1;
u16[1] = 2;
assert.deepStrictEqual(wasm.export_u16(u16), u16);
const i32 = new Int32Array(2);
i32[0] = 1;
i32[1] = 2;
assert.deepStrictEqual(wasm.export_i32(i32), i32);
const u32 = new Uint32Array(2);
u32[0] = 1;
u32[1] = 2;
assert.deepStrictEqual(wasm.export_u32(u32), u32);
const f32 = new Float32Array(2);
f32[0] = 1;
f32[1] = 2;
assert.deepStrictEqual(wasm.export_f32(f32), f32);
const f64 = new Float64Array(2);
f64[0] = 1;
f64[1] = 2;
assert.deepStrictEqual(wasm.export_f64(f64), f64);
};
exports.import_js_i8 = a => {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
};
exports.import_js_u8 = a => {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
};
exports.import_js_i16 = a => {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
};
exports.import_js_u16 = a => {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
};
exports.import_js_i32 = a => {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
};
exports.import_js_u32 = a => {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
};
exports.import_js_f32 = a => {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
};
exports.import_js_f64 = a => {
assert.strictEqual(a.length, 2);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
return a;
};
exports.js_import = () => {
const i8 = new Int8Array(2);
i8[0] = 1;
i8[1] = 2;
assert.deepStrictEqual(wasm.import_rust_i8(i8), i8);
const u8 = new Uint8Array(2);
u8[0] = 1;
u8[1] = 2;
assert.deepStrictEqual(wasm.import_rust_u8(u8), u8);
const i16 = new Int16Array(2);
i16[0] = 1;
i16[1] = 2;
assert.deepStrictEqual(wasm.import_rust_i16(i16), i16);
const u16 = new Uint16Array(2);
u16[0] = 1;
u16[1] = 2;
assert.deepStrictEqual(wasm.import_rust_u16(u16), u16);
const i32 = new Int32Array(2);
i32[0] = 1;
i32[1] = 2;
assert.deepStrictEqual(wasm.import_rust_i32(i32), i32);
const u32 = new Uint32Array(2);
u32[0] = 1;
u32[1] = 2;
assert.deepStrictEqual(wasm.import_rust_u32(u32), u32);
const f32 = new Float32Array(2);
f32[0] = 1;
f32[1] = 2;
assert.deepStrictEqual(wasm.import_rust_f32(f32), f32);
const f64 = new Float64Array(2);
f64[0] = 1;
f64[1] = 2;
assert.deepStrictEqual(wasm.import_rust_f64(f64), f64);
};
exports.js_pass_array = () => {
wasm.pass_array_rust_i8([1, 2]);
wasm.pass_array_rust_u8([1, 2]);
wasm.pass_array_rust_i16([1, 2]);
wasm.pass_array_rust_u16([1, 2]);
wasm.pass_array_rust_i32([1, 2]);
wasm.pass_array_rust_u32([1, 2]);
wasm.pass_array_rust_f32([1, 2]);
wasm.pass_array_rust_f64([1, 2]);
};
const import_mut_foo = a => {
assert.strictEqual(a.length, 3);
assert.strictEqual(a[0], 1);
assert.strictEqual(a[1], 2);
a[0] = 4;
a[1] = 5;
};
exports.import_mut_js_i8 = import_mut_foo;
exports.import_mut_js_u8 = import_mut_foo;
exports.import_mut_js_i16 = import_mut_foo;
exports.import_mut_js_u16 = import_mut_foo;
exports.import_mut_js_i32 = import_mut_foo;
exports.import_mut_js_u32 = import_mut_foo;
exports.import_mut_js_f32 = import_mut_foo;
exports.import_mut_js_f64 = import_mut_foo;
const export_mut_run = (a, rust) => {
assert.strictEqual(a.length, 3);
a[0] = 1;
a[1] = 2;
a[2] = 3;
console.log(a);
rust(a);
console.log(a);
assert.strictEqual(a.length, 3);
assert.strictEqual(a[0], 4);
assert.strictEqual(a[1], 5);
assert.strictEqual(a[2], 3);
};
exports.js_export_mut = () => {
export_mut_run(new Int8Array(3), wasm.export_mut_i8);
export_mut_run(new Uint8Array(3), wasm.export_mut_u8);
export_mut_run(new Int16Array(3), wasm.export_mut_i16);
export_mut_run(new Uint16Array(3), wasm.export_mut_u16);
export_mut_run(new Int32Array(3), wasm.export_mut_i32);
export_mut_run(new Uint32Array(3), wasm.export_mut_u32);
export_mut_run(new Float32Array(3), wasm.export_mut_f32);
export_mut_run(new Float64Array(3), wasm.export_mut_f64);
};
exports.js_return_vec = () => {
const app = wasm.return_vec_web_main();
for (let i = 0; i < 10; i++) {
app.tick();
const bad = wasm.return_vec_broken_vec();
console.log('Received from rust:', i, bad);
assert.strictEqual(bad[0], 1);
assert.strictEqual(bad[1], 2);
assert.strictEqual(bad[2], 3);
assert.strictEqual(bad[3], 4);
assert.strictEqual(bad[4], 5);
assert.strictEqual(bad[5], 6);
assert.strictEqual(bad[6], 7);
assert.strictEqual(bad[7], 8);
assert.strictEqual(bad[8], 9);
}
};

210
tests/wasm/slice.rs Normal file
View File

@ -0,0 +1,210 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/slice.js", version = "*")]
extern {
fn js_export();
fn js_import();
fn js_pass_array();
fn js_export_mut();
fn js_return_vec();
}
macro_rules! export_macro {
($(($i:ident, $n:ident))*) => ($(
#[wasm_bindgen]
pub fn $n(a: &[$i]) -> Vec<$i> {
assert_eq!(a.len(), 2);
assert_eq!(a[0], 1 as $i);
assert_eq!(a[1], 2 as $i);
a.to_vec()
}
)*)
}
export_macro! {
(i8, export_i8)
(u8, export_u8)
(i16, export_i16)
(u16, export_u16)
(i32, export_i32)
(u32, export_u32)
(f32, export_f32)
(f64, export_f64)
}
#[wasm_bindgen_test]
fn export() {
js_export();
}
macro_rules! import_macro {
($(($rust:ident, $js:ident, $i:ident))*) => ($(
#[wasm_bindgen(module = "tests/wasm/slice.js", version = "*")]
extern {
fn $js(a: &[$i]) -> Vec<$i>;
}
#[wasm_bindgen]
pub fn $rust(a: &[$i]) -> Vec<$i> {
assert_eq!(a.len(), 2);
assert_eq!(a[0], 1 as $i);
assert_eq!(a[1], 2 as $i);
$js(a)
}
)*)
}
import_macro! {
(import_rust_i8, import_js_i8, i8)
(import_rust_u8, import_js_u8, u8)
(import_rust_i16, import_js_i16, i16)
(import_rust_u16, import_js_u16, u16)
(import_rust_i32, import_js_i32, i32)
(import_rust_u32, import_js_u32, u32)
(import_rust_f32, import_js_f32, f32)
(import_rust_f64, import_js_f64, f64)
}
#[wasm_bindgen_test]
fn import() {
js_import();
}
macro_rules! pass_array_marco {
($(($rust:ident, $i:ident))*) => ($(
#[wasm_bindgen]
pub fn $rust(a: &[$i]) {
assert_eq!(a.len(), 2);
assert_eq!(a[0], 1 as $i);
assert_eq!(a[1], 2 as $i);
}
)*)
}
pass_array_marco! {
(pass_array_rust_i8, i8)
(pass_array_rust_u8, u8)
(pass_array_rust_i16, i16)
(pass_array_rust_u16, u16)
(pass_array_rust_i32, i32)
(pass_array_rust_u32, u32)
(pass_array_rust_f32, f32)
(pass_array_rust_f64, f64)
}
#[wasm_bindgen_test]
fn pass_array() {
js_pass_array();
}
macro_rules! import_mut_macro {
($(($rust:ident, $js:ident, $i:ident))*) => (
$(
#[wasm_bindgen(module = "tests/wasm/slice.js", version = "*")]
extern {
fn $js(a: &mut [$i]);
}
fn $rust() {
let mut buf = [
1 as $i,
2 as $i,
3 as $i,
];
$js(&mut buf);
assert_eq!(buf[0], 4 as $i);
assert_eq!(buf[1], 5 as $i);
assert_eq!(buf[2], 3 as $i);
}
)*
#[wasm_bindgen_test]
fn import_mut() {
$($rust();)*
}
)
}
import_mut_macro! {
(import_mut_rust_i8, import_mut_js_i8, i8)
(import_mut_rust_u8, import_mut_js_u8, u8)
(import_mut_rust_i16, import_mut_js_i16, i16)
(import_mut_rust_u16, import_mut_js_u16, u16)
(import_mut_rust_i32, import_mut_js_i32, i32)
(import_mut_rust_u32, import_mut_js_u32, u32)
(import_mut_rust_f32, import_mut_js_f32, f32)
(import_mut_rust_f64, import_mut_js_f64, f64)
}
macro_rules! export_mut_macro {
($(($i:ident, $n:ident))*) => ($(
#[wasm_bindgen]
pub fn $n(a: &mut [$i]) {
assert_eq!(a.len(), 3);
assert_eq!(a[0], 1 as $i);
assert_eq!(a[1], 2 as $i);
assert_eq!(a[2], 3 as $i);
a[0] = 4 as $i;
a[1] = 5 as $i;
}
)*)
}
export_mut_macro! {
(i8, export_mut_i8)
(u8, export_mut_u8)
(i16, export_mut_i16)
(u16, export_mut_u16)
(i32, export_mut_i32)
(u32, export_mut_u32)
(f32, export_mut_f32)
(f64, export_mut_f64)
}
#[wasm_bindgen_test]
fn export_mut() {
js_export_mut();
}
#[wasm_bindgen]
pub fn return_vec_broken_vec() -> Vec<u32> {
vec![1, 2, 3, 4, 5, 6, 7, 8, 9]
}
#[wasm_bindgen]
pub fn return_vec_web_main() -> ReturnVecApplication {
ReturnVecApplication::new()
}
#[wasm_bindgen]
pub struct ReturnVecApplication {
thing: Vec<u32>,
}
#[wasm_bindgen]
impl ReturnVecApplication {
pub fn new() -> ReturnVecApplication {
let mut thing = vec![];
thing.push(0);
thing.push(0);
thing.push(0);
thing.push(0);
thing.push(0);
ReturnVecApplication { thing }
}
pub fn tick(&mut self) {
self.thing = self.thing.clone();
}
}
#[wasm_bindgen_test]
fn return_vec() {
js_return_vec();
}

13
tests/wasm/structural.js Normal file
View File

@ -0,0 +1,13 @@
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
exports.js_works = () => {
let called = false;
wasm.run({
bar() {
called = true;
},
baz: 1,
});
assert.strictEqual(called, true);
};

32
tests/wasm/structural.rs Normal file
View File

@ -0,0 +1,32 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/structural.js", version = "*")]
extern {
fn js_works();
}
#[wasm_bindgen]
extern {
pub type Foo;
#[wasm_bindgen(method, structural)]
fn bar(this: &Foo);
#[wasm_bindgen(method, getter, structural)]
fn baz(this: &Foo) -> u32;
#[wasm_bindgen(method, setter, structural)]
fn set_baz(this: &Foo, val: u32);
}
#[wasm_bindgen]
pub fn run(a: &Foo) {
a.bar();
assert_eq!(a.baz(), 1);
a.set_baz(2);
assert_eq!(a.baz(), 2);
}
#[wasm_bindgen_test]
fn works() {
js_works();
}

37
tests/wasm/u64.js Normal file
View File

@ -0,0 +1,37 @@
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
exports.i64_js_identity = a => a;
exports.u64_js_identity = a => a;
exports.js_works = () => {
assert.strictEqual(wasm.zero(), BigInt('0'));
assert.strictEqual(wasm.one(), BigInt('1'));
assert.strictEqual(wasm.neg_one(), BigInt('-1'));
assert.strictEqual(wasm.i32_min(), BigInt('-2147483648'));
assert.strictEqual(wasm.u32_max(), BigInt('4294967295'));
assert.strictEqual(wasm.i64_min(), BigInt('-9223372036854775808'));
assert.strictEqual(wasm.u64_max(), BigInt('18446744073709551615'));
assert.strictEqual(wasm.i64_rust_identity(BigInt('0')), BigInt('0'));
assert.strictEqual(wasm.i64_rust_identity(BigInt('1')), BigInt('1'));
assert.strictEqual(wasm.i64_rust_identity(BigInt('-1')), BigInt('-1'));
assert.strictEqual(wasm.u64_rust_identity(BigInt('0')), BigInt('0'));
assert.strictEqual(wasm.u64_rust_identity(BigInt('1')), BigInt('1'));
assert.strictEqual(wasm.u64_rust_identity(BigInt('1') << BigInt('64')), BigInt('0'));
const u64_max = BigInt('18446744073709551615');
const i64_min = BigInt('-9223372036854775808');
assert.strictEqual(wasm.i64_rust_identity(i64_min), i64_min);
assert.strictEqual(wasm.u64_rust_identity(u64_max), u64_max);
assert.deepStrictEqual(wasm.u64_slice([]), new BigUint64Array());
assert.deepStrictEqual(wasm.i64_slice([]), new BigInt64Array());
const arr1 = new BigUint64Array([BigInt('1'), BigInt('2')]);
assert.deepStrictEqual(wasm.u64_slice([BigInt('1'), BigInt('2')]), arr1);
const arr2 = new BigInt64Array([BigInt('1'), BigInt('2')]);
assert.deepStrictEqual(wasm.i64_slice([BigInt('1'), BigInt('2')]), arr2);
assert.deepStrictEqual(wasm.i64_slice([i64_min]), new BigInt64Array([i64_min]));
assert.deepStrictEqual(wasm.u64_slice([u64_max]), new BigUint64Array([u64_max]));
};

47
tests/wasm/u64.rs Normal file
View File

@ -0,0 +1,47 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/u64.js", version = "*")]
extern {
fn i64_js_identity(a: i64) -> i64;
fn u64_js_identity(a: u64) -> u64;
fn js_works();
}
#[wasm_bindgen]
pub fn zero() -> u64 { 0 }
#[wasm_bindgen]
pub fn one() -> u64 { 1 }
#[wasm_bindgen]
pub fn neg_one() -> i64 { -1 }
#[wasm_bindgen]
pub fn i32_min() -> i64 { i32::min_value() as i64 }
#[wasm_bindgen]
pub fn u32_max() -> u64 { u32::max_value() as u64 }
#[wasm_bindgen]
pub fn i64_min() -> i64 { i64::min_value() }
#[wasm_bindgen]
pub fn u64_max() -> u64 { u64::max_value() }
#[wasm_bindgen]
pub fn i64_rust_identity(a: i64) -> i64 { i64_js_identity(a) }
#[wasm_bindgen]
pub fn u64_rust_identity(a: u64) -> u64 { u64_js_identity(a) }
#[wasm_bindgen]
pub fn i64_slice(a: &[i64]) -> Vec<i64> { a.to_vec() }
#[wasm_bindgen]
pub fn u64_slice(a: &[u64]) -> Vec<u64> { a.to_vec() }
#[wasm_bindgen_test]
fn works() {
js_works();
}

View File

@ -0,0 +1,21 @@
const wasm = require('wasm-bindgen-test.js');
const assert = require('assert');
const useMoved = () => {
const apple = new wasm.Fruit('apple');
apple.name();
wasm.eat(apple);
assert.throws(() => apple.name(), /Attempt to use a moved value/);
};
const moveMoved = () => {
const pear = new wasm.Fruit('pear');
pear.name();
wasm.eat(pear);
assert.throws(() => wasm.eat(pear), /Attempt to use a moved value/);
};
exports.js_works = () => {
useMoved();
moveMoved();
};

View File

@ -0,0 +1,33 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/validate_prt.js", version = "*")]
extern {
fn js_works();
}
#[wasm_bindgen]
pub struct Fruit {
name: String,
}
#[wasm_bindgen]
impl Fruit {
#[wasm_bindgen(method)]
pub fn name(&self) -> String {
self.name.clone()
}
#[wasm_bindgen(constructor)]
pub fn new(name: String) -> Self {
Fruit { name }
}
}
#[wasm_bindgen]
pub fn eat(_: Fruit) {}
#[wasm_bindgen_test]
fn works() {
js_works();
}