summaryrefslogtreecommitdiffstats
path: root/vendor/wasm-bindgen/tests/wasm/bigint.rs
blob: 30eb5c5cf7d25dcaa2541910933f316959c55541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "tests/wasm/bigint.js")]
extern "C" {
    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_jsvalue_identity(a: i64) -> JsValue {
    JsValue::from(a)
}

#[wasm_bindgen]
pub fn u64_jsvalue_identity(a: u64) -> JsValue {
    JsValue::from(a)
}

#[wasm_bindgen]
pub fn i128_min_jsvalue() -> JsValue {
    JsValue::from(i128::min_value())
}

#[wasm_bindgen]
pub fn u128_max_jsvalue() -> JsValue {
    JsValue::from(u128::max_value())
}

#[wasm_bindgen]
pub fn i64_slice(a: &[i64]) -> Vec<i64> {
    a.to_vec()
}

#[wasm_bindgen]
pub fn u64_slice(a: &[u64]) -> Vec<u64> {
    a.to_vec()
}

#[wasm_bindgen_test]
fn works() {
    js_works();
}

mod try_from_works {
    use super::*;
    use crate::JsValue;
    use core::convert::TryFrom;

    macro_rules! test_type_boundaries {
        ($($ty:ident)*) => {
            $(
                #[wasm_bindgen_test]
                fn $ty() {
                    // Not a bigint.
                    assert!($ty::try_from(JsValue::NULL).is_err());
                    assert!($ty::try_from(JsValue::from_f64(0.0)).is_err());
                    // Within range.
                    assert_eq!($ty::try_from(JsValue::from($ty::MIN)), Ok($ty::MIN));
                    // Too small.
                    assert!($ty::try_from(JsValue::from($ty::MIN) - JsValue::from(1_i64)).is_err());
                    // Within range.
                    assert_eq!($ty::try_from(JsValue::from($ty::MAX)), Ok($ty::MAX));
                    // Too large.
                    assert!($ty::try_from(JsValue::from($ty::MAX) + JsValue::from(1_i64)).is_err());
                }
            )*
        };
    }

    test_type_boundaries!(i64 u64 i128 u128);
}