-
Notifications
You must be signed in to change notification settings - Fork 825
Expand file tree
/
Copy paththrow.h
More file actions
81 lines (66 loc) · 2.11 KB
/
throw.h
File metadata and controls
81 lines (66 loc) · 2.11 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
** @file mruby/throw.h - mruby exception throwing handler
**
** See Copyright Notice in mruby.h
**
** WARNING: This header is for mruby core internal use only.
** Do not include this header in user code or mrbgems.
**
** When MRB_USE_CXX_EXCEPTION is defined, this header requires C++
** compilation. C source files that include this header will fail
** to compile when linked into C++ projects using MRB_USE_CXX_EXCEPTION.
**
** For exception-safe code in mrbgems and user code, use the
** mrb_protect_error() API from <mruby/error.h> instead:
**
** #include <mruby/error.h>
**
** mrb_value my_func_body(mrb_state *mrb, void *data) {
** // code that may raise exceptions
** return result;
** }
**
** void my_func(mrb_state *mrb) {
** mrb_bool error;
** mrb_value result = mrb_protect_error(mrb, my_func_body, data, &error);
** // cleanup code runs here regardless of exception
** if (error) mrb_exc_raise(mrb, result);
** }
*/
#ifndef MRB_THROW_H
#define MRB_THROW_H
#if defined(MRB_USE_CXX_ABI) && !defined(__cplusplus)
# error Trying to use C++ exception handling in C code
#endif
#if defined(MRB_USE_CXX_EXCEPTION)
# if defined(__cplusplus)
#define MRB_TRY(buf) try {
#define MRB_CATCH(buf) } catch(mrb_jmpbuf *e) { if (e != (buf)) { throw e; }
#define MRB_END_EXC(buf) }
#define MRB_THROW(buf) throw(buf)
typedef void *mrb_jmpbuf_impl;
# else
# error "need to be compiled with C++ compiler"
# endif /* __cplusplus */
#else
#include <setjmp.h>
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#define MRB_SETJMP _setjmp
#define MRB_LONGJMP _longjmp
#elif defined(__MINGW64__) && !defined(_M_ARM64) && defined(__GNUC__) && __GNUC__ >= 4
#define MRB_SETJMP __builtin_setjmp
#define MRB_LONGJMP __builtin_longjmp
#else
#define MRB_SETJMP setjmp
#define MRB_LONGJMP longjmp
#endif
#define MRB_TRY(buf) if (MRB_SETJMP((buf)->impl) == 0) {
#define MRB_CATCH(buf) } else {
#define MRB_END_EXC(buf) }
#define MRB_THROW(buf) MRB_LONGJMP((buf)->impl, 1);
#define mrb_jmpbuf_impl jmp_buf
#endif
struct mrb_jmpbuf {
mrb_jmpbuf_impl impl;
};
#endif /* MRB_THROW_H */