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
use crate::{
    helpers::{IntoWasm, WasmName},
    types::WasmType,
    ArrayType, StructureItem, WasmSymbol, WasmVariable,
};
use indexmap::IndexMap;
use nyar_error::FileSpan;
use wast::{
    core::{Expression, Global, GlobalKind, Instruction},
    token::{Float32, Float64, Span},
};
pub mod data;
pub mod global;
pub mod variable;
use crate::{data::DataItem, helpers::IndexedIterator};
mod convert;
use wast::core::GlobalType;

impl<'a, 'i> IntoWasm<'a, Instruction<'i>> for WasmValue
where
    'a: 'i,
{
    fn as_wast(&'a self) -> Instruction<'i> {
        match self {
            Self::Bool(v) => match v {
                true => Instruction::I32Const(1),
                false => Instruction::I32Const(0),
            },
            Self::U32(v) => Instruction::I32Const(*v as i32),
            Self::I32(v) => Instruction::I32Const(*v),
            Self::I64(v) => Instruction::I64Const(*v),
            Self::F32(v) => Instruction::F32Const(Float32 { bits: u32::from_le_bytes(v.to_le_bytes()) }),
            Self::F64(v) => Instruction::F64Const(Float64 { bits: u64::from_le_bytes(v.to_le_bytes()) }),
            Self::Function(_) => {
                todo!()
            }
            Self::Structure(o) => Instruction::StructNewDefault(o.symbol.as_index()),
            Self::Array(o) => Instruction::ArrayNewDefault(o.symbol.as_index()),
            Self::Any => {
                todo!()
            }
        }
    }
}

#[derive(Clone, Debug)]
pub enum WasmValue {
    Bool(bool),
    U32(u32),
    I32(i32),
    I64(i64),
    F32(f32),
    F64(f64),
    Function(WasmSymbol),
    Structure(StructureItem),
    Array(Box<ArrayType>),
    Any,
}