Ruby 4.1.0dev (2026-10-14 revision b709bdb3e37451bb1103b50fb280968dbf038103)
vm_eval.c (b709bdb3e37451bb1103b50fb280968dbf038103)
1/**********************************************************************
2
3 vm_eval.c - Included into vm.c.
4
5 $Author$
6 created at: Sat May 24 16:02:32 JST 2008
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12**********************************************************************/
13
14#include "internal/thread.h"
15#include "prism_compile.h"
16
18 VALUE tbl;
19};
20
21static inline VALUE method_missing(rb_execution_context_t *ec, VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status, int kw_splat);
22static inline VALUE vm_yield_with_cref(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat, const rb_cref_t *cref, int is_lambda);
23static inline VALUE vm_yield(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat);
24static inline VALUE vm_yield_with_block(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE block_handler, int kw_splat);
25static inline VALUE vm_yield_force_blockarg(rb_execution_context_t *ec, VALUE args);
26VALUE vm_exec(rb_execution_context_t *ec);
27static void vm_set_eval_stack(rb_execution_context_t * th, const rb_iseq_t *iseq, const rb_cref_t *cref, const struct rb_block *base_block);
28static int vm_collect_local_variables_in_heap(const VALUE *dfp, const struct local_var_list *vars);
29
30static VALUE rb_eUncaughtThrow;
31static ID id_result, id_tag, id_value;
32#define id_mesg idMesg
33
34static VALUE send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope);
35static VALUE vm_call0_body(rb_execution_context_t* ec, struct rb_calling_info *calling, const VALUE *argv);
36
37static VALUE *
38vm_argv_ruby_array(VALUE *av, const VALUE *argv, int *flags, int *argc, int kw_splat)
39{
40 *flags |= VM_CALL_ARGS_SPLAT;
41 VALUE argv_ary = rb_ary_hidden_new(*argc);
42 rb_ary_cat(argv_ary, argv, *argc);
43 *argc = 2;
44 av[0] = argv_ary;
45 if (kw_splat) {
46 av[1] = rb_ary_pop(argv_ary);
47 }
48 else {
49 // Make sure flagged keyword hash passed as regular argument
50 // isn't treated as keywords
51 *flags |= VM_CALL_KW_SPLAT;
52 av[1] = rb_hash_new();
53 }
54 return av;
55}
56
57static inline VALUE vm_call0_cc(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, const struct rb_callcache *cc, int kw_splat);
58
60rb_vm_call0(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, const rb_callable_method_entry_t *cme, int kw_splat)
61{
62 const struct rb_callcache cc = VM_CC_ON_STACK(Qundef, vm_call_general, {{ 0 }}, cme);
63 return vm_call0_cc(ec, recv, id, argc, argv, &cc, kw_splat);
64}
65
67rb_vm_call_with_refinements(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, int kw_splat)
68{
70 rb_callable_method_entry_with_refinements(CLASS_OF(recv), id, NULL);
71 if (me) {
72 return rb_vm_call0(ec, recv, id, argc, argv, me, kw_splat);
73 }
74 else {
75 /* fallback to funcall (e.g. method_missing) */
76 return rb_funcallv(recv, id, argc, argv);
77 }
78}
79
80static inline VALUE
81vm_call0_cc(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, const struct rb_callcache *cc, int kw_splat)
82{
83 int flags = kw_splat ? VM_CALL_KW_SPLAT : 0;
84 VALUE *use_argv = (VALUE *)argv;
85 VALUE av[2];
86
87 if (UNLIKELY(vm_cc_cme(cc)->def->type == VM_METHOD_TYPE_ISEQ && argc > VM_ARGC_STACK_MAX)) {
88 use_argv = vm_argv_ruby_array(av, argv, &flags, &argc, kw_splat);
89 }
90
91 struct rb_calling_info calling = {
92 .cd = &(struct rb_call_data) {
93 .ci = &VM_CI_ON_STACK(id, flags, argc, NULL),
94 .cc = NULL,
95 },
96 .cc = cc,
97 .block_handler = vm_passed_block_handler(ec),
98 .recv = recv,
99 .argc = argc,
100 .kw_splat = kw_splat,
101 };
102
103 return vm_call0_body(ec, &calling, use_argv);
104}
105
106static VALUE
107vm_call0_cme(rb_execution_context_t *ec, struct rb_calling_info *calling, const VALUE *argv, const rb_callable_method_entry_t *cme)
108{
109 calling->cc = &VM_CC_ON_STACK(Qundef, vm_call_general, {{ 0 }}, cme);
110 return vm_call0_body(ec, calling, argv);
111}
112
113static VALUE
114vm_call0_super(rb_execution_context_t *ec, struct rb_calling_info *calling, const VALUE *argv, VALUE klass, enum method_missing_reason ex)
115{
116 ID mid = vm_ci_mid(calling->cd->ci);
117 klass = RCLASS_SUPER(klass);
118
119 if (klass) {
120 const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, mid);
121
122 if (cme) {
123 RUBY_VM_CHECK_INTS(ec);
124 return vm_call0_cme(ec, calling, argv, cme);
125 }
126 }
127
128 vm_passed_block_handler_set(ec, calling->block_handler);
129 return method_missing(ec, calling->recv, mid, calling->argc, argv, ex, calling->kw_splat);
130}
131
132static VALUE
133vm_call0_cfunc_with_frame(rb_execution_context_t* ec, struct rb_calling_info *calling, const VALUE *argv)
134{
135 const struct rb_callinfo *ci = calling->cd->ci;
136 VALUE val;
137 const rb_callable_method_entry_t *me = vm_cc_cme(calling->cc);
138 const rb_method_cfunc_t *cfunc = UNALIGNED_MEMBER_PTR(me->def, body.cfunc);
139 int len = cfunc->argc;
140 VALUE recv = calling->recv;
141 int argc = calling->argc;
142 ID mid = vm_ci_mid(ci);
143 VALUE block_handler = calling->block_handler;
144 int frame_flags = VM_FRAME_MAGIC_CFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL;
145
146 if (calling->kw_splat) {
147 if (argc > 0 && RB_TYPE_P(argv[argc-1], T_HASH) && RHASH_EMPTY_P(argv[argc-1])) {
148 argc--;
149 }
150 else {
151 frame_flags |= VM_FRAME_FLAG_CFRAME_KW;
152 }
153 }
154
155 RUBY_DTRACE_CMETHOD_ENTRY_HOOK(ec, me->owner, me->def->original_id);
156 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, recv, me->def->original_id, mid, me->owner, Qnil);
157 {
158 rb_control_frame_t *reg_cfp = ec->cfp;
159
160 vm_push_frame(ec, 0, frame_flags, recv,
161 block_handler, (VALUE)me,
162 0, reg_cfp->sp, 0, 0);
163
164 if (len >= 0) rb_check_arity(argc, len, len);
165
166 val = (*cfunc->invoker)(recv, argc, argv, cfunc->func);
167
168 CHECK_CFP_CONSISTENCY("vm_call0_cfunc_with_frame");
169 rb_vm_pop_frame(ec);
170 }
171 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, recv, me->def->original_id, mid, me->owner, val);
172 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
173
174 return val;
175}
176
177static VALUE
178vm_call0_cfunc(rb_execution_context_t *ec, struct rb_calling_info *calling, const VALUE *argv)
179{
180 return vm_call0_cfunc_with_frame(ec, calling, argv);
181}
182
183static void
184vm_call_check_arity(struct rb_calling_info *calling, int argc, const VALUE *argv)
185{
186 if (calling->kw_splat &&
187 calling->argc > 0 &&
188 RB_TYPE_P(argv[calling->argc-1], T_HASH) &&
189 RHASH_EMPTY_P(argv[calling->argc-1])) {
190 calling->argc--;
191 }
192
193 rb_check_arity(calling->argc, argc, argc);
194}
195
196/* `ci' should point temporal value (on stack value) */
197static VALUE
198vm_call0_body(rb_execution_context_t *ec, struct rb_calling_info *calling, const VALUE *argv)
199{
200 const struct rb_callinfo *ci = calling->cd->ci;
201 const struct rb_callcache *cc = calling->cc;
202 VALUE ret;
203
204 retry:
205
206 switch (vm_cc_cme(cc)->def->type) {
207 case VM_METHOD_TYPE_ISEQ:
208 {
209 rb_control_frame_t *reg_cfp = ec->cfp;
210 int i;
211
212 CHECK_VM_STACK_OVERFLOW(reg_cfp, calling->argc + 1);
213 vm_check_canary(ec, reg_cfp->sp);
214
215 *reg_cfp->sp++ = calling->recv;
216 for (i = 0; i < calling->argc; i++) {
217 *reg_cfp->sp++ = argv[i];
218 }
219
220 if (ISEQ_BODY(def_iseq_ptr(vm_cc_cme(cc)->def))->param.flags.forwardable) {
221 vm_call_iseq_fwd_setup(ec, reg_cfp, calling);
222 }
223 else {
224 vm_call_iseq_setup(ec, reg_cfp, calling);
225 }
226 VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);
227 return vm_exec(ec); // CHECK_INTS in this function
228 }
229 case VM_METHOD_TYPE_NOTIMPLEMENTED:
230 case VM_METHOD_TYPE_CFUNC:
231 ret = vm_call0_cfunc(ec, calling, argv);
232 goto success;
233 case VM_METHOD_TYPE_ATTRSET:
234 vm_call_check_arity(calling, 1, argv);
235 VM_CALL_METHOD_ATTR(ret,
236 rb_ivar_set(calling->recv, vm_cc_cme(cc)->def->body.attr.id, argv[0]),
237 (void)0);
238 goto success;
239 case VM_METHOD_TYPE_IVAR:
240 vm_call_check_arity(calling, 0, argv);
241 VM_CALL_METHOD_ATTR(ret,
242 rb_attr_get(calling->recv, vm_cc_cme(cc)->def->body.attr.id),
243 (void)0);
244 goto success;
245 case VM_METHOD_TYPE_BMETHOD:
246 ret = vm_call_bmethod_body(ec, calling, argv);
247 goto success;
248 case VM_METHOD_TYPE_ZSUPER:
249 {
250 VALUE klass = RCLASS_ORIGIN(vm_cc_cme(cc)->defined_class);
251 return vm_call0_super(ec, calling, argv, klass, MISSING_SUPER);
252 }
253 case VM_METHOD_TYPE_REFINED:
254 {
255 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
256
257 if (cme->def->body.refined.orig_me) {
258 const rb_callable_method_entry_t *orig_cme = refined_method_callable_without_refinement(cme);
259 return vm_call0_cme(ec, calling, argv, orig_cme);
260 }
261
262 VALUE klass = cme->defined_class;
263 return vm_call0_super(ec, calling, argv, klass, 0);
264 }
265 case VM_METHOD_TYPE_ALIAS:
266 {
267 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
268 const rb_callable_method_entry_t *orig_cme = aliased_callable_method_entry(cme);
269
270 if (cme == orig_cme) rb_bug("same!!");
271
272 if (vm_cc_markable(cc)) {
273 return vm_call0_cme(ec, calling, argv, orig_cme);
274 }
275 else {
276 *((const rb_callable_method_entry_t **)&cc->cme_) = orig_cme;
277 goto retry;
278 }
279 }
280 case VM_METHOD_TYPE_MISSING:
281 {
282 vm_passed_block_handler_set(ec, calling->block_handler);
283 return method_missing(ec, calling->recv, vm_ci_mid(ci), calling->argc,
284 argv, MISSING_NOENTRY, calling->kw_splat);
285 }
286 case VM_METHOD_TYPE_OPTIMIZED:
287 switch (vm_cc_cme(cc)->def->body.optimized.type) {
288 case OPTIMIZED_METHOD_TYPE_SEND:
289 ret = send_internal(calling->argc, argv, calling->recv, calling->kw_splat ? CALL_FCALL_KW : CALL_FCALL);
290 goto success;
291 case OPTIMIZED_METHOD_TYPE_CALL:
292 {
293 rb_proc_t *proc;
294 GetProcPtr(calling->recv, proc);
295 ret = rb_vm_invoke_proc(ec, proc, calling->argc, argv, calling->kw_splat, calling->block_handler,
296 rb_proc_refinements_cref_for_call(calling->recv));
297 goto success;
298 }
299 case OPTIMIZED_METHOD_TYPE_STRUCT_AREF:
300 vm_call_check_arity(calling, 0, argv);
301 VM_CALL_METHOD_ATTR(ret,
302 vm_call_opt_struct_aref0(ec, calling),
303 (void)0);
304 goto success;
305 case OPTIMIZED_METHOD_TYPE_STRUCT_ASET:
306 vm_call_check_arity(calling, 1, argv);
307 VM_CALL_METHOD_ATTR(ret,
308 vm_call_opt_struct_aset0(ec, calling, argv[0]),
309 (void)0);
310 goto success;
311 default:
312 rb_bug("vm_call0: unsupported optimized method type (%d)", vm_cc_cme(cc)->def->body.optimized.type);
313 }
314 break;
315 case VM_METHOD_TYPE_UNDEF:
316 break;
317 }
318 rb_bug("vm_call0: unsupported method type (%d)", vm_cc_cme(cc)->def->type);
319 return Qundef;
320
321 success:
322 RUBY_VM_CHECK_INTS(ec);
323 return ret;
324}
325
326VALUE
327rb_vm_call_kw(rb_execution_context_t *ec, VALUE recv, VALUE id, int argc, const VALUE *argv, const rb_callable_method_entry_t *me, int kw_splat)
328{
329 return rb_vm_call0(ec, recv, id, argc, argv, me, kw_splat);
330}
331
332static inline VALUE
333vm_call_super(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat)
334{
335 VALUE recv = ec->cfp->self;
336 VALUE klass;
337 ID id;
338 rb_control_frame_t *cfp = ec->cfp;
339 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
340
341 if (VM_FRAME_RUBYFRAME_P(cfp)) {
342 rb_bug("vm_call_super: should not be reached");
343 }
344
345 klass = RCLASS_ORIGIN(me->defined_class);
346 klass = RCLASS_SUPER(klass);
347 id = me->def->original_id;
348 me = rb_callable_method_entry(klass, id);
349
350 if (!me) {
351 return method_missing(ec, recv, id, argc, argv, MISSING_SUPER, kw_splat);
352 }
353 return rb_vm_call_kw(ec, recv, id, argc, argv, me, kw_splat);
354}
355
356VALUE
357rb_call_super_kw(int argc, const VALUE *argv, int kw_splat)
358{
359 rb_execution_context_t *ec = GET_EC();
360 PASS_PASSED_BLOCK_HANDLER_EC(ec);
361 return vm_call_super(ec, argc, argv, kw_splat);
362}
363
364VALUE
365rb_call_super(int argc, const VALUE *argv)
366{
367 return rb_call_super_kw(argc, argv, RB_NO_KEYWORDS);
368}
369
370VALUE
372{
373 const rb_execution_context_t *ec = GET_EC();
375 if (!ec || !(cfp = ec->cfp)) {
376 rb_raise(rb_eRuntimeError, "no self, no life");
377 }
378 return cfp->self;
379}
380
381static inline void
382stack_check(rb_execution_context_t *ec)
383{
384 if (!rb_ec_raised_p(ec, RAISED_STACKOVERFLOW) &&
385 rb_ec_stack_check(ec)) {
386 rb_ec_raised_set(ec, RAISED_STACKOVERFLOW);
387 rb_ec_stack_overflow(ec, 0);
388 }
389}
390
391void
392rb_check_stack_overflow(void)
393{
394#ifndef RB_THREAD_LOCAL_SPECIFIER
395 if (!ruby_current_ec_key) return;
396#endif
397 rb_execution_context_t *ec = GET_EC();
398 if (ec) stack_check(ec);
399}
400
401NORETURN(static void uncallable_object(VALUE recv, ID mid));
402static inline const rb_callable_method_entry_t *rb_search_method_entry(VALUE recv, ID mid);
403static inline enum method_missing_reason rb_method_call_status(rb_execution_context_t *ec, const rb_callable_method_entry_t *me, call_type scope, VALUE self);
404
405static VALUE
406gccct_hash(VALUE klass, VALUE box_value, ID mid)
407{
408 return ((klass ^ box_value) >> 3) ^ (VALUE)mid;
409}
410
411NOINLINE(static const struct rb_callcache *gccct_method_search_slowpath(rb_vm_t *vm, VALUE klass, unsigned int index, const struct rb_callinfo * ci));
412
413static const struct rb_callcache *
414gccct_method_search_slowpath(rb_vm_t *vm, VALUE klass, unsigned int index, const struct rb_callinfo *ci)
415{
416 struct rb_call_data cd = {
417 .ci = ci,
418 .cc = NULL
419 };
420
421 vm_search_method_slowpath0(vm->self, &cd, klass);
422
423 if (UNLIKELY(!vm->global_cc_cache_table_used)) {
424 vm->global_cc_cache_table_used = true;
425 }
426 return vm->global_cc_cache_table[index] = cd.cc;
427}
428
429static void
430scope_to_ci(call_type scope, ID mid, int argc, struct rb_callinfo *ci)
431{
432 int flags = 0;
433
434 switch(scope) {
435 case CALL_PUBLIC:
436 break;
437 case CALL_FCALL:
438 flags |= VM_CALL_FCALL;
439 break;
440 case CALL_VCALL:
441 flags |= VM_CALL_VCALL;
442 break;
443 case CALL_PUBLIC_KW:
444 flags |= VM_CALL_KWARG;
445 break;
446 case CALL_FCALL_KW:
447 flags |= (VM_CALL_KWARG | VM_CALL_FCALL);
448 break;
449 }
450 *ci = VM_CI_ON_STACK(mid, flags, argc, NULL);
451}
452
453static inline const struct rb_callcache *
454gccct_method_search(rb_execution_context_t *ec, VALUE recv, ID mid, const struct rb_callinfo *ci)
455{
456 VALUE klass, box_value;
457 const rb_box_t *box = rb_current_box();
458
459 if (!SPECIAL_CONST_P(recv)) {
460 klass = RBASIC_CLASS(recv);
461 if (UNLIKELY(!klass)) uncallable_object(recv, mid);
462 }
463 else {
464 klass = CLASS_OF(recv);
465 }
466
467 if (BOX_USER_P(box)) {
468 box_value = box->box_object;
469 }
470 else {
471 box_value = 0;
472 }
473 // search global method cache
474 unsigned int index = (unsigned int)(gccct_hash(klass, box_value, mid) % VM_GLOBAL_CC_CACHE_TABLE_SIZE);
475 rb_vm_t *vm = rb_ec_vm_ptr(ec);
476 const struct rb_callcache *cc = vm->global_cc_cache_table[index];
477
478 if (LIKELY(cc)) {
479 if (LIKELY(vm_cc_class_check(cc, klass))) {
480 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
481 if (LIKELY(!METHOD_ENTRY_INVALIDATED(cme) &&
482 cme->called_id == mid)) {
483
484 VM_ASSERT(vm_cc_check_cme(cc, rb_callable_method_entry(klass, mid)));
485 RB_DEBUG_COUNTER_INC(gccct_hit);
486
487 return cc;
488 }
489 }
490 }
491 else {
492 RB_DEBUG_COUNTER_INC(gccct_null);
493 }
494
495 RB_DEBUG_COUNTER_INC(gccct_miss);
496 return gccct_method_search_slowpath(vm, klass, index, ci);
497}
498
499VALUE
500rb_gccct_clear_table(void)
501{
502 rb_vm_t *vm = GET_VM();
503 if (vm->global_cc_cache_table_used) {
504 const struct rb_callcache **const table = vm->global_cc_cache_table;
505 MEMZERO(table, struct rb_callcache *, VM_GLOBAL_CC_CACHE_TABLE_SIZE);
506 vm->global_cc_cache_table_used = false;
507 }
508 return Qnil;
509}
510
527static inline VALUE
528rb_call0(rb_execution_context_t *ec,
529 VALUE recv, ID mid, int argc, const VALUE *argv,
530 call_type call_scope, VALUE self)
531{
532 enum method_missing_reason call_status;
533 call_type scope = call_scope;
534 int kw_splat = RB_NO_KEYWORDS;
535
536 switch (scope) {
537 case CALL_PUBLIC_KW:
538 scope = CALL_PUBLIC;
539 kw_splat = 1;
540 break;
541 case CALL_FCALL_KW:
542 scope = CALL_FCALL;
543 kw_splat = 1;
544 break;
545 default:
546 break;
547 }
548
549 struct rb_callinfo ci;
550 scope_to_ci(scope, mid, argc, &ci);
551
552 const struct rb_callcache *cc = gccct_method_search(ec, recv, mid, &ci);
553
554 if (scope == CALL_PUBLIC) {
555 RB_DEBUG_COUNTER_INC(call0_public);
556
557 const rb_callable_method_entry_t *cc_cme = cc ? vm_cc_cme(cc) : NULL;
558 const rb_callable_method_entry_t *cme = callable_method_entry_refinements0(CLASS_OF(recv), mid, NULL, true, cc_cme);
559 call_status = rb_method_call_status(ec, cme, scope, self);
560
561 if (UNLIKELY(call_status != MISSING_NONE)) {
562 return method_missing(ec, recv, mid, argc, argv, call_status, kw_splat);
563 }
564 else if (UNLIKELY(cc_cme != cme)) { // refinement is solved
565 stack_check(ec);
566 return rb_vm_call_kw(ec, recv, mid, argc, argv, cme, kw_splat);
567 }
568 }
569 else {
570 RB_DEBUG_COUNTER_INC(call0_other);
571 call_status = rb_method_call_status(ec, cc ? vm_cc_cme(cc) : NULL, scope, self);
572
573 if (UNLIKELY(call_status != MISSING_NONE)) {
574 return method_missing(ec, recv, mid, argc, argv, call_status, kw_splat);
575 }
576 }
577
578 stack_check(ec);
579 return vm_call0_cc(ec, recv, mid, argc, argv, cc, kw_splat);
580}
581
583 VALUE defined_class;
584 VALUE recv;
585 ID mid;
588 unsigned int respond: 1;
589 unsigned int respond_to_missing: 1;
590 int argc;
591 const VALUE *argv;
592 int kw_splat;
593};
594
595static VALUE
596check_funcall_exec(VALUE v)
597{
598 struct rescue_funcall_args *args = (void *)v;
599 return call_method_entry(args->ec, args->defined_class,
600 args->recv, idMethodMissing,
601 args->cme, args->argc, args->argv, args->kw_splat);
602}
603
604static VALUE
605check_funcall_failed(VALUE v, VALUE e)
606{
607 struct rescue_funcall_args *args = (void *)v;
608 int ret = args->respond;
609 if (!ret) {
610 switch (method_boundp(args->defined_class, args->mid,
611 BOUND_PRIVATE|BOUND_RESPONDS)) {
612 case 2:
613 ret = TRUE;
614 break;
615 case 0:
616 ret = args->respond_to_missing;
617 break;
618 default:
619 ret = FALSE;
620 break;
621 }
622 }
623 if (ret) {
624 rb_exc_raise(e);
625 }
626 return Qundef;
627}
628
629static int
630check_funcall_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE recv, ID mid)
631{
632 return vm_respond_to(ec, klass, recv, mid, TRUE);
633}
634
635static int
636check_funcall_callable(rb_execution_context_t *ec, const rb_callable_method_entry_t *me)
637{
638 return rb_method_call_status(ec, me, CALL_FCALL, ec->cfp->self) == MISSING_NONE;
639}
640
641static VALUE
642check_funcall_missing(rb_execution_context_t *ec, VALUE klass, VALUE recv, ID mid, int argc, const VALUE *argv, int respond, VALUE def, int kw_splat)
643{
644 struct rescue_funcall_args args;
646 VALUE ret = Qundef;
647
648 ret = basic_obj_respond_to_missing(ec, klass, recv,
649 ID2SYM(mid), Qtrue);
650 if (!RTEST(ret)) return def;
651 args.respond = respond > 0;
652 args.respond_to_missing = !UNDEF_P(ret);
653 ret = def;
654 cme = callable_method_entry(klass, idMethodMissing, &args.defined_class);
655
656 if (cme && !METHOD_ENTRY_BASIC(cme)) {
657 VALUE argbuf, *new_args = ALLOCV_N(VALUE, argbuf, argc+1);
658
659 new_args[0] = ID2SYM(mid);
660 #ifdef __GLIBC__
661 if (!argv) {
662 static const VALUE buf = Qfalse;
663 VM_ASSERT(argc == 0);
664 argv = &buf;
665 }
666 #endif
667 MEMCPY(new_args+1, argv, VALUE, argc);
668 ec->method_missing_reason = MISSING_NOENTRY;
669 args.ec = ec;
670 args.recv = recv;
671 args.cme = cme;
672 args.mid = mid;
673 args.argc = argc + 1;
674 args.argv = new_args;
675 args.kw_splat = kw_splat;
676 ret = rb_rescue2(check_funcall_exec, (VALUE)&args,
677 check_funcall_failed, (VALUE)&args,
679 ALLOCV_END(argbuf);
680 }
681 return ret;
682}
683
684static VALUE rb_check_funcall_default_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def, int kw_splat);
685
686VALUE
687rb_check_funcall_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
688{
689 return rb_check_funcall_default_kw(recv, mid, argc, argv, Qundef, kw_splat);
690}
691
692VALUE
693rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
694{
695 return rb_check_funcall_default_kw(recv, mid, argc, argv, Qundef, RB_NO_KEYWORDS);
696}
697
698static VALUE
699rb_check_funcall_default_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def, int kw_splat)
700{
701 VM_ASSERT(ruby_thread_has_gvl_p());
702
703 VALUE klass = CLASS_OF(recv);
705 rb_execution_context_t *ec = GET_EC();
706 int respond = check_funcall_respond_to(ec, klass, recv, mid);
707
708 if (!respond)
709 return def;
710
711 me = rb_search_method_entry(recv, mid);
712 if (!check_funcall_callable(ec, me)) {
713 VALUE ret = check_funcall_missing(ec, klass, recv, mid, argc, argv,
714 respond, def, kw_splat);
715 if (UNDEF_P(ret)) ret = def;
716 return ret;
717 }
718 stack_check(ec);
719 return rb_vm_call_kw(ec, recv, mid, argc, argv, me, kw_splat);
720}
721
722VALUE
723rb_check_funcall_default(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def)
724{
725 return rb_check_funcall_default_kw(recv, mid, argc, argv, def, RB_NO_KEYWORDS);
726}
727
728VALUE
729rb_check_funcall_with_hook_kw(VALUE recv, ID mid, int argc, const VALUE *argv,
730 rb_check_funcall_hook *hook, VALUE arg, int kw_splat)
731{
732 VALUE klass = CLASS_OF(recv);
734 rb_execution_context_t *ec = GET_EC();
735 int respond = check_funcall_respond_to(ec, klass, recv, mid);
736
737 if (!respond) {
738 (*hook)(FALSE, recv, mid, argc, argv, arg);
739 return Qundef;
740 }
741
742 me = rb_search_method_entry(recv, mid);
743 if (!check_funcall_callable(ec, me)) {
744 VALUE ret = check_funcall_missing(ec, klass, recv, mid, argc, argv,
745 respond, Qundef, kw_splat);
746 (*hook)(!UNDEF_P(ret), recv, mid, argc, argv, arg);
747 return ret;
748 }
749 stack_check(ec);
750 (*hook)(TRUE, recv, mid, argc, argv, arg);
751 return rb_vm_call_kw(ec, recv, mid, argc, argv, me, kw_splat);
752}
753
754const char *
755rb_type_str(enum ruby_value_type type)
756{
757#define type_case(t) t: return #t
758 switch (type) {
759 case type_case(T_NONE);
760 case type_case(T_OBJECT);
761 case type_case(T_CLASS);
762 case type_case(T_MODULE);
763 case type_case(T_FLOAT);
764 case type_case(T_STRING);
765 case type_case(T_REGEXP);
766 case type_case(T_ARRAY);
767 case type_case(T_HASH);
768 case type_case(T_STRUCT);
769 case type_case(T_BIGNUM);
770 case type_case(T_FILE);
771 case type_case(T_DATA);
772 case type_case(T_MATCH);
773 case type_case(T_COMPLEX);
774 case type_case(T_RATIONAL);
775 case type_case(T_NIL);
776 case type_case(T_TRUE);
777 case type_case(T_FALSE);
778 case type_case(T_SYMBOL);
779 case type_case(T_FIXNUM);
780 case type_case(T_IMEMO);
781 case type_case(T_UNDEF);
782 case type_case(T_NODE);
783 case type_case(T_ICLASS);
784 case type_case(T_ZOMBIE);
785 case type_case(T_MOVED);
786 case T_MASK: break;
787 }
788#undef type_case
789 return NULL;
790}
791
792static void
793uncallable_object(VALUE recv, ID mid)
794{
795 VALUE flags;
796 int type;
797 const char *typestr;
798 VALUE mname = rb_id2str(mid);
799
800 if (SPECIAL_CONST_P(recv)) {
801 rb_raise(rb_eNotImpError,
802 "method '%"PRIsVALUE"' called on unexpected immediate object (%p)",
803 mname, (void *)recv);
804 }
805 else if ((flags = RBASIC(recv)->flags) == 0) {
806 rb_raise(rb_eNotImpError,
807 "method '%"PRIsVALUE"' called on terminated object (%p)",
808 mname, (void *)recv);
809 }
810 else if (!(typestr = rb_type_str(type = BUILTIN_TYPE(recv)))) {
811 rb_raise(rb_eNotImpError,
812 "method '%"PRIsVALUE"' called on broken T_?""?""?(0x%02x) object"
813 " (%p flags=0x%"PRIxVALUE")",
814 mname, type, (void *)recv, flags);
815 }
816 else if (T_OBJECT <= type && type < T_NIL) {
817 rb_raise(rb_eNotImpError,
818 "method '%"PRIsVALUE"' called on hidden %s object"
819 " (%p flags=0x%"PRIxVALUE")",
820 mname, typestr, (void *)recv, flags);
821 }
822 else {
823 rb_raise(rb_eNotImpError,
824 "method '%"PRIsVALUE"' called on unexpected %s object"
825 " (%p flags=0x%"PRIxVALUE")",
826 mname, typestr, (void *)recv, flags);
827 }
828}
829
830static inline const rb_callable_method_entry_t *
831rb_search_method_entry(VALUE recv, ID mid)
832{
833 VALUE klass = CLASS_OF(recv);
834
835 if (!klass) uncallable_object(recv, mid);
836 return rb_callable_method_entry(klass, mid);
837}
838
839static inline enum method_missing_reason
840rb_method_call_status(rb_execution_context_t *ec, const rb_callable_method_entry_t *me, call_type scope, VALUE self)
841{
842 if (UNLIKELY(UNDEFINED_METHOD_ENTRY_P(me))) {
843 goto undefined;
844 }
845 else if (UNLIKELY(me->def->type == VM_METHOD_TYPE_REFINED)) {
846 me = rb_resolve_refined_method_callable(Qnil, me);
847 if (UNDEFINED_METHOD_ENTRY_P(me)) goto undefined;
848 }
849
850 rb_method_visibility_t visi = METHOD_ENTRY_VISI(me);
851
852 /* receiver specified form for private method */
853 if (UNLIKELY(visi != METHOD_VISI_PUBLIC)) {
854 if (me->def->original_id == idMethodMissing) {
855 return MISSING_NONE;
856 }
857 else if (visi == METHOD_VISI_PRIVATE &&
858 scope == CALL_PUBLIC) {
859 return MISSING_PRIVATE;
860 }
861 /* self must be kind of a specified form for protected method */
862 else if (visi == METHOD_VISI_PROTECTED &&
863 scope == CALL_PUBLIC) {
864
865 VALUE defined_class = me->owner;
866 if (RB_TYPE_P(defined_class, T_ICLASS)) {
867 defined_class = RBASIC(defined_class)->klass;
868 }
869
870 if (UNDEF_P(self) || !rb_obj_is_kind_of(self, defined_class)) {
871 return MISSING_PROTECTED;
872 }
873 }
874 }
875
876 return MISSING_NONE;
877
878 undefined:
879 return scope == CALL_VCALL ? MISSING_VCALL : MISSING_NOENTRY;
880}
881
882
894static inline VALUE
895rb_call(VALUE recv, ID mid, int argc, const VALUE *argv, call_type scope)
896{
897 rb_execution_context_t *ec = GET_EC();
898 return rb_call0(ec, recv, mid, argc, argv, scope, ec->cfp->self);
899}
900
901NORETURN(static void raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv,
902 VALUE obj, enum method_missing_reason call_status));
903
904/*
905 * call-seq:
906 * obj.method_missing(symbol [, *args] ) -> result
907 *
908 * Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
909 * <i>symbol</i> is the symbol for the method called, and <i>args</i>
910 * are any arguments that were passed to it. By default, the interpreter
911 * raises an error when this method is called. However, it is possible
912 * to override the method to provide more dynamic behavior.
913 * If it is decided that a particular method should not be handled, then
914 * <i>super</i> should be called, so that ancestors can pick up the
915 * missing method.
916 * The example below creates
917 * a class <code>Roman</code>, which responds to methods with names
918 * consisting of roman numerals, returning the corresponding integer
919 * values.
920 *
921 * class Roman
922 * def roman_to_int(str)
923 * # ...
924 * end
925 *
926 * def method_missing(symbol, *args)
927 * str = symbol.id2name
928 * begin
929 * roman_to_int(str)
930 * rescue
931 * super(symbol, *args)
932 * end
933 * end
934 * end
935 *
936 * r = Roman.new
937 * r.iv #=> 4
938 * r.xxiii #=> 23
939 * r.mm #=> 2000
940 * r.foo #=> NoMethodError
941 */
942
943static VALUE
944rb_method_missing(int argc, const VALUE *argv, VALUE obj)
945{
946 rb_execution_context_t *ec = GET_EC();
947 raise_method_missing(ec, argc, argv, obj, ec->method_missing_reason);
949}
950
951VALUE
952rb_make_no_method_exception(VALUE exc, VALUE format, VALUE obj,
953 int argc, const VALUE *argv, int priv)
954{
955 VALUE name = argv[0];
956
957 if (!format) {
958 format = rb_fstring_lit("undefined method '%1$s' for %3$s%4$s");
959 }
960 if (exc == rb_eNoMethodError) {
961 VALUE args = rb_ary_new4(argc - 1, argv + 1);
962 return rb_nomethod_err_new(format, obj, name, args, priv);
963 }
964 else {
965 return rb_name_err_new(format, obj, name);
966 }
967}
968
969static void
970raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE obj,
971 enum method_missing_reason last_call_status)
972{
974 VALUE format = 0;
975
976 if (UNLIKELY(argc == 0)) {
977 rb_raise(rb_eArgError, "no method name given");
978 }
979 else if (UNLIKELY(!SYMBOL_P(argv[0]))) {
980 const VALUE e = rb_eArgError; /* TODO: TypeError? */
981 rb_raise(e, "method name must be a Symbol but %"PRIsVALUE" is given",
982 rb_obj_class(argv[0]));
983 }
984
985 stack_check(ec);
986
987 if (last_call_status & MISSING_PRIVATE) {
988 format = rb_fstring_lit("private method '%1$s' called for %3$s%4$s");
989 }
990 else if (last_call_status & MISSING_PROTECTED) {
991 format = rb_fstring_lit("protected method '%1$s' called for %3$s%4$s");
992 }
993 else if (last_call_status & MISSING_VCALL) {
994 format = rb_fstring_lit("undefined local variable or method '%1$s' for %3$s%4$s");
995 exc = rb_eNameError;
996 }
997 else if (last_call_status & MISSING_SUPER) {
998 format = rb_fstring_lit("super: no superclass method '%1$s' for %3$s%4$s");
999 }
1000
1001 {
1002 exc = rb_make_no_method_exception(exc, format, obj, argc, argv,
1003 last_call_status & (MISSING_FCALL|MISSING_VCALL));
1004 if (!(last_call_status & MISSING_MISSING)) {
1005 rb_vm_pop_cfunc_frame();
1006 }
1007 rb_exc_raise(exc);
1008 }
1009}
1010
1011static void
1012vm_raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv,
1013 VALUE obj, int call_status)
1014{
1015 vm_passed_block_handler_set(ec, VM_BLOCK_HANDLER_NONE);
1016 raise_method_missing(ec, argc, argv, obj, call_status | MISSING_MISSING);
1017}
1018
1019static inline VALUE
1020method_missing(rb_execution_context_t *ec, VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status, int kw_splat)
1021{
1022 VALUE *nargv, result, work, klass;
1023 VALUE block_handler = vm_passed_block_handler(ec);
1025
1026 ec->method_missing_reason = call_status;
1027
1028 if (id == idMethodMissing) {
1029 goto missing;
1030 }
1031
1032 nargv = ALLOCV_N(VALUE, work, argc + 1);
1033 nargv[0] = ID2SYM(id);
1034 #ifdef __GLIBC__
1035 if (!argv) {
1036 static const VALUE buf = Qfalse;
1037 VM_ASSERT(argc == 0);
1038 argv = &buf;
1039 }
1040 #endif
1041 MEMCPY(nargv + 1, argv, VALUE, argc);
1042 ++argc;
1043 argv = nargv;
1044
1045 klass = CLASS_OF(obj);
1046 if (!klass) goto missing;
1047 me = rb_callable_method_entry(klass, idMethodMissing);
1048 if (!me || METHOD_ENTRY_BASIC(me)) goto missing;
1049 vm_passed_block_handler_set(ec, block_handler);
1050 result = rb_vm_call_kw(ec, obj, idMethodMissing, argc, argv, me, kw_splat);
1051 if (work) ALLOCV_END(work);
1052 return result;
1053 missing:
1054 raise_method_missing(ec, argc, argv, obj, call_status | MISSING_MISSING);
1056}
1057
1058static inline VALUE
1059rb_funcallv_scope(VALUE recv, ID mid, int argc, const VALUE *argv, call_type scope)
1060{
1061 rb_execution_context_t *ec = GET_EC();
1062
1063 struct rb_callinfo ci;
1064 scope_to_ci(scope, mid, argc, &ci);
1065
1066 const struct rb_callcache *cc = gccct_method_search(ec, recv, mid, &ci);
1067 VALUE self = ec->cfp->self;
1068
1069 if (LIKELY(cc) &&
1070 LIKELY(rb_method_call_status(ec, vm_cc_cme(cc), scope, self) == MISSING_NONE)) {
1071 // fastpath
1072 return vm_call0_cc(ec, recv, mid, argc, argv, cc, false);
1073 }
1074 else {
1075 return rb_call0(ec, recv, mid, argc, argv, scope, self);
1076 }
1077}
1078
1079#ifdef rb_funcallv
1080#undef rb_funcallv
1081#endif
1082VALUE
1083rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
1084{
1085 VM_ASSERT(ruby_thread_has_gvl_p());
1086 VM_ASSERT(!RB_TYPE_P(recv, T_IMEMO));
1087
1088 return rb_funcallv_scope(recv, mid, argc, argv, CALL_FCALL);
1089}
1090
1091VALUE
1092rb_funcallv_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
1093{
1094 VM_ASSERT(ruby_thread_has_gvl_p());
1095
1096 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_FCALL_KW : CALL_FCALL);
1097}
1098
1099VALUE
1100rb_apply(VALUE recv, ID mid, VALUE args)
1101{
1102 int argc;
1103 VALUE *argv, ret;
1104
1105 argc = RARRAY_LENINT(args);
1106 if (argc >= 0x100) {
1107 args = rb_ary_subseq(args, 0, argc);
1108 RBASIC_CLEAR_CLASS(args);
1109 OBJ_FREEZE(args);
1110 ret = rb_call(recv, mid, argc, RARRAY_CONST_PTR(args), CALL_FCALL);
1111 RB_GC_GUARD(args);
1112 return ret;
1113 }
1114 argv = ALLOCA_N(VALUE, argc);
1115 MEMCPY(argv, RARRAY_CONST_PTR(args), VALUE, argc);
1116
1117 return rb_funcallv(recv, mid, argc, argv);
1118}
1119
1120#ifdef rb_funcall
1121#undef rb_funcall
1122#endif
1123
1124VALUE
1125rb_funcall(VALUE recv, ID mid, int n, ...)
1126{
1127 VALUE *argv;
1128 va_list ar;
1129
1130 if (n > 0) {
1131 long i;
1132
1133 va_start(ar, n);
1134
1135 argv = ALLOCA_N(VALUE, n);
1136
1137 for (i = 0; i < n; i++) {
1138 argv[i] = va_arg(ar, VALUE);
1139 }
1140 va_end(ar);
1141 }
1142 else {
1143 argv = 0;
1144 }
1145 return rb_funcallv(recv, mid, n, argv);
1146}
1147
1158VALUE
1159rb_check_funcall_basic_kw(VALUE recv, ID mid, VALUE ancestor, int argc, const VALUE *argv, int kw_splat)
1160{
1161 const rb_callable_method_entry_t *cme;
1163 VALUE klass = CLASS_OF(recv);
1164 if (!klass) return Qundef; /* hidden object */
1165
1166 cme = rb_callable_method_entry(klass, mid);
1167 if (cme && METHOD_ENTRY_BASIC(cme) && RBASIC_CLASS(cme->defined_class) == ancestor) {
1168 ec = GET_EC();
1169 return rb_vm_call0(ec, recv, mid, argc, argv, cme, kw_splat);
1170 }
1171
1172 return Qundef;
1173}
1174
1175VALUE
1176rb_funcallv_public(VALUE recv, ID mid, int argc, const VALUE *argv)
1177{
1178 return rb_funcallv_scope(recv, mid, argc, argv, CALL_PUBLIC);
1179}
1180
1181VALUE
1182rb_funcallv_public_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
1183{
1184 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_PUBLIC_KW : CALL_PUBLIC);
1185}
1186
1187VALUE
1188rb_funcall_passing_block(VALUE recv, ID mid, int argc, const VALUE *argv)
1189{
1190 PASS_PASSED_BLOCK_HANDLER();
1191 return rb_funcallv_public(recv, mid, argc, argv);
1192}
1193
1194VALUE
1195rb_funcall_passing_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
1196{
1197 PASS_PASSED_BLOCK_HANDLER();
1198 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_PUBLIC_KW : CALL_PUBLIC);
1199}
1200
1201VALUE
1202rb_funcall_with_block(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE passed_procval)
1203{
1204 if (!NIL_P(passed_procval)) {
1205 vm_passed_block_handler_set(GET_EC(), passed_procval);
1206 }
1207
1208 return rb_funcallv_public(recv, mid, argc, argv);
1209}
1210
1211VALUE
1212rb_funcall_with_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE passed_procval, int kw_splat)
1213{
1214 if (!NIL_P(passed_procval)) {
1215 vm_passed_block_handler_set(GET_EC(), passed_procval);
1216 }
1217
1218 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_PUBLIC_KW : CALL_PUBLIC);
1219}
1220
1221static VALUE *
1222current_vm_stack_arg(const rb_execution_context_t *ec, const VALUE *argv)
1223{
1224 rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
1225 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, prev_cfp)) return NULL;
1226 if (prev_cfp->sp + 1 != argv) return NULL;
1227 return prev_cfp->sp + 1;
1228}
1229
1230static VALUE
1231send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope)
1232{
1233 ID id;
1234 VALUE vid;
1235 VALUE self;
1236 VALUE ret, vargv = 0;
1237 rb_execution_context_t *ec = GET_EC();
1238 int public = scope == CALL_PUBLIC || scope == CALL_PUBLIC_KW;
1239
1240 if (public) {
1241 self = Qundef;
1242 }
1243 else {
1244 self = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp)->self;
1245 }
1246
1247 if (argc == 0) {
1248 rb_raise(rb_eArgError, "no method name given");
1249 }
1250
1251 vid = *argv;
1252
1253 id = rb_check_id(&vid);
1254 if (!id) {
1255 if (rb_method_basic_definition_p(CLASS_OF(recv), idMethodMissing)) {
1256 VALUE exc = rb_make_no_method_exception(rb_eNoMethodError, 0,
1257 recv, argc, argv,
1258 !public);
1259 rb_exc_raise(exc);
1260 }
1261 if (!SYMBOL_P(*argv)) {
1262 VALUE *tmp_argv = current_vm_stack_arg(ec, argv);
1263 vid = rb_str_intern(vid);
1264 if (tmp_argv) {
1265 tmp_argv[0] = vid;
1266 }
1267 else if (argc > 1) {
1268 tmp_argv = ALLOCV_N(VALUE, vargv, argc);
1269 tmp_argv[0] = vid;
1270 MEMCPY(tmp_argv+1, argv+1, VALUE, argc-1);
1271 argv = tmp_argv;
1272 }
1273 else {
1274 argv = &vid;
1275 }
1276 }
1277 id = idMethodMissing;
1278 ec->method_missing_reason = MISSING_NOENTRY;
1279 }
1280 else {
1281 argv++; argc--;
1282 }
1283 PASS_PASSED_BLOCK_HANDLER_EC(ec);
1284 ret = rb_call0(ec, recv, id, argc, argv, scope, self);
1285 ALLOCV_END(vargv);
1286 return ret;
1287}
1288
1289static VALUE
1290send_internal_kw(int argc, const VALUE *argv, VALUE recv, call_type scope)
1291{
1292 if (rb_keyword_given_p()) {
1293 switch (scope) {
1294 case CALL_PUBLIC:
1295 scope = CALL_PUBLIC_KW;
1296 break;
1297 case CALL_FCALL:
1298 scope = CALL_FCALL_KW;
1299 break;
1300 default:
1301 break;
1302 }
1303 }
1304 return send_internal(argc, argv, recv, scope);
1305}
1306
1307/*
1308 * call-seq:
1309 * foo.send(symbol [, args...]) -> obj
1310 * foo.__send__(symbol [, args...]) -> obj
1311 * foo.send(string [, args...]) -> obj
1312 * foo.__send__(string [, args...]) -> obj
1313 *
1314 * Invokes the method identified by _symbol_, passing it any
1315 * arguments specified.
1316 * When the method is identified by a string, the string is converted
1317 * to a symbol.
1318 *
1319 * BasicObject implements +__send__+, Kernel implements +send+.
1320 * <code>__send__</code> is safer than +send+
1321 * when _obj_ has the same method name like <code>Socket</code>.
1322 * See also <code>public_send</code>.
1323 *
1324 * class Klass
1325 * def hello(*args)
1326 * "Hello " + args.join(' ')
1327 * end
1328 * end
1329 * k = Klass.new
1330 * k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
1331 */
1332
1333VALUE
1334rb_f_send(int argc, VALUE *argv, VALUE recv)
1335{
1336 return send_internal_kw(argc, argv, recv, CALL_FCALL);
1337}
1338
1339/*
1340 * call-seq:
1341 * obj.public_send(symbol [, args...]) -> obj
1342 * obj.public_send(string [, args...]) -> obj
1343 *
1344 * Invokes the method identified by _symbol_, passing it any
1345 * arguments specified. Unlike send, public_send calls public
1346 * methods only.
1347 * When the method is identified by a string, the string is converted
1348 * to a symbol.
1349 *
1350 * 1.public_send(:puts, "hello") # causes NoMethodError
1351 */
1352
1353static VALUE
1354rb_f_public_send(int argc, VALUE *argv, VALUE recv)
1355{
1356 return send_internal_kw(argc, argv, recv, CALL_PUBLIC);
1357}
1358
1359/* yield */
1360
1361static inline VALUE
1362rb_yield_0_kw(int argc, const VALUE * argv, int kw_splat)
1363{
1364 return vm_yield(GET_EC(), argc, argv, kw_splat);
1365}
1366
1367static inline VALUE
1368rb_yield_0(int argc, const VALUE * argv)
1369{
1370 return vm_yield(GET_EC(), argc, argv, RB_NO_KEYWORDS);
1371}
1372
1373VALUE
1374rb_yield_1(VALUE val)
1375{
1376 return rb_yield_0(1, &val);
1377}
1378
1379VALUE
1381{
1382 if (UNDEF_P(val)) {
1383 return rb_yield_0(0, NULL);
1384 }
1385 else {
1386 return rb_yield_0(1, &val);
1387 }
1388}
1389
1390VALUE
1391rb_ec_yield(rb_execution_context_t *ec, VALUE val)
1392{
1393 if (UNDEF_P(val)) {
1394 return vm_yield(ec, 0, NULL, RB_NO_KEYWORDS);
1395 }
1396 else {
1397 return vm_yield(ec, 1, &val, RB_NO_KEYWORDS);
1398 }
1399}
1400
1401#undef rb_yield_values
1402VALUE
1404{
1405 if (n == 0) {
1406 return rb_yield_0(0, 0);
1407 }
1408 else {
1409 int i;
1410 VALUE *argv;
1411 va_list args;
1412 argv = ALLOCA_N(VALUE, n);
1413
1414 va_start(args, n);
1415 for (i=0; i<n; i++) {
1416 argv[i] = va_arg(args, VALUE);
1417 }
1418 va_end(args);
1419
1420 return rb_yield_0(n, argv);
1421 }
1422}
1423
1424VALUE
1425rb_yield_values2(int argc, const VALUE *argv)
1426{
1427 return rb_yield_0(argc, argv);
1428}
1429
1430VALUE
1431rb_yield_values_kw(int argc, const VALUE *argv, int kw_splat)
1432{
1433 return rb_yield_0_kw(argc, argv, kw_splat);
1434}
1435
1436VALUE
1438{
1439 VALUE tmp = rb_check_array_type(values);
1440 VALUE v;
1441 if (NIL_P(tmp)) {
1442 rb_raise(rb_eArgError, "not an array");
1443 }
1444 v = rb_yield_0(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp));
1445 RB_GC_GUARD(tmp);
1446 return v;
1447}
1448
1449VALUE
1450rb_yield_splat_kw(VALUE values, int kw_splat)
1451{
1452 VALUE tmp = rb_check_array_type(values);
1453 VALUE v;
1454 if (NIL_P(tmp)) {
1455 rb_raise(rb_eArgError, "not an array");
1456 }
1457 v = rb_yield_0_kw(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp), kw_splat);
1458 RB_GC_GUARD(tmp);
1459 return v;
1460}
1461
1462VALUE
1463rb_yield_force_blockarg(VALUE values)
1464{
1465 return vm_yield_force_blockarg(GET_EC(), values);
1466}
1467
1468VALUE
1470{
1471 return vm_yield_with_block(GET_EC(), argc, argv,
1472 NIL_P(blockarg) ? VM_BLOCK_HANDLER_NONE : blockarg,
1474}
1475
1476#if VMDEBUG
1477static const char *
1478vm_frametype_name(const rb_control_frame_t *cfp);
1479#endif
1480
1481static VALUE
1482rb_iterate0(VALUE (* it_proc) (VALUE), VALUE data1,
1483 const struct vm_ifunc *const ifunc,
1484 rb_execution_context_t *ec_arg)
1485{
1486 enum ruby_tag_type state;
1487 volatile VALUE retval = Qnil;
1488 rb_execution_context_t * volatile ec = ec_arg;
1489 rb_control_frame_t *volatile const cfp = ec->cfp;
1490
1491 EC_PUSH_TAG(ec);
1492 state = EC_EXEC_TAG();
1493 if (state == 0) {
1494 iter_retry:
1495 {
1496 VALUE block_handler;
1497
1498 if (ifunc) {
1499 struct rb_captured_block *captured = VM_CFP_TO_CAPTURED_BLOCK(cfp);
1500 captured->code.ifunc = ifunc;
1501 block_handler = VM_BH_FROM_IFUNC_BLOCK(captured);
1502 }
1503 else {
1504 block_handler = VM_CF_BLOCK_HANDLER(cfp);
1505 }
1506 vm_passed_block_handler_set(ec, block_handler);
1507 }
1508 retval = (*it_proc) (data1);
1509 }
1510 else if (state == TAG_BREAK || state == TAG_RETRY) {
1511 const struct vm_throw_data *const err = (struct vm_throw_data *)ec->errinfo;
1512 const rb_control_frame_t *const escape_cfp = THROW_DATA_CATCH_FRAME(err);
1513
1514 if (cfp == escape_cfp) {
1515 rb_vm_rewind_cfp(ec, cfp);
1516
1517 state = 0;
1518 ec->tag->state = TAG_NONE;
1519 ec->errinfo = Qnil;
1520
1521 if (state == TAG_RETRY) goto iter_retry;
1522 retval = THROW_DATA_VAL(err);
1523 }
1524 else if (0) {
1525 SDR(); fprintf(stderr, "%p, %p\n", (void *)cfp, (void *)escape_cfp);
1526 }
1527 }
1528 EC_POP_TAG();
1529
1530 if (state) {
1531 EC_JUMP_TAG(ec, state);
1532 }
1533 return retval;
1534}
1535
1536static VALUE
1537rb_iterate_internal(VALUE (* it_proc)(VALUE), VALUE data1,
1538 rb_block_call_func_t bl_proc, VALUE data2)
1539{
1540 return rb_iterate0(it_proc, data1,
1541 bl_proc ? rb_vm_ifunc_proc_new(bl_proc, (void *)data2) : 0,
1542 GET_EC());
1543}
1544
1546 VALUE obj;
1547 ID mid;
1548 int argc;
1549 const VALUE *argv;
1550 int kw_splat;
1551};
1552
1553static VALUE
1554iterate_method(VALUE obj)
1555{
1556 const struct iter_method_arg * arg =
1557 (struct iter_method_arg *) obj;
1558
1559 return rb_call(arg->obj, arg->mid, arg->argc, arg->argv, arg->kw_splat ? CALL_FCALL_KW : CALL_FCALL);
1560}
1561
1562VALUE rb_block_call_kw(VALUE obj, ID mid, int argc, const VALUE * argv, rb_block_call_func_t bl_proc, VALUE data2, int kw_splat);
1563
1564VALUE
1565rb_block_call(VALUE obj, ID mid, int argc, const VALUE * argv,
1566 rb_block_call_func_t bl_proc, VALUE data2)
1567{
1568 return rb_block_call_kw(obj, mid, argc, argv, bl_proc, data2, RB_NO_KEYWORDS);
1569}
1570
1571VALUE
1572rb_block_call_kw(VALUE obj, ID mid, int argc, const VALUE * argv,
1573 rb_block_call_func_t bl_proc, VALUE data2, int kw_splat)
1574{
1575 struct iter_method_arg arg;
1576
1577 arg.obj = obj;
1578 arg.mid = mid;
1579 arg.argc = argc;
1580 arg.argv = argv;
1581 arg.kw_splat = kw_splat;
1582 return rb_iterate_internal(iterate_method, (VALUE)&arg, bl_proc, data2);
1583}
1584
1585/*
1586 * A flexible variant of rb_block_call and rb_block_call_kw.
1587 * This function accepts flags:
1588 *
1589 * RB_NO_KEYWORDS, RB_PASS_KEYWORDS, RB_PASS_CALLED_KEYWORDS:
1590 * Works as the same as rb_block_call_kw.
1591 *
1592 * RB_BLOCK_NO_USE_PACKED_ARGS:
1593 * The given block ("bl_proc") does not use "yielded_arg" of rb_block_call_func_t.
1594 * Instead, the block accesses the yielded arguments via "argc" and "argv".
1595 * This flag allows the called method to yield arguments without allocating an Array.
1596 */
1597VALUE
1598rb_block_call2(VALUE obj, ID mid, int argc, const VALUE *argv,
1599 rb_block_call_func_t bl_proc, VALUE data2, long flags)
1600{
1601 struct iter_method_arg arg;
1602
1603 arg.obj = obj;
1604 arg.mid = mid;
1605 arg.argc = argc;
1606 arg.argv = argv;
1607 arg.kw_splat = flags & 1;
1608
1609 struct vm_ifunc *ifunc = rb_vm_ifunc_proc_new(bl_proc, (void *)data2);
1610 if (flags & RB_BLOCK_NO_USE_PACKED_ARGS)
1611 ifunc->flags |= IFUNC_YIELD_OPTIMIZABLE;
1612
1613 return rb_iterate0(iterate_method, (VALUE)&arg, ifunc, GET_EC());
1614}
1615
1616VALUE
1617rb_lambda_call(VALUE obj, ID mid, int argc, const VALUE *argv,
1618 rb_block_call_func_t bl_proc, int min_argc, int max_argc,
1619 VALUE data2)
1620{
1621 struct iter_method_arg arg;
1622 struct vm_ifunc *block;
1623
1624 if (!bl_proc) rb_raise(rb_eArgError, "NULL lambda function");
1625 arg.obj = obj;
1626 arg.mid = mid;
1627 arg.argc = argc;
1628 arg.argv = argv;
1629 arg.kw_splat = 0;
1630 block = rb_vm_ifunc_new(bl_proc, (void *)data2, min_argc, max_argc);
1631 return rb_iterate0(iterate_method, (VALUE)&arg, block, GET_EC());
1632}
1633
1634static VALUE
1635iterate_check_method(VALUE obj)
1636{
1637 const struct iter_method_arg * arg =
1638 (struct iter_method_arg *) obj;
1639
1640 return rb_check_funcall(arg->obj, arg->mid, arg->argc, arg->argv);
1641}
1642
1643VALUE
1644rb_check_block_call(VALUE obj, ID mid, int argc, const VALUE *argv,
1645 rb_block_call_func_t bl_proc, VALUE data2)
1646{
1647 struct iter_method_arg arg;
1648
1649 arg.obj = obj;
1650 arg.mid = mid;
1651 arg.argc = argc;
1652 arg.argv = argv;
1653 arg.kw_splat = 0;
1654 return rb_iterate_internal(iterate_check_method, (VALUE)&arg, bl_proc, data2);
1655}
1656
1657VALUE
1659{
1660 return rb_call(obj, idEach, 0, 0, CALL_FCALL);
1661}
1662
1663static VALUE eval_default_path = Qfalse;
1664
1665#define EVAL_LOCATION_MARK "eval at "
1666#define EVAL_LOCATION_MARK_LEN (int)rb_strlen_lit(EVAL_LOCATION_MARK)
1667
1668static VALUE
1669get_eval_default_path(void)
1670{
1671 int location_lineno;
1672 VALUE location_path = rb_source_location(&location_lineno);
1673 if (!NIL_P(location_path)) {
1674 return rb_fstring(rb_sprintf("("EVAL_LOCATION_MARK"%"PRIsVALUE":%d)",
1675 location_path, location_lineno));
1676 }
1677
1678 if (!eval_default_path) {
1679 eval_default_path = rb_fstring_lit("(eval)");
1680 rb_vm_register_global_object(eval_default_path);
1681 }
1682 return eval_default_path;
1683}
1684
1685static inline int
1686compute_isolated_depth_from_ep(const VALUE *ep)
1687{
1688 int depth = 1;
1689 while (1) {
1690 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ISOLATED)) return depth;
1691 if (VM_ENV_LOCAL_P(ep)) return 0;
1692 ep = VM_ENV_PREV_EP(ep);
1693 depth++;
1694 }
1695}
1696
1697static inline int
1698compute_isolated_depth_from_block(const struct rb_block *blk)
1699{
1700 return compute_isolated_depth_from_ep(vm_block_ep(blk));
1701}
1702
1703static const rb_iseq_t *
1704pm_eval_make_iseq(VALUE src, VALUE fname, int line,
1705 const struct rb_block *base_block)
1706{
1707 const rb_iseq_t *const parent = vm_block_iseq(base_block);
1708 const rb_iseq_t *iseq = parent;
1709 VALUE name = rb_fstring_lit("<compiled>");
1710
1711 int coverage_enabled = ((rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) != 0) ? 1 : 0;
1712 int isolated_depth = compute_isolated_depth_from_block(base_block);
1713
1714 if (!fname) {
1715 fname = rb_source_location(&line);
1716 }
1717
1718 if (!UNDEF_P(fname)) {
1719 if (!NIL_P(fname)) fname = rb_fstring(fname);
1720 }
1721 else {
1722 fname = get_eval_default_path();
1723 coverage_enabled = 0;
1724 }
1725
1726 pm_parse_result_t result;
1727 pm_parse_result_init(&result);
1728 pm_options_line_set(result.options, line);
1729 result.node.coverage_enabled = coverage_enabled;
1730
1731 // Count scopes, one for each parent iseq, plus one for our local scope
1732 int scopes_count = 0;
1733 do {
1734 scopes_count++;
1735 } while ((iseq = ISEQ_BODY(iseq)->parent_iseq));
1736 pm_options_scopes_init(result.options, scopes_count + 1);
1737
1738 // Walk over the scope tree, adding known locals at the correct depths. The
1739 // scope array should be deepest -> shallowest. so lower indexes in the
1740 // scopes array refer to root nodes on the tree, and higher indexes are the
1741 // leaf nodes.
1742 iseq = parent;
1743
1744#define FORWARDING_POSITIONALS_CHR '*'
1745#define FORWARDING_POSITIONALS_STR "*"
1746#define FORWARDING_KEYWORDS_CHR ':'
1747#define FORWARDING_KEYWORDS_STR ":"
1748#define FORWARDING_BLOCK_CHR '&'
1749#define FORWARDING_BLOCK_STR "&"
1750#define FORWARDING_ALL_CHR '.'
1751#define FORWARDING_ALL_STR "."
1752
1753 for (int scopes_index = 0; scopes_index < scopes_count; scopes_index++) {
1754 VALUE iseq_value = (VALUE)iseq;
1755 int locals_count = ISEQ_BODY(iseq)->local_table_size;
1756
1757 pm_options_scope_t *options_scope = pm_options_scope_mut(result.options, scopes_count - scopes_index - 1);
1758 pm_options_scope_init(options_scope, locals_count);
1759
1760 uint8_t forwarding = PM_OPTIONS_SCOPE_FORWARDING_NONE;
1761
1762 for (int local_index = 0; local_index < locals_count; local_index++) {
1763 pm_string_t *scope_local = pm_options_scope_local_mut(options_scope, local_index);
1764 ID local = ISEQ_BODY(iseq)->local_table[local_index];
1765
1766 if (rb_is_local_id(local)) {
1767 VALUE name_obj = rb_id2str(local);
1768 const char *name = RSTRING_PTR(name_obj);
1769 size_t length = RSTRING_LEN(name_obj);
1770
1771 /* We need to duplicate the string because the Ruby string may
1772 * be embedded so compaction could move the string and the pointer
1773 * will change. */
1774 char *name_dup = xmalloc(length);
1775 MEMCPY(name_dup, name, char, length);
1776
1777 RB_GC_GUARD(name_obj);
1778
1779 pm_string_owned_init(scope_local, (uint8_t *) name_dup, length);
1780 }
1781 else if (local == idMULT) {
1783 pm_string_constant_init(scope_local, FORWARDING_POSITIONALS_STR, 1);
1784 }
1785 else if (local == idPow) {
1787 pm_string_constant_init(scope_local, FORWARDING_KEYWORDS_STR, 1);
1788 }
1789 else if (local == idAnd) {
1791 pm_string_constant_init(scope_local, FORWARDING_BLOCK_STR, 1);
1792 }
1793 else if (local == idDot3) {
1794 forwarding |= PM_OPTIONS_SCOPE_FORWARDING_ALL;
1795 pm_string_constant_init(scope_local, FORWARDING_ALL_STR, 1);
1796 }
1797 }
1798
1799 pm_options_scope_forwarding_set(options_scope, forwarding);
1800 iseq = ISEQ_BODY(iseq)->parent_iseq;
1801
1802 /* We need to GC guard the iseq because the code above malloc memory
1803 * which could trigger a GC. Since we only use ISEQ_BODY, the compiler
1804 * may optimize out the iseq local variable so we need to GC guard it. */
1805 RB_GC_GUARD(iseq_value);
1806 }
1807
1808 // Add our empty local scope at the very end of the array for our eval
1809 // scope's locals.
1810 pm_options_scope_init(pm_options_scope_mut(result.options, scopes_count), 0);
1811
1812 VALUE script_lines;
1813 VALUE error = pm_parse_string(&result, src, fname, ruby_vm_keep_script_lines ? &script_lines : NULL);
1814
1815 // If the parse failed, clean up and raise.
1816 if (error != Qnil) {
1817 pm_parse_result_free(&result);
1818 rb_exc_raise(error);
1819 }
1820
1821 // Create one scope node for each scope passed in, initialize the local
1822 // lookup table with all the local variable information attached to the
1823 // scope used by the parser.
1824 pm_scope_node_t *node = &result.node;
1825 iseq = parent;
1826
1827 for (int scopes_index = 0; scopes_index < scopes_count; scopes_index++) {
1828 pm_scope_node_t *parent_scope = ruby_xcalloc(1, sizeof(pm_scope_node_t));
1829 RUBY_ASSERT(parent_scope != NULL);
1830
1831 const pm_options_scope_t *options_scope = pm_options_scope(result.options, scopes_count - scopes_index - 1);
1832 parent_scope->coverage_enabled = coverage_enabled;
1833 parent_scope->parser = result.parser;
1834 int locals_count = ISEQ_BODY(iseq)->local_table_size;
1835 pm_index_lookup_table_init_heap(&parent_scope->index_lookup_table, (int) pm_parser_constants_size(result.parser));
1836 parent_scope->local_table_for_iseq_size = locals_count;
1837 pm_constant_id_list_init(&parent_scope->locals);
1838
1839 for (int local_index = 0; local_index < locals_count; local_index++) {
1840 const pm_string_t *scope_local = pm_options_scope_local(options_scope, local_index);
1841 pm_constant_id_t constant_id = 0;
1842
1843 const uint8_t *source = pm_string_source(scope_local);
1844 size_t length = pm_string_length(scope_local);
1845
1846 if (length > 0) {
1847 if (length == 1) {
1848 switch (*source) {
1849 case FORWARDING_POSITIONALS_CHR:
1850 constant_id = PM_CONSTANT_MULT;
1851 break;
1852 case FORWARDING_KEYWORDS_CHR:
1853 constant_id = PM_CONSTANT_POW;
1854 break;
1855 case FORWARDING_BLOCK_CHR:
1856 constant_id = PM_CONSTANT_AND;
1857 break;
1858 case FORWARDING_ALL_CHR:
1859 constant_id = PM_CONSTANT_DOT3;
1860 break;
1861 default:
1862 constant_id = pm_parser_constant_find(result.parser, source, length);
1863 break;
1864 }
1865 }
1866 else {
1867 constant_id = pm_parser_constant_find(result.parser, source, length);
1868 }
1869
1870 pm_index_lookup_table_insert(&parent_scope->index_lookup_table, constant_id, local_index);
1871 }
1872
1873 pm_constant_id_list_append(result.arena, &parent_scope->locals, constant_id);
1874 }
1875
1876 node->previous = parent_scope;
1877 node = parent_scope;
1878 iseq = ISEQ_BODY(iseq)->parent_iseq;
1879 }
1880
1881#undef FORWARDING_POSITIONALS_CHR
1882#undef FORWARDING_POSITIONALS_STR
1883#undef FORWARDING_KEYWORDS_CHR
1884#undef FORWARDING_KEYWORDS_STR
1885#undef FORWARDING_BLOCK_CHR
1886#undef FORWARDING_BLOCK_STR
1887#undef FORWARDING_ALL_CHR
1888#undef FORWARDING_ALL_STR
1889
1890 int error_state;
1891 iseq = pm_iseq_new_eval(&result.node, name, fname, Qnil, line, parent, isolated_depth, &error_state);
1892
1893 pm_scope_node_t *prev = result.node.previous;
1894 while (prev) {
1895 pm_scope_node_t *next = prev->previous;
1896 pm_scope_node_destroy(prev);
1897 SIZED_FREE(prev);
1898 prev = next;
1899 }
1900
1901 pm_parse_result_free(&result);
1902
1903 // If there was an error, raise it after memory has been cleaned up
1904 if (error_state) {
1905 RUBY_ASSERT(iseq == NULL);
1906 rb_jump_tag(error_state);
1907 }
1908
1909 rb_exec_event_hook_script_compiled(GET_EC(), iseq, src);
1910
1911 return iseq;
1912}
1913
1914static const rb_iseq_t *
1915eval_make_iseq(VALUE src, VALUE fname, int line,
1916 const struct rb_block *base_block)
1917{
1918 if (rb_ruby_prism_p()) {
1919 return pm_eval_make_iseq(src, fname, line, base_block);
1920 }
1921 const VALUE parser = rb_parser_new();
1922 const rb_iseq_t *const parent = vm_block_iseq(base_block);
1923 rb_iseq_t *iseq = NULL;
1924 VALUE ast_value;
1925 rb_ast_t *ast;
1926
1927 int coverage_enabled = (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) != 0;
1928 int isolated_depth = compute_isolated_depth_from_block(base_block);
1929
1930 if (!fname) {
1931 fname = rb_source_location(&line);
1932 }
1933
1934 if (!UNDEF_P(fname)) {
1935 if (!NIL_P(fname)) fname = rb_fstring(fname);
1936 }
1937 else {
1938 fname = get_eval_default_path();
1939 coverage_enabled = FALSE;
1940 }
1941
1942 rb_parser_set_context(parser, parent, FALSE);
1943 if (ruby_vm_keep_script_lines) rb_parser_set_script_lines(parser);
1944 ast_value = rb_parser_compile_string_path(parser, fname, src, line);
1945
1946 ast = rb_ruby_ast_data_get(ast_value);
1947
1948 if (ast->body.root) {
1949 ast->body.coverage_enabled = coverage_enabled;
1950 iseq = rb_iseq_new_eval(ast_value,
1951 ISEQ_BODY(parent)->location.label,
1952 fname, Qnil, line,
1953 parent, isolated_depth);
1954 }
1955 rb_ast_dispose(ast);
1956
1957 if (iseq != NULL) {
1958 if (0 && iseq) { /* for debug */
1959 VALUE disasm = rb_iseq_disasm(iseq);
1960 printf("%s\n", StringValuePtr(disasm));
1961 }
1962
1963 rb_exec_event_hook_script_compiled(GET_EC(), iseq, src);
1964 }
1965
1966 return iseq;
1967}
1968
1969static VALUE
1970eval_string_with_cref(VALUE self, VALUE src, rb_cref_t *cref, VALUE file, int line)
1971{
1972 rb_execution_context_t *ec = GET_EC();
1973 struct rb_block block;
1974 const rb_iseq_t *iseq;
1975 rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1976 if (!cfp) {
1977 rb_raise(rb_eRuntimeError, "Can't eval on top of Fiber or Thread");
1978 }
1979
1980 block.as.captured = *VM_CFP_TO_CAPTURED_BLOCK(cfp);
1981 block.as.captured.self = self;
1982 block.as.captured.code.iseq = CFP_ISEQ(cfp);
1983 block.type = block_type_iseq;
1984
1985 // EP is not escaped to the heap here, but captured and reused by another frame.
1986 // ZJIT's locals are incompatible with it unlike YJIT's, so invalidate the ISEQ for ZJIT.
1987 if (rb_zjit_enabled_p) rb_zjit_invalidate_no_ep_escape(CFP_ISEQ(cfp));
1988
1989 iseq = eval_make_iseq(src, file, line, &block);
1990 if (!iseq) {
1991 rb_exc_raise(ec->errinfo);
1992 }
1993
1994 /* TODO: what the code checking? */
1995 if (!cref && block.as.captured.code.val) {
1996 rb_cref_t *orig_cref = vm_get_cref(vm_block_ep(&block));
1997 cref = rb_vm_cref_dup(orig_cref);
1998 }
1999 vm_set_eval_stack(ec, iseq, cref, &block);
2000
2001 /* kick */
2002 return vm_exec(ec);
2003}
2004
2005static VALUE
2006eval_string_with_scope(VALUE scope, VALUE src, VALUE file, int line)
2007{
2008 rb_execution_context_t *ec = GET_EC();
2009 rb_binding_t *bind = Check_TypedStruct(scope, &ruby_binding_data_type);
2010 const rb_iseq_t *iseq = eval_make_iseq(src, file, line, &bind->block);
2011 if (!iseq) {
2012 rb_exc_raise(ec->errinfo);
2013 }
2014
2015 vm_set_eval_stack(ec, iseq, NULL, &bind->block);
2016
2017 /* save new env */
2018 if (ISEQ_BODY(iseq)->local_table_size > 0) {
2019 vm_bind_update_env(scope, bind, vm_make_env_object(ec, ec->cfp));
2020 }
2021
2022 /* kick */
2023 return vm_exec(ec);
2024}
2025
2026/*
2027 * call-seq:
2028 * eval(string, binding = nil, filename = default_filename, lineno = 1) -> obj
2029 *
2030 * Evaluates the Ruby expression(s) in +string+. Returns the result of the last
2031 * expression.
2032 *
2033 * str = "Hello"
2034 * eval("str + ' World'") # => "Hello World"
2035 *
2036 * If +binding+ is given, which must be a Binding object, the
2037 * evaluation is performed in its context. Otherwise, the
2038 * evaluation is performed in the context of the caller.
2039 *
2040 * def get_binding(str) = binding
2041 * str = "Hello"
2042 * eval("str + ' World'", get_binding("Bye")) # => "Bye World"
2043 *
2044 * If the optional +filename+ is given, it will be used as the
2045 * filename of the evaluation (for <tt>__FILE__</tt> and errors).
2046 * Otherwise, it will default to <tt>(eval at __FILE__:__LINE__)</tt>
2047 * where <tt>__FILE__</tt> and <tt>__LINE__</tt> are the filename and
2048 * line number of the caller, respectively.
2049 *
2050 * eval("puts __FILE__") # => "(eval at test.rb:1)"
2051 * eval("puts __FILE__", nil, "foobar.rb") # => "foobar.rb"
2052 *
2053 * If the optional +lineno+ is given, it will be used as the
2054 * line number of the evaluation (for <tt>__LINE__</tt> and errors).
2055 * Otherwise, it will default to 1.
2056 *
2057 * eval("puts __LINE__") # => 1
2058 * eval("puts __LINE__", nil, "foobar.rb", 10) # => 10
2059 */
2060
2061VALUE
2062rb_f_eval(int argc, const VALUE *argv, VALUE self)
2063{
2064 VALUE src, scope, vfile, vline;
2065 VALUE file = Qundef;
2066 int line = 1;
2067
2068 rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
2069 StringValue(src);
2070 if (argc >= 3) {
2071 StringValue(vfile);
2072 }
2073 if (argc >= 4) {
2074 line = NUM2INT(vline);
2075 }
2076
2077 if (!NIL_P(vfile))
2078 file = vfile;
2079
2080 if (NIL_P(scope))
2081 return eval_string_with_cref(self, src, NULL, file, line);
2082 else
2083 return eval_string_with_scope(scope, src, file, line);
2084}
2085
2087VALUE
2088ruby_eval_string_from_file(const char *str, const char *filename)
2089{
2090 VALUE file = filename ? rb_str_new_cstr(filename) : 0;
2091 rb_execution_context_t *ec = GET_EC();
2092 rb_control_frame_t *cfp = ec ? rb_vm_get_ruby_level_next_cfp(ec, ec->cfp) : NULL;
2093 VALUE self = cfp ? cfp->self : rb_vm_top_self();
2094 return eval_string_with_cref(self, rb_str_new2(str), NULL, file, 1);
2095}
2096
2097VALUE
2098rb_eval_string(const char *str)
2099{
2100 return ruby_eval_string_from_file(str, "eval");
2101}
2102
2103static VALUE
2104eval_string_protect(VALUE str)
2105{
2106 return rb_eval_string((char *)str);
2107}
2108
2109VALUE
2110rb_eval_string_protect(const char *str, int *pstate)
2111{
2112 return rb_protect(eval_string_protect, (VALUE)str, pstate);
2113}
2114
2116 VALUE top_self;
2117 VALUE klass;
2118 const char *str;
2119};
2120
2121static VALUE
2122eval_string_wrap_protect(VALUE data)
2123{
2124 const struct eval_string_wrap_arg *const arg = (struct eval_string_wrap_arg*)data;
2125 rb_cref_t *cref = rb_vm_cref_new_toplevel();
2126 cref->klass_or_self = arg->klass;
2127 return eval_string_with_cref(arg->top_self, rb_str_new_cstr(arg->str), cref, rb_str_new_cstr("eval"), 1);
2128}
2129
2130VALUE
2131rb_eval_string_wrap(const char *str, int *pstate)
2132{
2133 int state;
2134 rb_thread_t *th = GET_THREAD();
2135 VALUE self = th->top_self;
2136 VALUE wrapper = th->top_wrapper;
2137 VALUE val;
2138 struct eval_string_wrap_arg data;
2139
2140 th->top_wrapper = rb_module_new();
2141 th->top_self = rb_obj_clone(rb_vm_top_self());
2142 rb_extend_object(th->top_self, th->top_wrapper);
2143
2144 data.top_self = th->top_self;
2145 data.klass = th->top_wrapper;
2146 data.str = str;
2147
2148 val = rb_protect(eval_string_wrap_protect, (VALUE)&data, &state);
2149
2150 th->top_self = self;
2151 th->top_wrapper = wrapper;
2152
2153 if (pstate) {
2154 *pstate = state;
2155 }
2156 else if (state != TAG_NONE) {
2157 EC_JUMP_TAG(th->ec, state);
2158 }
2159 return val;
2160}
2161
2162VALUE
2163rb_eval_cmd_kw(VALUE cmd, VALUE arg, int kw_splat)
2164{
2165 Check_Type(arg, T_ARRAY);
2166 int argc = RARRAY_LENINT(arg);
2167 const VALUE *argv = RARRAY_CONST_PTR(arg);
2168 VALUE val = rb_eval_cmd_call_kw(cmd, argc, argv, kw_splat);
2169 RB_GC_GUARD(arg);
2170 return val;
2171}
2172
2173VALUE
2174rb_eval_cmd_call_kw(VALUE cmd, int argc, const VALUE *argv, int kw_splat)
2175{
2176 enum ruby_tag_type state;
2177 volatile VALUE val = Qnil; /* OK */
2178 rb_execution_context_t * volatile ec = GET_EC();
2179
2180 EC_PUSH_TAG(ec);
2181 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2182 if (!RB_TYPE_P(cmd, T_STRING)) {
2183 val = rb_funcallv_kw(cmd, idCall, argc, argv, kw_splat);
2184 }
2185 else {
2186 val = eval_string_with_cref(rb_vm_top_self(), cmd, NULL, 0, 0);
2187 }
2188 }
2189 EC_POP_TAG();
2190
2191 if (state) EC_JUMP_TAG(ec, state);
2192 return val;
2193}
2194
2195/* block eval under the class/module context */
2196
2197static VALUE
2198yield_under(VALUE self, int singleton, int argc, const VALUE *argv, int kw_splat)
2199{
2200 rb_execution_context_t *ec = GET_EC();
2201 rb_control_frame_t *cfp = ec->cfp;
2202 VALUE block_handler = VM_CF_BLOCK_HANDLER(cfp);
2203 VALUE new_block_handler = 0;
2204 const struct rb_captured_block *captured = NULL;
2205 struct rb_captured_block new_captured;
2206 const VALUE *ep = NULL;
2207 rb_cref_t *cref;
2208 int is_lambda = FALSE;
2209 const rb_cref_t *proc_cref = NULL;
2210
2211 if (block_handler != VM_BLOCK_HANDLER_NONE) {
2212 again:
2213 switch (vm_block_handler_type(block_handler)) {
2214 case block_handler_type_iseq:
2215 captured = VM_BH_TO_CAPT_BLOCK(block_handler);
2216 new_captured = *captured;
2217 new_block_handler = VM_BH_FROM_ISEQ_BLOCK(&new_captured);
2218 break;
2219 case block_handler_type_ifunc:
2220 captured = VM_BH_TO_CAPT_BLOCK(block_handler);
2221 new_captured = *captured;
2222 new_block_handler = VM_BH_FROM_IFUNC_BLOCK(&new_captured);
2223 break;
2224 case block_handler_type_proc:
2225 {
2226 VALUE procval = VM_BH_TO_PROC(block_handler);
2227 rb_proc_t *po;
2228 GetProcPtr(procval, po);
2229 is_lambda = po->header.is_lambda;
2230 if (po->header.is_refined) proc_cref = rb_proc_refinements_cref_for_call(procval);
2231 block_handler = vm_block_to_block_handler(&po->block);
2232 }
2233 goto again;
2234 case block_handler_type_symbol:
2235 return rb_sym_proc_call(SYM2ID(VM_BH_TO_SYMBOL(block_handler)),
2236 argc, argv, kw_splat,
2237 VM_BLOCK_HANDLER_NONE);
2238 }
2239
2240 new_captured.self = self;
2241 ep = captured->ep;
2242
2243 VM_FORCE_WRITE_SPECIAL_CONST(&VM_CF_LEP(ec->cfp)[VM_ENV_DATA_INDEX_SPECVAL], new_block_handler);
2244 }
2245
2246 VM_ASSERT(singleton || RB_TYPE_P(self, T_MODULE) || RB_TYPE_P(self, T_CLASS));
2247 cref = vm_cref_push(ec, self, ep, TRUE, singleton);
2248
2249 if (proc_cref && !NIL_P(CREF_REFINEMENTS(proc_cref))) {
2250 CREF_REFINEMENTS_SET(cref, rb_hash_dup(CREF_REFINEMENTS(proc_cref)));
2251 CREF_REFINED_PROC_SET(cref);
2252 }
2253
2254 return vm_yield_with_cref(ec, argc, argv, kw_splat, cref, is_lambda);
2255}
2256
2257VALUE
2258rb_yield_refine_block(VALUE refinement, VALUE refinements)
2259{
2260 rb_execution_context_t *ec = GET_EC();
2261 VALUE block_handler = VM_CF_BLOCK_HANDLER(ec->cfp);
2262
2263 if (vm_block_handler_type(block_handler) != block_handler_type_iseq) {
2264 rb_bug("rb_yield_refine_block: an iseq block is required");
2265 }
2266 else {
2267 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(block_handler);
2268 struct rb_captured_block new_captured = *captured;
2269 const VALUE *const argv = &new_captured.self; /* dummy to suppress nonnull warning from gcc */
2270 VALUE new_block_handler = VM_BH_FROM_ISEQ_BLOCK(&new_captured);
2271 const VALUE *ep = captured->ep;
2272 rb_cref_t *cref = vm_cref_push(ec, refinement, ep, TRUE, FALSE);
2273 CREF_REFINEMENTS_SET(cref, refinements);
2274 VM_FORCE_WRITE_SPECIAL_CONST(&VM_CF_LEP(ec->cfp)[VM_ENV_DATA_INDEX_SPECVAL], new_block_handler);
2275 new_captured.self = refinement;
2276 return vm_yield_with_cref(ec, 0, argv, RB_NO_KEYWORDS, cref, FALSE);
2277 }
2278}
2279
2280/* string eval under the class/module context */
2281static VALUE
2282eval_under(VALUE self, int singleton, VALUE src, VALUE file, int line)
2283{
2284 rb_cref_t *cref = vm_cref_push(GET_EC(), self, NULL, FALSE, singleton);
2285 StringValue(src);
2286
2287 return eval_string_with_cref(self, src, cref, file, line);
2288}
2289
2290static VALUE
2291specific_eval(int argc, const VALUE *argv, VALUE self, int singleton, int kw_splat)
2292{
2293 if (rb_block_given_p()) {
2294 rb_check_arity(argc, 0, 0);
2295 return yield_under(self, singleton, 1, &self, kw_splat);
2296 }
2297 else {
2298 VALUE file = Qnil;
2299 int line = 1;
2300 VALUE code;
2301
2302 rb_check_arity(argc, 1, 3);
2303 code = argv[0];
2304 StringValue(code);
2305 if (argc > 2)
2306 line = NUM2INT(argv[2]);
2307 if (argc > 1) {
2308 file = argv[1];
2309 if (!NIL_P(file)) StringValue(file);
2310 }
2311
2312 if (NIL_P(file)) {
2313 file = get_eval_default_path();
2314 }
2315
2316 return eval_under(self, singleton, code, file, line);
2317 }
2318}
2319
2320/*
2321 * call-seq:
2322 * obj.instance_eval(string [, filename [, lineno]] ) -> obj
2323 * obj.instance_eval {|obj| block } -> obj
2324 *
2325 * Evaluates a string containing Ruby source code, or the given block,
2326 * within the context of the receiver (_obj_). In order to set the
2327 * context, the variable +self+ is set to _obj_ while
2328 * the code is executing, giving the code access to _obj_'s
2329 * instance variables and private methods.
2330 *
2331 * When <code>instance_eval</code> is given a block, _obj_ is also
2332 * passed in as the block's only argument.
2333 *
2334 * When <code>instance_eval</code> is given a +String+, the optional
2335 * second and third parameters supply a filename and starting line number
2336 * that are used when reporting compilation errors.
2337 *
2338 * class KlassWithSecret
2339 * def initialize
2340 * @secret = 99
2341 * end
2342 * private
2343 * def the_secret
2344 * "Ssssh! The secret is #{@secret}."
2345 * end
2346 * end
2347 * k = KlassWithSecret.new
2348 * k.instance_eval { @secret } #=> 99
2349 * k.instance_eval { the_secret } #=> "Ssssh! The secret is 99."
2350 * k.instance_eval {|obj| obj == self } #=> true
2351 */
2352
2353static VALUE
2354rb_obj_instance_eval_internal(int argc, const VALUE *argv, VALUE self)
2355{
2356 return specific_eval(argc, argv, self, TRUE, RB_PASS_CALLED_KEYWORDS);
2357}
2358
2359VALUE
2360rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self)
2361{
2362 return specific_eval(argc, argv, self, TRUE, RB_NO_KEYWORDS);
2363}
2364
2365/*
2366 * call-seq:
2367 * obj.instance_exec(arg...) {|var...| block } -> obj
2368 *
2369 * Executes the given block within the context of the receiver
2370 * (_obj_). In order to set the context, the variable +self+ is set
2371 * to _obj_ while the code is executing, giving the code access to
2372 * _obj_'s instance variables. Arguments are passed as block parameters.
2373 *
2374 * class KlassWithSecret
2375 * def initialize
2376 * @secret = 99
2377 * end
2378 * end
2379 * k = KlassWithSecret.new
2380 * k.instance_exec(5) {|x| @secret+x } #=> 104
2381 */
2382
2383static VALUE
2384rb_obj_instance_exec_internal(int argc, const VALUE *argv, VALUE self)
2385{
2386 return yield_under(self, TRUE, argc, argv, RB_PASS_CALLED_KEYWORDS);
2387}
2388
2389VALUE
2390rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self)
2391{
2392 return yield_under(self, TRUE, argc, argv, RB_NO_KEYWORDS);
2393}
2394
2395/*
2396 * call-seq:
2397 * class_eval(string, filename = nil, lineno = 1) -> obj
2398 * class_eval { |mod| ... } -> obj
2399 * module_eval(string, filename = nil, lineno = 1) -> obj
2400 * module_eval { |mod| ... } -> obj
2401 *
2402 * Evaluates the +string+ or block in the context of +self+.
2403 * Returns the result of the last expression.
2404 *
2405 * When +string+ is given, evaluates the given string in the
2406 * context of +self+:
2407 *
2408 * class Foo; end
2409 *
2410 * Foo.module_eval("def greeting = puts 'hello'")
2411 *
2412 * Foo.new.greeting # => "hello"
2413 *
2414 * If the optional +filename+ is given, it will be used as the
2415 * filename of the evaluation (for <tt>__FILE__</tt> and errors).
2416 * Otherwise, it will default to <tt>(eval at __FILE__:__LINE__)</tt>
2417 * where <tt>__FILE__</tt> and <tt>__LINE__</tt> are the filename and
2418 * line number of the caller, respectively:
2419 *
2420 * class Foo; end
2421 *
2422 * Foo.module_eval("puts __FILE__") # => "(eval at ../test.rb:3)"
2423 * Foo.module_eval("puts __FILE__", "foobar.rb") # => "foobar.rb"
2424 *
2425 * If the optional +lineno+ is given, it will be used as the
2426 * line number of the evaluation (for <tt>__LINE__</tt> and errors).
2427 * Otherwise, it will default to 1:
2428 *
2429 * class Foo; end
2430 *
2431 * Foo.module_eval("puts __LINE__") # => 1
2432 * Foo.module_eval("puts __LINE__", nil, 10) # => 10
2433 *
2434 * When a block is given, evaluates the block in the context
2435 * of +self+:
2436 *
2437 * class Foo; end
2438 *
2439 * Foo.module_eval do
2440 * def greeting = puts "hello"
2441 * end
2442 *
2443 * Foo.new.greeting
2444 *
2445 * However, constant and class variable lookup differs between
2446 * +string+ and block. When +string+ is given, constant and class
2447 * variables are looked up in the context of +self+. When a block
2448 * is given, the context of the lookup is not changed:
2449 *
2450 * class Foo
2451 * GREETING = "hello"
2452 * end
2453 *
2454 * Foo.module_eval("puts GREETING") # => "hello"
2455 *
2456 * Foo.module_eval { puts GREETING } # => NameError: uninitialized constant GREETING
2457 */
2458
2459static VALUE
2460rb_mod_module_eval_internal(int argc, const VALUE *argv, VALUE mod)
2461{
2462 return specific_eval(argc, argv, mod, FALSE, RB_PASS_CALLED_KEYWORDS);
2463}
2464
2465VALUE
2466rb_mod_module_eval(int argc, const VALUE *argv, VALUE mod)
2467{
2468 return specific_eval(argc, argv, mod, FALSE, RB_NO_KEYWORDS);
2469}
2470
2471/*
2472 * call-seq:
2473 * mod.module_exec(arg...) {|var...| block } -> obj
2474 * mod.class_exec(arg...) {|var...| block } -> obj
2475 *
2476 * Evaluates the given block in the context of the class/module.
2477 * The method defined in the block will belong to the receiver.
2478 * Any arguments passed to the method will be passed to the block.
2479 * This can be used if the block needs to access instance variables.
2480 *
2481 * class Thing
2482 * end
2483 * Thing.class_exec{
2484 * def hello() "Hello there!" end
2485 * }
2486 * puts Thing.new.hello()
2487 *
2488 * <em>produces:</em>
2489 *
2490 * Hello there!
2491 */
2492
2493static VALUE
2494rb_mod_module_exec_internal(int argc, const VALUE *argv, VALUE mod)
2495{
2496 return yield_under(mod, FALSE, argc, argv, RB_PASS_CALLED_KEYWORDS);
2497}
2498
2499VALUE
2500rb_mod_module_exec(int argc, const VALUE *argv, VALUE mod)
2501{
2502 return yield_under(mod, FALSE, argc, argv, RB_NO_KEYWORDS);
2503}
2504
2505/*
2506 * Document-class: UncaughtThrowError
2507 *
2508 * Raised when +throw+ is called with a _tag_ which does not have
2509 * corresponding +catch+ block.
2510 *
2511 * throw "foo", "bar"
2512 *
2513 * <em>raises the exception:</em>
2514 *
2515 * UncaughtThrowError: uncaught throw "foo"
2516 */
2517
2518static VALUE
2519uncaught_throw_init(int argc, const VALUE *argv, VALUE exc)
2520{
2522 rb_call_super(argc - 2, argv + 2);
2523 rb_ivar_set(exc, id_tag, argv[0]);
2524 rb_ivar_set(exc, id_value, argv[1]);
2525 return exc;
2526}
2527
2528/*
2529 * call-seq:
2530 * uncaught_throw.tag -> obj
2531 *
2532 * Return the tag object which was called for.
2533 */
2534
2535static VALUE
2536uncaught_throw_tag(VALUE exc)
2537{
2538 return rb_ivar_get(exc, id_tag);
2539}
2540
2541/*
2542 * call-seq:
2543 * uncaught_throw.value -> obj
2544 *
2545 * Return the return value which was called for.
2546 */
2547
2548static VALUE
2549uncaught_throw_value(VALUE exc)
2550{
2551 return rb_ivar_get(exc, id_value);
2552}
2553
2554/*
2555 * call-seq:
2556 * uncaught_throw.to_s -> string
2557 *
2558 * Returns formatted message with the inspected tag.
2559 */
2560
2561static VALUE
2562uncaught_throw_to_s(VALUE exc)
2563{
2564 VALUE mesg = rb_attr_get(exc, id_mesg);
2565 VALUE tag = uncaught_throw_tag(exc);
2566 return rb_str_format(1, &tag, mesg);
2567}
2568
2569/*
2570 * call-seq:
2571 * throw(tag [, obj])
2572 *
2573 * Transfers control to the end of the active +catch+ block
2574 * waiting for _tag_. Raises +UncaughtThrowError+ if there
2575 * is no +catch+ block for the _tag_. The optional second
2576 * parameter supplies a return value for the +catch+ block,
2577 * which otherwise defaults to +nil+. For examples, see
2578 * Kernel::catch.
2579 */
2580
2581static VALUE
2582rb_f_throw(int argc, VALUE *argv, VALUE _)
2583{
2584 VALUE tag, value;
2585
2586 rb_scan_args(argc, argv, "11", &tag, &value);
2587 rb_throw_obj(tag, value);
2589}
2590
2591void
2593{
2594 rb_execution_context_t *ec = GET_EC();
2595 struct rb_vm_tag *tt = ec->tag;
2596
2597 while (tt) {
2598 if (tt->tag == tag) {
2599 tt->retval = value;
2600 break;
2601 }
2602 tt = tt->prev;
2603 }
2604 if (!tt) {
2605 VALUE desc[3];
2606 desc[0] = tag;
2607 desc[1] = value;
2608 desc[2] = rb_str_new_cstr("uncaught throw %p");
2609 rb_exc_raise(rb_class_new_instance(numberof(desc), desc, rb_eUncaughtThrow));
2610 }
2611
2612 ec->errinfo = (VALUE)THROW_DATA_NEW(tag, NULL, TAG_THROW);
2613 EC_JUMP_TAG(ec, TAG_THROW);
2614}
2615
2616void
2617rb_throw(const char *tag, VALUE val)
2618{
2619 rb_throw_obj(rb_sym_intern_ascii_cstr(tag), val);
2620}
2621
2622static VALUE
2623catch_i(RB_BLOCK_CALL_FUNC_ARGLIST(tag, _))
2624{
2625 return rb_yield_0(1, &tag);
2626}
2627
2628/*
2629 * call-seq:
2630 * catch([tag]) {|tag| block } -> obj
2631 *
2632 * +catch+ executes its block. If +throw+ is not called, the block executes
2633 * normally, and +catch+ returns the value of the last expression evaluated.
2634 *
2635 * catch(1) { 123 } # => 123
2636 *
2637 * If <code>throw(tag2, val)</code> is called, Ruby searches up its stack for
2638 * a +catch+ block whose +tag+ has the same +object_id+ as _tag2_. When found,
2639 * the block stops executing and returns _val_ (or +nil+ if no second argument
2640 * was given to +throw+).
2641 *
2642 * catch(1) { throw(1, 456) } # => 456
2643 * catch(1) { throw(1) } # => nil
2644 *
2645 * When +tag+ is passed as the first argument, +catch+ yields it as the
2646 * parameter of the block.
2647 *
2648 * catch(1) {|x| x + 2 } # => 3
2649 *
2650 * When no +tag+ is given, +catch+ yields a new unique object (as from
2651 * +Object.new+) as the block parameter. This object can then be used as the
2652 * argument to +throw+, and will match the correct +catch+ block.
2653 *
2654 * catch do |obj_A|
2655 * catch do |obj_B|
2656 * throw(obj_B, 123)
2657 * puts "This puts is not reached"
2658 * end
2659 *
2660 * puts "This puts is displayed"
2661 * 456
2662 * end
2663 *
2664 * # => 456
2665 *
2666 * catch do |obj_A|
2667 * catch do |obj_B|
2668 * throw(obj_A, 123)
2669 * puts "This puts is still not reached"
2670 * end
2671 *
2672 * puts "Now this puts is also not reached"
2673 * 456
2674 * end
2675 *
2676 * # => 123
2677 */
2678
2679static VALUE
2680rb_f_catch(int argc, VALUE *argv, VALUE self)
2681{
2682 VALUE tag = rb_check_arity(argc, 0, 1) ? argv[0] : rb_obj_alloc(rb_cObject);
2683 return rb_catch_obj(tag, catch_i, 0);
2684}
2685
2686VALUE
2687rb_catch(const char *tag, rb_block_call_func_t func, VALUE data)
2688{
2689 VALUE vtag = tag ? rb_sym_intern_ascii_cstr(tag) : rb_obj_alloc(rb_cObject);
2690 return rb_catch_obj(vtag, func, data);
2691}
2692
2693static VALUE
2694vm_catch_protect(VALUE tag, rb_block_call_func *func, VALUE data,
2695 enum ruby_tag_type *stateptr_arg, rb_execution_context_t *volatile ec)
2696{
2697 enum ruby_tag_type state;
2698 VALUE val = Qnil; /* OK */
2699 rb_control_frame_t *volatile saved_cfp = ec->cfp;
2700 enum ruby_tag_type * volatile stateptr = stateptr_arg;
2701
2702 EC_PUSH_TAG(ec);
2703
2704 _tag.tag = tag;
2705
2706 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2707 /* call with argc=1, argv = [tag], block = Qnil to insure compatibility */
2708 val = (*func)(tag, data, 1, (const VALUE *)&tag, Qnil);
2709 }
2710 else if (state == TAG_THROW && THROW_DATA_VAL((struct vm_throw_data *)ec->errinfo) == tag) {
2711 rb_vm_rewind_cfp(ec, saved_cfp);
2712 val = ec->tag->retval;
2713 ec->errinfo = Qnil;
2714 state = 0;
2715 }
2716 EC_POP_TAG();
2717 if (stateptr)
2718 *stateptr = state;
2719
2720 return val;
2721}
2722
2723VALUE
2724rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, enum ruby_tag_type *stateptr)
2725{
2726 return vm_catch_protect(t, func, data, stateptr, GET_EC());
2727}
2728
2729VALUE
2731{
2732 enum ruby_tag_type state;
2733 rb_execution_context_t *ec = GET_EC();
2734 VALUE val = vm_catch_protect(t, (rb_block_call_func *)func, data, &state, ec);
2735 if (state) EC_JUMP_TAG(ec, state);
2736 return val;
2737}
2738
2739static void
2740local_var_list_init(struct local_var_list *vars)
2741{
2742 vars->tbl = rb_ident_hash_new();
2743 RBASIC_CLEAR_CLASS(vars->tbl);
2744}
2745
2746static VALUE
2747local_var_list_finish(struct local_var_list *vars)
2748{
2749 /* TODO: not to depend on the order of st_table */
2750 VALUE ary = rb_hash_keys(vars->tbl);
2751 rb_hash_clear(vars->tbl);
2752 vars->tbl = 0;
2753 return ary;
2754}
2755
2756static int
2757local_var_list_update(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
2758{
2759 if (existing) return ST_STOP;
2760 *value = (st_data_t)Qtrue; /* INT2FIX(arg) */
2761 return ST_CONTINUE;
2762}
2763
2764extern int rb_numparam_id_p(ID id);
2765
2766static void
2767local_var_list_add(const struct local_var_list *vars, ID lid)
2768{
2769 /* should skip temporary variable */
2770 if (!lid) return;
2771 if (!rb_is_local_id(lid)) return;
2772
2773 /* should skip numbered parameters as well */
2774 if (rb_numparam_id_p(lid)) return;
2775
2776 st_data_t idx = 0; /* tbl->num_entries */
2777 rb_hash_stlike_update(vars->tbl, ID2SYM(lid), local_var_list_update, idx);
2778}
2779
2780static void
2781numparam_list_add(const struct local_var_list *vars, ID lid)
2782{
2783 /* should skip temporary variable */
2784 if (!lid) return;
2785 if (!rb_is_local_id(lid)) return;
2786
2787 /* should skip anything but numbered parameters */
2788 if (rb_numparam_id_p(lid)) {
2789 st_data_t idx = 0; /* tbl->num_entries */
2790 rb_hash_stlike_update(vars->tbl, ID2SYM(lid), local_var_list_update, idx);
2791 }
2792}
2793
2794/*
2795 * call-seq:
2796 * local_variables -> array
2797 *
2798 * Returns the names of the current local variables.
2799 *
2800 * fred = 1
2801 * for i in 1..10
2802 * # ...
2803 * end
2804 * local_variables #=> [:fred, :i]
2805 */
2806
2807static VALUE
2808rb_f_local_variables(VALUE _)
2809{
2810 struct local_var_list vars;
2811 rb_execution_context_t *ec = GET_EC();
2812 rb_control_frame_t *cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp));
2813 unsigned int i;
2814
2815 local_var_list_init(&vars);
2816 while (cfp) {
2817 if (CFP_ISEQ(cfp)) {
2818 for (i = 0; i < ISEQ_BODY(CFP_ISEQ(cfp))->local_table_size; i++) {
2819 local_var_list_add(&vars, ISEQ_BODY(CFP_ISEQ(cfp))->local_table[i]);
2820 }
2821 }
2822 if (!VM_ENV_LOCAL_P(cfp->ep)) {
2823 /* block */
2824 const VALUE *ep = VM_CF_PREV_EP(cfp);
2825
2826 if (vm_collect_local_variables_in_heap(ep, &vars)) {
2827 break;
2828 }
2829 else {
2830 while (cfp->ep != ep) {
2831 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2832 }
2833 }
2834 }
2835 else {
2836 break;
2837 }
2838 }
2839 return local_var_list_finish(&vars);
2840}
2841
2842/*
2843 * call-seq:
2844 * block_given? -> true or false
2845 *
2846 * Returns <code>true</code> if <code>yield</code> would execute a
2847 * block in the current context. The <code>iterator?</code> form
2848 * is mildly deprecated.
2849 *
2850 * def try
2851 * if block_given?
2852 * yield
2853 * else
2854 * "no block"
2855 * end
2856 * end
2857 * try #=> "no block"
2858 * try { "hello" } #=> "hello"
2859 * try do "hello" end #=> "hello"
2860 */
2861
2862static VALUE
2863rb_f_block_given_p(VALUE _)
2864{
2865 rb_execution_context_t *ec = GET_EC();
2866 rb_control_frame_t *cfp = ec->cfp;
2867 cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
2868
2869 return RBOOL(cfp != NULL && VM_CF_BLOCK_HANDLER(cfp) != VM_BLOCK_HANDLER_NONE);
2870}
2871
2872/*
2873 * call-seq:
2874 * iterator? -> true or false
2875 *
2876 * Deprecated. Use block_given? instead.
2877 */
2878
2879static VALUE
2880rb_f_iterator_p(VALUE self)
2881{
2882 rb_warn_deprecated("iterator?", "block_given?");
2883 return rb_f_block_given_p(self);
2884}
2885
2886VALUE
2887rb_current_realfilepath(void)
2888{
2889 const rb_execution_context_t *ec = GET_EC();
2890 rb_control_frame_t *cfp = ec->cfp;
2891 cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
2892 if (cfp != NULL) {
2893 const rb_iseq_t *iseq = CFP_ISEQ(cfp);
2894 VALUE path = rb_iseq_realpath(iseq);
2895 if (RTEST(path)) return path;
2896 // eval context
2897 path = rb_iseq_path(iseq);
2898 if (path == eval_default_path) {
2899 return Qnil;
2900 }
2901
2902 // [Feature #19755] implicit eval location is "(eval at #{__FILE__}:#{__LINE__})"
2903 const rb_len_t len = RSTRING_LEN(path);
2904 if (len > EVAL_LOCATION_MARK_LEN+1) {
2905 const char *const ptr = RSTRING_PTR(path);
2906 if (ptr[len - 1] == ')' &&
2907 memcmp(ptr, "("EVAL_LOCATION_MARK, EVAL_LOCATION_MARK_LEN+1) == 0) {
2908 return Qnil;
2909 }
2910 }
2911
2912 return path;
2913 }
2914 return Qnil;
2915}
2916
2917// Assert that an internal function is running and return
2918// the imemo object that represents it.
2919struct vm_ifunc *
2920rb_current_ifunc(void)
2921{
2922 // Search VM_FRAME_MAGIC_IFUNC to see ifunc imemos put on the iseq field.
2923 VALUE ifunc = (VALUE)CFP_ISEQ(GET_EC()->cfp);
2924 RUBY_ASSERT_ALWAYS(imemo_type_p(ifunc, imemo_ifunc));
2925 return (struct vm_ifunc *)ifunc;
2926}
2927
2928void
2929Init_vm_eval(void)
2930{
2931 rb_define_global_function("eval", rb_f_eval, -1);
2932 rb_define_global_function("local_variables", rb_f_local_variables, 0);
2933 rb_define_global_function("iterator?", rb_f_iterator_p, 0);
2934 rb_define_global_function("block_given?", rb_f_block_given_p, 0);
2935
2936 rb_define_global_function("catch", rb_f_catch, -1);
2937 rb_define_global_function("throw", rb_f_throw, -1);
2938
2939 rb_define_method(rb_cBasicObject, "instance_eval", rb_obj_instance_eval_internal, -1);
2940 rb_define_method(rb_cBasicObject, "instance_exec", rb_obj_instance_exec_internal, -1);
2941 rb_define_private_method(rb_cBasicObject, "method_missing", rb_method_missing, -1);
2942
2943#if 1
2944 rb_add_method(rb_cBasicObject, id__send__,
2945 VM_METHOD_TYPE_OPTIMIZED, (void *)OPTIMIZED_METHOD_TYPE_SEND, METHOD_VISI_PUBLIC);
2946 rb_add_method(rb_mKernel, idSend,
2947 VM_METHOD_TYPE_OPTIMIZED, (void *)OPTIMIZED_METHOD_TYPE_SEND, METHOD_VISI_PUBLIC);
2948#else
2949 rb_define_method(rb_cBasicObject, "__send__", rb_f_send, -1);
2950 rb_define_method(rb_mKernel, "send", rb_f_send, -1);
2951#endif
2952 rb_define_method(rb_mKernel, "public_send", rb_f_public_send, -1);
2953
2954 rb_define_method(rb_cModule, "module_exec", rb_mod_module_exec_internal, -1);
2955 rb_define_method(rb_cModule, "class_exec", rb_mod_module_exec_internal, -1);
2956 rb_define_method(rb_cModule, "module_eval", rb_mod_module_eval_internal, -1);
2957 rb_define_method(rb_cModule, "class_eval", rb_mod_module_eval_internal, -1);
2958
2959 rb_eUncaughtThrow = rb_define_class("UncaughtThrowError", rb_eArgError);
2960 rb_define_method(rb_eUncaughtThrow, "initialize", uncaught_throw_init, -1);
2961 rb_define_method(rb_eUncaughtThrow, "tag", uncaught_throw_tag, 0);
2962 rb_define_method(rb_eUncaughtThrow, "value", uncaught_throw_value, 0);
2963 rb_define_method(rb_eUncaughtThrow, "to_s", uncaught_throw_to_s, 0);
2964
2965 id_result = rb_intern_const("result");
2966 id_tag = rb_intern_const("tag");
2967 id_value = rb_intern_const("value");
2968}
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
Definition assert.h:199
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
Definition event.h:43
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
Definition event.h:44
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
Definition eval.c:1940
VALUE rb_module_new(void)
Creates a new, anonymous module.
Definition class.c:1662
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:3385
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
Definition eval.c:1048
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1035
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1700
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition value_type.h:59
#define T_FILE
Old name of RUBY_T_FILE.
Definition value_type.h:62
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define T_MASK
Old name of RUBY_T_MASK.
Definition value_type.h:68
#define Qundef
Old name of RUBY_Qundef.
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:131
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define T_NONE
Old name of RUBY_T_NONE.
Definition value_type.h:74
#define T_NODE
Old name of RUBY_T_NODE.
Definition value_type.h:73
#define rb_ary_new4
Old name of rb_ary_new_from_values.
Definition array.h:659
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_TRUE
Old name of RUBY_T_TRUE.
Definition value_type.h:81
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define T_FALSE
Old name of RUBY_T_FALSE.
Definition value_type.h:61
#define T_UNDEF
Old name of RUBY_T_UNDEF.
Definition value_type.h:82
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define T_ZOMBIE
Old name of RUBY_T_ZOMBIE.
Definition value_type.h:83
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
Definition memory.h:405
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define T_MATCH
Old name of RUBY_T_MATCH.
Definition value_type.h:69
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define T_MOVED
Old name of RUBY_T_MOVED.
Definition value_type.h:71
#define Check_TypedStruct(v, t)
Old name of rb_check_typeddata.
Definition rtypeddata.h:109
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:406
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
#define T_REGEXP
Old name of RUBY_T_REGEXP.
Definition value_type.h:77
VALUE rb_eNotImpError
NotImplementedError exception.
Definition error.c:1467
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:678
VALUE rb_eNameError
NameError exception.
Definition error.c:1462
VALUE rb_eNoMethodError
NoMethodError exception.
Definition error.c:1465
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1455
VALUE rb_mKernel
Kernel module.
Definition object.c:59
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:2268
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition object.c:2309
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:234
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:58
VALUE rb_cModule
Module class.
Definition object.c:61
VALUE rb_obj_clone(VALUE obj)
Produces a shallow copy of the given object.
Definition object.c:501
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition object.c:906
VALUE rb_eval_string_wrap(const char *str, int *state)
Identical to rb_eval_string_protect(), except it evaluates the given string under a module binding in...
Definition vm_eval.c:2131
VALUE rb_funcall_passing_block(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv_public(), except you can pass the passed block.
Definition vm_eval.c:1188
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1125
VALUE rb_funcallv_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_funcallv(), except you can specify how to handle the last element of the given array.
Definition vm_eval.c:1092
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
Definition vm_eval.c:1083
VALUE rb_funcall_with_block(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE procval)
Identical to rb_funcallv_public(), except you can pass a block.
Definition vm_eval.c:1202
VALUE rb_eval_string_protect(const char *str, int *state)
Identical to rb_eval_string(), except it avoids potential global escapes.
Definition vm_eval.c:2110
VALUE rb_call_super_kw(int argc, const VALUE *argv, int kw_splat)
Identical to rb_call_super(), except you can specify how to handle the last element of the given arra...
Definition vm_eval.c:357
VALUE rb_funcallv_public(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it only takes public methods into account.
Definition vm_eval.c:1176
VALUE rb_current_receiver(void)
This resembles ruby's self.
Definition vm_eval.c:371
VALUE rb_funcall_passing_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_funcallv_passing_block(), except you can specify how to handle the last element of th...
Definition vm_eval.c:1195
VALUE rb_funcall_with_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE procval, int kw_splat)
Identical to rb_funcallv_with_block(), except you can specify how to handle the last element of the g...
Definition vm_eval.c:1212
VALUE rb_eval_string(const char *str)
Evaluates the given string.
Definition vm_eval.c:2098
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
Definition vm_eval.c:365
VALUE rb_funcallv_public_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_funcallv_public(), except you can specify how to handle the last element of the given...
Definition vm_eval.c:1182
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_pop(VALUE ary)
Destructively deletes an element from the end of the passed array and returns what was deleted.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_subseq(VALUE ary, rb_len_t beg, rb_len_t len)
Obtains a part of the passed array.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, rb_len_t len)
Destructively appends multiple elements at the end of the array.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
int rb_is_local_id(ID id)
Classifies the given ID, then sees if it is a local variable.
Definition symbol.c:1265
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1539
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:1085
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:2139
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1639
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well... Let us hesitate from describing what a "basic definition" is.
Definition vm_method.c:3574
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition vm_eval.c:693
VALUE rb_check_funcall_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_check_funcall(), except you can specify how to handle the last element of the given a...
Definition vm_eval.c:687
VALUE rb_mod_module_eval(int argc, const VALUE *argv, VALUE mod)
Identical to rb_obj_instance_eval(), except it evaluates within the context of module.
Definition vm_eval.c:2466
VALUE rb_mod_module_exec(int argc, const VALUE *argv, VALUE mod)
Identical to rb_obj_instance_exec(), except it evaluates within the context of module.
Definition vm_eval.c:2500
VALUE rb_obj_instance_exec(int argc, const VALUE *argv, VALUE recv)
Executes the given block within the context of the receiver.
Definition vm_eval.c:2390
VALUE rb_eval_cmd_kw(VALUE cmd, VALUE arg, int kw_splat)
This API is practically a variant of rb_proc_call_kw() now.
Definition vm_eval.c:2163
VALUE rb_apply(VALUE recv, ID mid, VALUE args)
Identical to rb_funcallv(), except it takes Ruby's array instead of C's.
Definition vm_eval.c:1100
VALUE rb_obj_instance_eval(int argc, const VALUE *argv, VALUE recv)
Evaluates a string containing Ruby source code, or the given block, within the context of the receive...
Definition vm_eval.c:2360
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1289
int len
Length of the buffer.
Definition io.h:8
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Formats a string.
Definition sprintf.c:974
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
Definition iterator.h:58
VALUE rb_each(VALUE obj)
This is a shorthand of calling obj.each.
Definition vm_eval.c:1658
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
Definition vm_eval.c:1403
VALUE rb_yield_splat(VALUE ary)
Identical to rb_yield_values(), except it splats an array to generate the list of parameters.
Definition vm_eval.c:1437
void rb_throw(const char *tag, VALUE val)
Transfers control to the end of the active catch block waiting for tag.
Definition vm_eval.c:2617
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
Definition vm_eval.c:1425
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1380
VALUE rb_yield_values_kw(int n, const VALUE *argv, int kw_splat)
Identical to rb_yield_values2(), except you can specify how to handle the last element of the given a...
Definition vm_eval.c:1431
rb_block_call_func * rb_block_call_func_t
Shorthand type that represents an iterator-written-in-C function pointer.
Definition iterator.h:88
VALUE rb_yield_block(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
Pass a passed block.
void rb_throw_obj(VALUE tag, VALUE val)
Identical to rb_throw(), except it allows arbitrary Ruby object to become a tag.
Definition vm_eval.c:2592
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
This is the type of a function that the interpreter expect for C-backended blocks.
Definition iterator.h:83
VALUE rb_block_call_kw(VALUE obj, ID mid, int argc, const VALUE *argv, rb_block_call_func_t proc, VALUE data2, int kw_splat)
Identical to rb_funcallv_kw(), except it additionally passes a function as a block.
Definition vm_eval.c:1572
VALUE rb_yield_splat_kw(VALUE ary, int kw_splat)
Identical to rb_yield_splat(), except you can specify how to handle the last element of the given arr...
Definition vm_eval.c:1450
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define ALLOCA_N(type, n)
Definition memory.h:292
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
Definition memory.h:360
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
VALUE rb_catch_obj(VALUE q, type *w, VALUE e)
An equivalent of Kernel#catch.
VALUE rb_catch(const char *q, type *w, VALUE e)
An equivalent of Kernel#catch.
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
VALUE type(ANYARGS)
ANYARGS-ed function type.
VALUE rb_rescue2(type *q, VALUE w, type *e, VALUE r,...)
An equivalent of rescue clause.
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_NONE
The default value for parameters.
Definition options.h:45
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_ALL
When the scope is forwarding with the ... parameter.
Definition options.h:57
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS
When the scope is forwarding with the * parameter.
Definition options.h:48
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS
When the scope is forwarding with the ** parameter.
Definition options.h:51
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_BLOCK
When the scope is forwarding with the & parameter.
Definition options.h:54
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:280
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:51
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:166
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:67
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition rstring.h:76
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
Definition scan_args.h:78
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
pm_parser_t * parser
The parser that will do the actual parsing.
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_options_t * options
The options that will be passed to the parser.
pm_arena_t * arena
The arena allocator for AST-lifetime memory.
pm_index_lookup_table_t index_lookup_table
A flat lookup table mapping constant IDs (or special IDs) to local variable indices.
A generic string type that can have various ownership semantics.
Definition stringy.h:18
Internal header for Ruby Box.
Definition box.h:14
Definition method.h:63
CREF (Class REFerence)
Definition method.h:45
IFUNC (Internal FUNCtion)
Definition imemo.h:89
THROW_DATA.
Definition imemo.h:63
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:131
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
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:425
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376
ruby_value_type
C-level type of an object.
Definition value_type.h:113