Renamings
This commit is contained in:
parent
5f49943cfb
commit
4c51136e7c
|
@ -23,18 +23,18 @@ pub fn codegen(ext_def: &ExtDefinition, to: &mut TokenStream) {
|
||||||
Trap, RuntimeValue, RuntimeArgs, Externals, ValueType, ModuleImportResolver,
|
Trap, RuntimeValue, RuntimeArgs, Externals, ValueType, ModuleImportResolver,
|
||||||
Signature, FuncRef, Error, FuncInstance,
|
Signature, FuncRef, Error, FuncInstance,
|
||||||
derive_support::{
|
derive_support::{
|
||||||
WasmResult,
|
IntoWasmResult,
|
||||||
ConvertibleToWasm,
|
IntoWasmValue,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn materialize_arg_ty<W: ConvertibleToWasm>(_w: Option<W>) -> ValueType {
|
fn materialize_arg_ty<W: IntoWasmValue>(_w: Option<W>) -> ValueType {
|
||||||
W::VALUE_TYPE
|
W::VALUE_TYPE
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn materialize_ret_type<W: WasmResult>(_w: Option<W>) -> Option<ValueType> {
|
fn materialize_ret_type<W: IntoWasmResult>(_w: Option<W>) -> Option<ValueType> {
|
||||||
W::VALUE_TYPE
|
W::VALUE_TYPE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ fn emit_dispatch_func_arm(func: &ExternalFunc) -> TokenStream {
|
||||||
#unmarshall_args
|
#unmarshall_args
|
||||||
};
|
};
|
||||||
let epilogue = quote_spanned! {return_ty_span=>
|
let epilogue = quote_spanned! {return_ty_span=>
|
||||||
WasmResult::to_wasm_result(r)
|
IntoWasmResult::into_wasm_result(r)
|
||||||
};
|
};
|
||||||
|
|
||||||
let call = {
|
let call = {
|
||||||
|
|
|
@ -23,8 +23,6 @@ pub struct ExternalFunc {
|
||||||
// TODO: Rename args to params
|
// TODO: Rename args to params
|
||||||
pub args: Vec<Param>,
|
pub args: Vec<Param>,
|
||||||
pub return_ty: Option<proc_macro2::Span>,
|
pub return_ty: Option<proc_macro2::Span>,
|
||||||
// TODO: remove
|
|
||||||
pub arity: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The core structure that contains the list of all functions
|
/// The core structure that contains the list of all functions
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::model::{self, ExtDefinition, ExternalFunc, Param};
|
||||||
use syn::{ItemImpl, ImplItem, ImplItemMethod, FnArg, ReturnType, Ident};
|
use syn::{ItemImpl, ImplItem, ImplItemMethod, FnArg, ReturnType, Ident};
|
||||||
use syn::spanned::Spanned;
|
use syn::spanned::Spanned;
|
||||||
|
|
||||||
/// Parse an incoming stream of tokens into a list of external functions.
|
/// Parse an incoming stream of tokens into externalities definition.
|
||||||
pub fn parse(input: proc_macro2::TokenStream) -> Result<ExtDefinition, ()> {
|
pub fn parse(input: proc_macro2::TokenStream) -> Result<ExtDefinition, ()> {
|
||||||
let item_impl = syn::parse2::<syn::ItemImpl>(input).map_err(|_| ())?;
|
let item_impl = syn::parse2::<syn::ItemImpl>(input).map_err(|_| ())?;
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@ pub fn parse(input: proc_macro2::TokenStream) -> Result<ExtDefinition, ()> {
|
||||||
name: sig.ident.to_string(),
|
name: sig.ident.to_string(),
|
||||||
args,
|
args,
|
||||||
return_ty,
|
return_ty,
|
||||||
arity: sig.decl.inputs.len() - 1, // self TODO: handle this properly
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
_ => {},
|
_ => {},
|
||||||
|
|
|
@ -1,53 +1,66 @@
|
||||||
use {ValueType, RuntimeValue, Trap};
|
use {ValueType, RuntimeValue, Trap};
|
||||||
|
use nan_preserving_float::{F32, F64};
|
||||||
|
|
||||||
pub trait ConvertibleToWasm {
|
/// A trait that represents a value that can be directly coerced to one of
|
||||||
|
/// wasm base value types.
|
||||||
|
pub trait IntoWasmValue {
|
||||||
const VALUE_TYPE: ValueType;
|
const VALUE_TYPE: ValueType;
|
||||||
type NativeType;
|
/// Perform the conversion.
|
||||||
fn to_runtime_value(self) -> RuntimeValue;
|
fn into_wasm_value(self) -> RuntimeValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConvertibleToWasm for i32 { type NativeType = i32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self) } }
|
macro_rules! impl_convertible_to_wasm {
|
||||||
impl ConvertibleToWasm for u32 { type NativeType = u32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as i32) } }
|
// TODO: Replace it to Kleene ? operator
|
||||||
impl ConvertibleToWasm for i64 { type NativeType = i64; const VALUE_TYPE: ValueType = ValueType::I64; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I64(self) } }
|
($ty:ty, $wasm_ty:ident $(, as $cast_to:ty)* ) => {
|
||||||
impl ConvertibleToWasm for u64 { type NativeType = u64; const VALUE_TYPE: ValueType = ValueType::I64; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I64(self as i64) } }
|
impl IntoWasmValue for $ty {
|
||||||
impl ConvertibleToWasm for isize { type NativeType = i32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as i32) } }
|
const VALUE_TYPE: ValueType = ValueType::$wasm_ty;
|
||||||
impl ConvertibleToWasm for usize { type NativeType = u32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as u32 as i32) } }
|
fn into_wasm_value(self) -> RuntimeValue {
|
||||||
impl<T> ConvertibleToWasm for *const T { type NativeType = u32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as isize as i32) } }
|
RuntimeValue::$wasm_ty(self $( as $cast_to)*)
|
||||||
impl<T> ConvertibleToWasm for *mut T { type NativeType = u32; const VALUE_TYPE: ValueType = ValueType::I32; fn to_runtime_value(self) -> RuntimeValue { RuntimeValue::I32(self as isize as i32) } }
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub trait WasmResult {
|
impl_convertible_to_wasm!(i32, I32);
|
||||||
|
impl_convertible_to_wasm!(u32, I32, as i32);
|
||||||
|
impl_convertible_to_wasm!(i64, I64);
|
||||||
|
impl_convertible_to_wasm!(u64, I64, as i64);
|
||||||
|
impl_convertible_to_wasm!(F32, F32);
|
||||||
|
impl_convertible_to_wasm!(F64, F64);
|
||||||
|
|
||||||
|
pub trait IntoWasmResult {
|
||||||
const VALUE_TYPE: Option<ValueType>;
|
const VALUE_TYPE: Option<ValueType>;
|
||||||
fn to_wasm_result(self) -> Result<Option<RuntimeValue>, Trap>;
|
fn into_wasm_result(self) -> Result<Option<RuntimeValue>, Trap>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WasmResult for () {
|
impl IntoWasmResult for () {
|
||||||
const VALUE_TYPE: Option<ValueType> = None;
|
const VALUE_TYPE: Option<ValueType> = None;
|
||||||
fn to_wasm_result(self) -> Result<Option<RuntimeValue>, Trap> {
|
fn into_wasm_result(self) -> Result<Option<RuntimeValue>, Trap> {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: ConvertibleToWasm, E: Into<Trap>> WasmResult for Result<R, E> {
|
impl<R: IntoWasmValue, E: Into<Trap>> IntoWasmResult for Result<R, E> {
|
||||||
const VALUE_TYPE: Option<ValueType> = Some(R::VALUE_TYPE);
|
const VALUE_TYPE: Option<ValueType> = Some(R::VALUE_TYPE);
|
||||||
fn to_wasm_result(self) -> Result<Option<RuntimeValue>, Trap> {
|
fn into_wasm_result(self) -> Result<Option<RuntimeValue>, Trap> {
|
||||||
self
|
self
|
||||||
.map(|v| Some(v.to_runtime_value()))
|
.map(|v| Some(v.into_wasm_value()))
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Into<Trap>> WasmResult for Result<(), E> {
|
impl<E: Into<Trap>> IntoWasmResult for Result<(), E> {
|
||||||
const VALUE_TYPE: Option<ValueType> = None;
|
const VALUE_TYPE: Option<ValueType> = None;
|
||||||
fn to_wasm_result(self) -> Result<Option<RuntimeValue>, Trap> {
|
fn into_wasm_result(self) -> Result<Option<RuntimeValue>, Trap> {
|
||||||
self
|
self
|
||||||
.map(|_| None)
|
.map(|_| None)
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: ConvertibleToWasm> WasmResult for R {
|
impl<R: IntoWasmValue> IntoWasmResult for R {
|
||||||
const VALUE_TYPE: Option<ValueType> = Some(R::VALUE_TYPE);
|
const VALUE_TYPE: Option<ValueType> = Some(R::VALUE_TYPE);
|
||||||
fn to_wasm_result(self) -> Result<Option<RuntimeValue>, Trap> {
|
fn into_wasm_result(self) -> Result<Option<RuntimeValue>, Trap> {
|
||||||
Ok(Some(self.to_runtime_value()))
|
Ok(Some(self.into_wasm_value()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue