Merge branch 'master' into chore-c-api-formalization

This commit is contained in:
Ivan Enderlin
2021-02-18 15:15:11 +01:00
16 changed files with 309 additions and 176 deletions

View File

@@ -1,4 +1,6 @@
pub use super::unstable::engine::wasm_config_set_target;
pub use super::unstable::engine::{
wasm_config_set_target, wasmer_is_compiler_available, wasmer_is_engine_available,
};
use super::unstable::target_lexicon::wasmer_target_t;
use crate::error::{update_last_error, CApiError};
use cfg_if::cfg_if;
@@ -170,7 +172,7 @@ pub extern "C" fn wasm_config_delete(_config: Option<Box<wasm_config_t>>) {}
///
/// # Example
///
/// ```rust,no_run
/// ```rust
/// # use inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
@@ -180,8 +182,19 @@ pub extern "C" fn wasm_config_delete(_config: Option<Box<wasm_config_t>>) {}
/// // Create the configuration.
/// wasm_config_t* config = wasm_config_new();
///
/// // Use the Cranelift compiler.
/// wasm_config_set_compiler(config, CRANELIFT);
/// // Use the Cranelift compiler, if available.
/// if (wasmer_is_compiler_available(CRANELIFT)) {
/// wasm_config_set_compiler(config, CRANELIFT);
/// }
/// // Or maybe LLVM?
/// else if (wasmer_is_compiler_available(LLVM)) {
/// wasm_config_set_compiler(config, LLVM);
/// }
/// // Or maybe Singlepass?
/// else if (wasmer_is_compiler_available(SINGLEPASS)) {
/// wasm_config_set_compiler(config, SINGLEPASS);
/// }
/// // OK, let's run with no particular compiler.
///
/// // Create the engine.
/// wasm_engine_t* engine = wasm_engine_new_with_config(config);
@@ -213,7 +226,7 @@ pub extern "C" fn wasm_config_set_compiler(
///
/// # Example
///
/// ```rust,no_run
/// ```rust
/// # use inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
@@ -223,8 +236,15 @@ pub extern "C" fn wasm_config_set_compiler(
/// // Create the configuration.
/// wasm_config_t* config = wasm_config_new();
///
/// // Use the JIT engine.
/// wasm_config_set_engine(config, JIT);
/// // Use the JIT engine, if available.
/// if (wasmer_is_engine_available(JIT)) {
/// wasm_config_set_engine(config, JIT);
/// }
/// // Or maybe the Native engine?
/// else if (wasmer_is_engine_available(NATIVE)) {
/// wasm_config_set_engine(config, NATIVE);
/// }
/// // OK, let's do not specify any particular engine.
///
/// // Create the engine.
/// wasm_engine_t* engine = wasm_engine_new_with_config(config);

View File

@@ -1,7 +1,7 @@
//! Unstable non-standard Wasmer-specific types for the
//! `wasm_engine_t` and siblings.
use super::super::engine::wasm_config_t;
use super::super::engine::{wasm_config_t, wasmer_compiler_t, wasmer_engine_t};
use super::target_lexicon::wasmer_target_t;
/// Unstable non-standard Wasmer-specific API to update the
@@ -44,9 +44,136 @@ use super::target_lexicon::wasmer_target_t;
/// # }
/// ```
#[no_mangle]
pub extern "C" fn wasm_config_set_target(
config: &mut wasm_config_t,
target: Box<wasmer_target_t>,
) {
pub extern "C" fn wasm_config_set_target(config: &mut wasm_config_t, target: Box<wasmer_target_t>) {
config.target = Some(target);
}
/// Check whether the given compiler is available, i.e. part of this
/// compiled library.
#[no_mangle]
pub extern "C" fn wasmer_is_compiler_available(compiler: wasmer_compiler_t) -> bool {
match compiler {
wasmer_compiler_t::CRANELIFT if cfg!(feature = "cranelift") => true,
wasmer_compiler_t::LLVM if cfg!(feature = "llvm") => true,
wasmer_compiler_t::SINGLEPASS if cfg!(feature = "singlepass") => true,
_ => false,
}
}
/// Check whether there is no compiler available in this compiled
/// library.
#[no_mangle]
pub extern "C" fn wasmer_is_headless() -> bool {
!cfg!(feature = "compiler")
}
/// Check whether the given engine is available, i.e. part of this
/// compiled library.
#[no_mangle]
pub extern "C" fn wasmer_is_engine_available(engine: wasmer_engine_t) -> bool {
match engine {
wasmer_engine_t::JIT if cfg!(feature = "jit") => true,
wasmer_engine_t::NATIVE if cfg!(feature = "native") => true,
wasmer_engine_t::OBJECT_FILE if cfg!(feature = "object-file") => true,
_ => false,
}
}
#[cfg(test)]
mod tests {
use inline_c::assert_c;
use std::env::{remove_var, set_var};
#[test]
fn test_wasmer_is_headless() {
set_var(
"COMPILER",
if cfg!(feature = "compiler") { "0" } else { "1" },
);
(assert_c! {
#include "tests/wasmer_wasm.h"
#include <stdlib.h>
int main() {
assert(wasmer_is_headless() == (getenv("COMPILER")[0] == '1'));
return 0;
}
})
.success();
remove_var("COMPILER");
}
#[test]
fn test_wasmer_is_compiler_available() {
set_var(
"CRANELIFT",
if cfg!(feature = "cranelift") {
"1"
} else {
"0"
},
);
set_var("LLVM", if cfg!(feature = "llvm") { "1" } else { "0" });
set_var(
"SINGLEPASS",
if cfg!(feature = "singlepass") {
"1"
} else {
"0"
},
);
(assert_c! {
#include "tests/wasmer_wasm.h"
#include <stdlib.h>
int main() {
assert(wasmer_is_compiler_available(CRANELIFT) == (getenv("CRANELIFT")[0] == '1'));
assert(wasmer_is_compiler_available(LLVM) == (getenv("LLVM")[0] == '1'));
assert(wasmer_is_compiler_available(SINGLEPASS) == (getenv("SINGLEPASS")[0] == '1'));
return 0;
}
})
.success();
remove_var("CRANELIFT");
remove_var("LLVM");
remove_var("SINGLEPASS");
}
#[test]
fn test_wasmer_is_engine_available() {
set_var("JIT", if cfg!(feature = "jit") { "1" } else { "0" });
set_var("NATIVE", if cfg!(feature = "native") { "1" } else { "0" });
set_var(
"OBJECT_FILE",
if cfg!(feature = "object-file") {
"1"
} else {
"0"
},
);
(assert_c! {
#include "tests/wasmer_wasm.h"
#include <stdlib.h>
int main() {
assert(wasmer_is_engine_available(JIT) == (getenv("JIT")[0] == '1'));
assert(wasmer_is_engine_available(NATIVE) == (getenv("NATIVE")[0] == '1'));
assert(wasmer_is_engine_available(OBJECT_FILE) == (getenv("OBJECT_FILE")[0] == '1'));
return 0;
}
})
.success();
remove_var("JIT");
remove_var("NATIVE");
remove_var("OBJECT_FILE");
}
}