Merge pull request #269 from ohanar/webidl_constructor

webidl: add support for (named) constructors
This commit is contained in:
Nick Fitzgerald 2018-06-15 11:35:29 -07:00 committed by GitHub
commit 639ccd53ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 688 additions and 501 deletions

View File

@ -42,6 +42,7 @@ pub enum ImportKind {
pub struct ImportFunction {
pub function: Function,
pub rust_name: Ident,
pub js_ret: Option<syn::Type>,
pub kind: ImportFunctionKind,
pub shim: Ident,
}
@ -71,11 +72,10 @@ pub struct ImportType {
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
pub struct Function {
pub name: Ident,
pub arguments: Vec<syn::Type>,
pub arguments: Vec<syn::ArgCaptured>,
pub ret: Option<syn::Type>,
pub opts: BindgenAttrs,
pub rust_attrs: Vec<syn::Attribute>,
pub rust_decl: Box<syn::FnDecl>,
pub rust_vis: syn::Visibility,
}
@ -143,7 +143,8 @@ impl Program {
syn::Item::Fn(mut f) => {
let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut f.attrs));
let no_mangle = f.attrs
let no_mangle = f
.attrs
.iter()
.enumerate()
.filter_map(|(i, m)| m.interpret_meta().map(|m| (i, m)))
@ -276,7 +277,8 @@ impl Program {
_ => panic!("only public enums are allowed"),
}
let variants = item.variants
let variants = item
.variants
.iter()
.enumerate()
.map(|(i, v)| {
@ -355,8 +357,8 @@ 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).clone();
let mut wasm = Function::from_decl(&js_name, f.decl, f.attrs, opts, f.vis, false).0;
if wasm.opts.catch() {
let wasm = Function::from_decl(&js_name, f.decl, f.attrs, opts, f.vis, false).0;
let js_ret = if wasm.opts.catch() {
// TODO: this assumes a whole bunch:
//
// * The outer type is actually a `Result`
@ -364,15 +366,18 @@ impl Program {
// * 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");
}
extract_first_ty_param(wasm.ret.as_ref())
.expect("can't `catch` without returning a Result")
} else {
wasm.ret.clone()
};
let kind = if wasm.opts.method() {
let class = wasm.arguments
let class = wasm
.arguments
.get(0)
.expect("methods must have at least one argument");
let class = match *class {
let class = match class.ty {
syn::Type::Reference(syn::TypeReference {
mutability: None,
ref elem,
@ -428,6 +433,7 @@ impl Program {
ImportKind::Function(ImportFunction {
function: wasm,
kind,
js_ret,
rust_name: f.ident.clone(),
shim: Ident::new(&shim, Span::call_site()),
})
@ -511,11 +517,13 @@ impl Function {
assert_no_lifetimes(&mut decl);
let syn::FnDecl { inputs, output, .. } = { *decl };
let mut mutable = None;
let arguments = decl.inputs
.iter()
.filter_map(|arg| match *arg {
syn::FnArg::Captured(ref c) => Some(c),
let arguments = inputs
.into_iter()
.filter_map(|arg| match arg {
syn::FnArg::Captured(c) => Some(c),
syn::FnArg::SelfValue(_) => {
panic!("by-value `self` not yet supported");
}
@ -526,12 +534,11 @@ impl Function {
}
_ => panic!("arguments cannot be `self` or ignored"),
})
.map(|arg| arg.ty.clone())
.collect::<Vec<_>>();
let ret = match decl.output {
let ret = match output {
syn::ReturnType::Default => None,
syn::ReturnType::Type(_, ref t) => Some((**t).clone()),
syn::ReturnType::Type(_, ty) => Some(*ty),
};
(
@ -541,7 +548,6 @@ impl Function {
ret,
opts,
rust_vis: vis,
rust_decl: decl,
rust_attrs: attrs,
},
mutable,

View File

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

View File

@ -18,15 +18,19 @@ extern crate syn;
extern crate wasm_bindgen_backend as backend;
extern crate webidl;
use failure::ResultExt;
use heck::SnakeCase;
use proc_macro2::Ident;
use quote::ToTokens;
use std::fs;
use std::io::{self, Read};
use std::iter::FromIterator;
use std::iter;
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)`.
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()
}
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,
}
trait WebidlParse<Ctx> {
fn webidl_parse(&self, program: &mut backend::ast::Program, context: Ctx) -> Result<()>;
}
// Create an `Ident`, possibly mangling it if it conflicts with a Rust keyword.
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 = ();
impl WebidlParse<()> for Vec<webidl::ast::Definition> {
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
for def in self {
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 {
type Extra = ();
impl WebidlParse<()> for webidl::ast::Definition {
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
match *self {
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 {
type Extra = ();
impl WebidlParse<()> for webidl::ast::Interface {
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
match *self {
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 {
type Extra = ();
impl WebidlParse<()> for webidl::ast::Typedef {
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
let dest = rust_ident(&self.name);
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 {
type Extra = ();
impl WebidlParse<()> for webidl::ast::NonPartialInterface {
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
program.imports.push(backend::ast::Import {
module: None,
@ -200,6 +164,10 @@ impl<'a> WebidlParse<'a> for webidl::ast::NonPartialInterface {
}),
});
for extended_attribute in &self.extended_attributes {
extended_attribute.webidl_parse(program, self)?;
}
for member in &self.members {
member.webidl_parse(program, &self.name)?;
}
@ -208,9 +176,72 @@ impl<'a> WebidlParse<'a> for webidl::ast::NonPartialInterface {
}
}
impl<'a> WebidlParse<'a> for webidl::ast::InterfaceMember {
type Extra = &'a str;
impl<'a> WebidlParse<&'a webidl::ast::NonPartialInterface> for webidl::ast::ExtendedAttribute {
fn webidl_parse(
&self,
program: &mut backend::ast::Program,
interface: &'a webidl::ast::NonPartialInterface,
) -> Result<()> {
let mut add_constructor = |arguments: &[webidl::ast::Argument], class: &str| {
let self_ty = ident_ty(rust_ident(&interface.name));
let kind = backend::ast::ImportFunctionKind::JsConstructor {
class: class.to_string(),
ty: self_ty.clone(),
};
create_function(
"new",
arguments
.iter()
.map(|arg| (&*arg.name, &*arg.type_, arg.variadic)),
kind,
Some(self_ty),
vec![backend::ast::BindgenAttr::Constructor],
).map(|function| {
program.imports.push(backend::ast::Import {
module: None,
version: None,
js_namespace: None,
kind: backend::ast::ImportKind::Function(function),
})
})
};
match self {
webidl::ast::ExtendedAttribute::ArgumentList(
webidl::ast::ArgumentListExtendedAttribute { arguments, name },
) if name == "Constructor" =>
{
add_constructor(arguments, &interface.name);
}
webidl::ast::ExtendedAttribute::NoArguments(webidl::ast::Other::Identifier(name))
if name == "Constructor" =>
{
add_constructor(&[] as &[_], &interface.name);
}
webidl::ast::ExtendedAttribute::NamedArgumentList(
webidl::ast::NamedArgumentListExtendedAttribute {
lhs_name,
rhs_arguments,
rhs_name,
},
) if lhs_name == "NamedConstructor" =>
{
add_constructor(rhs_arguments, rhs_name);
}
webidl::ast::ExtendedAttribute::ArgumentList(_)
| webidl::ast::ExtendedAttribute::Identifier(_)
| webidl::ast::ExtendedAttribute::IdentifierList(_)
| webidl::ast::ExtendedAttribute::NamedArgumentList(_)
| webidl::ast::ExtendedAttribute::NoArguments(_) => {
warn!("Unsupported WebIDL extended attribute: {:?}", self);
}
}
Ok(())
}
}
impl<'a> WebidlParse<&'a str> for webidl::ast::InterfaceMember {
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
match *self {
webidl::ast::InterfaceMember::Attribute(ref attr) => {
@ -229,9 +260,7 @@ impl<'a> WebidlParse<'a> for webidl::ast::InterfaceMember {
}
}
impl<'a> WebidlParse<'a> for webidl::ast::Attribute {
type Extra = &'a str;
impl<'a> WebidlParse<&'a str> for webidl::ast::Attribute {
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
match *self {
webidl::ast::Attribute::Regular(ref attr) => attr.webidl_parse(program, self_name),
@ -244,9 +273,7 @@ impl<'a> WebidlParse<'a> for webidl::ast::Attribute {
}
}
impl<'a> WebidlParse<'a> for webidl::ast::Operation {
type Extra = &'a str;
impl<'a> WebidlParse<&'a str> for webidl::ast::Operation {
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
match *self {
webidl::ast::Operation::Regular(ref op) => op.webidl_parse(program, self_name),
@ -261,192 +288,39 @@ impl<'a> WebidlParse<'a> for webidl::ast::Operation {
}
}
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()
}
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;
impl<'a> WebidlParse<&'a str> for webidl::ast::RegularAttribute {
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
fn create_getter(
this: &webidl::ast::RegularAttribute,
self_name: &str,
) -> Option<backend::ast::Import> {
let name = raw_ident(&this.name);
let rust_name = rust_ident(&this.name.to_snake_case());
let (output, ret) = match webidl_ty_to_syn_ty(&this.type_, TypePosition::Return) {
let ret = match webidl_ty_to_syn_ty(&this.type_, TypePosition::Return) {
None => {
warn!("Attribute's type does not yet support reading: {:?}. Skipping getter binding for {:?}",
this.type_, this);
return None;
}
Some(ty) => (
syn::ReturnType::Type(Default::default(), Box::new(ty.clone())),
Some(ty),
),
Some(ty) => Some(ty),
};
let self_ty = ident_ty(rust_ident(self_name));
let self_ref_ty = shared_ref(self_ty.clone());
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(self_name)),
};
let shim = rust_ident(&format!("__wbg_f_{}_{}_{}", name, rust_name, self_name));
Some(backend::ast::Import {
create_function(
&this.name,
iter::empty(),
kind,
ret,
vec![backend::ast::BindgenAttr::Getter(Some(raw_ident(
&this.name,
)))],
).map(|function| backend::ast::Import {
module: None,
version: None,
js_namespace: None,
kind: backend::ast::ImportKind::Function(backend::ast::ImportFunction {
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,
}),
kind: backend::ast::ImportKind::Function(function),
})
}
@ -454,65 +328,24 @@ impl<'a> WebidlParse<'a> for webidl::ast::RegularAttribute {
this: &webidl::ast::RegularAttribute,
self_name: &str,
) -> Option<backend::ast::Import> {
let name = raw_ident(&this.name);
let rust_attr_name = rust_ident(&this.name.to_snake_case());
let rust_name = rust_ident(&format!("set_{}", rust_attr_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 kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(self_name)),
};
let shim = rust_ident(&format!("__wbg_f_{}_{}_{}", name, rust_name, self_name));
Some(backend::ast::Import {
create_function(
&format!("set_{}", this.name.to_camel_case()),
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,
version: None,
js_namespace: None,
kind: backend::ast::ImportKind::Function(backend::ast::ImportFunction {
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,
}),
kind: backend::ast::ImportKind::Function(function),
})
}
@ -526,9 +359,7 @@ impl<'a> WebidlParse<'a> for webidl::ast::RegularAttribute {
}
}
impl<'a> WebidlParse<'a> for webidl::ast::RegularOperation {
type Extra = &'a str;
impl<'a> WebidlParse<&'a str> for webidl::ast::RegularOperation {
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
let name = match self.name {
None => {
@ -541,11 +372,13 @@ impl<'a> WebidlParse<'a> for webidl::ast::RegularOperation {
Some(ref name) => name,
};
let rust_name = rust_ident(&name.to_snake_case());
let name = raw_ident(name);
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(self_name)),
};
let (output, ret) = match self.return_type {
webidl::ast::ReturnType::Void => (syn::ReturnType::Default, None),
let ret = match self.return_type {
webidl::ast::ReturnType::Void => None,
webidl::ast::ReturnType::NonVoid(ref ty) => {
match webidl_ty_to_syn_ty(ty, TypePosition::Return) {
None => {
@ -555,80 +388,26 @@ impl<'a> WebidlParse<'a> for webidl::ast::RegularOperation {
);
return Ok(());
}
Some(ty) => (
syn::ReturnType::Type(Default::default(), Box::new(ty.clone())),
Some(ty),
),
Some(ty) => Some(ty),
}
}
};
let mut inputs = Vec::with_capacity(self.arguments.len() + 1);
let mut arguments = Vec::with_capacity(self.arguments.len() + 1);
let self_ty = ident_ty(rust_ident(self_name));
let self_ref_ty = shared_ref(self_ty.clone());
inputs.push(simple_fn_arg(raw_ident("self_"), self_ref_ty.clone()));
arguments.push(self_ref_ty);
for arg in &self.arguments {
if arg.variadic {
warn!(
"Variadic arguments are not supported yet. Skipping bindings for {:?}",
self
);
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,
}),
create_function(
&name,
self.arguments
.iter()
.map(|arg| (&*arg.name, &*arg.type_, arg.variadic)),
kind,
ret,
Vec::new(),
).map(|function| {
program.imports.push(backend::ast::Import {
module: None,
version: None,
js_namespace: None,
kind: backend::ast::ImportKind::Function(function),
})
});
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

View File

@ -13,6 +13,9 @@
// TODO: don't include this here, use Performance.webidl instead
typedef double DOMHighResTimeStamp;
// TODO: remove this when dicts are resolved
typedef boolean EventInit;
[Constructor(DOMString type, optional EventInit eventInitDict),
Exposed=(Window,Worker,System), ProbablyShortLivingWrapper]
interface Event {
@ -60,3 +63,9 @@ interface Event {
optional boolean cancelable = false);
attribute boolean cancelBubble;
};
dictionary EventInit {
boolean bubbles = false;
boolean cancelable = false;
boolean composed = false;
};

View File

@ -1,15 +1,15 @@
use super::project;
#[test]
fn webidl() {
fn method() {
project()
.file(
"foo.webidl",
r#"
[Constructor(float value)]
[Constructor(double value)]
interface Foo {
[Pure]
boolean my_cmp(Foo bar);
boolean myCmp(Foo bar);
};
"#,
)
@ -17,11 +17,10 @@ fn webidl() {
"foo.ts",
r#"
export class Foo {
value: number;
constructor(value: number) {
constructor(private value: number) {
this.value = value;
}
my_cmp(other: Foo): boolean {
myCmp(other: Foo): boolean {
return this.value === other.value;
}
}
@ -38,26 +37,140 @@ fn webidl() {
pub mod foo;
#[wasm_bindgen]
pub fn call_my_cmp(first: &foo::Foo, second: foo::Foo) -> bool {
first.my_cmp(second)
}
"#,
)
.file(
"test.ts",
r#"
import * as assert from 'assert';
import * as wasm from './out';
import {Foo} from './foo';
use foo::Foo;
export function test() {
const pi = new Foo(3.14159);
const e = new Foo(2.71828);
assert.strictEqual(wasm.call_my_cmp(pi, pi), true);
assert.strictEqual(wasm.call_my_cmp(pi, e), false);
assert.strictEqual(wasm.call_my_cmp(e, pi), false);
assert.strictEqual(wasm.call_my_cmp(e, e), true);
#[wasm_bindgen]
pub fn test() {
let pi = Foo::new(3.14159);
let e = Foo::new(2.71828);
let tmp = pi.my_cmp(Foo::new(3.14159));
assert!(tmp);
let tmp =!pi.my_cmp(Foo::new(2.71828));
assert!(tmp);
let tmp = !e.my_cmp(Foo::new(3.14159));
assert!(tmp);
let tmp = e.my_cmp(Foo::new(2.71828));
assert!(tmp);
}
"#,
)
.test();
}
#[test]
fn property() {
project()
.file(
"foo.webidl",
r#"
[Constructor(double value)]
interface Foo {
[Pure]
attribute double value;
};
"#,
)
.file(
"foo.ts",
r#"
export class Foo {
constructor(private _value: number) {
this._value = _value;
}
get value(): number {
return this._value;
}
set value(_value: number) {
this._value = _value;
}
}
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod foo;
use foo::Foo;
#[wasm_bindgen]
pub fn test() {
let x = Foo::new(3.14159);
let tmp = x.value() == 3.14159;
assert!(tmp);
let tmp = x.value() != 2.71828;
assert!(tmp);
x.set_value(2.71828);
let tmp = x.value() != 3.14159;
assert!(tmp);
let tmp = x.value() == 2.71828;
assert!(tmp);
}
"#,
)
.test();
}
#[test]
fn named_constructor() {
project()
.file(
"foo.webidl",
r#"
[NamedConstructor=Bar(double value)]
interface Foo {
[Pure]
readonly attribute double value;
};
"#,
)
.file(
// Not a perfect test, but it gets the job done.
"foo.ts",
r#"
export class Foo {
protected _value: number = 0;
get value(): number {
return this._value;
}
}
export class Bar extends Foo {
constructor(_value: number) {
super();
this._value = _value;
}
}
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod foo;
use foo::Foo;
#[wasm_bindgen]
pub fn test() {
let x = Foo::new(3.14159);
let tmp = x.value() == 3.14159;
assert!(tmp);
let tmp = x.value() != 0.;
assert!(tmp);
}
"#,
)