From 750a1c3645478616157872111ae1c6d0083a2ecd Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 12 Oct 2020 17:52:40 +0200 Subject: [PATCH 1/6] feat(c-api) Implement `wasm_module_name` and `wasm_module_set_name`. I submited a proposal to the official `wasm.h` by the way, https://github.com/WebAssembly/wasm-c-api/pull/157. For the moment, let's keep that as a vendor specific implementation. --- lib/c-api/src/wasm_c_api/wasmer.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/c-api/src/wasm_c_api/wasmer.rs b/lib/c-api/src/wasm_c_api/wasmer.rs index 5d6122c05de..6b811e5f2ff 100644 --- a/lib/c-api/src/wasm_c_api/wasmer.rs +++ b/lib/c-api/src/wasm_c_api/wasmer.rs @@ -1,9 +1,35 @@ //! 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; #[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: &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, + }; + + module.inner.set_name(name) +} From b9f98ce1f696088a9764704198c1606500895ecf Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 12 Oct 2020 17:54:21 +0200 Subject: [PATCH 2/6] feat(c-api) Update `.h` files. --- lib/c-api/build.rs | 6 ++++-- lib/c-api/wasmer_wasm.h | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) 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/wasmer_wasm.h b/lib/c-api/wasmer_wasm.h index b9eea99ae71..02430401182 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(const wasm_module_t *module, const wasm_name_t *name); + /** * Gets the length in bytes of the last error if any. * From fe70330772f8545e3f32e4c1696031c3ef2f575c Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 12 Oct 2020 18:02:32 +0200 Subject: [PATCH 3/6] fixup --- lib/c-api/src/wasm_c_api/wasmer.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/c-api/src/wasm_c_api/wasmer.rs b/lib/c-api/src/wasm_c_api/wasmer.rs index 6b811e5f2ff..b62a2ec1d51 100644 --- a/lib/c-api/src/wasm_c_api/wasmer.rs +++ b/lib/c-api/src/wasm_c_api/wasmer.rs @@ -5,6 +5,7 @@ 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 { @@ -22,7 +23,10 @@ pub unsafe extern "C" fn wasm_module_name(module: &wasm_module_t, out: &mut wasm } #[no_mangle] -pub unsafe extern "C" fn wasm_module_set_name(module: &wasm_module_t, name: &wasm_name_t) -> bool { +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, @@ -31,5 +35,8 @@ pub unsafe extern "C" fn wasm_module_set_name(module: &wasm_module_t, name: &was None => return false, }; - module.inner.set_name(name) + match Arc::get_mut(&mut module.inner) { + Some(module) => module.set_name(name), + None => false, + } } From f866ea39fd68d37dba6ac3ce431b37b58d75f130 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 12 Oct 2020 18:02:45 +0200 Subject: [PATCH 4/6] fixup --- lib/c-api/wasmer_wasm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/c-api/wasmer_wasm.h b/lib/c-api/wasmer_wasm.h index 02430401182..6dffd665d45 100644 --- a/lib/c-api/wasmer_wasm.h +++ b/lib/c-api/wasmer_wasm.h @@ -158,7 +158,7 @@ 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(const wasm_module_t *module, const wasm_name_t *name); +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. From 2a448ba1173661e96fe3a7b9480bbc65179a9d88 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 13 Oct 2020 10:08:56 +0200 Subject: [PATCH 5/6] doc(changelog) Add #1709. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41c7a1d46ed..6041ead00ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## **[Unreleased]** +- [#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 From 373b7a48154bce6f265907730a41fb11ba03df5f Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 13 Oct 2020 10:10:27 +0200 Subject: [PATCH 6/6] doc(changelog) Fix merge. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0bedf03e0e..edba3e67454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,9 @@ ## **[Unreleased]** -- [#1709](https://github.com/wasmerio/wasmer/pull/1709) Implement `wasm_module_name` and `wasm_module_set_name` in the Wasm(er) C API. ### 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