2023-04-07 16:44:33 -05:00
|
|
|
use alloc::{string::String, vec::Vec};
|
|
|
|
use core::fmt::Debug;
|
|
|
|
use core::fmt::Display;
|
2023-04-05 12:29:20 -05:00
|
|
|
use log::trace;
|
|
|
|
use wasmi::{Caller, Func, Linker, Module, Store};
|
2023-04-07 16:44:33 -05:00
|
|
|
use xml::XMLElement;
|
|
|
|
|
|
|
|
use crate::handle::{self, Handle};
|
2023-04-05 12:29:20 -05:00
|
|
|
|
|
|
|
pub fn wasm() -> Result<(), wasmi::Error> {
|
|
|
|
use wasmi::Config;
|
|
|
|
use wasmi::Engine;
|
2023-04-07 16:44:33 -05:00
|
|
|
let mut conf = Config::default();
|
|
|
|
conf.wasm_bulk_memory(true);
|
|
|
|
// conf.,
|
2023-04-05 12:29:20 -05:00
|
|
|
let engine = Engine::new(&conf);
|
2023-04-07 16:44:33 -05:00
|
|
|
// trace!("Engine constructed");
|
2023-04-05 12:29:20 -05:00
|
|
|
|
2023-04-07 16:44:33 -05:00
|
|
|
// let wasm = include_bytes!("../../wasm_syscall_test.wasm");
|
|
|
|
let wasm = include_bytes!("../../test.wasm");
|
2023-04-05 12:29:20 -05:00
|
|
|
|
2023-04-07 16:44:33 -05:00
|
|
|
// trace!("Loading WASM binary");
|
2023-04-05 12:29:20 -05:00
|
|
|
let module = Module::new(&engine, &wasm[..]).unwrap();
|
2023-04-07 16:44:33 -05:00
|
|
|
// trace!("Constructing wasm module");
|
|
|
|
let hs = HostState {
|
|
|
|
proc_handle: crate::handle::Handle::new(),
|
|
|
|
};
|
|
|
|
let mut store = Store::new(&engine, hs);
|
|
|
|
// trace!("constructing host store");
|
|
|
|
|
|
|
|
let read_mem_addr = Func::wrap(
|
|
|
|
&mut store,
|
|
|
|
|caller: Caller<'_, HostState>, param: i32| -> i32 { read_memory_address(caller, param) },
|
|
|
|
);
|
2023-04-05 12:29:20 -05:00
|
|
|
|
2023-04-07 16:44:33 -05:00
|
|
|
let mut linker = <Linker<HostState>>::new(&engine);
|
|
|
|
linker.define(
|
|
|
|
"host",
|
|
|
|
"read_mem_addr",
|
|
|
|
Func::wrap(
|
|
|
|
&mut store,
|
|
|
|
|caller: Caller<'_, HostState>, param: i32| -> i32 {
|
|
|
|
read_memory_address(caller, param)
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)?;
|
2023-04-05 12:29:20 -05:00
|
|
|
|
2023-04-07 16:44:33 -05:00
|
|
|
linker.define(
|
|
|
|
"host",
|
|
|
|
"read_object_attribute",
|
|
|
|
Func::wrap(&mut store, host_read_object_attribute),
|
|
|
|
)?;
|
2023-04-05 12:29:20 -05:00
|
|
|
|
2023-04-07 16:44:33 -05:00
|
|
|
linker.define(
|
|
|
|
"host",
|
|
|
|
"create_object",
|
|
|
|
Func::wrap(&mut store, host_make_object),
|
|
|
|
)?;
|
2023-04-05 12:29:20 -05:00
|
|
|
|
2023-04-07 16:44:33 -05:00
|
|
|
let instance = linker
|
|
|
|
.instantiate(&mut store, &module)?
|
|
|
|
.ensure_no_start(&mut store)?;
|
2023-04-05 12:29:20 -05:00
|
|
|
|
2023-04-07 16:44:33 -05:00
|
|
|
let version = instance.get_global(&store, "VERSION");
|
2023-04-05 12:29:20 -05:00
|
|
|
|
2023-04-07 16:44:33 -05:00
|
|
|
// trace!("Version: {:?}", version);
|
|
|
|
let hello = instance.get_typed_func::<(), (i32, i32)>(&store, "start")?;
|
|
|
|
|
|
|
|
let ret = hello.call(&mut store, ())?;
|
|
|
|
trace!("Called _start got return of {:?}", ret);
|
2023-04-05 12:29:20 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-07 16:44:33 -05:00
|
|
|
pub struct HostState {
|
|
|
|
/// In ableOS a handle is an unsigned 128 bit number
|
|
|
|
proc_handle: Handle,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_memory_address(caller: Caller<'_, HostState>, address: i32) -> i32 {
|
|
|
|
trace!("Proccess Handle: {:?}", caller.data().proc_handle);
|
2023-04-05 12:29:20 -05:00
|
|
|
trace!("Address: {}", address);
|
2023-04-07 16:44:33 -05:00
|
|
|
// let obj = host_make_object(caller, 16, 23);
|
2023-04-05 12:29:20 -05:00
|
|
|
0
|
|
|
|
}
|
2023-04-07 16:44:33 -05:00
|
|
|
use crate::kmain::OBJECTS;
|
|
|
|
pub fn host_make_object(
|
|
|
|
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 hand = handle::Handle::new();
|
|
|
|
{
|
|
|
|
let binding = OBJECTS;
|
|
|
|
let mut olock = binding.lock();
|
|
|
|
let obj = XMLElement::new(name);
|
|
|
|
|
|
|
|
olock.insert(handle::Handle::new(), obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
hand.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn host_read_object_attribute(
|
|
|
|
caller: Caller<'_, HostState>,
|
|
|
|
handle: i64,
|
|
|
|
address_start: i32,
|
|
|
|
length_of_string: i32,
|
|
|
|
) -> (i32, i32) {
|
|
|
|
{
|
|
|
|
let binding = OBJECTS;
|
|
|
|
let mut olock = binding.lock();
|
|
|
|
|
|
|
|
// olock.get(&handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
(0, 0)
|
|
|
|
}
|