use std::convert::TryFrom;
use std::fmt;
use std::string::{String, ToString};
use wasmer_types::Type;
use crate::js::externals::function::Function;
use super::store::AsStoreRef;
#[derive(Clone, PartialEq)]
pub enum Value {
I32(i32),
I64(i64),
F32(f32),
F64(f64),
FuncRef(Option<Function>),
}
macro_rules! accessors {
($bind:ident $(($variant:ident($ty:ty) $get:ident $unwrap:ident $cvt:expr))*) => ($(
/// Attempt to access the underlying value of this `Value`, returning
/// `None` if it is not the correct type.
pub fn $get(&self) -> Option<$ty> {
if let Self::$variant($bind) = self {
Some($cvt)
} else {
None
}
}
pub fn $unwrap(&self) -> $ty {
self.$get().expect(concat!("expected ", stringify!($ty)))
}
)*)
}
impl Value {
pub fn null() -> Self {
Self::FuncRef(None)
}
pub fn ty(&self) -> Type {
match self {
Self::I32(_) => Type::I32,
Self::I64(_) => Type::I64,
Self::F32(_) => Type::F32,
Self::F64(_) => Type::F64,
Self::FuncRef(_) => Type::FuncRef,
}
}
pub fn as_raw(&self, store: &impl AsStoreRef) -> f64 {
match *self {
Self::I32(v) => v as f64,
Self::I64(v) => v as f64,
Self::F32(v) => v as f64,
Self::F64(v) => v,
Self::FuncRef(Some(ref f)) => f
.handle
.get(store.as_store_ref().objects())
.function
.as_f64()
.unwrap_or(0_f64),
Self::FuncRef(None) => 0_f64,
}
}
pub unsafe fn from_raw(_store: &impl AsStoreRef, ty: Type, raw: f64) -> Self {
match ty {
Type::I32 => Self::I32(raw as i32),
Type::I64 => Self::I64(raw as i64),
Type::F32 => Self::F32(raw as f32),
Type::F64 => Self::F64(raw),
Type::FuncRef => todo!(),
Type::V128 => todo!(),
Type::ExternRef => todo!(),
}
}
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
match self {
Self::I32(_)
| Self::I64(_)
| Self::F32(_)
| Self::F64(_)
| Self::FuncRef(None) => true,
Self::FuncRef(Some(f)) => f.is_from_store(store),
}
}
accessors! {
e
(I32(i32) i32 unwrap_i32 *e)
(I64(i64) i64 unwrap_i64 *e)
(F32(f32) f32 unwrap_f32 *e)
(F64(f64) f64 unwrap_f64 *e)
(FuncRef(&Option<Function>) funcref unwrap_funcref e)
}
}
impl fmt::Debug for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::I32(v) => write!(f, "I32({:?})", v),
Self::I64(v) => write!(f, "I64({:?})", v),
Self::F32(v) => write!(f, "F32({:?})", v),
Self::F64(v) => write!(f, "F64({:?})", v),
Self::FuncRef(None) => write!(f, "Null FuncRef"),
Self::FuncRef(Some(v)) => write!(f, "FuncRef({:?})", v),
}
}
}
impl ToString for Value {
fn to_string(&self) -> String {
match self {
Self::I32(v) => v.to_string(),
Self::I64(v) => v.to_string(),
Self::F32(v) => v.to_string(),
Self::F64(v) => v.to_string(),
Self::FuncRef(_) => "funcref".to_string(),
}
}
}
impl From<i32> for Value {
fn from(val: i32) -> Self {
Self::I32(val)
}
}
impl From<u32> for Value {
fn from(val: u32) -> Self {
Self::I32(val as i32)
}
}
impl From<i64> for Value {
fn from(val: i64) -> Self {
Self::I64(val)
}
}
impl From<u64> for Value {
fn from(val: u64) -> Self {
Self::I64(val as i64)
}
}
impl From<f32> for Value {
fn from(val: f32) -> Self {
Self::F32(val)
}
}
impl From<f64> for Value {
fn from(val: f64) -> Self {
Self::F64(val)
}
}
impl From<Function> for Value {
fn from(val: Function) -> Self {
Self::FuncRef(Some(val))
}
}
impl From<Option<Function>> for Value {
fn from(val: Option<Function>) -> Self {
Self::FuncRef(val)
}
}
const NOT_I32: &str = "Value is not of Wasm type i32";
const NOT_I64: &str = "Value is not of Wasm type i64";
const NOT_F32: &str = "Value is not of Wasm type f32";
const NOT_F64: &str = "Value is not of Wasm type f64";
const NOT_FUNCREF: &str = "Value is not of Wasm type funcref";
impl TryFrom<Value> for i32 {
type Error = &'static str;
fn try_from(value: Value) -> Result<Self, Self::Error> {
value.i32().ok_or(NOT_I32)
}
}
impl TryFrom<Value> for u32 {
type Error = &'static str;
fn try_from(value: Value) -> Result<Self, Self::Error> {
value.i32().ok_or(NOT_I32).map(|int| int as Self)
}
}
impl TryFrom<Value> for i64 {
type Error = &'static str;
fn try_from(value: Value) -> Result<Self, Self::Error> {
value.i64().ok_or(NOT_I64)
}
}
impl TryFrom<Value> for u64 {
type Error = &'static str;
fn try_from(value: Value) -> Result<Self, Self::Error> {
value.i64().ok_or(NOT_I64).map(|int| int as Self)
}
}
impl TryFrom<Value> for f32 {
type Error = &'static str;
fn try_from(value: Value) -> Result<Self, Self::Error> {
value.f32().ok_or(NOT_F32)
}
}
impl TryFrom<Value> for f64 {
type Error = &'static str;
fn try_from(value: Value) -> Result<Self, Self::Error> {
value.f64().ok_or(NOT_F64)
}
}
impl TryFrom<Value> for Option<Function> {
type Error = &'static str;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::FuncRef(f) => Ok(f),
_ => Err(NOT_FUNCREF),
}
}
}
#[cfg(tests)]
mod tests {
use super::*;
}