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
119
120
121
122
123
124
125
126
127
128
use std::os::raw::c_char;
use std::sync::LazyLock;

const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");
const VERSION_PRE: &str = concat!(env!("CARGO_PKG_VERSION_PRE"), "\0");

static VERSION_MAJOR: LazyLock<u8> = LazyLock::new(|| {
    env!("CARGO_PKG_VERSION_MAJOR")
        .parse()
        .expect("Failed to parse value for `VERSION_MAJOR` from `CARGO_PKG_VERSION_MAJOR`")
});
static VERSION_MINOR: LazyLock<u8> = LazyLock::new(|| {
    env!("CARGO_PKG_VERSION_MINOR")
        .parse()
        .expect("Failed to parse value for `VERSION_MINOR` from `CARGO_PKG_VERSION_MINOR`")
});
static VERSION_PATCH: LazyLock<u8> = LazyLock::new(|| {
    env!("CARGO_PKG_VERSION_PATCH")
        .parse()
        .expect("Failed to parse value for `VERSION_PATCH` from `CARGO_PKG_VERSION_PATCH`")
});

/// Get the version of the Wasmer C API.
///
/// The `.h` files already define variables like `WASMER_VERSION*`,
/// but if this file is unreachable, one can use this function to
/// retrieve the full semver version of the Wasmer C API.
///
/// The returned string is statically allocated. It must _not_ be
/// freed!
///
/// # Example
///
/// See the module's documentation.
#[no_mangle]
pub unsafe extern "C" fn wasmer_version() -> *const c_char {
    VERSION.as_ptr() as *const _
}

/// Get the major version of the Wasmer C API.
///
/// See [`wasmer_version`] to learn more.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// #    (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
///     // Get and print the version components.
///     uint8_t version_major = wasmer_version_major();
///     uint8_t version_minor = wasmer_version_minor();
///     uint8_t version_patch = wasmer_version_patch();
///
///     printf("%d.%d.%d", version_major, version_minor, version_patch);
///
///     return 0;
/// }
/// #    })
/// #    .success()
/// #    .stdout(
/// #         format!(
/// #             "{}.{}.{}",
/// #             env!("CARGO_PKG_VERSION_MAJOR"),
/// #             env!("CARGO_PKG_VERSION_MINOR"),
/// #             env!("CARGO_PKG_VERSION_PATCH")
/// #         )
/// #     );
/// # }
/// ```
#[no_mangle]
pub unsafe extern "C" fn wasmer_version_major() -> u8 {
    *VERSION_MAJOR
}

/// Get the minor version of the Wasmer C API.
///
/// See [`wasmer_version_major`] to learn more and get an example.  
#[no_mangle]
pub unsafe extern "C" fn wasmer_version_minor() -> u8 {
    *VERSION_MINOR
}

/// Get the patch version of the Wasmer C API.
///
/// See [`wasmer_version_major`] to learn more and get an example.  
#[no_mangle]
pub unsafe extern "C" fn wasmer_version_patch() -> u8 {
    *VERSION_PATCH
}

/// Get the minor version of the Wasmer C API.
///
/// See [`wasmer_version_major`] to learn more.
///
/// The returned string is statically allocated. It must _not_ be
/// freed!
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// #    (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
///     // Get and print the pre version.
///     const char* version_pre = wasmer_version_pre();
///     printf("%s", version_pre);
///
///     // No need to free the string. It's statically allocated on
///     // the Rust side.
///
///     return 0;
/// }
/// #    })
/// #    .success()
/// #    .stdout(env!("CARGO_PKG_VERSION_PRE"));
/// # }
/// ```
#[no_mangle]
pub unsafe extern "C" fn wasmer_version_pre() -> *const c_char {
    VERSION_PRE.as_ptr() as *const _
}