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
|
#include <emscripten.h>
#include <stdio.h>
double result = 0.0;
EMSCRIPTEN_KEEPALIVE
void inc_result() {
puts("inc result");
// this will be called twice, leading to 4.2 as the final result.
// (we use floating-point numbers here to make sure that the final result is
// returned using the proper type, and not an int)
result += 2.1;
}
EMSCRIPTEN_KEEPALIVE
double get_result() {
return result;
}
EM_ASYNC_JS(double, foo, (int timeout), {
await new Promise(resolve => setTimeout(
() => {
// Do a simple call back into compiled code synchronously. This is
// allowed (an async one would not).
_inc_result();
// Do the same with ccall.
ccall("inc_result");
// Finish the async operation.
out("resolving promise");
resolve();
},
timeout
));
// Do another simple call back into compiled code synchronously.
return _get_result();
});
// Test out void return and no params.
EM_ASYNC_JS(void, bar, (), {
return;
});
int main() {
printf("foo returned: %f\n", foo(10));
bar();
return 0;
}
|