From b84730738b633dec51f6a01ee0760cda9abd8fea Mon Sep 17 00:00:00 2001
From: Kenny Kerr <kenny@kennykerr.ca>
Date: Sun, 17 Mar 2024 16:47:20 -0500
Subject: [PATCH] preserve module name

---
 crates/libs/bindgen/src/rust/functions.rs  | 7 +++++--
 crates/libs/bindgen/src/rust/standalone.rs | 2 +-
 crates/libs/metadata/src/tables.rs         | 9 ++-------
 crates/tools/lib/src/lib.rs                | 3 ++-
 4 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/crates/libs/bindgen/src/rust/functions.rs b/crates/libs/bindgen/src/rust/functions.rs
index 061d209441..1f09b90437 100644
--- a/crates/libs/bindgen/src/rust/functions.rs
+++ b/crates/libs/bindgen/src/rust/functions.rs
@@ -3,7 +3,7 @@ use metadata::HasAttributes;
 
 pub fn writer(writer: &Writer, namespace: &str, def: metadata::MethodDef) -> TokenStream {
     // TODO: remove inline functions from metadata
-    if def.module_name() == "forceinline" {
+    if def.module_name() == "FORCEINLINE" {
         return quote! {};
     }
 
@@ -197,7 +197,10 @@ fn gen_win_function(writer: &Writer, namespace: &str, def: metadata::MethodDef)
 fn gen_link(writer: &Writer, namespace: &str, signature: &metadata::Signature) -> TokenStream {
     let name = signature.def.name();
     let ident = to_ident(name);
-    let library = signature.def.module_name();
+
+    // Windows libs are always produced with lowercase module names.
+    let library = if namespace.starts_with("Windows.") { signature.def.module_name().to_lowercase() } else { signature.def.module_name().to_string() };
+
     let abi = method_def_extern_abi(signature.def);
 
     let symbol = if let Some(impl_map) = signature.def.impl_map() { impl_map.import_name() } else { name };
diff --git a/crates/libs/bindgen/src/rust/standalone.rs b/crates/libs/bindgen/src/rust/standalone.rs
index 99e0f00aec..14bbd3b15c 100644
--- a/crates/libs/bindgen/src/rust/standalone.rs
+++ b/crates/libs/bindgen/src/rust/standalone.rs
@@ -92,7 +92,7 @@ pub fn standalone_imp(writer: &Writer) -> String {
     }
 
     for (function, namespace) in functions {
-        sorted.insert(&format!(".{}.{}", function.module_name(), function.name()), functions::writer(writer, namespace, function));
+        sorted.insert(&format!(".{}.{}", function.module_name().to_lowercase(), function.name()), functions::writer(writer, namespace, function));
     }
 
     for constant in constants {
diff --git a/crates/libs/metadata/src/tables.rs b/crates/libs/metadata/src/tables.rs
index 15c1bf745b..950fb95512 100644
--- a/crates/libs/metadata/src/tables.rs
+++ b/crates/libs/metadata/src/tables.rs
@@ -255,13 +255,8 @@ impl MethodDef {
         self.equal_range(1, MemberForwarded::MethodDef(*self).encode()).next()
     }
 
-    pub fn module_name(&self) -> String {
-        // TODO: riddle should always lower case the module name to avoid allocating here
-        let Some(impl_map) = self.impl_map() else {
-            return String::new();
-        };
-
-        impl_map.scope().name().to_lowercase()
+    pub fn module_name(&self) -> &'static str {
+        self.impl_map().map_or("", |map| map.scope().name())
     }
 
     pub fn signature(&self, generics: &[Type]) -> MethodDefSig {
diff --git a/crates/tools/lib/src/lib.rs b/crates/tools/lib/src/lib.rs
index 173438bedd..cf57dd83ed 100644
--- a/crates/tools/lib/src/lib.rs
+++ b/crates/tools/lib/src/lib.rs
@@ -43,7 +43,8 @@ fn combine_libraries(
             continue;
         };
 
-        let library = method.module_name();
+        // Windows libs are always produced with lower case module names.
+        let library = method.module_name().to_lowercase();
         let impl_map = method.impl_map().expect("ImplMap not found");
         let flags = impl_map.flags();
         let name = impl_map.import_name().to_string();