1
0
mirror of https://github.com/fluencelabs/wasm-bindgen synced 2025-04-03 19:01:06 +00:00

Move get_arguments_possibilities into a function

This commit is contained in:
Anton Danilkin 2018-08-10 22:17:29 +03:00
parent ce2c0ded74
commit 2c0e13a033

@ -844,27 +844,17 @@ impl<'a> GetTypeName for Type<'a> {
} }
impl<'src> FirstPassRecord<'src> { impl<'src> FirstPassRecord<'src> {
/// Use the first pass to convert webidl function arguments to rust arguments. /// Uses the first pass to convert webidl function arguments to all possible argument combinations.
///
/// `kind` is whether the function is a method, in which case we would need a `self`
/// parameter.
///
/// Returns option that contains a value if the conversion succeeds. /// Returns option that contains a value if the conversion succeeds.
/// The value is a vector of argument variants. /// The value is a vector of argument possibilities.
/// Each variant is a vector of original arguments, converted argument types and type names. /// Each possibility is a vector of tuples of converted argument (syn) types and type names
fn get_variants( fn get_arguments_possibilities(&self, arguments: &'src [Argument]) -> Option<Vec<Vec<(syn::Type, String)>>> {
&self,
arguments: &'src [Argument],
kind: &backend::ast::ImportFunctionKind,
) -> Option<Vec<Vec<(Option<&'src Argument>, (syn::ArgCaptured, Option<String>))>>>
{
let arguments_possibilities = {
fn get_argument_possibilities(record: &FirstPassRecord, argument: &Argument) -> Option<Vec<(syn::Type, String)>> { fn get_argument_possibilities(record: &FirstPassRecord, argument: &Argument) -> Option<Vec<(syn::Type, String)>> {
let single = match argument { let type_ = match argument {
Argument::Single(single) => single, Argument::Single(single) => &single.type_.type_,
Argument::Variadic(_) => return None, Argument::Variadic(variadic) => &variadic.type_,
}; };
match single.type_.type_.get_argument_possibilities(record) { match type_.get_argument_possibilities(record) {
None => { None => {
warn!("Argument's type is not yet supported: {:?}", argument); warn!("Argument's type is not yet supported: {:?}", argument);
None None
@ -911,11 +901,27 @@ impl<'src> FirstPassRecord<'src> {
arguments_possibilities = new_arguments_possibilities arguments_possibilities = new_arguments_possibilities
} }
optional_arguments_possibilities.extend(arguments_possibilities.into_iter()); optional_arguments_possibilities.extend(arguments_possibilities.into_iter());
optional_arguments_possibilities Some(optional_arguments_possibilities)
} else { } else {
vec![Vec::new()] Some(vec![Vec::new()])
} }
}; }
/// Uses the first pass to convert webidl function arguments to rust arguments.
///
/// `kind` is whether the function is a method, in which case we would need a `self`
/// parameter.
///
/// Returns option that contains a value if the conversion succeeds.
/// The value is a vector of argument variants.
/// Each variant is a vector of tuples of original arguments, converted argument types and type names.
fn get_variants(
&self,
arguments: &'src [Argument],
kind: &backend::ast::ImportFunctionKind,
) -> Option<Vec<Vec<(Option<&'src Argument>, syn::ArgCaptured, Option<String>)>>>
{
let arguments_possibilities = self.get_arguments_possibilities(arguments)?;
let mut result = Vec::new(); let mut result = Vec::new();
for arguments_possibility in arguments_possibilities { for arguments_possibility in arguments_possibilities {
let mut res = if let backend::ast::ImportFunctionKind::Method { let mut res = if let backend::ast::ImportFunctionKind::Method {
@ -931,11 +937,9 @@ impl<'src> FirstPassRecord<'src> {
res.push( res.push(
( (
None, None,
(
simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone())), simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone())),
None, None,
), ),
)
); );
res res
} else { } else {
@ -949,14 +953,12 @@ impl<'src> FirstPassRecord<'src> {
res.push( res.push(
( (
Some(argument), Some(argument),
(
simple_fn_arg( simple_fn_arg(
rust_ident(&single.identifier.0.to_snake_case()), rust_ident(&single.identifier.0.to_snake_case()),
argument_possibility.0.clone() argument_possibility.0.clone()
), ),
Some(argument_possibility.1.clone()), Some(argument_possibility.1.clone()),
), ),
)
); );
} }
result.push(res); result.push(res);
@ -1011,7 +1013,6 @@ impl<'src> FirstPassRecord<'src> {
} }
let variants = self.get_variants(arguments, &kind)?; let variants = self.get_variants(arguments, &kind)?;
let multiple_variants = variants.len() > 1;
let mut arguments_count_variants_count = BTreeMap::new(); let mut arguments_count_variants_count = BTreeMap::new();
for variant in &variants { for variant in &variants {
arguments_count_variants_count arguments_count_variants_count
@ -1020,14 +1021,11 @@ impl<'src> FirstPassRecord<'src> {
.or_insert(1usize); .or_insert(1usize);
} }
let mut result = Vec::new(); let mut result = Vec::new();
for variant in variants { for variant in &variants {
let (arguments, variant_types_variant_names): (Vec<_>, Vec<(_, _)>) = variant.into_iter().unzip(); let rust_name = if variants.len() > 1 {
let (variant_types, variant_names): (Vec<_>, Vec<_>) = variant_types_variant_names.into_iter().unzip();
let variant_names_len = variant_names.len();
let rust_name = if multiple_variants {
let mut rust_name = rust_name.clone(); let mut rust_name = rust_name.clone();
let mut first = true; let mut first = true;
for (argument, variant_name) in arguments.iter().zip(variant_names) { for (argument, _, variant_name) in variant {
if let Some(type_name) = variant_name { if let Some(type_name) = variant_name {
if first { if first {
rust_name.push_str("_using_"); rust_name.push_str("_using_");
@ -1035,7 +1033,7 @@ impl<'src> FirstPassRecord<'src> {
} else { } else {
rust_name.push_str("_and_"); rust_name.push_str("_and_");
} }
if arguments_count_variants_count[&variant_names_len] == 1 { if arguments_count_variants_count[&variant.len()] == 1 {
if let Some(argument) = argument { if let Some(argument) = argument {
let argument = match argument { let argument = match argument {
Argument::Single(single) => single, Argument::Single(single) => single,
@ -1067,7 +1065,7 @@ impl<'src> FirstPassRecord<'src> {
result.push(backend::ast::ImportFunction { result.push(backend::ast::ImportFunction {
function: backend::ast::Function { function: backend::ast::Function {
name: name.to_string(), name: name.to_string(),
arguments: variant_types, arguments: variant.iter().map(|variant| variant.1.clone()).collect(),
ret: ret.clone(), ret: ret.clone(),
rust_attrs: vec![], rust_attrs: vec![],
rust_vis: public(), rust_vis: public(),