#![no_std] #![no_main] #![feature(custom_test_frameworks)] #![test_runner(xe_os::test_runner)] #![reexport_test_harness_main = "test_main"] extern crate alloc; 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); fn kernel_main(boot_info: &'static BootInfo) -> ! { println!("booted"); xe_os::init(&boot_info); println!("Hello World{}", "!"); #[cfg(test)] test_main(); println!("It did not crash!"); run_wasm(); 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] fn panic(info: &PanicInfo) -> ! { println!("{}", info); xe_os::hlt_loop(); } // our panic handler in test mode #[cfg(test)] #[panic_handler] 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) } }