Skip to content

Commit cb1dc27

Browse files
authored
Fix certain invalid @lute/ runtime requires giving a nonsensical error (#280)
1 parent 1e82c96 commit cb1dc27

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

runtime/src/require.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ static luarequire_NavigateResult jump_to_alias(lua_State* L, void* ctx, const ch
9696
reqCtx->suffix = "";
9797
return NAVIGATE_SUCCESS;
9898
}
99+
else if (std::string_view(path) == "$lute")
100+
{
101+
reqCtx->absPath = "@lute";
102+
return NAVIGATE_SUCCESS;
103+
}
99104

100105
luarequire_NavigateResult result = storePathResult(reqCtx, getAbsolutePathResult(reqCtx->currentVFSType, path));
101106
if (result != NAVIGATE_SUCCESS)
@@ -111,6 +116,9 @@ static luarequire_NavigateResult to_parent(lua_State* L, void* ctx)
111116
{
112117
RequireCtx* reqCtx = static_cast<RequireCtx*>(ctx);
113118

119+
if (std::string_view(reqCtx->absPath) == "@lute")
120+
luaL_error(L, "cannot get the parent of @lute");
121+
114122
PathResult result = getParent(reqCtx->currentVFSType, reqCtx->absPath, reqCtx->relPath);
115123
if (result.status == PathResult::Status::NOT_FOUND)
116124
{
@@ -127,13 +135,21 @@ static luarequire_NavigateResult to_parent(lua_State* L, void* ctx)
127135
static luarequire_NavigateResult to_child(lua_State* L, void* ctx, const char* name)
128136
{
129137
RequireCtx* reqCtx = static_cast<RequireCtx*>(ctx);
138+
139+
if (std::string_view(reqCtx->absPath) == "@lute")
140+
luaL_error(L, "'%s' is not a lute library", name);
141+
130142
reqCtx->atFakeRoot = false;
131143
return storePathResult(reqCtx, getChild(reqCtx->currentVFSType, reqCtx->absPath, reqCtx->relPath, name));
132144
}
133145

134146
static bool is_module_present(lua_State* L, void* ctx)
135147
{
136148
RequireCtx* reqCtx = static_cast<RequireCtx*>(ctx);
149+
150+
if (std::string_view(reqCtx->absPath) == "@lute")
151+
luaL_error(L, "@lute is not requirable");
152+
137153
return isFilePresent(reqCtx->currentVFSType, reqCtx->absPath, reqCtx->suffix);
138154
}
139155

@@ -174,10 +190,11 @@ static luarequire_WriteResult get_config(lua_State* L, void* ctx, char* buffer,
174190
if (reqCtx->atFakeRoot)
175191
{
176192
std::string globalConfig = "{\n"
177-
" \"aliases\": {\n"
178-
" \"std\": \"$std\",\n"
179-
" }\n"
180-
"}\n";
193+
" \"aliases\": {\n"
194+
" \"std\": \"$std\",\n"
195+
" \"lute\": \"$lute\",\n"
196+
" }\n"
197+
"}\n";
181198
return write(globalConfig, buffer, buffer_size, size_out);
182199
}
183200

@@ -186,15 +203,6 @@ static luarequire_WriteResult get_config(lua_State* L, void* ctx, char* buffer,
186203

187204
static int load(lua_State* L, void* ctx, const char* path, const char* chunkname, const char* contents)
188205
{
189-
std::string_view pathView = path;
190-
191-
if (pathView.rfind("@lute/", 0) == 0)
192-
{
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);
196-
}
197-
198206
// module needs to run in a new thread, isolated from the rest
199207
// note: we create ML on main thread so that it doesn't inherit environment of L
200208
lua_State* GL = lua_mainthread(L);
@@ -218,7 +226,7 @@ static int load(lua_State* L, void* ctx, const char* path, const char* chunkname
218226

219227
if (status == 0)
220228
{
221-
const std::string prefix = "module " + std::string(pathView) + " must";
229+
const std::string prefix = "module " + std::string(path) + " must";
222230

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

0 commit comments

Comments
 (0)