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;
13/// @cond INTERNAL — the runtime hook's definition; programs read `sys.argv`
14void 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 }
22/// @endcond
24} // namespace cheatah::sys
26// ---------------------------------------------------------------------------
27// The stable, exported entry point the cheatah runtime resolves with dlsym and
28// calls (before purr_main) to hand the program its command-line arguments. It is
29// `extern "C"` so the runtime can find it by a fixed name, and given default
30// visibility so it survives in the loaded module's dynamic symbol table. Only
31// programs that `import sys` carry this symbol; for the rest the runtime simply
32// finds nothing and forwards no arguments.
33#if defined(_WIN32)
34#define CHEATAH_SYS_EXPORT extern "C" __declspec(dllexport)
35#else
36#define CHEATAH_SYS_EXPORT extern "C" __attribute__((visibility("default")))
37#endif
39/// @cond INTERNAL — the exported C hook the runtime dlsym()s; never cheatah-visible
40CHEATAH_SYS_EXPORT void cheatah_set_argv(int argc, char** argv) {
41 cheatah::sys::set_argv(argc, argv);
43/// @endcond