* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / eval_intern.h
blob266a84601e9eb86f74cdf1e52cdf40c893d6467e
1 #ifndef RUBY_EVAL_INTERN_H
2 #define RUBY_EVAL_INTERN_H
4 #include "ruby/ruby.h"
5 #include "vm_core.h"
7 static inline void
8 vm_passed_block_handler_set(rb_execution_context_t *ec, VALUE block_handler)
10 vm_block_handler_verify(block_handler);
11 ec->passed_block_handler = block_handler;
14 static inline void
15 pass_passed_block_handler(rb_execution_context_t *ec)
17 VALUE block_handler = rb_vm_frame_block_handler(ec->cfp);
18 vm_passed_block_handler_set(ec, block_handler);
19 VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_PASSED);
22 #define PASS_PASSED_BLOCK_HANDLER_EC(ec) pass_passed_block_handler(ec)
23 #define PASS_PASSED_BLOCK_HANDLER() pass_passed_block_handler(GET_EC())
25 #ifdef HAVE_STDLIB_H
26 #include <stdlib.h>
27 #endif
28 #ifndef EXIT_SUCCESS
29 #define EXIT_SUCCESS 0
30 #endif
31 #ifndef EXIT_FAILURE
32 #define EXIT_FAILURE 1
33 #endif
35 #include <stdio.h>
36 #include <setjmp.h>
38 #ifdef __APPLE__
39 # ifdef HAVE_CRT_EXTERNS_H
40 # include <crt_externs.h>
41 # else
42 # include "missing/crt_externs.h"
43 # endif
44 #endif
46 #ifndef HAVE_STRING_H
47 char *strrchr(const char *, const char);
48 #endif
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
54 #ifdef HAVE_NET_SOCKET_H
55 #include <net/socket.h>
56 #endif
58 #define ruby_setjmp(env) RUBY_SETJMP(env)
59 #define ruby_longjmp(env,val) RUBY_LONGJMP((env),(val))
60 #ifdef __CYGWIN__
61 # ifndef _setjmp
62 int _setjmp(jmp_buf);
63 # endif
64 # ifndef _longjmp
65 NORETURN(void _longjmp(jmp_buf, int));
66 # endif
67 #endif
69 #include <sys/types.h>
70 #include <signal.h>
71 #include <errno.h>
73 #ifdef HAVE_SYS_SELECT_H
74 #include <sys/select.h>
75 #endif
78 Solaris sys/select.h switches select to select_large_fdset to support larger
79 file descriptors if FD_SETSIZE is larger than 1024 on 32bit environment.
80 But Ruby doesn't change FD_SETSIZE because fd_set is allocated dynamically.
81 So following definition is required to use select_large_fdset.
83 #ifdef HAVE_SELECT_LARGE_FDSET
84 #define select(n, r, w, e, t) select_large_fdset((n), (r), (w), (e), (t))
85 extern int select_large_fdset(int, fd_set *, fd_set *, fd_set *, struct timeval *);
86 #endif
88 #ifdef HAVE_SYS_PARAM_H
89 #include <sys/param.h>
90 #endif
92 #include <sys/stat.h>
95 #define SAVE_ROOT_JMPBUF(th, stmt) do \
96 if (true) { \
97 stmt; \
98 } \
99 else if (th) { /* suppress unused-variable warning */ \
100 } while (0)
102 #define EC_PUSH_TAG(ec) do { \
103 rb_execution_context_t * const _ec = (ec); \
104 struct rb_vm_tag _tag; \
105 _tag.state = TAG_NONE; \
106 _tag.tag = Qundef; \
107 _tag.prev = _ec->tag; \
108 _tag.lock_rec = rb_ec_vm_lock_rec(_ec); \
110 #define EC_POP_TAG() \
111 _ec->tag = _tag.prev; \
112 } while (0)
114 #define EC_TMPPOP_TAG() \
115 _ec->tag = _tag.prev
117 #define EC_REPUSH_TAG() (void)(_ec->tag = &_tag)
119 #if defined __GNUC__ && __GNUC__ == 4 && (__GNUC_MINOR__ >= 6 && __GNUC_MINOR__ <= 8) || defined __clang__
120 /* This macro prevents GCC 4.6--4.8 from emitting maybe-uninitialized warnings.
121 * This macro also prevents Clang from dumping core in EC_EXEC_TAG().
122 * (I confirmed Clang 4.0.1 and 5.0.0.)
124 # define VAR_FROM_MEMORY(var) __extension__(*(__typeof__(var) volatile *)&(var))
125 # define VAR_INITIALIZED(var) ((var) = VAR_FROM_MEMORY(var))
126 # define VAR_NOCLOBBERED(var) volatile var
127 #else
128 # define VAR_FROM_MEMORY(var) (var)
129 # define VAR_INITIALIZED(var) ((void)&(var))
130 # define VAR_NOCLOBBERED(var) var
131 #endif
133 static inline void
134 rb_ec_vm_lock_rec_check(const rb_execution_context_t *ec, unsigned int recorded_lock_rec)
136 unsigned int current_lock_rec = rb_ec_vm_lock_rec(ec);
137 if (current_lock_rec != recorded_lock_rec) {
138 rb_ec_vm_lock_rec_release(ec, recorded_lock_rec, current_lock_rec);
142 /* clear ec->tag->state, and return the value */
143 static inline int
144 rb_ec_tag_state(const rb_execution_context_t *ec)
146 struct rb_vm_tag *tag = ec->tag;
147 enum ruby_tag_type state = tag->state;
148 tag->state = TAG_NONE;
149 rb_ec_vm_lock_rec_check(ec, tag->lock_rec);
150 return state;
153 NORETURN(static inline void rb_ec_tag_jump(const rb_execution_context_t *ec, enum ruby_tag_type st));
154 static inline void
155 rb_ec_tag_jump(const rb_execution_context_t *ec, enum ruby_tag_type st)
157 ec->tag->state = st;
158 ruby_longjmp(ec->tag->buf, 1);
162 setjmp() in assignment expression rhs is undefined behavior
163 [ISO/IEC 9899:1999] 7.13.1.1
165 #define EC_EXEC_TAG() \
166 (ruby_setjmp(_tag.buf) ? rb_ec_tag_state(VAR_FROM_MEMORY(_ec)) : (EC_REPUSH_TAG(), 0))
168 #define EC_JUMP_TAG(ec, st) rb_ec_tag_jump(ec, st)
170 #define INTERNAL_EXCEPTION_P(exc) FIXNUM_P(exc)
172 /* CREF operators */
174 #define CREF_FL_PUSHED_BY_EVAL IMEMO_FL_USER1
175 #define CREF_FL_OMOD_SHARED IMEMO_FL_USER2
176 #define CREF_FL_SINGLETON IMEMO_FL_USER3
178 static inline int CREF_SINGLETON(const rb_cref_t *cref);
180 static inline VALUE
181 CREF_CLASS(const rb_cref_t *cref)
183 if (CREF_SINGLETON(cref)) {
184 return CLASS_OF(cref->klass_or_self);
186 else {
187 return cref->klass_or_self;
191 static inline VALUE
192 CREF_CLASS_FOR_DEFINITION(const rb_cref_t *cref)
194 if (CREF_SINGLETON(cref)) {
195 return rb_singleton_class(cref->klass_or_self);
197 else {
198 return cref->klass_or_self;
202 static inline rb_cref_t *
203 CREF_NEXT(const rb_cref_t *cref)
205 return cref->next;
208 static inline const rb_scope_visibility_t *
209 CREF_SCOPE_VISI(const rb_cref_t *cref)
211 return &cref->scope_visi;
214 static inline VALUE
215 CREF_REFINEMENTS(const rb_cref_t *cref)
217 return cref->refinements;
220 static inline void
221 CREF_REFINEMENTS_SET(rb_cref_t *cref, VALUE refs)
223 RB_OBJ_WRITE(cref, &cref->refinements, refs);
226 static inline int
227 CREF_PUSHED_BY_EVAL(const rb_cref_t *cref)
229 return cref->flags & CREF_FL_PUSHED_BY_EVAL;
232 static inline void
233 CREF_PUSHED_BY_EVAL_SET(rb_cref_t *cref)
235 cref->flags |= CREF_FL_PUSHED_BY_EVAL;
238 static inline int
239 CREF_SINGLETON(const rb_cref_t *cref)
241 return cref->flags & CREF_FL_SINGLETON;
244 static inline void
245 CREF_SINGLETON_SET(rb_cref_t *cref)
247 cref->flags |= CREF_FL_SINGLETON;
250 static inline int
251 CREF_OMOD_SHARED(const rb_cref_t *cref)
253 return cref->flags & CREF_FL_OMOD_SHARED;
256 static inline void
257 CREF_OMOD_SHARED_SET(rb_cref_t *cref)
259 cref->flags |= CREF_FL_OMOD_SHARED;
262 static inline void
263 CREF_OMOD_SHARED_UNSET(rb_cref_t *cref)
265 cref->flags &= ~CREF_FL_OMOD_SHARED;
268 enum {
269 RAISED_EXCEPTION = 1,
270 RAISED_STACKOVERFLOW = 2,
271 RAISED_NOMEMORY = 4
273 #define rb_ec_raised_set(ec, f) ((ec)->raised_flag |= (f))
274 #define rb_ec_raised_reset(ec, f) ((ec)->raised_flag &= ~(f))
275 #define rb_ec_raised_p(ec, f) (((ec)->raised_flag & (f)) != 0)
276 #define rb_ec_raised_clear(ec) ((ec)->raised_flag = 0)
277 int rb_ec_set_raised(rb_execution_context_t *ec);
278 int rb_ec_reset_raised(rb_execution_context_t *ec);
279 int rb_ec_stack_check(rb_execution_context_t *ec);
281 VALUE rb_f_eval(int argc, const VALUE *argv, VALUE self);
282 VALUE rb_make_exception(int argc, const VALUE *argv);
284 NORETURN(void rb_method_name_error(VALUE, VALUE));
286 void rb_fiber_start(rb_fiber_t*);
288 NORETURN(void rb_print_undef(VALUE, ID, rb_method_visibility_t));
289 NORETURN(void rb_print_undef_str(VALUE, VALUE));
290 NORETURN(void rb_print_inaccessible(VALUE, ID, rb_method_visibility_t));
291 NORETURN(void rb_vm_localjump_error(const char *,VALUE, int));
292 NORETURN(void rb_vm_jump_tag_but_local_jump(int));
294 VALUE rb_vm_make_jump_tag_but_local_jump(int state, VALUE val);
295 rb_cref_t *rb_vm_cref(void);
296 rb_cref_t *rb_vm_cref_replace_with_duplicated_cref(void);
297 VALUE rb_vm_call_cfunc(VALUE recv, VALUE (*func)(VALUE), VALUE arg, VALUE block_handler, VALUE filename);
298 void rb_vm_set_progname(VALUE filename);
299 VALUE rb_vm_cbase(void);
301 /* vm_backtrace.c */
302 VALUE rb_ec_backtrace_object(const rb_execution_context_t *ec);
303 VALUE rb_ec_backtrace_str_ary(const rb_execution_context_t *ec, long lev, long n);
304 VALUE rb_ec_backtrace_location_ary(const rb_execution_context_t *ec, long lev, long n, bool skip_internal);
306 #ifndef CharNext /* defined as CharNext[AW] on Windows. */
307 # ifdef HAVE_MBLEN
308 # define CharNext(p) rb_char_next(p)
309 static inline char *
310 rb_char_next(const char *p)
312 if (p) {
313 int len = mblen(p, RUBY_MBCHAR_MAXSIZE);
314 p += len > 0 ? len : 1;
316 return (char *)p;
318 # else
319 # define CharNext(p) ((p) + 1)
320 # endif
321 #endif
323 #if defined DOSISH || defined __CYGWIN__
324 static inline void
325 translit_char(char *p, int from, int to)
327 while (*p) {
328 if ((unsigned char)*p == from)
329 *p = to;
330 p = CharNext(p);
333 #endif
335 #endif /* RUBY_EVAL_INTERN_H */