Skip to content

Commit

Permalink
Merge #1761
Browse files Browse the repository at this point in the history
1761: feat(c-api) Implement the `traps` argument of `wasm_instance_new` r=Hywan a=Hywan

# Description

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.

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file

Fixes #1744.

Co-authored-by: Ivan Enderlin <[email protected]>
  • Loading branch information
bors[bot] and Hywan authored Oct 30, 2020
2 parents ff3a6c8 + bdb0dc7 commit 2d59b27
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
## **[Unreleased]**

- [#1710](https://github.com/wasmerio/wasmer/pull/1710) Memory for function call trampolines is now owned by the Artifact.

### Added

- [#1761](https://github.com/wasmerio/wasmer/pull/1761) Implement the `wasm_trap_t**` argument of `wasm_instance_new` in the Wasm C API.
- [#1687](https://github.com/wasmerio/wasmer/pull/1687) Add basic table example; fix ownership of local memory and local table metadata in the VM.
- [#1751](https://github.com/wasmerio/wasmer/pull/1751) Implement `wasm_trap_t` inside a function declared with `wasm_func_new_with_env` in the Wasm C API.
- [#1741](https://github.com/wasmerio/wasmer/pull/1741) Implement `wasm_memory_type` in the Wasm C API.
Expand Down
23 changes: 19 additions & 4 deletions lib/c-api/src/wasm_c_api/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::trap::wasm_trap_t;
use crate::ordered_resolver::OrderedResolver;
use std::mem;
use std::sync::Arc;
use wasmer::{Extern, Instance};
use wasmer::{Extern, Instance, InstantiationError};

#[allow(non_camel_case_types)]
pub struct wasm_instance_t {
Expand All @@ -17,8 +17,7 @@ pub unsafe extern "C" fn wasm_instance_new(
_store: &wasm_store_t,
module: &wasm_module_t,
imports: &wasm_extern_vec_t,
// own
_traps: *mut *mut wasm_trap_t,
traps: *mut *mut wasm_trap_t,
) -> Option<Box<wasm_instance_t>> {
let wasm_module = &module.inner;
let module_imports = wasm_module.imports();
Expand All @@ -32,7 +31,23 @@ pub unsafe extern "C" fn wasm_instance_new(
.cloned()
.collect();

let instance = Arc::new(c_try!(Instance::new(wasm_module, &resolver)));
let instance = match Instance::new(wasm_module, &resolver) {
Ok(instance) => Arc::new(instance),

Err(InstantiationError::Link(link_error)) => {
crate::error::update_last_error(link_error);

return None;
}

Err(InstantiationError::Start(runtime_error)) => {
let trap: Box<wasm_trap_t> = Box::new(runtime_error.into());
*traps = Box::into_raw(trap);

return None;
}
};

Some(Box::new(wasm_instance_t { inner: instance }))
}

Expand Down
14 changes: 7 additions & 7 deletions lib/c-api/tests/wasm_c_api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_executable(wasm-c-api-hello wasm-c-api/example/hello.c)
#add_executable(wasm-c-api-multi wasm-c-api/example/multi.c)
add_executable(wasm-c-api-reflect wasm-c-api/example/reflect.c)
add_executable(wasm-c-api-serialize wasm-c-api/example/serialize.c)
#add_executable(wasm-c-api-start wasm-c-api/example/start.c)
add_executable(wasm-c-api-start wasm-c-api/example/start.c)
#add_executable(wasm-c-api-table wasm-c-api/example/table.c)
#add_executable(wasm-c-api-threads wasm-c-api/example/threads.c)
add_executable(wasm-c-api-trap wasm-c-api/example/trap.c)
Expand Down Expand Up @@ -106,12 +106,12 @@ add_test(NAME wasm-c-api-serialize
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/wasm-c-api/example/
)

#target_link_libraries(wasm-c-api-start general ${WASMER_LIB})
#target_compile_options(wasm-c-api-start PRIVATE ${COMPILER_OPTIONS})
#add_test(NAME wasm-c-api-start
# COMMAND wasm-c-api-start
# WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/wasm-c-api/example/
#)
target_link_libraries(wasm-c-api-start general ${WASMER_LIB})
target_compile_options(wasm-c-api-start PRIVATE ${COMPILER_OPTIONS})
add_test(NAME wasm-c-api-start
COMMAND wasm-c-api-start
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/wasm-c-api/example/
)

#target_link_libraries(wasm-c-api-table general ${WASMER_LIB})
#target_compile_options(wasm-c-api-table PRIVATE ${COMPILER_OPTIONS})
Expand Down

0 comments on commit 2d59b27

Please sign in to comment.