Skip to content

Commit 0ab97a0

Browse files
committed
Unify onerror handler used by test framework
1 parent 57270ce commit 0ab97a0

11 files changed

+76
-117
lines changed

src/postamble_minimal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function initRuntime(asm) {
6161
Module['_emscripten_tls_init'] = _emscripten_tls_init;
6262
Module['HEAPU32'] = HEAPU32;
6363
Module['__emscripten_thread_init'] = __emscripten_thread_init;
64+
Module['__emscripten_thread_exit'] = __emscripten_thread_exit;
6465
Module['_pthread_self'] = _pthread_self;
6566

6667
if (ENVIRONMENT_IS_PTHREAD) {

src/preamble.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,6 @@ assert(typeof Int32Array !== 'undefined' && typeof Float64Array !== 'undefined'
305305
'JS engine does not provide full typed array support');
306306
#endif
307307

308-
#if IN_TEST_HARNESS
309-
// Test runs in browsers should always be free from uncaught exceptions. If an uncaught exception is thrown, we fail browser test execution in the REPORT_RESULT() macro to output an error value.
310-
if (ENVIRONMENT_IS_WEB) {
311-
window.addEventListener('error', function(e) {
312-
if (e.message.includes('unwind')) return;
313-
console.error('Page threw an exception ' + e);
314-
Module['pageThrewException'] = true;
315-
});
316-
}
317-
#endif
318-
319308
#if IMPORTED_MEMORY
320309
// In non-standalone/normal mode, we create the memory here.
321310
#include "runtime_init_memory.js"

tests/browser/async_bad_list.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,10 @@
77
#include <emscripten.h>
88

99
int main() {
10-
int x = EM_ASM_INT({
11-
window.onerror = function(e) {
12-
var message = e.toString();
13-
var success = message.indexOf("unreachable") >= 0 || // firefox
14-
message.indexOf("Script error.") >= 0; // chrome
15-
if (success && !Module.reported) {
16-
Module.reported = true;
17-
console.log("reporting success");
18-
// manually REPORT_RESULT; we shouldn't call back into native code at this point
19-
var xhr = new XMLHttpRequest();
20-
xhr.open("GET", "http://localhost:8888/report_result?0");
21-
xhr.onload = xhr.onerror = function() {
22-
window.close();
23-
};
24-
xhr.send();
25-
}
26-
};
27-
return 0;
28-
});
29-
3010
emscripten_sleep(1);
3111

32-
// We should not get here - the unwind will fail as we did now all the right
33-
// functions - this function should be instrumented, but will not be.
12+
// We should not get here - the unwind will fail as we did not list all the
13+
// right functions - this function should be instrumented, but will not be.
3414
puts("We should not get here!");
35-
REPORT_RESULT(1);
36-
37-
return 0;
15+
return 1;
3816
}

tests/browser/async_returnvalue.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,6 @@ extern "C" int sync_tunnel_bool(bool);
3030
#endif
3131

3232
int main() {
33-
#ifdef BAD
34-
EM_ASM({
35-
window.onerror = function(e) {
36-
var success = e.toString().indexOf("import sync_tunnel was not in ASYNCIFY_IMPORTS, but changed the state") > 0;
37-
if (success && !Module.reported) {
38-
Module.reported = true;
39-
console.log("reporting success");
40-
// manually REPORT_RESULT; we shouldn't call back into native code at this point
41-
var xhr = new XMLHttpRequest();
42-
xhr.open("GET", "http://localhost:8888/report_result?0");
43-
xhr.onload = xhr.onerror = function() {
44-
window.close();
45-
};
46-
xhr.send();
47-
}
48-
};
49-
});
50-
#endif
5133
int x;
5234
x = sync_tunnel(0);
5335
assert(x == 1);
@@ -68,15 +50,13 @@ int main() {
6850
y = sync_tunnel_bool(true);
6951
assert(y == false);
7052

71-
72-
7353
#ifdef BAD
7454
// We should not get here.
7555
printf("We should not get here\n");
56+
return 1;
7657
#else
7758
// Success!
7859
REPORT_RESULT(0);
79-
#endif
80-
8160
return 0;
61+
#endif
8262
}

tests/browser_reporting.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
var hasModule = typeof Module === 'object' && Module;
2+
var hasWindow = typeof window === 'object' && window;
3+
var keepWindowAlive = false;
24

35
/** @param {boolean=} sync
46
@param {number=} port */
57
function reportResultToServer(result, sync, port) {
68
port = port || 8888;
79
if (reportResultToServer.reported) {
810
// Only report one result per test, even if the test misbehaves and tries to report more.
9-
reportErrorToServer("excessive reported results, sending " + result + ", test will fail");
11+
reportStderrToServer("excessive reported results, sending " + result + ", test will fail");
1012
}
1113
reportResultToServer.reported = true;
1214
var xhr = new XMLHttpRequest();
13-
if (hasModule && Module['pageThrewException']) {
14-
result = 'pageThrewException';
15-
}
1615
xhr.open('GET', 'http://localhost:' + port + '/report_result?' + result, !sync);
1716
xhr.send();
18-
if (typeof window === 'object' && window && hasModule && !Module['pageThrewException'] /* for easy debugging, don't close window on failure */) setTimeout(function() { window.close() }, 1000);
17+
if (hasWindow && hasModule && !keepWindowAlive) {
18+
setTimeout(function() { window.close() }, 1000);
19+
}
1920
}
2021

2122
/** @param {boolean=} sync
@@ -25,18 +26,35 @@ function maybeReportResultToServer(result, sync, port) {
2526
reportResultToServer(result, sync, port);
2627
}
2728

28-
function reportErrorToServer(message) {
29+
function reportStderrToServer(message) {
2930
var xhr = new XMLHttpRequest();
3031
xhr.open('GET', encodeURI('http://localhost:8888?stderr=' + message));
3132
xhr.send();
3233
}
3334

35+
function reportExceptionToServer(e) {
36+
var xhr = new XMLHttpRequest();
37+
xhr.open('GET', encodeURI('http://localhost:8888?exception=' + e.message + ' / ' + e.stack));
38+
xhr.send();
39+
}
40+
3441
if (typeof window === 'object' && window) {
3542
function report_error(e) {
43+
var message = e.message || e;
44+
if (e.error) {
45+
message = e.error.message;
46+
}
47+
// MINIMAL_RUNTIME lets unwind exceptions remain uncaught, and these
48+
// should not be considered actually errors.
49+
if (message == 'unwind') {
50+
return;
51+
}
52+
console.error("emtest: got top level error: " + message);
53+
// We report aborts via the onAbort handler so can ignore them here
54+
if (message.indexOf(' abort(') != -1)
55+
return;
3656
// MINIMAL_RUNTIME doesn't handle exit or call the below onExit handler
3757
// so we detect the exit by parsing the uncaught exception message.
38-
var message = e.message || e;
39-
console.error("got top level error: " + message);
4058
var offset = message.indexOf('exit(');
4159
if (offset != -1) {
4260
var status = message.substring(offset + 5);
@@ -45,9 +63,14 @@ if (typeof window === 'object' && window) {
4563
console.error(status);
4664
maybeReportResultToServer('exit:' + status);
4765
} else {
48-
var xhr = new XMLHttpRequest();
49-
xhr.open('GET', encodeURI('http://localhost:8888?exception=' + e.message + ' / ' + e.stack));
50-
xhr.send();
66+
reportExceptionToServer(e);
67+
/*
68+
* Also report the exception as the result of the test if non has been
69+
* reported yet
70+
* For easy debugging, don't close window on failure.
71+
*/
72+
keepWindowAlive = true;
73+
maybeReportResultToServer('exception:' + e.message);
5174
}
5275
}
5376
window.addEventListener('error', report_error);

tests/common.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ def assert_out_queue_empty(self, who):
13341334
# synchronously, so we have a timeout, which can be hit if the VM
13351335
# we run on stalls temporarily), so we let each test try more than
13361336
# once by default
1337-
def run_browser(self, html_file, message, expectedResult=None, timeout=None, extra_tries=1):
1337+
def run_browser(self, html_file, message, expectedResult=None, timeout=None, extra_tries=1, assert_all=False):
13381338
if not has_browser():
13391339
return
13401340
if BrowserCore.unresponsive_tests >= BrowserCore.MAX_UNRESPONSIVE_TESTS:
@@ -1372,12 +1372,16 @@ def run_browser(self, html_file, message, expectedResult=None, timeout=None, ext
13721372
# verify the result, and try again if we should do so
13731373
output = unquote(output)
13741374
try:
1375-
self.assertContained(expectedResult, output)
1375+
if assert_all:
1376+
for o in expectedResult:
1377+
self.assertContained(o, output)
1378+
else:
1379+
self.assertContained(expectedResult, output)
13761380
except Exception as e:
13771381
if extra_tries > 0:
13781382
print('[test error (see below), automatically retrying]')
13791383
print(e)
1380-
return self.run_browser(html_file, message, expectedResult, timeout, extra_tries - 1)
1384+
return self.run_browser(html_file, message, expectedResult, timeout, extra_tries - 1, assert_all=assert_all)
13811385
else:
13821386
raise e
13831387
finally:

tests/emscripten_throw_number.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33
int main()
44
{
55
emscripten_throw_number(42);
6-
#ifdef REPORT_RESULT
7-
REPORT_RESULT(1); // failed
8-
#endif
6+
__builtin_trap();
97
}

tests/emscripten_throw_number_pre.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/emscripten_throw_string.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33
int main()
44
{
55
emscripten_throw_string("Hello!");
6-
#ifdef REPORT_RESULT
7-
REPORT_RESULT(1); // failed
8-
#endif
6+
__builtin_trap();
97
}

tests/emscripten_throw_string_pre.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)