luau/Require/Runtime/src/Navigation.cpp
Hunter Goldstein 92cce5776c
Sync to upstream/release/674 (#1832)
# General

* Expose an optional `get_alias` API as an alternative to `get_config`
in Luau.Require and Luau.RequireNavigator.
* Improve the Luau CLI's virtual filesystem implementation to fix bugs
related to `init.luau`. Fixes
https://github.com/luau-lang/luau/issues/1816


# New Type Solver

* Avoid double reporting errors when erroneous arguments are provided to
type functions.
* Fix some instances of unresovable cyclic type functions in loops by
only considering the first loop cycles. This results in some type
inference inaccuracies when the type of a variable in loop through
multiple iterations. Fixes
https://github.com/luau-lang/luau/issues/1413.
* Better generalize free types that have meaningful lower and upper
bounds, especially for table indexers.
* Report more specific errors when assigning or returning table literal
types, instead of citing the *entire* table type.
* Inference for functions with generic type packs is greatly improved.
* Fix some internal compiler exceptions when using type-stating
functions like `table.freeze` in `if _ then _ else _` expressions and
short circuiting binary operations.
* More consistently simplify unions of primitive types, especially in
array-like and dictionary-like tables.
* Fix a crash when type checking an erroneous type alias containing
`typeof` with a type assertion expression, as in:
  ```
  type MyTable = {}
  -- This will error at type checking time as it's a duplicate
  type MyTable = typeof(setmetatable(SomeTable :: {}, SomeMetaTable));
  ```
* Fix a crash when inferring the type of an index expression where the
indexee is invalid (e.g. `nil`).

# Runtime
* Avoid throwing an exception from `luau_load` if we run out of memory.
* Type functions are no longer compiled and included in bytecode. Fixes
#1817.
* Fix some instances of Luau C API functions reading invalid debug
information (generally when the first or last instruction of a block was
being inspected). Fixes #1369.
* Avoid potential signed integer overflow when doing bounds checks on
tables.
* Support 16 byte aligned userdata objects when system allocation
alignment is also 16 bytes.
* Fix memory leaks in `Luau.Require` when using VM build with no
exceptions. Fixes #1827.

---------

Co-authored-by: Andy Friesen <afriesen@roblox.com>
Co-authored-by: Ariel Weiss <aaronweiss@roblox.com>
Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com>
Co-authored-by: James McNellis <jmcnellis@roblox.com>
Co-authored-by: Sora Kanosue <skanosue@roblox.com>
Co-authored-by: Talha Pathan <tpathan@roblox.com>
Co-authored-by: Varun Saini <vsaini@roblox.com>
Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2025-05-16 12:39:58 -07:00

168 lines
4.7 KiB
C++

// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Navigation.h"
#include "Luau/Require.h"
#include "lua.h"
#include "lualib.h"
#include <string>
static constexpr size_t initalFileBufferSize = 1024;
static constexpr size_t initalIdentifierBufferSize = 64;
namespace Luau::Require
{
static NavigationContext::NavigateResult convertNavigateResult(luarequire_NavigateResult result)
{
if (result == NAVIGATE_SUCCESS)
return NavigationContext::NavigateResult::Success;
if (result == NAVIGATE_AMBIGUOUS)
return NavigationContext::NavigateResult::Ambiguous;
return NavigationContext::NavigateResult::NotFound;
}
RuntimeNavigationContext::RuntimeNavigationContext(luarequire_Configuration* config, lua_State* L, void* ctx, std::string requirerChunkname)
: config(config)
, L(L)
, ctx(ctx)
, requirerChunkname(std::move(requirerChunkname))
{
}
std::string RuntimeNavigationContext::getRequirerIdentifier() const
{
return requirerChunkname;
}
NavigationContext::NavigateResult RuntimeNavigationContext::reset(const std::string& requirerChunkname)
{
return convertNavigateResult(config->reset(L, ctx, requirerChunkname.c_str()));
}
NavigationContext::NavigateResult RuntimeNavigationContext::jumpToAlias(const std::string& path)
{
return convertNavigateResult(config->jump_to_alias(L, ctx, path.c_str()));
}
NavigationContext::NavigateResult RuntimeNavigationContext::toParent()
{
return convertNavigateResult(config->to_parent(L, ctx));
}
NavigationContext::NavigateResult RuntimeNavigationContext::toChild(const std::string& component)
{
return convertNavigateResult(config->to_child(L, ctx, component.c_str()));
}
bool RuntimeNavigationContext::isModulePresent() const
{
return config->is_module_present(L, ctx);
}
std::optional<std::string> RuntimeNavigationContext::getChunkname() const
{
return getStringFromCWriter(config->get_chunkname, initalIdentifierBufferSize);
}
std::optional<std::string> RuntimeNavigationContext::getLoadname() const
{
return getStringFromCWriter(config->get_loadname, initalIdentifierBufferSize);
}
std::optional<std::string> RuntimeNavigationContext::getCacheKey() const
{
return getStringFromCWriter(config->get_cache_key, initalIdentifierBufferSize);
}
bool RuntimeNavigationContext::isConfigPresent() const
{
return config->is_config_present(L, ctx);
}
NavigationContext::ConfigBehavior RuntimeNavigationContext::getConfigBehavior() const
{
if (config->get_alias)
return ConfigBehavior::GetAlias;
return ConfigBehavior::GetConfig;
}
std::optional<std::string> RuntimeNavigationContext::getAlias(const std::string& alias) const
{
return getStringFromCWriterWithInput(config->get_alias, alias, initalIdentifierBufferSize);
}
std::optional<std::string> RuntimeNavigationContext::getConfig() const
{
return getStringFromCWriter(config->get_config, initalFileBufferSize);
}
std::optional<std::string> RuntimeNavigationContext::getStringFromCWriter(
luarequire_WriteResult (*writer)(lua_State* L, void* ctx, char* buffer, size_t buffer_size, size_t* size_out),
size_t initalBufferSize
) const
{
std::string buffer;
buffer.resize(initalBufferSize);
size_t size;
luarequire_WriteResult result = writer(L, ctx, buffer.data(), buffer.size(), &size);
if (result == WRITE_BUFFER_TOO_SMALL)
{
buffer.resize(size);
result = writer(L, ctx, buffer.data(), buffer.size(), &size);
}
if (result == WRITE_SUCCESS)
{
buffer.resize(size);
return buffer;
}
return std::nullopt;
}
std::optional<std::string> RuntimeNavigationContext::getStringFromCWriterWithInput(
luarequire_WriteResult (*writer)(lua_State* L, void* ctx, const char* input, char* buffer, size_t buffer_size, size_t* size_out),
std::string input,
size_t initalBufferSize
) const
{
std::string buffer;
buffer.resize(initalBufferSize);
size_t size;
luarequire_WriteResult result = writer(L, ctx, input.c_str(), buffer.data(), buffer.size(), &size);
if (result == WRITE_BUFFER_TOO_SMALL)
{
buffer.resize(size);
result = writer(L, ctx, input.c_str(), buffer.data(), buffer.size(), &size);
}
if (result == WRITE_SUCCESS)
{
buffer.resize(size);
return buffer;
}
return std::nullopt;
}
RuntimeErrorHandler::RuntimeErrorHandler(std::string requiredPath)
: errorPrefix("error requiring module \"" + std::move(requiredPath) + "\": ")
{
}
void RuntimeErrorHandler::reportError(std::string message)
{
errorMessage = errorPrefix + std::move(message);
}
const std::string& RuntimeErrorHandler::getReportedError() const
{
return errorMessage;
}
} // namespace Luau::Require