diff --git a/CHANGELOG.md b/CHANGELOG.md index 74d19d04d77..edba3e67454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/c-api/build.rs b/lib/c-api/build.rs index 627ad9039e0..e43285a6263 100644 --- a/lib/c-api/build.rs +++ b/lib/c-api/build.rs @@ -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") } diff --git a/lib/c-api/src/wasm_c_api/wasmer.rs b/lib/c-api/src/wasm_c_api/wasmer.rs index 5d6122c05de..b62a2ec1d51 100644 --- a/lib/c-api/src/wasm_c_api/wasmer.rs +++ b/lib/c-api/src/wasm_c_api/wasmer.rs @@ -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, + } +} diff --git a/lib/c-api/wasmer_wasm.h b/lib/c-api/wasmer_wasm.h index b9eea99ae71..6dffd665d45 100644 --- a/lib/c-api/wasmer_wasm.h +++ b/lib/c-api/wasmer_wasm.h @@ -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. *