Skip to content

Commit f27e0f3

Browse files
authored
Update Luau dependency to 0.671 to remove require-by-string hacks (#242)
This PR removes several of the hacks involved in implementing support for Luau.Require in Lute, as release 0.671 of Luau.Require added various helpful functions like `luarequire_pushproxyrequire` and `luarequire_registermodule`.
1 parent bcaeab7 commit f27e0f3

File tree

8 files changed

+45
-114
lines changed

8 files changed

+45
-114
lines changed

cli/main.cpp

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ static void* createCliRequireContext(lua_State* L)
6060
return ctx;
6161
}
6262

63+
static void luteopen_libs(lua_State* L)
64+
{
65+
std::vector<std::pair<const char*, lua_CFunction>> libs = {{
66+
{"@lute/crypto", luteopen_crypto},
67+
{"@lute/fs", luteopen_fs},
68+
{"@lute/luau", luteopen_luau},
69+
{"@lute/net", luteopen_net},
70+
{"@lute/process", luteopen_process},
71+
{"@lute/task", luteopen_task},
72+
{"@lute/vm", luteopen_vm},
73+
{"@lute/system", luteopen_system},
74+
{"@lute/time", luteopen_time},
75+
}};
76+
77+
for (const auto& [name, func] : libs)
78+
{
79+
lua_pushcfunction(L, luarequire_registermodule, nullptr);
80+
lua_pushstring(L, name);
81+
func(L);
82+
lua_call(L, 2, 0);
83+
}
84+
}
85+
6386
lua_State* setupState(Runtime& runtime)
6487
{
6588
// Separate VM for data copies
@@ -80,38 +103,8 @@ lua_State* setupState(Runtime& runtime)
80103
// register the builtin tables
81104
luaL_openlibs(L);
82105

83-
luaL_findtable(L, LUA_REGISTRYINDEX, "_MODULES", 1);
84-
85-
luteopen_crypto(L);
86-
lua_setfield(L, -2, "@lute/crypto");
87-
88-
luteopen_fs(L);
89-
lua_setfield(L, -2, "@lute/fs");
90-
91-
luteopen_luau(L);
92-
lua_setfield(L, -2, "@lute/luau");
93-
94-
luteopen_net(L);
95-
lua_setfield(L, -2, "@lute/net");
96-
97-
luteopen_process(L);
98-
lua_setfield(L, -2, "@lute/process");
99-
100-
luteopen_task(L);
101-
lua_setfield(L, -2, "@lute/task");
102-
103-
luteopen_vm(L);
104-
lua_setfield(L, -2, "@lute/vm");
105-
106-
luteopen_system(L);
107-
lua_setfield(L, -2, "@lute/system");
108-
109-
luteopen_time(L);
110-
lua_setfield(L, -2, "@lute/time");
111-
112-
lua_pop(L, 1);
113-
114106
luaopen_require(L, requireConfigInit, createCliRequireContext(L));
107+
luteopen_libs(L);
115108

116109
lua_pushnil(L);
117110
lua_setglobal(L, "setfenv");

extern/luau.tune

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[dependency]
22
name = "luau"
33
remote = "https://github.com/luau-lang/luau.git"
4-
branch = "0.670"
5-
revision = "a2303a6ae68c53035eccf230c4450b9f068536af"
4+
branch = "0.671"
5+
revision = "c51743268bcbe5c5af1bd78bcacaefe7f6fe3391"

luau/src/luau.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ struct AstSerialize : public Luau::AstVisitor
14201420
// TODO: declarations
14211421
}
14221422

1423-
void serializeStat(Luau::AstStatDeclareClass* node)
1423+
void serializeStat(Luau::AstStatDeclareExternType* node)
14241424
{
14251425
// TODO: declarations
14261426
}
@@ -1845,7 +1845,7 @@ struct AstSerialize : public Luau::AstVisitor
18451845
return false;
18461846
}
18471847

1848-
bool visit(Luau::AstStatDeclareClass* node) override
1848+
bool visit(Luau::AstStatDeclareExternType* node) override
18491849
{
18501850
serializeStat(node);
18511851
return false;

runtime/include/lute/require.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ void requireConfigInit(luarequire_Configuration* config);
1414
struct RequireCtx
1515
{
1616
RequireCtx() = default;
17-
RequireCtx(std::string sourceOverride)
18-
: sourceOverride(std::move(sourceOverride))
19-
{
20-
}
21-
22-
std::optional<std::string> sourceOverride = std::nullopt;
2317

2418
std::string absPath;
2519
std::string relPath;

runtime/include/lute/requireutils.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ enum class VFSType
88
{
99
Disk,
1010
Std,
11-
12-
// FIXME: this is a temporary workaround until Luau.Require provides an
13-
// API for registering the @lute/* libraries.
14-
Lute,
1511
};
1612

1713
struct PathResult

runtime/src/require.cpp

Lines changed: 13 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,18 @@ static luarequire_NavigateResult storePathResult(RequireCtx* reqCtx, PathResult
4747

4848
static bool is_require_allowed(lua_State* L, void* ctx, const char* requirer_chunkname)
4949
{
50-
// FIXME: this is a temporary workaround until Luau.Require provides a way
51-
// to perform proxy requires.
52-
return true;
53-
54-
// std::string_view chunkname = requirer_chunkname;
55-
// bool isStdin = (chunkname == "=stdin");
56-
// bool isFile = (!chunkname.empty() && chunkname[0] == '@');
57-
// bool isStdLibFile = (chunkname.size() >= 6 && chunkname.substr(0, 6) == "@@std/");
58-
// return isStdin || isFile || isStdLibFile;
50+
std::string_view chunkname = requirer_chunkname;
51+
bool isStdin = (chunkname == "=stdin");
52+
bool isFile = (!chunkname.empty() && chunkname[0] == '@');
53+
bool isStdLibFile = (chunkname.size() >= 6 && chunkname.substr(0, 6) == "@@std/");
54+
return isStdin || isFile || isStdLibFile;
5955
}
6056

6157
static luarequire_NavigateResult reset(lua_State* L, void* ctx, const char* requirer_chunkname)
6258
{
6359
RequireCtx* reqCtx = static_cast<RequireCtx*>(ctx);
6460

65-
std::string chunkname = reqCtx->sourceOverride ? *reqCtx->sourceOverride : requirer_chunkname;
61+
std::string chunkname = requirer_chunkname;
6662
reqCtx->atFakeRoot = false;
6763

6864
if ((chunkname.size() >= 6 && chunkname.substr(0, 6) == "@@std/"))
@@ -91,18 +87,6 @@ static luarequire_NavigateResult jump_to_alias(lua_State* L, void* ctx, const ch
9187
{
9288
RequireCtx* reqCtx = static_cast<RequireCtx*>(ctx);
9389

94-
// FIXME: this is a temporary workaround until Luau.Require provides an
95-
// API for registering the @lute/* libraries.
96-
if (std::string_view(path) == "$lute")
97-
{
98-
reqCtx->atFakeRoot = false;
99-
reqCtx->currentVFSType = VFSType::Lute;
100-
reqCtx->absPath = "@lute";
101-
reqCtx->relPath = "";
102-
reqCtx->suffix = "";
103-
return NAVIGATE_SUCCESS;
104-
}
105-
10690
if (std::string_view(path) == "$std")
10791
{
10892
reqCtx->atFakeRoot = false;
@@ -150,36 +134,19 @@ static luarequire_NavigateResult to_child(lua_State* L, void* ctx, const char* n
150134
static bool is_module_present(lua_State* L, void* ctx)
151135
{
152136
RequireCtx* reqCtx = static_cast<RequireCtx*>(ctx);
153-
154-
// FIXME: this is a temporary workaround until Luau.Require provides an
155-
// API for registering the @lute/* libraries.
156-
if (reqCtx->currentVFSType == VFSType::Lute)
157-
return true;
158-
159137
return isFilePresent(reqCtx->currentVFSType, reqCtx->absPath, reqCtx->suffix);
160138
}
161139

162140
static luarequire_WriteResult get_contents(lua_State* L, void* ctx, char* buffer, size_t buffer_size, size_t* size_out)
163141
{
164142
RequireCtx* reqCtx = static_cast<RequireCtx*>(ctx);
165-
166-
// FIXME: this is a temporary workaround until Luau.Require provides an
167-
// API for registering the @lute/* libraries.
168-
if (reqCtx->currentVFSType == VFSType::Lute)
169-
return write("", buffer, buffer_size, size_out);
170-
171143
return write(getFileContents(reqCtx->currentVFSType, reqCtx->absPath, reqCtx->suffix), buffer, buffer_size, size_out);
172144
}
173145

174146
static luarequire_WriteResult get_chunkname(lua_State* L, void* ctx, char* buffer, size_t buffer_size, size_t* size_out)
175147
{
176148
RequireCtx* reqCtx = static_cast<RequireCtx*>(ctx);
177149

178-
// FIXME: this is a temporary workaround until Luau.Require provides an
179-
// API for registering the @lute/* libraries.
180-
if (reqCtx->currentVFSType == VFSType::Lute)
181-
return write("@" + reqCtx->absPath, buffer, buffer_size, size_out);
182-
183150
if (reqCtx->currentVFSType == VFSType::Std)
184151
return write("@" + reqCtx->absPath, buffer, buffer_size, size_out);
185152

@@ -198,11 +165,6 @@ static bool is_config_present(lua_State* L, void* ctx)
198165
if (reqCtx->atFakeRoot)
199166
return true;
200167

201-
// FIXME: this is a temporary workaround until Luau.Require provides an
202-
// API for registering the @lute/* libraries.
203-
if (reqCtx->currentVFSType == VFSType::Lute)
204-
return false;
205-
206168
return isFilePresent(reqCtx->currentVFSType, reqCtx->absPath, "/.luaurc");
207169
}
208170

@@ -214,7 +176,6 @@ static luarequire_WriteResult get_config(lua_State* L, void* ctx, char* buffer,
214176
std::string globalConfig = "{\n"
215177
" \"aliases\": {\n"
216178
" \"std\": \"$std\",\n"
217-
" \"lute\": \"$lute\",\n"
218179
" }\n"
219180
"}\n";
220181
return write(globalConfig, buffer, buffer_size, size_out);
@@ -223,24 +184,15 @@ static luarequire_WriteResult get_config(lua_State* L, void* ctx, char* buffer,
223184
return write(getFileContents(reqCtx->currentVFSType, reqCtx->absPath, "/.luaurc"), buffer, buffer_size, size_out);
224185
}
225186

226-
static int load(lua_State* L, void* ctx, const char* chunkname, const char* contents)
187+
static int load(lua_State* L, void* ctx, const char* path, const char* chunkname, const char* contents)
227188
{
228-
std::string_view chunknameView = chunkname;
189+
std::string_view pathView = path;
229190

230-
// FIXME: this is a temporary workaround until Luau.Require provides an
231-
// API for registering the @lute/* libraries.
232-
if (chunknameView.rfind("@@lute/", 0) == 0)
191+
if (pathView.rfind("@lute/", 0) == 0)
233192
{
234-
lua_getfield(L, LUA_REGISTRYINDEX, "_MODULES");
235-
lua_getfield(L, -1, chunknameView.substr(1).data());
236-
237-
if (lua_isnil(L, -1))
238-
{
239-
lua_pop(L, 1);
240-
luaL_error(L, "no luau runtime library: %s", &chunkname[1]);
241-
}
242-
243-
return 1;
193+
// @lute library tables are registered into require-by-string directly
194+
// and are not loaded here.
195+
luaL_error(L, "no luau runtime library: %s", path);
244196
}
245197

246198
// module needs to run in a new thread, isolated from the rest
@@ -266,7 +218,7 @@ static int load(lua_State* L, void* ctx, const char* chunkname, const char* cont
266218

267219
if (status == 0)
268220
{
269-
const std::string prefix = "module " + std::string(chunknameView.substr(1)) + " must";
221+
const std::string prefix = "module " + std::string(pathView) + " must";
270222

271223
if (lua_gettop(ML) == 0)
272224
lua_pushstring(ML, (prefix + " return a value, if it has no return value, you should explicitly return `nil`\n").c_str());

runtime/src/requireutils.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ static std::pair<PathResult::Status, std::string> getSuffixWithAmbiguityCheck(VF
7676

7777
static PathResult addSuffix(VFSType vfsType, PathResult partialResult)
7878
{
79-
// FIXME: this is a temporary workaround until Luau.Require provides an
80-
// API for registering the @lute/* libraries.
81-
if (vfsType == VFSType::Lute)
82-
return partialResult;
83-
8479
if (partialResult.status != PathResult::Status::SUCCESS)
8580
return partialResult;
8681

vm/src/spawn.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,11 @@ int lua_spawn(lua_State* L)
185185
lua_getinfo(L, 1, "s", &ar);
186186

187187
// Require the target module
188-
RequireCtx ctx{/* sourceOverride = */ ar.source};
189-
lua_pushrequire(child->GL, requireConfigInit, &ctx);
188+
RequireCtx ctx{};
189+
luarequire_pushproxyrequire(child->GL, requireConfigInit, &ctx);
190190
lua_pushstring(child->GL, file);
191-
int status = lua_pcall(child->GL, 1, 1, 0);
191+
lua_pushstring(child->GL, ar.source);
192+
int status = lua_pcall(child->GL, 2, 1, 0);
192193

193194
if (status == LUA_ERRRUN && lua_type(child->GL, -1) == LUA_TSTRING)
194195
{

0 commit comments

Comments
 (0)