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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
pub use super::unstable::engine::wasm_config_set_features;
use super::unstable::features::wasmer_features_t;
use wasmer_api::{BackendKind, Engine};
mod config;
#[allow(unused_imports)]
pub use config::*;
/// Kind of engines that can be used by the store.
///
/// This is a Wasmer-specific type with Wasmer-specific functions for
/// manipulating it.
#[derive(Debug, Copy, Clone)]
#[repr(C)]
#[allow(non_camel_case_types)]
pub enum wasmer_backend_t {
/// The cranelift backend.
CRANELIFT = 0,
/// The LLVM backend.
LLVM,
/// The singlepass backend.
SINGLEPASS,
/// The headless backend.
HEADLESS,
/// The V8 backend.
V8,
/// The WASMI backend.
WASMI,
/// The WAMR backend.
WAMR,
/// The JSC backend.
JSC,
}
impl From<BackendKind> for wasmer_backend_t {
fn from(value: BackendKind) -> Self {
match value {
#[cfg(feature = "cranelift")]
BackendKind::Cranelift => Self::CRANELIFT,
#[cfg(feature = "llvm")]
BackendKind::LLVM => Self::LLVM,
#[cfg(feature = "singlepass")]
BackendKind::Singlepass => Self::SINGLEPASS,
#[cfg(feature = "sys")]
BackendKind::Headless => Self::HEADLESS,
#[cfg(feature = "wamr")]
BackendKind::Wamr => Self::WAMR,
#[cfg(feature = "wasmi")]
BackendKind::Wasmi => Self::WASMI,
#[cfg(feature = "v8")]
BackendKind::V8 => Self::V8,
#[cfg(feature = "jsc")]
BackendKind::Jsc => Self::JSC,
v => panic!("Unsupported backend kind {v:?}"),
}
}
}
impl Default for wasmer_backend_t {
fn default() -> Self {
// Let the `wasmer_api` crate decide which is the default engine, given the enabled
// features.
wasmer_api::BackendKind::default().into()
}
}
/// An engine is used by the store to drive the compilation and the
/// execution of a WebAssembly module.
///
/// cbindgen:ignore
#[repr(C)]
// We can let the API decide which engine is default with the given set of
// features.
//
// See the impl of `Default` for `BackendEngine` in the `wasmer` (API) crate.
#[derive(Default)]
pub struct wasm_engine_t {
pub(crate) inner: Engine,
}
#[no_mangle]
#[allow(unreachable_code)]
pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
Box::new(wasm_engine_t::default())
}
/// Deletes an engine.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
/// // Create a default engine.
/// wasm_engine_t* engine = wasm_engine_new();
/// int error_length = wasmer_last_error_length();
/// if (error_length > 0) {
/// char *error_message = malloc(error_length);
/// wasmer_last_error_message(error_message, error_length);
///
/// printf("Attempted to set an immutable global: `%s`\n", error_message);
/// free(error_message);
/// }
///
/// // Check we have an engine!
/// assert(engine);
///
/// // Free everything.
/// wasm_engine_delete(engine);
///
/// return 0;
/// }
/// # })
/// # .success();
/// # }
/// ```
///
/// cbindgen:ignore
#[no_mangle]
pub unsafe extern "C" fn wasm_engine_delete(_engine: Option<Box<wasm_engine_t>>) {}
/// Creates an engine with a particular configuration.
///
/// # Example
///
/// See [`wasm_config_new`].
///
/// cbindgen:ignore
#[no_mangle]
pub extern "C" fn wasm_engine_new_with_config(
config: Option<Box<wasm_config_t>>,
) -> Option<Box<wasm_engine_t>> {
#[allow(unused)]
let config = *(config?);
match config.backend {
#[cfg(feature = "llvm")]
wasmer_backend_t::LLVM => config::sys::wasm_sys_engine_new_with_config(config),
#[cfg(feature = "cranelift")]
wasmer_backend_t::CRANELIFT => config::sys::wasm_sys_engine_new_with_config(config),
#[cfg(feature = "singlepass")]
wasmer_backend_t::SINGLEPASS => config::sys::wasm_sys_engine_new_with_config(config),
#[cfg(feature = "sys")]
wasmer_backend_t::HEADLESS => config::sys::wasm_sys_engine_new_with_config(config),
#[cfg(feature = "v8")]
wasmer_backend_t::V8 => config::v8::wasm_v8_engine_new_with_config(config),
#[cfg(feature = "wasmi")]
wasmer_backend_t::WASMI => config::wasmi::wasm_wasmi_engine_new_with_config(config),
#[cfg(feature = "wamr")]
wasmer_backend_t::WAMR => config::wamr::wasm_wamr_engine_new_with_config(config),
#[cfg(feature = "jsc")]
wasmer_backend_t::JSC => config::jsc::wasm_jsc_engine_new_with_config(config),
_ => unreachable!(),
}
}
/// A configuration holds the compiler and the engine used by the store.
///
/// cbindgen:ignore
#[derive(Debug, Default)]
#[repr(C)]
pub struct wasm_config_t {
pub(super) backend: wasmer_backend_t,
pub(super) backend_config: wasmer_backend_config_t,
pub(super) features: Option<Box<wasmer_features_t>>,
}
/// Create a new default Wasmer configuration.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
/// // Create the configuration.
/// wasm_config_t* config = wasm_config_new();
///
/// // Create the engine.
/// wasm_engine_t* engine = wasm_engine_new_with_config(config);
///
/// // Check we have an engine!
/// assert(engine);
///
/// // Free everything.
/// wasm_engine_delete(engine);
///
/// return 0;
/// }
/// # })
/// # .success();
/// # }
/// ```
///
/// cbindgen:ignore
#[no_mangle]
pub extern "C" fn wasm_config_new() -> Box<wasm_config_t> {
Box::<wasm_config_t>::default()
}
/// Delete a Wasmer config object.
///
/// This function does not need to be called if `wasm_engine_new_with_config` or
/// another function that takes ownership of the `wasm_config_t` is called.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
/// // Create the configuration.
/// wasm_config_t* config = wasm_config_new();
///
/// // Delete the configuration
/// wasm_config_delete(config);
///
/// return 0;
/// }
/// # })
/// # .success();
/// # }
/// ```
/// cbindgen:ignore
#[no_mangle]
pub extern "C" fn wasm_config_delete(_config: Option<Box<wasm_config_t>>) {}
/// Updates the configuration to specify a particular engine to use.
///
/// This is a Wasmer-specific function.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
/// // Create the configuration.
/// wasm_config_t* config = wasm_config_new();
///
/// // Create the engine.
/// wasm_engine_t* engine = wasm_engine_new_with_config(config);
///
/// // Check we have an engine!
/// assert(engine);
///
/// // Free everything.
/// wasm_engine_delete(engine);
///
/// return 0;
/// }
/// # })
/// # .success();
/// # }
/// ```
#[no_mangle]
pub extern "C" fn wasm_config_set_backend(config: &mut wasm_config_t, engine: wasmer_backend_t) {
config.backend = engine;
}
#[cfg(test)]
mod tests {
#[cfg(not(target_os = "windows"))]
use inline_c::assert_c;
#[cfg(target_os = "windows")]
use wasmer_inline_c::assert_c;
#[cfg_attr(coverage, ignore)]
#[test]
fn test_engine_new() {
(assert_c! {
#include "tests/wasmer.h"
int main() {
wasm_engine_t* engine = wasm_engine_new();
assert(engine);
wasm_engine_delete(engine);
return 0;
}
})
.success();
}
}