Add test to import function and call it

This commit is contained in:
Brandon Fish
2019-02-02 17:43:59 -06:00
parent 9120a9d1f8
commit ffb3dc083a
8 changed files with 129 additions and 1 deletions

View File

@ -1,10 +1,16 @@
extern crate wasmer_runtime;
extern crate wasmer_runtime_core;
use libc::{c_char, c_int, int32_t, int64_t, uint32_t, uint8_t};
use std::ffi::CStr;
use std::slice;
use std::str;
use std::sync::Arc;
use std::{ffi::c_void, mem, ptr};
use wasmer_runtime::{ImportObject, Instance, Value};
use wasmer_runtime_core::export::{Context, Export, FuncPointer};
use wasmer_runtime_core::import::{LikeNamespace, Namespace};
use wasmer_runtime_core::types::{FuncSig, Type};
#[allow(non_camel_case_types)]
pub struct wasmer_import_object_t();
@ -12,6 +18,9 @@ pub struct wasmer_import_object_t();
#[allow(non_camel_case_types)]
pub struct wasmer_instance_t();
#[allow(non_camel_case_types)]
pub struct wasmer_instance_context_t();
#[allow(non_camel_case_types)]
#[no_mangle]
#[repr(C)]
@ -53,6 +62,13 @@ pub struct wasmer_value_t {
value: wasmer_value,
}
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_memory_t {
pub ptr: *mut uint8_t,
pub len: uint32_t,
}
#[no_mangle]
pub extern "C" fn wasmer_import_object_new() -> *mut wasmer_import_object_t {
Box::into_raw(Box::new(ImportObject::new())) as *mut wasmer_import_object_t
@ -89,6 +105,7 @@ pub unsafe extern "C" fn wasmer_instantiate(
}
};
unsafe { *instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t };
// Box::into_raw(import_object); // TODO Review is this the correct way not to drop
wasmer_compile_result_t::WASMER_COMPILE_OK
}
@ -125,6 +142,7 @@ pub unsafe extern "C" fn wasmer_instance_call(
let results: &mut [wasmer_value_t] = slice::from_raw_parts_mut(results, results_len as usize);
let result = instance.call(func_name_r, &params[..]);
Box::into_raw(instance);
match result {
Ok(results_vec) => {
println!("Call Res: {:?}", results_vec);
@ -158,6 +176,53 @@ pub unsafe extern "C" fn wasmer_instance_call(
}
}
#[no_mangle]
pub extern "C" fn wasmer_imports_set_import_func(
import_object: *mut wasmer_import_object_t,
namespace: *const c_char,
name: *const c_char,
func: extern "C" fn(data: *mut c_void),
) {
let mut import_object = unsafe { Box::from_raw(import_object as *mut ImportObject) };
let namespace_c = unsafe { CStr::from_ptr(namespace) };
let namespace_r = namespace_c.to_str().unwrap();
let name_c = unsafe { CStr::from_ptr(name) };
let name_r = name_c.to_str().unwrap();
let export = Export::Function {
func: unsafe { FuncPointer::new(func as _) },
ctx: Context::Internal,
signature: Arc::new(FuncSig::new(vec![Type::I32, Type::I32], vec![])),
};
// TODO handle existing namespace
// let maybe_namespace = import_object.get_namespace(namespace_r);
// if let Some(n) = maybe_namespace {
// n.insert(name_r, export);
// } else {
let mut namespace = Namespace::new();
namespace.insert(name_r, export);
import_object.register(namespace_r, namespace);
Box::into_raw(import_object);
// };
}
//#[no_mangle]
//pub extern "C" fn wasmer_debug_print(kind: uint8_t, thing: *mut c_void) {
// match kind {
// 1 => {
// println!("wasmer import object:");
// let import_object = unsafe { Box::from_raw(thing as *mut ImportObject) };
// println!("after import object");
// Box::into_raw(import_object);
// },
// _ => panic!("unknown kind {:?}", kind)
// }
//}
#[no_mangle]
pub extern "C" fn wasmer_instance_context_memory(instance: *mut wasmer_instance_context_t) {}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_instance_destroy(instance: *mut wasmer_instance_t) {