Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions definitions/luau.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lute/luau/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
3 changes: 3 additions & 0 deletions lute/luau/include/lute/luau.h
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down
34 changes: 34 additions & 0 deletions lute/luau/src/luau.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -9,6 +10,7 @@
#include "Luau/Compiler.h"
#include "Luau/NotNull.h"

#include "lute/require.h"
#include "lute/userdatas.h"


Expand Down Expand Up @@ -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<std::string> cwd = getCurrentWorkingDirectory();
if (!cwd)
luaL_error(L, "failed to get current working directory");

cwd = joinPaths(*cwd, "psuedofile.luau");

std::optional<std::string> 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)
Expand Down
6 changes: 6 additions & 0 deletions varun_test/requirer.luau
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions varun_test/test.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return "Success"