Skip to content

Commit 35777c2

Browse files
committed
Use switch-case for sub-VFS trees
1 parent 277728b commit 35777c2

File tree

3 files changed

+97
-53
lines changed

3 files changed

+97
-53
lines changed

runtime/src/modulepath.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ ModulePath::ModulePath(std::string filePath, size_t endRootDirectory, bool (*isA
5151
if (endRootDirectory == pathView.size() - 1)
5252
{
5353
realPathPrefix = pathView;
54+
return;
5455
}
55-
else
56-
{
57-
realPathPrefix = pathView.substr(0, endRootDirectory + 1);
58-
modulePath = pathView.substr(endRootDirectory + 1);
59-
}
56+
57+
realPathPrefix = pathView.substr(0, endRootDirectory + 1);
58+
modulePath = pathView.substr(endRootDirectory + 1);
6059
}
6160

6261
ResolvedRealPath ModulePath::getRealPath() const
@@ -84,6 +83,7 @@ ResolvedRealPath ModulePath::getRealPath() const
8483
}
8584
}
8685
}
86+
8787
if (isADirectory(partialRealPath))
8888
{
8989
if (found)

runtime/src/require.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ static luarequire_NavigateResult convert(NavigationStatus status)
3434
{
3535
if (status == NavigationStatus::Success)
3636
return NAVIGATE_SUCCESS;
37-
else if (status == NavigationStatus::Ambiguous)
37+
38+
if (status == NavigationStatus::Ambiguous)
3839
return NAVIGATE_AMBIGUOUS;
3940

4041
return NAVIGATE_NOT_FOUND;

runtime/src/requirevfs.cpp

Lines changed: 90 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,13 @@ NavigationStatus RequireVfs::reset(lua_State* L, std::string_view requirerChunkn
2424
vfsType = VFSType::Std;
2525
return stdLibVfs.resetToPath(std::string(requirerChunkname.substr(1)));
2626
}
27-
else
28-
{
29-
vfsType = VFSType::Disk;
30-
if (requirerChunkname == "=stdin")
31-
{
32-
return fileVfs.resetToStdIn();
33-
}
34-
else if (!requirerChunkname.empty() && requirerChunkname[0] == '@')
35-
{
36-
return fileVfs.resetToPath(std::string(requirerChunkname.substr(1)));
37-
}
38-
}
27+
28+
vfsType = VFSType::Disk;
29+
if (requirerChunkname == "=stdin")
30+
return fileVfs.resetToStdIn();
31+
32+
if (!requirerChunkname.empty() && requirerChunkname[0] == '@')
33+
return fileVfs.resetToPath(std::string(requirerChunkname.substr(1)));
3934

4035
return NavigationStatus::NotFound;
4136
}
@@ -55,25 +50,35 @@ NavigationStatus RequireVfs::jumpToAlias(lua_State* L, std::string_view path)
5550
return NavigationStatus::Success;
5651
}
5752

58-
if (vfsType == VFSType::Disk)
53+
switch (vfsType)
54+
{
55+
case VFSType::Disk:
5956
return fileVfs.resetToPath(std::string(path));
60-
else if (vfsType == VFSType::Std)
57+
case VFSType::Std:
6158
return stdLibVfs.resetToPath(std::string(path));
62-
63-
return NavigationStatus::NotFound;
59+
default:
60+
return NavigationStatus::NotFound;
61+
}
6462
}
6563

6664
NavigationStatus RequireVfs::toParent(lua_State* L)
6765
{
6866
NavigationStatus status;
69-
if (vfsType == VFSType::Disk)
67+
68+
switch (vfsType)
69+
{
70+
case VFSType::Disk:
7071
status = fileVfs.toParent();
71-
else if (vfsType == VFSType::Std)
72+
break;
73+
case VFSType::Std:
7274
status = stdLibVfs.toParent();
73-
else if (vfsType == VFSType::Lute)
75+
break;
76+
case VFSType::Lute:
7477
luaL_error(L, "cannot get the parent of @lute");
75-
else
78+
break;
79+
default:
7680
return NavigationStatus::NotFound;
81+
}
7782

7883
if (status == NavigationStatus::NotFound)
7984
{
@@ -91,24 +96,36 @@ NavigationStatus RequireVfs::toChild(lua_State* L, std::string_view name)
9196
{
9297
atFakeRoot = false;
9398

94-
if (vfsType == VFSType::Disk)
99+
switch (vfsType)
100+
{
101+
case VFSType::Disk:
95102
return fileVfs.toChild(std::string(name));
96-
else if (vfsType == VFSType::Std)
103+
case VFSType::Std:
97104
return stdLibVfs.toChild(std::string(name));
98-
else if (vfsType == VFSType::Lute)
105+
case VFSType::Lute:
99106
luaL_error(L, "'%s' is not a lute library", std::string(name).c_str());
107+
break;
108+
default:
109+
break;
110+
}
100111

101112
return NavigationStatus::NotFound;
102113
}
103114

104115
bool RequireVfs::isModulePresent(lua_State* L) const
105116
{
106-
if (vfsType == VFSType::Disk)
117+
switch (vfsType)
118+
{
119+
case VFSType::Disk:
107120
return fileVfs.isModulePresent();
108-
else if (vfsType == VFSType::Std)
121+
case VFSType::Std:
109122
return stdLibVfs.isModulePresent();
110-
else if (vfsType == VFSType::Lute)
123+
case VFSType::Lute:
111124
luaL_error(L, "@lute is not requirable");
125+
break;
126+
default:
127+
break;
128+
}
112129

113130
return false;
114131
}
@@ -117,55 +134,74 @@ std::string RequireVfs::getContents(lua_State* L, const std::string& loadname) c
117134
{
118135
std::optional<std::string> contents;
119136

120-
if (vfsType == VFSType::Disk)
137+
switch (vfsType)
138+
{
139+
case VFSType::Disk:
121140
contents = fileVfs.getContents(loadname);
122-
else if (vfsType == VFSType::Std)
141+
break;
142+
case VFSType::Std:
123143
contents = stdLibVfs.getContents(loadname);
144+
break;
145+
default:
146+
break;
147+
}
124148

125149
return contents ? *contents : "";
126150
}
127151

128152
std::string RequireVfs::getChunkname(lua_State* L) const
129153
{
130-
if (vfsType == VFSType::Disk)
154+
switch (vfsType)
155+
{
156+
case VFSType::Disk:
131157
return "@" + fileVfs.getFilePath();
132-
else if (vfsType == VFSType::Std)
158+
case VFSType::Std:
133159
return "@" + stdLibVfs.getIdentifier();
134-
135-
return "";
160+
default:
161+
return "";
162+
}
136163
}
137164

138165
std::string RequireVfs::getLoadname(lua_State* L) const
139166
{
140-
if (vfsType == VFSType::Disk)
167+
switch (vfsType)
168+
{
169+
case VFSType::Disk:
141170
return fileVfs.getAbsoluteFilePath();
142-
else if (vfsType == VFSType::Std)
171+
case VFSType::Std:
143172
return stdLibVfs.getIdentifier();
144-
145-
return "";
173+
default:
174+
return "";
175+
}
146176
}
147177

148178
std::string RequireVfs::getCacheKey(lua_State* L) const
149179
{
150-
if (vfsType == VFSType::Disk)
180+
switch (vfsType)
181+
{
182+
case VFSType::Disk:
151183
return fileVfs.getAbsoluteFilePath();
152-
else if (vfsType == VFSType::Std)
184+
case VFSType::Std:
153185
return stdLibVfs.getIdentifier();
154-
155-
return "";
186+
default:
187+
return "";
188+
}
156189
}
157190

158191
bool RequireVfs::isConfigPresent(lua_State* L) const
159192
{
160193
if (atFakeRoot)
161194
return true;
162195

163-
if (vfsType == VFSType::Disk)
164-
return fileVfs.isConfigPresent();
165-
else if (vfsType == VFSType::Std)
196+
switch (vfsType)
197+
{
198+
case VFSType::Disk:
166199
return fileVfs.isConfigPresent();
167-
168-
return false;
200+
case VFSType::Std:
201+
return stdLibVfs.isConfigPresent();
202+
default:
203+
return false;
204+
}
169205
}
170206

171207
std::string RequireVfs::getConfig(lua_State* L) const
@@ -183,10 +219,17 @@ std::string RequireVfs::getConfig(lua_State* L) const
183219

184220
std::optional<std::string> configContents;
185221

186-
if (vfsType == VFSType::Disk)
222+
switch (vfsType)
223+
{
224+
case VFSType::Disk:
187225
configContents = fileVfs.getConfig();
188-
else if (vfsType == VFSType::Std)
226+
break;
227+
case VFSType::Std:
189228
configContents = stdLibVfs.getConfig();
229+
break;
230+
default:
231+
break;
232+
}
190233

191234
return configContents ? *configContents : "";
192235
}

0 commit comments

Comments
 (0)