From 654bb9b6832c46e90255274fcbcbf344e0b9581f Mon Sep 17 00:00:00 2001 From: Anton Danilkin Date: Sun, 5 Aug 2018 05:00:16 +0300 Subject: [PATCH] Port tests that use only basic features --- tests/all/closures.rs | 573 ------------------------------ tests/all/duplicates.rs | 78 ---- tests/all/enums.rs | 103 ------ tests/all/js_objects.rs | 94 +++++ tests/all/jsobjects.rs | 375 ------------------- tests/all/main.rs | 10 +- tests/all/math.rs | 82 ----- tests/all/slice.rs | 526 --------------------------- tests/all/structural.rs | 52 --- tests/all/u64.rs | 106 ------ tests/all/validate_prt.rs | 75 ---- tests/wasm/api.js | 72 ++-- tests/wasm/api.rs | 12 +- tests/wasm/char.js | 32 +- tests/wasm/char.rs | 40 ++- tests/wasm/classes.js | 188 +++++----- tests/wasm/classes.rs | 76 ++-- tests/wasm/closures.js | 93 +++++ tests/wasm/closures.rs | 209 +++++++++++ tests/wasm/duplicates.rs | 49 +++ tests/wasm/duplicates_a.js | 1 + tests/wasm/duplicates_b.js | 1 + tests/wasm/duplicates_c.js | 1 + tests/wasm/enums.js | 20 ++ tests/wasm/enums.rs | 55 +++ tests/wasm/js_objects.js | 84 +++++ tests/wasm/js_objects.rs | 107 ++++++ tests/wasm/main.rs | 9 + tests/wasm/math.js | 5 + tests/wasm/math.rs | 67 ++++ tests/wasm/option.js | 39 +- tests/wasm/optional_primitives.js | 172 ++++----- tests/wasm/optional_primitives.rs | 399 ++++++++++++--------- tests/wasm/slice.js | 206 +++++++++++ tests/wasm/slice.rs | 210 +++++++++++ tests/wasm/structural.js | 13 + tests/wasm/structural.rs | 32 ++ tests/wasm/u64.js | 37 ++ tests/wasm/u64.rs | 47 +++ tests/wasm/validate_prt.js | 21 ++ tests/wasm/validate_prt.rs | 33 ++ 41 files changed, 1938 insertions(+), 2466 deletions(-) delete mode 100644 tests/all/closures.rs delete mode 100644 tests/all/duplicates.rs delete mode 100644 tests/all/enums.rs create mode 100644 tests/all/js_objects.rs delete mode 100644 tests/all/jsobjects.rs delete mode 100644 tests/all/math.rs delete mode 100644 tests/all/slice.rs delete mode 100644 tests/all/structural.rs delete mode 100644 tests/all/u64.rs delete mode 100644 tests/all/validate_prt.rs create mode 100644 tests/wasm/closures.js create mode 100644 tests/wasm/closures.rs create mode 100644 tests/wasm/duplicates.rs create mode 100644 tests/wasm/duplicates_a.js create mode 100644 tests/wasm/duplicates_b.js create mode 100644 tests/wasm/duplicates_c.js create mode 100644 tests/wasm/enums.js create mode 100644 tests/wasm/enums.rs create mode 100644 tests/wasm/js_objects.js create mode 100644 tests/wasm/js_objects.rs create mode 100644 tests/wasm/math.js create mode 100644 tests/wasm/math.rs create mode 100644 tests/wasm/slice.js create mode 100644 tests/wasm/slice.rs create mode 100644 tests/wasm/structural.js create mode 100644 tests/wasm/structural.rs create mode 100644 tests/wasm/u64.js create mode 100644 tests/wasm/u64.rs create mode 100644 tests/wasm/validate_prt.js create mode 100644 tests/wasm/validate_prt.rs diff --git a/tests/all/closures.rs b/tests/all/closures.rs deleted file mode 100644 index 36a16d4b..00000000 --- a/tests/all/closures.rs +++ /dev/null @@ -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 call2(a: &Closure 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 call2(a: &Closure); - fn call3(a: &Closure); - fn call4(a: &Closure); - fn call5(a: &Closure); - fn call6(a: &Closure); - fn call7(a: &Closure); - fn call8(a: &Closure); - - #[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); - #[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); - #[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(); -} diff --git a/tests/all/duplicates.rs b/tests/all/duplicates.rs deleted file mode 100644 index d70f042b..00000000 --- a/tests/all/duplicates.rs +++ /dev/null @@ -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(); -} diff --git a/tests/all/enums.rs b/tests/all/enums.rs deleted file mode 100644 index d4e66270..00000000 --- a/tests/all/enums.rs +++ /dev/null @@ -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(); -} diff --git a/tests/all/js_objects.rs b/tests/all/js_objects.rs new file mode 100644 index 00000000..5bee2819 --- /dev/null +++ b/tests/all/js_objects.rs @@ -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, + 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::().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::().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(); +} diff --git a/tests/all/jsobjects.rs b/tests/all/jsobjects.rs deleted file mode 100644 index c7c1e476..00000000 --- a/tests/all/jsobjects.rs +++ /dev/null @@ -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 { - 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 { - 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, - 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::().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::().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(); -} diff --git a/tests/all/main.rs b/tests/all/main.rs index 3d3acf20..74488206 100644 --- a/tests/all/main.rs +++ b/tests/all/main.rs @@ -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; diff --git a/tests/all/math.rs b/tests/all/math.rs deleted file mode 100644 index 7c247c95..00000000 --- a/tests/all/math.rs +++ /dev/null @@ -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(); -} diff --git a/tests/all/slice.rs b/tests/all/slice.rs deleted file mode 100644 index 62c4bda4..00000000 --- a/tests/all/slice.rs +++ /dev/null @@ -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 { - 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, - } - - #[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(); -} diff --git a/tests/all/structural.rs b/tests/all/structural.rs deleted file mode 100644 index b5f75350..00000000 --- a/tests/all/structural.rs +++ /dev/null @@ -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(); -} diff --git a/tests/all/u64.rs b/tests/all/u64.rs deleted file mode 100644 index bff0dd78..00000000 --- a/tests/all/u64.rs +++ /dev/null @@ -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 { a.to_vec() } - - #[wasm_bindgen] - pub fn u64_slice(a: &[u64]) -> Vec { 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(); -} diff --git a/tests/all/validate_prt.rs b/tests/all/validate_prt.rs deleted file mode 100644 index b0c6557b..00000000 --- a/tests/all/validate_prt.rs +++ /dev/null @@ -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(); -} diff --git a/tests/wasm/api.js b/tests/wasm/api.js index 5f723f71..7136b5db 100644 --- a/tests/wasm/api.js +++ b/tests/wasm/api.js @@ -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); }; diff --git a/tests/wasm/api.rs b/tests/wasm/api.rs index 0b6ab5e1..f7be28ea 100644 --- a/tests/wasm/api.rs +++ b/tests/wasm/api.rs @@ -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] diff --git a/tests/wasm/char.js b/tests/wasm/char.js index 314044fb..d145333e 100644 --- a/tests/wasm/char.js +++ b/tests/wasm/char.js @@ -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; }; diff --git a/tests/wasm/char.rs b/tests/wasm/char.rs index 57b5e565..328151a8 100644 --- a/tests/wasm/char.rs +++ b/tests/wasm/char.rs @@ -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) } diff --git a/tests/wasm/classes.js b/tests/wasm/classes.js index cb92484b..6764a0a8 100644 --- a/tests/wasm/classes.js +++ b/tests/wasm/classes.js @@ -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(); }; diff --git a/tests/wasm/classes.rs b/tests/wasm/classes.rs index c4b1aa57..ae4bd2e2 100644 --- a/tests/wasm/classes.rs +++ b/tests/wasm/classes.rs @@ -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)] diff --git a/tests/wasm/closures.js b/tests/wasm/closures.js new file mode 100644 index 00000000..afdac0e8 --- /dev/null +++ b/tests/wasm/closures.js @@ -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'); +}; \ No newline at end of file diff --git a/tests/wasm/closures.rs b/tests/wasm/closures.rs new file mode 100644 index 00000000..2c926866 --- /dev/null +++ b/tests/wasm/closures.rs @@ -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 long_lived_call2(a: &Closure u32>) -> u32; + + fn many_arity_call1(a: &Closure); + fn many_arity_call2(a: &Closure); + fn many_arity_call3(a: &Closure); + fn many_arity_call4(a: &Closure); + fn many_arity_call5(a: &Closure); + fn many_arity_call6(a: &Closure); + fn many_arity_call7(a: &Closure); + fn many_arity_call8(a: &Closure); + #[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); + #[wasm_bindgen(catch)] + fn long_lived_dropping_call() -> Result<(), JsValue>; + + fn long_fnmut_recursive_cache(a: &Closure); + #[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); +} diff --git a/tests/wasm/duplicates.rs b/tests/wasm/duplicates.rs new file mode 100644 index 00000000..c0beadad --- /dev/null +++ b/tests/wasm/duplicates.rs @@ -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()); +} diff --git a/tests/wasm/duplicates_a.js b/tests/wasm/duplicates_a.js new file mode 100644 index 00000000..ee5ebb32 --- /dev/null +++ b/tests/wasm/duplicates_a.js @@ -0,0 +1 @@ +exports.foo = () => {}; diff --git a/tests/wasm/duplicates_b.js b/tests/wasm/duplicates_b.js new file mode 100644 index 00000000..00128ede --- /dev/null +++ b/tests/wasm/duplicates_b.js @@ -0,0 +1 @@ +exports.foo = () => true; diff --git a/tests/wasm/duplicates_c.js b/tests/wasm/duplicates_c.js new file mode 100644 index 00000000..90ca9240 --- /dev/null +++ b/tests/wasm/duplicates_c.js @@ -0,0 +1 @@ +exports.foo = () => false; diff --git a/tests/wasm/enums.js b/tests/wasm/enums.js new file mode 100644 index 00000000..ae66eb4d --- /dev/null +++ b/tests/wasm/enums.js @@ -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); +}; diff --git a/tests/wasm/enums.rs b/tests/wasm/enums.rs new file mode 100644 index 00000000..f49a18b9 --- /dev/null +++ b/tests/wasm/enums.rs @@ -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(); +} diff --git a/tests/wasm/js_objects.js b/tests/wasm/js_objects.js new file mode 100644 index 00000000..f53b84d7 --- /dev/null +++ b/tests/wasm/js_objects.js @@ -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]); +}; diff --git a/tests/wasm/js_objects.rs b/tests/wasm/js_objects.rs new file mode 100644 index 00000000..6b162410 --- /dev/null +++ b/tests/wasm/js_objects.rs @@ -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 { + 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 { + 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(); +} diff --git a/tests/wasm/main.rs b/tests/wasm/main.rs index 204c09e0..3e6f6552 100644 --- a/tests/wasm/main.rs +++ b/tests/wasm/main.rs @@ -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; diff --git a/tests/wasm/math.js b/tests/wasm/math.js new file mode 100644 index 00000000..c4865494 --- /dev/null +++ b/tests/wasm/math.js @@ -0,0 +1,5 @@ +const wasm = require('wasm-bindgen-test.js'); + +exports.js_auto_bind_math = () => { + wasm.math(1.0, 2.0); +}; diff --git a/tests/wasm/math.rs b/tests/wasm/math.rs new file mode 100644 index 00000000..47ea5ce2 --- /dev/null +++ b/tests/wasm/math.rs @@ -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(); +} \ No newline at end of file diff --git a/tests/wasm/option.js b/tests/wasm/option.js index ce7bdeff..e9162feb 100644 --- a/tests/wasm/option.js +++ b/tests/wasm/option.js @@ -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); }; diff --git a/tests/wasm/optional_primitives.js b/tests/wasm/optional_primitives.js index 4f39209b..89bf4df7 100644 --- a/tests/wasm/optional_primitives.js +++ b/tests/wasm/optional_primitives.js @@ -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; }; diff --git a/tests/wasm/optional_primitives.rs b/tests/wasm/optional_primitives.rs index 2bc279e3..57a4bd1e 100644 --- a/tests/wasm/optional_primitives.rs +++ b/tests/wasm/optional_primitives.rs @@ -3,205 +3,274 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen(module = "tests/wasm/optional_primitives.js", version = "*")] extern { - fn i32_js_identity(a: Option) -> Option; - fn u32_js_identity(a: Option) -> Option; - fn isize_js_identity(a: Option) -> Option; - fn usize_js_identity(a: Option) -> Option; - fn f32_js_identity(a: Option) -> Option; - fn f64_js_identity(a: Option) -> Option; - fn i8_js_identity(a: Option) -> Option; - fn u8_js_identity(a: Option) -> Option; - fn i16_js_identity(a: Option) -> Option; - fn u16_js_identity(a: Option) -> Option; - fn i64_js_identity(a: Option) -> Option; - fn u64_js_identity(a: Option) -> Option; - fn bool_js_identity(a: Option) -> Option; - fn char_js_identity(a: Option) -> Option; + fn optional_i32_js_identity(a: Option) -> Option; + fn optional_u32_js_identity(a: Option) -> Option; + fn optional_isize_js_identity(a: Option) -> Option; + fn optional_usize_js_identity(a: Option) -> Option; + fn optional_f32_js_identity(a: Option) -> Option; + fn optional_f64_js_identity(a: Option) -> Option; + fn optional_i8_js_identity(a: Option) -> Option; + fn optional_u8_js_identity(a: Option) -> Option; + fn optional_i16_js_identity(a: Option) -> Option; + fn optional_u16_js_identity(a: Option) -> Option; + fn optional_i64_js_identity(a: Option) -> Option; + fn optional_u64_js_identity(a: Option) -> Option; + fn optional_bool_js_identity(a: Option) -> Option; + fn optional_char_js_identity(a: Option) -> Option; - fn test_works(); + fn js_works(); } #[wasm_bindgen] -pub fn i32_none() -> Option { None } -#[wasm_bindgen] -pub fn i32_zero() -> Option { Some(0) } -#[wasm_bindgen] -pub fn i32_one() -> Option { Some(1) } -#[wasm_bindgen] -pub fn i32_neg_one() -> Option { Some(-1) } -#[wasm_bindgen] -pub fn i32_max() -> Option { Some(i32::max_value()) } -#[wasm_bindgen] -pub fn i32_min() -> Option { Some(i32::min_value()) } -#[wasm_bindgen] -pub fn i32_identity(a: Option) -> Option { i32_js_identity(a) } +pub fn optional_i32_none() -> Option { None } #[wasm_bindgen] -pub fn u32_none() -> Option { None } -#[wasm_bindgen] -pub fn u32_zero() -> Option { Some(0) } -#[wasm_bindgen] -pub fn u32_one() -> Option { Some(1) } -#[wasm_bindgen] -pub fn u32_max() -> Option { Some(u32::max_value()) } -#[wasm_bindgen] -pub fn u32_min() -> Option { Some(u32::min_value()) } -#[wasm_bindgen] -pub fn u32_identity(a: Option) -> Option { u32_js_identity(a) } +pub fn optional_i32_zero() -> Option { Some(0) } #[wasm_bindgen] -pub fn isize_none() -> Option { None } -#[wasm_bindgen] -pub fn isize_zero() -> Option { Some(0) } -#[wasm_bindgen] -pub fn isize_one() -> Option { Some(1) } -#[wasm_bindgen] -pub fn isize_neg_one() -> Option { Some(-1) } -#[wasm_bindgen] -pub fn isize_max() -> Option { Some(isize::max_value()) } -#[wasm_bindgen] -pub fn isize_min() -> Option { Some(isize::min_value()) } -#[wasm_bindgen] -pub fn isize_identity(a: Option) -> Option { isize_js_identity(a) } +pub fn optional_i32_one() -> Option { Some(1) } #[wasm_bindgen] -pub fn usize_none() -> Option { None } -#[wasm_bindgen] -pub fn usize_zero() -> Option { Some(0) } -#[wasm_bindgen] -pub fn usize_one() -> Option { Some(1) } -#[wasm_bindgen] -pub fn usize_max() -> Option { Some(usize::max_value()) } -#[wasm_bindgen] -pub fn usize_min() -> Option { Some(usize::min_value()) } -#[wasm_bindgen] -pub fn usize_identity(a: Option) -> Option { usize_js_identity(a) } +pub fn optional_i32_neg_one() -> Option { Some(-1) } #[wasm_bindgen] -pub fn f32_none() -> Option { None } -#[wasm_bindgen] -pub fn f32_zero() -> Option { Some(0f32) } -#[wasm_bindgen] -pub fn f32_one() -> Option { Some(1f32) } -#[wasm_bindgen] -pub fn f32_neg_one() -> Option { Some(-1f32) } -#[wasm_bindgen] -pub fn f32_identity(a: Option) -> Option { f32_js_identity(a) } +pub fn optional_i32_min() -> Option { Some(i32::min_value()) } #[wasm_bindgen] -pub fn f64_none() -> Option { None } -#[wasm_bindgen] -pub fn f64_zero() -> Option { Some(0f64) } -#[wasm_bindgen] -pub fn f64_one() -> Option { Some(1f64) } -#[wasm_bindgen] -pub fn f64_neg_one() -> Option { Some(-1f64) } -#[wasm_bindgen] -pub fn f64_identity(a: Option) -> Option { f64_js_identity(a) } +pub fn optional_i32_max() -> Option { Some(i32::max_value()) } #[wasm_bindgen] -pub fn i8_none() -> Option { None } -#[wasm_bindgen] -pub fn i8_zero() -> Option { Some(0) } -#[wasm_bindgen] -pub fn i8_one() -> Option { Some(1) } -#[wasm_bindgen] -pub fn i8_neg_one() -> Option { Some(-1) } -#[wasm_bindgen] -pub fn i8_max() -> Option { Some(i8::max_value()) } -#[wasm_bindgen] -pub fn i8_min() -> Option { Some(i8::min_value()) } -#[wasm_bindgen] -pub fn i8_identity(a: Option) -> Option { i8_js_identity(a) } +pub fn optional_i32_identity(a: Option) -> Option { optional_i32_js_identity(a) } #[wasm_bindgen] -pub fn u8_none() -> Option { None } -#[wasm_bindgen] -pub fn u8_zero() -> Option { Some(0) } -#[wasm_bindgen] -pub fn u8_one() -> Option { Some(1) } -#[wasm_bindgen] -pub fn u8_max() -> Option { Some(u8::max_value()) } -#[wasm_bindgen] -pub fn u8_min() -> Option { Some(u8::min_value()) } -#[wasm_bindgen] -pub fn u8_identity(a: Option) -> Option { u8_js_identity(a) } +pub fn optional_u32_none() -> Option { None } #[wasm_bindgen] -pub fn i16_none() -> Option { None } -#[wasm_bindgen] -pub fn i16_zero() -> Option { Some(0) } -#[wasm_bindgen] -pub fn i16_one() -> Option { Some(1) } -#[wasm_bindgen] -pub fn i16_neg_one() -> Option { Some(-1) } -#[wasm_bindgen] -pub fn i16_max() -> Option { Some(i16::max_value()) } -#[wasm_bindgen] -pub fn i16_min() -> Option { Some(i16::min_value()) } -#[wasm_bindgen] -pub fn i16_identity(a: Option) -> Option { i16_js_identity(a) } +pub fn optional_u32_zero() -> Option { Some(0) } #[wasm_bindgen] -pub fn u16_none() -> Option { None } -#[wasm_bindgen] -pub fn u16_zero() -> Option { Some(0) } -#[wasm_bindgen] -pub fn u16_one() -> Option { Some(1) } -#[wasm_bindgen] -pub fn u16_max() -> Option { Some(u16::max_value()) } -#[wasm_bindgen] -pub fn u16_min() -> Option { Some(u16::min_value()) } -#[wasm_bindgen] -pub fn u16_identity(a: Option) -> Option { u16_js_identity(a) } +pub fn optional_u32_one() -> Option { Some(1) } #[wasm_bindgen] -pub fn i64_none() -> Option { None } -#[wasm_bindgen] -pub fn i64_zero() -> Option { Some(0) } -#[wasm_bindgen] -pub fn i64_one() -> Option { Some(1) } -#[wasm_bindgen] -pub fn i64_neg_one() -> Option { Some(-1) } -#[wasm_bindgen] -pub fn i64_max() -> Option { Some(i64::max_value()) } -#[wasm_bindgen] -pub fn i64_min() -> Option { Some(i64::min_value()) } -#[wasm_bindgen] -pub fn i64_identity(a: Option) -> Option { i64_js_identity(a) } +pub fn optional_u32_min() -> Option { Some(u32::min_value()) } #[wasm_bindgen] -pub fn u64_none() -> Option { None } -#[wasm_bindgen] -pub fn u64_zero() -> Option { Some(0) } -#[wasm_bindgen] -pub fn u64_one() -> Option { Some(1) } -#[wasm_bindgen] -pub fn u64_max() -> Option { Some(u64::max_value()) } -#[wasm_bindgen] -pub fn u64_min() -> Option { Some(u64::min_value()) } -#[wasm_bindgen] -pub fn u64_identity(a: Option) -> Option { u64_js_identity(a) } +pub fn optional_u32_max() -> Option { Some(u32::max_value()) } #[wasm_bindgen] -pub fn bool_none() -> Option { None } -#[wasm_bindgen] -pub fn bool_false() -> Option { Some(false) } -#[wasm_bindgen] -pub fn bool_true() -> Option { Some(true) } -#[wasm_bindgen] -pub fn bool_identity(a: Option) -> Option { bool_js_identity(a) } +pub fn optional_u32_identity(a: Option) -> Option { optional_u32_js_identity(a) } #[wasm_bindgen] -pub fn char_none() -> Option { None } +pub fn optional_isize_none() -> Option { None } + #[wasm_bindgen] -pub fn char_letter() -> Option { Some('a') } +pub fn optional_isize_zero() -> Option { Some(0) } + #[wasm_bindgen] -pub fn char_face() -> Option { Some('😀') } +pub fn optional_isize_one() -> Option { Some(1) } + #[wasm_bindgen] -pub fn char_identity(a: Option) -> Option { char_js_identity(a) } +pub fn optional_isize_neg_one() -> Option { Some(-1) } + +#[wasm_bindgen] +pub fn optional_isize_min() -> Option { Some(isize::min_value()) } + +#[wasm_bindgen] +pub fn optional_isize_max() -> Option { Some(isize::max_value()) } + +#[wasm_bindgen] +pub fn optional_isize_identity(a: Option) -> Option { optional_isize_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_usize_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_usize_zero() -> Option { Some(0) } + +#[wasm_bindgen] +pub fn optional_usize_one() -> Option { Some(1) } + +#[wasm_bindgen] +pub fn optional_usize_min() -> Option { Some(usize::min_value()) } + +#[wasm_bindgen] +pub fn optional_usize_max() -> Option { Some(usize::max_value()) } + +#[wasm_bindgen] +pub fn optional_usize_identity(a: Option) -> Option { optional_usize_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_f32_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_f32_zero() -> Option { Some(0f32) } + +#[wasm_bindgen] +pub fn optional_f32_one() -> Option { Some(1f32) } + +#[wasm_bindgen] +pub fn optional_f32_neg_one() -> Option { Some(-1f32) } + +#[wasm_bindgen] +pub fn optional_f32_identity(a: Option) -> Option { optional_f32_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_f64_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_f64_zero() -> Option { Some(0f64) } + +#[wasm_bindgen] +pub fn optional_f64_one() -> Option { Some(1f64) } + +#[wasm_bindgen] +pub fn optional_f64_neg_one() -> Option { Some(-1f64) } + +#[wasm_bindgen] +pub fn optional_f64_identity(a: Option) -> Option { optional_f64_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_i8_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_i8_zero() -> Option { Some(0) } + +#[wasm_bindgen] +pub fn optional_i8_one() -> Option { Some(1) } + +#[wasm_bindgen] +pub fn optional_i8_neg_one() -> Option { Some(-1) } + +#[wasm_bindgen] +pub fn optional_i8_min() -> Option { Some(i8::min_value()) } + +#[wasm_bindgen] +pub fn optional_i8_max() -> Option { Some(i8::max_value()) } + +#[wasm_bindgen] +pub fn optional_i8_identity(a: Option) -> Option { optional_i8_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_u8_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_u8_zero() -> Option { Some(0) } + +#[wasm_bindgen] +pub fn optional_u8_one() -> Option { Some(1) } + +#[wasm_bindgen] +pub fn optional_u8_min() -> Option { Some(u8::min_value()) } + +#[wasm_bindgen] +pub fn optional_u8_max() -> Option { Some(u8::max_value()) } + +#[wasm_bindgen] +pub fn optional_u8_identity(a: Option) -> Option { optional_u8_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_i16_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_i16_zero() -> Option { Some(0) } + +#[wasm_bindgen] +pub fn optional_i16_one() -> Option { Some(1) } + +#[wasm_bindgen] +pub fn optional_i16_neg_one() -> Option { Some(-1) } + +#[wasm_bindgen] +pub fn optional_i16_min() -> Option { Some(i16::min_value()) } + +#[wasm_bindgen] +pub fn optional_i16_max() -> Option { Some(i16::max_value()) } + +#[wasm_bindgen] +pub fn optional_i16_identity(a: Option) -> Option { optional_i16_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_u16_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_u16_zero() -> Option { Some(0) } + +#[wasm_bindgen] +pub fn optional_u16_one() -> Option { Some(1) } + +#[wasm_bindgen] +pub fn optional_u16_min() -> Option { Some(u16::min_value()) } + +#[wasm_bindgen] +pub fn optional_u16_max() -> Option { Some(u16::max_value()) } + +#[wasm_bindgen] +pub fn optional_u16_identity(a: Option) -> Option { optional_u16_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_i64_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_i64_zero() -> Option { Some(0) } + +#[wasm_bindgen] +pub fn optional_i64_one() -> Option { Some(1) } + +#[wasm_bindgen] +pub fn optional_i64_neg_one() -> Option { Some(-1) } + +#[wasm_bindgen] +pub fn optional_i64_min() -> Option { Some(i64::min_value()) } + +#[wasm_bindgen] +pub fn optional_i64_max() -> Option { Some(i64::max_value()) } + +#[wasm_bindgen] +pub fn optional_i64_identity(a: Option) -> Option { optional_i64_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_u64_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_u64_zero() -> Option { Some(0) } + +#[wasm_bindgen] +pub fn optional_u64_one() -> Option { Some(1) } + +#[wasm_bindgen] +pub fn optional_u64_min() -> Option { Some(u64::min_value()) } + +#[wasm_bindgen] +pub fn optional_u64_max() -> Option { Some(u64::max_value()) } + +#[wasm_bindgen] +pub fn optional_u64_identity(a: Option) -> Option { optional_u64_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_bool_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_bool_false() -> Option { Some(false) } + +#[wasm_bindgen] +pub fn optional_bool_true() -> Option { Some(true) } + +#[wasm_bindgen] +pub fn optional_bool_identity(a: Option) -> Option { optional_bool_js_identity(a) } + +#[wasm_bindgen] +pub fn optional_char_none() -> Option { None } + +#[wasm_bindgen] +pub fn optional_char_letter() -> Option { Some('a') } + +#[wasm_bindgen] +pub fn optional_char_face() -> Option { Some('😀') } + +#[wasm_bindgen] +pub fn optional_char_identity(a: Option) -> Option { optional_char_js_identity(a) } #[wasm_bindgen_test] fn works() { - test_works(); + js_works(); } diff --git a/tests/wasm/slice.js b/tests/wasm/slice.js new file mode 100644 index 00000000..b6ceaa28 --- /dev/null +++ b/tests/wasm/slice.js @@ -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); + } +}; diff --git a/tests/wasm/slice.rs b/tests/wasm/slice.rs new file mode 100644 index 00000000..653ec01c --- /dev/null +++ b/tests/wasm/slice.rs @@ -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 { + 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, +} + +#[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(); +} diff --git a/tests/wasm/structural.js b/tests/wasm/structural.js new file mode 100644 index 00000000..0261c50e --- /dev/null +++ b/tests/wasm/structural.js @@ -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); +}; diff --git a/tests/wasm/structural.rs b/tests/wasm/structural.rs new file mode 100644 index 00000000..92a13fe2 --- /dev/null +++ b/tests/wasm/structural.rs @@ -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(); +} diff --git a/tests/wasm/u64.js b/tests/wasm/u64.js new file mode 100644 index 00000000..5555e065 --- /dev/null +++ b/tests/wasm/u64.js @@ -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])); +}; diff --git a/tests/wasm/u64.rs b/tests/wasm/u64.rs new file mode 100644 index 00000000..d5d60760 --- /dev/null +++ b/tests/wasm/u64.rs @@ -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 { a.to_vec() } + +#[wasm_bindgen] +pub fn u64_slice(a: &[u64]) -> Vec { a.to_vec() } + +#[wasm_bindgen_test] +fn works() { + js_works(); +} diff --git a/tests/wasm/validate_prt.js b/tests/wasm/validate_prt.js new file mode 100644 index 00000000..4b8a3e73 --- /dev/null +++ b/tests/wasm/validate_prt.js @@ -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(); +}; diff --git a/tests/wasm/validate_prt.rs b/tests/wasm/validate_prt.rs new file mode 100644 index 00000000..f6446fde --- /dev/null +++ b/tests/wasm/validate_prt.rs @@ -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(); +}