@@ -1543,6 +1543,61 @@ def test_exceptions_rethrow_missing(self):
15431543 create_file ('main.cpp' , 'int main() { throw; }' )
15441544 self .do_runf ('main.cpp' , None , assert_returncode = NON_ZERO )
15451545
1546+ def test_format_exception (self ):
1547+ self .set_setting ('DISABLE_EXCEPTION_CATCHING' , 0 )
1548+ self .set_setting ('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE' , ['$formatException' ])
1549+ self .set_setting ('EXPORTED_FUNCTIONS' , ['_main' , 'formatException' , '_emscripten_format_exception' , '_free' ])
1550+ self .maybe_closure ()
1551+ src = '''
1552+ #include <emscripten.h>
1553+ #include <exception>
1554+ #include <stdexcept>
1555+ using namespace std;
1556+
1557+ class myexception : public exception {
1558+ virtual const char* what() const throw() { return "My exception happened"; }
1559+ } myex;
1560+
1561+ EMSCRIPTEN_KEEPALIVE extern "C" void throw_exc(int x) {
1562+ if (x == 1) {
1563+ throw 1000;
1564+ }
1565+ if (x == 2) {
1566+ throw 'c';
1567+ }
1568+ if (x == 3) {
1569+ throw runtime_error("abc");
1570+ }
1571+ if (x == 4) {
1572+ throw myex;
1573+ }
1574+ if (x == 5) {
1575+ throw "abc";
1576+ }
1577+ }
1578+
1579+ int main() {
1580+ EM_ASM({
1581+ for (let i = 1; i < 6; i++){
1582+ try {
1583+ Module["_throw_exc"](i);
1584+ } catch(p) {
1585+ console.log(Module["formatException"](p).replace(/0x[0-9a-f]*/, "xxx"));
1586+ }
1587+ }
1588+ });
1589+ }
1590+ '''
1591+ expected = '''\
1592+ Cpp Exception: The exception is an object of type 'int' at address xxx which does not inherit from std::exception
1593+ Cpp Exception: The exception is an object of type 'char' at address xxx which does not inherit from std::exception
1594+ Cpp Exception std::runtime_error: abc
1595+ Cpp Exception myexception: My exception happened
1596+ Cpp Exception: The exception is an object of type 'char const*' at address xxx which does not inherit from std::exception
1597+ '''
1598+
1599+ self .do_run (src , expected )
1600+
15461601 @with_both_eh_sjlj
15471602 def test_bad_typeid (self ):
15481603 self .do_run (r'''
0 commit comments