summaryrefslogtreecommitdiffstats
path: root/tests/run-make/wasm-abi/foo.rs
blob: 0678eb3ff51ae57b1aa1aed9cd506495d5c8fe42 (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
#![crate_type = "cdylib"]
#![deny(warnings)]
#![feature(wasm_abi)]

#[repr(C)]
#[derive(PartialEq, Debug)]
pub struct TwoI32 {
    pub a: i32,
    pub b: i32,
}

#[no_mangle]
pub extern "wasm" fn return_two_i32() -> TwoI32 {
    TwoI32 { a: 1, b: 2 }
}

#[repr(C)]
#[derive(PartialEq, Debug)]
pub struct TwoI64 {
    pub a: i64,
    pub b: i64,
}

#[no_mangle]
pub extern "wasm" fn return_two_i64() -> TwoI64 {
    TwoI64 { a: 3, b: 4 }
}

#[repr(C)]
#[derive(PartialEq, Debug)]
pub struct TwoF32 {
    pub a: f32,
    pub b: f32,
}

#[no_mangle]
pub extern "wasm" fn return_two_f32() -> TwoF32 {
    TwoF32 { a: 5., b: 6. }
}

#[repr(C)]
#[derive(PartialEq, Debug)]
pub struct TwoF64 {
    pub a: f64,
    pub b: f64,
}

#[no_mangle]
pub extern "wasm" fn return_two_f64() -> TwoF64 {
    TwoF64 { a: 7., b: 8. }
}

#[repr(C)]
#[derive(PartialEq, Debug)]
pub struct Mishmash {
    pub a: f64,
    pub b: f32,
    pub c: i32,
    pub d: i64,
    pub e: TwoI32,
}

#[no_mangle]
pub extern "wasm" fn return_mishmash() -> Mishmash {
    Mishmash { a: 9., b: 10., c: 11, d: 12, e: TwoI32 { a: 13, b: 14 } }
}

#[link(wasm_import_module = "host")]
extern "wasm" {
    fn two_i32() -> TwoI32;
    fn two_i64() -> TwoI64;
    fn two_f32() -> TwoF32;
    fn two_f64() -> TwoF64;
    fn mishmash() -> Mishmash;
}

#[no_mangle]
pub unsafe extern "C" fn call_imports() {
    assert_eq!(two_i32(), TwoI32 { a: 100, b: 101 });
    assert_eq!(two_i64(), TwoI64 { a: 102, b: 103 });
    assert_eq!(two_f32(), TwoF32 { a: 104., b: 105. });
    assert_eq!(two_f64(), TwoF64 { a: 106., b: 107. });
    assert_eq!(
        mishmash(),
        Mishmash { a: 108., b: 109., c: 110, d: 111, e: TwoI32 { a: 112, b: 113 } }
    );
}