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