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;13
/// @cond INTERNAL — the runtime hook's definition; programs read `sys.argv`14
void set_argv(int argc, char** argv_) {15
argv.clear();16
if (argc < 0 || argv_ == nullptr) return;17
argv.reserve(static_cast<std::size_t>(argc));18
for (int i = 0; i < argc; ++i) {19
argv.emplace_back(argv_[i] ? argv_[i] : "");20
}21
}22
/// @endcond24
} // namespace cheatah::sys26
// ---------------------------------------------------------------------------27
// The stable, exported entry point the cheatah runtime resolves with dlsym and28
// calls (before purr_main) to hand the program its command-line arguments. It is29
// `extern "C"` so the runtime can find it by a fixed name, and given default30
// visibility so it survives in the loaded module's dynamic symbol table. Only31
// programs that `import sys` carry this symbol; for the rest the runtime simply32
// finds nothing and forwards no arguments.33
#if defined(_WIN32)34
#define CHEATAH_SYS_EXPORT extern "C" __declspec(dllexport)35
#else36
#define CHEATAH_SYS_EXPORT extern "C" __attribute__((visibility("default")))37
#endif39
/// @cond INTERNAL — the exported C hook the runtime dlsym()s; never cheatah-visible40
CHEATAH_SYS_EXPORT void cheatah_set_argv(int argc, char** argv) {41
cheatah::sys::set_argv(argc, argv);42
}43
/// @endcond