diff --git a/definitions/luau.luau b/definitions/luau.luau index 78950109c..1b18f7739 100644 --- a/definitions/luau.luau +++ b/definitions/luau.luau @@ -580,4 +580,8 @@ function luau.load(bytecode: Bytecode, chunkname: string?, env: { [any]: any }?) error("not implemented") end +function luau.requirefromcwd(path: string): any + error("not implemented") +end + return luau diff --git a/lute/luau/CMakeLists.txt b/lute/luau/CMakeLists.txt index edbac0404..3bd3b57c0 100644 --- a/lute/luau/CMakeLists.txt +++ b/lute/luau/CMakeLists.txt @@ -8,5 +8,5 @@ target_sources(Lute.Luau PRIVATE target_compile_features(Lute.Luau PUBLIC cxx_std_17) target_include_directories(Lute.Luau PUBLIC "include" ${LIBUV_INCLUDE_DIR}) -target_link_libraries(Lute.Luau PRIVATE Lute.Runtime Luau.VM uv_a Luau.Analysis Luau.Ast Luau.Compiler) +target_link_libraries(Lute.Luau PRIVATE Lute.Require Lute.Runtime Luau.VM uv_a Luau.Analysis Luau.Ast Luau.Compiler Luau.CLI.lib) target_compile_options(Lute.Luau PRIVATE ${LUTE_OPTIONS}) diff --git a/lute/luau/include/lute/luau.h b/lute/luau/include/lute/luau.h index fe124c788..a5a4ac300 100644 --- a/lute/luau/include/lute/luau.h +++ b/lute/luau/include/lute/luau.h @@ -19,11 +19,14 @@ int compile_luau(lua_State* L); int load_luau(lua_State* L); +int luau_requirefromcwd(lua_State* L); + static const luaL_Reg lib[] = { {"parse", luau_parse}, {"parseexpr", luau_parseexpr}, {"compile", compile_luau}, {"load", load_luau}, + {"requirefromcwd", luau_requirefromcwd}, {nullptr, nullptr}, }; diff --git a/lute/luau/src/luau.cpp b/lute/luau/src/luau.cpp index efb2069d0..9e3f9904e 100644 --- a/lute/luau/src/luau.cpp +++ b/lute/luau/src/luau.cpp @@ -1,6 +1,7 @@ #include "lute/luau.h" #include "Luau/Ast.h" +#include "Luau/FileUtils.h" #include "Luau/Location.h" #include "Luau/ParseResult.h" #include "Luau/Parser.h" @@ -9,6 +10,7 @@ #include "Luau/Compiler.h" #include "Luau/NotNull.h" +#include "lute/require.h" #include "lute/userdatas.h" @@ -2667,6 +2669,38 @@ int load_luau(lua_State* L) return 1; } +int luau_requirefromcwd(lua_State* L) +{ + const char* path = luaL_checkstring(L, 1); + + std::string absolutePath = path; + if (!isAbsolutePath(path)) + { + std::optional cwd = getCurrentWorkingDirectory(); + if (!cwd) + luaL_error(L, "failed to get current working directory"); + + cwd = joinPaths(*cwd, "psuedofile.luau"); + + std::optional resolvedPath = resolvePath(path, *cwd); + if (!resolvedPath) + luaL_error(L, "failed to resolve path"); + + absolutePath = *resolvedPath; + } + + if (!isFile(absolutePath)) + luaL_error(L, "file not found: %s", absolutePath.c_str()); + + std::string chunkname = "@" + std::move(absolutePath); + RequireCtx ctx{}; + luarequire_pushproxyrequire(L, requireConfigInit, &ctx); + lua_pushstring(L, "@self"); + lua_pushstring(L, chunkname.c_str()); + lua_call(L, 2, 1); + return 1; +} + } // namespace luau static int index_result(lua_State* L) diff --git a/varun_test/requirer.luau b/varun_test/requirer.luau new file mode 100644 index 000000000..38379bb41 --- /dev/null +++ b/varun_test/requirer.luau @@ -0,0 +1,6 @@ +local args = { ... } +assert(#args == 2, "Expected one argument: path to Luau script to require") + +local luau = require("@lute/luau") +local result = luau.requirefromcwd(args[2]) +print(result) diff --git a/varun_test/test.luau b/varun_test/test.luau new file mode 100644 index 000000000..4b4751d14 --- /dev/null +++ b/varun_test/test.luau @@ -0,0 +1 @@ +return "Success"