mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-24 18:21:33 +00:00
Support wildcard arguments in foreign functions (#351)
No real reason to not support them! Closes #346
This commit is contained in:
@ -573,6 +573,7 @@ impl ToTokens for ast::ImportFunction {
|
|||||||
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 mut arguments = Vec::new();
|
||||||
let ret_ident = Ident::new("_ret", Span::call_site());
|
let ret_ident = Ident::new("_ret", Span::call_site());
|
||||||
|
|
||||||
for (i, syn::ArgCaptured { pat, ty, .. }) in self.function.arguments.iter().enumerate() {
|
for (i, syn::ArgCaptured { pat, ty, .. }) in self.function.arguments.iter().enumerate() {
|
||||||
@ -583,6 +584,9 @@ impl ToTokens for ast::ImportFunction {
|
|||||||
subpat: None,
|
subpat: None,
|
||||||
..
|
..
|
||||||
}) => ident.clone(),
|
}) => ident.clone(),
|
||||||
|
syn::Pat::Wild(_) => {
|
||||||
|
syn::Ident::new(&format!("__genarg_{}", i), Span::call_site())
|
||||||
|
}
|
||||||
_ => panic!("unsupported pattern in foreign function"),
|
_ => panic!("unsupported pattern in foreign function"),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -593,6 +597,7 @@ impl ToTokens for ast::ImportFunction {
|
|||||||
let var = if i == 0 && is_method {
|
let var = if i == 0 && is_method {
|
||||||
quote! { self }
|
quote! { self }
|
||||||
} else {
|
} else {
|
||||||
|
arguments.push(quote! { #name: #ty });
|
||||||
quote! { #name }
|
quote! { #name }
|
||||||
};
|
};
|
||||||
arg_conversions.push(quote! {
|
arg_conversions.push(quote! {
|
||||||
@ -651,12 +656,7 @@ impl ToTokens for ast::ImportFunction {
|
|||||||
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 = &arguments;
|
||||||
let arguments = if is_method {
|
|
||||||
&self.function.arguments[1..]
|
|
||||||
} else {
|
|
||||||
&self.function.arguments[..]
|
|
||||||
};
|
|
||||||
|
|
||||||
let me = if is_method {
|
let me = if is_method {
|
||||||
quote! { &self, }
|
quote! { &self, }
|
||||||
|
@ -490,6 +490,40 @@ fn versions() {
|
|||||||
.test();
|
.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]
|
#[test]
|
||||||
fn rust_keyword() {
|
fn rust_keyword() {
|
||||||
project()
|
project()
|
||||||
|
Reference in New Issue
Block a user