2018-03-07 16:28:42 -08:00
|
|
|
use literal::{self, Literal};
|
2018-01-08 10:42:01 -08:00
|
|
|
use proc_macro2::Span;
|
2018-03-29 09:14:32 -07:00
|
|
|
use quote::{ToTokens, Tokens};
|
2018-02-06 07:56:14 -08:00
|
|
|
use shared;
|
2017-12-14 19:31:01 -08:00
|
|
|
use syn;
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
#[derive(Default)]
|
2017-12-18 12:39:14 -08:00
|
|
|
pub struct Program {
|
2018-02-07 16:41:33 -08:00
|
|
|
pub exports: Vec<Export>,
|
2017-12-18 21:43:16 -08:00
|
|
|
pub imports: Vec<Import>,
|
2018-02-22 00:55:11 +01:00
|
|
|
pub enums: Vec<Enum>,
|
2018-02-07 16:41:33 -08:00
|
|
|
pub structs: Vec<Struct>,
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
pub struct Export {
|
|
|
|
pub class: Option<syn::Ident>,
|
|
|
|
pub method: bool,
|
|
|
|
pub mutable: bool,
|
|
|
|
pub function: Function,
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
|
2017-12-18 21:43:16 -08:00
|
|
|
pub struct Import {
|
2018-02-07 16:41:33 -08:00
|
|
|
pub module: Option<String>,
|
2018-03-22 16:59:48 -07:00
|
|
|
pub js_namespace: Option<syn::Ident>,
|
2018-02-07 16:41:33 -08:00
|
|
|
pub kind: ImportKind,
|
2018-02-05 14:24:25 -08:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
pub enum ImportKind {
|
2018-03-21 09:55:16 -07:00
|
|
|
Function(ImportFunction),
|
|
|
|
Static(ImportStatic),
|
|
|
|
Type(ImportType),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ImportFunction {
|
|
|
|
pub function: Function,
|
2018-03-22 19:05:14 -07:00
|
|
|
pub rust_name: syn::Ident,
|
2018-03-21 09:55:16 -07:00
|
|
|
pub kind: ImportFunctionKind,
|
2018-03-22 19:05:14 -07:00
|
|
|
pub shim: syn::Ident,
|
2018-03-21 09:55:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ImportFunctionKind {
|
2018-03-29 09:14:32 -07:00
|
|
|
Method { class: String, ty: syn::Type },
|
|
|
|
JsConstructor { class: String, ty: syn::Type },
|
2018-02-07 16:41:33 -08:00
|
|
|
Normal,
|
2018-02-05 14:24:25 -08:00
|
|
|
}
|
|
|
|
|
2018-03-21 09:55:16 -07:00
|
|
|
pub struct ImportStatic {
|
|
|
|
pub vis: syn::Visibility,
|
|
|
|
pub ty: syn::Type,
|
2018-03-22 19:05:14 -07:00
|
|
|
pub shim: syn::Ident,
|
|
|
|
pub rust_name: syn::Ident,
|
|
|
|
pub js_name: syn::Ident,
|
2018-03-21 09:55:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ImportType {
|
|
|
|
pub vis: syn::Visibility,
|
|
|
|
pub name: syn::Ident,
|
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
pub struct Function {
|
2018-02-05 14:24:25 -08:00
|
|
|
pub name: syn::Ident,
|
2018-02-07 16:41:33 -08:00
|
|
|
pub arguments: Vec<Type>,
|
|
|
|
pub ret: Option<Type>,
|
|
|
|
pub opts: BindgenAttrs,
|
|
|
|
pub rust_attrs: Vec<syn::Attribute>,
|
|
|
|
pub rust_decl: Box<syn::FnDecl>,
|
|
|
|
pub rust_vis: syn::Visibility,
|
2018-02-06 15:19:47 -08:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
pub struct Struct {
|
|
|
|
pub name: syn::Ident,
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
|
|
|
|
2018-02-22 00:55:11 +01:00
|
|
|
pub struct Enum {
|
2018-02-22 10:55:44 +01:00
|
|
|
pub name: syn::Ident,
|
2018-03-29 09:14:32 -07:00
|
|
|
pub variants: Vec<Variant>,
|
2018-03-07 16:28:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Variant {
|
|
|
|
pub name: syn::Ident,
|
|
|
|
pub value: u32,
|
2018-02-22 00:55:11 +01:00
|
|
|
}
|
|
|
|
|
2018-04-04 08:24:19 -07:00
|
|
|
pub struct Type {
|
|
|
|
pub ty: syn::Type,
|
|
|
|
pub kind: TypeKind,
|
|
|
|
pub loc: TypeLocation,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum TypeKind {
|
|
|
|
ByRef,
|
|
|
|
ByMutRef,
|
|
|
|
ByValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum TypeLocation {
|
|
|
|
ImportArgument,
|
|
|
|
ImportRet,
|
|
|
|
ExportArgument,
|
|
|
|
ExportRet,
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Program {
|
2018-03-29 09:14:32 -07:00
|
|
|
pub fn push_item(&mut self, item: syn::Item, opts: Option<BindgenAttrs>, tokens: &mut Tokens) {
|
2018-02-07 16:41:33 -08:00
|
|
|
match item {
|
|
|
|
syn::Item::Fn(mut f) => {
|
|
|
|
let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut f.attrs));
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
let no_mangle = f.attrs
|
|
|
|
.iter()
|
2018-02-07 16:41:33 -08:00
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(i, m)| m.interpret_meta().map(|m| (i, m)))
|
|
|
|
.find(|&(_, ref m)| m.name() == "no_mangle");
|
|
|
|
match no_mangle {
|
2018-03-29 09:14:32 -07:00
|
|
|
Some((i, _)) => {
|
|
|
|
f.attrs.remove(i);
|
|
|
|
}
|
2018-03-05 23:25:15 +01:00
|
|
|
_ => {}
|
2018-02-07 16:41:33 -08:00
|
|
|
}
|
|
|
|
f.to_tokens(tokens);
|
|
|
|
self.exports.push(Export {
|
|
|
|
class: None,
|
|
|
|
method: false,
|
|
|
|
mutable: false,
|
|
|
|
function: Function::from(f, opts),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
syn::Item::Struct(mut s) => {
|
|
|
|
let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut s.attrs));
|
|
|
|
s.to_tokens(tokens);
|
|
|
|
self.structs.push(Struct::from(s, opts));
|
|
|
|
}
|
|
|
|
syn::Item::Impl(mut i) => {
|
|
|
|
let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut i.attrs));
|
|
|
|
i.to_tokens(tokens);
|
|
|
|
self.push_impl(i, opts);
|
|
|
|
}
|
|
|
|
syn::Item::ForeignMod(mut f) => {
|
|
|
|
let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut f.attrs));
|
|
|
|
self.push_foreign_mod(f, opts);
|
|
|
|
}
|
2018-02-22 00:55:11 +01:00
|
|
|
syn::Item::Enum(mut e) => {
|
|
|
|
let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut e.attrs));
|
|
|
|
e.to_tokens(tokens);
|
|
|
|
self.push_enum(e, opts);
|
|
|
|
}
|
2018-03-29 09:14:32 -07:00
|
|
|
_ => panic!(
|
|
|
|
"#[wasm_bindgen] can only be applied to a function, \
|
|
|
|
struct, enum, impl, or extern block"
|
|
|
|
),
|
2018-02-07 16:41:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_impl(&mut self, item: syn::ItemImpl, _opts: BindgenAttrs) {
|
2017-12-31 14:40:57 -08:00
|
|
|
if item.defaultness.is_some() {
|
|
|
|
panic!("default impls are not supported");
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
2017-12-31 14:40:57 -08:00
|
|
|
if item.unsafety.is_some() {
|
|
|
|
panic!("unsafe impls are not supported");
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
if item.trait_.is_some() {
|
|
|
|
panic!("trait impls are not supported");
|
|
|
|
}
|
|
|
|
if item.generics.params.len() > 0 {
|
|
|
|
panic!("generic impls aren't supported");
|
|
|
|
}
|
2018-02-06 07:56:14 -08:00
|
|
|
let name = match *item.self_ty {
|
2018-03-29 09:14:32 -07:00
|
|
|
syn::Type::Path(syn::TypePath {
|
|
|
|
qself: None,
|
|
|
|
ref path,
|
|
|
|
}) => match extract_path_ident(path) {
|
|
|
|
Some(ident) => ident,
|
|
|
|
None => panic!("unsupported self type in impl"),
|
|
|
|
},
|
2017-12-18 12:39:14 -08:00
|
|
|
_ => panic!("unsupported self type in impl"),
|
|
|
|
};
|
2018-02-07 16:41:33 -08:00
|
|
|
for item in item.items.into_iter() {
|
|
|
|
self.push_impl_item(name, item);
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
fn push_impl_item(&mut self, class: syn::Ident, item: syn::ImplItem) {
|
|
|
|
let mut method = match item {
|
|
|
|
syn::ImplItem::Const(_) => panic!("const definitions aren't supported"),
|
|
|
|
syn::ImplItem::Type(_) => panic!("type definitions in impls aren't supported"),
|
|
|
|
syn::ImplItem::Method(m) => m,
|
|
|
|
syn::ImplItem::Macro(_) => panic!("macros in impls aren't supported"),
|
|
|
|
syn::ImplItem::Verbatim(_) => panic!("unparsed impl item?"),
|
|
|
|
};
|
|
|
|
match method.vis {
|
|
|
|
syn::Visibility::Public(_) => {}
|
|
|
|
_ => return,
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
2018-02-07 16:41:33 -08:00
|
|
|
if method.defaultness.is_some() {
|
|
|
|
panic!("default methods are not supported");
|
|
|
|
}
|
|
|
|
if method.sig.constness.is_some() {
|
|
|
|
panic!("can only bindgen non-const functions");
|
|
|
|
}
|
|
|
|
if method.sig.unsafety.is_some() {
|
|
|
|
panic!("can only bindgen safe functions");
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
let opts = BindgenAttrs::find(&mut method.attrs);
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
let (function, mutable) = Function::from_decl(
|
|
|
|
method.sig.ident,
|
|
|
|
Box::new(method.sig.decl),
|
|
|
|
method.attrs,
|
|
|
|
opts,
|
|
|
|
method.vis,
|
|
|
|
true,
|
2018-04-04 08:24:19 -07:00
|
|
|
false,
|
2018-03-29 09:14:32 -07:00
|
|
|
);
|
2018-02-07 16:41:33 -08:00
|
|
|
self.exports.push(Export {
|
|
|
|
class: Some(class),
|
|
|
|
method: mutable.is_some(),
|
|
|
|
mutable: mutable.unwrap_or(false),
|
|
|
|
function,
|
|
|
|
});
|
|
|
|
}
|
2017-12-18 21:43:16 -08:00
|
|
|
|
2018-02-22 12:08:28 +01:00
|
|
|
pub fn push_enum(&mut self, item: syn::ItemEnum, _opts: BindgenAttrs) {
|
2018-02-22 00:55:11 +01:00
|
|
|
match item.vis {
|
|
|
|
syn::Visibility::Public(_) => {}
|
|
|
|
_ => panic!("only public enums are allowed"),
|
|
|
|
}
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
let variants = item.variants
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, v)| {
|
|
|
|
match v.fields {
|
|
|
|
syn::Fields::Unit => (),
|
|
|
|
_ => panic!("Only C-Style enums allowed"),
|
|
|
|
}
|
|
|
|
let value = match v.discriminant {
|
|
|
|
Some((
|
|
|
|
_,
|
|
|
|
syn::Expr::Lit(syn::ExprLit {
|
|
|
|
attrs: _,
|
|
|
|
lit: syn::Lit::Int(ref int_lit),
|
|
|
|
}),
|
|
|
|
)) => {
|
|
|
|
if int_lit.value() > <u32>::max_value() as u64 {
|
|
|
|
panic!("Enums can only support numbers that can be represented as u32");
|
|
|
|
}
|
|
|
|
int_lit.value() as u32
|
2018-02-23 14:17:53 +01:00
|
|
|
}
|
2018-03-29 09:14:32 -07:00
|
|
|
None => i as u32,
|
|
|
|
_ => panic!("Enums may only have number literal values"),
|
|
|
|
};
|
2018-02-23 14:17:53 +01:00
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
Variant {
|
|
|
|
name: v.ident,
|
|
|
|
value,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2018-02-22 10:55:44 +01:00
|
|
|
self.enums.push(Enum {
|
|
|
|
name: item.ident,
|
2018-03-29 09:14:32 -07:00
|
|
|
variants,
|
2018-02-22 00:55:11 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-21 09:55:16 -07:00
|
|
|
pub fn push_foreign_mod(&mut self, f: syn::ItemForeignMod, opts: BindgenAttrs) {
|
|
|
|
match f.abi.name {
|
|
|
|
Some(ref l) if l.value() == "C" => {}
|
|
|
|
None => {}
|
|
|
|
_ => panic!("only foreign mods with the `C` ABI are allowed"),
|
|
|
|
}
|
|
|
|
for mut item in f.items.into_iter() {
|
|
|
|
let item_opts = {
|
|
|
|
let attrs = match item {
|
|
|
|
syn::ForeignItem::Fn(ref mut f) => &mut f.attrs,
|
|
|
|
syn::ForeignItem::Type(ref mut t) => &mut t.attrs,
|
|
|
|
syn::ForeignItem::Static(ref mut s) => &mut s.attrs,
|
|
|
|
_ => panic!("only foreign functions/types allowed for now"),
|
|
|
|
};
|
|
|
|
BindgenAttrs::find(attrs)
|
|
|
|
};
|
|
|
|
let module = item_opts.module().or(opts.module()).map(|s| s.to_string());
|
2018-03-22 16:59:48 -07:00
|
|
|
let js_namespace = item_opts.js_namespace().or(opts.js_namespace());
|
2018-03-21 09:55:16 -07:00
|
|
|
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),
|
2018-03-22 19:05:14 -07:00
|
|
|
syn::ForeignItem::Static(s) => self.push_foreign_static(s, item_opts),
|
2018-03-21 09:55:16 -07:00
|
|
|
_ => panic!("only foreign functions/types allowed for now"),
|
|
|
|
};
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
self.imports.push(Import {
|
|
|
|
module,
|
|
|
|
js_namespace,
|
|
|
|
kind,
|
|
|
|
});
|
2018-03-21 09:55:16 -07:00
|
|
|
}
|
|
|
|
}
|
2018-02-07 16:41:33 -08:00
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
pub fn push_foreign_fn(&mut self, f: syn::ForeignItemFn, opts: BindgenAttrs) -> ImportKind {
|
2018-03-22 19:05:14 -07:00
|
|
|
let js_name = opts.js_name().unwrap_or(f.ident);
|
2018-04-04 08:24:19 -07:00
|
|
|
let mut wasm = Function::from_decl(
|
|
|
|
js_name,
|
|
|
|
f.decl,
|
|
|
|
f.attrs,
|
|
|
|
opts,
|
|
|
|
f.vis,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
).0;
|
2018-02-07 16:41:33 -08:00
|
|
|
if wasm.opts.catch() {
|
2018-02-06 19:04:12 -08:00
|
|
|
// TODO: this assumes a whole bunch:
|
|
|
|
//
|
|
|
|
// * The outer type is actually a `Result`
|
|
|
|
// * The error type is a `JsValue`
|
|
|
|
// * The actual type is the first type parameter
|
|
|
|
//
|
|
|
|
// should probably fix this one day...
|
|
|
|
wasm.ret = extract_first_ty_param(wasm.ret.as_ref())
|
|
|
|
.expect("can't `catch` without returning a Result");
|
|
|
|
}
|
2018-02-05 14:24:25 -08:00
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
let kind = if wasm.opts.method() {
|
2018-03-29 09:14:32 -07:00
|
|
|
let class = wasm.arguments
|
|
|
|
.get(0)
|
2018-02-07 16:41:33 -08:00
|
|
|
.expect("methods must have at least one argument");
|
2018-04-04 08:24:19 -07:00
|
|
|
let class_name = match class.ty {
|
2018-03-29 09:14:32 -07:00
|
|
|
syn::Type::Path(syn::TypePath {
|
|
|
|
qself: None,
|
|
|
|
ref path,
|
|
|
|
}) => path,
|
2018-02-07 16:41:33 -08:00
|
|
|
_ => panic!("first argument of method must be a path"),
|
|
|
|
};
|
|
|
|
let class_name = extract_path_ident(class_name)
|
|
|
|
.expect("first argument of method must be a bare type");
|
|
|
|
|
2018-03-21 09:55:16 -07:00
|
|
|
ImportFunctionKind::Method {
|
2018-02-07 16:41:33 -08:00
|
|
|
class: class_name.as_ref().to_string(),
|
2018-04-04 08:24:19 -07:00
|
|
|
ty: class.ty.clone(),
|
2018-02-07 16:41:33 -08:00
|
|
|
}
|
|
|
|
} else if wasm.opts.constructor() {
|
|
|
|
let class = match wasm.ret {
|
2018-04-04 08:24:19 -07:00
|
|
|
Some(Type { ref ty, kind: TypeKind::ByValue, .. }) => ty,
|
2018-02-07 16:41:33 -08:00
|
|
|
_ => panic!("constructor returns must be bare types"),
|
|
|
|
};
|
|
|
|
let class_name = match *class {
|
2018-03-29 09:14:32 -07:00
|
|
|
syn::Type::Path(syn::TypePath {
|
|
|
|
qself: None,
|
|
|
|
ref path,
|
|
|
|
}) => path,
|
2018-02-07 16:41:33 -08:00
|
|
|
_ => panic!("first argument of method must be a path"),
|
|
|
|
};
|
|
|
|
let class_name = extract_path_ident(class_name)
|
|
|
|
.expect("first argument of method must be a bare type");
|
|
|
|
|
2018-03-21 09:55:16 -07:00
|
|
|
ImportFunctionKind::JsConstructor {
|
2018-02-07 16:41:33 -08:00
|
|
|
class: class_name.as_ref().to_string(),
|
|
|
|
ty: class.clone(),
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-21 09:55:16 -07:00
|
|
|
ImportFunctionKind::Normal
|
2018-02-07 16:41:33 -08:00
|
|
|
};
|
|
|
|
|
2018-03-22 19:05:14 -07:00
|
|
|
let shim = {
|
|
|
|
let ns = match kind {
|
|
|
|
ImportFunctionKind::Normal => "n",
|
|
|
|
ImportFunctionKind::Method { ref class, .. } => class,
|
|
|
|
ImportFunctionKind::JsConstructor { ref class, .. } => class,
|
|
|
|
};
|
|
|
|
format!("__wbg_f_{}_{}_{}", js_name, f.ident, ns)
|
|
|
|
};
|
2018-03-21 09:55:16 -07:00
|
|
|
ImportKind::Function(ImportFunction {
|
2018-02-07 16:41:33 -08:00
|
|
|
function: wasm,
|
2018-03-21 09:55:16 -07:00
|
|
|
kind,
|
2018-03-22 19:05:14 -07:00
|
|
|
rust_name: f.ident,
|
|
|
|
shim: shim.into(),
|
2018-03-21 09:55:16 -07:00
|
|
|
})
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
pub fn push_foreign_ty(&mut self, f: syn::ForeignItemType) -> ImportKind {
|
2018-03-21 09:55:16 -07:00
|
|
|
ImportKind::Type(ImportType {
|
2018-03-07 14:48:18 -08:00
|
|
|
vis: f.vis,
|
2018-03-29 09:14:32 -07:00
|
|
|
name: f.ident,
|
2018-03-21 09:55:16 -07:00
|
|
|
})
|
2018-02-07 16:41:33 -08:00
|
|
|
}
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
pub fn push_foreign_static(
|
|
|
|
&mut self,
|
|
|
|
f: syn::ForeignItemStatic,
|
|
|
|
opts: BindgenAttrs,
|
|
|
|
) -> ImportKind {
|
2018-03-21 08:09:59 -07:00
|
|
|
if f.mutability.is_some() {
|
|
|
|
panic!("cannot import mutable globals yet")
|
|
|
|
}
|
2018-03-22 19:05:14 -07:00
|
|
|
let js_name = opts.js_name().unwrap_or(f.ident);
|
|
|
|
let shim = format!("__wbg_static_accessor_{}_{}", js_name, f.ident);
|
2018-03-21 09:55:16 -07:00
|
|
|
ImportKind::Static(ImportStatic {
|
2018-03-21 08:09:59 -07:00
|
|
|
ty: *f.ty,
|
|
|
|
vis: f.vis,
|
2018-03-22 19:05:14 -07:00
|
|
|
rust_name: f.ident,
|
|
|
|
js_name,
|
|
|
|
shim: shim.into(),
|
2018-03-21 09:55:16 -07:00
|
|
|
})
|
2018-03-21 08:09:59 -07:00
|
|
|
}
|
|
|
|
|
2018-03-07 16:28:42 -08:00
|
|
|
pub fn literal(&self, dst: &mut Tokens) -> usize {
|
2018-02-10 10:05:43 -08:00
|
|
|
let mut tmp = Tokens::new();
|
|
|
|
let cnt = {
|
2018-03-07 16:28:42 -08:00
|
|
|
let mut a = literal::LiteralBuilder::new(&mut tmp);
|
|
|
|
Literal::literal(self, &mut a);
|
|
|
|
a.finish()
|
2018-02-06 07:56:14 -08:00
|
|
|
};
|
2018-02-10 10:05:43 -08:00
|
|
|
let cnt = cnt as u32;
|
|
|
|
(quote! {
|
2018-03-14 14:33:53 -07:00
|
|
|
(#cnt >> 0) as u8,
|
|
|
|
(#cnt >> 8) as u8,
|
|
|
|
(#cnt >> 16) as u8,
|
|
|
|
(#cnt >> 24) as u8
|
2018-02-10 10:05:43 -08:00
|
|
|
}).to_tokens(dst);
|
|
|
|
tmp.to_tokens(dst);
|
2018-03-14 14:33:53 -07:00
|
|
|
(cnt as usize) + 4
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Function {
|
2018-02-07 16:41:33 -08:00
|
|
|
pub fn from(input: syn::ItemFn, opts: BindgenAttrs) -> Function {
|
2017-12-14 19:31:01 -08:00
|
|
|
match input.vis {
|
|
|
|
syn::Visibility::Public(_) => {}
|
|
|
|
_ => panic!("can only bindgen public functions"),
|
|
|
|
}
|
2017-12-31 14:40:57 -08:00
|
|
|
if input.constness.is_some() {
|
|
|
|
panic!("can only bindgen non-const functions");
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
2017-12-31 14:40:57 -08:00
|
|
|
if input.unsafety.is_some() {
|
|
|
|
panic!("can only bindgen safe functions");
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
2017-12-18 12:39:14 -08:00
|
|
|
|
2018-04-04 08:24:19 -07:00
|
|
|
Function::from_decl(
|
|
|
|
input.ident,
|
|
|
|
input.decl,
|
|
|
|
input.attrs,
|
|
|
|
opts,
|
|
|
|
input.vis,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
).0
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
pub fn from_decl(
|
|
|
|
name: syn::Ident,
|
|
|
|
decl: Box<syn::FnDecl>,
|
|
|
|
attrs: Vec<syn::Attribute>,
|
|
|
|
opts: BindgenAttrs,
|
|
|
|
vis: syn::Visibility,
|
|
|
|
allow_self: bool,
|
2018-04-04 08:24:19 -07:00
|
|
|
import: bool,
|
2018-03-29 09:14:32 -07:00
|
|
|
) -> (Function, Option<bool>) {
|
2017-12-31 14:40:57 -08:00
|
|
|
if decl.variadic.is_some() {
|
2017-12-14 19:31:01 -08:00
|
|
|
panic!("can't bindgen variadic functions")
|
|
|
|
}
|
2017-12-18 21:43:16 -08:00
|
|
|
if decl.generics.params.len() > 0 {
|
2017-12-14 19:31:01 -08:00
|
|
|
panic!("can't bindgen functions with lifetime or type parameters")
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:24:25 -08:00
|
|
|
let mut mutable = None;
|
2018-03-29 09:14:32 -07:00
|
|
|
let arguments = decl.inputs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|arg| match *arg {
|
|
|
|
syn::FnArg::Captured(ref c) => Some(c),
|
|
|
|
syn::FnArg::SelfValue(_) => {
|
|
|
|
panic!("by-value `self` not yet supported");
|
|
|
|
}
|
|
|
|
syn::FnArg::SelfRef(ref a) if allow_self => {
|
|
|
|
assert!(mutable.is_none());
|
|
|
|
mutable = Some(a.mutability.is_some());
|
|
|
|
None
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
2018-03-29 09:14:32 -07:00
|
|
|
_ => panic!("arguments cannot be `self` or ignored"),
|
2017-12-14 19:31:01 -08:00
|
|
|
})
|
2018-04-04 08:24:19 -07:00
|
|
|
.map(|arg| {
|
|
|
|
Type::from(&arg.ty, if import {
|
|
|
|
TypeLocation::ImportArgument
|
|
|
|
} else {
|
|
|
|
TypeLocation::ExportArgument
|
|
|
|
})
|
|
|
|
})
|
2017-12-14 19:31:01 -08:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2017-12-18 21:43:16 -08:00
|
|
|
let ret = match decl.output {
|
2017-12-14 19:31:01 -08:00
|
|
|
syn::ReturnType::Default => None,
|
2018-04-04 08:24:19 -07:00
|
|
|
syn::ReturnType::Type(_, ref t) => {
|
|
|
|
Some(Type::from(t, if import {
|
|
|
|
TypeLocation::ImportRet
|
|
|
|
} else {
|
|
|
|
TypeLocation::ExportRet
|
|
|
|
}))
|
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
};
|
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
(
|
|
|
|
Function {
|
|
|
|
name,
|
|
|
|
arguments,
|
|
|
|
ret,
|
|
|
|
opts,
|
|
|
|
rust_vis: vis,
|
|
|
|
rust_decl: decl,
|
|
|
|
rust_attrs: attrs,
|
|
|
|
},
|
|
|
|
mutable,
|
|
|
|
)
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 07:56:14 -08:00
|
|
|
pub fn extract_path_ident(path: &syn::Path) -> Option<syn::Ident> {
|
2017-12-18 21:43:16 -08:00
|
|
|
if path.leading_colon.is_some() {
|
2018-03-29 09:14:32 -07:00
|
|
|
return None;
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
|
|
|
if path.segments.len() != 1 {
|
2018-03-29 09:14:32 -07:00
|
|
|
return None;
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
2018-01-08 10:42:01 -08:00
|
|
|
match path.segments.first().unwrap().value().arguments {
|
2017-12-18 21:43:16 -08:00
|
|
|
syn::PathArguments::None => {}
|
2018-02-06 07:56:14 -08:00
|
|
|
_ => return None,
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
2018-02-06 07:56:14 -08:00
|
|
|
path.segments.first().map(|v| v.value().ident)
|
2017-12-18 21:43:16 -08:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
impl Export {
|
|
|
|
pub fn rust_symbol(&self) -> syn::Ident {
|
|
|
|
let mut generated_name = format!("__wasm_bindgen_generated");
|
|
|
|
if let Some(class) = self.class {
|
|
|
|
generated_name.push_str("_");
|
|
|
|
generated_name.push_str(class.as_ref());
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
2018-02-07 16:41:33 -08:00
|
|
|
generated_name.push_str("_");
|
|
|
|
generated_name.push_str(self.function.name.as_ref());
|
|
|
|
syn::Ident::from(generated_name)
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
pub fn export_name(&self) -> syn::LitStr {
|
|
|
|
let name = match self.class {
|
|
|
|
Some(class) => {
|
2018-03-29 09:14:32 -07:00
|
|
|
shared::struct_function_export_name(class.as_ref(), self.function.name.as_ref())
|
2017-12-14 21:55:21 -08:00
|
|
|
}
|
2018-03-29 09:14:32 -07:00
|
|
|
None => shared::free_function_export_name(self.function.name.as_ref()),
|
2018-02-07 16:41:33 -08:00
|
|
|
};
|
2018-04-03 07:07:14 -07:00
|
|
|
syn::LitStr::new(&name, Span::call_site())
|
2017-12-18 12:39:14 -08:00
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
2018-02-05 14:24:25 -08:00
|
|
|
|
2018-03-21 09:55:16 -07:00
|
|
|
impl ImportFunction {
|
2018-03-07 16:28:42 -08:00
|
|
|
pub fn infer_getter_property(&self) -> String {
|
2018-02-14 13:16:02 -08:00
|
|
|
self.function.name.as_ref().to_string()
|
|
|
|
}
|
|
|
|
|
2018-03-07 16:28:42 -08:00
|
|
|
pub fn infer_setter_property(&self) -> String {
|
2018-02-14 13:16:02 -08:00
|
|
|
let name = self.function.name.as_ref();
|
|
|
|
assert!(name.starts_with("set_"), "setters must start with `set_`");
|
|
|
|
name[4..].to_string()
|
|
|
|
}
|
2018-02-06 07:56:14 -08:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
impl Struct {
|
|
|
|
fn from(s: syn::ItemStruct, _opts: BindgenAttrs) -> Struct {
|
|
|
|
Struct { name: s.ident }
|
2018-02-05 14:24:25 -08:00
|
|
|
}
|
|
|
|
}
|
2018-02-06 07:56:14 -08:00
|
|
|
|
2018-04-04 08:24:19 -07:00
|
|
|
impl Type {
|
|
|
|
pub fn from(ty: &syn::Type, loc: TypeLocation) -> Type {
|
|
|
|
let (ty, kind) = match *ty {
|
|
|
|
syn::Type::Reference(ref r) => {
|
|
|
|
if r.mutability.is_some() {
|
|
|
|
((*r.elem).clone(), TypeKind::ByMutRef)
|
|
|
|
} else {
|
|
|
|
((*r.elem).clone(), TypeKind::ByRef)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (ty.clone(), TypeKind::ByValue),
|
|
|
|
};
|
|
|
|
Type { loc, ty, kind }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 19:04:12 -08:00
|
|
|
#[derive(Default)]
|
2018-02-07 16:41:33 -08:00
|
|
|
pub struct BindgenAttrs {
|
|
|
|
attrs: Vec<BindgenAttr>,
|
2018-02-06 19:04:12 -08:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
impl BindgenAttrs {
|
|
|
|
pub fn find(attrs: &mut Vec<syn::Attribute>) -> BindgenAttrs {
|
2018-03-29 09:14:32 -07:00
|
|
|
let pos = attrs
|
|
|
|
.iter()
|
2018-02-07 16:41:33 -08:00
|
|
|
.enumerate()
|
|
|
|
.find(|&(_, ref m)| m.path.segments[0].ident == "wasm_bindgen")
|
|
|
|
.map(|a| a.0);
|
|
|
|
let pos = match pos {
|
|
|
|
Some(i) => i,
|
|
|
|
None => return BindgenAttrs::default(),
|
|
|
|
};
|
2018-03-29 09:14:32 -07:00
|
|
|
syn::parse(attrs.remove(pos).tts.into()).expect("malformed #[wasm_bindgen] attribute")
|
2018-02-07 16:41:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn module(&self) -> Option<&str> {
|
2018-03-29 09:14:32 -07:00
|
|
|
self.attrs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match *a {
|
|
|
|
BindgenAttr::Module(ref s) => Some(&s[..]),
|
|
|
|
_ => None,
|
2018-02-06 19:04:12 -08:00
|
|
|
})
|
2018-02-07 16:41:33 -08:00
|
|
|
.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn catch(&self) -> bool {
|
2018-03-29 09:14:32 -07:00
|
|
|
self.attrs.iter().any(|a| match *a {
|
|
|
|
BindgenAttr::Catch => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
2018-02-07 16:41:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn constructor(&self) -> bool {
|
2018-03-29 09:14:32 -07:00
|
|
|
self.attrs.iter().any(|a| match *a {
|
|
|
|
BindgenAttr::Constructor => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
2018-02-07 16:41:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn method(&self) -> bool {
|
2018-03-29 09:14:32 -07:00
|
|
|
self.attrs.iter().any(|a| match *a {
|
|
|
|
BindgenAttr::Method => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
2018-02-07 16:41:33 -08:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:59:48 -07:00
|
|
|
fn js_namespace(&self) -> Option<syn::Ident> {
|
2018-03-29 09:14:32 -07:00
|
|
|
self.attrs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match *a {
|
|
|
|
BindgenAttr::JsNamespace(s) => Some(s),
|
|
|
|
_ => None,
|
2018-02-07 16:41:33 -08:00
|
|
|
})
|
|
|
|
.next()
|
2018-02-06 19:04:12 -08:00
|
|
|
}
|
2018-02-14 13:16:02 -08:00
|
|
|
|
2018-03-22 19:05:14 -07:00
|
|
|
pub fn getter(&self) -> Option<Option<syn::Ident>> {
|
2018-03-29 09:14:32 -07:00
|
|
|
self.attrs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match *a {
|
|
|
|
BindgenAttr::Getter(s) => Some(s),
|
|
|
|
_ => None,
|
2018-02-14 13:16:02 -08:00
|
|
|
})
|
2018-03-22 18:21:41 -07:00
|
|
|
.next()
|
2018-02-14 13:16:02 -08:00
|
|
|
}
|
|
|
|
|
2018-03-22 19:05:14 -07:00
|
|
|
pub fn setter(&self) -> Option<Option<syn::Ident>> {
|
2018-03-29 09:14:32 -07:00
|
|
|
self.attrs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match *a {
|
|
|
|
BindgenAttr::Setter(s) => Some(s),
|
|
|
|
_ => None,
|
2018-02-14 13:16:02 -08:00
|
|
|
})
|
2018-03-22 18:21:41 -07:00
|
|
|
.next()
|
2018-02-14 13:16:02 -08:00
|
|
|
}
|
2018-03-22 17:37:27 -07:00
|
|
|
|
|
|
|
pub fn structural(&self) -> bool {
|
2018-03-29 09:14:32 -07:00
|
|
|
self.attrs.iter().any(|a| match *a {
|
|
|
|
BindgenAttr::Structural => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
2018-03-22 17:37:27 -07:00
|
|
|
}
|
2018-03-22 19:05:14 -07:00
|
|
|
|
|
|
|
pub fn js_name(&self) -> Option<syn::Ident> {
|
2018-03-29 09:14:32 -07:00
|
|
|
self.attrs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| match *a {
|
|
|
|
BindgenAttr::JsName(s) => Some(s),
|
|
|
|
_ => None,
|
2018-03-22 19:05:14 -07:00
|
|
|
})
|
|
|
|
.next()
|
|
|
|
}
|
2018-02-06 19:04:12 -08:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
impl syn::synom::Synom for BindgenAttrs {
|
|
|
|
named!(parse -> Self, alt!(
|
|
|
|
do_parse!(
|
|
|
|
opts: parens!(call!(
|
|
|
|
syn::punctuated::Punctuated::<_, syn::token::Comma>::parse_terminated
|
|
|
|
)) >>
|
|
|
|
(BindgenAttrs {
|
|
|
|
attrs: opts.1.into_iter().collect(),
|
|
|
|
})
|
|
|
|
) => { |s| s }
|
|
|
|
|
|
|
|
|
epsilon!() => { |_| BindgenAttrs { attrs: Vec::new() } }
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
enum BindgenAttr {
|
|
|
|
Catch,
|
|
|
|
Constructor,
|
|
|
|
Method,
|
2018-03-22 16:59:48 -07:00
|
|
|
JsNamespace(syn::Ident),
|
2018-02-07 16:41:33 -08:00
|
|
|
Module(String),
|
2018-03-22 19:05:14 -07:00
|
|
|
Getter(Option<syn::Ident>),
|
|
|
|
Setter(Option<syn::Ident>),
|
2018-03-22 17:37:27 -07:00
|
|
|
Structural,
|
2018-03-22 19:05:14 -07:00
|
|
|
JsName(syn::Ident),
|
2018-02-07 16:41:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl syn::synom::Synom for BindgenAttr {
|
|
|
|
named!(parse -> Self, alt!(
|
|
|
|
call!(term, "catch") => { |_| BindgenAttr::Catch }
|
|
|
|
|
|
|
|
|
call!(term, "constructor") => { |_| BindgenAttr::Constructor }
|
|
|
|
|
|
|
|
|
call!(term, "method") => { |_| BindgenAttr::Method }
|
|
|
|
|
|
2018-03-22 18:21:41 -07:00
|
|
|
do_parse!(
|
|
|
|
call!(term, "getter") >>
|
|
|
|
val: option!(do_parse!(
|
|
|
|
punct!(=) >>
|
2018-03-22 19:05:14 -07:00
|
|
|
s: syn!(syn::Ident) >>
|
|
|
|
(s)
|
2018-03-22 18:21:41 -07:00
|
|
|
)) >>
|
|
|
|
(val)
|
|
|
|
)=> { BindgenAttr::Getter }
|
2018-02-14 13:16:02 -08:00
|
|
|
|
|
2018-03-22 18:21:41 -07:00
|
|
|
do_parse!(
|
|
|
|
call!(term, "setter") >>
|
|
|
|
val: option!(do_parse!(
|
|
|
|
punct!(=) >>
|
2018-03-22 19:05:14 -07:00
|
|
|
s: syn!(syn::Ident) >>
|
|
|
|
(s)
|
2018-03-22 18:21:41 -07:00
|
|
|
)) >>
|
|
|
|
(val)
|
|
|
|
)=> { BindgenAttr::Setter }
|
2018-02-14 13:16:02 -08:00
|
|
|
|
|
2018-03-22 17:37:27 -07:00
|
|
|
call!(term, "structural") => { |_| BindgenAttr::Structural }
|
|
|
|
|
|
2018-02-07 16:41:33 -08:00
|
|
|
do_parse!(
|
2018-03-22 16:59:48 -07:00
|
|
|
call!(term, "js_namespace") >>
|
2018-02-07 16:41:33 -08:00
|
|
|
punct!(=) >>
|
2018-03-21 09:55:16 -07:00
|
|
|
ns: syn!(syn::Ident) >>
|
|
|
|
(ns)
|
2018-03-22 16:59:48 -07:00
|
|
|
)=> { BindgenAttr::JsNamespace }
|
2018-02-07 16:41:33 -08:00
|
|
|
|
|
|
|
|
do_parse!(
|
|
|
|
call!(term, "module") >>
|
|
|
|
punct!(=) >>
|
|
|
|
s: syn!(syn::LitStr) >>
|
|
|
|
(s.value())
|
|
|
|
)=> { BindgenAttr::Module }
|
2018-03-22 19:05:14 -07:00
|
|
|
|
|
|
|
|
do_parse!(
|
|
|
|
call!(term, "js_name") >>
|
|
|
|
punct!(=) >>
|
|
|
|
ns: syn!(syn::Ident) >>
|
|
|
|
(ns)
|
|
|
|
)=> { BindgenAttr::JsName }
|
2018-02-07 16:41:33 -08:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-02-06 19:04:12 -08:00
|
|
|
fn extract_first_ty_param(ty: Option<&Type>) -> Option<Option<Type>> {
|
2018-04-04 08:24:19 -07:00
|
|
|
let t = match ty {
|
2018-02-06 19:04:12 -08:00
|
|
|
Some(t) => t,
|
2018-03-29 09:14:32 -07:00
|
|
|
None => return Some(None),
|
2018-02-06 19:04:12 -08:00
|
|
|
};
|
2018-04-04 08:24:19 -07:00
|
|
|
let ty = match *t {
|
|
|
|
Type { ref ty, kind: TypeKind::ByValue, .. } => ty,
|
2018-02-06 19:04:12 -08:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
let path = match *ty {
|
2018-03-29 09:14:32 -07:00
|
|
|
syn::Type::Path(syn::TypePath {
|
|
|
|
qself: None,
|
|
|
|
ref path,
|
|
|
|
}) => path,
|
2018-02-06 19:04:12 -08:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
let seg = path.segments.last()?.into_value();
|
|
|
|
let generics = match seg.arguments {
|
|
|
|
syn::PathArguments::AngleBracketed(ref t) => t,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
let ty = match *generics.args.first()?.into_value() {
|
|
|
|
syn::GenericArgument::Type(ref t) => t,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
match *ty {
|
|
|
|
syn::Type::Tuple(ref t) if t.elems.len() == 0 => return Some(None),
|
|
|
|
_ => {}
|
|
|
|
}
|
2018-04-04 08:24:19 -07:00
|
|
|
Some(Some(Type {
|
|
|
|
ty: ty.clone(),
|
|
|
|
kind: TypeKind::ByValue,
|
|
|
|
loc: t.loc,
|
|
|
|
}))
|
2018-02-06 19:04:12 -08:00
|
|
|
}
|
2018-02-07 16:41:33 -08:00
|
|
|
|
2018-03-29 09:14:32 -07:00
|
|
|
fn term<'a>(cursor: syn::buffer::Cursor<'a>, name: &str) -> syn::synom::PResult<'a, ()> {
|
2018-04-03 07:07:14 -07:00
|
|
|
if let Some((term, next)) = cursor.term() {
|
2018-02-07 16:41:33 -08:00
|
|
|
if term.as_str() == name {
|
2018-03-29 09:14:32 -07:00
|
|
|
return Ok(((), next));
|
2018-02-07 16:41:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
syn::parse_error()
|
|
|
|
}
|