Created
November 21, 2012 22:43
-
-
Save xxuejie/4128331 to your computer and use it in GitHub Desktop.
Stack manipulation setjmp with emscripten
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ clang stack-manipulation.c | |
$ ./a.out | |
Entering stack_manipulate_func, level: 0 | |
Setjmp normal execution path, level: 0 | |
Entering stack_manipulate_func, level: 1 | |
Perform longjmp at level 1 | |
Setjmp error execution path, level: 0 | |
Exiting stack_manipulate_func, level: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ emcc stack-manipulation.c | |
$ node ./a.out.js | |
Entering stack_manipulate_func, level: 0 | |
Setjmp normal execution path, level: 0 | |
Entering stack_manipulate_func, level: 1 | |
Perform longjmp at level 1 | |
Setjmp error execution path, level: 1 | |
Exiting stack_manipulate_func, level: 1 | |
Exiting stack_manipulate_func, level: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <setjmp.h> | |
#include <stdio.h> | |
typedef struct { | |
jmp_buf* jmp; | |
} jmp_state; | |
void stack_manipulate_func(jmp_state* s, int level) { | |
jmp_buf buf; | |
printf("Entering stack_manipulate_func, level: %d\n", level); | |
if (level == 0) { | |
s->jmp = &buf; | |
if (setjmp(*(s->jmp)) == 0) { | |
printf("Setjmp normal execution path, level: %d\n", level); | |
stack_manipulate_func(s, level + 1); | |
} else { | |
printf("Setjmp error execution path, level: %d\n", level); | |
} | |
} else { | |
printf("Perform longjmp at level %d\n", level); | |
longjmp(*(s->jmp), 1); | |
} | |
printf("Exiting stack_manipulate_func, level: %d\n", level); | |
} | |
int main(int argc, char *argv[]) { | |
jmp_state s; | |
s.jmp = NULL; | |
stack_manipulate_func(&s, 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment