Ruby: eval_intern.h Source File

Ruby 3.3.5p100 (2024-09-03 revision ef084cc8f4958c1b6e4ead99136631bef6d8ddba)

eval_intern.h
1#ifndef RUBY_EVAL_INTERN_H
2#define RUBY_EVAL_INTERN_H
3
4#include "ruby/ruby.h"
5#include "vm_core.h"
6
7static inline void
8vm_passed_block_handler_set(rb_execution_context_t *ec, VALUE block_handler)
9{
10 vm_block_handler_verify(block_handler);
11 ec->passed_block_handler = block_handler;
12}
13
14static inline void
15pass_passed_block_handler(rb_execution_context_t *ec)
16{
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);
20}
21
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())
24
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
34
35#include <stdio.h>
36#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
37# include "wasm/setjmp.h"
38#else
39# include <setjmp.h>
40#endif
41
42#ifdef __APPLE__
43# ifdef HAVE_CRT_EXTERNS_H
44# include <crt_externs.h>
45# else
46# include "missing/crt_externs.h"
47# endif
48#endif
49
50#ifndef HAVE_STRING_H
51char *strrchr(const char *, const char);
52#endif
53
54#ifdef HAVE_UNISTD_H
55#include <unistd.h>
56#endif
57
58#ifdef HAVE_NET_SOCKET_H
59#include <net/socket.h>
60#endif
61
62#define ruby_setjmp(env) RUBY_SETJMP(env)
63#define ruby_longjmp(env,val) RUBY_LONGJMP((env),(val))
64#ifdef __CYGWIN__
65# ifndef _setjmp
66int _setjmp(jmp_buf);
67# endif
68# ifndef _longjmp
69NORETURN(void _longjmp(jmp_buf, int));
70# endif
71#endif
72
73#include <sys/types.h>
74#include <signal.h>
75#include <errno.h>
76
77#ifdef HAVE_SYS_SELECT_H
78#include <sys/select.h>
79#endif
80
81/*
82 Solaris sys/select.h switches select to select_large_fdset to support larger
83 file descriptors if FD_SETSIZE is larger than 1024 on 32bit environment.
84 But Ruby doesn't change FD_SETSIZE because fd_set is allocated dynamically.
85 So following definition is required to use select_large_fdset.
86*/
87#ifdef HAVE_SELECT_LARGE_FDSET
88#define select(n, r, w, e, t) select_large_fdset((n), (r), (w), (e), (t))
89extern int select_large_fdset(int, fd_set *, fd_set *, fd_set *, struct timeval *);
90#endif
91
92#ifdef HAVE_SYS_PARAM_H
93#include <sys/param.h>
94#endif
95
96#include <sys/stat.h>
97
98
99#define SAVE_ROOT_JMPBUF(th, stmt) do \
100 if (true) { \
101 stmt; \
102 } \
103 else if (th) { /* suppress unused-variable warning */ \
104 } while (0)
105
106#define EC_PUSH_TAG(ec) do { \
107 rb_execution_context_t * const _ec = (ec); \
108 struct rb_vm_tag _tag; \
109 _tag.state = TAG_NONE; \
110 _tag.tag = Qundef; \
111 _tag.prev = _ec->tag; \
112 _tag.lock_rec = rb_ec_vm_lock_rec(_ec); \
113 rb_vm_tag_jmpbuf_init(&_tag.buf); \
114
115#define EC_POP_TAG() \
116 _ec->tag = _tag.prev; \
117 rb_vm_tag_jmpbuf_deinit(&_tag.buf); \
118} while (0)
119
120#define EC_TMPPOP_TAG() \
121 _ec->tag = _tag.prev
122
123#define EC_REPUSH_TAG() (void)(_ec->tag = &_tag)
124
125#if defined __GNUC__ && __GNUC__ == 4 && (__GNUC_MINOR__ >= 6 && __GNUC_MINOR__ <= 8) || defined __clang__
126/* This macro prevents GCC 4.6--4.8 from emitting maybe-uninitialized warnings.
127 * This macro also prevents Clang from dumping core in EC_EXEC_TAG().
128 * (I confirmed Clang 4.0.1 and 5.0.0.)
129 */
130# define VAR_FROM_MEMORY(var) __extension__(*(__typeof__(var) volatile *)&(var))
131# define VAR_INITIALIZED(var) ((var) = VAR_FROM_MEMORY(var))
132# define VAR_NOCLOBBERED(var) volatile var
133#else
134# define VAR_FROM_MEMORY(var) (var)
135# define VAR_INITIALIZED(var) ((void)&(var))
136# define VAR_NOCLOBBERED(var) var
137#endif
138
139static inline void
140rb_ec_vm_lock_rec_check(const rb_execution_context_t *ec, unsigned int recorded_lock_rec)
141{
142 unsigned int current_lock_rec = rb_ec_vm_lock_rec(ec);
143 if (current_lock_rec != recorded_lock_rec) {
144 rb_ec_vm_lock_rec_release(ec, recorded_lock_rec, current_lock_rec);
145 }
146}
147
148/* clear ec->tag->state, and return the value */
149static inline int
150rb_ec_tag_state(const rb_execution_context_t *ec)
151{
152 struct rb_vm_tag *tag = ec->tag;
153 enum ruby_tag_type state = tag->state;
154 tag->state = TAG_NONE;
155 rb_ec_vm_lock_rec_check(ec, tag->lock_rec);
156 RBIMPL_ASSUME(state != TAG_NONE);
157 return state;
158}
159
160NORETURN(static inline void rb_ec_tag_jump(const rb_execution_context_t *ec, enum ruby_tag_type st));
161static inline void
162rb_ec_tag_jump(const rb_execution_context_t *ec, enum ruby_tag_type st)
163{
164 RUBY_ASSERT(st != TAG_NONE);
165 ec->tag->state = st;
166 ruby_longjmp(RB_VM_TAG_JMPBUF_GET(ec->tag->buf), 1);
167}
168
169/*
170 setjmp() in assignment expression rhs is undefined behavior
171 [ISO/IEC 9899:1999] 7.13.1.1
172*/
173#define EC_EXEC_TAG() \
174 (UNLIKELY(ruby_setjmp(RB_VM_TAG_JMPBUF_GET(_tag.buf))) ? rb_ec_tag_state(VAR_FROM_MEMORY(_ec)) : (EC_REPUSH_TAG(), 0))
175
176#define EC_JUMP_TAG(ec, st) rb_ec_tag_jump(ec, st)
177
178#define INTERNAL_EXCEPTION_P(exc) FIXNUM_P(exc)
179
180/* CREF operators */
181
182#define CREF_FL_PUSHED_BY_EVAL IMEMO_FL_USER1
183#define CREF_FL_OMOD_SHARED IMEMO_FL_USER2
184#define CREF_FL_SINGLETON IMEMO_FL_USER3
185
186static inline int CREF_SINGLETON(const rb_cref_t *cref);
187
188static inline VALUE
189CREF_CLASS(const rb_cref_t *cref)
190{
191 if (CREF_SINGLETON(cref)) {
192 return CLASS_OF(cref->klass_or_self);
193 }
194 else {
195 return cref->klass_or_self;
196 }
197}
198
199static inline VALUE
200CREF_CLASS_FOR_DEFINITION(const rb_cref_t *cref)
201{
202 if (CREF_SINGLETON(cref)) {
203 return rb_singleton_class(cref->klass_or_self);
204 }
205 else {
206 return cref->klass_or_self;
207 }
208}
209
210static inline rb_cref_t *
211CREF_NEXT(const rb_cref_t *cref)
212{
213 return cref->next;
214}
215
216static inline const rb_scope_visibility_t *
217CREF_SCOPE_VISI(const rb_cref_t *cref)
218{
219 return &cref->scope_visi;
220}
221
222static inline VALUE
223CREF_REFINEMENTS(const rb_cref_t *cref)
224{
225 return cref->refinements;
226}
227
228static inline void
229CREF_REFINEMENTS_SET(rb_cref_t *cref, VALUE refs)
230{
231 RB_OBJ_WRITE(cref, &cref->refinements, refs);
232}
233
234static inline int
235CREF_PUSHED_BY_EVAL(const rb_cref_t *cref)
236{
237 return cref->flags & CREF_FL_PUSHED_BY_EVAL;
238}
239
240static inline void
241CREF_PUSHED_BY_EVAL_SET(rb_cref_t *cref)
242{
243 cref->flags |= CREF_FL_PUSHED_BY_EVAL;
244}
245
246static inline int
247CREF_SINGLETON(const rb_cref_t *cref)
248{
249 return cref->flags & CREF_FL_SINGLETON;
250}
251
252static inline void
253CREF_SINGLETON_SET(rb_cref_t *cref)
254{
255 cref->flags |= CREF_FL_SINGLETON;
256}
257
258static inline int
259CREF_OMOD_SHARED(const rb_cref_t *cref)
260{
261 return cref->flags & CREF_FL_OMOD_SHARED;
262}
263
264static inline void
265CREF_OMOD_SHARED_SET(rb_cref_t *cref)
266{
267 cref->flags |= CREF_FL_OMOD_SHARED;
268}
269
270static inline void
271CREF_OMOD_SHARED_UNSET(rb_cref_t *cref)
272{
273 cref->flags &= ~CREF_FL_OMOD_SHARED;
274}
275
276enum {
277 RAISED_EXCEPTION = 1,
278 RAISED_STACKOVERFLOW = 2,
279 RAISED_NOMEMORY = 4
280};
281#define rb_ec_raised_set(ec, f) ((ec)->raised_flag |= (f))
282#define rb_ec_raised_reset(ec, f) ((ec)->raised_flag &= ~(f))
283#define rb_ec_raised_p(ec, f) (((ec)->raised_flag & (f)) != 0)
284#define rb_ec_raised_clear(ec) ((ec)->raised_flag = 0)
285int rb_ec_set_raised(rb_execution_context_t *ec);
286int rb_ec_reset_raised(rb_execution_context_t *ec);
287int rb_ec_stack_check(rb_execution_context_t *ec);
288
289 VALUE rb_f_eval(int argc, const VALUE *argv, VALUE self);
290 VALUE rb_make_exception(int argc, const VALUE *argv);
291
292NORETURN(void rb_method_name_error(VALUE, VALUE));
293
294NORETURN(void rb_fiber_start(rb_fiber_t*));
295
296NORETURN(void rb_print_undef(VALUE, ID, rb_method_visibility_t));
297NORETURN(void rb_print_undef_str(VALUE, VALUE));
298NORETURN(void rb_print_inaccessible(VALUE, ID, rb_method_visibility_t));
299NORETURN(void rb_vm_localjump_error(const char *,VALUE, int));
300NORETURN(void rb_vm_jump_tag_but_local_jump(int));
301
302 VALUE rb_vm_make_jump_tag_but_local_jump(int state, VALUE val);
303 rb_cref_t *rb_vm_cref(void);
304 rb_cref_t *rb_vm_cref_replace_with_duplicated_cref(void);
305 VALUE rb_vm_call_cfunc(VALUE recv, VALUE (*func)(VALUE), VALUE arg, VALUE block_handler, VALUE filename);
306void rb_vm_set_progname(VALUE filename);
307 VALUE rb_vm_cbase(void);
308
309/* vm_backtrace.c */
310 VALUE rb_ec_backtrace_object(const rb_execution_context_t *ec);
311 VALUE rb_ec_backtrace_str_ary(const rb_execution_context_t *ec, long lev, long n);
312 VALUE rb_ec_backtrace_location_ary(const rb_execution_context_t *ec, long lev, long n, bool skip_internal);
313
314#ifndef CharNext /* defined as CharNext[AW] on Windows. */
315# ifdef HAVE_MBLEN
316# define CharNext(p) rb_char_next(p)
317static inline char *
318rb_char_next(const char *p)
319{
320 if (p) {
321 int len = mblen(p, RUBY_MBCHAR_MAXSIZE);
322 p += len > 0 ? len : 1;
323 }
324 return (char *)p;
325}
326# else
327# define CharNext(p) ((p) + 1)
328# endif
329#endif
330
331#if defined DOSISH || defined __CYGWIN__
332static inline void
333translit_char(char *p, int from, int to)
334{
335 while (*p) {
336 if ((unsigned char)*p == from)
337 *p = to;
338 p = CharNext(p);
339 }
340}
341#endif
342
343#endif /* RUBY_EVAL_INTERN_H */
RUBY_ASSERT
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:177
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2288
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:619
len
int len
Length of the buffer.
Definition io.h:8
RBIMPL_ASSUME
#define RBIMPL_ASSUME(_)
Wraps (or simulates) __builtin_unreachable.
Definition assume.h:76
ruby.h
rb_cref_struct
CREF (Class REFerence)
Definition method.h:44
rb_execution_context_struct
Definition vm_core.h:986
rb_fiber_struct
Definition cont.c:259
rb_scope_visi_struct
Definition method.h:38
rb_vm_tag
Definition vm_core.h:946
rb_wasm_jmp_buf
Definition setjmp.h:17
timeval
Definition missing.h:63
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40