cheatah
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"
7namespace cheatah::sys {
9// Definition of the storage declared `extern` in the header. Empty until the
10// cheatah runtime forwards the program's arguments via cheatah_set_argv().
11std::vector<std::string> argv; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables): `sys.argv` IS a process-wide variable by contract — programs read it directly
13/// @cond INTERNAL
14/// the runtime hook's definition; programs read `sys.argv`
15void 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 }
23/// @endcond
25} // namespace cheatah::sys
27// ---------------------------------------------------------------------------
28// The stable, exported entry point the cheatah runtime resolves with dlsym and
29// calls (before purr_main) to hand the program its command-line arguments. It is
30// `extern "C"` so the runtime can find it by a fixed name, and given default
31// visibility so it survives in the loaded module's dynamic symbol table. Only
32// programs that `import sys` carry this symbol; for the rest the runtime simply
33// finds nothing and forwards no arguments.
34#if defined(_WIN32)
35#define CHEATAH_SYS_EXPORT extern "C" __declspec(dllexport)
36#else
37#define CHEATAH_SYS_EXPORT extern "C" __attribute__((visibility("default")))
38#endif
40/// @cond INTERNAL
41/// the exported C hook the runtime dlsym()s; never cheatah-visible
42CHEATAH_SYS_EXPORT void cheatah_set_argv(int argc, char** argv) {
43 cheatah::sys::set_argv(argc, argv);
45/// @endcond