From 6b50ec328cd02508494620d766afd4270683b690 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bennet=20Ble=C3=9Fmann?= Date: Thu, 25 Jul 2024 18:53:36 +0200 Subject: [PATCH] move the creation of the LIBRARIES IndexMap out of the generated file - only leave the filling of the map in the generated code --- build/main.rs | 37 +++---------------------------------- src/machine/mod.rs | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 45 deletions(-) diff --git a/build/main.rs b/build/main.rs index 1f026de3..a43b9e86 100644 --- a/build/main.rs +++ b/build/main.rs @@ -58,28 +58,8 @@ fn main() { let mut libraries = File::create(dest_path).unwrap(); let lib_path = Path::new("src").join("lib"); - writeln!( - libraries, - "\ -use indexmap::IndexMap; -\ - " - ) - .unwrap(); - let constants = find_prolog_files("", &lib_path); - writeln!( - libraries, - "\ -std::thread_local!{{ - static LIBRARIES: IndexMap<&'static str, &'static str> = {{ - let mut m = IndexMap::new(); -\ - " - ) - .unwrap(); - let out_dir = std::env::var("OUT_DIR").unwrap(); let out_dir_path: &Path = out_dir.as_ref(); let manifest_dir = &std::env::var("CARGO_MANIFEST_DIR").unwrap(); @@ -104,23 +84,12 @@ std::thread_local!{{ manifest_dir_path.to_path_buf() }; + writeln!(libraries, "{{").unwrap(); for (name, lib_path) in constants { let path: PathBuf = prefix.join(lib_path); - writeln!( - libraries, - " m.insert(\"{name}\", include_str!({path:?}));" - ) - .unwrap(); + writeln!(libraries, "m.insert(\"{name}\", include_str!({path:?}));").unwrap(); } - - writeln!( - libraries, - " - m - }}; -}}" - ) - .unwrap(); + writeln!(libraries, "}}").unwrap(); let instructions_path = Path::new(&out_dir).join("instructions.rs"); let mut instructions_file = File::create(&instructions_path).unwrap(); diff --git a/src/machine/mod.rs b/src/machine/mod.rs index 33199edd..12170e39 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -119,7 +119,17 @@ fn current_dir() -> PathBuf { } mod libraries { - include!(concat!(env!("OUT_DIR"), "/libraries.rs")); + use indexmap::IndexMap; + + std::thread_local! { + static LIBRARIES: IndexMap<&'static str, &'static str> = { + let mut m = IndexMap::new(); + + include!(concat!(env!("OUT_DIR"), "/libraries.rs")); + + m + } + } pub(crate) fn contains(name: &str) -> bool { LIBRARIES.with(|libs| libs.contains_key(name)) @@ -128,16 +138,6 @@ mod libraries { pub(crate) fn get(name: &str) -> Option<&'static str> { LIBRARIES.with(|libs| libs.get(name).copied()) } - - #[cfg(test)] - std::thread_local! { - #[allow(dead_code)] - static LIBRARIES2 : IndexMap<&'static str, &'static str> = { - let mut m = IndexMap::new(); - m.insert("test", "test2"); - m - }; - } } pub static BREAK_FROM_DISPATCH_LOOP_LOC: usize = 0; -- 2.54.0