Crafter.Build/tests/WasmExceptions/fixture/main.cpp
Jorijn van der Graaf 2a05b8e6fb
test(wasm): the cases the review found
WasmBuildRefusals checks that -flto in compileFlags and in linkFlags,
-fno-exceptions and a shared library are each refused with their
cause, and that none of them leaves bin/ or build/ behind. With the
check disabled, each of them failed on a missing std.cppm instead,
after the build directories had been made.

WasiSysrootWithoutExceptions also takes its sysroot through missing
and without the target's directory, each with a message of its own,
checks that a refused build leaves nothing behind, and that a static
library passes without libunwind.

WasmExceptions catches an exception thrown inside libc++.a (stoi).
vector::at throws from a header compiled into the fixture, so the
fixture also passed against the libc++ built without exceptions. Its C
source uses setjmp, which the old C flags did not compile. An
exception that escapes main has to end the run as wasmtime's uncaught
exception, where any nonzero exit used to pass, a trap or a timeout
included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 06:10:41 +01:00

46 lines
1.3 KiB
C++

// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
// jump.c, compiled as C.
extern "C" std::int32_t JumpRoundTrip();
namespace {
std::int32_t unwound = 0;
struct Guard {
~Guard() { ++unwound; }
};
void Throw(std::int32_t depth) {
Guard guard;
if (depth == 0) throw std::runtime_error("thrown in wasm");
Throw(depth - 1);
}
}
int main(int argc, char** argv) {
// With an argument, nothing catches: the run has to fail.
if (argc > 1) Throw(0);
try {
Throw(3);
} catch (const std::runtime_error& e) {
std::println("caught: {} after {} destructors", e.what(), unwound);
}
try {
const std::vector<std::int32_t> empty;
std::println("unreachable: {}", empty.at(1));
} catch (const std::out_of_range&) {
std::println("caught: out_of_range from libc++");
}
// vector::at throws from a header, compiled into this unit; stoi throws
// inside libc++.a, which therefore has to be the exception-enabled one.
try {
std::println("unreachable: {}", std::stoi("not a number"));
} catch (const std::invalid_argument&) {
std::println("caught: invalid_argument from inside libc++");
}
std::println("longjmp came back with {}", JumpRoundTrip());
return 0;
}