| 10 | #include <emscripten.h> |
| 11 | |
| 12 | struct Class { |
| 13 | static Class *instance; |
| 14 | |
| 15 | int x; |
| 16 | |
| 17 | Class() : x(0) {} |
| 18 | ~Class() { |
| 19 | // Destructor should never be called. |
| 20 | assert(false); |
| 21 | x = -9999; |
| 22 | } |
| 23 | |
| 24 | void print() { |
| 25 | printf("waka %d\n", x++); |
| 26 | |
| 27 | if (x == 7 || x < 0) { |
| 28 | emscripten_cancel_main_loop(); |
| 29 | exit(x == 7 ? 0 : 1); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | static void callback() { |
| 34 | instance->print(); |
| 35 | } |
| 36 | |
| 37 | void start() { |
| 38 | instance = this; |
| 39 | |
| 40 | EM_ASM({ |
| 41 | var initial = stackSave(); |
| 42 | out('seeing initial stack of ' + initial); |
| 43 | setTimeout(function() { |
| 44 | var current = stackSave(); |
| 45 | out('seeing later stack of ' + current); |
| 46 | assert(current === initial); |
| 47 | }, 0); |
| 48 | }); |
| 49 | |
| 50 | // important if we simulate an infinite loop here or not. With an infinite loop, the |
| 51 | // destructor should *NOT* have been called |
| 52 | emscripten_set_main_loop(Class::callback, 3, 1); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | Class *Class::instance = NULL; |
| 57 | |