// Copyright 2015 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45#include <stdio.h>6#include <emscripten.h>7#include <assert.h>89__attribute__((noinline)) int calc(int x) {10printf("..%d..\n", x);11if (x == 0) {12// We reached the desired number of stack frames. Log out the stack trace13// for purposes of debugging this test.14char buffer[10000];15emscripten_get_callstack(EM_LOG_JS_STACK, buffer, 10000);16int newlines = 0;17char *b = buffer;18while (*b) {19if (*b == '\n') newlines++;20b++;21}22// The runtime adds more frames from JS, so do not look for a specific23// number, but ensure it is quite large.24if (newlines > 40) newlines = 40;25printf("stack: %s => %d, sleeping...\n", buffer, newlines);26// Sleep to test the saving of a fairly deep stack, including locals along27// the way.28emscripten_sleep(1);29printf("..and we're back, returning %d!\n", newlines);30// Return a value to test it passing through all the stack frames to the31// caller, after the sleep.32return newlines;33}34// Keep the recursion by passing the function pointer between C++ and JS, so35// that we have a deeply nested stack.36int (*fp)(int) = (int(*)(int))EM_ASM_PTR({37return $0;38}, &calc);39return fp(x - 1);40}4142int main() {43// Ensure at least 40 stack frames.44int x = 40;45int result = calc(x);46printf("calc(%d) = %d\n", x, result);47assert(result == 40);48return 0;49}50515253