ziglang/zig

zig c++ targeting wasi doesn't support throw

mm318 opened this issue · 1 comments

mm318 commented

Zig Version

0.12.0

Steps to Reproduce and Observed Behavior

Using the following source file hello.cpp:

#include <stdexcept>

int main() {
    throw std::runtime_error("HELLO WORLD!!");
    return 0;
}

Try to compile it with zig build-exe -target wasm32-wasi -lc++ hello.cpp and get the following error:

error: wasm-ld: /home/user/.cache/zig/o/5b3098130f329d0e12f11fb126f2b04f/hello.o: undefined symbol: __cxa_allocate_exception
error: wasm-ld: /home/user/.cache/zig/o/5b3098130f329d0e12f11fb126f2b04f/hello.o: undefined symbol: __cxa_throw

Expected Behavior

The compilation would succeed and a hello.wasm file is produced.

mm318 commented

Managed to workaround this by adding to the source file:

#include <stdexcept>
#include <typeinfo>

int main() {
    throw std::runtime_error("HELLO WORLD!!");
    return 0;
}

extern "C" {

void * __cxa_allocate_exception(size_t /*thrown_size*/) { abort(); }
void __cxa_throw(void */*thrown_object*/, std::type_info */*tinfo*/, void (*/*dest*/)(void *)) { abort(); }

}

Though not sure if this should be the user experience (haven't tried emscripten).