1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![allow(non_camel_case_types)]
use crate::ctx::WasiCtx;
use crate::memory::*;
use crate::wasm32;
use log::trace;

use wasi_common_cbindgen::wasi_common_cbindgen;

#[wasi_common_cbindgen]
pub unsafe fn proc_exit(rval: wasm32::__wasi_exitcode_t) {
    trace!("proc_exit(rval={:?})", rval);
    // TODO: Rather than call std::process::exit here, we should trigger a
    // stack unwind similar to a trap.
    std::process::exit(dec_exitcode(rval) as i32);
}

#[wasi_common_cbindgen]
pub unsafe fn proc_raise(
    _wasi_ctx: &WasiCtx,
    _memory: &mut [u8],
    _sig: wasm32::__wasi_signal_t,
) -> wasm32::__wasi_errno_t {
    unimplemented!("proc_raise")
}

hostcalls! {
    pub unsafe fn args_get(
        wasi_ctx: &WasiCtx,
        memory: &mut [u8],
        argv_ptr: wasm32::uintptr_t,
        argv_buf: wasm32::uintptr_t,
    ) -> wasm32::__wasi_errno_t;

    pub unsafe fn args_sizes_get(
        wasi_ctx: &WasiCtx,
        memory: &mut [u8],
        argc_ptr: wasm32::uintptr_t,
        argv_buf_size_ptr: wasm32::uintptr_t,
    ) -> wasm32::__wasi_errno_t;

    pub unsafe fn environ_get(
        wasi_ctx: &WasiCtx,
        memory: &mut [u8],
        environ_ptr: wasm32::uintptr_t,
        environ_buf: wasm32::uintptr_t,
    ) -> wasm32::__wasi_errno_t;

    pub unsafe fn environ_sizes_get(
        wasi_ctx: &WasiCtx,
        memory: &mut [u8],
        environ_count_ptr: wasm32::uintptr_t,
        environ_size_ptr: wasm32::uintptr_t,
    ) -> wasm32::__wasi_errno_t;

    pub unsafe fn random_get(
        memory: &mut [u8],
        buf_ptr: wasm32::uintptr_t,
        buf_len: wasm32::size_t,
    ) -> wasm32::__wasi_errno_t;

    pub unsafe fn clock_res_get(
        memory: &mut [u8],
        clock_id: wasm32::__wasi_clockid_t,
        resolution_ptr: wasm32::uintptr_t,
    ) -> wasm32::__wasi_errno_t;

    pub unsafe fn clock_time_get(
        memory: &mut [u8],
        clock_id: wasm32::__wasi_clockid_t,
        precision: wasm32::__wasi_timestamp_t,
        time_ptr: wasm32::uintptr_t,
    ) -> wasm32::__wasi_errno_t;

    pub unsafe fn poll_oneoff(
        memory: &mut [u8],
        input: wasm32::uintptr_t,
        output: wasm32::uintptr_t,
        nsubscriptions: wasm32::size_t,
        nevents: wasm32::uintptr_t,
    ) -> wasm32::__wasi_errno_t;

    pub unsafe fn sched_yield() -> wasm32::__wasi_errno_t;
}