add __syscall42 (pipe)

This commit is contained in:
Mackenzie Clark 2019-03-01 10:54:39 -08:00
parent cf2909f5f3
commit 2ea9d0b09b
7 changed files with 52 additions and 0 deletions

13
lib/emscripten/emtests/test_pipe.c vendored Normal file
View File

@ -0,0 +1,13 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
int mypipe[2];
printf("pipe\n");
if (pipe(mypipe)) {
printf("error\n");
}
return 0;
}

1
lib/emscripten/emtests/test_pipe.out vendored Normal file
View File

@ -0,0 +1 @@
pipe

BIN
lib/emscripten/emtests/test_pipe.wasm vendored Normal file

Binary file not shown.

View File

@ -481,6 +481,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
"___syscall39" => func!(crate::syscalls::___syscall39),
"___syscall38" => func!(crate::syscalls::___syscall38),
"___syscall40" => func!(crate::syscalls::___syscall40),
"___syscall42" => func!(crate::syscalls::___syscall42),
"___syscall54" => func!(crate::syscalls::___syscall54),
"___syscall57" => func!(crate::syscalls::___syscall57),
"___syscall60" => func!(crate::syscalls::___syscall60),

View File

@ -40,6 +40,7 @@ use libc::{
use wasmer_runtime_core::vm::Ctx;
use super::env;
use std::cell::Cell;
use std::slice;
// use std::sys::fd::FileDesc;
@ -140,6 +141,33 @@ pub fn ___syscall40(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int
unsafe { rmdir(pathname_addr) }
}
// pipe
pub fn ___syscall42(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int {
debug!("emscripten::___syscall42 (pipe)");
// offset to a file descriptor, which contains a read end and write end, 2 integers
let fd_offset: u32 = varargs.get(ctx);
use std::cell::Cell;
let emscripten_memory = ctx.memory(0);
// convert the file descriptor into a vec with two slots
let mut fd_vec: Vec<c_int> = emscripten_memory.view()[((fd_offset / 4) as usize)..]
.iter()
.map(|pipe_end: &Cell<c_int>| pipe_end.get())
.take(2)
.collect();
// get it as a mutable pointer
let fd_ptr = fd_vec.as_mut_ptr();
// call pipe and store the pointers in this array
#[cfg(target_os = "windows")]
let result: c_int = unsafe { libc::pipe(fd_ptr, 2048, 0) };
#[cfg(not(target_os = "windows"))]
let result: c_int = unsafe { libc::pipe(fd_ptr) };
result
}
pub fn ___syscall60(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 {
debug!("emscripten::___syscall60");
-1

View File

@ -118,6 +118,7 @@ mod test_nested_struct_varargs;
mod test_nl_types;
mod test_perrar;
mod test_phiundef;
mod test_pipe;
mod test_poll;
mod test_posixtime;
mod test_printf_2;

View File

@ -0,0 +1,8 @@
use crate::emtests::_common::assert_emscripten_output;
#[test]
fn test_pipe() {
let wasm_bytes = include_bytes!("../../emtests/test_pipe.wasm");
let expected_str = include_str!("../../emtests/test_pipe.out");
assert_emscripten_output(wasm_bytes, expected_str);
}