pypyjs.wasm is finally working 🎉

This commit is contained in:
Syrus Akbary 2018-11-21 20:58:32 -08:00
parent a50e846f9b
commit ccdabd26b5
5 changed files with 57 additions and 50 deletions

View File

@ -18,8 +18,7 @@ pub extern "C" fn _emscripten_memcpy_big(dest: u32, src: u32, len: u32, instance
/// emscripten: getTotalMemory
pub extern "C" fn get_total_memory(instance: &mut Instance) -> u32 {
debug!("emscripten::get_total_memory");
// instance.memories[0].current_size()
67108864
instance.memories[0].current_size()
}
/// emscripten: enlargeMemory

View File

@ -22,6 +22,8 @@ pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
import_object.set("env", "___syscall3", ImportValue::Func(syscalls::___syscall3 as *const u8));
import_object.set("env", "___syscall4", ImportValue::Func(syscalls::___syscall4 as *const u8));
import_object.set("env", "___syscall5", ImportValue::Func(syscalls::___syscall5 as *const u8));
import_object.set("env", "___syscall54", ImportValue::Func(syscalls::___syscall54 as *const u8));
import_object.set("env", "___syscall122", ImportValue::Func(syscalls::___syscall122 as *const u8));
// Emscripten other APIs
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));

View File

@ -1,4 +1,5 @@
/// NOTE: These syscalls only support wasm_32 for now because they take u32 offset
/// Syscall list: https://www.cs.utexas.edu/~bismith/test/syscalls/syscalls32.html
use libc::{
c_int,
@ -11,62 +12,68 @@ use libc::{
open,
close,
};
use std::{mem, ptr};
use std::os::raw::c_char;
use std::ffi::CStr;
use crate::webassembly::{Instance};
/// emscripten: ___syscall3 (sys_read)
// A macro to retrieve variadic arguments given a varargs offset
macro_rules! vararg {
($name:ident, $type:ident, $instance:ident, $varargs:ident) => (
let ($name, $varargs) = unsafe {
let ptr = $instance.memory_offset_addr(0, $varargs as usize);
let ret = ptr::read(ptr as *const $type);
(ret, $varargs + 4)
}
)
}
/// sys_read
pub extern "C" fn ___syscall3(which: c_int, varargs: c_int, instance: &mut Instance) -> ssize_t {
// function ___syscall3(which, varargs) {
// which, varargs
// SYSCALLS.varargs = varargs;
// try {
// var stream = SYSCALLS.getStreamFromFD(),
// buf = SYSCALLS.get(),
// count = SYSCALLS.get();
// return FS.read(stream, HEAP8, buf, count);
// } catch (e) {
// if (typeof FS === "undefined" || !(e instanceof FS.ErrnoError)) abort(e);
// return -e.errno;
// }
// }
debug!("emscripten::___syscall3({}, {})", which, varargs);
debug!("emscripten::___syscall3");
0
}
/// emscripten: ___syscall4 (sys_write)
pub extern "C" fn ___syscall4(which_ptr: c_int, varargs_ptr: c_int, instance: &mut Instance) -> c_int {
// function ___syscall4(which, varargs) {
// SYSCALLS.varargs = varargs;
// try {
// var stream = SYSCALLS.getStreamFromFD(),
// buf = SYSCALLS.get(),
// count = SYSCALLS.get();
// return FS.write(stream, HEAP8, buf, count);
// } catch (e) {
// if (typeof FS === "undefined" || !(e instanceof FS.ErrnoError)) abort(e);
// return -e.errno;
// }
// }
debug!("emscripten::___syscall4({}, {})", which_ptr, varargs_ptr);
0
/// sys_write
#[no_mangle]
pub extern "C" fn ___syscall4(which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
debug!("emscripten::___syscall4");
vararg!(fd, i32, instance, varargs);
vararg!(buf_ptr, u32, instance, varargs);
vararg!(count, u32, instance, varargs);
debug!("fd: {}, buf_ptr: {}, count: {}", fd, buf_ptr, count);
let buf = instance.memory_offset_addr(0, buf_ptr as usize) as *const c_void;
unsafe { write(fd, buf, count as usize) as i32 }
}
/// emscripten: ___syscall5 (sys_open)
/// sys_open
pub extern "C" fn ___syscall5(which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
// function ___syscall5(which, varargs) {
// SYSCALLS.varargs = varargs;
// try {
// var pathname = SYSCALLS.getStr(),
// flags = SYSCALLS.get(),
// mode = SYSCALLS.get();
// var stream = FS.open(pathname, flags, mode);
// return stream.fd;
// } catch (e) {
// if (typeof FS === "undefined" || !(e instanceof FS.ErrnoError)) abort(e);
// return -e.errno;
// }
// }
debug!("host::___syscall5({}, {})", which, varargs);
debug!("emscripten::___syscall5");
vararg!(pathname, u32, instance, varargs);
vararg!(flags, u32, instance, varargs);
vararg!(mode, u32, instance, varargs);
debug!("pathname: {}, flags: {}, mode: {}", pathname, flags, mode);
-2
}
// sys_ioctl
#[no_mangle]
pub extern "C" fn ___syscall54(which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
debug!("emscripten::___syscall54");
vararg!(stream, u32, instance, varargs);
vararg!(op, u32, instance, varargs);
debug!("stream: {}, op: {}", stream, op);
0
}
// sys_newuname
#[no_mangle]
pub extern "C" fn ___syscall122(which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
debug!("emscripten::___syscall122");
vararg!(buf, u32, instance, varargs);
debug!("buf: {}", buf);
0
}

View File

@ -17,6 +17,6 @@ use crate::webassembly::{Instance};
use std::env;
pub extern "C" fn get_env(name: &str, instance: &mut Instance) -> Result<String, env::VarError> {
debug!("host::get_env({})", name);
debug!("host::get_env({:?})", name);
env::var(name)
}

View File

@ -415,7 +415,6 @@ impl Instance {
// let func_index = *elem_index - module.info.imported_funcs.len() as u32;
// let func_addr = functions[func_index.index()].as_ptr();
println!("TABLE LENGTH: {}", table.len());
let func_addr = get_function_addr(&func_index, &import_functions, &functions);
table[base + table_element.offset + i] = func_addr as _;
}