Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/test_async_2.c
4150 views
1
// Copyright 2015 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
#include <stdio.h>
7
#include <emscripten.h>
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.
45
int x = 40;
46
int result = calc(x);
47
printf("calc(%d) = %d\n", x, result);
48
assert(result == 40);
49
return 0;
50
}
51
52
53