mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-22 13:11:32 +00:00
Add function signature parameters to set import func
This commit is contained in:
@ -183,6 +183,10 @@ pub unsafe extern "C" fn wasmer_imports_set_import_func(
|
|||||||
namespace: *const c_char,
|
namespace: *const c_char,
|
||||||
name: *const c_char,
|
name: *const c_char,
|
||||||
func: extern "C" fn(data: *mut c_void),
|
func: extern "C" fn(data: *mut c_void),
|
||||||
|
params: *const wasmer_value_tag,
|
||||||
|
params_len: c_int,
|
||||||
|
returns: *const wasmer_value_tag,
|
||||||
|
returns_len: c_int,
|
||||||
) {
|
) {
|
||||||
let mut import_object = unsafe { Box::from_raw(import_object as *mut ImportObject) };
|
let mut import_object = unsafe { Box::from_raw(import_object as *mut ImportObject) };
|
||||||
let namespace_c = unsafe { CStr::from_ptr(namespace) };
|
let namespace_c = unsafe { CStr::from_ptr(namespace) };
|
||||||
@ -190,10 +194,15 @@ pub unsafe extern "C" fn wasmer_imports_set_import_func(
|
|||||||
let name_c = unsafe { CStr::from_ptr(name) };
|
let name_c = unsafe { CStr::from_ptr(name) };
|
||||||
let name_r = name_c.to_str().unwrap();
|
let name_r = name_c.to_str().unwrap();
|
||||||
|
|
||||||
|
let params: &[wasmer_value_tag] = slice::from_raw_parts(params, params_len as usize);
|
||||||
|
let params: Vec<Type> = params.iter().cloned().map(|x| x.into()).collect();
|
||||||
|
let returns: &[wasmer_value_tag] = slice::from_raw_parts(returns, returns_len as usize);
|
||||||
|
let returns: Vec<Type> = returns.iter().cloned().map(|x| x.into()).collect();
|
||||||
|
|
||||||
let export = Export::Function {
|
let export = Export::Function {
|
||||||
func: unsafe { FuncPointer::new(func as _) },
|
func: unsafe { FuncPointer::new(func as _) },
|
||||||
ctx: Context::Internal,
|
ctx: Context::Internal,
|
||||||
signature: Arc::new(FuncSig::new(vec![Type::I32, Type::I32], vec![])),
|
signature: Arc::new(FuncSig::new(params, returns)),
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO handle existing namespace
|
// TODO handle existing namespace
|
||||||
@ -257,3 +266,17 @@ impl From<wasmer_value_t> for Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<wasmer_value_tag> for Type {
|
||||||
|
fn from(v: wasmer_value_tag) -> Self {
|
||||||
|
unsafe {
|
||||||
|
match v {
|
||||||
|
wasmer_value_tag::WASM_I32 => Type::I32,
|
||||||
|
wasmer_value_tag::WASM_I64 => Type::I64,
|
||||||
|
wasmer_value_tag::WASM_F32 => Type::F32,
|
||||||
|
wasmer_value_tag::WASM_F64 => Type::F64,
|
||||||
|
_ => panic!("not implemented"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -13,7 +13,9 @@ void print_str(int32_t ptr, int32_t len, wasmer_instance_context_t *ctx) {
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
wasmer_import_object_t *import_object = wasmer_import_object_new();
|
wasmer_import_object_t *import_object = wasmer_import_object_new();
|
||||||
wasmer_imports_set_import_func(import_object, "env", "print_str", print_str);
|
wasmer_value_tag params_sig[] = {WASM_I32, WASM_I32};
|
||||||
|
wasmer_value_tag returns_sig[] = {};
|
||||||
|
wasmer_imports_set_import_func(import_object, "env", "print_str", print_str, params_sig, 2, returns_sig, 0);
|
||||||
|
|
||||||
// Read the wasm file bytes
|
// Read the wasm file bytes
|
||||||
FILE *file = fopen("wasm_sample_app.wasm", "r");
|
FILE *file = fopen("wasm_sample_app.wasm", "r");
|
||||||
|
@ -46,7 +46,11 @@ wasmer_import_object_t *wasmer_import_object_new(void);
|
|||||||
void wasmer_imports_set_import_func(wasmer_import_object_t *import_object,
|
void wasmer_imports_set_import_func(wasmer_import_object_t *import_object,
|
||||||
const char *namespace_,
|
const char *namespace_,
|
||||||
const char *name,
|
const char *name,
|
||||||
void (*func)(void *data));
|
void (*func)(void *data),
|
||||||
|
const wasmer_value_tag *params,
|
||||||
|
int params_len,
|
||||||
|
const wasmer_value_tag *returns,
|
||||||
|
int returns_len);
|
||||||
|
|
||||||
wasmer_call_result_t wasmer_instance_call(wasmer_instance_t *instance,
|
wasmer_call_result_t wasmer_instance_call(wasmer_instance_t *instance,
|
||||||
const char *name,
|
const char *name,
|
||||||
|
Reference in New Issue
Block a user