| 8 | #include <assert.h> |
| 9 | |
| 10 | __attribute__((noinline)) int calc(int x) { |
| 11 | printf("..%d..\n", x); |
| 12 | if (x == 0) { |
| 13 | // We reached the desired number of stack frames. Log out the stack trace |
| 14 | // for purposes of debugging this test. |
| 15 | char buffer[10000]; |
| 16 | emscripten_get_callstack(EM_LOG_JS_STACK, buffer, 10000); |
| 17 | int newlines = 0; |
| 18 | char *b = buffer; |
| 19 | while (*b) { |
| 20 | if (*b == '\n') newlines++; |
| 21 | b++; |
| 22 | } |
| 23 | // The runtime adds more frames from JS, so do not look for a specific |
| 24 | // number, but ensure it is quite large. |
| 25 | if (newlines > 40) newlines = 40; |
| 26 | printf("stack: %s => %d, sleeping...\n", buffer, newlines); |
| 27 | // Sleep to test the saving of a fairly deep stack, including locals along |
| 28 | // the way. |
| 29 | emscripten_sleep(1); |
| 30 | printf("..and we're back, returning %d!\n", newlines); |
| 31 | // Return a value to test it passing through all the stack frames to the |
| 32 | // caller, after the sleep. |
| 33 | return newlines; |
| 34 | } |
| 35 | // Keep the recursion by passing the function pointer between C++ and JS, so |
| 36 | // that we have a deeply nested stack. |
| 37 | int (*fp)(int) = (int(*)(int))EM_ASM_PTR({ |
| 38 | return $0; |
| 39 | }, &calc); |
| 40 | return fp(x - 1); |
| 41 | } |
| 42 | |
| 43 | int main() { |
| 44 | // Ensure at least 40 stack frames. |