diff --git a/lib/c-api/build.rs b/lib/c-api/build.rs index ab0683f58..660ffd478 100644 --- a/lib/c-api/build.rs +++ b/lib/c-api/build.rs @@ -469,31 +469,34 @@ fn exclude_items_from_wasm_c_api(builder: Builder) -> Builder { .exclude_item("wasi_version_t") .exclude_item("wasm_config_set_compiler") .exclude_item("wasm_config_set_engine") - .exclude_item("wasm_config_set_target") - .exclude_item("wasm_cpu_features_add") - .exclude_item("wasm_cpu_features_delete") - .exclude_item("wasm_cpu_features_new") - .exclude_item("wasm_cpu_features_t") - .exclude_item("wasm_module_name") - .exclude_item("wasm_module_set_name") - .exclude_item("wasm_named_extern_module") - .exclude_item("wasm_named_extern_name") - .exclude_item("wasm_named_extern_t") - .exclude_item("wasm_named_extern_unwrap") - .exclude_item("wasm_named_extern_vec_copy") - .exclude_item("wasm_named_extern_vec_delete") - .exclude_item("wasm_named_extern_vec_new") - .exclude_item("wasm_named_extern_vec_new_empty") - .exclude_item("wasm_named_extern_vec_new_uninitialized") - .exclude_item("wasm_target_delete") - .exclude_item("wasm_target_new") - .exclude_item("wasm_target_t") - .exclude_item("wasm_triple_delete") - .exclude_item("wasm_triple_new") - .exclude_item("wasm_triple_new_from_host") - .exclude_item("wasm_triple_t") .exclude_item("wasmer_compiler_t") + .exclude_item("wasmer_config_set_compiler") + .exclude_item("wasmer_config_set_engine") + .exclude_item("wasmer_config_set_target") + .exclude_item("wasmer_cpu_features_add") + .exclude_item("wasmer_cpu_features_delete") + .exclude_item("wasmer_cpu_features_new") + .exclude_item("wasmer_cpu_features_t") .exclude_item("wasmer_engine_t") + .exclude_item("wasmer_module_name") + .exclude_item("wasmer_module_set_name") + .exclude_item("wasmer_named_extern_module") + .exclude_item("wasmer_named_extern_name") + .exclude_item("wasmer_named_extern_t") + .exclude_item("wasmer_named_extern_unwrap") + .exclude_item("wasmer_named_extern_vec_copy") + .exclude_item("wasmer_named_extern_vec_delete") + .exclude_item("wasmer_named_extern_vec_new") + .exclude_item("wasmer_named_extern_vec_new_empty") + .exclude_item("wasmer_named_extern_vec_new_uninitialized") + .exclude_item("wasmer_target_delete") + .exclude_item("wasmer_target_new") + .exclude_item("wasmer_target_t") + .exclude_item("wasmer_triple_delete") + .exclude_item("wasmer_triple_new") + .exclude_item("wasmer_triple_new_from_host") + .exclude_item("wasmer_triple_t") + .exclude_item("wasmer_wat2wasm") .exclude_item("wat2wasm") } diff --git a/lib/c-api/src/error.rs b/lib/c-api/src/error.rs index 9011303dc..991f1c4f6 100644 --- a/lib/c-api/src/error.rs +++ b/lib/c-api/src/error.rs @@ -21,7 +21,7 @@ //! wasm_byte_vec_t wat; //! wasmer_byte_vec_new_from_string(&wat, "(foobar)"); //! wasm_byte_vec_t wasm; -//! wat2wasm(&wat, &wasm); +//! wasmer_wat2wasm(&wat, &wasm); //! //! int error_length = wasmer_last_error_length(); //! diff --git a/lib/c-api/src/wasm_c_api/engine.rs b/lib/c-api/src/wasm_c_api/engine.rs index bfc3e4f09..c031bfba2 100644 --- a/lib/c-api/src/wasm_c_api/engine.rs +++ b/lib/c-api/src/wasm_c_api/engine.rs @@ -1,5 +1,5 @@ -pub use super::unstable::engine::wasm_config_set_target; -use super::unstable::target_lexicon::wasm_target_t; +pub use super::unstable::engine::wasmer_config_set_target; +use super::unstable::target_lexicon::wasmer_target_t; use crate::error::{update_last_error, CApiError}; use cfg_if::cfg_if; use std::sync::Arc; @@ -95,7 +95,7 @@ pub struct wasm_config_t { engine: wasmer_engine_t, #[cfg(feature = "compiler")] compiler: wasmer_compiler_t, - pub(super) target: Option>, + pub(super) target: Option>, } /// Create a new default Wasmer configuration. @@ -181,7 +181,7 @@ pub extern "C" fn wasm_config_delete(_config: Option>) {} /// wasm_config_t* config = wasm_config_new(); /// /// // Use the Cranelift compiler. -/// wasm_config_set_compiler(config, CRANELIFT); +/// wasmer_config_set_compiler(config, CRANELIFT); /// /// // Create the engine. /// wasm_engine_t* engine = wasm_engine_new_with_config(config); @@ -200,13 +200,25 @@ pub extern "C" fn wasm_config_delete(_config: Option>) {} /// ``` #[cfg(feature = "compiler")] #[no_mangle] -pub extern "C" fn wasm_config_set_compiler( +pub extern "C" fn wasmer_config_set_compiler( config: &mut wasm_config_t, compiler: wasmer_compiler_t, ) { config.compiler = compiler; } +/// Please use `wasmer_config_set_compiler` instead. +/// +/// cbindgen:prefix=DEPRECATED("This function has been renamed `wasmer_config_set_compiler`.") +#[cfg(feature = "compiler")] +#[no_mangle] +pub extern "C" fn wasm_config_set_compiler( + config: &mut wasm_config_t, + compiler: wasmer_compiler_t, +) { + wasmer_config_set_compiler(config, compiler); +} + /// Updates the configuration to specify a particular engine to use. /// /// This is a Wasmer-specific function. @@ -224,7 +236,7 @@ pub extern "C" fn wasm_config_set_compiler( /// wasm_config_t* config = wasm_config_new(); /// /// // Use the JIT engine. -/// wasm_config_set_engine(config, JIT); +/// wasmer_config_set_engine(config, JIT); /// /// // Create the engine. /// wasm_engine_t* engine = wasm_engine_new_with_config(config); @@ -242,10 +254,18 @@ pub extern "C" fn wasm_config_set_compiler( /// # } /// ``` #[no_mangle] -pub extern "C" fn wasm_config_set_engine(config: &mut wasm_config_t, engine: wasmer_engine_t) { +pub extern "C" fn wasmer_config_set_engine(config: &mut wasm_config_t, engine: wasmer_engine_t) { config.engine = engine; } +/// Please use `wasmer_config_set_engine` instead. +/// +/// cbindgen:prefix=DEPRECATED("This function has been renamed `wasmer_config_set_engine`.") +#[no_mangle] +pub extern "C" fn wasm_config_set_engine(config: &mut wasm_config_t, engine: wasmer_engine_t) { + wasmer_config_set_engine(config, engine); +} + /// An engine is used by the store to drive the compilation and the /// execution of a WebAssembly module. /// diff --git a/lib/c-api/src/wasm_c_api/externals/global.rs b/lib/c-api/src/wasm_c_api/externals/global.rs index b7b53b3e5..e13fc62c8 100644 --- a/lib/c-api/src/wasm_c_api/externals/global.rs +++ b/lib/c-api/src/wasm_c_api/externals/global.rs @@ -127,7 +127,7 @@ mod tests { wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module (global $global (export \"global\") f32 (f32.const 1)))"); wasm_byte_vec_t wasm_bytes; - wat2wasm(&wat, &wasm_bytes); + wasmer_wat2wasm(&wat, &wasm_bytes); wasm_module_t* module = wasm_module_new(store, &wasm_bytes); wasm_extern_vec_t import_object = WASM_EMPTY_VEC; wasm_instance_t* instance = wasm_instance_new(store, module, &import_object, NULL); diff --git a/lib/c-api/src/wasm_c_api/externals/mod.rs b/lib/c-api/src/wasm_c_api/externals/mod.rs index 23dff955e..a7d4aac18 100644 --- a/lib/c-api/src/wasm_c_api/externals/mod.rs +++ b/lib/c-api/src/wasm_c_api/externals/mod.rs @@ -156,7 +156,7 @@ mod tests { " (func (export \"function\")))" ); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); wasm_module_t* module = wasm_module_new(store, &wasm); assert(module); diff --git a/lib/c-api/src/wasm_c_api/instance.rs b/lib/c-api/src/wasm_c_api/instance.rs index c562daa41..798fe138c 100644 --- a/lib/c-api/src/wasm_c_api/instance.rs +++ b/lib/c-api/src/wasm_c_api/instance.rs @@ -118,7 +118,7 @@ pub unsafe extern "C" fn wasm_instance_delete(_instance: Option { + 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 [](out: *mut []) { + pub unsafe extern "C" fn [<$prefix _ $name _vec_new_empty>](out: *mut [<$prefix _ $name _vec_t>]) { // TODO: actually implement this - [](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 [] { + pub struct [<$prefix _ $name _vec_t>] { pub size: usize, - pub data: *mut [], + pub data: *mut [<$prefix _ $name _t>], } - impl Clone for [] { + 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]>> for [] { - fn from(mut vec: Vec<[]>) -> Self { + impl<'a> From]>> 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<[]> + Clone> From<&'a [T]> for [] { + 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::]>>() + .collect::]>>() .into_boxed_slice(); let data = copied_data.as_mut_ptr(); ::std::mem::forget(copied_data); @@ -142,8 +150,8 @@ int main() { } } - impl [] { - pub unsafe fn into_slice(&self) -> Option<&[[]]>{ + 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 [[]]>{ + 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 [](out: *mut [], length: usize, init: *mut []) { - let mut bytes: Vec<[]> = 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 [](out: *mut [], length: usize) { - let mut bytes: Vec<[]> = 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 []( - out_ptr: &mut [], + pub unsafe extern "C" fn [<$prefix _ $name _vec_copy>]( + out_ptr: &mut [<$prefix _ $name _vec_t>], in_ptr: & []) { *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 [](ptr: Option<&mut []>) { + 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 [] { + pub struct [<$prefix _ $name _vec_t>] { pub size: usize, - pub data: *mut *mut [], + pub data: *mut *mut [<$prefix _ $name _t>], } - impl Clone for [] { + 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]>> = self.data as _; + let data: *mut Option]>> = 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 []; + 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]>>> for [] { - fn from(other: Vec]>>) -> Self { - let boxed_slice: Box<[Box<[]>]> = other.into_boxed_slice(); - let mut boxed_slice: Box<[*mut []]> = unsafe { ::std::mem::transmute(boxed_slice) }; + impl<'a> From]>>> for [<$prefix _ $name _vec_t>] { + fn from(other: Vec]>>) -> 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<[]> + Clone> From<&'a [T]> for [] { + 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::]>>() + .collect::]>>() .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 [] { - pub unsafe fn into_slice(&self) -> Option<&[Box<[]>]>{ + 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 []] = ::std::slice::from_raw_parts(self.data, self.size); - let slice: &[Box<[]>] = ::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 [](out: *mut [], length: usize, init: *const *mut []) { - let mut bytes: Vec<*mut []> = 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 [](out: *mut [], length: usize) { - let mut bytes: Vec<*mut []> = 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 []( - out_ptr: &mut [], - in_ptr: & []) + 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 [](ptr: Option<&mut []>) { + 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]>>; - let _data: Vec]>>> = Vec::from_raw_parts(data, vec.size, vec.size); + let data = vec.data as *mut Option]>>; + let _data: Vec]>>> = 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 [](_arg: *const []) -> *mut [] { + 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 [] {} + pub struct [<$prefix _ $name _t>] {} #[no_mangle] - pub extern "C" fn [](_arg: *mut []) { + pub extern "C" fn [<$prefix _ $name _delete>](_arg: *mut [<$prefix _ $name _t>]) { todo!("in generated delete") } } diff --git a/lib/c-api/src/wasm_c_api/mod.rs b/lib/c-api/src/wasm_c_api/mod.rs index 890393785..65459d5d8 100644 --- a/lib/c-api/src/wasm_c_api/mod.rs +++ b/lib/c-api/src/wasm_c_api/mod.rs @@ -94,7 +94,7 @@ pub mod externals; /// wasm_byte_vec_t wat; /// wasmer_byte_vec_new_from_string(&wat, "(module)"); /// wasm_byte_vec_t wasm; -/// wat2wasm(&wat, &wasm); +/// wasmer_wat2wasm(&wat, &wasm); /// /// // Create the module. /// wasm_module_t* module = wasm_module_new(store, &wasm); @@ -152,7 +152,7 @@ pub mod instance; /// wasm_byte_vec_t wat; /// wasmer_byte_vec_new_from_string(&wat, "(module)"); /// wasm_byte_vec_t wasm; -/// wat2wasm(&wat, &wasm); +/// wasmer_wat2wasm(&wat, &wasm); /// /// // Create the module. /// wasm_module_t* module = wasm_module_new(store, &wasm); @@ -302,7 +302,7 @@ pub mod wasi; /// /// // Our Wasm bytes. /// wasm_byte_vec_t wasm; -/// wat2wasm(&wat, &wasm); +/// wasmer_wat2wasm(&wat, &wasm); /// /// // It works! /// assert(wasm.size > 0); diff --git a/lib/c-api/src/wasm_c_api/module.rs b/lib/c-api/src/wasm_c_api/module.rs index aee356b03..e718716bc 100644 --- a/lib/c-api/src/wasm_c_api/module.rs +++ b/lib/c-api/src/wasm_c_api/module.rs @@ -76,7 +76,7 @@ pub unsafe extern "C" fn wasm_module_delete(_module: Option>) /// wasm_byte_vec_t wat; /// wasmer_byte_vec_new_from_string(&wat, "(module)"); /// wasm_byte_vec_t wasm; -/// wat2wasm(&wat, &wasm); +/// wasmer_wat2wasm(&wat, &wasm); /// /// // Validate that the WebAssembly module is correct. /// assert(wasm_module_validate(store, &wasm)); @@ -150,7 +150,7 @@ pub unsafe extern "C" fn wasm_module_validate( /// " (memory (export \"memory\") 1))" /// ); /// wasm_byte_vec_t wasm; -/// wat2wasm(&wat, &wasm); +/// wasmer_wat2wasm(&wat, &wasm); /// /// // Create the module. /// wasm_module_t* module = wasm_module_new(store, &wasm); @@ -274,7 +274,7 @@ pub unsafe extern "C" fn wasm_module_exports( /// " (import \"ns\" \"memory\" (memory 3 4)))" /// ); /// wasm_byte_vec_t wasm; -/// wat2wasm(&wat, &wasm); +/// wasmer_wat2wasm(&wat, &wasm); /// /// // Create the module. /// wasm_module_t* module = wasm_module_new(store, &wasm); @@ -426,7 +426,7 @@ pub unsafe extern "C" fn wasm_module_imports( /// " (memory (export \"memory\") 1))" /// ); /// wasm_byte_vec_t wasm; -/// wat2wasm(&wat, &wasm); +/// wasmer_wat2wasm(&wat, &wasm); /// /// // Create the module. /// wasm_module_t* module = wasm_module_new(store, &wasm); @@ -533,7 +533,7 @@ mod tests { wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module)"); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); assert(wasm_module_validate(store, &wasm)); @@ -560,7 +560,7 @@ mod tests { wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module)"); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); wasm_module_t* module = wasm_module_new(store, &wasm); assert(module); @@ -596,7 +596,7 @@ mod tests { " (memory (export \"memory\") 1))" ); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); wasm_module_t* module = wasm_module_new(store, &wasm); assert(module); @@ -704,7 +704,7 @@ mod tests { " (import \"ns\" \"memory\" (memory 3 4)))" ); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); wasm_module_t* module = wasm_module_new(store, &wasm); assert(module); @@ -815,7 +815,7 @@ mod tests { wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module)"); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); wasm_module_t* module = wasm_module_new(store, &wasm); assert(module); @@ -856,7 +856,7 @@ mod tests { " (memory (export \"memory\") 1))" ); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); wasm_module_t* module = wasm_module_new(store, &wasm); assert(module); diff --git a/lib/c-api/src/wasm_c_api/unstable/engine.rs b/lib/c-api/src/wasm_c_api/unstable/engine.rs index 92264a9a6..e2e8a1770 100644 --- a/lib/c-api/src/wasm_c_api/unstable/engine.rs +++ b/lib/c-api/src/wasm_c_api/unstable/engine.rs @@ -2,7 +2,7 @@ //! `wasm_engine_t` and siblings. use super::super::engine::wasm_config_t; -use super::target_lexicon::wasm_target_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,11 +21,11 @@ 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); +/// wasmer_config_set_target(config, target); /// } /// /// // Create the engine. @@ -44,6 +44,9 @@ use super::target_lexicon::wasm_target_t; /// # } /// ``` #[no_mangle] -pub extern "C" fn wasm_config_set_target(config: &mut wasm_config_t, target: Box) { +pub extern "C" fn wasmer_config_set_target( + config: &mut wasm_config_t, + target: Box, +) { config.target = Some(target); } diff --git a/lib/c-api/src/wasm_c_api/unstable/mod.rs b/lib/c-api/src/wasm_c_api/unstable/mod.rs index 5b673aec6..bf6586894 100644 --- a/lib/c-api/src/wasm_c_api/unstable/mod.rs +++ b/lib/c-api/src/wasm_c_api/unstable/mod.rs @@ -1,3 +1,6 @@ pub mod engine; pub mod module; pub mod target_lexicon; + +#[cfg(feature = "wasi")] +pub mod wasi; diff --git a/lib/c-api/src/wasm_c_api/unstable/module.rs b/lib/c-api/src/wasm_c_api/unstable/module.rs index 6787a8365..5ff5e0185 100644 --- a/lib/c-api/src/wasm_c_api/unstable/module.rs +++ b/lib/c-api/src/wasm_c_api/unstable/module.rs @@ -28,14 +28,14 @@ use std::sync::Arc; /// wasmer_byte_vec_new_from_string(&wat, "(module $moduleName)"); /// // ^~~~~~~~~~~ that's the name! /// wasm_byte_vec_t wasm; -/// wat2wasm(&wat, &wasm); +/// wasmer_wat2wasm(&wat, &wasm); /// /// // Create the module. /// wasm_module_t* module = wasm_module_new(store, &wasm); /// /// // 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, @@ -94,7 +94,7 @@ pub unsafe extern "C" fn wasm_module_name( /// wasm_byte_vec_t wat; /// wasmer_byte_vec_new_from_string(&wat, "(module)"); /// wasm_byte_vec_t wasm; -/// wat2wasm(&wat, &wasm); +/// wasmer_wat2wasm(&wat, &wasm); /// /// // Create the module. /// wasm_module_t* module = wasm_module_new(store, &wasm); @@ -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, diff --git a/lib/c-api/src/wasm_c_api/unstable/target_lexicon.rs b/lib/c-api/src/wasm_c_api/unstable/target_lexicon.rs index c92a2edcb..bea0ec64e 100644 --- a/lib/c-api/src/wasm_c_api/unstable/target_lexicon.rs +++ b/lib/c-api/src/wasm_c_api/unstable/target_lexicon.rs @@ -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>, - cpu_features: Option>, -) -> Option> { +pub extern "C" fn wasmer_target_new( + triple: Option>, + cpu_features: Option>, +) -> Option> { 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>) {} +pub extern "C" fn wasmer_target_delete(_target: Option>) {} /// Unstable non-standard Wasmer-specific API to represent a target /// “triple”. @@ -118,10 +118,10 @@ pub extern "C" fn wasm_target_delete(_target: Option>) {} /// 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>) {} /// # } /// ``` /// -/// 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> { +) -> Option> { 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 { - Box::new(wasm_triple_t { +pub extern "C" fn wasmer_triple_new_from_host() -> Box { + 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>) {} +pub extern "C" fn wasmer_triple_delete(_triple: Option>) {} /// 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>) {} /// # /// 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>) {} /// # } /// ``` #[allow(non_camel_case_types)] -pub struct wasm_cpu_features_t { +pub struct wasmer_cpu_features_t { inner: EnumSet, } -/// 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 { - Box::new(wasm_cpu_features_t { +pub extern "C" fn wasmer_cpu_features_new() -> Box { + 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>) {} +pub extern "C" fn wasmer_cpu_features_delete(_cpu_features: Option>) {} /// 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 { diff --git a/lib/c-api/src/wasm_c_api/unstable/wasi.rs b/lib/c-api/src/wasm_c_api/unstable/wasi.rs new file mode 100644 index 000000000..8eb0a5fa2 --- /dev/null +++ b/lib/c-api/src/wasm_c_api/unstable/wasi.rs @@ -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, + name: Box, + r#extern: Box, +} + +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::>() + .into(); + + Some(()) +} diff --git a/lib/c-api/src/wasm_c_api/wasi/mod.rs b/lib/c-api/src/wasm_c_api/wasi/mod.rs index 7ac5bae3b..6bddcbadb 100644 --- a/lib/c-api/src/wasm_c_api/wasi/mod.rs +++ b/lib/c-api/src/wasm_c_api/wasi/mod.rs @@ -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, - name: Box, - r#extern: Box, -} - -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::>() - .into(); - - Some(()) -} - /// Non-standard function to get the imports needed for the WASI /// implementation ordered as expected by the `wasm_module_t`. #[no_mangle] @@ -617,7 +429,7 @@ mod tests { wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module (import \"wasi_unstable\" \"args_get\" (func (param i32 i32) (result i32))))"); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); wasm_module_t* module = wasm_module_new(store, &wasm); assert(module); @@ -648,7 +460,7 @@ mod tests { wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module (import \"wasi_snapshot_preview1\" \"args_get\" (func (param i32 i32) (result i32))))"); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); wasm_module_t* module = wasm_module_new(store, &wasm); assert(module); @@ -679,7 +491,7 @@ mod tests { wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module (import \"wasi_snpsht_prvw1\" \"args_get\" (func (param i32 i32) (result i32))))"); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); wasm_module_t* module = wasm_module_new(store, &wasm); assert(module); diff --git a/lib/c-api/src/wasm_c_api/wat.rs b/lib/c-api/src/wasm_c_api/wat.rs index 07d634b88..ca1200976 100644 --- a/lib/c-api/src/wasm_c_api/wat.rs +++ b/lib/c-api/src/wasm_c_api/wat.rs @@ -10,7 +10,7 @@ use super::types::wasm_byte_vec_t; /// See the module's documentation. #[cfg(feature = "wat")] #[no_mangle] -pub unsafe extern "C" fn wat2wasm(wat: &wasm_byte_vec_t, out: &mut wasm_byte_vec_t) { +pub unsafe extern "C" fn wasmer_wat2wasm(wat: &wasm_byte_vec_t, out: &mut wasm_byte_vec_t) { let wat: &[u8] = match wat.into_slice() { Some(v) => v, _ => { @@ -32,6 +32,15 @@ pub unsafe extern "C" fn wat2wasm(wat: &wasm_byte_vec_t, out: &mut wasm_byte_vec *out = result; } +/// Please use `wasmer_wat2wasm`. +/// +/// cbindgen:prefix=DEPRECATED("This function has been renamed `wasmer_wat2wasm`.") +#[cfg(feature = "wat")] +#[no_mangle] +pub unsafe extern "C" fn wat2wasm(wat: &wasm_byte_vec_t, out: &mut wasm_byte_vec_t) { + wasmer_wat2wasm(wat, out); +} + #[cfg(test)] mod tests { use inline_c::assert_c; @@ -45,7 +54,7 @@ mod tests { wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module)"); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); assert(wasm.data); assert(wasm.size == 8); @@ -78,7 +87,7 @@ mod tests { wasm_byte_vec_t wat; wasmer_byte_vec_new_from_string(&wat, "(module"); wasm_byte_vec_t wasm; - wat2wasm(&wat, &wasm); + wasmer_wat2wasm(&wat, &wasm); assert(!wasm.data); assert(wasmer_last_error_length() > 0); diff --git a/lib/c-api/wasmer_wasm.h b/lib/c-api/wasmer_wasm.h index f13aab209..982c95883 100644 --- a/lib/c-api/wasmer_wasm.h +++ b/lib/c-api/wasmer_wasm.h @@ -134,21 +134,21 @@ typedef struct wasi_config_t wasi_config_t; typedef struct wasi_env_t wasi_env_t; #endif -typedef struct wasm_cpu_features_t wasm_cpu_features_t; +typedef struct wasmer_cpu_features_t wasmer_cpu_features_t; #if defined(WASMER_WASI_ENABLED) -typedef struct wasm_named_extern_t wasm_named_extern_t; +typedef struct wasmer_named_extern_t wasmer_named_extern_t; #endif -typedef struct wasm_target_t wasm_target_t; +typedef struct wasmer_target_t wasmer_target_t; -typedef struct wasm_triple_t wasm_triple_t; +typedef struct wasmer_triple_t wasmer_triple_t; #if defined(WASMER_WASI_ENABLED) typedef struct { uintptr_t size; - wasm_named_extern_t **data; -} wasm_named_extern_vec_t; + wasmer_named_extern_t **data; +} wasmer_named_extern_vec_t; #endif #ifdef __cplusplus @@ -238,7 +238,7 @@ wasm_func_t *wasi_get_start_function(wasm_instance_t *instance); bool wasi_get_unordered_imports(const wasm_store_t *store, const wasm_module_t *module, const wasi_env_t *wasi_env, - wasm_named_extern_vec_t *imports); + wasmer_named_extern_vec_t *imports); #endif #if defined(WASMER_WASI_ENABLED) @@ -246,72 +246,82 @@ wasi_version_t wasi_get_wasi_version(const wasm_module_t *module); #endif #if defined(WASMER_COMPILER_ENABLED) -void wasm_config_set_compiler(wasm_config_t *config, wasmer_compiler_t compiler); +DEPRECATED("This function has been renamed `wasmer_config_set_compiler`.") +void wasm_config_set_compiler(wasm_config_t *config, + wasmer_compiler_t compiler); #endif -void wasm_config_set_engine(wasm_config_t *config, wasmer_engine_t engine); +DEPRECATED("This function has been renamed `wasmer_config_set_engine`.") +void wasm_config_set_engine(wasm_config_t *config, + wasmer_engine_t engine); -void wasm_config_set_target(wasm_config_t *config, wasm_target_t *target); - -bool wasm_cpu_features_add(wasm_cpu_features_t *cpu_features, const wasm_name_t *feature); - -void wasm_cpu_features_delete(wasm_cpu_features_t *_cpu_features); - -wasm_cpu_features_t *wasm_cpu_features_new(void); - -void wasm_module_name(const wasm_module_t *module, wasm_name_t *out); - -bool wasm_module_set_name(wasm_module_t *module, const wasm_name_t *name); - -#if defined(WASMER_WASI_ENABLED) -const wasm_name_t *wasm_named_extern_module(const wasm_named_extern_t *named_extern); +#if defined(WASMER_COMPILER_ENABLED) +void wasmer_config_set_compiler(wasm_config_t *config, wasmer_compiler_t compiler); #endif -#if defined(WASMER_WASI_ENABLED) -const wasm_name_t *wasm_named_extern_name(const wasm_named_extern_t *named_extern); -#endif +void wasmer_config_set_engine(wasm_config_t *config, wasmer_engine_t engine); -#if defined(WASMER_WASI_ENABLED) -const wasm_extern_t *wasm_named_extern_unwrap(const wasm_named_extern_t *named_extern); -#endif +void wasmer_config_set_target(wasm_config_t *config, wasmer_target_t *target); -#if defined(WASMER_WASI_ENABLED) -void wasm_named_extern_vec_copy(wasm_named_extern_vec_t *out_ptr, - const wasm_named_extern_vec_t *in_ptr); -#endif +bool wasmer_cpu_features_add(wasmer_cpu_features_t *cpu_features, const wasm_name_t *feature); -#if defined(WASMER_WASI_ENABLED) -void wasm_named_extern_vec_delete(wasm_named_extern_vec_t *ptr); -#endif +void wasmer_cpu_features_delete(wasmer_cpu_features_t *_cpu_features); -#if defined(WASMER_WASI_ENABLED) -void wasm_named_extern_vec_new(wasm_named_extern_vec_t *out, - uintptr_t length, - wasm_named_extern_t *const *init); -#endif - -#if defined(WASMER_WASI_ENABLED) -void wasm_named_extern_vec_new_empty(wasm_named_extern_vec_t *out); -#endif - -#if defined(WASMER_WASI_ENABLED) -void wasm_named_extern_vec_new_uninitialized(wasm_named_extern_vec_t *out, uintptr_t length); -#endif - -void wasm_target_delete(wasm_target_t *_target); - -wasm_target_t *wasm_target_new(wasm_triple_t *triple, wasm_cpu_features_t *cpu_features); - -void wasm_triple_delete(wasm_triple_t *_triple); - -wasm_triple_t *wasm_triple_new(const wasm_name_t *triple); - -wasm_triple_t *wasm_triple_new_from_host(void); +wasmer_cpu_features_t *wasmer_cpu_features_new(void); int wasmer_last_error_length(void); int wasmer_last_error_message(char *buffer, int length); +void wasmer_module_name(const wasm_module_t *module, wasm_name_t *out); + +bool wasmer_module_set_name(wasm_module_t *module, const wasm_name_t *name); + +#if defined(WASMER_WASI_ENABLED) +const wasm_name_t *wasmer_named_extern_module(const wasmer_named_extern_t *named_extern); +#endif + +#if defined(WASMER_WASI_ENABLED) +const wasm_name_t *wasmer_named_extern_name(const wasmer_named_extern_t *named_extern); +#endif + +#if defined(WASMER_WASI_ENABLED) +const wasm_extern_t *wasmer_named_extern_unwrap(const wasmer_named_extern_t *named_extern); +#endif + +#if defined(WASMER_WASI_ENABLED) +void wasmer_named_extern_vec_copy(wasmer_named_extern_vec_t *out_ptr, + const wasmer_named_extern_vec_t *in_ptr); +#endif + +#if defined(WASMER_WASI_ENABLED) +void wasmer_named_extern_vec_delete(wasmer_named_extern_vec_t *ptr); +#endif + +#if defined(WASMER_WASI_ENABLED) +void wasmer_named_extern_vec_new(wasmer_named_extern_vec_t *out, + uintptr_t length, + wasmer_named_extern_t *const *init); +#endif + +#if defined(WASMER_WASI_ENABLED) +void wasmer_named_extern_vec_new_empty(wasmer_named_extern_vec_t *out); +#endif + +#if defined(WASMER_WASI_ENABLED) +void wasmer_named_extern_vec_new_uninitialized(wasmer_named_extern_vec_t *out, uintptr_t length); +#endif + +void wasmer_target_delete(wasmer_target_t *_target); + +wasmer_target_t *wasmer_target_new(wasmer_triple_t *triple, wasmer_cpu_features_t *cpu_features); + +void wasmer_triple_delete(wasmer_triple_t *_triple); + +wasmer_triple_t *wasmer_triple_new(const wasm_name_t *triple); + +wasmer_triple_t *wasmer_triple_new_from_host(void); + const char *wasmer_version(void); uint8_t wasmer_version_major(void); @@ -322,7 +332,11 @@ uint8_t wasmer_version_patch(void); const char *wasmer_version_pre(void); -void wat2wasm(const wasm_byte_vec_t *wat, wasm_byte_vec_t *out); +void wasmer_wat2wasm(const wasm_byte_vec_t *wat, wasm_byte_vec_t *out); + +DEPRECATED("This function has been renamed `wasmer_wat2wasm`.") +void wat2wasm(const wasm_byte_vec_t *wat, + wasm_byte_vec_t *out); #ifdef __cplusplus } // extern "C"