Support wildcard arguments in foreign functions (#351)

No real reason to not support them!

Closes #346
This commit is contained in:
Alex Crichton 2018-06-28 20:06:35 -05:00 committed by GitHub
parent 37293ee42a
commit 4138583dff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 6 deletions

View File

@ -573,6 +573,7 @@ impl ToTokens for ast::ImportFunction {
let mut abi_argument_names = Vec::new();
let mut abi_arguments = Vec::new();
let mut arg_conversions = Vec::new();
let mut arguments = Vec::new();
let ret_ident = Ident::new("_ret", Span::call_site());
for (i, syn::ArgCaptured { pat, ty, .. }) in self.function.arguments.iter().enumerate() {
@ -583,6 +584,9 @@ impl ToTokens for ast::ImportFunction {
subpat: None,
..
}) => ident.clone(),
syn::Pat::Wild(_) => {
syn::Ident::new(&format!("__genarg_{}", i), Span::call_site())
}
_ => panic!("unsupported pattern in foreign function"),
};
@ -593,6 +597,7 @@ impl ToTokens for ast::ImportFunction {
let var = if i == 0 && is_method {
quote! { self }
} else {
arguments.push(quote! { #name: #ty });
quote! { #name }
};
arg_conversions.push(quote! {
@ -651,12 +656,7 @@ impl ToTokens for ast::ImportFunction {
let rust_name = &self.rust_name;
let import_name = &self.shim;
let attrs = &self.function.rust_attrs;
let arguments = if is_method {
&self.function.arguments[1..]
} else {
&self.function.arguments[..]
};
let arguments = &arguments;
let me = if is_method {
quote! { &self, }

View File

@ -490,6 +490,40 @@ fn versions() {
.test();
}
#[test]
fn underscore_pattern() {
project()
.debug(false)
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn foo(_: u8);
}
#[wasm_bindgen]
pub fn run() {
foo(1);
}
"#)
.file("test.ts", r#"
import { run } from "./out";
export function foo(_a: number) {
}
export function test() {
run();
}
"#)
.test();
}
#[test]
fn rust_keyword() {
project()