commits
tags
// Runtime check 1: C++ runtime in a GCC-built XEX (static constructor,
// exception, std::regex, a 896 KB deep stack), reported with guide
// notifications.
#include <string>
#include <regex>
#include <stdexcept>
#include <cstdio>
#include "xex_runtime.h"
struct StaticInit {
int value;
StaticInit() : value(42) {}
};
static StaticInit g_static;
// Stack probe: recurses in 32 KB frames, logging each 64 KB to
// game:/stack-test.txt, so a crash shows how deep the main thread's stack
// goes (xex-link asks for 1 MB).
static void stackLog(int kb) {
if (FILE *f = fopen("game:/stack-test.txt", "a")) {
fprintf(f, "stack %d KB ok\n", kb);
fclose(f);
}
}
static int __attribute__((noinline)) stackDepth(int kb, int limitKb) {
volatile char frame[32 * 1024];
for (unsigned i = 0; i < sizeof(frame); i += 512) frame[i] = (char)kb;
if (kb % 64 == 0) stackLog(kb);
int r = kb >= limitKb ? kb : stackDepth(kb + 32, limitKb);
return r + frame[0] - (char)kb; // keeps the frame live
}
int main(int, char **) {
std::string result = "GCC XEX ok:";
result += g_static.value == 42 ? " ctor" : " CTOR-FAIL";
try {
throw std::runtime_error("x");
} catch (const std::exception &) {
result += " exc";
} catch (...) {
result += " EXC-WRONG";
}
std::smatch m;
std::string s = "#include foo.lua";
bool re = std::regex_search(s, m, std::regex("#include\\s+([a-z.]+)")) && m[1] == "foo.lua";
result += re ? " regex" : " REGEX-FAIL";
remove("game:/stack-test.txt");
int deepest = stackDepth(32, 896);
result += deepest >= 896 ? " stack896K" : " STACK-FAIL";
printf("hello: %s\n", result.c_str());
xex_notify("Hello from xexkit (GCC)");
xex_sleep_ms(3000);
xex_notify(result.c_str());
xex_sleep_ms(5000);
return 0;
}