Skip to content

Commit

Permalink
Cleanup JavaScript assertion info when message is undefined (#15572)
Browse files Browse the repository at this point in the history
When JS libray code calles `assert` without a message we were aborting
with `Aborted(Assertion failed: undefined)` which can be confusing
because it sounds like the assert was related to something being
undefined at runtime where as the undefined here was just the missing
message text.
  • Loading branch information
sbc100 authored Dec 1, 2021
1 parent 25546fd commit b6fb2b0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ var EXITSTATUS;
/** @type {function(*, string=)} */
function assert(condition, text) {
if (!condition) {
abort('Assertion failed: ' + text);
#if ASSERTIONS
abort('Assertion failed' + (text ? ': ' + text : ''));
#else
// This build was created without ASSERTIONS defined. `assert()` should not
// ever be called in this configuration but in case there are callers in
// the wild leave this simple abort() implemenation here for now.
abort(text);
#endif
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function dump(item) {

function assert(a, msg) {
if (!a) {
msg = 'Assertion failed: ' + msg;
msg = 'Assertion failed' + (msg ? ': ' + msg : '');
print(msg);
printErr('Stack: ' + new Error().stack);
throw msg;
Expand Down
2 changes: 2 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7132,6 +7132,8 @@ def test_webidl(self, mode, allow_memory_growth):
# Export things on "TheModule". This matches the typical use pattern of the bound library
# being used as Box2D.* or Ammo.*, and we cannot rely on "Module" being always present (closure may remove it).
self.emcc_args += ['-s', 'EXPORTED_FUNCTIONS=_malloc,_free', '--post-js=glue.js', '--extern-post-js=extern-post.js']
if mode == 'ALL':
self.emcc_args += ['-sASSERTIONS']
if allow_memory_growth:
self.set_setting('ALLOW_MEMORY_GROWTH')

Expand Down
2 changes: 0 additions & 2 deletions tests/webidl/output_ALL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,3 @@ Aborted(Assertion failed: [CHECK FAILED] Parent::Parent(val:val): Expecting <int
Parent:42
Aborted(Assertion failed: [CHECK FAILED] Parent::voidStar(something:something): Expecting <pointer>)
Aborted(Assertion failed: [CHECK FAILED] StringUser::Print(anotherString:anotherString): Expecting <string>)

done.

0 comments on commit b6fb2b0

Please sign in to comment.