chore(c-api) Formalize API prefixes.

The new rule is the following:

* `wasm_` for the standard C API,
* `wasmer_` or `wasi_` for the Wasmer non-standard C API.

For all symbols inside the `unstable` module, the renaming `wasm_` to
`wasmer_` is done without deprecations. It was clear that those API
were unstable.

For all the other symbols, symbols have been renamed to `wasmer_` but
the old symbols have been kept with deprecation warnings.

Special note: The `wasm_named_extern_t` type (and associated
functions) was in `wasi` by mistake. Its place was in the `unstable`
module. This patch also fixes that.

The `wasm_declare_vec_*` macros have been updated to support a default
prefix, or a user-defined prefix. It's now possible to write
`wasm_declare_boxed_vec!(foo);` to get all the API prefixed by `wasm_`
(as previously), or `wasm_declare_boxed_vec!(foo, wasmer);` to prefix
with `wasmer_`.

A user not using symbols from the `unstable` module will continue to
get working code, modulo some deprecations, after this patch.
This commit is contained in:
Ivan Enderlin
2021-02-11 23:30:27 +01:00
parent f21556f85f
commit fd6689c580
17 changed files with 536 additions and 453 deletions

View File

@@ -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<Box<wasm_target_t>>,
pub(super) target: Option<Box<wasmer_target_t>>,
}
/// Create a new default Wasmer configuration.
@@ -181,7 +181,7 @@ pub extern "C" fn wasm_config_delete(_config: Option<Box<wasm_config_t>>) {}
/// 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<Box<wasm_config_t>>) {}
/// ```
#[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.
///

View File

@@ -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);

View File

@@ -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);

View File

@@ -118,7 +118,7 @@ pub unsafe extern "C" fn wasm_instance_delete(_instance: Option<Box<wasm_instanc
/// " (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);
@@ -245,7 +245,7 @@ mod tests {
" call $sum))"
);
wasm_byte_vec_t wasm;
wat2wasm(&wat, &wasm);
wasmer_wat2wasm(&wat, &wasm);
// Create the module.
wasm_module_t* module = wasm_module_new(store, &wasm);

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

@@ -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);

View File

@@ -76,7 +76,7 @@ pub unsafe extern "C" fn wasm_module_delete(_module: Option<Box<wasm_module_t>>)
/// 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);

View File

@@ -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<wasm_target_t>) {
pub extern "C" fn wasmer_config_set_target(
config: &mut wasm_config_t,
target: Box<wasmer_target_t>,
) {
config.target = Some(target);
}

View File

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

View File

@@ -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,

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]
@@ -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);

View File

@@ -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);