Source
stdlib/sys/sys.cpp
1
// Copyright (c) 2026 BigBrain LLC. MIT-licensed (see LICENSE).2
// Original work; see ACKNOWLEDGMENTS.md for the open-source ideas we build upon.3
// cheatah `sys` module implementation — see sys.hpp for the interface.5
#include "sys.hpp"7
namespace cheatah::sys {9
// Definition of the storage declared `extern` in the header. Empty until the10
// cheatah runtime forwards the program's arguments via cheatah_set_argv().11
std::vector<std::string> argv; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables): `sys.argv` IS a process-wide variable by contract — programs read it directly13
/// @cond INTERNAL14
/// the runtime hook's definition; programs read `sys.argv`15
void set_argv(int argc, char** argv_) {16
argv.clear();17
if (argc < 0 || argv_ == nullptr) return;18
argv.reserve(static_cast<std::size_t>(argc));19
for (int i = 0; i < argc; ++i) {20
argv.emplace_back(argv_[i] ? argv_[i] : "");21
}22
}23
/// @endcond25
} // namespace cheatah::sys27
// ---------------------------------------------------------------------------28
// The stable, exported entry point the cheatah runtime resolves with dlsym and29
// calls (before purr_main) to hand the program its command-line arguments. It is30
// `extern "C"` so the runtime can find it by a fixed name, and given default31
// visibility so it survives in the loaded module's dynamic symbol table. Only32
// programs that `import sys` carry this symbol; for the rest the runtime simply33
// finds nothing and forwards no arguments.34
#if defined(_WIN32)35
#define CHEATAH_SYS_EXPORT extern "C" __declspec(dllexport)36
#else37
#define CHEATAH_SYS_EXPORT extern "C" __attribute__((visibility("default")))38
#endif40
/// @cond INTERNAL41
/// the exported C hook the runtime dlsym()s; never cheatah-visible42
CHEATAH_SYS_EXPORT void cheatah_set_argv(int argc, char** argv) {43
cheatah::sys::set_argv(argc, argv);44
}45
/// @endcond