2023-12-02 07:46:57 +00:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Luau/Config.h"
|
|
|
|
|
2024-11-15 22:29:30 +00:00
|
|
|
#include <functional>
|
2023-12-02 07:46:57 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
class RequireResolver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class ModuleStatus
|
|
|
|
{
|
|
|
|
Cached,
|
|
|
|
FileRead,
|
2024-11-15 22:29:30 +00:00
|
|
|
ErrorReported
|
2023-12-02 07:46:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ResolvedRequire
|
|
|
|
{
|
|
|
|
ModuleStatus status;
|
2024-11-15 22:29:30 +00:00
|
|
|
std::string identifier;
|
2023-12-02 07:46:57 +00:00
|
|
|
std::string absolutePath;
|
|
|
|
std::string sourceCode;
|
|
|
|
};
|
|
|
|
|
2024-11-15 22:29:30 +00:00
|
|
|
struct RequireContext
|
|
|
|
{
|
|
|
|
virtual ~RequireContext() = default;
|
|
|
|
virtual std::string getPath() = 0;
|
|
|
|
virtual bool isRequireAllowed() = 0;
|
|
|
|
virtual bool isStdin() = 0;
|
|
|
|
virtual std::string createNewIdentifer(const std::string& path) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CacheManager
|
|
|
|
{
|
|
|
|
virtual ~CacheManager() = default;
|
|
|
|
virtual bool isCached(const std::string& path)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ErrorHandler
|
|
|
|
{
|
|
|
|
virtual ~ErrorHandler() = default;
|
|
|
|
virtual void reportError(const std::string message) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
RequireResolver(std::string pathToResolve, RequireContext& requireContext, CacheManager& cacheManager, ErrorHandler& errorHandler);
|
|
|
|
|
|
|
|
[[nodiscard]] ResolvedRequire resolveRequire(std::function<void(const ModuleStatus)> completionCallback = nullptr);
|
2023-12-02 07:46:57 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string pathToResolve;
|
|
|
|
|
2024-11-15 22:29:30 +00:00
|
|
|
RequireContext& requireContext;
|
|
|
|
CacheManager& cacheManager;
|
|
|
|
ErrorHandler& errorHandler;
|
|
|
|
|
|
|
|
ResolvedRequire resolvedRequire;
|
|
|
|
bool isRequireResolved = false;
|
2023-12-02 07:46:57 +00:00
|
|
|
|
|
|
|
Luau::Config config;
|
|
|
|
std::string lastSearchedDir;
|
|
|
|
bool isConfigFullyResolved = false;
|
|
|
|
|
2024-11-15 22:29:30 +00:00
|
|
|
[[nodiscard]] bool initialize();
|
2023-12-02 07:46:57 +00:00
|
|
|
|
2024-11-15 22:29:30 +00:00
|
|
|
ModuleStatus findModule();
|
2023-12-02 07:46:57 +00:00
|
|
|
ModuleStatus findModuleImpl();
|
|
|
|
|
2024-11-15 22:29:30 +00:00
|
|
|
[[nodiscard]] bool resolveAndStoreDefaultPaths();
|
2023-12-02 07:46:57 +00:00
|
|
|
std::optional<std::string> getRequiringContextAbsolute();
|
|
|
|
std::string getRequiringContextRelative();
|
|
|
|
|
2024-11-15 22:29:30 +00:00
|
|
|
[[nodiscard]] bool substituteAliasIfPresent(std::string& path);
|
2023-12-02 07:46:57 +00:00
|
|
|
std::optional<std::string> getAlias(std::string alias);
|
|
|
|
|
2024-11-15 22:29:30 +00:00
|
|
|
[[nodiscard]] bool parseNextConfig();
|
|
|
|
[[nodiscard]] bool parseConfigInDirectory(const std::string& directory);
|
|
|
|
};
|