`assert.h` does nothing when the code is compiled in release
mode. That's not what we want :-). Let's reimplement `assert` as
`wasmer_assert`, which is close to the original `assert` function.
There is no memory leaks with those functions as far as I understand this code.
Check the following code as a fun playground, which reproduces a
typical use of `wasm_functype_params` or `_results` that both return a
`wasm_valtype_vec_t`:
```rust
struct wasm_valtype_t { x: u8 }
impl Drop for wasm_valtype_t {
fn drop(&mut self) {
println!("wasm_valtype_t {{ {} }} dropped!", self.x)
}
}
struct wasm_valtype_vec_t {
size: usize,
data: *mut *mut wasm_valtype_t,
}
unsafe fn wasm_valtype_vec_delete(ptr: *mut wasm_valtype_vec_t) {
let vec = &mut *ptr;
if !vec.data.is_null() {
let data: Vec<*mut wasm_valtype_t> = Vec::from_raw_parts(vec.data, vec.size, vec.size);
let _data: Vec<Box<wasm_valtype_t>> = ::std::mem::transmute(data);
vec.data = ::std::ptr::null_mut();
vec.size = 0;
}
}
fn main() {
let x = Box::into_raw(Box::new(wasm_valtype_t { x: 1 }));
let y = Box::into_raw(Box::new(wasm_valtype_t { x: 2 }));
let z = Box::into_raw(Box::new(wasm_valtype_t { x: 3 }));
let mut valtypes: Vec<*mut wasm_valtype_t> = vec![x, y, z];
let vec = Box::into_raw(Box::new(wasm_valtype_vec_t {
size: valtypes.len(),
data: valtypes.as_mut_ptr(),
}));
::std::mem::forget(valtypes);
unsafe { wasm_valtype_vec_delete(vec as *mut _); }
}
```
It prints:
```
wasm_valtype_t { 1 } dropped!
wasm_valtype_t { 2 } dropped!
wasm_valtype_t { 3 } dropped!
```
All `wasm_valtype_t` are dropped correctly. Since they contain an
owned value, there is no leaks here.
Remove the `build.rs` hack to add helper functions that are used only
in our tests. Now the tests can use the `tests/wasmer_wasm.h` file,
which simplifies the test workflow: No need to hack the build script
or anything if we want to add a helper function.
It's very similar to `wasm_name_new_from_string` but for
`wasm_byte_vec_t`. It does not make sense to get that in the standard,
but it's very useful when writing tests.
1765: doc(c-api) Update documentation. r=Hywan a=Hywan
# Description
This patch adds documentation to the `engine.rs` module.
# Review
- [ ] ~Add a short description of the the change to the CHANGELOG.md file~ not necessary
Co-authored-by: Ivan Enderlin <ivan@mnt.io>
The `wasm_trap_t**` argument of `wasm_instance_new` represents an
output pointer to a `wasm_trap_t*`, not an array of
`wasm_trap_t*`. This patch updates the code accordingly.
When running `Instance::new`, it can error with an
`InstantiationError`. There is 2 scenarii:
1. Either it's a `InstantiationError::Link`. In this case, the
`wasm_instance_new` function must return `NULL` and register the
error in the Wasmer error registry.
2. Either it's a `InstantiationError::Start`. In this case, the
`wasm_instance_new` function must return `NULL` and the error must be
converted into a `wasm_trap_t`, which is stored in the `wasm_trap_t**`
array. This array is initialized by `wasm_instance_new` itself.
1751: feat(c-api) Correctly implement “trap” in `wasm_func_new*` r=Hywan a=Hywan
The implementation of “trap” in `wasm_func_new` was “incorrect”. It's
more idiomatic to return a `RuntimeError` than raising it in this
case, so that we don't duplicate locations where runtime errors are
raised.
The implementation of “trap” in `wasm_func_new_with_env` was
missing. This patch implements a similar strategy than the sibling
function.
Fixes https://github.com/wasmerio/wasmer/issues/1744.
Co-authored-by: Ivan Enderlin <ivan@mnt.io>
The implementation of “trap” in `wasm_func_new` was “incorrect”. It's
more idiomatic to return a `RuntimeError` than raising it in this
case, so that we don't duplicate locations where runtime errors are
raised.
The implementation of “trap” in `wasm_func_new_with_env` was
missing. This patch implements a similar strategy than the sibling
function.
1725: feat(c-api) Implement `wasm_func_type` r=MarkMcCaskey a=Hywan
This patch creates a `wasm_functype_t::new` constructor, that is used
by the new `wasm_func_type` function, and the existing
`wasm_functype_new_inner` function.
Note: `wasm_func_type` is defined in 193d7c8ce1/lib/c-api/tests/wasm_c_api/wasm-c-api/include/wasm.h (L421).
# Review
- [x] Add a short description of the the change to the CHANGELOG.md file
Co-authored-by: Ivan Enderlin <ivan@mnt.io>