This commit is contained in:
Sergey Pepyakin 2019-01-25 10:43:03 +01:00
parent 4c51136e7c
commit af19c66589
5 changed files with 59 additions and 51 deletions

View File

@ -2,7 +2,6 @@ use crate::model::{ExtDefinition, ExternalFunc};
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::{quote, quote_spanned, ToTokens};
pub fn codegen(ext_def: &ExtDefinition, to: &mut TokenStream) {
let mut externals = TokenStream::new();
let mut module_resolver = TokenStream::new();
@ -41,7 +40,8 @@ pub fn codegen(ext_def: &ExtDefinition, to: &mut TokenStream) {
#externals
#module_resolver
};
}).to_tokens(to);
})
.to_tokens(to);
}
fn emit_dispatch_func_arm(func: &ExternalFunc) -> TokenStream {
@ -58,7 +58,8 @@ fn emit_dispatch_func_arm(func: &ExternalFunc) -> TokenStream {
args.next()
.and_then(|rt_val| rt_val.try_into())
.unwrap();
}).to_tokens(&mut unmarshall_args);
})
.to_tokens(&mut unmarshall_args);
}
let prologue = quote! {
@ -109,7 +110,8 @@ fn derive_externals(ext_def: &ExtDefinition, to: &mut TokenStream) {
// ...
}
}).to_tokens(to);
})
.to_tokens(to);
}
fn emit_resolve_func_arm(func: &ExternalFunc) -> TokenStream {
@ -129,18 +131,26 @@ fn emit_resolve_func_arm(func: &ExternalFunc) -> TokenStream {
}
};
let init = func.args.iter().map(|param| {
let init = func
.args
.iter()
.map(|param| {
let ident = &param.ident;
quote! {
let #ident = None;
}
}).collect::<Vec<_>>();
})
.collect::<Vec<_>>();
let params_materialized_tys = func.args.iter().map(|param| {
let params_materialized_tys = func
.args
.iter()
.map(|param| {
let ident = &param.ident;
let span = param.ident.span();
quote_spanned! {span=> materialize_arg_ty(#ident) }
}).collect::<Vec<_>>();
})
.collect::<Vec<_>>();
let materialized_return_ty = quote_spanned! { return_ty_span=>
materialize_ret_type(return_val)

View File

@ -1,10 +1,10 @@
#![recursion_limit="128"]
#![recursion_limit = "128"]
extern crate proc_macro;
mod codegen;
mod model;
mod parser;
mod codegen;
use proc_macro::TokenStream;

View File

@ -1,6 +1,6 @@
use crate::model::{self, ExtDefinition, ExternalFunc, Param};
use syn::{ItemImpl, ImplItem, ImplItemMethod, FnArg, ReturnType, Ident};
use syn::spanned::Spanned;
use syn::{FnArg, Ident, ImplItem, ImplItemMethod, ItemImpl, ReturnType};
/// Parse an incoming stream of tokens into externalities definition.
pub fn parse(input: proc_macro2::TokenStream) -> Result<ExtDefinition, ()> {
@ -10,20 +10,22 @@ pub fn parse(input: proc_macro2::TokenStream) -> Result<ExtDefinition, ()> {
for item in item_impl.items {
match item {
ImplItem::Method(ImplItemMethod {
sig,
..
}) => {
ImplItem::Method(ImplItemMethod { sig, .. }) => {
let index = funcs.len() as u32;
// self TODO: handle this properly
let args = sig.decl.inputs.iter().skip(1).enumerate().map(|(idx, input)| {
let args = sig
.decl
.inputs
.iter()
.skip(1)
.enumerate()
.map(|(idx, input)| {
let param_name = format!("arg{}", idx);
let ident = Ident::new(&param_name, input.span());
Param {
ident,
}
}).collect::<Vec<_>>();
Param { ident }
})
.collect::<Vec<_>>();
let return_ty = match sig.decl.output {
ReturnType::Default => None,
@ -36,8 +38,8 @@ pub fn parse(input: proc_macro2::TokenStream) -> Result<ExtDefinition, ()> {
args,
return_ty,
});
},
_ => {},
}
_ => {}
}
}

View File

@ -1,5 +1,5 @@
use {ValueType, RuntimeValue, Trap};
use nan_preserving_float::{F32, F64};
use {RuntimeValue, Trap, ValueType};
/// A trait that represents a value that can be directly coerced to one of
/// wasm base value types.
@ -43,18 +43,14 @@ impl IntoWasmResult for () {
impl<R: IntoWasmValue, E: Into<Trap>> IntoWasmResult for Result<R, E> {
const VALUE_TYPE: Option<ValueType> = Some(R::VALUE_TYPE);
fn into_wasm_result(self) -> Result<Option<RuntimeValue>, Trap> {
self
.map(|v| Some(v.into_wasm_value()))
.map_err(Into::into)
self.map(|v| Some(v.into_wasm_value())).map_err(Into::into)
}
}
impl<E: Into<Trap>> IntoWasmResult for Result<(), E> {
const VALUE_TYPE: Option<ValueType> = None;
fn into_wasm_result(self) -> Result<Option<RuntimeValue>, Trap> {
self
.map(|_| None)
.map_err(Into::into)
self.map(|_| None).map_err(Into::into)
}
}

View File

@ -1,9 +1,9 @@
extern crate wasmi_derive;
extern crate wasmi;
extern crate wasmi_derive;
use wasmi_derive::derive_externals;
use wasmi::HostError;
use std::fmt;
use wasmi::HostError;
use wasmi_derive::derive_externals;
#[derive(Debug)]
struct NoInfoError;