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
use crate::*;
use log::info;
use std::{thread, time};
use wasmer_runtime::{Array, Ctx, WasmPtr};

pub fn exit(ctx: &mut Ctx, code: i32) -> Result<(), ExitCode> {
    let (_, env) = Process::get_memory_and_environment(ctx, 0);
    env.log_call("runtime_exit".to_string());
    info!("{}: exiting with {}", env.name, code);
    Err(ExitCode { code: code })
}

pub const NAME: &'static str = "pa'i";

pub fn name(
    ctx: &mut Ctx,
    ptr: WasmPtr<u8, Array>,
    len: u32,
) -> Result<u32, wasmer_runtime::error::RuntimeError> {
    let (memory, env) = Process::get_memory_and_environment(ctx, 0);
    if len < NAME.len() as u32 {
        return Ok(NAME.len() as u32);
    }

    unsafe {
        let memory_writer = ptr
            .deref_mut(memory, 0, NAME.len() as u32)
            .expect("pointer deference to work");
        for (i, b) in NAME.bytes().enumerate() {
            memory_writer[i].set(b);
        }

        assert_eq!(
            ptr.get_utf8_string(memory, NAME.len() as u32).unwrap(),
            NAME
        );
    }

    env.log_call("runtime_name".to_string());
    Ok(NAME.len() as u32)
}

pub const CWA_VERSION_MAJOR: u32 = 0;
pub const CWA_VERSION_MINOR: u32 = 2;

pub fn spec_major(ctx: &mut Ctx) -> u32 {
    let (_, env) = Process::get_memory_and_environment(ctx, 0);
    env.log_call("runtime_spec_major".to_string());

    CWA_VERSION_MAJOR
}
pub fn spec_minor(ctx: &mut Ctx) -> u32 {
    let (_, env) = Process::get_memory_and_environment(ctx, 0);
    env.log_call("runtime_spec_major".to_string());

    CWA_VERSION_MINOR
}

pub fn sleep(_: &mut Ctx, len: i32) {
    let amt = time::Duration::from_millis(len as u64);
    thread::sleep(amt);
}