Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(c-api) Implement wasm_module_name and wasm_module_set_name #1709

Merged
merged 7 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!
Hywan marked this conversation as resolved.
Show resolved Hide resolved
},
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