-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexception.c
58 lines (49 loc) · 1.34 KB
/
exception.c
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
48
49
50
51
52
53
54
55
56
57
58
#include "exception.h"
#include "stack.h"
#include "vstring.h"
#include <setjmp.h>
#include <stdlib.h>
static Stack *exception_stack = NULL;
static jmp_buf *current_frame = NULL;
static jmp_buf *pop_execution_context(void);
static char *last_exception_message = NULL;
static char *last_exception_information = NULL;
void enable_exceptions(void) {
exception_stack = create_stack();
}
void disable_exceptions(void) {
if (current_frame != NULL) {
free(current_frame);
current_frame = NULL;
}
destroy_stack(exception_stack, free);
exception_stack = NULL;
}
jmp_buf *push_execution_context(void) {
jmp_buf *frame = (jmp_buf *)malloc(sizeof(jmp_buf));
push(exception_stack, frame);
return frame;
}
void *throw_exception(void *information, char *message, ...) {
last_exception_message = message;
last_exception_information = information;
longjmp(*pop_execution_context(), 1);
return NULL;
}
void *rethrow(void) {
longjmp(*pop_execution_context(), 1);
return NULL;
}
char *exception_message(void) {
return last_exception_message;
}
void *exception_information(void) {
return last_exception_information;
}
static jmp_buf *pop_execution_context(void) {
if (current_frame != NULL) {
free(current_frame);
}
current_frame = pop(exception_stack);
return current_frame;
}