Refactored host code

This commit is contained in:
Syrus Akbary 2018-11-21 15:10:03 -08:00
parent 1f6e640054
commit a50e846f9b
5 changed files with 162 additions and 79 deletions

View File

@ -0,0 +1,34 @@
/// NOTE: These syscalls only support wasm_32 for now because they take u32 offset
use libc::{
c_int,
c_void,
size_t,
ssize_t,
exit,
read,
open,
close,
};
use std::os::raw::c_char;
use std::ffi::CStr;
use super::super::host;
use crate::webassembly::{Instance};
/// emscripten: _getenv
pub extern "C" fn _getenv(name_ptr: c_int, instance: &mut Instance) -> c_int {
debug!("emscripten::_getenv {}", name_ptr);
let name = unsafe {
let memory_name_ptr = instance.memory_offset_addr(0, name_ptr as usize) as *const c_char;
CStr::from_ptr(memory_name_ptr).to_str().unwrap()
};
match host::get_env(name, instance) {
Ok(_) => {
unimplemented!();
}
Err(_) => {
0
}
}
}

View File

@ -1,10 +1,12 @@
use crate::webassembly::{ImportObject, ImportValue};
// EMSCRIPTEN APIS
mod env;
mod memory;
mod process;
mod io;
mod utils;
mod syscalls;
// SYSCALLS
use super::host;
@ -14,15 +16,13 @@ pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
let mut import_object = ImportObject::new();
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", "")
// // EMSCRIPTEN SYSCALLS
import_object.set("env", "_getenv", ImportValue::Func(host::get_env 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", "___syscall4", ImportValue::Func(host::sys_write 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));
// // EMSCRIPTEN APIS
// Emscripten Env
import_object.set("env", "_getenv", ImportValue::Func(env::_getenv as *const u8));
// Emscripten syscalls
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));
// 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));
import_object.set("env", "abortOnCannotGrowMemory", ImportValue::Func(process::abort_on_cannot_grow_memory as *const u8));

View File

@ -0,0 +1,72 @@
/// NOTE: These syscalls only support wasm_32 for now because they take u32 offset
use libc::{
c_int,
c_void,
size_t,
ssize_t,
exit,
read,
write,
open,
close,
};
use std::os::raw::c_char;
use std::ffi::CStr;
use crate::webassembly::{Instance};
/// emscripten: ___syscall3 (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);
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
}
/// emscripten: ___syscall5 (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);
-2
}

View File

@ -14,20 +14,9 @@ use std::os::raw::c_char;
use std::ffi::CStr;
use crate::webassembly::{Instance};
use std::env;
/// emscripten: _getenv
pub extern "C" fn get_env(name_ptr: c_int, instance: &mut Instance) -> c_int {
// name = Pointer_stringify(name);
// if (!ENV.hasOwnProperty(name)) return 0;
// if (_getenv.ret) _free(_getenv.ret);
// _getenv.ret = allocate(intArrayFromString(ENV[name]), "i8", ALLOC_NORMAL);
// return _getenv.ret;
debug!("host::get_env {}", name_ptr);
let name = unsafe {
let memory_name_ptr = instance.memory_offset_addr(0, name_ptr as usize) as *const c_char;
CStr::from_ptr(memory_name_ptr)
};
// let x: &str; unsafe { mem::transmute(str_addr) }
debug!("host::get_env::name {:?}", name);
return 0
pub extern "C" fn get_env(name: &str, instance: &mut Instance) -> Result<String, env::VarError> {
debug!("host::get_env({})", name);
env::var(name)
}

View File

@ -1,60 +1,48 @@
/// NOTE: These syscalls only support wasm_32 for now because they take u32 offset
// NOTE: These syscalls only support wasm_32 for now because they take u32 offset
use libc::{
c_int,
c_void,
size_t,
ssize_t,
exit,
read,
write,
open,
close,
};
// use libc::{
// c_int,
// c_void,
// size_t,
// ssize_t,
// exit,
// read,
// write,
// open,
// close,
// };
use crate::webassembly::{Instance};
// use crate::webassembly::{Instance};
/// emscripten: ___syscall1
pub extern "C" fn sys_exit(status: c_int, _instance: &mut Instance) {
debug!("host::sys_exit");
unsafe { exit(status); }
}
/// emscripten: ___syscall3
pub extern "C" fn sys_read(fd: c_int, buf: u32, count: size_t, instance: &mut Instance) -> ssize_t {
debug!("host::sys_read");
let buf_addr = instance.memory_offset_addr(0, buf as usize) as *mut c_void;
unsafe { read(fd, buf_addr, count) }
}
/// emscripten: ___syscall4
pub extern "C" fn sys_write(which: c_int, mode: 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;
// }
// /// emscripten: ___syscall1
// pub extern "C" fn sys_exit(status: c_int, _instance: &mut Instance) {
// debug!("host::sys_exit");
// unsafe { exit(status); }
// }
debug!("host::sys_write({}, {})", which, mode);
// unsafe { write(which, mode) };
0
}
/// emscripten: ___syscall5
pub extern "C" fn sys_open(path: u32, flags: c_int, mode: c_int, instance: &mut Instance) -> c_int {
debug!("host::sys_open({}, {}, {})", path, flags, mode);
// let path_addr = instance.memory_offset_addr(0, path as usize) as *const i8;
// unsafe { open(path_addr, flags, mode) };
-2
}
/// emscripten: ___syscall6
pub extern "C" fn sys_close(fd: c_int, _instance: &mut Instance) -> c_int {
debug!("host::sys_close");
unsafe { close(fd) }
}
// /// emscripten: ___syscall3
// pub extern "C" fn sys_read(fd: c_int, buf: *mut c_void, count: size_t, instance: &mut Instance) -> ssize_t {
// debug!("host::sys_read");
// let buf_addr = instance.memory_offset_addr(0, buf as usize) as *mut c_void;
// unsafe { read(fd, buf_addr, count) }
// }
// /// emscripten: ___syscall4
// pub extern "C" fn sys_write(which: c_int, mode: c_int, instance: &mut Instance) -> c_int {
// debug!("host::sys_write({}, {})", which, mode);
// // unsafe { write(which, mode) };
// 0
// }
// /// emscripten: ___syscall5
// pub extern "C" fn sys_open(path: u32, flags: c_int, mode: c_int, instance: &mut Instance) -> c_int {
// debug!("host::sys_open({}, {}, {})", path, flags, mode);
// // let path_addr = instance.memory_offset_addr(0, path as usize) as *const i8;
// // unsafe { open(path_addr, flags, mode) };
// -2
// }
// /// emscripten: ___syscall6
// pub extern "C" fn sys_close(fd: c_int, _instance: &mut Instance) -> c_int {
// debug!("host::sys_close");
// unsafe { close(fd) }
// }