mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-27 12:06:14 +03:00
Merge pull request #803 from alexcrichton/fix-required
Change how filtering is done in WebIDL
This commit is contained in:
commit
2cf82bc0b3
@ -289,6 +289,9 @@ impl ImportedTypes for ast::ImportType {
|
|||||||
F: FnMut(&Ident, ImportedTypeKind),
|
F: FnMut(&Ident, ImportedTypeKind),
|
||||||
{
|
{
|
||||||
f(&self.rust_name, ImportedTypeKind::Definition);
|
f(&self.rust_name, ImportedTypeKind::Definition);
|
||||||
|
for class in self.extends.iter() {
|
||||||
|
f(class, ImportedTypeKind::Reference);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -344,10 +347,27 @@ impl RemoveUndefinedImports for ast::Program {
|
|||||||
where
|
where
|
||||||
F: Fn(&Ident) -> bool,
|
F: Fn(&Ident) -> bool,
|
||||||
{
|
{
|
||||||
let a = self.imports.remove_undefined_imports(is_defined);
|
let mut changed = self.imports.remove_undefined_imports(is_defined);
|
||||||
let b = self.consts.remove_undefined_imports(is_defined);
|
changed = self.consts.remove_undefined_imports(is_defined) || changed;
|
||||||
let c = self.dictionaries.remove_undefined_imports(is_defined);
|
|
||||||
a || b || c
|
let mut dictionaries_to_remove = Vec::new();
|
||||||
|
for (i, dictionary) in self.dictionaries.iter_mut().enumerate() {
|
||||||
|
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 before != num_required(dictionary) {
|
||||||
|
warn!("removing {} due to a required field being removed",
|
||||||
|
dictionary.name);
|
||||||
|
dictionaries_to_remove.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i in dictionaries_to_remove.iter().rev() {
|
||||||
|
self.dictionaries.swap_remove(*i);
|
||||||
|
}
|
||||||
|
|
||||||
|
changed || dictionaries_to_remove.len() > 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ mod idl_type;
|
|||||||
mod util;
|
mod util;
|
||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
use std::collections::{BTreeSet, HashSet, BTreeMap};
|
use std::collections::{BTreeSet, HashSet};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
@ -80,49 +80,47 @@ fn parse(webidl_source: &str, allowed_types: Option<&[&str]>)
|
|||||||
definitions.first_pass(&mut first_pass_record, ())?;
|
definitions.first_pass(&mut first_pass_record, ())?;
|
||||||
let mut program = Default::default();
|
let mut program = Default::default();
|
||||||
|
|
||||||
// Prune out everything in the `first_pass_record` which isn't allowed, or
|
let allowed_types = allowed_types.map(|list| {
|
||||||
// is otherwise gated from not actually being generated.
|
list.iter().cloned().collect::<HashSet<_>>()
|
||||||
if let Some(allowed_types) = allowed_types {
|
});
|
||||||
let allowed = allowed_types.iter().cloned().collect::<HashSet<_>>();
|
let filter = |name: &str| {
|
||||||
let filter = |name: &&str| {
|
match &allowed_types {
|
||||||
allowed.contains(&camel_case_ident(name)[..])
|
Some(set) => set.contains(&camel_case_ident(name)[..]),
|
||||||
};
|
None => true,
|
||||||
retain(&mut first_pass_record.enums, &filter);
|
}
|
||||||
retain(&mut first_pass_record.dictionaries, &filter);
|
};
|
||||||
retain(&mut first_pass_record.interfaces, &filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
for e in first_pass_record.enums.values() {
|
for (name, e) in first_pass_record.enums.iter() {
|
||||||
first_pass_record.append_enum(&mut program, e);
|
if filter(name) {
|
||||||
|
first_pass_record.append_enum(&mut program, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for d in first_pass_record.dictionaries.values() {
|
for (name, d) in first_pass_record.dictionaries.iter() {
|
||||||
first_pass_record.append_dictionary(&mut program, d);
|
if filter(name) {
|
||||||
|
first_pass_record.append_dictionary(&mut program, d);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (name, n) in first_pass_record.namespaces.iter() {
|
for (name, n) in first_pass_record.namespaces.iter() {
|
||||||
first_pass_record.append_ns(&mut program, name, n);
|
first_pass_record.append_ns(&mut program, name, n);
|
||||||
}
|
}
|
||||||
for (name, d) in first_pass_record.interfaces.iter() {
|
for (name, d) in first_pass_record.interfaces.iter() {
|
||||||
first_pass_record.append_interface(&mut program, name, d);
|
if filter(name) {
|
||||||
|
first_pass_record.append_interface(&mut program, name, d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prune out `extends` annotations that aren't defined as these shouldn't
|
||||||
|
// prevent the type from being usable entirely. They're just there for
|
||||||
|
// `AsRef` and such implementations.
|
||||||
|
for import in program.imports.iter_mut() {
|
||||||
|
if let backend::ast::ImportKind::Type(t) = &mut import.kind {
|
||||||
|
t.extends.retain(|n| filter(&n.to_string()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(program)
|
Ok(program)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn retain<K: Copy + Ord, V>(
|
|
||||||
map: &mut BTreeMap<K, V>,
|
|
||||||
mut filter: impl FnMut(&K) -> bool,
|
|
||||||
) {
|
|
||||||
let mut to_remove = Vec::new();
|
|
||||||
for k in map.keys() {
|
|
||||||
if !filter(k) {
|
|
||||||
to_remove.push(*k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for k in to_remove {
|
|
||||||
map.remove(&k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Compile the given WebIDL source text into Rust source text containing
|
/// Compile the given WebIDL source text into Rust source text containing
|
||||||
/// `wasm-bindgen` bindings to the things described in the WebIDL.
|
/// `wasm-bindgen` bindings to the things described in the WebIDL.
|
||||||
pub fn compile(
|
pub fn compile(
|
||||||
|
Loading…
Reference in New Issue
Block a user