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 "lua.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
#include "Luau/Config.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
class RequireResolver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string chunkname;
|
|
|
|
std::string absolutePath;
|
|
|
|
std::string sourceCode;
|
|
|
|
|
|
|
|
enum class ModuleStatus
|
|
|
|
{
|
|
|
|
Cached,
|
|
|
|
FileRead,
|
2024-10-04 19:29:55 +01:00
|
|
|
Ambiguous,
|
2023-12-02 07:46:57 +00:00
|
|
|
NotFound
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ResolvedRequire
|
|
|
|
{
|
|
|
|
ModuleStatus status;
|
|
|
|
std::string chunkName;
|
|
|
|
std::string absolutePath;
|
|
|
|
std::string sourceCode;
|
|
|
|
};
|
|
|
|
|
|
|
|
[[nodiscard]] ResolvedRequire static resolveRequire(lua_State* L, std::string path);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string pathToResolve;
|
|
|
|
std::string_view sourceChunkname;
|
|
|
|
|
|
|
|
RequireResolver(lua_State* L, std::string path);
|
|
|
|
|
|
|
|
ModuleStatus findModule();
|
|
|
|
lua_State* L;
|
|
|
|
Luau::Config config;
|
|
|
|
std::string lastSearchedDir;
|
|
|
|
bool isConfigFullyResolved = false;
|
|
|
|
|
|
|
|
bool isRequireAllowed(std::string_view sourceChunkname);
|
2024-10-04 19:29:55 +01:00
|
|
|
bool isPrefixValid();
|
2023-12-02 07:46:57 +00:00
|
|
|
|
|
|
|
void resolveAndStoreDefaultPaths();
|
|
|
|
ModuleStatus findModuleImpl();
|
2024-10-04 19:29:55 +01:00
|
|
|
bool isPathAmbiguous(const std::string& path);
|
2023-12-02 07:46:57 +00:00
|
|
|
|
|
|
|
std::optional<std::string> getRequiringContextAbsolute();
|
|
|
|
std::string getRequiringContextRelative();
|
|
|
|
|
|
|
|
void substituteAliasIfPresent(std::string& path);
|
|
|
|
std::optional<std::string> getAlias(std::string alias);
|
|
|
|
|
|
|
|
void parseNextConfig();
|
|
|
|
void parseConfigInDirectory(const std::string& path);
|
|
|
|
};
|