Emscripten ‘val.h’ API allows calling methods of JS objects, however, C++ try-catch won’t catch JS exception. Consider this example:
#include <emscripten.h>
#include <emscripten/val.h>
void test(){
string t = "some invalid json";
val v = val::object();
// This C++ try-catch doesn't catch JS exception
try {
v = val::global("JSON").call<val>("parse", t);
cout <<"ok" <<endl;
}
catch(...){
cout <<"failed" <<endl;
}
cout <<"ret" <<endl;
}
The JS exception makes the ‘test’ function stop and no ok
, no failed
, no ret
printed out. How to catch that JS exception thrown by JSON.parse
?
There’s 1 issue here but it’s still open: https://github.com/emscripten-core/emscripten/issues/11496