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
use super::types::wasm_byte_vec_t;

/// Parses in-memory bytes as either the WAT format, or a binary Wasm
/// module. This is wasmer-specific.
///
/// In case of failure, `wat2wasm` sets the `out->data = NULL` and `out->size = 0`.
///
/// # Example
///
/// See the module's documentation.
///
/// # Safety
/// This function is unsafe in order to be callable from C.
#[cfg(feature = "wat")]
#[no_mangle]
pub unsafe extern "C" fn wat2wasm(wat: &wasm_byte_vec_t, out: &mut wasm_byte_vec_t) {
    match wasmer_api::wat2wasm(wat.as_slice()) {
        Ok(val) => out.set_buffer(val.into_owned()),
        Err(err) => {
            crate::error::update_last_error(err);
            out.data = std::ptr::null_mut();
            out.size = 0;
        }
    };
}

#[cfg(test)]
mod tests {
    #[cfg(not(target_os = "windows"))]
    use inline_c::assert_c;
    #[cfg(target_os = "windows")]
    use wasmer_inline_c::assert_c;

    #[test]
    fn test_wat2wasm() {
        (assert_c! {
            #include "tests/wasmer.h"

            int main() {
                wasm_byte_vec_t wat;
                wasmer_byte_vec_new_from_string(&wat, "(module)");
                wasm_byte_vec_t wasm;
                wat2wasm(&wat, &wasm);

                assert(wasm.data);
                assert(wasm.size == 8);
                assert(
                    wasm.data[0] == 0 &&
                        wasm.data[1] == 'a' &&
                        wasm.data[2] == 's' &&
                        wasm.data[3] == 'm' &&
                        wasm.data[4] == 1 &&
                        wasm.data[5] == 0 &&
                        wasm.data[6] == 0 &&
                        wasm.data[7] == 0
                );

                wasm_byte_vec_delete(&wasm);
                wasm_byte_vec_delete(&wat);

                return 0;
            }
        })
        .success();
    }

    #[test]
    fn test_wat2wasm_failed() {
        (assert_c! {
            #include "tests/wasmer.h"

            int main() {
                wasm_byte_vec_t wat;
                wasmer_byte_vec_new_from_string(&wat, "(module");
                wasm_byte_vec_t wasm;
                wat2wasm(&wat, &wasm);

                assert(!wasm.data);
                assert(wasmer_last_error_length() > 0);

                wasm_byte_vec_delete(&wat);

                return 0;
            }
        })
        .success();
    }
}