From 750a1c3645478616157872111ae1c6d0083a2ecd Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 12 Oct 2020 17:52:40 +0200 Subject: [PATCH] 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) +}