File: test_em_async_js.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (47 lines) | stat: -rw-r--r-- 1,073 bytes parent folder | download | duplicates (2)
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;
}