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
use std::fmt;

#[derive(Debug, Clone, Copy)]
pub enum ValueType {
  I32,
  I64,
  F32,
  F64,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Value {
  I32(i32),
  I64(i64),
  F32(f32),
  F64(f64),
}

impl fmt::Display for Value {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Value::I32(v) => write!(f, "{}", v),
      Value::I64(v) => write!(f, "{}", v),
      Value::F32(v) => write!(f, "{}", v),
      Value::F64(v) => write!(f, "{}", v),
    }
  }
}

impl From<bwasm::ValueType> for ValueType {
  fn from(val_type: bwasm::ValueType) -> Self {
    match val_type {
      bwasm::ValueType::I32 => ValueType::I32,
      bwasm::ValueType::I64 => ValueType::I64,
      bwasm::ValueType::F32 => ValueType::F32,
      bwasm::ValueType::F64 => ValueType::F64,
    }
  }
}

impl From<&bwasm::ValueType> for ValueType {
  fn from(val_type: &bwasm::ValueType) -> Self {
    ValueType::from(*val_type)
  }
}

impl ValueType {
  pub fn from_slice(val_types: &[bwasm::ValueType]) -> Vec<ValueType> {
    val_types.iter().map(ValueType::from).collect()
  }
}

pub type RetValue = Option<Value>;

pub type ConstI32 = i32;
pub type ConstI64 = i64;
pub type ConstF32 = f32;
pub type ConstF64 = f64;

pub type TypeIdx = u32;
pub type FuncIdx = u32;
pub type TableIdx = u32;
pub type MemIdx = u32;
pub type GlobalIdx = u32;
pub type LocalIdx = u32;
pub type LabelIdx = u32;

pub type ModuleInstanceAddr = u32;
pub type FuncAddr = u32;
pub type TableAddr = u32;
pub type MemAddr = u32;
pub type GlobalAddr = u32;

#[derive(Debug, Clone, Default)]
pub struct FunctionType {
  pub params: Vec<ValueType>,
  pub ret_type: Option<ValueType>,
}

impl FunctionType {
  pub fn new() -> FunctionType {
    Default::default()
  }

  pub fn param_count(&self) -> usize {
    self.params.len()
  }
}

impl From<bwasm::FunctionType> for FunctionType {
  fn from(func_type: bwasm::FunctionType) -> Self {
    FunctionType::from(&func_type)
  }
}

impl From<&bwasm::FunctionType> for FunctionType {
  fn from(func_type: &bwasm::FunctionType) -> Self {
    FunctionType {
      params: ValueType::from_slice(func_type.params()),
      ret_type: func_type.return_type().map(ValueType::from),
    }
  }
}