push
This commit is contained in:
parent
04859555e9
commit
9f1b708ccf
17
src/main.rs
17
src/main.rs
|
@ -1,19 +1,19 @@
|
||||||
extern crate wabt;
|
use wabt;
|
||||||
extern crate wasmi;
|
|
||||||
|
|
||||||
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;
|
||||||
|
use wasm_sys::SysCall;
|
||||||
|
|
||||||
pub const KILL: usize = 0;
|
pub const KILL: usize = 0;
|
||||||
pub const ADD_FUNC_INDEX: usize = 1;
|
|
||||||
|
|
||||||
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 {
|
let (params, ret_ty): (&[ValueType], Option<ValueType>) = match index {
|
||||||
ADD_FUNC_INDEX => (&[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
|
||||||
|
@ -27,8 +27,7 @@ impl Externals for HostFunctions {
|
||||||
args: RuntimeArgs,
|
args: RuntimeArgs,
|
||||||
) -> Result<Option<RuntimeValue>, Trap> {
|
) -> Result<Option<RuntimeValue>, Trap> {
|
||||||
match index {
|
match index {
|
||||||
ADD_FUNC_INDEX => {
|
SysCall::KILL => {
|
||||||
println!("you pe");
|
|
||||||
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;
|
||||||
|
@ -42,7 +41,7 @@ impl Externals for HostFunctions {
|
||||||
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" => ADD_FUNC_INDEX,
|
"add" => SysCall::KILL,
|
||||||
_ => {
|
_ => {
|
||||||
return Err(Error::Instantiation(format!(
|
return Err(Error::Instantiation(format!(
|
||||||
"Export {} not found",
|
"Export {} not found",
|
||||||
|
@ -67,7 +66,7 @@ impl ModuleImportResolver for HostFunctions {
|
||||||
|
|
||||||
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!("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,
|
||||||
|
|
16
src/test.wat
16
src/test.wat
|
@ -1,16 +0,0 @@
|
||||||
(module
|
|
||||||
(import "host" "add" (func $add (param i32 i32)(result i32)))
|
|
||||||
(func (export "main") (result i32)
|
|
||||||
(call $add (i32.const 123) (i32.const 456))
|
|
||||||
(; i32.const 1337 ;)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(;;
|
|
||||||
(func $main (export "_start")
|
|
||||||
(; (call $print_greeting) ;)
|
|
||||||
return
|
|
||||||
)
|
|
||||||
;;)
|
|
36
src/wasm_sys.rs
Normal file
36
src/wasm_sys.rs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#[repr(usize)]
|
||||||
|
pub enum SysCall {
|
||||||
|
KILL = 0, // Provide a PID
|
||||||
|
CONSOLE_RESET = 1, // Reset the console
|
||||||
|
CONSOLE_IN = 2, // Console Input
|
||||||
|
CONSOLE_OUT = 3, // Console output
|
||||||
|
CONSOLE_GET_TITLE = 4, // Get the console title
|
||||||
|
CONSOLE_SET_TITLE = 5, // Set the console title
|
||||||
|
|
||||||
|
//scheduler Related Syscals
|
||||||
|
GET_PRIORITY = 10, // Get scheduler priority
|
||||||
|
SET_PRIORITY = 11, // Set scheduler priority
|
||||||
|
//
|
||||||
|
GET_HOSTNAME = 12,
|
||||||
|
SET_HOSTNAME = 13,
|
||||||
|
|
||||||
|
//File Related syscalls
|
||||||
|
GET_CONFIG = 20, // Get config
|
||||||
|
SET_CONFIG = 21, // Set the config
|
||||||
|
//
|
||||||
|
MAKE_DIRECTORY = 22, //
|
||||||
|
DELETE_DIRECTORY = 23, //
|
||||||
|
RENAME_DIRECTORY = 24, //
|
||||||
|
SET_DIRECTORY_ACCESS = 25, //
|
||||||
|
//
|
||||||
|
MAKE_FILE = 26, //
|
||||||
|
DELETE_FILE = 27, //
|
||||||
|
RENAME_FILE = 28, //
|
||||||
|
SET_FILE_ACCESS = 29, //
|
||||||
|
|
||||||
|
FILE_READ = 30,
|
||||||
|
FILE_WRITE = 31,
|
||||||
|
|
||||||
|
// Security Syscalls
|
||||||
|
ENCRYPT = 50,
|
||||||
|
}
|
6
wasm/test.wat
Normal file
6
wasm/test.wat
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
(module
|
||||||
|
(import "host" "add" (func $add (param i32 i32)(result i32)))
|
||||||
|
(func (export "main") (result i32)
|
||||||
|
(call $add (i32.const 123) (i32.const 456))
|
||||||
|
)
|
||||||
|
)
|
Reference in a new issue