Skip to content

Instantly share code, notes, and snippets.

@xxuejie
Created November 21, 2012 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xxuejie/4128331 to your computer and use it in GitHub Desktop.
Save xxuejie/4128331 to your computer and use it in GitHub Desktop.
Stack manipulation setjmp with emscripten
$ 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
$ 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
#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