From 85fd49f90afb4c23bc6d9c206aeab30fc27623be Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 27 Aug 2018 09:59:47 -0700 Subject: [PATCH] Fix importing same types in two modules/crates This'll hopefully fix fallout from 4f4da747adbe1 --- crates/cli-support/src/js/mod.rs | 14 ++++++++++++++ crates/cli-support/src/lib.rs | 2 ++ 2 files changed, 16 insertions(+) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index efedf96e1..10ce5a76e 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -24,6 +24,8 @@ pub struct Context<'a> { pub typescript: String, pub exposed_globals: HashSet<&'static str>, pub required_internal_exports: HashSet<&'static str>, + pub imported_functions: HashSet, + pub imported_statics: HashSet, pub config: &'a Bindgen, pub module: &'a mut Module, @@ -1884,6 +1886,12 @@ impl<'a, 'b> SubContext<'a, 'b> { info: &shared::Import, import: &shared::ImportStatic, ) -> Result<(), Error> { + // The same static can be imported in multiple locations, so only + // generate bindings once for it. + if !self.cx.imported_statics.insert(import.shim.clone()) { + return Ok(()) + } + // TODO: should support more types to import here let obj = self.import_name(info, &import.name)?; self.cx.expose_add_heap_object(); @@ -1911,6 +1919,12 @@ impl<'a, 'b> SubContext<'a, 'b> { return Ok(()); } + // It's possible for the same function to be imported in two locations, + // but we only want to generate one. + if !self.cx.imported_functions.insert(import.shim.clone()) { + return Ok(()); + } + let descriptor = match self.cx.describe(&import.shim) { None => return Ok(()), Some(d) => d, diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index c17337b5a..6af4ac1ae 100644 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -201,6 +201,8 @@ impl Bindgen { function_table_needed: false, interpreter: &mut instance, memory_init: None, + imported_functions: Default::default(), + imported_statics: Default::default(), }; for program in programs.iter() { js::SubContext {