Skip to content

trigger setjmp on negative values #1195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/jsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ function JSify(data, functionsOnly, givenFunctions) {
if (ASM_JS && funcData.setjmpTable) {
// check if a longjmp was done. If a setjmp happened, check if ours. If ours, go to -111 to handle it.
// otherwise, just return - the call to us must also have been an invoke, so the setjmp propagates that way
ret += '; if (((__THREW__|0) != 0) & ((threwValue|0) > 0)) { setjmpLabel = ' + asmCoercion('_testSetjmp(' + makeGetValue('__THREW__', 0, 'i32') + ', setjmpTable)', 'i32') + '; if ((setjmpLabel|0) > 0) { label = -1111; break } else return ' + (funcData.returnType != 'void' ? asmCoercion('0', funcData.returnType) : '') + ' } __THREW__ = threwValue = 0;\n';
ret += '; if (((__THREW__|0) != 0) & ((threwValue|0) !== 0)) { setjmpLabel = ' + asmCoercion('_testSetjmp(' + makeGetValue('__THREW__', 0, 'i32') + ', setjmpTable)', 'i32') + '; if ((setjmpLabel|0) > 0) { label = -1111; break } else return ' + (funcData.returnType != 'void' ? asmCoercion('0', funcData.returnType) : '') + ' } __THREW__ = threwValue = 0;\n';
}

return ret;
Expand Down
24 changes: 15 additions & 9 deletions tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2403,28 +2403,34 @@ def test_longjmp(self):
static jmp_buf buf;

void second(void) {
printf("second\n"); // prints
longjmp(buf,1); // jumps back to where setjmp was called - making setjmp now return 1
printf("second\n");
longjmp(buf,-1);
}

void first(void) {
second();
printf("first\n"); // does not print
printf("first\n"); // prints
longjmp(buf,1); // jumps back to where setjmp was called - making setjmp now return 1
}

int main() {
volatile int x = 0;
if ( ! setjmp(buf) ) {
int jmpval = setjmp(buf);
if (!jmpval) {
x++; // should be properly restored once longjmp jumps back
first(); // when executed, setjmp returns 1
printf("skipped\n"); // does not print
} else if (jmpval == 1) { // when first() jumps back, setjmp returns 1
printf("result: %d %d\n", x, jmpval); // prints
x++;
first(); // when executed, setjmp returns 0
} else { // when longjmp jumps back, setjmp returns 1
printf("main: %d\n", x); // prints
second(); // when executed, setjmp returns -1
} else if (jmpval == -1) { // when second() jumps back, setjmp returns -1
printf("result: %d %d\n", x, jmpval); // prints
}

return 0;
}
'''
self.do_run(src, 'second\nmain: 1\n')
self.do_run(src, 'first\nresult: 1 1\nsecond\nresult: 2 -1')

def test_longjmp2(self):
src = r'''
Expand Down