2019-02-01 05:51:34 +00:00
|
|
|
extern crate wasmer_runtime;
|
2019-02-02 23:43:59 +00:00
|
|
|
extern crate wasmer_runtime_core;
|
2019-02-01 05:51:34 +00:00
|
|
|
|
2019-02-02 20:53:07 +00:00
|
|
|
use libc::{c_char, c_int, int32_t, int64_t, uint32_t, uint8_t};
|
2019-02-02 06:26:10 +00:00
|
|
|
use std::ffi::CStr;
|
2019-02-02 16:44:08 +00:00
|
|
|
use std::slice;
|
2019-02-02 06:26:10 +00:00
|
|
|
use std::str;
|
2019-02-02 23:43:59 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::{ffi::c_void, mem, ptr};
|
2019-02-02 14:43:29 +00:00
|
|
|
use wasmer_runtime::{ImportObject, Instance, Value};
|
2019-02-02 23:43:59 +00:00
|
|
|
use wasmer_runtime_core::export::{Context, Export, FuncPointer};
|
|
|
|
use wasmer_runtime_core::import::{LikeNamespace, Namespace};
|
|
|
|
use wasmer_runtime_core::types::{FuncSig, Type};
|
2019-02-01 05:51:34 +00:00
|
|
|
|
|
|
|
#[allow(non_camel_case_types)]
|
2019-02-02 00:52:22 +00:00
|
|
|
pub struct wasmer_import_object_t();
|
2019-02-01 05:51:34 +00:00
|
|
|
|
|
|
|
#[allow(non_camel_case_types)]
|
2019-02-02 00:52:22 +00:00
|
|
|
pub struct wasmer_instance_t();
|
2019-02-01 05:51:34 +00:00
|
|
|
|
2019-02-02 23:43:59 +00:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub struct wasmer_instance_context_t();
|
|
|
|
|
2019-02-01 05:51:34 +00:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[no_mangle]
|
|
|
|
#[repr(C)]
|
|
|
|
pub enum wasmer_compile_result_t {
|
|
|
|
WASMER_COMPILE_OK = 1,
|
|
|
|
WASMER_COMPILE_ERROR = 2,
|
|
|
|
}
|
|
|
|
|
2019-02-02 06:26:10 +00:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[no_mangle]
|
|
|
|
#[repr(C)]
|
|
|
|
pub enum wasmer_call_result_t {
|
|
|
|
WASMER_CALL_OK = 1,
|
|
|
|
WASMER_CALL_ERROR = 2,
|
|
|
|
}
|
|
|
|
|
2019-02-02 20:53:07 +00:00
|
|
|
#[repr(u32)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum wasmer_value_tag {
|
|
|
|
WASM_I32,
|
|
|
|
WASM_I64,
|
|
|
|
WASM_F32,
|
|
|
|
WASM_F64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub union wasmer_value {
|
|
|
|
I32: int32_t,
|
|
|
|
I64: int64_t,
|
|
|
|
F32: f32,
|
|
|
|
F64: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct wasmer_value_t {
|
|
|
|
tag: wasmer_value_tag,
|
|
|
|
value: wasmer_value,
|
|
|
|
}
|
|
|
|
|
2019-02-02 23:43:59 +00:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct wasmer_memory_t {
|
|
|
|
pub ptr: *mut uint8_t,
|
|
|
|
pub len: uint32_t,
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:51:34 +00:00
|
|
|
#[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
|
|
|
|
}
|
|
|
|
|
2019-02-02 04:10:36 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-02-01 05:51:34 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import_object_t) {
|
|
|
|
if !import_object.is_null() {
|
|
|
|
drop(unsafe { Box::from_raw(import_object as *mut ImportObject) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 04:10:36 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-02-01 05:51:34 +00:00
|
|
|
#[no_mangle]
|
2019-02-02 14:43:29 +00:00
|
|
|
pub unsafe extern "C" fn wasmer_instantiate(
|
|
|
|
mut instance: *mut *mut wasmer_instance_t,
|
2019-02-02 06:26:10 +00:00
|
|
|
wasm_bytes: *mut uint8_t,
|
|
|
|
wasm_bytes_len: uint32_t,
|
2019-02-01 05:51:34 +00:00
|
|
|
import_object: *mut wasmer_import_object_t,
|
|
|
|
) -> wasmer_compile_result_t {
|
|
|
|
let import_object = unsafe { Box::from_raw(import_object as *mut ImportObject) };
|
2019-02-02 06:26:10 +00:00
|
|
|
if wasm_bytes.is_null() {
|
2019-02-02 14:43:29 +00:00
|
|
|
return wasmer_compile_result_t::WASMER_COMPILE_ERROR;
|
2019-02-02 06:26:10 +00:00
|
|
|
}
|
2019-02-02 14:43:29 +00:00
|
|
|
let bytes: &[u8] =
|
|
|
|
unsafe { ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize) };
|
2019-02-01 05:51:34 +00:00
|
|
|
let result = wasmer_runtime::instantiate(bytes, *import_object);
|
|
|
|
let new_instance = match result {
|
|
|
|
Ok(instance) => instance,
|
2019-02-02 06:26:10 +00:00
|
|
|
Err(error) => {
|
|
|
|
println!("Err: {:?}", error);
|
2019-02-02 14:43:29 +00:00
|
|
|
return wasmer_compile_result_t::WASMER_COMPILE_ERROR;
|
|
|
|
}
|
2019-02-01 05:51:34 +00:00
|
|
|
};
|
2019-02-02 14:43:29 +00:00
|
|
|
unsafe { *instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t };
|
2019-02-02 23:43:59 +00:00
|
|
|
// Box::into_raw(import_object); // TODO Review is this the correct way not to drop
|
2019-02-01 05:51:34 +00:00
|
|
|
wasmer_compile_result_t::WASMER_COMPILE_OK
|
|
|
|
}
|
2019-02-02 06:26:10 +00:00
|
|
|
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
#[no_mangle]
|
2019-02-02 14:43:29 +00:00
|
|
|
pub unsafe extern "C" fn wasmer_instance_call(
|
|
|
|
instance: *mut wasmer_instance_t,
|
|
|
|
name: *const c_char,
|
2019-02-02 20:53:07 +00:00
|
|
|
params: *const wasmer_value_t,
|
2019-02-02 16:44:08 +00:00
|
|
|
params_len: c_int,
|
2019-02-02 20:53:07 +00:00
|
|
|
results: *mut wasmer_value_t,
|
2019-02-02 16:44:08 +00:00
|
|
|
results_len: c_int,
|
2019-02-02 14:43:29 +00:00
|
|
|
) -> wasmer_call_result_t {
|
2019-02-02 06:26:10 +00:00
|
|
|
// TODO handle params and results
|
2019-02-02 14:43:29 +00:00
|
|
|
if instance.is_null() {
|
2019-02-02 06:26:10 +00:00
|
|
|
return wasmer_call_result_t::WASMER_CALL_ERROR;
|
|
|
|
}
|
2019-02-02 14:43:29 +00:00
|
|
|
if name.is_null() {
|
2019-02-02 06:26:10 +00:00
|
|
|
return wasmer_call_result_t::WASMER_CALL_ERROR;
|
|
|
|
}
|
2019-02-02 16:44:08 +00:00
|
|
|
|
|
|
|
if params.is_null() {
|
|
|
|
return wasmer_call_result_t::WASMER_CALL_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-02-02 20:53:07 +00:00
|
|
|
let params: &[wasmer_value_t] = slice::from_raw_parts(params, params_len as usize);
|
2019-02-02 16:44:08 +00:00
|
|
|
// TODO Fix this conversion and params
|
2019-02-02 20:53:07 +00:00
|
|
|
let params: Vec<Value> = params.iter().cloned().map(|x| x.into()).collect();
|
2019-02-02 16:44:08 +00:00
|
|
|
// let params= &[Value::I32(3), Value::I32(4)];
|
|
|
|
|
2019-02-02 14:43:29 +00:00
|
|
|
let func_name_c = unsafe { CStr::from_ptr(name) };
|
2019-02-02 06:26:10 +00:00
|
|
|
let func_name_r = func_name_c.to_str().unwrap();
|
|
|
|
let instance = unsafe { Box::from_raw(instance as *mut Instance) };
|
2019-02-02 16:44:08 +00:00
|
|
|
|
2019-02-02 20:53:07 +00:00
|
|
|
let results: &mut [wasmer_value_t] = slice::from_raw_parts_mut(results, results_len as usize);
|
2019-02-02 16:44:08 +00:00
|
|
|
let result = instance.call(func_name_r, ¶ms[..]);
|
2019-02-02 23:43:59 +00:00
|
|
|
Box::into_raw(instance);
|
2019-02-02 06:26:10 +00:00
|
|
|
match result {
|
2019-02-02 20:53:07 +00:00
|
|
|
Ok(results_vec) => {
|
|
|
|
println!("Call Res: {:?}", results_vec);
|
|
|
|
if results_vec.len() > 0 {
|
|
|
|
let ret = match results_vec[0] {
|
|
|
|
Value::I32(x) => wasmer_value_t {
|
|
|
|
tag: wasmer_value_tag::WASM_I32,
|
|
|
|
value: wasmer_value { I32: x },
|
|
|
|
},
|
|
|
|
Value::I64(x) => wasmer_value_t {
|
|
|
|
tag: wasmer_value_tag::WASM_I64,
|
|
|
|
value: wasmer_value { I64: x },
|
|
|
|
},
|
|
|
|
Value::F32(x) => wasmer_value_t {
|
|
|
|
tag: wasmer_value_tag::WASM_F32,
|
|
|
|
value: wasmer_value { F32: x },
|
|
|
|
},
|
|
|
|
Value::F64(x) => wasmer_value_t {
|
|
|
|
tag: wasmer_value_tag::WASM_F64,
|
|
|
|
value: wasmer_value { F64: x },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
results[0] = ret;
|
2019-02-02 16:44:08 +00:00
|
|
|
}
|
2019-02-02 06:26:10 +00:00
|
|
|
wasmer_call_result_t::WASMER_CALL_OK
|
2019-02-02 14:43:29 +00:00
|
|
|
}
|
2019-02-02 06:26:10 +00:00
|
|
|
Err(err) => {
|
2019-02-02 20:53:07 +00:00
|
|
|
println!("Call Err: {:?}", err);
|
2019-02-02 06:26:10 +00:00
|
|
|
wasmer_call_result_t::WASMER_CALL_ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 23:43:59 +00:00
|
|
|
#[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) {}
|
|
|
|
|
2019-02-02 06:26:10 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn wasmer_instance_destroy(instance: *mut wasmer_instance_t) {
|
|
|
|
if !instance.is_null() {
|
|
|
|
drop(unsafe { Box::from_raw(instance as *mut Instance) });
|
|
|
|
}
|
2019-02-02 14:43:29 +00:00
|
|
|
}
|
2019-02-02 20:53:07 +00:00
|
|
|
|
|
|
|
impl From<wasmer_value_t> for Value {
|
|
|
|
fn from(v: wasmer_value_t) -> Self {
|
|
|
|
unsafe {
|
|
|
|
match v {
|
|
|
|
wasmer_value_t {
|
|
|
|
tag: WASM_I32,
|
|
|
|
value: wasmer_value { I32 },
|
|
|
|
} => Value::I32(I32),
|
|
|
|
wasmer_value_t {
|
|
|
|
tag: WASM_I64,
|
|
|
|
value: wasmer_value { I64 },
|
|
|
|
} => Value::I64(I64),
|
|
|
|
wasmer_value_t {
|
|
|
|
tag: WASM_F32,
|
|
|
|
value: wasmer_value { F32 },
|
|
|
|
} => Value::F32(F32),
|
|
|
|
wasmer_value_t {
|
|
|
|
tag: WASM_F64,
|
|
|
|
value: wasmer_value { F64 },
|
|
|
|
} => Value::F64(F64),
|
|
|
|
_ => panic!("not implemented"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|