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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#![feature(associated_type_defaults)]
#![feature(extend_one)]

mod modules;
mod structures;

use std::{io::Write, path::Path, str::FromStr, sync::Arc};

use nyar_wasm::{
    CanonicalWasi, DependentGraph, Identifier, WasiFunction, WasiModule, WasiParameter, WasiRecordField, WasiRecordType,
    WasiResource, WasiType, WasiTypeReference, WasiVariantItem, WasiVariantType,
};

pub use crate::{
    modules::{ResolveContext, ValkyrieModule},
    structures::{ValkyrieField, ValkyrieMethod, ValkyrieStructure},
};

pub fn define_io_types() -> DependentGraph {
    let mut global = DependentGraph::default();
    let m_debugger = WasiModule::from_str("unstable:debugger/print").unwrap();
    let m_io_error = WasiModule::from_str("wasi:io/error@0.2.0").unwrap();
    let m_io_streams = WasiModule::from_str("wasi:io/streams@0.2.0").unwrap();
    {
        global += WasiResource::new(m_io_error.clone(), "error", "std::io::IoError");
        global += WasiResource::new(m_io_streams.clone(), "output-stream", "std::io::OutputStream");
        global += WasiResource::new(m_io_streams.clone(), "input-stream", "std::io::InputStream");
        let mut stream_error = WasiVariantType::new("std::io::StreamError");
        stream_error += WasiVariantItem::new("LastOperationFailed")
            .with_fields(WasiTypeReference::new(Identifier::from_str("std::io::IoError").unwrap()));
        stream_error += WasiVariantItem::new("Closed");
        global += stream_error;
    }
    {
        let mut point = WasiRecordType::new(Identifier::from_str("Point").unwrap());
        point += WasiRecordField::new(Arc::from("x"), WasiType::Float32);
        point += WasiRecordField::new(Arc::from("y"), WasiType::Float32);
        global += point;
        let mut printer = WasiFunction::external(m_debugger.clone(), "print-point", "test::print_point");
        printer.inputs.push(WasiParameter::new("value", WasiTypeReference::new(Identifier::from_str("Point").unwrap())));
        global += printer;
    }
    {
        let wasi_cli_get = WasiModule::from_str("wasi:cli/stdin@0.2.0").unwrap();
        let mut function = WasiFunction::external(wasi_cli_get.clone(), "get-stdin", "std::io::standard_input");
        function.output = Some(WasiTypeReference::owned(Identifier::from_str("std::io::InputStream").unwrap()).into());
        global += function;
    }
    {
        let wasi_cli_get = WasiModule::from_str("wasi:cli/stdout@0.2.0").unwrap();
        let mut function = WasiFunction::external(wasi_cli_get.clone(), "get-stdout", "std::io::standard_output");
        function.output = Some(WasiTypeReference::owned(Identifier::from_str("std::io::OutputStream").unwrap()).into());
        global += function;
    }
    {
        let wasi_cli_get = WasiModule::from_str("wasi:cli/stderr@0.2.0").unwrap();
        let mut function = WasiFunction::external(wasi_cli_get.clone(), "get-stderr", "std::io::standard_error");
        function.output = Some(WasiTypeReference::owned(Identifier::from_str("std::io::OutputStream").unwrap()).into());
        global += function;
    }
    {
        // let mut f1 = WasiExternalFunction::new(
        //     wasi_io_streams.clone(),
        //     "[method]output-stream.blocking-write-zeroes-and-flush",
        //     "std::io::OutputStream::write_zeros",
        // );
        // f1 += WasiParameter::new(
        //     "self",
        //     WasiType::TypeHandler { name: Identifier::from_str("std::io::OutputStream").unwrap(), own: false },
        // );
        // f1 += WasiParameter::new("len", WasiType::Integer64 { signed: false });
        // f1 += WasiType::Result {
        //     success: None,
        //     failure: Some(Box::new(WasiType::TypeAlias { name: Identifier::from_str("std::io::StreamError").unwrap() })),
        // };
        // global += f1;
    }
    {
        // let mut f1 = WasiExternalFunction::new(
        //     wasi_io_streams.clone(),
        //     "[method]output-stream.blocking-write-and-flush",
        //     "std::io::OutputStream::write",
        // );
        // f1 += WasiParameter::new(
        //     "self",
        //     WasiType::TypeHandler { name: Identifier::from_str("std::io::OutputStream").unwrap(), own: false },
        // );
        // f1 += WasiParameter::new("contents", WasiArrayType::new(WasiType::Integer8 { signed: false }));
        // f1 += WasiType::Result {
        //     success: None,
        //     failure: Some(Box::new(WasiType::TypeAlias { name: Identifier::from_str("std::io::StreamError").unwrap() })),
        // };
        // global += f1;
    }
    {
        let mut function = WasiFunction::external(m_debugger.clone(), "print-i32", "print_i32");
        function.inputs.push(WasiParameter::new("value", WasiType::Integer32 { signed: true }));
        global += function;
    }
    {
        let wasi_cli_get = WasiModule::from_str("unstable:debugger/print").unwrap();
        let mut function = WasiFunction::external(wasi_cli_get.clone(), "print-u32", "print_u32");
        function.inputs.push(WasiParameter::new("value", WasiType::Integer32 { signed: true }));
        global += function;
    }
    global
}