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

Add function to retrieve function name from wasm_frame_t #3222

Merged
merged 15 commits into from
Oct 25, 2022
50 changes: 50 additions & 0 deletions lib/c-api/src/wasm_c_api/types/frame.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::super::instance::wasm_instance_t;
use libc::c_char;
use std::ffi::CString;
use wasmer_api::FrameInfo;

#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -47,4 +49,52 @@ pub unsafe extern "C" fn wasm_frame_module_offset(frame: &wasm_frame_t) -> usize
frame.info.module_offset()
}

#[repr(C)]
#[allow(non_camel_case_types)]
#[derive(Debug, Clone)]
pub struct wasm_name_t {
pub name: *mut c_char,
}

#[no_mangle]
pub unsafe extern "C" fn wasm_frame_module_name(frame: &wasm_frame_t) -> wasm_name_t {
let null = wasm_name_t {
name: core::ptr::null_mut(),
};

let module_name =
Some(frame.info.module_name()).and_then(|f| Some(CString::new(f).ok()?.into_raw()));

match module_name {
Some(s) => wasm_name_t { name: s },
None => null,
}
fschutt marked this conversation as resolved.
Show resolved Hide resolved
}

#[no_mangle]
pub unsafe extern "C" fn wasm_frame_func_name(frame: &wasm_frame_t) -> wasm_name_t {
let null = wasm_name_t {
name: core::ptr::null_mut(),
};

let func_name = frame
.info
.function_name()
.and_then(|f| Some(CString::new(f).ok()?.into_raw()));

match func_name {
Some(s) => wasm_name_t { name: s },
None => null,
}
fschutt marked this conversation as resolved.
Show resolved Hide resolved
}

#[no_mangle]
pub unsafe extern "C" fn wasm_name_delete(name: Option<&mut wasm_name_t>) {
if let Some(s) = name {
if !s.name.is_null() {
let _ = CString::from_raw(s.name);
}
}
}

wasm_declare_boxed_vec!(frame);