finally got the macro done, now merging
This commit is contained in:
commit
796f9ae128
158
src/main.rs
158
src/main.rs
|
@ -1,7 +1,7 @@
|
||||||
use wabt;
|
use wabt;
|
||||||
use wasmi::{
|
use wasmi::{
|
||||||
Error, Externals, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver, ModuleInstance,
|
Error, Externals, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver, ModuleInstance,
|
||||||
RuntimeArgs, RuntimeValue, Signature, Trap, ValueType,
|
RuntimeArgs, RuntimeValue, Signature, Trap, ValueType,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod wasm_sys;
|
mod wasm_sys;
|
||||||
|
@ -11,96 +11,102 @@ pub const KILL: usize = 0;
|
||||||
|
|
||||||
struct HostFunctions;
|
struct HostFunctions;
|
||||||
impl HostFunctions {
|
impl HostFunctions {
|
||||||
fn check_signature(&self, index: usize, signature: &Signature) -> bool {
|
fn check_signature(&self, index: usize, signature: &Signature) -> bool {
|
||||||
let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index.into() {
|
let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index.into() {
|
||||||
SysCall::KILL => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
|
SysCall::KILL => (&[ValueType::I32, ValueType::I32], Some(ValueType::I32)),
|
||||||
_ => return false,
|
_ => return false,
|
||||||
};
|
};
|
||||||
signature.params() == params && signature.return_type() == ret_ty
|
signature.params() == params && signature.return_type() == ret_ty
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Externals for HostFunctions {
|
impl Externals for HostFunctions {
|
||||||
fn invoke_index(
|
fn invoke_index(
|
||||||
&mut self,
|
&mut self,
|
||||||
index: usize,
|
index: usize,
|
||||||
args: RuntimeArgs,
|
args: RuntimeArgs,
|
||||||
) -> Result<Option<RuntimeValue>, Trap> {
|
) -> Result<Option<RuntimeValue>, Trap> {
|
||||||
match index.into() {
|
match index.into() {
|
||||||
SysCall::KILL => {
|
SysCall::KILL => {
|
||||||
let a: u32 = args.nth_checked(0)?;
|
let a: u32 = args.nth_checked(0)?;
|
||||||
let b: u32 = args.nth_checked(1)?;
|
let b: u32 = args.nth_checked(1)?;
|
||||||
let result = a + b;
|
let result = a + b;
|
||||||
|
|
||||||
Ok(Some(RuntimeValue::I32(result as i32)))
|
Ok(Some(RuntimeValue::I32(result as i32)))
|
||||||
}
|
}
|
||||||
_ => panic!("Unimplemented function at {}", index),
|
SysCall::CONSOLE_RESET => {}
|
||||||
}
|
SysCall::CONSOLE_IN => {}
|
||||||
}
|
SysCall::CONSOLE_OUT => {}
|
||||||
|
SysCall::CONSOLE_GET_TITLE => {}
|
||||||
|
SysCall::CONSOLE_SET_TITLE => {}
|
||||||
|
|
||||||
|
_ => panic!("Unimplemented function at {}", index),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl ModuleImportResolver for HostFunctions {
|
impl ModuleImportResolver for HostFunctions {
|
||||||
fn resolve_func(&self, field_name: &str, signature: &Signature) -> Result<FuncRef, Error> {
|
fn resolve_func(&self, field_name: &str, signature: &Signature) -> Result<FuncRef, Error> {
|
||||||
let index = match field_name {
|
let index = match field_name {
|
||||||
"add" => SysCall::KILL as usize,
|
"add" => SysCall::KILL as usize,
|
||||||
_ => {
|
_ => {
|
||||||
|
return Err(Error::Instantiation(format!(
|
||||||
|
"Export {} not found",
|
||||||
|
field_name
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if !self.check_signature(index, signature) {
|
||||||
return Err(Error::Instantiation(format!(
|
return Err(Error::Instantiation(format!(
|
||||||
"Export {} not found",
|
"Export {} has a bad signature",
|
||||||
field_name
|
field_name
|
||||||
)))
|
)));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
if !self.check_signature(index, signature) {
|
Ok(FuncInstance::alloc_host(
|
||||||
return Err(Error::Instantiation(format!(
|
Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)),
|
||||||
"Export {} has a bad signature",
|
index,
|
||||||
field_name
|
))
|
||||||
)));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(FuncInstance::alloc_host(
|
|
||||||
Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)),
|
|
||||||
index,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Parse WAT (WebAssembly Text format) into wasm bytecode.
|
// Parse WAT (WebAssembly Text format) into wasm bytecode.
|
||||||
let wasm_binary = wabt::wat2wasm(include_str!("../wasm/test.wat"));
|
let wasm_binary = wabt::wat2wasm(include_str!("../wasm/test.wat"));
|
||||||
|
|
||||||
let wasm_binary = match wasm_binary {
|
let wasm_binary = match wasm_binary {
|
||||||
Ok(abc) => abc,
|
Ok(abc) => abc,
|
||||||
Err(abc) => {
|
Err(abc) => {
|
||||||
println!("{}", abc);
|
println!("{}", abc);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// .expect("failed to parse wat");
|
// .expect("failed to parse wat");
|
||||||
|
|
||||||
// Load wasm binary and prepare it for instantiation.
|
// Load wasm binary and prepare it for instantiation.
|
||||||
let module = wasmi::Module::from_buffer(&wasm_binary).expect("failed to load wasm");
|
let module = wasmi::Module::from_buffer(&wasm_binary).expect("failed to load wasm");
|
||||||
|
|
||||||
let imports = ImportsBuilder::new().with_resolver("host", &HostFunctions);
|
let imports = ImportsBuilder::new().with_resolver("host", &HostFunctions);
|
||||||
|
|
||||||
// Instantiate a module with empty imports and
|
// Instantiate a module with empty imports and
|
||||||
// assert that there is no `start` function.
|
// assert that there is no `start` function.
|
||||||
let instance = ModuleInstance::new(&module, &imports)
|
let instance = ModuleInstance::new(&module, &imports)
|
||||||
.expect("failed to instantiate wasm module")
|
.expect("failed to instantiate wasm module")
|
||||||
.assert_no_start();
|
.assert_no_start();
|
||||||
|
|
||||||
// Finally, invoke the exported function "test" with no parameters
|
// Finally, invoke the exported function "test" with no parameters
|
||||||
// and empty external function executor.
|
// and empty external function executor.
|
||||||
let result: i32 = instance
|
let result: i32 = instance
|
||||||
.invoke_export("main", &[], &mut HostFunctions)
|
.invoke_export("main", &[], &mut HostFunctions)
|
||||||
// .with_resolver(&mut HostFunctions)
|
// .with_resolver(&mut HostFunctions)
|
||||||
.expect("failed to execute export")
|
.expect("failed to execute export")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.try_into()
|
.try_into()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{:?}",
|
"{:?}",
|
||||||
result // .unwrap()
|
result // .unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,8 @@ syscall_enum! {
|
||||||
CONSOLE_OUT = 3, // Console output
|
CONSOLE_OUT = 3, // Console output
|
||||||
CONSOLE_GET_TITLE = 4, // Get the console title
|
CONSOLE_GET_TITLE = 4, // Get the console title
|
||||||
CONSOLE_SET_TITLE = 5, // Set the console title
|
CONSOLE_SET_TITLE = 5, // Set the console title
|
||||||
|
GET_PID = 6; // Get the proccess ID
|
||||||
|
PROCESS_INFO = 7; // Get information about the process
|
||||||
//scheduler Related Syscals
|
//scheduler Related Syscals
|
||||||
GET_PRIORITY = 10, // Get scheduler priority
|
GET_PRIORITY = 10, // Get scheduler priority
|
||||||
SET_PRIORITY = 11, // Set scheduler priority
|
SET_PRIORITY = 11, // Set scheduler priority
|
||||||
|
@ -57,6 +58,24 @@ syscall_enum! {
|
||||||
FILE_READ = 30,
|
FILE_READ = 30,
|
||||||
FILE_WRITE = 31,
|
FILE_WRITE = 31,
|
||||||
|
|
||||||
|
SLEEP=32, // Sleep in milliseconds
|
||||||
|
SLEEP_UNTIL=33, // Sleep until this time in milliseconds (if this is below the current time return)
|
||||||
|
NANOSLEEP=34, // Sleep in nanoseconds
|
||||||
|
NANOSLEEP_UNTIL=35, // Sleep until this time nanoseconds (if this is below the current time return)
|
||||||
|
GET_TIME = 36, // Gets the system time (some derivitive of seconds)
|
||||||
|
SET_TIME = 37, // Sets the system time (some derivitive of seconds)
|
||||||
|
|
||||||
|
|
||||||
|
// Socket SysCall
|
||||||
|
SOCKET_BIND=39, // Used by servers to lock a port
|
||||||
|
SOCKET_CONNECT=40,
|
||||||
|
SOCKET_DISCONNECT=41,
|
||||||
|
SOCKET_SEND=42,
|
||||||
|
SOCKET_RECIEVE=43,
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Security Syscalls
|
// Security Syscalls
|
||||||
ENCRYPT = 50,
|
ENCRYPT = 50,
|
||||||
EMPTY = 0xFFFF,
|
EMPTY = 0xFFFF,
|
||||||
|
|
Reference in a new issue