rust -> wasm binry can now be loaded
This commit is contained in:
parent
9e0d14ee00
commit
5ea0f4938b
52
src/main.rs
52
src/main.rs
|
@ -13,7 +13,7 @@ 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 => (&[], None),
|
||||||
SysCall::EMPTY => (&[], None),
|
SysCall::EMPTY => (&[], None),
|
||||||
_ => return false,
|
_ => return false,
|
||||||
};
|
};
|
||||||
|
@ -27,13 +27,12 @@ impl Externals for HostFunctions {
|
||||||
args: RuntimeArgs,
|
args: RuntimeArgs,
|
||||||
) -> Result<Option<RuntimeValue>, Trap> {
|
) -> Result<Option<RuntimeValue>, Trap> {
|
||||||
match index.into() {
|
match index.into() {
|
||||||
|
/// Take in one arg discard the rest
|
||||||
SysCall::KILL => {
|
SysCall::KILL => {
|
||||||
let a: u32 = args.nth_checked(0)?;
|
println!("hewwo");
|
||||||
let b: u32 = args.nth_checked(1)?;
|
Ok(None)
|
||||||
let result = a + b;
|
|
||||||
|
|
||||||
Ok(Some(RuntimeValue::I32(result as i32)))
|
|
||||||
}
|
}
|
||||||
|
/// Do nothing
|
||||||
SysCall::EMPTY => Ok(None),
|
SysCall::EMPTY => Ok(None),
|
||||||
// SysCall::CONSOLE_RESET => {}
|
// SysCall::CONSOLE_RESET => {}
|
||||||
// SysCall::CONSOLE_IN => {}
|
// SysCall::CONSOLE_IN => {}
|
||||||
|
@ -47,7 +46,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" => SysCall::KILL as usize,
|
"kill" => SysCall::KILL as usize,
|
||||||
"empty" => SysCall::EMPTY as usize,
|
"empty" => SysCall::EMPTY as usize,
|
||||||
_ => {
|
_ => {
|
||||||
return Err(Error::Instantiation(format!(
|
return Err(Error::Instantiation(format!(
|
||||||
|
@ -65,7 +64,7 @@ impl ModuleImportResolver for HostFunctions {
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(FuncInstance::alloc_host(
|
Ok(FuncInstance::alloc_host(
|
||||||
Signature::new(&[ValueType::I32, ValueType::I32][..], Some(ValueType::I32)),
|
Signature::new(&[][..], None),
|
||||||
index,
|
index,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -73,22 +72,25 @@ 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!("../wasm/test.wat"));
|
// let wasm_binary = wabt::wat2wasm(include_str!("../wasm/test.wat"));
|
||||||
|
let wasm_binary = //wabt::wat2wasm(
|
||||||
let wasm_binary = match wasm_binary {
|
include_bytes!("../wasm/ableos-wasm-test.wasm"); //.unwrap();
|
||||||
Ok(abc) => abc,
|
// );
|
||||||
Err(abc) => {
|
/*
|
||||||
println!("{}", abc);
|
let wasm_binary = match wasm_binary {
|
||||||
return;
|
Ok(abc) => abc,
|
||||||
}
|
Err(abc) => {
|
||||||
};
|
println!("{}", abc);
|
||||||
|
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("env", &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.
|
||||||
|
@ -98,13 +100,13 @@ fn main() {
|
||||||
|
|
||||||
// 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 = instance
|
||||||
.invoke_export("main", &[], &mut HostFunctions)
|
.invoke_export("_start", &[], &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!(
|
||||||
"{:?}",
|
"{:?}",
|
||||||
|
|
BIN
wasm/ableos-wasm-test.wasm
Executable file
BIN
wasm/ableos-wasm-test.wasm
Executable file
Binary file not shown.
Reference in a new issue