From 32ef4fd0faa553f302322da5a5faa9b97ef90bcf Mon Sep 17 00:00:00 2001 From: phosit Date: Thu, 3 Apr 2025 19:59:05 +0200 Subject: [PATCH] Normalize module-path Modules indipendant of the path, should only be evaluated once. --- .../module/blabbermouth.js | 1 + source/lib/path.h | 9 +-------- source/scriptinterface/ModuleLoader.cpp | 3 ++- source/scriptinterface/tests/test_Module.h | 17 +++++++++++++++++ 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 binaries/data/mods/_test.scriptinterface/module/blabbermouth.js diff --git a/binaries/data/mods/_test.scriptinterface/module/blabbermouth.js b/binaries/data/mods/_test.scriptinterface/module/blabbermouth.js new file mode 100644 index 0000000000..8834d90a69 --- /dev/null +++ b/binaries/data/mods/_test.scriptinterface/module/blabbermouth.js @@ -0,0 +1 @@ +log("blah blah blah"); diff --git a/source/lib/path.h b/source/lib/path.h index aa4e22b6bd..fe18579650 100644 --- a/source/lib/path.h +++ b/source/lib/path.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2025 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -42,9 +42,7 @@ #include #include -#if OS_WIN #include -#endif #include namespace ERR @@ -130,10 +128,6 @@ public: return path.empty(); } - // TODO: This macro should be removed later when macOS supports std::filesystem. - // Currently it does in more recent SDKs, but it also causes a slowdown on - // OpenGL. See #6193. -#if OS_WIN /** * @returns a STL version of the path. */ @@ -141,7 +135,6 @@ public: { return std::filesystem::path(path); } -#endif const String& string() const { diff --git a/source/scriptinterface/ModuleLoader.cpp b/source/scriptinterface/ModuleLoader.cpp index cc86a1cdf0..73b23ae867 100644 --- a/source/scriptinterface/ModuleLoader.cpp +++ b/source/scriptinterface/ModuleLoader.cpp @@ -58,7 +58,8 @@ namespace [[nodiscard]] JSObject* CompileModule(const ScriptRequest& rq, ModuleLoader::RegistryType& registry, const VfsPath& filePath) { - const auto insertResult = registry.try_emplace(filePath, rq, filePath); + const VfsPath normalizedPath{filePath.fileSystemPath().lexically_normal().generic_string()}; + const auto insertResult = registry.try_emplace(normalizedPath, rq, normalizedPath); return std::get<1>(*std::get<0>(insertResult)).m_ModuleObject; } diff --git a/source/scriptinterface/tests/test_Module.h b/source/scriptinterface/tests/test_Module.h index 9969deed15..ad63974c4a 100644 --- a/source/scriptinterface/tests/test_Module.h +++ b/source/scriptinterface/tests/test_Module.h @@ -97,4 +97,21 @@ public: TS_ASSERT_THROWS(script.GetModuleLoader().LoadModule(rq, "nonexistent.js"), const std::runtime_error&); } + + void test_EvaluateOnce() + { + ScriptInterface script{"Test", "Test", g_ScriptContext}; + const ScriptRequest rq{script}; + + { + TestLogger logger; + script.GetModuleLoader().LoadModule(rq, "blabbermouth.js"); + TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "blah blah blah"); + } + { + TestLogger logger; + script.GetModuleLoader().LoadModule(rq, "include/../blabbermouth.js"); + TS_ASSERT_STR_NOT_CONTAINS(logger.GetOutput(), "blah blah blah"); + } + } };