Upgrade to new versions of proc-macro2

Gonna get some nice spans back!
This commit is contained in:
Alex Crichton 2018-05-21 07:29:34 -07:00
parent 627ca1d638
commit e76f5537e0
5 changed files with 212 additions and 190 deletions

View File

@ -14,8 +14,8 @@ Backend code generation of the wasm-bindgen tool
spans = []
[dependencies]
quote = '0.5'
proc-macro2 = "0.3"
quote = '0.6'
proc-macro2 = "0.4"
wasm-bindgen-shared = { path = "../shared", version = "=0.2.10" }
syn = { version = '0.13', features = ['full', 'visit-mut'] }
syn = { version = '0.14', features = ['full', 'visit-mut'] }
serde_json = "1.0"

View File

@ -1,5 +1,5 @@
use proc_macro2::TokenTree;
use quote::{ToTokens, Tokens};
use proc_macro2::{TokenTree, TokenStream, Ident, Span};
use quote::ToTokens;
use shared;
use syn;
@ -12,7 +12,7 @@ pub struct Program {
}
pub struct Export {
pub class: Option<syn::Ident>,
pub class: Option<Ident>,
pub method: bool,
pub mutable: bool,
pub constructor: Option<String>,
@ -22,7 +22,7 @@ pub struct Export {
pub struct Import {
pub module: Option<String>,
pub version: Option<String>,
pub js_namespace: Option<syn::Ident>,
pub js_namespace: Option<Ident>,
pub kind: ImportKind,
}
@ -34,9 +34,9 @@ pub enum ImportKind {
pub struct ImportFunction {
pub function: Function,
pub rust_name: syn::Ident,
pub rust_name: Ident,
pub kind: ImportFunctionKind,
pub shim: syn::Ident,
pub shim: Ident,
}
pub enum ImportFunctionKind {
@ -48,18 +48,18 @@ pub enum ImportFunctionKind {
pub struct ImportStatic {
pub vis: syn::Visibility,
pub ty: syn::Type,
pub shim: syn::Ident,
pub rust_name: syn::Ident,
pub js_name: syn::Ident,
pub shim: Ident,
pub rust_name: Ident,
pub js_name: Ident,
}
pub struct ImportType {
pub vis: syn::Visibility,
pub name: syn::Ident,
pub name: Ident,
}
pub struct Function {
pub name: syn::Ident,
pub name: Ident,
pub arguments: Vec<syn::Type>,
pub ret: Option<syn::Type>,
pub opts: BindgenAttrs,
@ -69,26 +69,26 @@ pub struct Function {
}
pub struct Struct {
pub name: syn::Ident,
pub name: Ident,
pub fields: Vec<StructField>,
}
pub struct StructField {
pub opts: BindgenAttrs,
pub name: syn::Ident,
pub struct_name: syn::Ident,
pub name: Ident,
pub struct_name: Ident,
pub ty: syn::Type,
pub getter: syn::Ident,
pub setter: syn::Ident,
pub getter: Ident,
pub setter: Ident,
}
pub struct Enum {
pub name: syn::Ident,
pub name: Ident,
pub variants: Vec<Variant>,
}
pub struct Variant {
pub name: syn::Ident,
pub name: Ident,
pub value: u32,
}
@ -108,7 +108,7 @@ pub enum TypeLocation {
}
impl Program {
pub fn push_item(&mut self, item: syn::Item, opts: Option<BindgenAttrs>, tokens: &mut Tokens) {
pub fn push_item(&mut self, item: syn::Item, opts: Option<BindgenAttrs>, tokens: &mut TokenStream) {
match item {
syn::Item::Fn(mut f) => {
let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut f.attrs));
@ -183,11 +183,11 @@ impl Program {
_ => panic!("unsupported self type in impl"),
};
for item in item.items.iter_mut() {
self.push_impl_item(name, item);
self.push_impl_item(&name, item);
}
}
fn push_impl_item(&mut self, class: syn::Ident, item: &mut syn::ImplItem) {
fn push_impl_item(&mut self, class: &Ident, item: &mut syn::ImplItem) {
replace_self(class, item);
let method = match item {
syn::ImplItem::Const(_) => panic!("const definitions aren't supported"),
@ -219,7 +219,7 @@ impl Program {
};
let (function, mutable) = Function::from_decl(
method.sig.ident,
&method.sig.ident,
Box::new(method.sig.decl.clone()),
method.attrs.clone(),
opts,
@ -228,7 +228,7 @@ impl Program {
);
self.exports.push(Export {
class: Some(class),
class: Some(class.clone()),
method: mutable.is_some(),
mutable: mutable.unwrap_or(false),
constructor,
@ -268,7 +268,7 @@ impl Program {
};
Variant {
name: v.ident,
name: v.ident.clone(),
value,
}
})
@ -297,7 +297,7 @@ impl Program {
};
let module = item_opts.module().or(opts.module()).map(|s| s.to_string());
let version = item_opts.version().or(opts.version()).map(|s| s.to_string());
let js_namespace = item_opts.js_namespace().or(opts.js_namespace());
let js_namespace = item_opts.js_namespace().or(opts.js_namespace()).cloned();
let mut kind = match item {
syn::ForeignItem::Fn(f) => self.push_foreign_fn(f, item_opts),
syn::ForeignItem::Type(t) => self.push_foreign_ty(t),
@ -315,9 +315,9 @@ impl Program {
}
pub fn push_foreign_fn(&mut self, f: syn::ForeignItemFn, opts: BindgenAttrs) -> ImportKind {
let js_name = opts.js_name().unwrap_or(f.ident);
let js_name = opts.js_name().unwrap_or(&f.ident).clone();
let mut wasm = Function::from_decl(
js_name,
&js_name,
f.decl,
f.attrs,
opts,
@ -359,7 +359,7 @@ impl Program {
.expect("first argument of method must be a bare type");
ImportFunctionKind::Method {
class: class_name.as_ref().to_string(),
class: class_name.to_string(),
ty: class.clone(),
}
} else if wasm.opts.constructor() {
@ -378,7 +378,7 @@ impl Program {
.expect("first argument of method must be a bare type");
ImportFunctionKind::JsConstructor {
class: class_name.as_ref().to_string(),
class: class_name.to_string(),
ty: class.clone(),
}
} else {
@ -396,8 +396,8 @@ impl Program {
ImportKind::Function(ImportFunction {
function: wasm,
kind,
rust_name: f.ident,
shim: shim.into(),
rust_name: f.ident.clone(),
shim: Ident::new(&shim, Span::call_site()),
})
}
@ -416,14 +416,14 @@ impl Program {
if f.mutability.is_some() {
panic!("cannot import mutable globals yet")
}
let js_name = opts.js_name().unwrap_or(f.ident);
let js_name = opts.js_name().unwrap_or(&f.ident);
let shim = format!("__wbg_static_accessor_{}_{}", js_name, f.ident);
ImportKind::Static(ImportStatic {
ty: *f.ty,
vis: f.vis,
rust_name: f.ident,
js_name,
shim: shim.into(),
rust_name: f.ident.clone(),
js_name: js_name.clone(),
shim: Ident::new(&shim, Span::call_site()),
})
}
@ -453,7 +453,7 @@ impl Function {
}
Function::from_decl(
input.ident,
&input.ident,
input.decl,
input.attrs,
opts,
@ -463,7 +463,7 @@ impl Function {
}
pub fn from_decl(
name: syn::Ident,
name: &Ident,
mut decl: Box<syn::FnDecl>,
attrs: Vec<syn::Attribute>,
opts: BindgenAttrs,
@ -504,7 +504,7 @@ impl Function {
(
Function {
name,
name: name.clone(),
arguments,
ret,
opts,
@ -518,12 +518,12 @@ impl Function {
fn shared(&self) -> shared::Function {
shared::Function {
name: self.name.as_ref().to_string(),
name: self.name.to_string(),
}
}
}
pub fn extract_path_ident(path: &syn::Path) -> Option<syn::Ident> {
pub fn extract_path_ident(path: &syn::Path) -> Option<Ident> {
if path.leading_colon.is_some() {
return None;
}
@ -534,33 +534,34 @@ pub fn extract_path_ident(path: &syn::Path) -> Option<syn::Ident> {
syn::PathArguments::None => {}
_ => return None,
}
path.segments.first().map(|v| v.value().ident)
path.segments.first().map(|v| v.value().ident.clone())
}
impl Export {
pub fn rust_symbol(&self) -> syn::Ident {
pub fn rust_symbol(&self) -> Ident {
let mut generated_name = format!("__wasm_bindgen_generated");
if let Some(class) = self.class {
if let Some(class) = &self.class {
generated_name.push_str("_");
generated_name.push_str(class.as_ref());
generated_name.push_str(&class.to_string());
}
generated_name.push_str("_");
generated_name.push_str(self.function.name.as_ref());
syn::Ident::from(generated_name)
generated_name.push_str(&self.function.name.to_string());
Ident::new(&generated_name, Span::call_site())
}
pub fn export_name(&self) -> String {
match self.class {
let fn_name = self.function.name.to_string();
match &self.class {
Some(class) => {
shared::struct_function_export_name(class.as_ref(), self.function.name.as_ref())
shared::struct_function_export_name(&class.to_string(), &fn_name)
}
None => shared::free_function_export_name(self.function.name.as_ref()),
None => shared::free_function_export_name(&fn_name),
}
}
fn shared(&self) -> shared::Export {
shared::Export {
class: self.class.map(|s| s.as_ref().to_string()),
class: self.class.as_ref().map(|s| s.to_string()),
method: self.method,
constructor: self.constructor.clone(),
function: self.function.shared(),
@ -571,7 +572,7 @@ impl Export {
impl Enum {
fn shared(&self) -> shared::Enum {
shared::Enum {
name: self.name.as_ref().to_string(),
name: self.name.to_string(),
variants: self.variants.iter().map(|v| v.shared()).collect(),
}
}
@ -580,7 +581,7 @@ impl Enum {
impl Variant {
fn shared(&self) -> shared::EnumVariant {
shared::EnumVariant {
name: self.name.as_ref().to_string(),
name: self.name.to_string(),
value: self.value,
}
}
@ -611,7 +612,7 @@ impl Import {
shared::Import {
module: self.module.clone(),
version: self.version.clone(),
js_namespace: self.js_namespace.map(|s| s.as_ref().to_string()),
js_namespace: self.js_namespace.as_ref().map(|s| s.to_string()),
kind: self.kind.shared(),
}
}
@ -637,11 +638,11 @@ impl ImportKind {
impl ImportFunction {
pub fn infer_getter_property(&self) -> String {
self.function.name.as_ref().to_string()
self.function.name.to_string()
}
pub fn infer_setter_property(&self) -> String {
let name = self.function.name.as_ref();
let name = self.function.name.to_string();
assert!(name.starts_with("set_"), "setters must start with `set_`");
name[4..].to_string()
}
@ -673,7 +674,7 @@ impl ImportFunction {
setter = Some(s.unwrap_or_else(|| self.infer_setter_property()));
}
shared::ImportFunction {
shim: self.shim.as_ref().to_string(),
shim: self.shim.to_string(),
catch: self.function.opts.catch(),
method,
js_new,
@ -689,8 +690,8 @@ impl ImportFunction {
impl ImportStatic {
fn shared(&self) -> shared::ImportStatic {
shared::ImportStatic {
name: self.js_name.as_ref().to_string(),
shim: self.shim.as_ref().to_string(),
name: self.js_name.to_string(),
shim: self.shim.to_string(),
}
}
}
@ -710,32 +711,34 @@ impl Struct {
syn::Visibility::Public(..) => {}
_ => continue,
}
let name = match field.ident {
let name = match &field.ident {
Some(n) => n,
None => continue,
};
let getter = shared::struct_field_get(s.ident.as_ref(), name.as_ref());
let setter = shared::struct_field_set(s.ident.as_ref(), name.as_ref());
let ident = s.ident.to_string();
let name_str = name.to_string();
let getter = shared::struct_field_get(&ident, &name_str);
let setter = shared::struct_field_set(&ident, &name_str);
let opts = BindgenAttrs::find(&mut field.attrs);
fields.push(StructField {
opts,
name,
struct_name: s.ident,
name: name.clone(),
struct_name: s.ident.clone(),
ty: field.ty.clone(),
getter: getter.into(),
setter: setter.into(),
getter: Ident::new(&getter, Span::call_site()),
setter: Ident::new(&setter, Span::call_site()),
});
}
}
Struct {
name: s.ident,
name: s.ident.clone(),
fields,
}
}
fn shared(&self) -> shared::Struct {
shared::Struct {
name: self.name.as_ref().to_string(),
name: self.name.to_string(),
fields: self.fields.iter().map(|s| s.shared()).collect(),
}
}
@ -744,7 +747,7 @@ impl Struct {
impl StructField {
fn shared(&self) -> shared::StructField {
shared::StructField {
name: self.name.as_ref().to_string(),
name: self.name.to_string(),
readonly: self.opts.readonly(),
}
}
@ -781,9 +784,11 @@ impl BindgenAttrs {
fn module(&self) -> Option<&str> {
self.attrs
.iter()
.filter_map(|a| match *a {
BindgenAttr::Module(ref s) => Some(&s[..]),
_ => None,
.filter_map(|a| {
match a {
BindgenAttr::Module(s) => Some(&s[..]),
_ => None,
}
})
.next()
}
@ -791,84 +796,104 @@ impl BindgenAttrs {
fn version(&self) -> Option<&str> {
self.attrs
.iter()
.filter_map(|a| match *a {
BindgenAttr::Version(ref s) => Some(&s[..]),
_ => None,
.filter_map(|a| {
match a {
BindgenAttr::Version(s) => Some(&s[..]),
_ => None,
}
})
.next()
}
pub fn catch(&self) -> bool {
self.attrs.iter().any(|a| match *a {
BindgenAttr::Catch => true,
_ => false,
self.attrs.iter().any(|a| {
match a {
BindgenAttr::Catch => true,
_ => false,
}
})
}
fn constructor(&self) -> bool {
self.attrs.iter().any(|a| match *a {
BindgenAttr::Constructor => true,
_ => false,
self.attrs.iter().any(|a| {
match a {
BindgenAttr::Constructor => true,
_ => false,
}
})
}
fn method(&self) -> bool {
self.attrs.iter().any(|a| match *a {
BindgenAttr::Method => true,
_ => false,
self.attrs.iter().any(|a| {
match a {
BindgenAttr::Method => true,
_ => false,
}
})
}
fn js_namespace(&self) -> Option<syn::Ident> {
fn js_namespace(&self) -> Option<&Ident> {
self.attrs
.iter()
.filter_map(|a| match *a {
BindgenAttr::JsNamespace(s) => Some(s),
_ => None,
.filter_map(|a| {
match a {
BindgenAttr::JsNamespace(s) => Some(s),
_ => None,
}
})
.next()
}
pub fn getter(&self) -> Option<Option<syn::Ident>> {
pub fn getter(&self) -> Option<Option<&Ident>> {
self.attrs
.iter()
.filter_map(|a| match *a {
BindgenAttr::Getter(s) => Some(s),
_ => None,
.filter_map(|a| {
match a {
BindgenAttr::Getter(s) => Some(s.as_ref()),
_ => None,
}
})
.next()
}
pub fn setter(&self) -> Option<Option<syn::Ident>> {
pub fn setter(&self) -> Option<Option<&Ident>> {
self.attrs
.iter()
.filter_map(|a| match *a {
BindgenAttr::Setter(s) => Some(s),
_ => None,
.filter_map(|a| {
match a {
BindgenAttr::Setter(s) => Some(s.as_ref()),
_ => None,
}
})
.next()
}
pub fn structural(&self) -> bool {
self.attrs.iter().any(|a| match *a {
BindgenAttr::Structural => true,
_ => false,
self.attrs.iter().any(|a| {
match *a {
BindgenAttr::Structural => true,
_ => false,
}
})
}
pub fn readonly(&self) -> bool {
self.attrs.iter().any(|a| match *a {
BindgenAttr::Readonly => true,
_ => false,
self.attrs.iter().any(|a| {
match *a {
BindgenAttr::Readonly => true,
_ => false,
}
})
}
pub fn js_name(&self) -> Option<syn::Ident> {
pub fn js_name(&self) -> Option<&Ident> {
self.attrs
.iter()
.filter_map(|a| match *a {
BindgenAttr::JsName(s) => Some(s),
_ => None,
.filter_map(|a| {
match a {
BindgenAttr::JsName(s) => Some(s),
_ => None,
}
})
.next()
}
@ -894,14 +919,14 @@ enum BindgenAttr {
Catch,
Constructor,
Method,
JsNamespace(syn::Ident),
JsNamespace(Ident),
Module(String),
Version(String),
Getter(Option<syn::Ident>),
Setter(Option<syn::Ident>),
Getter(Option<Ident>),
Setter(Option<Ident>),
Structural,
Readonly,
JsName(syn::Ident),
JsName(Ident),
}
impl syn::synom::Synom for BindgenAttr {
@ -995,8 +1020,8 @@ fn extract_first_ty_param(ty: Option<&syn::Type>) -> Option<Option<syn::Type>> {
}
fn term<'a>(cursor: syn::buffer::Cursor<'a>, name: &str) -> syn::synom::PResult<'a, ()> {
if let Some((term, next)) = cursor.term() {
if term.as_str() == name {
if let Some((ident, next)) = cursor.ident() {
if ident == name {
return Ok(((), next));
}
}
@ -1004,16 +1029,12 @@ fn term<'a>(cursor: syn::buffer::Cursor<'a>, name: &str) -> syn::synom::PResult<
}
fn term2ident<'a>(cursor: syn::buffer::Cursor<'a>)
-> syn::synom::PResult<'a, syn::Ident>
-> syn::synom::PResult<'a, Ident>
{
if let Some((term, next)) = cursor.term() {
let n = term.to_string();
if !n.starts_with("'") {
let i = syn::Ident::new(&n, term.span());
return Ok((i, next));
}
match cursor.ident() {
Some(pair) => Ok(pair),
None => syn::parse_error()
}
syn::parse_error()
}
fn assert_no_lifetimes(decl: &mut syn::FnDecl) {
@ -1029,13 +1050,13 @@ fn assert_no_lifetimes(decl: &mut syn::FnDecl) {
syn::visit_mut::VisitMut::visit_fn_decl_mut(&mut Walk, decl);
}
fn replace_self(name: syn::Ident, item: &mut syn::ImplItem) {
struct Walk(syn::Ident);
fn replace_self(name: &Ident, item: &mut syn::ImplItem) {
struct Walk<'a>(&'a Ident);
impl syn::visit_mut::VisitMut for Walk {
fn visit_ident_mut(&mut self, i: &mut syn::Ident) {
if i.as_ref() == "Self" {
*i = self.0;
impl<'a> syn::visit_mut::VisitMut for Walk<'a> {
fn visit_ident_mut(&mut self, i: &mut Ident) {
if i == "Self" {
*i = self.0.clone();
}
}
}

View File

@ -4,8 +4,8 @@ use std::env;
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
use ast;
use proc_macro2::Span;
use quote::{ToTokens, Tokens};
use proc_macro2::{Span, Ident, TokenStream};
use quote::ToTokens;
use serde_json;
use shared;
use syn;
@ -30,7 +30,7 @@ fn to_ident_name(s: &str) -> Cow<str> {
impl ToTokens for ast::Program {
// Generate wrappers for all the items that we've found
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
for export in self.exports.iter() {
export.to_tokens(tokens);
}
@ -39,15 +39,15 @@ impl ToTokens for ast::Program {
}
let mut types = HashSet::new();
for i in self.imports.iter() {
if let ast::ImportKind::Type(ref t) = i.kind {
types.insert(t.name);
if let ast::ImportKind::Type(t) = &i.kind {
types.insert(t.name.clone());
}
}
for i in self.imports.iter() {
DescribeImport(&i.kind).to_tokens(tokens);
if let Some(ns) = i.js_namespace {
if types.contains(&ns) && i.kind.fits_on_impl() {
if let Some(ns) = &i.js_namespace {
if types.contains(ns) && i.kind.fits_on_impl() {
let kind = &i.kind;
(quote! { impl #ns { #kind } }).to_tokens(tokens);
continue
@ -76,7 +76,7 @@ impl ToTokens for ast::Program {
to_ident_name(&crate_vers),
CNT.fetch_add(1, Ordering::SeqCst)
);
let generated_static_name = syn::Ident::from(generated_static_name);
let generated_static_name = Ident::new(&generated_static_name, Span::call_site());
let description = serde_json::to_string(&self.shared()).unwrap();
@ -103,12 +103,13 @@ impl ToTokens for ast::Program {
}
impl ToTokens for ast::Struct {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
let name = &self.name;
let name_len = name.as_ref().len() as u32;
let name_chars = name.as_ref().chars().map(|c| c as u32);
let new_fn = syn::Ident::from(shared::new_function(self.name.as_ref()));
let free_fn = syn::Ident::from(shared::free_function(self.name.as_ref()));
let name_str = name.to_string();
let name_len = name_str.len() as u32;
let name_chars = name_str.chars().map(|c| c as u32);
let new_fn = Ident::new(&shared::new_function(&name_str), Span::call_site());
let free_fn = Ident::new(&shared::free_function(&name_str), Span::call_site());
(quote! {
impl ::wasm_bindgen::describe::WasmDescribe for #name {
fn describe() {
@ -230,13 +231,13 @@ impl ToTokens for ast::Struct {
}
impl ToTokens for ast::StructField {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
let name = &self.name;
let struct_name = &self.struct_name;
let ty = &self.ty;
let getter = &self.getter;
let setter = &self.setter;
let desc = syn::Ident::from(format!("__wbindgen_describe_{}", getter));
let desc = Ident::new(&format!("__wbindgen_describe_{}", getter), Span::call_site());
(quote! {
#[no_mangle]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
@ -292,17 +293,17 @@ impl ToTokens for ast::StructField {
}
impl ToTokens for ast::Export {
fn to_tokens(self: &ast::Export, into: &mut Tokens) {
fn to_tokens(self: &ast::Export, into: &mut TokenStream) {
let generated_name = self.rust_symbol();
let export_name = self.export_name();
let mut args = vec![];
let mut arg_conversions = vec![];
let mut converted_arguments = vec![];
let ret = syn::Ident::from("_ret");
let ret = Ident::new("_ret", Span::call_site());
let mut offset = 0;
if self.method {
let class = self.class.unwrap();
let class = self.class.as_ref().unwrap();
args.push(quote! { me: *mut ::wasm_bindgen::__rt::WasmRefCell<#class> });
arg_conversions.push(quote! {
::wasm_bindgen::__rt::assert_not_null(me);
@ -313,7 +314,7 @@ impl ToTokens for ast::Export {
for (i, ty) in self.function.arguments.iter().enumerate() {
let i = i + offset;
let ident = syn::Ident::from(format!("arg{}", i));
let ident = Ident::new(&format!("arg{}", i), Span::call_site());
match *ty {
syn::Type::Reference(syn::TypeReference {
mutability: Some(_),
@ -359,9 +360,9 @@ impl ToTokens for ast::Export {
}
let ret_ty;
let convert_ret;
match self.function.ret {
match &self.function.ret {
Some(syn::Type::Reference(_)) => panic!("can't return a borrowed ref"),
Some(ref ty) => {
Some(ty) => {
ret_ty = quote! {
-> <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi
};
@ -377,8 +378,8 @@ impl ToTokens for ast::Export {
convert_ret = quote!{};
}
}
let describe_ret = match self.function.ret {
Some(ref ty) => {
let describe_ret = match &self.function.ret {
Some(ty) => {
quote! {
inform(1);
<#ty as WasmDescribe>::describe();
@ -387,8 +388,8 @@ impl ToTokens for ast::Export {
None => quote! { inform(0); },
};
let name = self.function.name;
let receiver = match self.class {
let name = &self.function.name;
let receiver = match &self.class {
Some(_) if self.method => {
if self.mutable {
quote! { me.borrow_mut().#name }
@ -400,7 +401,7 @@ impl ToTokens for ast::Export {
None => quote!{ #name },
};
let descriptor_name = format!("__wbindgen_describe_{}", export_name);
let descriptor_name = syn::Ident::from(descriptor_name);
let descriptor_name = Ident::new(&descriptor_name, Span::call_site());
let nargs = self.function.arguments.len() as u32;
let argtys = self.function.arguments.iter();
@ -451,7 +452,7 @@ impl ToTokens for ast::Export {
}
impl ToTokens for ast::ImportKind {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
match *self {
ast::ImportKind::Function(ref f) => f.to_tokens(tokens),
ast::ImportKind::Static(ref s) => s.to_tokens(tokens),
@ -461,7 +462,7 @@ impl ToTokens for ast::ImportKind {
}
impl ToTokens for ast::ImportType {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
let vis = &self.vis;
let name = &self.name;
(quote! {
@ -541,7 +542,7 @@ impl ToTokens for ast::ImportType {
}
impl ToTokens for ast::ImportFunction {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
let mut class_ty = None;
let mut is_method = false;
match self.kind {
@ -561,28 +562,32 @@ impl ToTokens for ast::ImportFunction {
let mut abi_argument_names = Vec::new();
let mut abi_arguments = Vec::new();
let mut arg_conversions = Vec::new();
let ret_ident = syn::Ident::from("_ret");
let ret_ident = Ident::new("_ret", Span::call_site());
let names = self.function
.rust_decl
.inputs
.iter()
.map(|arg| match *arg {
syn::FnArg::Captured(ref c) => c,
_ => panic!("arguments cannot be `self` or ignored"),
.map(|arg| {
match arg {
syn::FnArg::Captured(c) => c,
_ => panic!("arguments cannot be `self` or ignored"),
}
})
.map(|arg| match arg.pat {
syn::Pat::Ident(syn::PatIdent {
by_ref: None,
ident,
subpat: None,
..
}) => ident,
_ => panic!("unsupported pattern in foreign function"),
.map(|arg| {
match &arg.pat {
syn::Pat::Ident(syn::PatIdent {
by_ref: None,
ident,
subpat: None,
..
}) => ident.clone(),
_ => panic!("unsupported pattern in foreign function"),
}
});
for (i, (ty, name)) in self.function.arguments.iter().zip(names).enumerate() {
abi_argument_names.push(name);
abi_argument_names.push(name.clone());
abi_arguments.push(quote! {
#name: <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi
});
@ -622,9 +627,9 @@ impl ToTokens for ast::ImportFunction {
let mut exceptional_ret = quote!{};
let exn_data = if self.function.opts.catch() {
let exn_data = syn::Ident::from("exn_data");
let exn_data_ptr = syn::Ident::from("exn_data_ptr");
abi_argument_names.push(exn_data_ptr);
let exn_data = Ident::new("exn_data", Span::call_site());
let exn_data_ptr = Ident::new("exn_data_ptr", Span::call_site());
abi_argument_names.push(exn_data_ptr.clone());
abi_arguments.push(quote! { #exn_data_ptr: *mut u32 });
convert_ret = quote! { Ok(#convert_ret) };
exceptional_ret = quote! {
@ -644,8 +649,8 @@ impl ToTokens for ast::ImportFunction {
quote! {}
};
let rust_name = self.rust_name;
let import_name = self.shim;
let rust_name = &self.rust_name;
let import_name = &self.shim;
let attrs = &self.function.rust_attrs;
let arguments = self.function
@ -710,14 +715,14 @@ impl ToTokens for ast::ImportFunction {
struct DescribeImport<'a>(&'a ast::ImportKind);
impl<'a> ToTokens for DescribeImport<'a> {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
let f = match *self.0 {
ast::ImportKind::Function(ref f) => f,
ast::ImportKind::Static(_) => return,
ast::ImportKind::Type(_) => return,
};
let describe_name = format!("__wbindgen_describe_{}", f.shim);
let describe_name = syn::Ident::from(describe_name);
let describe_name = Ident::new(&describe_name, Span::call_site());
let argtys = f.function.arguments.iter();
let nargs = f.function.arguments.len() as u32;
let inform_ret = match f.function.ret {
@ -739,7 +744,7 @@ impl<'a> ToTokens for DescribeImport<'a> {
}
impl ToTokens for ast::Enum {
fn to_tokens(&self, into: &mut Tokens) {
fn to_tokens(&self, into: &mut TokenStream) {
let enum_name = &self.name;
let cast_clauses = self.variants.iter().map(|variant| {
let variant_name = &variant.name;
@ -782,10 +787,10 @@ impl ToTokens for ast::Enum {
}
impl ToTokens for ast::ImportStatic {
fn to_tokens(&self, into: &mut Tokens) {
let name = self.rust_name;
fn to_tokens(&self, into: &mut TokenStream) {
let name = &self.rust_name;
let ty = &self.ty;
let shim_name = self.shim;
let shim_name = &self.shim;
let vis = &self.vis;
(quote! {
#[allow(bad_style)]

View File

@ -17,9 +17,7 @@ proc-macro = true
spans = ["wasm-bindgen-backend/spans"]
[dependencies]
syn = { version = '0.13', features = ['full'] }
quote = '0.5'
proc-macro2 = "0.3"
serde_json = "1"
wasm-bindgen-shared = { path = "../shared", version = "=0.2.10" }
syn = { version = '0.14', features = ['full'] }
quote = '0.6'
proc-macro2 = "0.4"
wasm-bindgen-backend = { path = "../backend", version = "=0.2.10" }

View File

@ -4,12 +4,10 @@ extern crate syn;
extern crate quote;
extern crate proc_macro;
extern crate proc_macro2;
extern crate serde_json;
extern crate wasm_bindgen_backend as backend;
extern crate wasm_bindgen_shared as shared;
use proc_macro::TokenStream;
use quote::{ToTokens, Tokens};
use quote::ToTokens;
#[proc_macro_attribute]
pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
@ -17,7 +15,7 @@ pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
let opts = syn::parse::<backend::ast::BindgenAttrs>(attr)
.expect("invalid arguments to #[wasm_bindgen]");
let mut ret = Tokens::new();
let mut ret = proc_macro2::TokenStream::empty();
let mut program = backend::ast::Program::default();
program.push_item(item, Some(opts), &mut ret);
program.to_tokens(&mut ret);