mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-25 14:41:32 +00:00
Get env printf basic example working
This commit is contained in:
@ -1,14 +1,21 @@
|
|||||||
use crate::webassembly::{ImportObject, VmCtx};
|
use crate::webassembly::{ImportObject, VmCtx};
|
||||||
use libc::putchar;
|
use libc::{printf, putchar};
|
||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
fn printf(format: i32, values: i32, context: &VmCtx) -> i32 {
|
extern "C" fn _printf(memory_offset: i32, extra: i32, vm_context: &mut VmCtx) -> i32 {
|
||||||
println!("PRINTF {:?} {:?}", format, values);
|
// println!("PRINTF");
|
||||||
return 0;
|
let mem = &vm_context.user_data.instance.memories[0];
|
||||||
|
// let x = mem.carve_slice(memory_offset as u32, 16).unwrap();
|
||||||
|
return unsafe {
|
||||||
|
let base_memory_offset = mem.mmap.as_ptr().offset(memory_offset as isize) as *const i8;
|
||||||
|
printf(base_memory_offset, extra)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_libc_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
|
pub fn generate_libc_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
|
||||||
let mut import_object = ImportObject::new();
|
let mut import_object = ImportObject::new();
|
||||||
import_object.set("env", "printf", printf as *const u8);
|
import_object.set("env", "printf", _printf as *const u8);
|
||||||
import_object.set("env", "putchar", putchar as *const u8);
|
import_object.set("env", "putchar", putchar as *const u8);
|
||||||
import_object
|
import_object
|
||||||
}
|
}
|
||||||
@ -37,19 +44,19 @@ mod tests {
|
|||||||
main(&context);
|
main(&context);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[test]
|
#[test]
|
||||||
// fn test_printf() {
|
fn test_print() {
|
||||||
// let wasm_bytes = include_wast2wasm_bytes!("tests/printf.wast");
|
let wasm_bytes = include_wast2wasm_bytes!("tests/printf.wast");
|
||||||
// let import_object = generate_libc_env();
|
let import_object = generate_libc_env();
|
||||||
// let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
|
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
|
||||||
// let module = result_object.module;
|
let module = result_object.module;
|
||||||
// let mut instance = result_object.instance;
|
let mut instance = result_object.instance;
|
||||||
// let func_index = match module.info.exports.get("main") {
|
let func_index = match module.info.exports.get("main") {
|
||||||
// Some(&Export::Function(index)) => index,
|
Some(&Export::Function(index)) => index,
|
||||||
// _ => panic!("Function not found"),
|
_ => panic!("Function not found"),
|
||||||
// };
|
};
|
||||||
// let main: fn(&VmCtx) = get_instance_function!(instance, func_index);
|
let main: fn(&VmCtx) = get_instance_function!(instance, func_index);
|
||||||
// let context = instance.generate_context();
|
let context = instance.generate_context();
|
||||||
// main(&context);
|
main(&context);
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
19
src/integrations/tests/printf.wast
Normal file
19
src/integrations/tests/printf.wast
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
(module
|
||||||
|
(type $FUNCSIG$ii (func (param i32) (result i32)))
|
||||||
|
(type $FUNCSIG$iii (func (param i32 i32) (result i32)))
|
||||||
|
(import "env" "printf" (func $printf (param i32 i32) (result i32)))
|
||||||
|
(table 0 anyfunc)
|
||||||
|
(memory $0 1)
|
||||||
|
(data (i32.const 16) "Hello World\00")
|
||||||
|
(export "memory" (memory $0))
|
||||||
|
(export "main" (func $main))
|
||||||
|
(func $main (; 1 ;) (result i32)
|
||||||
|
(drop
|
||||||
|
(call $printf
|
||||||
|
(i32.const 16)
|
||||||
|
(i32.const 0)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(i32.const 0)
|
||||||
|
)
|
||||||
|
)
|
@ -116,6 +116,18 @@ impl LinearMemory {
|
|||||||
|
|
||||||
Some(prev_pages as i32)
|
Some(prev_pages as i32)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn carve_slice(&self, offset: u32, size: u32) -> Option<&[u8]> {
|
||||||
|
let start = offset as usize;
|
||||||
|
let end = start + size as usize;
|
||||||
|
let slice: &[u8] = &*self;
|
||||||
|
|
||||||
|
// if end <= self.mapped_size() {
|
||||||
|
Some(&slice[start..end])
|
||||||
|
// } else {
|
||||||
|
// None
|
||||||
|
// }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for LinearMemory {
|
impl fmt::Debug for LinearMemory {
|
||||||
@ -151,3 +163,18 @@ impl DerefMut for LinearMemory {
|
|||||||
&mut *self.mmap
|
&mut *self.mmap
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// impl Clone for LinearMemory {
|
||||||
|
// fn clone(&self) -> LinearMemory {
|
||||||
|
// let mut mmap = MmapMut::map_anon(self.maximum.unwrap_or(self.current) as usize).unwrap();
|
||||||
|
// let mut base_mmap = &self.mmap;
|
||||||
|
// let to_init = &mut mmap[0..self.current as usize];
|
||||||
|
// to_init.copy_from_slice(&self.mmap);
|
||||||
|
|
||||||
|
// return LinearMemory {
|
||||||
|
// mmap: mmap,
|
||||||
|
// current: self.current,
|
||||||
|
// maximum: self.maximum,
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
Reference in New Issue
Block a user