forked from AbleOS/ableos
110 lines
2.8 KiB
Rust
110 lines
2.8 KiB
Rust
use {
|
|
crate::interp::{HFIDT, TARGETS},
|
|
alloc::string::String,
|
|
log::trace,
|
|
wasmi::{Caller, TypedFunc},
|
|
};
|
|
|
|
use super::{HostState, WasmContext};
|
|
|
|
pub fn host_register_idt_handler(
|
|
caller: Caller<'_, HostState>,
|
|
interupt_number: i32,
|
|
address_start: i32,
|
|
length_of_string: i32,
|
|
) -> i32 {
|
|
// TODO: get the proc_id to address which function it is
|
|
// TODO: Register the function name and proc_id into the idt handler
|
|
|
|
let mem = caller.get_export("memory").unwrap().into_memory().unwrap();
|
|
let mem_array = mem.data(&caller);
|
|
let mut name = String::new();
|
|
for i in address_start..(address_start + length_of_string) {
|
|
let ch = mem_array[i as usize] as char;
|
|
name.push(ch);
|
|
}
|
|
|
|
let index = interupt_number as usize;
|
|
|
|
let hf = HFIDT.lock();
|
|
|
|
// hf.insert(index);
|
|
|
|
trace!("{}", name);
|
|
0
|
|
}
|
|
|
|
use crate::{
|
|
arch::hardware_random_u64,
|
|
handle::GLOBALS,
|
|
interp::{
|
|
objects::{HandleTarget, Object},
|
|
Handle,
|
|
},
|
|
};
|
|
|
|
pub fn host_make_object(
|
|
mut caller: Caller<'_, HostState>,
|
|
address_start: i32,
|
|
length_of_string: i32,
|
|
) -> i64 {
|
|
trace!(
|
|
"Called with addr {{ start {} length {} }}",
|
|
address_start,
|
|
length_of_string
|
|
);
|
|
let mem = caller.get_export("memory").unwrap().into_memory().unwrap();
|
|
let mem_array = mem.data(&caller);
|
|
let mut name = String::new();
|
|
for i in address_start..(address_start + length_of_string) {
|
|
let ch = mem_array[i as usize] as char;
|
|
name.push(ch);
|
|
}
|
|
trace!("Object Name {}", name);
|
|
let binding = TARGETS;
|
|
let mut olock = binding.lock();
|
|
let hand = Handle::new(olock.len());
|
|
let obj = xml::XMLElement::new(name);
|
|
|
|
olock.push(Some(HandleTarget::Object(Object { xml: obj })));
|
|
caller.data_mut().add_handle(hand).try_into().unwrap()
|
|
}
|
|
|
|
pub type WFIDT = TypedFunc<(), ()>;
|
|
|
|
fn get_fn_from_wc(wc: WasmContext, function_name: String) -> WFIDT {
|
|
wc.instance
|
|
.get_typed_func(wc.store, &function_name)
|
|
.unwrap()
|
|
}
|
|
|
|
pub fn host_make_global(mut caller: Caller<'_, HostState>, h: i64) -> (u64, u64, u64, u64) {
|
|
let binding = GLOBALS;
|
|
let Some(hand): Option<&mut Option<Handle>> = caller.data_mut().handles.get_mut(h as usize) else {
|
|
return (0,0,0,0);
|
|
};
|
|
let Some(t) = hand.take() else {
|
|
return (0,0,0,0);
|
|
};
|
|
let a = [hardware_random_u64(); 4];
|
|
let mut l = binding.lock();
|
|
l.insert(a, t);
|
|
return (a[0], a[1], a[2], a[3]);
|
|
}
|
|
|
|
pub fn host_take_global(mut caller: Caller<'_, HostState>, a: u64, b: u64, c: u64, d: u64) -> i64 {
|
|
let binding = GLOBALS;
|
|
let mut l = binding.lock();
|
|
let mut j: [u64; 4] = [0u64; 4];
|
|
j[0] = a;
|
|
j[1] = b;
|
|
j[2] = c;
|
|
j[3] = d;
|
|
let Some(m) = l.remove::<[u64; 4]>({
|
|
&j
|
|
}) else{
|
|
return -1;
|
|
};
|
|
caller.data_mut().add_handle(m) as i64
|
|
}
|