Skip to content

Commit

Permalink
Try #307:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] committed Mar 29, 2019
2 parents 4df5f02 + 48d34d9 commit a8333cf
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ authors = ["The Wasmer Engineering Team <[email protected]>"]
edition = "2018"

[dependencies]
wasmer-runtime-core = { path = "../runtime-core", version = "0.2.1" }
wasmer-runtime-core = { path = "../runtime-core", version = "0.2.1" }
libc = "0.2.50"
30 changes: 26 additions & 4 deletions lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(unused)]

mod types;
pub mod types;
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub mod unix;
#[cfg(any(target_os = "windows"))]
pub mod windows;

use self::types::*;
use crate::{
Expand All @@ -9,6 +12,12 @@ use crate::{
};
use wasmer_runtime_core::{memory::Memory, vm::Ctx};

#[cfg(any(target_os = "linux", target_os = "macos"))]
pub use unix::*;

#[cfg(any(target_os = "windows"))]
pub use windows::*;

#[allow(clippy::mut_from_ref)]
fn get_wasi_state(ctx: &Ctx) -> &mut WasiState {
unsafe { &mut *(ctx.data as *mut WasiState) }
Expand Down Expand Up @@ -99,15 +108,28 @@ pub fn clock_res_get(
clock_id: __wasi_clockid_t,
resolution: WasmPtr<__wasi_timestamp_t>,
) -> __wasi_errno_t {
unimplemented!()
let memory = ctx.memory(0);

if let Some(out_addr) = resolution.deref(memory) {
platform_clock_res_get(clock_id, out_addr)
} else {
__WASI_EFAULT
}
}

pub fn clock_time_get(
ctx: &mut Ctx,
clock_id: __wasi_clockid_t,
precision: __wasi_timestamp_t,
time: WasmPtr<__wasi_timestamp_t>,
) -> __wasi_errno_t {
unimplemented!()
let memory = ctx.memory(0);

if let Some(out_addr) = time.deref(memory) {
platform_clock_time_get(clock_id, precision, out_addr)
} else {
__WASI_EFAULT
}
}

/// ### `environ_get()`
Expand Down
54 changes: 54 additions & 0 deletions lib/wasi/src/syscalls/unix/linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::syscalls::types::*;
use libc::{
clock_getres, clock_gettime, timespec, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID,
CLOCK_REALTIME, CLOCK_THREAD_CPUTIME_ID,
};
use std::cell::Cell;
use std::mem;

pub fn platform_clock_res_get(
clock_id: __wasi_clockid_t,
resolution: &Cell<__wasi_timestamp_t>,
) -> __wasi_errno_t {
let linux_clock_id = match clock_id {
__WASI_CLOCK_MONOTONIC => CLOCK_MONOTONIC,
__WASI_CLOCK_PROCESS_CPUTIME_ID => CLOCK_PROCESS_CPUTIME_ID,
__WASI_CLOCK_REALTIME => CLOCK_REALTIME,
__WASI_CLOCK_THREAD_CPUTIME_ID => CLOCK_THREAD_CPUTIME_ID,
_ => return __WASI_EINVAL,
};

let output = unsafe {
let mut timespec_out: timespec = mem::uninitialized();
clock_getres(linux_clock_id, &mut timespec_out);
};

resolution.set(timespec_out.tv_nsec as __wasi_timestamp_t);

// TODO: map output of clock_getres to __wasi_errno_t
__WASI_ESUCCESS
}

pub fn platform_clock_time_get(
clock_id: __wasi_clockid_t,
precision: __wasi_timestamp_t,
time: &Cell<__wasi_timestamp_t>,
) -> __wasi_errno_t {
let linux_clock_id = match clock_id {
__WASI_CLOCK_MONOTONIC => CLOCK_MONOTONIC,
__WASI_CLOCK_PROCESS_CPUTIME_ID => CLOCK_PROCESS_CPUTIME_ID,
__WASI_CLOCK_REALTIME => CLOCK_REALTIME,
__WASI_CLOCK_THREAD_CPUTIME_ID => CLOCK_THREAD_CPUTIME_ID,
_ => return __WASI_EINVAL,
};

let output = unsafe {
let mut timespec_out: timespec = mem::uninitialized();
clock_gettime(linux_clock_id, precision, &mut timespec_out);
};

resolution.set(timespec_out.tv_nsec as __wasi_timestamp_t);

// TODO: map output of clock_gettime to __wasi_errno_t
__WASI_ESUCCESS
}
17 changes: 17 additions & 0 deletions lib/wasi/src/syscalls/unix/macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::syscalls::types::*;
use std::cell::Cell;

pub fn platform_clock_res_get(
clock_id: __wasi_clockid_t,
resolution: &Cell<__wasi_timestamp_t>,
) -> __wasi_errno_t {
__WASI_EINVAL
}

pub fn platform_clock_time_get(
clock_id: __wasi_clockid_t,
precision: __wasi_timestamp_t,
time: &Cell<__wasi_timestamp_t>,
) -> __wasi_errno_t {
unimplemented!()
}
11 changes: 11 additions & 0 deletions lib/wasi/src/syscalls/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[cfg(target_os = "linux")]
pub mod linux;

#[cfg(target_os = "macos")]
pub mod macos;

#[cfg(target_os = "linux")]
pub use linux::*;

#[cfg(target_os = "macos")]
pub use macos::*;
17 changes: 17 additions & 0 deletions lib/wasi/src/syscalls/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::syscalls::types::*;
use std::cell::Cell;

pub fn platform_clock_res_get(
clock_id: __wasi_clockid_t,
resolution: &Cell<__wasi_timestamp_t>,
) -> __wasi_errno_t {
__WASI_EINVAL
}

pub fn platform_clock_time_get(
clock_id: __wasi_clockid_t,
precision: __wasi_timestamp_t,
time: &Cell<__wasi_timestamp_t>,
) -> __wasi_errno_t {
unimplemented!()
}

0 comments on commit a8333cf

Please sign in to comment.