webidl: refactor method/getter/getter generation

This commit is contained in:
R. Andrew Ohana 2018-06-14 02:03:52 -07:00
parent 0cd767c9d4
commit 400015a061
5 changed files with 422 additions and 477 deletions

View File

@ -41,6 +41,7 @@ pub enum ImportKind {
pub struct ImportFunction { pub struct ImportFunction {
pub function: Function, pub function: Function,
pub rust_name: Ident, pub rust_name: Ident,
pub js_ret: Option<syn::Type>,
pub kind: ImportFunctionKind, pub kind: ImportFunctionKind,
pub shim: Ident, pub shim: Ident,
} }
@ -70,11 +71,10 @@ pub struct ImportType {
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))] #[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
pub struct Function { pub struct Function {
pub name: Ident, pub name: Ident,
pub arguments: Vec<syn::Type>, pub arguments: Vec<syn::ArgCaptured>,
pub ret: Option<syn::Type>, pub ret: Option<syn::Type>,
pub opts: BindgenAttrs, pub opts: BindgenAttrs,
pub rust_attrs: Vec<syn::Attribute>, pub rust_attrs: Vec<syn::Attribute>,
pub rust_decl: Box<syn::FnDecl>,
pub rust_vis: syn::Visibility, pub rust_vis: syn::Visibility,
} }
@ -139,7 +139,8 @@ impl Program {
syn::Item::Fn(mut f) => { syn::Item::Fn(mut f) => {
let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut f.attrs)); let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut f.attrs));
let no_mangle = f.attrs let no_mangle = f
.attrs
.iter() .iter()
.enumerate() .enumerate()
.filter_map(|(i, m)| m.interpret_meta().map(|m| (i, m))) .filter_map(|(i, m)| m.interpret_meta().map(|m| (i, m)))
@ -268,7 +269,8 @@ impl Program {
_ => panic!("only public enums are allowed"), _ => panic!("only public enums are allowed"),
} }
let variants = item.variants let variants = item
.variants
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, v)| { .map(|(i, v)| {
@ -345,8 +347,8 @@ impl Program {
pub fn push_foreign_fn(&mut self, f: syn::ForeignItemFn, opts: BindgenAttrs) -> ImportKind { pub fn push_foreign_fn(&mut self, f: syn::ForeignItemFn, opts: BindgenAttrs) -> ImportKind {
let js_name = opts.js_name().unwrap_or(&f.ident).clone(); let js_name = opts.js_name().unwrap_or(&f.ident).clone();
let mut wasm = Function::from_decl(&js_name, f.decl, f.attrs, opts, f.vis, false).0; let wasm = Function::from_decl(&js_name, f.decl, f.attrs, opts, f.vis, false).0;
if wasm.opts.catch() { let js_ret = if wasm.opts.catch() {
// TODO: this assumes a whole bunch: // TODO: this assumes a whole bunch:
// //
// * The outer type is actually a `Result` // * The outer type is actually a `Result`
@ -354,15 +356,18 @@ impl Program {
// * The actual type is the first type parameter // * The actual type is the first type parameter
// //
// should probably fix this one day... // should probably fix this one day...
wasm.ret = extract_first_ty_param(wasm.ret.as_ref()) extract_first_ty_param(wasm.ret.as_ref())
.expect("can't `catch` without returning a Result"); .expect("can't `catch` without returning a Result")
} } else {
wasm.ret.clone()
};
let kind = if wasm.opts.method() { let kind = if wasm.opts.method() {
let class = wasm.arguments let class = wasm
.arguments
.get(0) .get(0)
.expect("methods must have at least one argument"); .expect("methods must have at least one argument");
let class = match *class { let class = match class.ty {
syn::Type::Reference(syn::TypeReference { syn::Type::Reference(syn::TypeReference {
mutability: None, mutability: None,
ref elem, ref elem,
@ -418,6 +423,7 @@ impl Program {
ImportKind::Function(ImportFunction { ImportKind::Function(ImportFunction {
function: wasm, function: wasm,
kind, kind,
js_ret,
rust_name: f.ident.clone(), rust_name: f.ident.clone(),
shim: Ident::new(&shim, Span::call_site()), shim: Ident::new(&shim, Span::call_site()),
}) })
@ -501,11 +507,13 @@ impl Function {
assert_no_lifetimes(&mut decl); assert_no_lifetimes(&mut decl);
let syn::FnDecl { inputs, output, .. } = { *decl };
let mut mutable = None; let mut mutable = None;
let arguments = decl.inputs let arguments = inputs
.iter() .into_iter()
.filter_map(|arg| match *arg { .filter_map(|arg| match arg {
syn::FnArg::Captured(ref c) => Some(c), syn::FnArg::Captured(c) => Some(c),
syn::FnArg::SelfValue(_) => { syn::FnArg::SelfValue(_) => {
panic!("by-value `self` not yet supported"); panic!("by-value `self` not yet supported");
} }
@ -516,12 +524,11 @@ impl Function {
} }
_ => panic!("arguments cannot be `self` or ignored"), _ => panic!("arguments cannot be `self` or ignored"),
}) })
.map(|arg| arg.ty.clone())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let ret = match decl.output { let ret = match output {
syn::ReturnType::Default => None, syn::ReturnType::Default => None,
syn::ReturnType::Type(_, ref t) => Some((**t).clone()), syn::ReturnType::Type(_, ty) => Some(*ty),
}; };
( (
@ -531,7 +538,6 @@ impl Function {
ret, ret,
opts, opts,
rust_vis: vis, rust_vis: vis,
rust_decl: decl,
rust_attrs: attrs, rust_attrs: attrs,
}, },
mutable, mutable,

View File

@ -1,10 +1,10 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashSet; use std::collections::HashSet;
use std::env; use std::env;
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use ast; use ast;
use proc_macro2::{Span, Ident, TokenStream}; use proc_macro2::{Ident, Span, TokenStream};
use quote::ToTokens; use quote::ToTokens;
use serde_json; use serde_json;
use shared; use shared;
@ -50,7 +50,7 @@ impl ToTokens for ast::Program {
if types.contains(ns) && i.kind.fits_on_impl() { if types.contains(ns) && i.kind.fits_on_impl() {
let kind = &i.kind; let kind = &i.kind;
(quote! { impl #ns { #kind } }).to_tokens(tokens); (quote! { impl #ns { #kind } }).to_tokens(tokens);
continue continue;
} }
} }
@ -240,7 +240,10 @@ impl ToTokens for ast::StructField {
let ty = &self.ty; let ty = &self.ty;
let getter = &self.getter; let getter = &self.getter;
let setter = &self.setter; let setter = &self.setter;
let desc = Ident::new(&format!("__wbindgen_describe_{}", getter), Span::call_site()); let desc = Ident::new(
&format!("__wbindgen_describe_{}", getter),
Span::call_site(),
);
(quote! { (quote! {
#[no_mangle] #[no_mangle]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
@ -270,7 +273,7 @@ impl ToTokens for ast::StructField {
}).to_tokens(tokens); }).to_tokens(tokens);
if self.opts.readonly() { if self.opts.readonly() {
return return;
} }
(quote! { (quote! {
@ -315,7 +318,7 @@ impl ToTokens for ast::Export {
offset = 1; offset = 1;
} }
for (i, ty) in self.function.arguments.iter().enumerate() { for (i, syn::ArgCaptured { ty, .. }) in self.function.arguments.iter().enumerate() {
let i = i + offset; let i = i + offset;
let ident = Ident::new(&format!("arg{}", i), Span::call_site()); let ident = Ident::new(&format!("arg{}", i), Span::call_site());
match *ty { match *ty {
@ -377,8 +380,8 @@ impl ToTokens for ast::Export {
}; };
} }
None => { None => {
ret_ty = quote!{}; ret_ty = quote!();
convert_ret = quote!{}; convert_ret = quote!();
} }
} }
let describe_ret = match &self.function.ret { let describe_ret = match &self.function.ret {
@ -406,7 +409,7 @@ impl ToTokens for ast::Export {
let descriptor_name = format!("__wbindgen_describe_{}", export_name); let descriptor_name = format!("__wbindgen_describe_{}", export_name);
let descriptor_name = Ident::new(&descriptor_name, Span::call_site()); let descriptor_name = Ident::new(&descriptor_name, Span::call_site());
let nargs = self.function.arguments.len() as u32; let nargs = self.function.arguments.len() as u32;
let argtys = self.function.arguments.iter(); let argtys = self.function.arguments.iter().map(|arg| &arg.ty);
let tokens = quote! { let tokens = quote! {
#[export_name = #export_name] #[export_name = #export_name]
@ -559,37 +562,27 @@ impl ToTokens for ast::ImportFunction {
ast::ImportFunctionKind::Normal => {} ast::ImportFunctionKind::Normal => {}
} }
let vis = &self.function.rust_vis; let vis = &self.function.rust_vis;
let ret = &self.function.rust_decl.output; let ret = match &self.function.ret {
let fn_token = &self.function.rust_decl.fn_token; Some(ty) => quote! { -> #ty },
None => quote!(),
};
let mut abi_argument_names = Vec::new(); let mut abi_argument_names = Vec::new();
let mut abi_arguments = Vec::new(); let mut abi_arguments = Vec::new();
let mut arg_conversions = Vec::new(); let mut arg_conversions = Vec::new();
let ret_ident = Ident::new("_ret", Span::call_site()); let ret_ident = Ident::new("_ret", Span::call_site());
let names = self.function for (i, syn::ArgCaptured { pat, ty, .. }) in self.function.arguments.iter().enumerate() {
.rust_decl let name = match pat {
.inputs syn::Pat::Ident(syn::PatIdent {
.iter() by_ref: None,
.map(|arg| { ident,
match arg { subpat: None,
syn::FnArg::Captured(c) => c, ..
_ => panic!("arguments cannot be `self` or ignored"), }) => ident.clone(),
} _ => panic!("unsupoported 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.clone()); abi_argument_names.push(name.clone());
abi_arguments.push(quote! { abi_arguments.push(quote! {
#name: <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi #name: <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi
@ -606,7 +599,7 @@ impl ToTokens for ast::ImportFunction {
} }
let abi_ret; let abi_ret;
let mut convert_ret; let mut convert_ret;
match self.function.ret { match &self.js_ret {
Some(syn::Type::Reference(_)) => { Some(syn::Type::Reference(_)) => {
panic!("cannot return references in imports yet"); panic!("cannot return references in imports yet");
} }
@ -628,7 +621,7 @@ impl ToTokens for ast::ImportFunction {
} }
} }
let mut exceptional_ret = quote!{}; let mut exceptional_ret = quote!();
let exn_data = if self.function.opts.catch() { let exn_data = if self.function.opts.catch() {
let exn_data = Ident::new("exn_data", Span::call_site()); let exn_data = Ident::new("exn_data", Span::call_site());
let exn_data_ptr = Ident::new("exn_data_ptr", Span::call_site()); let exn_data_ptr = Ident::new("exn_data_ptr", Span::call_site());
@ -649,20 +642,18 @@ impl ToTokens for ast::ImportFunction {
let #exn_data_ptr = #exn_data.as_mut_ptr(); let #exn_data_ptr = #exn_data.as_mut_ptr();
} }
} else { } else {
quote! {} quote!()
}; };
let rust_name = &self.rust_name; let rust_name = &self.rust_name;
let import_name = &self.shim; let import_name = &self.shim;
let attrs = &self.function.rust_attrs; let attrs = &self.function.rust_attrs;
let arguments = self.function let arguments = if is_method {
.rust_decl &self.function.arguments[1..]
.inputs } else {
.iter() &self.function.arguments[..]
.skip(if is_method { 1 } else { 0 }) };
.collect::<Vec<_>>();
let arguments = &arguments[..];
let me = if is_method { let me = if is_method {
quote! { &self, } quote! { &self, }
@ -674,7 +665,7 @@ impl ToTokens for ast::ImportFunction {
#(#attrs)* #(#attrs)*
#[allow(bad_style)] #[allow(bad_style)]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#vis extern #fn_token #rust_name(#me #(#arguments),*) #ret { #vis extern fn #rust_name(#me #(#arguments),*) #ret {
::wasm_bindgen::__rt::link_this_library(); ::wasm_bindgen::__rt::link_this_library();
#[wasm_import_module = "__wbindgen_placeholder__"] #[wasm_import_module = "__wbindgen_placeholder__"]
extern { extern {
@ -695,7 +686,7 @@ impl ToTokens for ast::ImportFunction {
#(#attrs)* #(#attrs)*
#[allow(bad_style, unused_variables)] #[allow(bad_style, unused_variables)]
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))] #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
#vis extern #fn_token #rust_name(#me #(#arguments),*) #ret { #vis extern fn #rust_name(#me #(#arguments),*) #ret {
panic!("cannot call wasm-bindgen imported functions on \ panic!("cannot call wasm-bindgen imported functions on \
non-wasm targets"); non-wasm targets");
} }
@ -726,9 +717,9 @@ impl<'a> ToTokens for DescribeImport<'a> {
}; };
let describe_name = format!("__wbindgen_describe_{}", f.shim); let describe_name = format!("__wbindgen_describe_{}", f.shim);
let describe_name = Ident::new(&describe_name, Span::call_site()); let describe_name = Ident::new(&describe_name, Span::call_site());
let argtys = f.function.arguments.iter(); let argtys = f.function.arguments.iter().map(|arg| &arg.ty);
let nargs = f.function.arguments.len() as u32; let nargs = f.function.arguments.len() as u32;
let inform_ret = match f.function.ret { let inform_ret = match &f.js_ret {
Some(ref t) => quote! { inform(1); <#t as WasmDescribe>::describe(); }, Some(ref t) => quote! { inform(1); <#t as WasmDescribe>::describe(); },
None => quote! { inform(0); }, None => quote! { inform(0); },
}; };

View File

@ -18,15 +18,19 @@ extern crate syn;
extern crate wasm_bindgen_backend as backend; extern crate wasm_bindgen_backend as backend;
extern crate webidl; extern crate webidl;
use failure::ResultExt;
use heck::SnakeCase;
use proc_macro2::Ident;
use quote::ToTokens;
use std::fs; use std::fs;
use std::io::{self, Read}; use std::io::{self, Read};
use std::iter::FromIterator; use std::iter;
use std::path::Path; use std::path::Path;
use failure::ResultExt;
use heck::CamelCase;
use quote::ToTokens;
mod util;
use util::{create_function, ident_ty, raw_ident, rust_ident, webidl_ty_to_syn_ty, TypePosition};
/// Either `Ok(t)` or `Err(failure::Error)`. /// Either `Ok(t)` or `Err(failure::Error)`.
pub type Result<T> = ::std::result::Result<T, failure::Error>; pub type Result<T> = ::std::result::Result<T, failure::Error>;
@ -70,43 +74,11 @@ fn compile_ast(ast: &backend::ast::Program) -> String {
tokens.to_string() tokens.to_string()
} }
fn is_rust_keyword(name: &str) -> bool { trait WebidlParse<Ctx> {
match name { fn webidl_parse(&self, program: &mut backend::ast::Program, context: Ctx) -> Result<()>;
"abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue"
| "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if"
| "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut"
| "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | "return"
| "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | "true"
| "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | "while"
| "yield" | "bool" | "_" => true,
_ => false,
}
} }
// Create an `Ident`, possibly mangling it if it conflicts with a Rust keyword. impl WebidlParse<()> for Vec<webidl::ast::Definition> {
fn rust_ident(name: &str) -> Ident {
if is_rust_keyword(name) {
Ident::new(&format!("{}_", name), proc_macro2::Span::call_site())
} else {
raw_ident(name)
}
}
// Create an `Ident` without checking to see if it conflicts with a Rust
// keyword.
fn raw_ident(name: &str) -> Ident {
Ident::new(name, proc_macro2::Span::call_site())
}
trait WebidlParse<'a> {
type Extra;
fn webidl_parse(&self, program: &mut backend::ast::Program, extra: Self::Extra) -> Result<()>;
}
impl<'a> WebidlParse<'a> for Vec<webidl::ast::Definition> {
type Extra = ();
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> { fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
for def in self { for def in self {
def.webidl_parse(program, ())?; def.webidl_parse(program, ())?;
@ -115,9 +87,7 @@ impl<'a> WebidlParse<'a> for Vec<webidl::ast::Definition> {
} }
} }
impl<'a> WebidlParse<'a> for webidl::ast::Definition { impl WebidlParse<()> for webidl::ast::Definition {
type Extra = ();
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> { fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
match *self { match *self {
webidl::ast::Definition::Interface(ref interface) => { webidl::ast::Definition::Interface(ref interface) => {
@ -139,9 +109,7 @@ impl<'a> WebidlParse<'a> for webidl::ast::Definition {
} }
} }
impl<'a> WebidlParse<'a> for webidl::ast::Interface { impl WebidlParse<()> for webidl::ast::Interface {
type Extra = ();
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> { fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
match *self { match *self {
webidl::ast::Interface::NonPartial(ref interface) => { webidl::ast::Interface::NonPartial(ref interface) => {
@ -156,9 +124,7 @@ impl<'a> WebidlParse<'a> for webidl::ast::Interface {
} }
} }
impl<'a> WebidlParse<'a> for webidl::ast::Typedef { impl WebidlParse<()> for webidl::ast::Typedef {
type Extra = ();
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> { fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
let dest = rust_ident(&self.name); let dest = rust_ident(&self.name);
let src = match webidl_ty_to_syn_ty(&self.type_, TypePosition::Return) { let src = match webidl_ty_to_syn_ty(&self.type_, TypePosition::Return) {
@ -184,9 +150,7 @@ impl<'a> WebidlParse<'a> for webidl::ast::Typedef {
} }
} }
impl<'a> WebidlParse<'a> for webidl::ast::NonPartialInterface { impl WebidlParse<()> for webidl::ast::NonPartialInterface {
type Extra = ();
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> { fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
program.imports.push(backend::ast::Import { program.imports.push(backend::ast::Import {
module: None, module: None,
@ -208,9 +172,7 @@ impl<'a> WebidlParse<'a> for webidl::ast::NonPartialInterface {
} }
} }
impl<'a> WebidlParse<'a> for webidl::ast::InterfaceMember { impl<'a> WebidlParse<&'a str> for webidl::ast::InterfaceMember {
type Extra = &'a str;
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> { fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
match *self { match *self {
webidl::ast::InterfaceMember::Attribute(ref attr) => { webidl::ast::InterfaceMember::Attribute(ref attr) => {
@ -229,9 +191,7 @@ impl<'a> WebidlParse<'a> for webidl::ast::InterfaceMember {
} }
} }
impl<'a> WebidlParse<'a> for webidl::ast::Attribute { impl<'a> WebidlParse<&'a str> for webidl::ast::Attribute {
type Extra = &'a str;
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> { fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
match *self { match *self {
webidl::ast::Attribute::Regular(ref attr) => attr.webidl_parse(program, self_name), webidl::ast::Attribute::Regular(ref attr) => attr.webidl_parse(program, self_name),
@ -244,9 +204,7 @@ impl<'a> WebidlParse<'a> for webidl::ast::Attribute {
} }
} }
impl<'a> WebidlParse<'a> for webidl::ast::Operation { impl<'a> WebidlParse<&'a str> for webidl::ast::Operation {
type Extra = &'a str;
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> { fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
match *self { match *self {
webidl::ast::Operation::Regular(ref op) => op.webidl_parse(program, self_name), webidl::ast::Operation::Regular(ref op) => op.webidl_parse(program, self_name),
@ -261,192 +219,39 @@ impl<'a> WebidlParse<'a> for webidl::ast::Operation {
} }
} }
fn simple_path_ty<I>(segments: I) -> syn::Type impl<'a> WebidlParse<&'a str> for webidl::ast::RegularAttribute {
where
I: IntoIterator<Item = Ident>,
{
let segments: Vec<_> = segments
.into_iter()
.map(|i| syn::PathSegment {
ident: i,
arguments: syn::PathArguments::None,
})
.collect();
syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: syn::punctuated::Punctuated::from_iter(segments),
},
}.into()
}
fn ident_ty(ident: Ident) -> syn::Type {
simple_path_ty(Some(ident))
}
fn shared_ref(ty: syn::Type) -> syn::Type {
syn::TypeReference {
and_token: Default::default(),
lifetime: None,
mutability: None,
elem: Box::new(ty),
}.into()
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum TypePosition {
Argument,
Return,
}
fn webidl_ty_to_syn_ty(ty: &webidl::ast::Type, pos: TypePosition) -> Option<syn::Type> {
// nullable types are not yet supported (see issue #14)
if ty.nullable {
return None;
}
Some(match ty.kind {
// `any` becomes `::wasm_bindgen::JsValue`.
webidl::ast::TypeKind::Any => {
simple_path_ty(vec![rust_ident("wasm_bindgen"), rust_ident("JsValue")])
}
// A reference to a type by name becomes the same thing in the
// bindings.
webidl::ast::TypeKind::Identifier(ref id) => ident_ty(rust_ident(id)),
// Scalars.
webidl::ast::TypeKind::Boolean => ident_ty(raw_ident("bool")),
webidl::ast::TypeKind::Byte => ident_ty(raw_ident("i8")),
webidl::ast::TypeKind::Octet => ident_ty(raw_ident("u8")),
webidl::ast::TypeKind::RestrictedDouble | webidl::ast::TypeKind::UnrestrictedDouble => {
ident_ty(raw_ident("f64"))
}
webidl::ast::TypeKind::RestrictedFloat | webidl::ast::TypeKind::UnrestrictedFloat => {
ident_ty(raw_ident("f32"))
}
webidl::ast::TypeKind::SignedLong => ident_ty(raw_ident("i32")),
webidl::ast::TypeKind::SignedLongLong => ident_ty(raw_ident("i64")),
webidl::ast::TypeKind::SignedShort => ident_ty(raw_ident("i16")),
webidl::ast::TypeKind::UnsignedLong => ident_ty(raw_ident("u32")),
webidl::ast::TypeKind::UnsignedLongLong => ident_ty(raw_ident("u64")),
webidl::ast::TypeKind::UnsignedShort => ident_ty(raw_ident("u16")),
// `DOMString -> `&str` for arguments
webidl::ast::TypeKind::DOMString if pos == TypePosition::Argument => {
shared_ref(ident_ty(raw_ident("str")))
}
// `DOMString` is not supported yet in other positions.
webidl::ast::TypeKind::DOMString => return None,
// Support for these types is not yet implemented, so skip
// generating any bindings for this function.
webidl::ast::TypeKind::ArrayBuffer
| webidl::ast::TypeKind::ByteString
| webidl::ast::TypeKind::DataView
| webidl::ast::TypeKind::Error
| webidl::ast::TypeKind::Float32Array
| webidl::ast::TypeKind::Float64Array
| webidl::ast::TypeKind::FrozenArray(_)
| webidl::ast::TypeKind::Int16Array
| webidl::ast::TypeKind::Int32Array
| webidl::ast::TypeKind::Int8Array
| webidl::ast::TypeKind::Object
| webidl::ast::TypeKind::Promise(_)
| webidl::ast::TypeKind::Record(..)
| webidl::ast::TypeKind::Sequence(_)
| webidl::ast::TypeKind::Symbol
| webidl::ast::TypeKind::USVString
| webidl::ast::TypeKind::Uint16Array
| webidl::ast::TypeKind::Uint32Array
| webidl::ast::TypeKind::Uint8Array
| webidl::ast::TypeKind::Uint8ClampedArray
| webidl::ast::TypeKind::Union(_) => {
return None;
}
})
}
fn simple_fn_arg(ident: Ident, ty: syn::Type) -> syn::FnArg {
syn::FnArg::Captured(syn::ArgCaptured {
pat: syn::Pat::Ident(syn::PatIdent {
by_ref: None,
mutability: None,
ident,
subpat: None,
}),
colon_token: Default::default(),
ty,
})
}
impl<'a> WebidlParse<'a> for webidl::ast::RegularAttribute {
type Extra = &'a str;
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> { fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
fn create_getter( fn create_getter(
this: &webidl::ast::RegularAttribute, this: &webidl::ast::RegularAttribute,
self_name: &str, self_name: &str,
) -> Option<backend::ast::Import> { ) -> Option<backend::ast::Import> {
let name = raw_ident(&this.name); let ret = match webidl_ty_to_syn_ty(&this.type_, TypePosition::Return) {
let rust_name = rust_ident(&this.name.to_snake_case());
let (output, ret) = match webidl_ty_to_syn_ty(&this.type_, TypePosition::Return) {
None => { None => {
warn!("Attribute's type does not yet support reading: {:?}. Skipping getter binding for {:?}", warn!("Attribute's type does not yet support reading: {:?}. Skipping getter binding for {:?}",
this.type_, this); this.type_, this);
return None; return None;
} }
Some(ty) => ( Some(ty) => Some(ty),
syn::ReturnType::Type(Default::default(), Box::new(ty.clone())),
Some(ty),
),
}; };
let self_ty = ident_ty(rust_ident(self_name)); let kind = backend::ast::ImportFunctionKind::Method {
let self_ref_ty = shared_ref(self_ty.clone()); class: self_name.to_string(),
ty: ident_ty(rust_ident(self_name)),
};
let shim = rust_ident(&format!("__wbg_f_{}_{}_{}", name, rust_name, self_name)); create_function(
&this.name,
Some(backend::ast::Import { iter::empty(),
kind,
ret,
vec![backend::ast::BindgenAttr::Getter(Some(raw_ident(
&this.name,
)))],
).map(|function| backend::ast::Import {
module: None, module: None,
version: None, version: None,
js_namespace: None, js_namespace: None,
kind: backend::ast::ImportKind::Function(backend::ast::ImportFunction { kind: backend::ast::ImportKind::Function(function),
function: backend::ast::Function {
name: name.clone(),
arguments: vec![self_ref_ty.clone()],
ret,
opts: backend::ast::BindgenAttrs {
attrs: vec![
backend::ast::BindgenAttr::Method,
backend::ast::BindgenAttr::Getter(Some(name.clone())),
],
},
rust_attrs: vec![],
rust_decl: Box::new(syn::FnDecl {
fn_token: Default::default(),
generics: Default::default(),
paren_token: Default::default(),
inputs: syn::punctuated::Punctuated::from_iter(vec![simple_fn_arg(
raw_ident("self_"),
self_ref_ty,
)]),
variadic: None,
output,
}),
rust_vis: syn::Visibility::Public(syn::VisPublic {
pub_token: Default::default(),
}),
},
rust_name,
kind: backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: self_ty,
},
shim,
}),
}) })
} }
@ -454,65 +259,24 @@ impl<'a> WebidlParse<'a> for webidl::ast::RegularAttribute {
this: &webidl::ast::RegularAttribute, this: &webidl::ast::RegularAttribute,
self_name: &str, self_name: &str,
) -> Option<backend::ast::Import> { ) -> Option<backend::ast::Import> {
let name = raw_ident(&this.name); let kind = backend::ast::ImportFunctionKind::Method {
let rust_attr_name = rust_ident(&this.name.to_snake_case()); class: self_name.to_string(),
let rust_name = rust_ident(&format!("set_{}", rust_attr_name)); ty: ident_ty(rust_ident(self_name)),
let self_ty = ident_ty(rust_ident(self_name));
let (inputs, arguments) = match webidl_ty_to_syn_ty(&this.type_, TypePosition::Argument)
{
None => {
warn!("Attribute's type does not yet support writing: {:?}. Skipping setter binding for {:?}",
this.type_, this);
return None;
}
Some(ty) => {
let self_ref_ty = shared_ref(self_ty.clone());
let mut inputs = syn::punctuated::Punctuated::new();
inputs.push(simple_fn_arg(raw_ident("self_"), self_ref_ty.clone()));
inputs.push(simple_fn_arg(rust_attr_name, ty.clone()));
(inputs, vec![self_ref_ty, ty])
}
}; };
let shim = rust_ident(&format!("__wbg_f_{}_{}_{}", name, rust_name, self_name)); create_function(
&format!("set_{}", this.name.to_camel_case()),
Some(backend::ast::Import { iter::once((&*this.name, &*this.type_, false)),
kind,
None,
vec![backend::ast::BindgenAttr::Setter(Some(raw_ident(
&this.name,
)))],
).map(|function| backend::ast::Import {
module: None, module: None,
version: None, version: None,
js_namespace: None, js_namespace: None,
kind: backend::ast::ImportKind::Function(backend::ast::ImportFunction { kind: backend::ast::ImportKind::Function(function),
function: backend::ast::Function {
name: name.clone(),
arguments,
ret: None,
opts: backend::ast::BindgenAttrs {
attrs: vec![
backend::ast::BindgenAttr::Method,
backend::ast::BindgenAttr::Setter(Some(name)),
],
},
rust_attrs: vec![],
rust_decl: Box::new(syn::FnDecl {
fn_token: Default::default(),
generics: Default::default(),
paren_token: Default::default(),
inputs,
variadic: None,
output: syn::ReturnType::Default,
}),
rust_vis: syn::Visibility::Public(syn::VisPublic {
pub_token: Default::default(),
}),
},
rust_name,
kind: backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: self_ty,
},
shim,
}),
}) })
} }
@ -526,9 +290,7 @@ impl<'a> WebidlParse<'a> for webidl::ast::RegularAttribute {
} }
} }
impl<'a> WebidlParse<'a> for webidl::ast::RegularOperation { impl<'a> WebidlParse<&'a str> for webidl::ast::RegularOperation {
type Extra = &'a str;
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> { fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
let name = match self.name { let name = match self.name {
None => { None => {
@ -541,11 +303,13 @@ impl<'a> WebidlParse<'a> for webidl::ast::RegularOperation {
Some(ref name) => name, Some(ref name) => name,
}; };
let rust_name = rust_ident(&name.to_snake_case()); let kind = backend::ast::ImportFunctionKind::Method {
let name = raw_ident(name); class: self_name.to_string(),
ty: ident_ty(rust_ident(self_name)),
};
let (output, ret) = match self.return_type { let ret = match self.return_type {
webidl::ast::ReturnType::Void => (syn::ReturnType::Default, None), webidl::ast::ReturnType::Void => None,
webidl::ast::ReturnType::NonVoid(ref ty) => { webidl::ast::ReturnType::NonVoid(ref ty) => {
match webidl_ty_to_syn_ty(ty, TypePosition::Return) { match webidl_ty_to_syn_ty(ty, TypePosition::Return) {
None => { None => {
@ -555,80 +319,26 @@ impl<'a> WebidlParse<'a> for webidl::ast::RegularOperation {
); );
return Ok(()); return Ok(());
} }
Some(ty) => ( Some(ty) => Some(ty),
syn::ReturnType::Type(Default::default(), Box::new(ty.clone())),
Some(ty),
),
} }
} }
}; };
let mut inputs = Vec::with_capacity(self.arguments.len() + 1); create_function(
let mut arguments = Vec::with_capacity(self.arguments.len() + 1); &name,
self.arguments
let self_ty = ident_ty(rust_ident(self_name)); .iter()
let self_ref_ty = shared_ref(self_ty.clone()); .map(|arg| (&*arg.name, &*arg.type_, arg.variadic)),
inputs.push(simple_fn_arg(raw_ident("self_"), self_ref_ty.clone())); kind,
arguments.push(self_ref_ty); ret,
Vec::new(),
for arg in &self.arguments { ).map(|function| {
if arg.variadic { program.imports.push(backend::ast::Import {
warn!( module: None,
"Variadic arguments are not supported yet. Skipping bindings for {:?}", version: None,
self js_namespace: None,
); kind: backend::ast::ImportKind::Function(function),
return Ok(()); })
}
match webidl_ty_to_syn_ty(&arg.type_, TypePosition::Argument) {
None => {
warn!(
"Argument's type is not yet supported: {:?}. Skipping bindings for {:?}",
arg.type_, self
);
return Ok(());
}
Some(ty) => {
inputs.push(simple_fn_arg(rust_ident(&arg.name), ty.clone()));
arguments.push(ty);
}
}
}
let shim = rust_ident(&format!("__wbg_f_{}_{}_{}", name, rust_name, self_name));
program.imports.push(backend::ast::Import {
module: None,
version: None,
js_namespace: None,
kind: backend::ast::ImportKind::Function(backend::ast::ImportFunction {
function: backend::ast::Function {
name,
arguments,
ret,
opts: backend::ast::BindgenAttrs {
attrs: vec![backend::ast::BindgenAttr::Method],
},
rust_attrs: vec![],
rust_decl: Box::new(syn::FnDecl {
fn_token: Default::default(),
generics: Default::default(),
paren_token: Default::default(),
inputs: syn::punctuated::Punctuated::from_iter(inputs),
variadic: None,
output,
}),
rust_vis: syn::Visibility::Public(syn::VisPublic {
pub_token: Default::default(),
}),
},
rust_name,
kind: backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: self_ty,
},
shim,
}),
}); });
Ok(()) Ok(())

241
crates/webidl/src/util.rs Normal file
View File

@ -0,0 +1,241 @@
use std::iter::FromIterator;
use backend;
use heck::SnakeCase;
use proc_macro2::{self, Ident};
use syn;
use webidl;
fn is_rust_keyword(name: &str) -> bool {
match name {
"abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue"
| "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if"
| "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut"
| "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | "return"
| "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | "true"
| "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | "while"
| "yield" | "bool" | "_" => true,
_ => false,
}
}
// Create an `Ident`, possibly mangling it if it conflicts with a Rust keyword.
pub fn rust_ident(name: &str) -> Ident {
if is_rust_keyword(name) {
Ident::new(&format!("{}_", name), proc_macro2::Span::call_site())
} else {
raw_ident(name)
}
}
// Create an `Ident` without checking to see if it conflicts with a Rust
// keyword.
pub fn raw_ident(name: &str) -> Ident {
Ident::new(name, proc_macro2::Span::call_site())
}
fn simple_path_ty<I>(segments: I) -> syn::Type
where
I: IntoIterator<Item = Ident>,
{
let segments: Vec<_> = segments
.into_iter()
.map(|i| syn::PathSegment {
ident: i,
arguments: syn::PathArguments::None,
})
.collect();
syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: syn::punctuated::Punctuated::from_iter(segments),
},
}.into()
}
pub fn ident_ty(ident: Ident) -> syn::Type {
simple_path_ty(Some(ident))
}
fn shared_ref(ty: syn::Type) -> syn::Type {
syn::TypeReference {
and_token: Default::default(),
lifetime: None,
mutability: None,
elem: Box::new(ty),
}.into()
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TypePosition {
Argument,
Return,
}
pub fn webidl_ty_to_syn_ty(ty: &webidl::ast::Type, pos: TypePosition) -> Option<syn::Type> {
// nullable types are not yet supported (see issue #14)
if ty.nullable {
return None;
}
Some(match ty.kind {
// `any` becomes `::wasm_bindgen::JsValue`.
webidl::ast::TypeKind::Any => {
simple_path_ty(vec![rust_ident("wasm_bindgen"), rust_ident("JsValue")])
}
// A reference to a type by name becomes the same thing in the
// bindings.
webidl::ast::TypeKind::Identifier(ref id) => ident_ty(rust_ident(id)),
// Scalars.
webidl::ast::TypeKind::Boolean => ident_ty(raw_ident("bool")),
webidl::ast::TypeKind::Byte => ident_ty(raw_ident("i8")),
webidl::ast::TypeKind::Octet => ident_ty(raw_ident("u8")),
webidl::ast::TypeKind::RestrictedDouble | webidl::ast::TypeKind::UnrestrictedDouble => {
ident_ty(raw_ident("f64"))
}
webidl::ast::TypeKind::RestrictedFloat | webidl::ast::TypeKind::UnrestrictedFloat => {
ident_ty(raw_ident("f32"))
}
webidl::ast::TypeKind::SignedLong => ident_ty(raw_ident("i32")),
webidl::ast::TypeKind::SignedLongLong => ident_ty(raw_ident("i64")),
webidl::ast::TypeKind::SignedShort => ident_ty(raw_ident("i16")),
webidl::ast::TypeKind::UnsignedLong => ident_ty(raw_ident("u32")),
webidl::ast::TypeKind::UnsignedLongLong => ident_ty(raw_ident("u64")),
webidl::ast::TypeKind::UnsignedShort => ident_ty(raw_ident("u16")),
// `DOMString -> `&str` for arguments
webidl::ast::TypeKind::DOMString if pos == TypePosition::Argument => {
shared_ref(ident_ty(raw_ident("str")))
}
// `DOMString` is not supported yet in other positions.
webidl::ast::TypeKind::DOMString => return None,
// Support for these types is not yet implemented, so skip
// generating any bindings for this function.
webidl::ast::TypeKind::ArrayBuffer
| webidl::ast::TypeKind::ByteString
| webidl::ast::TypeKind::DataView
| webidl::ast::TypeKind::Error
| webidl::ast::TypeKind::Float32Array
| webidl::ast::TypeKind::Float64Array
| webidl::ast::TypeKind::FrozenArray(_)
| webidl::ast::TypeKind::Int16Array
| webidl::ast::TypeKind::Int32Array
| webidl::ast::TypeKind::Int8Array
| webidl::ast::TypeKind::Object
| webidl::ast::TypeKind::Promise(_)
| webidl::ast::TypeKind::Record(..)
| webidl::ast::TypeKind::Sequence(_)
| webidl::ast::TypeKind::Symbol
| webidl::ast::TypeKind::USVString
| webidl::ast::TypeKind::Uint16Array
| webidl::ast::TypeKind::Uint32Array
| webidl::ast::TypeKind::Uint8Array
| webidl::ast::TypeKind::Uint8ClampedArray
| webidl::ast::TypeKind::Union(_) => {
return None;
}
})
}
fn simple_fn_arg(ident: Ident, ty: syn::Type) -> syn::ArgCaptured {
syn::ArgCaptured {
pat: syn::Pat::Ident(syn::PatIdent {
by_ref: None,
mutability: None,
ident,
subpat: None,
}),
colon_token: Default::default(),
ty,
}
}
fn webidl_arguments_to_syn_arg_captured<'a, I>(
arguments: I,
kind: &backend::ast::ImportFunctionKind,
) -> Option<Vec<syn::ArgCaptured>>
where
I: Iterator<Item = (&'a str, &'a webidl::ast::Type, bool)>,
{
let estimate = arguments.size_hint();
let len = estimate.1.unwrap_or(estimate.0);
let mut res = if let backend::ast::ImportFunctionKind::Method { ty, .. } = kind {
let mut res = Vec::with_capacity(len + 1);
res.push(simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone())));
res
} else {
Vec::with_capacity(len)
};
for (name, ty, variadic) in arguments {
if variadic {
warn!("Variadic arguments are not supported yet",);
return None;
}
match webidl_ty_to_syn_ty(ty, TypePosition::Argument) {
None => {
warn!("Argument's type is not yet supported: {:?}", ty);
return None;
}
Some(ty) => res.push(simple_fn_arg(rust_ident(&name.to_snake_case()), ty)),
}
}
Some(res)
}
pub fn create_function<'a, 'b, I>(
name: &str,
arguments: I,
kind: backend::ast::ImportFunctionKind,
ret: Option<syn::Type>,
mut attrs: Vec<backend::ast::BindgenAttr>,
) -> Option<backend::ast::ImportFunction>
where
I: Iterator<Item = (&'a str, &'a webidl::ast::Type, bool)>,
{
let rust_name = rust_ident(&name.to_snake_case());
let name = raw_ident(name);
let arguments = webidl_arguments_to_syn_arg_captured(arguments, &kind)?;
let js_ret = ret.clone();
if let backend::ast::ImportFunctionKind::Method { .. } = kind {
attrs.push(backend::ast::BindgenAttr::Method);
}
let opts = backend::ast::BindgenAttrs { attrs };
let shim = {
let ns = match kind {
backend::ast::ImportFunctionKind::Normal => "",
backend::ast::ImportFunctionKind::Method { ref class, .. } => class,
backend::ast::ImportFunctionKind::JsConstructor { ref class, .. } => class,
};
raw_ident(&format!("__widl_f_{}_{}", rust_name, ns))
};
Some(backend::ast::ImportFunction {
function: backend::ast::Function {
name,
arguments,
ret,
opts,
rust_attrs: vec![],
rust_vis: syn::Visibility::Public(syn::VisPublic {
pub_token: Default::default(),
}),
},
rust_name,
js_ret,
kind,
shim,
})
}

File diff suppressed because one or more lines are too long