mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 10:10:52 +00:00
web-sys: Don't remove dictionaries if required fields are removed
This commit updates the conditional binding generation for dictionaries to ensure that a dictionary is not entirely removed if any of its required fields are removed. If a required field is removed, however, it cannot be constructed, so the constructor is removed.
This commit is contained in:
parent
618b5d3531
commit
877c31cdc8
@ -292,6 +292,7 @@ pub enum ConstValue {
|
||||
pub struct Dictionary {
|
||||
pub name: Ident,
|
||||
pub fields: Vec<DictionaryField>,
|
||||
pub ctor: bool,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
||||
|
@ -1282,6 +1282,18 @@ impl ToTokens for ast::Dictionary {
|
||||
let required_names2 = required_names;
|
||||
let required_names3 = required_names;
|
||||
|
||||
let ctor = if self.ctor {
|
||||
quote! {
|
||||
pub fn new(#(#required_names: #required_types),*) -> #name {
|
||||
let mut _ret = #name { obj: ::js_sys::Object::new() };
|
||||
#(_ret.#required_names2(#required_names3);)*
|
||||
return _ret
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
let const_name = Ident::new(&format!("_CONST_{}", name), Span::call_site());
|
||||
(quote! {
|
||||
#[derive(Clone, Debug)]
|
||||
@ -1293,12 +1305,7 @@ impl ToTokens for ast::Dictionary {
|
||||
|
||||
#[allow(clippy::all)]
|
||||
impl #name {
|
||||
pub fn new(#(#required_names: #required_types),*) -> #name {
|
||||
let mut _ret = #name { obj: ::js_sys::Object::new() };
|
||||
#(_ret.#required_names2(#required_names3);)*
|
||||
return _ret
|
||||
}
|
||||
|
||||
#ctor
|
||||
#methods
|
||||
}
|
||||
|
||||
|
@ -348,25 +348,20 @@ impl RemoveUndefinedImports for ast::Program {
|
||||
let mut changed = self.imports.remove_undefined_imports(is_defined);
|
||||
changed = self.consts.remove_undefined_imports(is_defined) || changed;
|
||||
|
||||
let mut dictionaries_to_remove = Vec::new();
|
||||
for (i, dictionary) in self.dictionaries.iter_mut().enumerate() {
|
||||
for dictionary in self.dictionaries.iter_mut() {
|
||||
let num_required =
|
||||
|dict: &ast::Dictionary| dict.fields.iter().filter(|f| f.required).count();
|
||||
let before = num_required(dictionary);
|
||||
changed = dictionary.fields.remove_undefined_imports(is_defined) || changed;
|
||||
|
||||
// If a required field was removed we can no longer construct this
|
||||
// dictionary so disable the constructor.
|
||||
if before != num_required(dictionary) {
|
||||
log::warn!(
|
||||
"removing {} due to a required field being removed",
|
||||
dictionary.name
|
||||
);
|
||||
dictionaries_to_remove.push(i);
|
||||
dictionary.ctor = false;
|
||||
}
|
||||
}
|
||||
for i in dictionaries_to_remove.iter().rev() {
|
||||
self.dictionaries.swap_remove(*i);
|
||||
}
|
||||
|
||||
changed || dictionaries_to_remove.len() > 0
|
||||
changed
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -317,6 +317,7 @@ impl<'src> FirstPassRecord<'src> {
|
||||
program.dictionaries.push(ast::Dictionary {
|
||||
name: rust_ident(&camel_case_ident(def.identifier.0)),
|
||||
fields,
|
||||
ctor: true,
|
||||
});
|
||||
}
|
||||
|
||||
@ -784,6 +785,7 @@ impl<'src> FirstPassRecord<'src> {
|
||||
program.dictionaries.push(ast::Dictionary {
|
||||
name: rust_ident(&camel_case_ident(item.definition.identifier.0)),
|
||||
fields,
|
||||
ctor: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user