mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-15 01:41:21 +00:00
Make emscripten “work”
This commit is contained in:
@ -7,6 +7,7 @@ pub use libc::putchar;
|
|||||||
|
|
||||||
/// printf
|
/// printf
|
||||||
pub extern "C" fn printf(memory_offset: i32, extra: i32, instance: &Instance) -> i32 {
|
pub extern "C" fn printf(memory_offset: i32, extra: i32, instance: &Instance) -> i32 {
|
||||||
|
debug!("emscripten::printf");
|
||||||
let mem = &instance.memories[0];
|
let mem = &instance.memories[0];
|
||||||
return unsafe {
|
return unsafe {
|
||||||
let base_memory_offset = mem.mmap.as_ptr().offset(memory_offset as isize) as *const i8;
|
let base_memory_offset = mem.mmap.as_ptr().offset(memory_offset as isize) as *const i8;
|
||||||
|
@ -8,6 +8,7 @@ use crate::webassembly::{Instance};
|
|||||||
|
|
||||||
/// emscripten: _emscripten_memcpy_big
|
/// emscripten: _emscripten_memcpy_big
|
||||||
pub extern "C" fn _emscripten_memcpy_big(dest: u32, src: u32, len: u32, instance: &mut Instance) -> u32 {
|
pub extern "C" fn _emscripten_memcpy_big(dest: u32, src: u32, len: u32, instance: &mut Instance) -> u32 {
|
||||||
|
debug!("emscripten::_emscripten_memcpy_big");
|
||||||
let dest_addr = instance.memory_offset_addr(0, dest as usize) as *mut c_void;
|
let dest_addr = instance.memory_offset_addr(0, dest as usize) as *mut c_void;
|
||||||
let src_addr = instance.memory_offset_addr(0, src as usize) as *mut c_void;
|
let src_addr = instance.memory_offset_addr(0, src as usize) as *mut c_void;
|
||||||
unsafe { memcpy(dest_addr, src_addr, len as size_t); }
|
unsafe { memcpy(dest_addr, src_addr, len as size_t); }
|
||||||
@ -16,10 +17,12 @@ pub extern "C" fn _emscripten_memcpy_big(dest: u32, src: u32, len: u32, instance
|
|||||||
|
|
||||||
/// emscripten: getTotalMemory
|
/// emscripten: getTotalMemory
|
||||||
pub extern "C" fn get_total_memory(instance: &mut Instance) -> u32 {
|
pub extern "C" fn get_total_memory(instance: &mut Instance) -> u32 {
|
||||||
|
debug!("emscripten::get_total_memory");
|
||||||
instance.memories[0].current_size()
|
instance.memories[0].current_size()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// emscripten: enlargeMemory
|
/// emscripten: enlargeMemory
|
||||||
pub extern "C" fn enlarge_memory() {
|
pub extern "C" fn enlarge_memory(instance: &mut Instance) {
|
||||||
// stub
|
debug!("emscripten::enlarge_memory");
|
||||||
|
// instance.memories[0].grow(100);
|
||||||
}
|
}
|
||||||
|
@ -12,17 +12,17 @@ pub fn generate_emscripten_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", ImportValue::Func(io::printf as *const u8));
|
import_object.set("env", "printf", ImportValue::Func(io::printf as *const u8));
|
||||||
import_object.set("env", "putchar", ImportValue::Func(io::putchar as *const u8));
|
import_object.set("env", "putchar", ImportValue::Func(io::putchar as *const u8));
|
||||||
// EMSCRIPTEN SYSCALLS
|
// // EMSCRIPTEN SYSCALLS
|
||||||
import_object.set("env", "___syscall1", ImportValue::Func(host::sys_exit as *const u8));
|
import_object.set("env", "___syscall1", ImportValue::Func(host::sys_exit as *const u8));
|
||||||
import_object.set("env", "___syscall3", ImportValue::Func(host::sys_read as *const u8));
|
import_object.set("env", "___syscall3", ImportValue::Func(host::sys_read as *const u8));
|
||||||
import_object.set("env", "___syscall5", ImportValue::Func(host::sys_open as *const u8));
|
import_object.set("env", "___syscall5", ImportValue::Func(host::sys_open as *const u8));
|
||||||
import_object.set("env", "___syscall6", ImportValue::Func(host::sys_close as *const u8));
|
import_object.set("env", "___syscall6", ImportValue::Func(host::sys_close as *const u8));
|
||||||
// EMSCRIPTEN APIS
|
// // EMSCRIPTEN APIS
|
||||||
import_object.set("env", "abort", ImportValue::Func(process::em_abort as *const u8));
|
import_object.set("env", "abort", ImportValue::Func(process::em_abort as *const u8));
|
||||||
import_object.set("env", "_abort", ImportValue::Func(process::_abort as *const u8));
|
import_object.set("env", "_abort", ImportValue::Func(process::_abort as *const u8));
|
||||||
import_object.set("env", "abortOnCannotGrowMemory", ImportValue::Func(process::abort_on_cannot_grow_memory as *const u8));
|
import_object.set("env", "abortOnCannotGrowMemory", ImportValue::Func(process::abort_on_cannot_grow_memory as *const u8));
|
||||||
import_object.set("env", "_emscripten_memcpy_big", ImportValue::Func(memory::_emscripten_memcpy_big as *const u8));
|
import_object.set("env", "_emscripten_memcpy_big", ImportValue::Func(memory::_emscripten_memcpy_big as *const u8));
|
||||||
import_object.set("env", "enlargeMemory", ImportValue::Func(memory::enlarge_memory as *const u8));
|
// import_object.set("env", "enlargeMemory", ImportValue::Func(memory::enlarge_memory as *const u8));
|
||||||
import_object.set("env", "getTotalMemory", ImportValue::Func(memory::get_total_memory as *const u8));
|
import_object.set("env", "getTotalMemory", ImportValue::Func(memory::get_total_memory as *const u8));
|
||||||
import_object
|
import_object
|
||||||
}
|
}
|
||||||
|
@ -11,17 +11,20 @@ use std::ffi::CStr;
|
|||||||
use crate::webassembly::{Instance};
|
use crate::webassembly::{Instance};
|
||||||
|
|
||||||
extern "C" fn abort_with_message(message: &str) {
|
extern "C" fn abort_with_message(message: &str) {
|
||||||
|
debug!("emscripten::abort_with_message");
|
||||||
println!("{}", message);
|
println!("{}", message);
|
||||||
_abort();
|
_abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// emscripten: _abort
|
/// emscripten: _abort
|
||||||
pub extern "C" fn _abort() {
|
pub extern "C" fn _abort() {
|
||||||
|
debug!("emscripten::_abort");
|
||||||
unsafe { abort(); }
|
unsafe { abort(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// emscripten: abort
|
/// emscripten: abort
|
||||||
pub extern "C" fn em_abort(message: u32, instance: &mut Instance) {
|
pub extern "C" fn em_abort(message: u32, instance: &mut Instance) {
|
||||||
|
debug!("emscripten::em_abort");
|
||||||
let message_addr = instance.memory_offset_addr(0, message as usize) as *mut c_char;
|
let message_addr = instance.memory_offset_addr(0, message as usize) as *mut c_char;
|
||||||
unsafe {
|
unsafe {
|
||||||
let message = CStr::from_ptr(message_addr)
|
let message = CStr::from_ptr(message_addr)
|
||||||
@ -34,6 +37,7 @@ pub extern "C" fn em_abort(message: u32, instance: &mut Instance) {
|
|||||||
|
|
||||||
/// emscripten: abortOnCannotGrowMemory
|
/// emscripten: abortOnCannotGrowMemory
|
||||||
pub extern "C" fn abort_on_cannot_grow_memory() {
|
pub extern "C" fn abort_on_cannot_grow_memory() {
|
||||||
|
debug!("emscripten::abort_on_cannot_grow_memory");
|
||||||
abort_with_message("Cannot enlarge memory arrays!");
|
abort_with_message("Cannot enlarge memory arrays!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,8 +85,8 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
|
|||||||
Some(&webassembly::Export::Function(index)) => index,
|
Some(&webassembly::Export::Function(index)) => index,
|
||||||
_ => panic!("Main function not found"),
|
_ => panic!("Main function not found"),
|
||||||
});
|
});
|
||||||
let main: fn(&webassembly::Instance) = get_instance_function!(instance, func_index);
|
let main: extern fn(u32, u32, &webassembly::Instance) = get_instance_function!(instance, func_index);
|
||||||
main(&instance);
|
main(0, 0, &instance);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user