Merge branch 'master' into feature/metering-c-api

This commit is contained in:
Ivan Enderlin
2021-03-01 16:43:16 +01:00
62 changed files with 2690 additions and 1116 deletions

View File

@@ -1,5 +1,7 @@
pub use super::unstable::engine::wasm_config_set_target;
use super::unstable::target_lexicon::wasm_target_t;
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;
use std::sync::Arc;
@@ -114,6 +116,7 @@ pub struct wasm_config_t {
pub(super) target: Option<Box<wasm_target_t>>,
#[cfg(feature = "middlewares")]
pub(super) middlewares: Vec<wasmer_module_middleware_t>,
pub(super) target: Option<Box<wasmer_target_t>>,
}
/// Create a new default Wasmer configuration.
@@ -188,7 +191,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! {
@@ -198,8 +201,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);
@@ -231,7 +245,7 @@ pub extern "C" fn wasm_config_set_compiler(
///
/// # Example
///
/// ```rust,no_run
/// ```rust
/// # use inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
@@ -241,8 +255,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

@@ -2,8 +2,12 @@
#[macro_export]
macro_rules! wasm_declare_vec_inner {
($name:ident) => {
wasm_declare_vec_inner!($name, wasm);
};
($name:ident, $prefix:ident) => {
paste::paste! {
#[doc = "Creates an empty vector of [`wasm_" $name "_t`].
#[doc = "Creates an empty vector of [`" $prefix "_" $name "_t`].
# Example
@@ -14,24 +18,24 @@ macro_rules! wasm_declare_vec_inner {
# #include \"tests/wasmer_wasm.h\"
#
int main() {
// Creates an empty vector of `wasm_" $name "_t`.
wasm_" $name "_vec_t vector;
wasm_" $name "_vec_new_empty(&vector);
// Creates an empty vector of `" $prefix "_" $name "_t`.
" $prefix "_" $name "_vec_t vector;
" $prefix "_" $name "_vec_new_empty(&vector);
// Check that it is empty.
assert(vector.size == 0);
// Free it.
wasm_" $name "_vec_delete(&vector);
" $prefix "_" $name "_vec_delete(&vector);
}
# })
# .success();
# }
```"]
#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_new_empty>](out: *mut [<wasm_ $name _vec_t>]) {
pub unsafe extern "C" fn [<$prefix _ $name _vec_new_empty>](out: *mut [<$prefix _ $name _vec_t>]) {
// TODO: actually implement this
[<wasm_ $name _vec_new_uninitialized>](out, 0);
[<$prefix _ $name _vec_new_uninitialized>](out, 0);
}
}
}
@@ -41,10 +45,14 @@ int main() {
#[macro_export]
macro_rules! wasm_declare_vec {
($name:ident) => {
paste::paste! {
#[doc = "Represents a vector of `wasm_" $name "_t`.
wasm_declare_vec!($name, wasm);
};
Read the documentation of [`wasm_" $name "_t`] to see more concrete examples.
($name:ident, $prefix:ident) => {
paste::paste! {
#[doc = "Represents a vector of `" $prefix "_" $name "_t`.
Read the documentation of [`" $prefix "_" $name "_t`] to see more concrete examples.
# Example
@@ -55,19 +63,19 @@ Read the documentation of [`wasm_" $name "_t`] to see more concrete examples.
# #include \"tests/wasmer_wasm.h\"
#
int main() {
// Create a vector of 2 `wasm_" $name "_t`.
wasm_" $name "_t x;
wasm_" $name "_t y;
wasm_" $name "_t* items[2] = {&x, &y};
// Create a vector of 2 `" $prefix "_" $name "_t`.
" $prefix "_" $name "_t x;
" $prefix "_" $name "_t y;
" $prefix "_" $name "_t* items[2] = {&x, &y};
wasm_" $name "_vec_t vector;
wasm_" $name "_vec_new(&vector, 2, (wasm_" $name "_t*) items);
" $prefix "_" $name "_vec_t vector;
" $prefix "_" $name "_vec_new(&vector, 2, (" $prefix "_" $name "_t*) items);
// Check that it contains 2 items.
assert(vector.size == 2);
// Free it.
wasm_" $name "_vec_delete(&vector);
" $prefix "_" $name "_vec_delete(&vector);
}
# })
# .success();
@@ -75,12 +83,12 @@ int main() {
```"]
#[derive(Debug)]
#[repr(C)]
pub struct [<wasm_ $name _vec_t>] {
pub struct [<$prefix _ $name _vec_t>] {
pub size: usize,
pub data: *mut [<wasm_ $name _t>],
pub data: *mut [<$prefix _ $name _t>],
}
impl Clone for [<wasm_ $name _vec_t>] {
impl Clone for [<$prefix _ $name _vec_t>] {
fn clone(&self) -> Self {
if self.data.is_null() {
return Self {
@@ -107,8 +115,8 @@ int main() {
}
}
impl<'a> From<Vec<[<wasm_ $name _t>]>> for [<wasm_ $name _vec_t>] {
fn from(mut vec: Vec<[<wasm_ $name _t>]>) -> Self {
impl<'a> From<Vec<[<$prefix _ $name _t>]>> for [<$prefix _ $name _vec_t>] {
fn from(mut vec: Vec<[<$prefix _ $name _t>]>) -> Self {
vec.shrink_to_fit();
let length = vec.len();
@@ -123,14 +131,14 @@ int main() {
}
}
impl<'a, T: Into<[<wasm_ $name _t>]> + Clone> From<&'a [T]> for [<wasm_ $name _vec_t>] {
impl<'a, T: Into<[<$prefix _ $name _t>]> + Clone> From<&'a [T]> for [<$prefix _ $name _vec_t>] {
fn from(other: &'a [T]) -> Self {
let size = other.len();
let mut copied_data = other
.iter()
.cloned()
.map(Into::into)
.collect::<Vec<[<wasm_ $name _t>]>>()
.collect::<Vec<[<$prefix _ $name _t>]>>()
.into_boxed_slice();
let data = copied_data.as_mut_ptr();
::std::mem::forget(copied_data);
@@ -142,8 +150,8 @@ int main() {
}
}
impl [<wasm_ $name _vec_t>] {
pub unsafe fn into_slice(&self) -> Option<&[[<wasm_ $name _t>]]>{
impl [<$prefix _ $name _vec_t>] {
pub unsafe fn into_slice(&self) -> Option<&[[<$prefix _ $name _t>]]>{
if self.is_uninitialized() {
return None;
}
@@ -151,7 +159,7 @@ int main() {
Some(::std::slice::from_raw_parts(self.data, self.size))
}
pub unsafe fn into_slice_mut(&mut self) -> Option<&mut [[<wasm_ $name _t>]]>{
pub unsafe fn into_slice_mut(&mut self) -> Option<&mut [[<$prefix _ $name _t>]]>{
if self.is_uninitialized() {
return None;
}
@@ -165,14 +173,14 @@ int main() {
}
// TODO: investigate possible memory leak on `init` (owned pointer)
#[doc = "Creates a new vector of [`wasm_" $name "_t`].
#[doc = "Creates a new vector of [`" $prefix "_" $name "_t`].
# Example
See the [`wasm_" $name "_vec_t`] type to get an example."]
See the [`" $prefix "_" $name "_vec_t`] type to get an example."]
#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_new>](out: *mut [<wasm_ $name _vec_t>], length: usize, init: *mut [<wasm_ $name _t>]) {
let mut bytes: Vec<[<wasm_ $name _t>]> = Vec::with_capacity(length);
pub unsafe extern "C" fn [<$prefix _ $name _vec_new>](out: *mut [<$prefix _ $name _vec_t>], length: usize, init: *mut [<$prefix _ $name _t>]) {
let mut bytes: Vec<[<$prefix _ $name _t>]> = Vec::with_capacity(length);
for i in 0..length {
bytes.push(::std::ptr::read(init.add(i)));
@@ -186,7 +194,7 @@ See the [`wasm_" $name "_vec_t`] type to get an example."]
::std::mem::forget(bytes);
}
#[doc = "Creates a new uninitialized vector of [`wasm_" $name "_t`].
#[doc = "Creates a new uninitialized vector of [`" $prefix "_" $name "_t`].
# Example
@@ -197,23 +205,23 @@ See the [`wasm_" $name "_vec_t`] type to get an example."]
# #include \"tests/wasmer_wasm.h\"
#
int main() {
// Creates an empty vector of `wasm_" $name "_t`.
wasm_" $name "_vec_t vector;
wasm_" $name "_vec_new_uninitialized(&vector, 3);
// Creates an empty vector of `" $prefix "_" $name "_t`.
" $prefix "_" $name "_vec_t vector;
" $prefix "_" $name "_vec_new_uninitialized(&vector, 3);
// Check that it contains 3 items.
assert(vector.size == 3);
// Free it.
wasm_" $name "_vec_delete(&vector);
" $prefix "_" $name "_vec_delete(&vector);
}
# })
# .success();
# }
```"]
#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_new_uninitialized>](out: *mut [<wasm_ $name _vec_t>], length: usize) {
let mut bytes: Vec<[<wasm_ $name _t>]> = Vec::with_capacity(length);
pub unsafe extern "C" fn [<$prefix _ $name _vec_new_uninitialized>](out: *mut [<$prefix _ $name _vec_t>], length: usize) {
let mut bytes: Vec<[<$prefix _ $name _t>]> = Vec::with_capacity(length);
let pointer = bytes.as_mut_ptr();
(*out).data = pointer;
@@ -222,22 +230,22 @@ int main() {
::std::mem::forget(bytes);
}
#[doc = "Performs a deep copy of a vector of [`wasm_" $name "_t`]."]
#[doc = "Performs a deep copy of a vector of [`" $prefix "_" $name "_t`]."]
#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_copy>](
out_ptr: &mut [<wasm_ $name _vec_t>],
pub unsafe extern "C" fn [<$prefix _ $name _vec_copy>](
out_ptr: &mut [<$prefix _ $name _vec_t>],
in_ptr: & [<wasm _$name _vec_t>])
{
*out_ptr = in_ptr.clone();
}
#[doc = "Deletes a vector of [`wasm_" $name "_t`].
#[doc = "Deletes a vector of [`" $prefix "_" $name "_t`].
# Example
See the [`wasm_" $name "_vec_t`] type to get an example."]
See the [`" $prefix "_" $name "_vec_t`] type to get an example."]
#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_delete>](ptr: Option<&mut [<wasm_ $name _vec_t>]>) {
pub unsafe extern "C" fn [<$prefix _ $name _vec_delete>](ptr: Option<&mut [<$prefix _ $name _vec_t>]>) {
if let Some(vec) = ptr {
if !vec.data.is_null() {
Vec::from_raw_parts(vec.data, vec.size, vec.size);
@@ -248,7 +256,7 @@ See the [`wasm_" $name "_vec_t`] type to get an example."]
}
}
wasm_declare_vec_inner!($name);
wasm_declare_vec_inner!($name, $prefix);
};
}
@@ -256,18 +264,22 @@ See the [`wasm_" $name "_vec_t`] type to get an example."]
#[macro_export]
macro_rules! wasm_declare_boxed_vec {
($name:ident) => {
paste::paste! {
#[doc = "Represents a vector of `wasm_" $name "_t`.
wasm_declare_boxed_vec!($name, wasm);
};
Read the documentation of [`wasm_" $name "_t`] to see more concrete examples."]
($name:ident, $prefix:ident) => {
paste::paste! {
#[doc = "Represents a vector of `" $prefix "_" $name "_t`.
Read the documentation of [`" $prefix "_" $name "_t`] to see more concrete examples."]
#[derive(Debug)]
#[repr(C)]
pub struct [<wasm_ $name _vec_t>] {
pub struct [<$prefix _ $name _vec_t>] {
pub size: usize,
pub data: *mut *mut [<wasm_ $name _t>],
pub data: *mut *mut [<$prefix _ $name _t>],
}
impl Clone for [<wasm_ $name _vec_t>] {
impl Clone for [<$prefix _ $name _vec_t>] {
fn clone(&self) -> Self {
if self.data.is_null() {
return Self {
@@ -277,10 +289,10 @@ Read the documentation of [`wasm_" $name "_t`] to see more concrete examples."]
}
let data =
unsafe {
let data: *mut Option<Box<[<wasm_ $name _t>]>> = self.data as _;
let data: *mut Option<Box<[<$prefix _ $name _t>]>> = self.data as _;
let vec = Vec::from_raw_parts(data, self.size, self.size);
let mut vec_copy = vec.clone().into_boxed_slice();
let new_ptr = vec_copy.as_mut_ptr() as *mut *mut [<wasm_ $name _t>];
let new_ptr = vec_copy.as_mut_ptr() as *mut *mut [<$prefix _ $name _t>];
::std::mem::forget(vec);
::std::mem::forget(vec_copy);
@@ -295,10 +307,10 @@ Read the documentation of [`wasm_" $name "_t`] to see more concrete examples."]
}
}
impl<'a> From<Vec<Box<[<wasm_ $name _t>]>>> for [<wasm_ $name _vec_t>] {
fn from(other: Vec<Box<[<wasm_ $name _t>]>>) -> Self {
let boxed_slice: Box<[Box<[<wasm_ $name _t>]>]> = other.into_boxed_slice();
let mut boxed_slice: Box<[*mut [<wasm_ $name _t>]]> = unsafe { ::std::mem::transmute(boxed_slice) };
impl<'a> From<Vec<Box<[<$prefix _ $name _t>]>>> for [<$prefix _ $name _vec_t>] {
fn from(other: Vec<Box<[<$prefix _ $name _t>]>>) -> Self {
let boxed_slice: Box<[Box<[<$prefix _ $name _t>]>]> = other.into_boxed_slice();
let mut boxed_slice: Box<[*mut [<$prefix _ $name _t>]]> = unsafe { ::std::mem::transmute(boxed_slice) };
let size = boxed_slice.len();
let data = boxed_slice.as_mut_ptr();
@@ -310,7 +322,7 @@ Read the documentation of [`wasm_" $name "_t`] to see more concrete examples."]
}
}
impl<'a, T: Into<[<wasm_ $name _t>]> + Clone> From<&'a [T]> for [<wasm_ $name _vec_t>] {
impl<'a, T: Into<[<$prefix _ $name _t>]> + Clone> From<&'a [T]> for [<$prefix _ $name _vec_t>] {
fn from(other: &'a [T]) -> Self {
let size = other.len();
let mut copied_data = other
@@ -319,7 +331,7 @@ Read the documentation of [`wasm_" $name "_t`] to see more concrete examples."]
.map(Into::into)
.map(Box::new)
.map(Box::into_raw)
.collect::<Vec<*mut [<wasm_ $name _t>]>>()
.collect::<Vec<*mut [<$prefix _ $name _t>]>>()
.into_boxed_slice();
let data = copied_data.as_mut_ptr();
::std::mem::forget(copied_data);
@@ -332,23 +344,23 @@ Read the documentation of [`wasm_" $name "_t`] to see more concrete examples."]
}
// TODO: do this properly
impl [<wasm_ $name _vec_t>] {
pub unsafe fn into_slice(&self) -> Option<&[Box<[<wasm_ $name _t>]>]>{
impl [<$prefix _ $name _vec_t>] {
pub unsafe fn into_slice(&self) -> Option<&[Box<[<$prefix _ $name _t>]>]>{
if self.data.is_null() {
return None;
}
let slice: &[*mut [<wasm_ $name _t>]] = ::std::slice::from_raw_parts(self.data, self.size);
let slice: &[Box<[<wasm_ $name _t>]>] = ::std::mem::transmute(slice);
let slice: &[*mut [<$prefix _ $name _t>]] = ::std::slice::from_raw_parts(self.data, self.size);
let slice: &[Box<[<$prefix _ $name _t>]>] = ::std::mem::transmute(slice);
Some(slice)
}
}
// TODO: investigate possible memory leak on `init` (owned pointer)
#[doc = "Creates a new vector of [`wasm_" $name "_t`]."]
#[doc = "Creates a new vector of [`" $prefix "_" $name "_t`]."]
#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_new>](out: *mut [<wasm_ $name _vec_t>], length: usize, init: *const *mut [<wasm_ $name _t>]) {
let mut bytes: Vec<*mut [<wasm_ $name _t>]> = Vec::with_capacity(length);
pub unsafe extern "C" fn [<$prefix _ $name _vec_new>](out: *mut [<$prefix _ $name _vec_t>], length: usize, init: *const *mut [<$prefix _ $name _t>]) {
let mut bytes: Vec<*mut [<$prefix _ $name _t>]> = Vec::with_capacity(length);
for i in 0..length {
bytes.push(*init.add(i));
@@ -363,7 +375,7 @@ Read the documentation of [`wasm_" $name "_t`] to see more concrete examples."]
::std::mem::forget(boxed_vec);
}
#[doc = "Creates a new uninitialized vector of [`wasm_" $name "_t`].
#[doc = "Creates a new uninitialized vector of [`" $prefix "_" $name "_t`].
# Example
@@ -374,23 +386,23 @@ Read the documentation of [`wasm_" $name "_t`] to see more concrete examples."]
# #include \"tests/wasmer_wasm.h\"
#
int main() {
// Creates an empty vector of `wasm_" $name "_t`.
wasm_" $name "_vec_t vector;
wasm_" $name "_vec_new_uninitialized(&vector, 3);
// Creates an empty vector of `" $prefix "_" $name "_t`.
" $prefix "_" $name "_vec_t vector;
" $prefix "_" $name "_vec_new_uninitialized(&vector, 3);
// Check that it contains 3 items.
assert(vector.size == 3);
// Free it.
wasm_" $name "_vec_delete(&vector);
" $prefix "_" $name "_vec_delete(&vector);
}
# })
# .success();
# }
```"]
#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_new_uninitialized>](out: *mut [<wasm_ $name _vec_t>], length: usize) {
let mut bytes: Vec<*mut [<wasm_ $name _t>]> = vec![::std::ptr::null_mut(); length];
pub unsafe extern "C" fn [<$prefix _ $name _vec_new_uninitialized>](out: *mut [<$prefix _ $name _vec_t>], length: usize) {
let mut bytes: Vec<*mut [<$prefix _ $name _t>]> = vec![::std::ptr::null_mut(); length];
let pointer = bytes.as_mut_ptr();
(*out).data = pointer;
@@ -399,26 +411,26 @@ int main() {
::std::mem::forget(bytes);
}
#[doc = "Performs a deep copy of a vector of [`wasm_" $name "_t`]."]
#[doc = "Performs a deep copy of a vector of [`" $prefix "_" $name "_t`]."]
#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_copy>](
out_ptr: &mut [<wasm_ $name _vec_t>],
in_ptr: & [<wasm_ $name _vec_t>])
pub unsafe extern "C" fn [<$prefix _ $name _vec_copy>](
out_ptr: &mut [<$prefix _ $name _vec_t>],
in_ptr: & [<$prefix _ $name _vec_t>])
{
*out_ptr = in_ptr.clone();
}
#[doc = "Deletes a vector of [`wasm_" $name "_t`].
#[doc = "Deletes a vector of [`" $prefix "_" $name "_t`].
# Example
See the [`wasm_" $name "_vec_t`] type to get an example."]
See the [`" $prefix "_" $name "_vec_t`] type to get an example."]
#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_delete>](ptr: Option<&mut [<wasm_ $name _vec_t>]>) {
pub unsafe extern "C" fn [<$prefix _ $name _vec_delete>](ptr: Option<&mut [<$prefix _ $name _vec_t>]>) {
if let Some(vec) = ptr {
if !vec.data.is_null() {
let data = vec.data as *mut Option<Box<[<wasm_ $name _t>]>>;
let _data: Vec<Option<Box<[<wasm_ $name _t>]>>> = Vec::from_raw_parts(data, vec.size, vec.size);
let data = vec.data as *mut Option<Box<[<$prefix _ $name _t>]>>;
let _data: Vec<Option<Box<[<$prefix _ $name _t>]>>> = Vec::from_raw_parts(data, vec.size, vec.size);
vec.data = ::std::ptr::null_mut();
vec.size = 0;
@@ -427,7 +439,7 @@ See the [`wasm_" $name "_vec_t`] type to get an example."]
}
}
wasm_declare_vec_inner!($name);
wasm_declare_vec_inner!($name, $prefix);
};
}
@@ -435,11 +447,15 @@ See the [`wasm_" $name "_vec_t`] type to get an example."]
#[macro_export]
macro_rules! wasm_declare_ref_base {
($name:ident) => {
wasm_declare_own!($name);
wasm_declare_ref_base!($name, wasm);
};
($name:ident, $prefix:ident) => {
wasm_declare_own!($name, $prefix);
paste::paste! {
#[no_mangle]
pub extern "C" fn [<wasm_ $name _copy>](_arg: *const [<wasm_ $name _t>]) -> *mut [<wasm_ $name _t>] {
pub extern "C" fn [<$prefix _ $name _copy>](_arg: *const [<$prefix _ $name _t>]) -> *mut [<$prefix _ $name _t>] {
todo!("in generated declare ref base");
//ptr::null_mut()
}
@@ -453,12 +469,16 @@ macro_rules! wasm_declare_ref_base {
#[macro_export]
macro_rules! wasm_declare_own {
($name:ident) => {
wasm_declare_own!($name, $prefix);
};
($name:ident, $prefix:ident) => {
paste::paste! {
#[repr(C)]
pub struct [<wasm_ $name _t>] {}
pub struct [<$prefix _ $name _t>] {}
#[no_mangle]
pub extern "C" fn [<wasm_ $name _delete>](_arg: *mut [<wasm_ $name _t>]) {
pub extern "C" fn [<$prefix _ $name _delete>](_arg: *mut [<$prefix _ $name _t>]) {
todo!("in generated delete")
}
}

View File

@@ -1,8 +1,8 @@
//! Unstable non-standard Wasmer-specific types for the
//! `wasm_engine_t` and siblings.
use super::super::engine::wasm_config_t;
use super::target_lexicon::wasm_target_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
/// configuration to specify a particular target for the engine.
@@ -21,9 +21,9 @@ use super::target_lexicon::wasm_target_t;
///
/// // Set the target.
/// {
/// wasm_triple_t* triple = wasm_triple_new_from_host();
/// wasm_cpu_features_t* cpu_features = wasm_cpu_features_new();
/// wasm_target_t* target = wasm_target_new(triple, cpu_features);
/// wasmer_triple_t* triple = wasmer_triple_new_from_host();
/// wasmer_cpu_features_t* cpu_features = wasmer_cpu_features_new();
/// wasmer_target_t* target = wasmer_target_new(triple, cpu_features);
///
/// wasm_config_set_target(config, target);
/// }
@@ -44,6 +44,136 @@ use super::target_lexicon::wasm_target_t;
/// # }
/// ```
#[no_mangle]
pub extern "C" fn wasm_config_set_target(config: &mut wasm_config_t, target: Box<wasm_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");
}
}

View File

@@ -3,3 +3,6 @@ pub mod engine;
pub mod middlewares;
pub mod module;
pub mod target_lexicon;
#[cfg(feature = "wasi")]
pub mod wasi;

View File

@@ -35,7 +35,7 @@ use std::sync::Arc;
///
/// // Read the module's name.
/// wasm_name_t name;
/// wasm_module_name(module, &name);
/// wasmer_module_name(module, &name);
///
/// // It works!
/// wasmer_assert_name(&name, "moduleName");
@@ -55,7 +55,7 @@ use std::sync::Arc;
/// # }
/// ```
#[no_mangle]
pub unsafe extern "C" fn wasm_module_name(
pub unsafe extern "C" fn wasmer_module_name(
module: &wasm_module_t,
// own
out: &mut wasm_name_t,
@@ -102,7 +102,7 @@ pub unsafe extern "C" fn wasm_module_name(
/// // Read the module's name. There is none for the moment.
/// {
/// wasm_name_t name;
/// wasm_module_name(module, &name);
/// wasmer_module_name(module, &name);
///
/// assert(name.size == 0);
/// }
@@ -111,13 +111,13 @@ pub unsafe extern "C" fn wasm_module_name(
/// {
/// wasm_name_t name;
/// wasmer_byte_vec_new_from_string(&name, "hello");
/// wasm_module_set_name(module, &name);
/// wasmer_module_set_name(module, &name);
/// }
///
/// // And now, let's see the new name.
/// {
/// wasm_name_t name;
/// wasm_module_name(module, &name);
/// wasmer_module_name(module, &name);
///
/// // It works!
/// wasmer_assert_name(&name, "hello");
@@ -139,7 +139,7 @@ pub unsafe extern "C" fn wasm_module_name(
/// # }
/// ```
#[no_mangle]
pub unsafe extern "C" fn wasm_module_set_name(
pub unsafe extern "C" fn wasmer_module_set_name(
module: &mut wasm_module_t,
// own
name: &wasm_name_t,

View File

@@ -13,13 +13,13 @@
//! #
//! int main() {
//! // Declare the target triple.
//! wasm_triple_t* triple;
//! wasmer_triple_t* triple;
//!
//! {
//! wasm_name_t triple_name;
//! wasm_name_new_from_string(&triple_name, "x86_64-apple-darwin");
//!
//! triple = wasm_triple_new(&triple_name);
//! triple = wasmer_triple_new(&triple_name);
//!
//! wasm_name_delete(&triple_name);
//! }
@@ -27,13 +27,13 @@
//! assert(triple);
//!
//! // Declare the target CPU features.
//! wasm_cpu_features_t* cpu_features = wasm_cpu_features_new();
//! wasmer_cpu_features_t* cpu_features = wasmer_cpu_features_new();
//!
//! {
//! wasm_name_t cpu_feature_name;
//! wasm_name_new_from_string(&cpu_feature_name, "sse2");
//!
//! wasm_cpu_features_add(cpu_features, &cpu_feature_name);
//! wasmer_cpu_features_add(cpu_features, &cpu_feature_name);
//!
//! wasm_name_delete(&cpu_feature_name);
//! }
@@ -41,10 +41,10 @@
//! assert(cpu_features);
//!
//! // Create the target!
//! wasm_target_t* target = wasm_target_new(triple, cpu_features);
//! wasmer_target_t* target = wasmer_target_new(triple, cpu_features);
//! assert(target);
//!
//! wasm_target_delete(target);
//! wasmer_target_delete(target);
//!
//! return 0;
//! }
@@ -68,11 +68,11 @@ use wasmer_compiler::{CpuFeature, Target, Triple};
/// See the module's documentation.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct wasm_target_t {
pub struct wasmer_target_t {
pub(crate) inner: Target,
}
/// Creates a new [`wasm_target_t`].
/// Creates a new [`wasmer_target_t`].
///
/// It takes ownership of `triple` and `cpu_features`.
///
@@ -80,25 +80,25 @@ pub struct wasm_target_t {
///
/// See the module's documentation.
#[no_mangle]
pub extern "C" fn wasm_target_new(
triple: Option<Box<wasm_triple_t>>,
cpu_features: Option<Box<wasm_cpu_features_t>>,
) -> Option<Box<wasm_target_t>> {
pub extern "C" fn wasmer_target_new(
triple: Option<Box<wasmer_triple_t>>,
cpu_features: Option<Box<wasmer_cpu_features_t>>,
) -> Option<Box<wasmer_target_t>> {
let triple = triple?;
let cpu_features = cpu_features?;
Some(Box::new(wasm_target_t {
Some(Box::new(wasmer_target_t {
inner: Target::new(triple.inner.clone(), cpu_features.inner.clone()),
}))
}
/// Delete a [`wasm_target_t`].
/// Delete a [`wasmer_target_t`].
///
/// # Example
///
/// See the module's documentation.
#[no_mangle]
pub extern "C" fn wasm_target_delete(_target: Option<Box<wasm_target_t>>) {}
pub extern "C" fn wasmer_target_delete(_target: Option<Box<wasmer_target_t>>) {}
/// Unstable non-standard Wasmer-specific API to represent a target
/// “triple”.
@@ -118,10 +118,10 @@ pub extern "C" fn wasm_target_delete(_target: Option<Box<wasm_target_t>>) {}
/// wasm_name_t triple_name;
/// wasm_name_new_from_string(&triple_name, "x86_64-apple-darwin");
///
/// wasm_triple_t* triple = wasm_triple_new(&triple_name);
/// wasmer_triple_t* triple = wasmer_triple_new(&triple_name);
/// assert(triple);
///
/// wasm_triple_delete(triple);
/// wasmer_triple_delete(triple);
/// wasm_name_delete(&triple_name);
///
/// return 0;
@@ -131,33 +131,33 @@ pub extern "C" fn wasm_target_delete(_target: Option<Box<wasm_target_t>>) {}
/// # }
/// ```
///
/// See also [`wasm_triple_new_from_host`].
/// See also [`wasmer_triple_new_from_host`].
#[allow(non_camel_case_types)]
pub struct wasm_triple_t {
pub struct wasmer_triple_t {
inner: Triple,
}
/// Create a new [`wasm_triple_t`] based on a triple string.
/// Create a new [`wasmer_triple_t`] based on a triple string.
///
/// # Example
///
/// See [`wasm_triple_t`] or [`wasm_triple_new_from_host`].
/// See [`wasmer_triple_t`] or [`wasmer_triple_new_from_host`].
#[no_mangle]
pub unsafe extern "C" fn wasm_triple_new(
pub unsafe extern "C" fn wasmer_triple_new(
triple: Option<&wasm_name_t>,
) -> Option<Box<wasm_triple_t>> {
) -> Option<Box<wasmer_triple_t>> {
let triple = triple?;
let triple = c_try!(str::from_utf8(slice::from_raw_parts(
triple.data,
triple.size
)));
Some(Box::new(wasm_triple_t {
Some(Box::new(wasmer_triple_t {
inner: c_try!(Triple::from_str(triple).map_err(|e| CApiError { msg: e.to_string() })),
}))
}
/// Create the [`wasm_triple_t`] for the current host.
/// Create the [`wasmer_triple_t`] for the current host.
///
/// # Example
///
@@ -168,10 +168,10 @@ pub unsafe extern "C" fn wasm_triple_new(
/// # #include "tests/wasmer_wasm.h"
/// #
/// int main() {
/// wasm_triple_t* triple = wasm_triple_new_from_host();
/// wasmer_triple_t* triple = wasmer_triple_new_from_host();
/// assert(triple);
///
/// wasm_triple_delete(triple);
/// wasmer_triple_delete(triple);
///
/// return 0;
/// }
@@ -180,21 +180,21 @@ pub unsafe extern "C" fn wasm_triple_new(
/// # }
/// ```
///
/// See also [`wasm_triple_new`].
/// See also [`wasmer_triple_new`].
#[no_mangle]
pub extern "C" fn wasm_triple_new_from_host() -> Box<wasm_triple_t> {
Box::new(wasm_triple_t {
pub extern "C" fn wasmer_triple_new_from_host() -> Box<wasmer_triple_t> {
Box::new(wasmer_triple_t {
inner: Triple::host(),
})
}
/// Delete a [`wasm_triple_t`].
/// Delete a [`wasmer_triple_t`].
///
/// # Example
///
/// See [`wasm_triple_t`].
/// See [`wasmer_triple_t`].
#[no_mangle]
pub extern "C" fn wasm_triple_delete(_triple: Option<Box<wasm_triple_t>>) {}
pub extern "C" fn wasmer_triple_delete(_triple: Option<Box<wasmer_triple_t>>) {}
/// Unstable non-standard Wasmer-specific API to represent a set of
/// CPU features.
@@ -233,19 +233,19 @@ pub extern "C" fn wasm_triple_delete(_triple: Option<Box<wasm_triple_t>>) {}
/// #
/// int main() {
/// // Create a new CPU feature set.
/// wasm_cpu_features_t* cpu_features = wasm_cpu_features_new();
/// wasmer_cpu_features_t* cpu_features = wasmer_cpu_features_new();
///
/// // Create a new feature name, here `sse2`, and add it to the set.
/// {
/// wasm_name_t cpu_feature_name;
/// wasm_name_new_from_string(&cpu_feature_name, "sse2");
///
/// wasm_cpu_features_add(cpu_features, &cpu_feature_name);
/// wasmer_cpu_features_add(cpu_features, &cpu_feature_name);
///
/// wasm_name_delete(&cpu_feature_name);
/// }
///
/// wasm_cpu_features_delete(cpu_features);
/// wasmer_cpu_features_delete(cpu_features);
///
/// return 0;
/// }
@@ -254,39 +254,39 @@ pub extern "C" fn wasm_triple_delete(_triple: Option<Box<wasm_triple_t>>) {}
/// # }
/// ```
#[allow(non_camel_case_types)]
pub struct wasm_cpu_features_t {
pub struct wasmer_cpu_features_t {
inner: EnumSet<CpuFeature>,
}
/// Create a new [`wasm_cpu_features_t`].
/// Create a new [`wasmer_cpu_features_t`].
///
/// # Example
///
/// See [`wasm_cpu_features_t`].
/// See [`wasmer_cpu_features_t`].
#[no_mangle]
pub extern "C" fn wasm_cpu_features_new() -> Box<wasm_cpu_features_t> {
Box::new(wasm_cpu_features_t {
pub extern "C" fn wasmer_cpu_features_new() -> Box<wasmer_cpu_features_t> {
Box::new(wasmer_cpu_features_t {
inner: CpuFeature::set(),
})
}
/// Delete a [`wasm_cpu_features_t`].
/// Delete a [`wasmer_cpu_features_t`].
///
/// # Example
///
/// See [`wasm_cpu_features_t`].
/// See [`wasmer_cpu_features_t`].
#[no_mangle]
pub extern "C" fn wasm_cpu_features_delete(_cpu_features: Option<Box<wasm_cpu_features_t>>) {}
pub extern "C" fn wasmer_cpu_features_delete(_cpu_features: Option<Box<wasmer_cpu_features_t>>) {}
/// Add a new CPU feature into the set represented by
/// [`wasm_cpu_features_t`].
/// [`wasmer_cpu_features_t`].
///
/// # Example
///
/// See [`wasm_cpu_features_t`].
/// See [`wasmer_cpu_features_t`].
#[no_mangle]
pub unsafe extern "C" fn wasm_cpu_features_add(
cpu_features: Option<&mut wasm_cpu_features_t>,
pub unsafe extern "C" fn wasmer_cpu_features_add(
cpu_features: Option<&mut wasmer_cpu_features_t>,
feature: Option<&wasm_name_t>,
) -> bool {
let cpu_features = match cpu_features {

View File

@@ -0,0 +1,199 @@
//! Unstable non-standard Wasmer-specific API that contains more WASI
//! API.
use super::super::{
externals::wasm_extern_t, module::wasm_module_t, store::wasm_store_t, types::wasm_name_t,
wasi::wasi_env_t,
};
use crate::error::CApiError;
use wasmer::Extern;
use wasmer_wasi::{generate_import_object_from_env, get_wasi_version};
/// Unstable non-standard type wrapping `wasm_extern_t` with the
/// addition of two `wasm_name_t` respectively for the module name and
/// the name of the extern (very likely to be an import). This
/// non-standard type is used by the unstable non-standard
/// `wasi_get_unordered_imports` function.
///
/// The `module`, `name` and `extern` fields are all owned by this type.
#[allow(non_camel_case_types)]
#[derive(Clone)]
pub struct wasmer_named_extern_t {
module: Box<wasm_name_t>,
name: Box<wasm_name_t>,
r#extern: Box<wasm_extern_t>,
}
wasm_declare_boxed_vec!(named_extern, wasmer);
/// So. Let's explain a dirty hack. `cbindgen` reads the code and
/// collects symbols. What symbols do we need? None of the one
/// declared in `wasm.h`, but for non-standard API, we need to collect
/// all of them. The problem is that `wasmer_named_extern_t` is the only
/// non-standard type where extra symbols are generated by a macro
/// (`wasm_declare_boxed_vec!`). If we want those macro-generated
/// symbols to be collected by `cbindgen`, we need to _expand_ the
/// crate (i.e. running something like `rustc -- -Zunstable-options
/// --pretty=expanded`). Expanding code is unstable and available only
/// on nightly compiler. We _don't want_ to use a nightly compiler
/// only for that. So how can we help `cbindgen` to _see_ those
/// symbols?
///
/// First solution: We write the C code directly in a file, which is
/// then included in the generated header file with the `cbindgen`
/// API. Problem, it's super easy to get it outdated, and it makes the
/// build process more complex.
///
/// Second solution: We write those symbols in a custom module, that
/// is just here for `cbindgen`, never used by our Rust code
/// (otherwise it's duplicated code), with no particular
/// implementation.
///
/// And that's why we have the following `cbindgen_hack`
/// module.
///
/// But this module must not be compiled by `rustc`. How to force
/// `rustc` to ignore a module? With conditional compilation. Because
/// `cbindgen` does not support conditional compilation, it will
/// always _ignore_ the `#[cfg]` attribute, and will always read the
/// content of the module.
///
/// Sorry.
#[doc(hidden)]
#[cfg(__cbindgen_hack__ = "yes")]
mod __cbindgen_hack__ {
use super::*;
#[repr(C)]
pub struct wasmer_named_extern_vec_t {
pub size: usize,
pub data: *mut *mut wasmer_named_extern_t,
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_named_extern_vec_new(
out: *mut wasmer_named_extern_vec_t,
length: usize,
init: *const *mut wasmer_named_extern_t,
) {
unimplemented!()
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_named_extern_vec_new_uninitialized(
out: *mut wasmer_named_extern_vec_t,
length: usize,
) {
unimplemented!()
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_named_extern_vec_copy(
out_ptr: &mut wasmer_named_extern_vec_t,
in_ptr: &wasmer_named_extern_vec_t,
) {
unimplemented!()
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_named_extern_vec_delete(
ptr: Option<&mut wasmer_named_extern_vec_t>,
) {
unimplemented!()
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_named_extern_vec_new_empty(
out: *mut wasmer_named_extern_vec_t,
) {
unimplemented!()
}
}
/// Non-standard function to get the module name of a
/// `wasmer_named_extern_t`.
///
/// The returned value isn't owned by the caller.
#[no_mangle]
pub extern "C" fn wasmer_named_extern_module(
named_extern: Option<&wasmer_named_extern_t>,
) -> Option<&wasm_name_t> {
Some(named_extern?.module.as_ref())
}
/// Non-standard function to get the name of a `wasmer_named_extern_t`.
///
/// The returned value isn't owned by the caller.
#[no_mangle]
pub extern "C" fn wasmer_named_extern_name(
named_extern: Option<&wasmer_named_extern_t>,
) -> Option<&wasm_name_t> {
Some(named_extern?.name.as_ref())
}
/// Non-standard function to get the wrapped extern of a
/// `wasmer_named_extern_t`.
///
/// The returned value isn't owned by the caller.
#[no_mangle]
pub extern "C" fn wasmer_named_extern_unwrap(
named_extern: Option<&wasmer_named_extern_t>,
) -> Option<&wasm_extern_t> {
Some(named_extern?.r#extern.as_ref())
}
/// Non-standard function to get the imports needed for the WASI
/// implementation with no particular order. Each import has its
/// associated module name and name, so that it can be re-order later
/// based on the `wasm_module_t` requirements.
#[no_mangle]
pub unsafe extern "C" fn wasi_get_unordered_imports(
store: Option<&wasm_store_t>,
module: Option<&wasm_module_t>,
wasi_env: Option<&wasi_env_t>,
imports: &mut wasmer_named_extern_vec_t,
) -> bool {
wasi_get_unordered_imports_inner(store, module, wasi_env, imports).is_some()
}
fn wasi_get_unordered_imports_inner(
store: Option<&wasm_store_t>,
module: Option<&wasm_module_t>,
wasi_env: Option<&wasi_env_t>,
imports: &mut wasmer_named_extern_vec_t,
) -> Option<()> {
let store = store?;
let module = module?;
let wasi_env = wasi_env?;
let store = &store.inner;
let version = c_try!(
get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
})
);
let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version);
*imports = import_object
.into_iter()
.map(|((module, name), export)| {
let module = Box::new(module.into());
let name = Box::new(name.into());
let extern_inner = Extern::from_vm_export(store, export);
Box::new(wasmer_named_extern_t {
module,
name,
r#extern: Box::new(wasm_extern_t {
instance: None,
inner: extern_inner,
}),
})
})
.collect::<Vec<_>>()
.into();
Some(())
}

View File

@@ -4,15 +4,13 @@
mod capture_files;
pub use super::unstable::wasi::wasi_get_unordered_imports;
use super::{
externals::{wasm_extern_t, wasm_extern_vec_t, wasm_func_t, wasm_memory_t},
instance::wasm_instance_t,
module::wasm_module_t,
store::wasm_store_t,
types::wasm_name_t,
};
// required due to really weird Rust resolution rules for macros
// https://github.com/rust-lang/rust/issues/57966
use crate::error::{update_last_error, CApiError};
use std::cmp::min;
use std::convert::TryFrom;
@@ -168,7 +166,7 @@ pub extern "C" fn wasi_config_inherit_stdin(config: &mut wasi_config_t) {
#[allow(non_camel_case_types)]
pub struct wasi_env_t {
/// cbindgen:ignore
inner: WasiEnv,
pub(super) inner: WasiEnv,
}
/// Create a new WASI environment.
@@ -345,192 +343,6 @@ pub unsafe extern "C" fn wasi_get_wasi_version(module: &wasm_module_t) -> wasi_v
.unwrap_or(wasi_version_t::INVALID_VERSION)
}
/// Non-standard type wrapping `wasm_extern_t` with the addition of
/// two `wasm_name_t` respectively for the module name and the name of
/// the extern (very likely to be an import). This non-standard type
/// is used by the non-standard `wasi_get_unordered_imports` function.
///
/// The `module`, `name` and `extern` fields are all owned by this type.
#[allow(non_camel_case_types)]
#[derive(Clone)]
pub struct wasm_named_extern_t {
module: Box<wasm_name_t>,
name: Box<wasm_name_t>,
r#extern: Box<wasm_extern_t>,
}
wasm_declare_boxed_vec!(named_extern);
/// So. Let's explain a dirty hack. `cbindgen` reads the code and
/// collects symbols. What symbols do we need? None of the one
/// declared in `wasm.h`, but for non-standard API, we need to collect
/// all of them. The problem is that `wasm_named_extern_t` is the only
/// non-standard type where extra symbols are generated by a macro
/// (`wasm_declare_boxed_vec!`). If we want those macro-generated
/// symbols to be collected by `cbindgen`, we need to _expand_ the
/// crate (i.e. running something like `rustc -- -Zunstable-options
/// --pretty=expanded`). Expanding code is unstable and available only
/// on nightly compiler. We _don't want_ to use a nightly compiler
/// only for that. So how can we help `cbindgen` to _see_ those
/// symbols?
///
/// First solution: We write the C code directly in a file, which is
/// then included in the generated header file with the `cbindgen`
/// API. Problem, it's super easy to get it outdated, and it makes the
/// build process more complex.
///
/// Second solution: We write those symbols in a custom module, that
/// is just here for `cbindgen`, never used by our Rust code
/// (otherwise it's duplicated code), with no particular
/// implementation.
///
/// And that's why we have the following `cbindgen_hack`
/// module.
///
/// But this module must not be compiled by `rustc`. How to force
/// `rustc` to ignore a module? With conditional compilation. Because
/// `cbindgen` does not support conditional compilation, it will
/// always _ignore_ the `#[cfg]` attribute, and will always read the
/// content of the module.
///
/// Sorry.
#[doc(hidden)]
#[cfg(__cbindgen_hack__ = "yes")]
mod __cbindgen_hack__ {
use super::*;
#[repr(C)]
pub struct wasm_named_extern_vec_t {
pub size: usize,
pub data: *mut *mut wasm_named_extern_t,
}
#[no_mangle]
pub unsafe extern "C" fn wasm_named_extern_vec_new(
out: *mut wasm_named_extern_vec_t,
length: usize,
init: *const *mut wasm_named_extern_t,
) {
unimplemented!()
}
#[no_mangle]
pub unsafe extern "C" fn wasm_named_extern_vec_new_uninitialized(
out: *mut wasm_named_extern_vec_t,
length: usize,
) {
unimplemented!()
}
#[no_mangle]
pub unsafe extern "C" fn wasm_named_extern_vec_copy(
out_ptr: &mut wasm_named_extern_vec_t,
in_ptr: &wasm_named_extern_vec_t,
) {
unimplemented!()
}
#[no_mangle]
pub unsafe extern "C" fn wasm_named_extern_vec_delete(
ptr: Option<&mut wasm_named_extern_vec_t>,
) {
unimplemented!()
}
#[no_mangle]
pub unsafe extern "C" fn wasm_named_extern_vec_new_empty(out: *mut wasm_named_extern_vec_t) {
unimplemented!()
}
}
/// Non-standard function to get the module name of a
/// `wasm_named_extern_t`.
///
/// The returned value isn't owned by the caller.
#[no_mangle]
pub extern "C" fn wasm_named_extern_module(
named_extern: Option<&wasm_named_extern_t>,
) -> Option<&wasm_name_t> {
Some(named_extern?.module.as_ref())
}
/// Non-standard function to get the name of a `wasm_named_extern_t`.
///
/// The returned value isn't owned by the caller.
#[no_mangle]
pub extern "C" fn wasm_named_extern_name(
named_extern: Option<&wasm_named_extern_t>,
) -> Option<&wasm_name_t> {
Some(named_extern?.name.as_ref())
}
/// Non-standard function to get the wrapped extern of a
/// `wasm_named_extern_t`.
///
/// The returned value isn't owned by the caller.
#[no_mangle]
pub extern "C" fn wasm_named_extern_unwrap(
named_extern: Option<&wasm_named_extern_t>,
) -> Option<&wasm_extern_t> {
Some(named_extern?.r#extern.as_ref())
}
/// Non-standard function to get the imports needed for the WASI
/// implementation with no particular order. Each import has its
/// associated module name and name, so that it can be re-order later
/// based on the `wasm_module_t` requirements.
#[no_mangle]
pub unsafe extern "C" fn wasi_get_unordered_imports(
store: Option<&wasm_store_t>,
module: Option<&wasm_module_t>,
wasi_env: Option<&wasi_env_t>,
imports: &mut wasm_named_extern_vec_t,
) -> bool {
wasi_get_unordered_imports_inner(store, module, wasi_env, imports).is_some()
}
fn wasi_get_unordered_imports_inner(
store: Option<&wasm_store_t>,
module: Option<&wasm_module_t>,
wasi_env: Option<&wasi_env_t>,
imports: &mut wasm_named_extern_vec_t,
) -> Option<()> {
let store = store?;
let module = module?;
let wasi_env = wasi_env?;
let store = &store.inner;
let version = c_try!(
get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
})
);
let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version);
*imports = import_object
.into_iter()
.map(|((module, name), export)| {
let module = Box::new(module.into());
let name = Box::new(name.into());
let extern_inner = Extern::from_vm_export(store, export);
Box::new(wasm_named_extern_t {
module,
name,
r#extern: Box::new(wasm_extern_t {
instance: None,
inner: extern_inner,
}),
})
})
.collect::<Vec<_>>()
.into();
Some(())
}
/// Non-standard function to get the imports needed for the WASI
/// implementation ordered as expected by the `wasm_module_t`.
#[no_mangle]