forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_returnvalue.cpp
More file actions
83 lines (74 loc) · 1.91 KB
/
async_returnvalue.cpp
File metadata and controls
83 lines (74 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2019 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.
#include <assert.h>
#include <stdio.h>
#include <emscripten.h>
// A "sync" tunnel that adds 1.
#if USE_EM_JS
EM_JS(int, sync_tunnel, (int value), {
return Asyncify.handleSleep((wakeUp) => {
setTimeout(function() {
wakeUp(value + 1);
}, 1);
});
})
EM_JS(int, sync_tunnel_bool, (bool value), {
return Asyncify.handleSleep((wakeUp) => {
setTimeout(function() {
wakeUp(!value);
}, 1);
});
})
#else
extern "C" int sync_tunnel(int);
extern "C" int sync_tunnel_bool(bool);
#endif
int main() {
#ifdef BAD
EM_ASM({
window.disableErrorReporting = true;
window.onerror = function(e) {
var success = e.toString().indexOf("import sync_tunnel was not in ASYNCIFY_IMPORTS, but changed the state") > 0;
if (success && !Module.reported) {
Module.reported = true;
console.log("reporting success");
// manually REPORT_RESULT; we shouldn't call back into native code at this point
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:8888/report_result?0");
xhr.onload = xhr.onerror = function() {
window.close();
};
xhr.send();
}
};
});
#endif
int x;
x = sync_tunnel(0);
assert(x == 1);
x = sync_tunnel(1);
assert(x == 2);
x = sync_tunnel(2);
assert(x == 3);
x = sync_tunnel(42);
assert(x == 43);
x = sync_tunnel(-1);
assert(x == 0);
x = sync_tunnel(-2);
assert(x == -1);
bool y;
y = sync_tunnel_bool(false);
assert(y == true);
y = sync_tunnel_bool(true);
assert(y == false);
#ifdef BAD
// We should not get here.
printf("We should not get here\n");
#else
// Success!
REPORT_RESULT(0);
#endif
return 0;
}