Skip to content

Commit

Permalink
Merge #1709
Browse files Browse the repository at this point in the history
1709: feat(c-api) Implement `wasm_module_name` and `wasm_module_set_name` r=Hywan a=Hywan

Until my official proposal made here, WebAssembly/wasm-c-api#157 is accepted or rejected, I implement those functions as “wasmer-specific” API.

Co-authored-by: Ivan Enderlin <[email protected]>
  • Loading branch information
bors[bot] and Hywan authored Oct 13, 2020
2 parents fd25edb + 373b7a4 commit 41abb4d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Added

- [#1709](https://github.com/wasmerio/wasmer/pull/1709) Implement `wasm_module_name` and `wasm_module_set_name` in the Wasm(er) C API.
- [#1700](https://github.com/wasmerio/wasmer/pull/1700) Implement `wasm_externtype_copy` in the Wasm C API.

## 1.0.0-alpha4 - 2020-10-08
Expand Down
6 changes: 4 additions & 2 deletions lib/c-api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,12 @@ fn exclude_items_from_wasm_c_api(builder: Builder) -> Builder {
.exclude_item("wasi_get_start_function")
.exclude_item("wasi_get_wasi_version")
.exclude_item("wasi_version_t")
.exclude_item("wasm_config_set_compiler")
.exclude_item("wasm_config_set_engine")
.exclude_item("wasm_instance_get_vmctx_ptr")
.exclude_item("wasm_module_name")
.exclude_item("wasm_module_set_name")
.exclude_item("wasmer_compiler_t")
.exclude_item("wasmer_engine_t")
.exclude_item("wasm_config_set_compiler")
.exclude_item("wasm_config_set_engine")
.exclude_item("wat2wasm")
}
35 changes: 34 additions & 1 deletion lib/c-api/src/wasm_c_api/wasmer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
//! Wasmer-specific extensions to the Wasm C API.
use crate::wasm_c_api::instance::wasm_instance_t;
use super::instance::wasm_instance_t;
use super::module::wasm_module_t;
use super::types::wasm_name_t;
use std::ffi::c_void;
use std::str;
use std::sync::Arc;

#[no_mangle]
pub unsafe extern "C" fn wasm_instance_get_vmctx_ptr(instance: &wasm_instance_t) -> *mut c_void {
instance.inner.vmctx_ptr() as _
}

#[no_mangle]
pub unsafe extern "C" fn wasm_module_name(module: &wasm_module_t, out: &mut wasm_name_t) {
let name = match module.inner.name() {
Some(name) => name,
None => return,
};

*out = name.as_bytes().to_vec().into();
}

#[no_mangle]
pub unsafe extern "C" fn wasm_module_set_name(
module: &mut wasm_module_t,
name: &wasm_name_t,
) -> bool {
let name = match name.into_slice() {
Some(name) => match str::from_utf8(name) {
Ok(name) => name,
Err(_) => return false, // not ideal!
},
None => return false,
};

match Arc::get_mut(&mut module.inner) {
Some(module) => module.set_name(name),
None => false,
}
}
4 changes: 4 additions & 0 deletions lib/c-api/wasmer_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ void wasm_config_set_engine(wasm_config_t *config, wasmer_engine_t engine);

void *wasm_instance_get_vmctx_ptr(const wasm_instance_t *instance);

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

/**
* Gets the length in bytes of the last error if any.
*
Expand Down

0 comments on commit 41abb4d

Please sign in to comment.