mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 04:21:21 +00:00
Merge pull request #269 from ohanar/webidl_constructor
webidl: add support for (named) constructors
This commit is contained in:
@ -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,
|
||||
|
@ -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); },
|
||||
};
|
||||
|
Reference in New Issue
Block a user