From 43f5f9196a15d103c66d6adcb083f5da97c7b724 Mon Sep 17 00:00:00 2001 From: Within Date: Thu, 24 Oct 2019 09:43:00 -0400 Subject: [PATCH] move wasm into its own module --- src/lib.rs | 1 + src/main.rs | 67 +------------------------------------------------ src/wasm/mod.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 66 deletions(-) create mode 100644 src/wasm/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 5f2e20c..cfc99e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ pub mod interrupts; pub mod memory; pub mod serial; pub mod vga_buffer; +pub mod wasm; use linked_list_allocator::LockedHeap; diff --git a/src/main.rs b/src/main.rs index 3ace6b6..1633f97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,11 +10,6 @@ mod hack; use bootloader::{entry_point, BootInfo}; use core::panic::PanicInfo; use xe_os::{print, println}; -use wasmi::{ - Error as InterpreterError, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder, - ModuleImportResolver, ModuleInstance, ModuleRef, RuntimeArgs, RuntimeValue, Signature, Trap, - ValueType, - }; entry_point!(kernel_main); @@ -28,31 +23,12 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { println!("It did not crash!"); - run_wasm(); + xe_os::wasm::run(); println!("THE WASM RAN :DDDD"); xe_os::hlt_loop(); } -fn run_wasm() { - use alloc::vec::Vec; - - let bytes = include_bytes!("../data/h.wasm"); - let b: Vec = bytes.iter().cloned().collect(); - println!("loaded wasm into a Vec"); - - let module = wasmi::Module::from_buffer(&b).unwrap(); - let mut imports = ImportsBuilder::new(); - imports.push_resolver("h", &H); - - let instance = ModuleInstance::new(&module, &imports) - .expect("failed to load wasm module") - .assert_no_start(); - - println!("invoking export"); - let _ = instance.invoke_export("h", &[], &mut H); -} - // our existing panic handler #[cfg(not(test))] // new attribute #[panic_handler] @@ -67,44 +43,3 @@ fn panic(info: &PanicInfo) -> ! { fn panic(info: &PanicInfo) -> ! { xe_os::test_panic_handler(info); } - -struct H; -const H_FUNC: usize = 0; - -impl Externals for H { - fn invoke_index( - &mut self, - index: usize, - args: RuntimeArgs, - ) -> Result, Trap> { - use core::char::from_u32; - match index { - H_FUNC => { - let ch_t: i32 = args.nth(0); - let ch = from_u32(ch_t as u32); - print!("{}", ch.unwrap()); - Ok(None) - } - - _ => panic!("unknown function called"), - } - } -} - -impl ModuleImportResolver for H { - fn resolve_func( - &self, - field_name: &str, - _signature: &Signature, - ) -> Result { - let func_ref = match field_name { - "h" => FuncInstance::alloc_host( - Signature::new(&[ValueType::I32][..], None), - H_FUNC, - ), - _ => panic!("unknwon function asked for"), - }; - - Ok(func_ref) - } -} diff --git a/src/wasm/mod.rs b/src/wasm/mod.rs new file mode 100644 index 0000000..36c98cd --- /dev/null +++ b/src/wasm/mod.rs @@ -0,0 +1,66 @@ +use wasmi::{ + Error as InterpreterError, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder, + ModuleImportResolver, ModuleInstance, ModuleRef, RuntimeArgs, RuntimeValue, Signature, Trap, + ValueType, + }; +use crate::{print, println}; + +pub fn run() { + use alloc::vec::Vec; + + let bytes = include_bytes!("../../data/h.wasm"); + let b: Vec = bytes.iter().cloned().collect(); + println!("loaded wasm into a Vec"); + + let module = wasmi::Module::from_buffer(&b).unwrap(); + let mut imports = ImportsBuilder::new(); + imports.push_resolver("h", &H); + + let instance = ModuleInstance::new(&module, &imports) + .expect("failed to load wasm module") + .assert_no_start(); + + println!("invoking export"); + let _ = instance.invoke_export("h", &[], &mut H); +} + +struct H; +const H_FUNC: usize = 0; + +impl Externals for H { + fn invoke_index( + &mut self, + index: usize, + args: RuntimeArgs, + ) -> Result, Trap> { + use core::char::from_u32; + match index { + H_FUNC => { + let ch_t: i32 = args.nth(0); + let ch = from_u32(ch_t as u32); + print!("{}", ch.unwrap()); + Ok(None) + } + + _ => panic!("unknown function called"), + } + } +} + +impl ModuleImportResolver for H { + fn resolve_func( + &self, + field_name: &str, + _signature: &Signature, + ) -> Result { + let func_ref = match field_name { + "h" => FuncInstance::alloc_host( + Signature::new(&[ValueType::I32][..], None), + H_FUNC, + ), + _ => panic!("unknwon function asked for"), + }; + + Ok(func_ref) + } +}