Skip to content

Commit 2c98d5e

Browse files
committed
refactor: apply some suggestions from clang-tidy
1 parent 0882ba4 commit 2c98d5e

File tree

4 files changed

+63
-62
lines changed

4 files changed

+63
-62
lines changed

src/legacy/main/NodeJsHelper.cpp

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,34 @@ std::unordered_map<node::Environment*, bool>
3636
std::set<node::Environment*> uvLoopTask;
3737

3838
ll::Expected<> PatchDelayImport(HMODULE hAddon, HMODULE hLibNode) {
39-
BYTE* base = (BYTE*)hAddon;
40-
auto dos = (PIMAGE_DOS_HEADER)base;
39+
BYTE* base = reinterpret_cast<BYTE*>(hAddon);
40+
auto dos = reinterpret_cast<PIMAGE_DOS_HEADER>(base);
4141
if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
4242
return ll::makeStringError("Invalid DOS signature.");
4343
}
44-
auto nt = (PIMAGE_NT_HEADERS)(base + dos->e_lfanew);
44+
auto nt = reinterpret_cast<PIMAGE_NT_HEADERS>(base + dos->e_lfanew);
4545
if (nt->Signature != IMAGE_NT_SIGNATURE) {
4646
return ll::makeStringError("Invalid NT signature.");
4747
}
4848
DWORD rva = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress;
4949
if (!rva) {};
50-
auto pDesc = (PIMAGE_DELAYLOAD_DESCRIPTOR)(base + rva);
50+
auto pDesc = reinterpret_cast<PIMAGE_DELAYLOAD_DESCRIPTOR>(base + rva);
5151
for (; pDesc->DllNameRVA; ++pDesc) {
52-
char* szDll = (char*)(base + pDesc->DllNameRVA);
52+
char* szDll = reinterpret_cast<char*>(base + pDesc->DllNameRVA);
5353
if (_stricmp(szDll, NODE_HOST_BINARY_NAME) != 0) continue;
5454

55-
auto pIAT = (PIMAGE_THUNK_DATA)(base + pDesc->ImportAddressTableRVA);
56-
auto pINT = (PIMAGE_THUNK_DATA)(base + pDesc->ImportNameTableRVA);
55+
auto pIAT = reinterpret_cast<PIMAGE_THUNK_DATA>(base + pDesc->ImportAddressTableRVA);
56+
auto pINT = reinterpret_cast<PIMAGE_THUNK_DATA>(base + pDesc->ImportNameTableRVA);
5757

5858
for (; pIAT->u1.Function; ++pIAT, ++pINT) {
59-
FARPROC f = nullptr;
59+
FARPROC f;
6060
if (pINT->u1.Ordinal & IMAGE_ORDINAL_FLAG) {
6161
// Import by Ordinal
6262
WORD ordinal = IMAGE_ORDINAL(pINT->u1.Ordinal);
6363
f = GetProcAddress(hLibNode, MAKEINTRESOURCEA(ordinal));
6464
} else {
6565
// Import by name
66-
auto name = (PIMAGE_IMPORT_BY_NAME)(base + pINT->u1.AddressOfData);
66+
auto name = reinterpret_cast<PIMAGE_IMPORT_BY_NAME>(base + pINT->u1.AddressOfData);
6767
f = GetProcAddress(hLibNode, name->Name);
6868
}
6969
if (f) {
@@ -112,7 +112,7 @@ bool initNodeJs() {
112112
}
113113
// Init NodeJs
114114
auto path = ll::string_utils::u8str2str(ll::sys_utils::getModulePath(nullptr).value().u8string());
115-
char* cPath = (char*)path.c_str();
115+
char* cPath = const_cast<char*>(path.c_str());
116116
uv_setup_args(1, &cPath);
117117
auto full_args = std::vector<std::string>{path};
118118
#if defined(LSE_DEBUG) || defined(LSE_TEST)
@@ -131,15 +131,15 @@ bool initNodeJs() {
131131
);
132132
if (result->exit_code() != 0) {
133133
lse::LegacyScriptEngine::getLogger().error("Failed to initialize node! NodeJs plugins won't be loaded");
134-
for (const std::string& error : result->errors()) lse::LegacyScriptEngine::getLogger().error(error);
134+
for (std::string const& error : result->errors()) lse::LegacyScriptEngine::getLogger().error(error);
135135
return false;
136136
}
137137
args = result->args();
138138
exec_args = result->exec_args();
139139

140140
// Init V8
141141
using namespace v8;
142-
platform = node::MultiIsolatePlatform::Create((int)std::thread::hardware_concurrency());
142+
platform = node::MultiIsolatePlatform::Create(static_cast<int>(std::thread::hardware_concurrency()));
143143
V8::InitializePlatform(platform.get());
144144
V8::Initialize();
145145

@@ -169,7 +169,7 @@ std::shared_ptr<ScriptEngine> newEngine() {
169169
// CHECK_EQ(start_io_thread_async_initialized.exchange(true), false) fail!
170170

171171
if (!setup) {
172-
for (const std::string& err : errors)
172+
for (std::string const& err : errors)
173173
lse::LegacyScriptEngine::getLogger().error("CommonEnvironmentSetup Error: {}", err.c_str());
174174
return nullptr;
175175
}
@@ -184,7 +184,10 @@ std::shared_ptr<ScriptEngine> newEngine() {
184184
std::shared_ptr<ScriptEngine> engine(new ScriptEngineImpl({}, isolate, setup->context(), false), [](ScriptEngine*) {
185185
});
186186

187-
lse::LegacyScriptEngine::getLogger().debug("Initialize ScriptEngine for node.js [{}]", (void*)engine.get());
187+
lse::LegacyScriptEngine::getLogger().debug(
188+
"Initialize ScriptEngine for node.js [{}]",
189+
static_cast<void*>(engine.get())
190+
);
188191
environments[engine] = env;
189192
setups[engine] = std::move(setup);
190193
isRunning[env] = true;
@@ -289,7 +292,7 @@ bool loadPluginCode(
289292
}
290293

291294
// Set exit handler
292-
node::SetProcessExitHandler(env, [](node::Environment* env_, int exit_code) {
295+
node::SetProcessExitHandler(env, [](node::Environment const* env_, int exit_code) {
293296
auto engine = getEngine(env_);
294297
lse::LegacyScriptEngine::getLogger().log(
295298
exit_code == 0 ? ll::io::LogLevel::Debug : ll::io::LogLevel::Error,
@@ -342,7 +345,7 @@ bool loadPluginCode(
342345
using namespace ll::chrono_literals;
343346
while (uvLoopTask.contains(env)) {
344347
co_await 2_tick;
345-
if (!(ll::getGamingStatus() != ll::GamingStatus::Running) && (*isRunningMap)[env]) {
348+
if (ll::getGamingStatus() == ll::GamingStatus::Running && (*isRunningMap)[env]) {
346349
EngineScope enter(engine.get());
347350
// v8::MicrotasksScope microtaskScope(isolate, v8::MicrotasksScope::kRunMicrotasks);
348351
uv_run(eventLoop, UV_RUN_NOWAIT);
@@ -364,13 +367,13 @@ bool loadPluginCode(
364367
}
365368
}
366369

367-
node::Environment* getEnvironmentOf(std::shared_ptr<ScriptEngine> engine) {
370+
node::Environment* getEnvironmentOf(std::shared_ptr<ScriptEngine> const& engine) {
368371
auto it = environments.find(engine);
369372
if (it == environments.end()) return nullptr;
370373
return it->second;
371374
}
372375

373-
v8::Isolate* getIsolateOf(std::shared_ptr<ScriptEngine> engine) {
376+
v8::Isolate* getIsolateOf(std::shared_ptr<ScriptEngine> const& engine) {
374377
auto it = setups.find(engine);
375378
if (it == setups.end()) return nullptr;
376379
return it->second->isolate();
@@ -393,24 +396,24 @@ bool stopEngine(node::Environment* env) {
393396

394397
return true;
395398
} catch (...) {
396-
lse::LegacyScriptEngine::getLogger().error("Fail to stop engine {}", (void*)env);
399+
lse::LegacyScriptEngine::getLogger().error("Fail to stop engine {}", static_cast<void*>(env));
397400
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger());
398401
return false;
399402
}
400403
}
401404

402-
bool stopEngine(std::shared_ptr<ScriptEngine> engine) {
405+
bool stopEngine(std::shared_ptr<ScriptEngine> const& engine) {
403406
auto env = NodeJsHelper::getEnvironmentOf(engine);
404407
return stopEngine(env);
405408
}
406409

407-
std::shared_ptr<ScriptEngine> getEngine(node::Environment* env) {
410+
std::shared_ptr<ScriptEngine> getEngine(node::Environment const* env) {
408411
for (auto& [engine, environment] : environments)
409412
if (env == environment) return engine;
410413
return nullptr;
411414
}
412415

413-
std::string findEntryScript(const std::string& dirPath) {
416+
std::string findEntryScript(std::string const& dirPath) {
414417
auto dirPath_obj = std::filesystem::path(dirPath);
415418

416419
std::filesystem::path packageFilePath = dirPath_obj / "package.json";
@@ -432,7 +435,7 @@ std::string findEntryScript(const std::string& dirPath) {
432435
}
433436
}
434437

435-
std::string getPluginPackageName(const std::string& dirPath) {
438+
std::string getPluginPackageName(std::string const& dirPath) {
436439
auto dirPath_obj = std::filesystem::path(dirPath);
437440

438441
std::filesystem::path packageFilePath = dirPath_obj / std::filesystem::path("package.json");
@@ -452,7 +455,7 @@ std::string getPluginPackageName(const std::string& dirPath) {
452455
}
453456
}
454457

455-
bool doesPluginPackHasDependency(const std::string& dirPath) {
458+
bool doesPluginPackHasDependency(std::string const& dirPath) {
456459
auto dirPath_obj = std::filesystem::path(dirPath);
457460

458461
std::filesystem::path packageFilePath = dirPath_obj / std::filesystem::path("package.json");
@@ -471,7 +474,7 @@ bool doesPluginPackHasDependency(const std::string& dirPath) {
471474
}
472475
}
473476

474-
bool isESModulesSystem(const std::string& dirPath) {
477+
bool isESModulesSystem(std::string const& dirPath) {
475478
auto dirPath_obj = std::filesystem::path(dirPath);
476479

477480
std::filesystem::path packageFilePath = dirPath_obj / std::filesystem::path("package.json");
@@ -490,7 +493,7 @@ bool isESModulesSystem(const std::string& dirPath) {
490493
}
491494
}
492495

493-
bool processConsoleNpmCmd(const std::string& cmd) {
496+
bool processConsoleNpmCmd(std::string const& cmd) {
494497
#ifdef LSE_BACKEND_NODEJS
495498
if (cmd.starts_with("npm ") || cmd.starts_with("npx ")) {
496499
executeNpmCommand(SplitCmdLine(cmd));
@@ -537,7 +540,7 @@ int executeNpmCommand(std::vector<std::string> npmArgs, std::string workingDir)
537540
// CHECK_EQ(start_io_thread_async_initialized.exchange(true), false) fail!
538541

539542
if (!setup) {
540-
for (const std::string& err : errors)
543+
for (std::string const& err : errors)
541544
lse::LegacyScriptEngine::getLogger().error("CommonEnvironmentSetup Error: {}", err.c_str());
542545
return -1;
543546
}

src/legacy/main/NodeJsHelper.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ bool initNodeJs();
1212
void shutdownNodeJs();
1313

1414
std::shared_ptr<script::ScriptEngine> newEngine();
15-
bool stopEngine(std::shared_ptr<script::ScriptEngine> engine);
15+
bool stopEngine(std::shared_ptr<script::ScriptEngine> const& engine);
1616
bool stopEngine(node::Environment* env);
17-
std::shared_ptr<script::ScriptEngine> getEngine(node::Environment* env);
17+
std::shared_ptr<script::ScriptEngine> getEngine(node::Environment const* env);
1818

1919
bool loadPluginCode(
2020
std::shared_ptr<script::ScriptEngine> engine,
@@ -23,12 +23,12 @@ bool loadPluginCode(
2323
bool esm = false
2424
); // raw
2525

26-
std::string findEntryScript(const std::string& dirPath);
27-
std::string getPluginPackageName(const std::string& dirPath);
28-
bool doesPluginPackHasDependency(const std::string& dirPath);
29-
bool isESModulesSystem(const std::string& dirPath);
26+
std::string findEntryScript(std::string const& dirPath);
27+
std::string getPluginPackageName(std::string const& dirPath);
28+
bool doesPluginPackHasDependency(std::string const& dirPath);
29+
bool isESModulesSystem(std::string const& dirPath);
3030

31-
bool processConsoleNpmCmd(const std::string& cmd);
31+
bool processConsoleNpmCmd(std::string const& cmd);
3232
int executeNpmCommand(std::vector<std::string> npmArgs = {"i", "--omit=dev", "--no-fund"}, std::string workingDir = "");
3333

3434
} // namespace NodeJsHelper

src/legacy/main/PythonHelper.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "Global.h"
55
#include "engine/EngineManager.h"
6-
#include "engine/RemoteCall.h"
76
#include "engine/TimeTaskSystem.h"
87
#include "legacy/api/CommandAPI.h"
98
#include "ll/api/utils/StringUtils.h"
@@ -14,7 +13,7 @@
1413
#include <filesystem>
1514
#include <toml++/toml.h>
1615

17-
const unsigned long PIP_EXECUTE_TIMEOUT = 1800 * 1000;
16+
constexpr unsigned long PIP_EXECUTE_TIMEOUT = 1800 * 1000;
1817

1918
// pre-declare
2019
extern bool InConsoleDebugMode;
@@ -23,13 +22,13 @@ extern std::shared_ptr<ll::io::Logger> DebugCmdLogger;
2322

2423
struct PyConfig;
2524
typedef PyObject* (*create_stdio_func_type)(
26-
const PyConfig* config,
25+
PyConfig const* config,
2726
PyObject* io,
2827
int fd,
2928
int write_mode,
30-
const char* name,
31-
const wchar_t* encoding,
32-
const wchar_t* errors
29+
char const* name,
30+
wchar_t const* encoding,
31+
wchar_t const* errors
3332
);
3433

3534
namespace PythonHelper {
@@ -39,13 +38,13 @@ bool pythonInited = false;
3938
bool initPythonRuntime() {
4039
if (!pythonInited) {
4140
script::py_interop::setPythonHomePath(lse::LegacyScriptEngine::getInstance().getSelf().getModDir());
42-
const char* pathEnv = std::getenv("PATH");
41+
char const* pathEnv = std::getenv("PATH");
4342
auto paths = ll::string_utils::splitByPattern(pathEnv, ";");
4443
std::vector<std::wstring> modulePaths;
4544
modulePaths.push_back(lse::LegacyScriptEngine::getInstance().getSelf().getModDir() / "lib");
4645
modulePaths.push_back(lse::LegacyScriptEngine::getInstance().getSelf().getModDir() / "DLLs");
4746
modulePaths.push_back(lse::LegacyScriptEngine::getInstance().getSelf().getModDir() / "site-packages");
48-
for (const auto& p : paths) {
47+
for (auto const& p : paths) {
4948
if (p.find("Python") != std::string::npos) {
5049
std::wstring wstr = ll::string_utils::str2wstr(p);
5150
modulePaths.push_back(wstr + L"\\DLLs");
@@ -60,30 +59,30 @@ bool initPythonRuntime() {
6059
}
6160

6261
bool loadPluginCode(
63-
std::shared_ptr<script::ScriptEngine> engine,
64-
std::string entryScriptPath,
65-
std::string pluginDirPath
62+
std::shared_ptr<script::ScriptEngine> const& engine,
63+
std::string const& entryScriptPath,
64+
std::string pluginDirPath
6665
) {
6766
// TODO: add import path to sys.path
6867
try {
6968
engine->loadFile(String::newString(entryScriptPath));
70-
} catch (const Exception& e1) {
69+
} catch (Exception const&) {
7170
// Fail
7271
lse::LegacyScriptEngine::getLogger().error("Fail in Loading Script Plugin!");
73-
throw e1;
72+
throw;
7473
}
7574
return true;
7675
}
7776

78-
std::string findEntryScript(const std::string& dirPath) {
77+
std::string findEntryScript(std::string const& dirPath) {
7978
auto dirPath_obj = std::filesystem::path(dirPath);
8079

8180
std::filesystem::path entryFilePath = dirPath_obj / "__init__.py";
8281
if (!std::filesystem::exists(entryFilePath)) return "";
8382
else return ll::string_utils::u8str2str(entryFilePath.u8string());
8483
}
8584

86-
std::string getPluginPackageName(const std::string& dirPath) {
85+
std::string getPluginPackageName(std::string const& dirPath) {
8786
auto dirPath_obj = std::filesystem::path(dirPath);
8887
std::string defaultReturnName = ll::string_utils::u8str2str(std::filesystem::path(dirPath).filename().u8string());
8988

@@ -103,7 +102,7 @@ std::string getPluginPackageName(const std::string& dirPath) {
103102
}
104103
}
105104

106-
std::string getPluginPackDependencyFilePath(const std::string& dirPath) {
105+
std::string getPluginPackDependencyFilePath(std::string const& dirPath) {
107106
auto dirPath_obj = std::filesystem::path(dirPath);
108107
std::filesystem::path packageFilePath = dirPath_obj / std::filesystem::path("pyproject.toml");
109108
std::filesystem::path requirementsFilePath = dirPath_obj / std::filesystem::path("requirements.txt");
@@ -159,7 +158,7 @@ static PyObject* getPyGlobalDict() {
159158
return PyModule_GetDict(m);
160159
}
161160

162-
bool processPythonDebugEngine(const std::string& cmd) {
161+
bool processPythonDebugEngine(std::string const& cmd) {
163162
auto& logger = lse::LegacyScriptEngine::getLogger();
164163
if (cmd == LLSE_DEBUG_CMD) {
165164
if (InConsoleDebugMode) {
@@ -224,7 +223,7 @@ bool processPythonDebugEngine(const std::string& cmd) {
224223
return true;
225224
}
226225

227-
bool processConsolePipCmd(const std::string& cmd) {
226+
bool processConsolePipCmd(std::string const& cmd) {
228227
#ifdef LSE_BACKEND_PYTHON
229228
if (cmd == "pip" || cmd.starts_with("pip ")) {
230229
PythonHelper::executePipCommand(cmd);
@@ -251,7 +250,7 @@ int executePipCommand(std::string cmd) {
251250
sa.bInheritHandle = TRUE;
252251

253252
STARTUPINFOW si = {0};
254-
PROCESS_INFORMATION pi = {0};
253+
PROCESS_INFORMATION pi = {nullptr};
255254
si.cb = sizeof(STARTUPINFO);
256255
GetStartupInfoW(&si);
257256

src/legacy/main/PythonHelper.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22
#include <ScriptX/ScriptX.h>
33
#include <filesystem>
4-
#include <map>
54
#include <string>
65

76
namespace PythonHelper {
@@ -10,18 +9,18 @@ bool initPythonRuntime();
109

1110
// raw, will throw exception if fail
1211
bool loadPluginCode(
13-
std::shared_ptr<script::ScriptEngine> engine,
14-
std::string entryScriptPath,
15-
std::string pluginDirPath
12+
std::shared_ptr<script::ScriptEngine> const& engine,
13+
std::string const& entryScriptPath,
14+
std::string pluginDirPath
1615
);
1716

18-
std::string findEntryScript(const std::string& dirPath);
19-
std::string getPluginPackageName(const std::string& dirPath);
20-
std::string getPluginPackDependencyFilePath(const std::string& dirPath);
17+
std::string findEntryScript(std::string const& dirPath);
18+
std::string getPluginPackageName(std::string const& dirPath);
19+
std::string getPluginPackDependencyFilePath(std::string const& dirPath);
2120

22-
bool processPythonDebugEngine(const std::string& cmd);
21+
bool processPythonDebugEngine(std::string const& cmd);
2322

24-
bool processConsolePipCmd(const std::string& cmd);
23+
bool processConsolePipCmd(std::string const& cmd);
2524
int executePipCommand(std::string cmd);
2625

2726
} // namespace PythonHelper

0 commit comments

Comments
 (0)