Skip to content

Commit dc9bd8a

Browse files
authored
Use handleException library function also when calling main (emscripten-core#14876)
This was suggesting when I first added `handleException` and it seems like good idea and should be code size win.
1 parent dd5733a commit dc9bd8a

5 files changed

Lines changed: 38 additions & 22 deletions

File tree

emcc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,9 @@ def default_setting(name, new_default):
18261826
'$demangle',
18271827
'$demangleAll',
18281828
'$jsStackTrace',
1829-
'$stackTrace'
1829+
'$stackTrace',
1830+
# Called by `callMain` to handle exceptions
1831+
'$handleException'
18301832
]
18311833

18321834
if settings.FILESYSTEM and not settings.BOOTSTRAPPING_STRUCT_INFO and not settings.STANDALONE_WASM:

src/library.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,12 +3592,27 @@ LibraryManager.library = {
35923592
},
35933593

35943594
$handleException: function(e) {
3595-
if (e instanceof ExitStatus || e === 'unwind') {
3595+
// Certain exception types we do not treat as errors since they are used for
3596+
// internal control flow.
3597+
// 1. ExitStatus, which is thrown by exit()
3598+
// 2. "unwind", which is thrown by emscripten_unwind_to_js_event_loop() and others
3599+
// that wish to return to JS event loop.
3600+
if (e instanceof ExitStatus || e == 'unwind') {
35963601
return;
35973602
}
3598-
// And actual unexpected user-exectpion occured
3599-
if (e && typeof e === 'object' && e.stack) err('exception thrown: ' + [e, e.stack]);
3603+
// Anything else is an unexpected exception and we treat it as hard error.
3604+
var toLog = e;
3605+
#if ASSERTIONS
3606+
if (e && typeof e === 'object' && e.stack) {
3607+
toLog = [e, e.stack];
3608+
}
3609+
#endif
3610+
err('exception thrown: ' + toLog);
3611+
#if MINIMAL_RUNTIME
36003612
throw e;
3613+
#else
3614+
quit_(1, e);
3615+
#endif
36013616
},
36023617

36033618
#if !MINIMAL_RUNTIME

src/library_bootstrap.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ assert(false, "library_bootstrap.js only designed for use with BOOTSTRAPPING_STR
1414
assert(!LibraryManager.library);
1515
LibraryManager.library = {
1616
$callRuntimeCallbacks: function() {},
17+
$handleException: function(e) { if (!e instanceof ExitStatus && !e == 'unwind') throw e; },
1718
};

src/postamble.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,7 @@ function callMain(args) {
194194
exit(ret, /* implicit = */ true);
195195
}
196196
catch (e) {
197-
// Certain exception types we do not treat as errors since they are used for
198-
// internal control flow.
199-
// 1. ExitStatus, which is thrown by exit()
200-
// 2. "unwind", which is thrown by emscripten_unwind_to_js_event_loop() and others
201-
// that wish to return to JS event loop.
202-
if (e instanceof ExitStatus || e == 'unwind') {
203-
return;
204-
}
205-
// Anything else is an unexpected exception and we treat it as hard error.
206-
var toLog = e;
207-
if (e && typeof e === 'object' && e.stack) {
208-
toLog = [e, e.stack];
209-
}
210-
err('exception thrown: ' + toLog);
211-
quit_(1, e);
197+
handleException(e);
212198
#endif // !PROXY_TO_PTHREAD
213199
} finally {
214200
calledMain = true;

tests/test_core.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8238,8 +8238,12 @@ def test_safe_heap_user_js(self):
82388238
def test_safe_stack(self):
82398239
self.set_setting('STACK_OVERFLOW_CHECK', 2)
82408240
self.set_setting('TOTAL_STACK', 65536)
8241+
if is_optimizing(self.emcc_args):
8242+
expected = ['abort(stack overflow)']
8243+
else:
8244+
expected = ['abort(stack overflow)', '__handle_stack_overflow']
82418245
self.do_runf(test_file('core/test_safe_stack.c'),
8242-
expected_output=['abort(stack overflow)', '__handle_stack_overflow'],
8246+
expected_output=expected,
82438247
assert_returncode=NON_ZERO, assert_all=True)
82448248

82458249
@node_pthreads
@@ -8248,15 +8252,23 @@ def test_safe_stack_pthread(self):
82488252
self.set_setting('TOTAL_STACK', 65536)
82498253
self.set_setting('PROXY_TO_PTHREAD')
82508254
self.set_setting('USE_PTHREADS')
8255+
if is_optimizing(self.emcc_args):
8256+
expected = ['abort(stack overflow)']
8257+
else:
8258+
expected = ['abort(stack overflow)', '__handle_stack_overflow']
82518259
self.do_runf(test_file('core/test_safe_stack.c'),
8252-
expected_output=['abort(stack overflow)', '__handle_stack_overflow'],
8260+
expected_output=expected,
82538261
assert_returncode=NON_ZERO, assert_all=True)
82548262

82558263
def test_safe_stack_alloca(self):
82568264
self.set_setting('STACK_OVERFLOW_CHECK', 2)
82578265
self.set_setting('TOTAL_STACK', 65536)
8266+
if is_optimizing(self.emcc_args):
8267+
expected = ['abort(stack overflow)']
8268+
else:
8269+
expected = ['abort(stack overflow)', '__handle_stack_overflow']
82588270
self.do_runf(test_file('core/test_safe_stack_alloca.c'),
8259-
expected_output=['abort(stack overflow)', '__handle_stack_overflow'],
8271+
expected_output=expected,
82608272
assert_returncode=NON_ZERO, assert_all=True)
82618273

82628274
@needs_dylink

0 commit comments

Comments
 (0)