Normalize module-path

Modules indipendant of the path, should only be evaluated once.
This commit is contained in:
phosit
2025-04-03 19:59:05 +02:00
committed by phosit
parent c6d42ebbd5
commit 32ef4fd0fa
4 changed files with 21 additions and 9 deletions
@@ -0,0 +1 @@
log("blah blah blah");
+1 -8
View File
@@ -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 <algorithm>
#include <cstring>
#if OS_WIN
#include <filesystem>
#endif
#include <functional>
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
{
+2 -1
View File
@@ -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;
}
@@ -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");
}
}
};