Ruby 4.1.0dev (2026-10-29 revision 952e77c511a0d183f43464cba30c55834eac643e)
vm_eval.c (952e77c511a0d183f43464cba30c55834eac643e)
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(cd.cc == &rb_vm_uncached_cc)) {
424 // Carries no cme, so it would only ever miss here. Don't evict a usable entry for it.
425 return cd.cc;
426 }
427
428 if (UNLIKELY(!vm->global_cc_cache_table_used)) {
429 vm->global_cc_cache_table_used = true;
430 }
431 return vm->global_cc_cache_table[index] = cd.cc;
432}
433
434static void
435scope_to_ci(call_type scope, ID mid, int argc, struct rb_callinfo *ci)
436{
437 int flags = 0;
438
439 switch(scope) {
440 case CALL_PUBLIC:
441 break;
442 case CALL_FCALL:
443 flags |= VM_CALL_FCALL;
444 break;
445 case CALL_VCALL:
446 flags |= VM_CALL_VCALL;
447 break;
448 case CALL_PUBLIC_KW:
449 flags |= VM_CALL_KWARG;
450 break;
451 case CALL_FCALL_KW:
452 flags |= (VM_CALL_KWARG | VM_CALL_FCALL);
453 break;
454 }
455 *ci = VM_CI_ON_STACK(mid, flags, argc, NULL);
456}
457
458static inline const struct rb_callcache *
459gccct_method_search(rb_execution_context_t *ec, VALUE recv, ID mid, const struct rb_callinfo *ci)
460{
461 VALUE klass, box_value;
462 const rb_box_t *box = rb_current_box();
463
464 if (!SPECIAL_CONST_P(recv)) {
465 klass = RBASIC_CLASS(recv);
466 if (UNLIKELY(!klass)) uncallable_object(recv, mid);
467 }
468 else {
469 klass = CLASS_OF(recv);
470 }
471
472 if (BOX_USER_P(box)) {
473 box_value = box->box_object;
474 }
475 else {
476 box_value = 0;
477 }
478 // search global method cache
479 unsigned int index = (unsigned int)(gccct_hash(klass, box_value, mid) % VM_GLOBAL_CC_CACHE_TABLE_SIZE);
480 rb_vm_t *vm = rb_ec_vm_ptr(ec);
481 const struct rb_callcache *cc = vm->global_cc_cache_table[index];
482
483 if (LIKELY(cc)) {
484 if (LIKELY(vm_cc_class_check(cc, klass))) {
485 const rb_callable_method_entry_t *cme = vm_cc_cme(cc);
486 if (LIKELY(!METHOD_ENTRY_INVALIDATED(cme) &&
487 cme->called_id == mid)) {
488
489 VM_ASSERT(vm_cc_check_cme(cc, rb_callable_method_entry(klass, mid)));
490 RB_DEBUG_COUNTER_INC(gccct_hit);
491
492 return cc;
493 }
494 }
495 }
496 else {
497 RB_DEBUG_COUNTER_INC(gccct_null);
498 }
499
500 RB_DEBUG_COUNTER_INC(gccct_miss);
501 return gccct_method_search_slowpath(vm, klass, index, ci);
502}
503
504VALUE
505rb_gccct_clear_table(void)
506{
507 rb_vm_t *vm = GET_VM();
508 if (vm->global_cc_cache_table_used) {
509 const struct rb_callcache **const table = vm->global_cc_cache_table;
510 MEMZERO(table, struct rb_callcache *, VM_GLOBAL_CC_CACHE_TABLE_SIZE);
511 vm->global_cc_cache_table_used = false;
512 }
513 return Qnil;
514}
515
532static VALUE
533vm_call0_uncached(rb_execution_context_t *ec, VALUE recv, ID mid, int argc, const VALUE *argv,
534 int kw_splat, call_type scope, VALUE self)
535{
536 const rb_callable_method_entry_t *cme = rb_callable_method_entry(CLASS_OF(recv), mid);
537 enum method_missing_reason call_status = rb_method_call_status(ec, cme, scope, self);
538
539 if (UNLIKELY(call_status != MISSING_NONE)) {
540 return method_missing(ec, recv, mid, argc, argv, call_status, kw_splat);
541 }
542
543 stack_check(ec);
544 return rb_vm_call0(ec, recv, mid, argc, argv, cme, kw_splat);
545}
546
547static inline VALUE
548rb_call0(rb_execution_context_t *ec,
549 VALUE recv, ID mid, int argc, const VALUE *argv,
550 call_type call_scope, VALUE self)
551{
552 enum method_missing_reason call_status;
553 call_type scope = call_scope;
554 int kw_splat = RB_NO_KEYWORDS;
555
556 switch (scope) {
557 case CALL_PUBLIC_KW:
558 scope = CALL_PUBLIC;
559 kw_splat = 1;
560 break;
561 case CALL_FCALL_KW:
562 scope = CALL_FCALL;
563 kw_splat = 1;
564 break;
565 default:
566 break;
567 }
568
569 struct rb_callinfo ci;
570 scope_to_ci(scope, mid, argc, &ci);
571
572 const struct rb_callcache *cc = gccct_method_search(ec, recv, mid, &ci);
573
574 if (UNLIKELY(cc == &rb_vm_uncached_cc)) {
575 return vm_call0_uncached(ec, recv, mid, argc, argv, kw_splat, scope, self);
576 }
577
578 if (scope == CALL_PUBLIC) {
579 RB_DEBUG_COUNTER_INC(call0_public);
580
581 const rb_callable_method_entry_t *cc_cme = cc ? vm_cc_cme(cc) : NULL;
582 const rb_callable_method_entry_t *cme = callable_method_entry_refinements0(CLASS_OF(recv), mid, NULL, true, cc_cme);
583 call_status = rb_method_call_status(ec, cme, scope, self);
584
585 if (UNLIKELY(call_status != MISSING_NONE)) {
586 return method_missing(ec, recv, mid, argc, argv, call_status, kw_splat);
587 }
588 else if (UNLIKELY(cc_cme != cme)) { // refinement is solved
589 stack_check(ec);
590 return rb_vm_call_kw(ec, recv, mid, argc, argv, cme, kw_splat);
591 }
592 }
593 else {
594 RB_DEBUG_COUNTER_INC(call0_other);
595 call_status = rb_method_call_status(ec, cc ? vm_cc_cme(cc) : NULL, scope, self);
596
597 if (UNLIKELY(call_status != MISSING_NONE)) {
598 return method_missing(ec, recv, mid, argc, argv, call_status, kw_splat);
599 }
600 }
601
602 stack_check(ec);
603 return vm_call0_cc(ec, recv, mid, argc, argv, cc, kw_splat);
604}
605
607 VALUE defined_class;
608 VALUE recv;
609 ID mid;
612 unsigned int respond: 1;
613 unsigned int respond_to_missing: 1;
614 int argc;
615 const VALUE *argv;
616 int kw_splat;
617};
618
619static VALUE
620check_funcall_exec(VALUE v)
621{
622 struct rescue_funcall_args *args = (void *)v;
623 return rb_vm_call_kw(args->ec, args->recv, idMethodMissing,
624 args->argc, args->argv, args->cme, args->kw_splat);
625}
626
627static VALUE
628check_funcall_failed(VALUE v, VALUE e)
629{
630 struct rescue_funcall_args *args = (void *)v;
631 int ret = args->respond;
632 if (!ret) {
633 switch (method_boundp(args->defined_class, args->mid,
634 BOUND_PRIVATE|BOUND_RESPONDS)) {
635 case 2:
636 ret = TRUE;
637 break;
638 case 0:
639 ret = args->respond_to_missing;
640 break;
641 default:
642 ret = FALSE;
643 break;
644 }
645 }
646 if (ret) {
647 rb_exc_raise(e);
648 }
649 return Qundef;
650}
651
652static int
653check_funcall_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE recv, ID mid)
654{
655 return vm_respond_to(ec, klass, recv, mid, TRUE);
656}
657
658static int
659check_funcall_callable(rb_execution_context_t *ec, const rb_callable_method_entry_t *me)
660{
661 return rb_method_call_status(ec, me, CALL_FCALL, ec->cfp->self) == MISSING_NONE;
662}
663
664static VALUE
665check_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)
666{
667 struct rescue_funcall_args args;
669 VALUE ret = Qundef;
670
671 ret = basic_obj_respond_to_missing(ec, klass, recv,
672 ID2SYM(mid), Qtrue);
673 if (!RTEST(ret)) return def;
674 args.respond = respond > 0;
675 args.respond_to_missing = !UNDEF_P(ret);
676 ret = def;
677 cme = callable_method_entry(klass, idMethodMissing, &args.defined_class);
678
679 if (cme && !METHOD_ENTRY_BASIC(cme)) {
680 VALUE argbuf, *new_args = ALLOCV_N(VALUE, argbuf, argc+1);
681
682 new_args[0] = ID2SYM(mid);
683 #ifdef __GLIBC__
684 if (!argv) {
685 static const VALUE buf = Qfalse;
686 VM_ASSERT(argc == 0);
687 argv = &buf;
688 }
689 #endif
690 MEMCPY(new_args+1, argv, VALUE, argc);
691 ec->method_missing_reason = MISSING_NOENTRY;
692 args.ec = ec;
693 args.recv = recv;
694 args.cme = cme;
695 args.mid = mid;
696 args.argc = argc + 1;
697 args.argv = new_args;
698 args.kw_splat = kw_splat;
699 ret = rb_rescue2(check_funcall_exec, (VALUE)&args,
700 check_funcall_failed, (VALUE)&args,
702 ALLOCV_END(argbuf);
703 }
704 return ret;
705}
706
707static VALUE rb_check_funcall_default_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def, int kw_splat);
708
709VALUE
710rb_check_funcall_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
711{
712 return rb_check_funcall_default_kw(recv, mid, argc, argv, Qundef, kw_splat);
713}
714
715VALUE
716rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
717{
718 return rb_check_funcall_default_kw(recv, mid, argc, argv, Qundef, RB_NO_KEYWORDS);
719}
720
721static VALUE
722rb_check_funcall_default_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def, int kw_splat)
723{
724 VM_ASSERT(ruby_thread_has_gvl_p());
725
726 VALUE klass = CLASS_OF(recv);
728 rb_execution_context_t *ec = GET_EC();
729 int respond = check_funcall_respond_to(ec, klass, recv, mid);
730
731 if (!respond)
732 return def;
733
734 me = rb_search_method_entry(recv, mid);
735 if (!check_funcall_callable(ec, me)) {
736 VALUE ret = check_funcall_missing(ec, klass, recv, mid, argc, argv,
737 respond, def, kw_splat);
738 if (UNDEF_P(ret)) ret = def;
739 return ret;
740 }
741 stack_check(ec);
742 return rb_vm_call_kw(ec, recv, mid, argc, argv, me, kw_splat);
743}
744
745VALUE
746rb_check_funcall_default(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def)
747{
748 return rb_check_funcall_default_kw(recv, mid, argc, argv, def, RB_NO_KEYWORDS);
749}
750
751VALUE
752rb_check_funcall_with_hook_kw(VALUE recv, ID mid, int argc, const VALUE *argv,
753 rb_check_funcall_hook *hook, VALUE arg, int kw_splat)
754{
755 VALUE klass = CLASS_OF(recv);
757 rb_execution_context_t *ec = GET_EC();
758 int respond = check_funcall_respond_to(ec, klass, recv, mid);
759
760 if (!respond) {
761 (*hook)(FALSE, recv, mid, argc, argv, arg);
762 return Qundef;
763 }
764
765 me = rb_search_method_entry(recv, mid);
766 if (!check_funcall_callable(ec, me)) {
767 VALUE ret = check_funcall_missing(ec, klass, recv, mid, argc, argv,
768 respond, Qundef, kw_splat);
769 (*hook)(!UNDEF_P(ret), recv, mid, argc, argv, arg);
770 return ret;
771 }
772 stack_check(ec);
773 (*hook)(TRUE, recv, mid, argc, argv, arg);
774 return rb_vm_call_kw(ec, recv, mid, argc, argv, me, kw_splat);
775}
776
777const char *
778rb_type_str(enum ruby_value_type type)
779{
780#define type_case(t) t: return #t
781 switch (type) {
782 case type_case(T_NONE);
783 case type_case(T_OBJECT);
784 case type_case(T_CLASS);
785 case type_case(T_MODULE);
786 case type_case(T_FLOAT);
787 case type_case(T_STRING);
788 case type_case(T_REGEXP);
789 case type_case(T_ARRAY);
790 case type_case(T_HASH);
791 case type_case(T_STRUCT);
792 case type_case(T_BIGNUM);
793 case type_case(T_FILE);
794 case type_case(T_DATA);
795 case type_case(T_MATCH);
796 case type_case(T_COMPLEX);
797 case type_case(T_RATIONAL);
798 case type_case(T_NIL);
799 case type_case(T_TRUE);
800 case type_case(T_FALSE);
801 case type_case(T_SYMBOL);
802 case type_case(T_FIXNUM);
803 case type_case(T_IMEMO);
804 case type_case(T_UNDEF);
805 case type_case(T_NODE);
806 case type_case(T_ICLASS);
807 case type_case(T_ZOMBIE);
808 case type_case(T_MOVED);
809 case T_MASK: break;
810 }
811#undef type_case
812 return NULL;
813}
814
815static void
816uncallable_object(VALUE recv, ID mid)
817{
818 VALUE flags;
819 int type;
820 const char *typestr;
821 VALUE mname = rb_id2str(mid);
822
823 if (SPECIAL_CONST_P(recv)) {
824 rb_raise(rb_eNotImpError,
825 "method '%"PRIsVALUE"' called on unexpected immediate object (%p)",
826 mname, (void *)recv);
827 }
828 else if ((flags = RBASIC(recv)->flags) == 0) {
829 rb_raise(rb_eNotImpError,
830 "method '%"PRIsVALUE"' called on terminated object (%p)",
831 mname, (void *)recv);
832 }
833 else if (!(typestr = rb_type_str(type = BUILTIN_TYPE(recv)))) {
834 rb_raise(rb_eNotImpError,
835 "method '%"PRIsVALUE"' called on broken T_?""?""?(0x%02x) object"
836 " (%p flags=0x%"PRIxVALUE")",
837 mname, type, (void *)recv, flags);
838 }
839 else if (T_OBJECT <= type && type < T_NIL) {
840 rb_raise(rb_eNotImpError,
841 "method '%"PRIsVALUE"' called on hidden %s object"
842 " (%p flags=0x%"PRIxVALUE")",
843 mname, typestr, (void *)recv, flags);
844 }
845 else {
846 rb_raise(rb_eNotImpError,
847 "method '%"PRIsVALUE"' called on unexpected %s object"
848 " (%p flags=0x%"PRIxVALUE")",
849 mname, typestr, (void *)recv, flags);
850 }
851}
852
853static inline const rb_callable_method_entry_t *
854rb_search_method_entry(VALUE recv, ID mid)
855{
856 VALUE klass = CLASS_OF(recv);
857
858 if (!klass) uncallable_object(recv, mid);
859 return rb_callable_method_entry(klass, mid);
860}
861
862static inline enum method_missing_reason
863rb_method_call_status(rb_execution_context_t *ec, const rb_callable_method_entry_t *me, call_type scope, VALUE self)
864{
865 if (UNLIKELY(UNDEFINED_METHOD_ENTRY_P(me))) {
866 goto undefined;
867 }
868 else if (UNLIKELY(me->def->type == VM_METHOD_TYPE_REFINED)) {
869 me = rb_resolve_refined_method_callable(Qnil, me);
870 if (UNDEFINED_METHOD_ENTRY_P(me)) goto undefined;
871 }
872
873 rb_method_visibility_t visi = METHOD_ENTRY_VISI(me);
874
875 /* receiver specified form for private method */
876 if (UNLIKELY(visi != METHOD_VISI_PUBLIC)) {
877 if (me->def->original_id == idMethodMissing) {
878 return MISSING_NONE;
879 }
880 else if (visi == METHOD_VISI_PRIVATE &&
881 scope == CALL_PUBLIC) {
882 return MISSING_PRIVATE;
883 }
884 /* self must be kind of a specified form for protected method */
885 else if (visi == METHOD_VISI_PROTECTED &&
886 scope == CALL_PUBLIC) {
887
888 VALUE defined_class = me->owner;
889 if (RB_TYPE_P(defined_class, T_ICLASS)) {
890 defined_class = RBASIC(defined_class)->klass;
891 }
892
893 if (UNDEF_P(self) || !rb_obj_is_kind_of(self, defined_class)) {
894 return MISSING_PROTECTED;
895 }
896 }
897 }
898
899 return MISSING_NONE;
900
901 undefined:
902 return scope == CALL_VCALL ? MISSING_VCALL : MISSING_NOENTRY;
903}
904
905
917static inline VALUE
918rb_call(VALUE recv, ID mid, int argc, const VALUE *argv, call_type scope)
919{
920 rb_execution_context_t *ec = GET_EC();
921 return rb_call0(ec, recv, mid, argc, argv, scope, ec->cfp->self);
922}
923
924NORETURN(static void raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv,
925 VALUE obj, enum method_missing_reason call_status));
926
927/*
928 * call-seq:
929 * obj.method_missing(symbol [, *args] ) -> result
930 *
931 * Invoked by Ruby when <i>obj</i> is sent a message it cannot handle.
932 * <i>symbol</i> is the symbol for the method called, and <i>args</i>
933 * are any arguments that were passed to it. By default, the interpreter
934 * raises an error when this method is called. However, it is possible
935 * to override the method to provide more dynamic behavior.
936 * If it is decided that a particular method should not be handled, then
937 * <i>super</i> should be called, so that ancestors can pick up the
938 * missing method.
939 * The example below creates
940 * a class <code>Roman</code>, which responds to methods with names
941 * consisting of roman numerals, returning the corresponding integer
942 * values.
943 *
944 * class Roman
945 * def roman_to_int(str)
946 * # ...
947 * end
948 *
949 * def method_missing(symbol, *args)
950 * str = symbol.id2name
951 * begin
952 * roman_to_int(str)
953 * rescue
954 * super(symbol, *args)
955 * end
956 * end
957 * end
958 *
959 * r = Roman.new
960 * r.iv #=> 4
961 * r.xxiii #=> 23
962 * r.mm #=> 2000
963 * r.foo #=> NoMethodError
964 */
965
966static VALUE
967rb_method_missing(int argc, const VALUE *argv, VALUE obj)
968{
969 rb_execution_context_t *ec = GET_EC();
970 raise_method_missing(ec, argc, argv, obj, ec->method_missing_reason);
972}
973
974VALUE
975rb_make_no_method_exception(VALUE exc, VALUE format, VALUE obj,
976 int argc, const VALUE *argv, int priv)
977{
978 VALUE name = argv[0];
979
980 if (!format) {
981 format = rb_fstring_lit("undefined method '%1$s' for %3$s%4$s");
982 }
983 if (exc == rb_eNoMethodError) {
984 VALUE args = rb_ary_new4(argc - 1, argv + 1);
985 return rb_nomethod_err_new(format, obj, name, args, priv);
986 }
987 else {
988 return rb_name_err_new(format, obj, name);
989 }
990}
991
992static void
993raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE obj,
994 enum method_missing_reason last_call_status)
995{
997 VALUE format = 0;
998
999 if (UNLIKELY(argc == 0)) {
1000 rb_raise(rb_eArgError, "no method name given");
1001 }
1002 else if (UNLIKELY(!SYMBOL_P(argv[0]))) {
1003 const VALUE e = rb_eArgError; /* TODO: TypeError? */
1004 rb_raise(e, "method name must be a Symbol but %"PRIsVALUE" is given",
1005 rb_obj_class(argv[0]));
1006 }
1007
1008 stack_check(ec);
1009
1010 if (last_call_status & MISSING_PRIVATE) {
1011 format = rb_fstring_lit("private method '%1$s' called for %3$s%4$s");
1012 }
1013 else if (last_call_status & MISSING_PROTECTED) {
1014 format = rb_fstring_lit("protected method '%1$s' called for %3$s%4$s");
1015 }
1016 else if (last_call_status & MISSING_VCALL) {
1017 format = rb_fstring_lit("undefined local variable or method '%1$s' for %3$s%4$s");
1018 exc = rb_eNameError;
1019 }
1020 else if (last_call_status & MISSING_SUPER) {
1021 format = rb_fstring_lit("super: no superclass method '%1$s' for %3$s%4$s");
1022 }
1023
1024 {
1025 exc = rb_make_no_method_exception(exc, format, obj, argc, argv,
1026 last_call_status & (MISSING_FCALL|MISSING_VCALL));
1027 if (!(last_call_status & MISSING_MISSING)) {
1028 rb_vm_pop_cfunc_frame();
1029 }
1030 rb_exc_raise(exc);
1031 }
1032}
1033
1034static void
1035vm_raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv,
1036 VALUE obj, int call_status)
1037{
1038 vm_passed_block_handler_set(ec, VM_BLOCK_HANDLER_NONE);
1039 raise_method_missing(ec, argc, argv, obj, call_status | MISSING_MISSING);
1040}
1041
1042static inline VALUE
1043method_missing(rb_execution_context_t *ec, VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status, int kw_splat)
1044{
1045 VALUE *nargv, result, work, klass;
1046 VALUE block_handler = vm_passed_block_handler(ec);
1048
1049 ec->method_missing_reason = call_status;
1050
1051 if (id == idMethodMissing) {
1052 goto missing;
1053 }
1054
1055 nargv = ALLOCV_N(VALUE, work, argc + 1);
1056 nargv[0] = ID2SYM(id);
1057 #ifdef __GLIBC__
1058 if (!argv) {
1059 static const VALUE buf = Qfalse;
1060 VM_ASSERT(argc == 0);
1061 argv = &buf;
1062 }
1063 #endif
1064 MEMCPY(nargv + 1, argv, VALUE, argc);
1065 ++argc;
1066 argv = nargv;
1067
1068 klass = CLASS_OF(obj);
1069 if (!klass) goto missing;
1070 me = rb_callable_method_entry(klass, idMethodMissing);
1071 if (!me || METHOD_ENTRY_BASIC(me)) goto missing;
1072 vm_passed_block_handler_set(ec, block_handler);
1073 result = rb_vm_call_kw(ec, obj, idMethodMissing, argc, argv, me, kw_splat);
1074 if (work) ALLOCV_END(work);
1075 return result;
1076 missing:
1077 raise_method_missing(ec, argc, argv, obj, call_status | MISSING_MISSING);
1079}
1080
1081static inline VALUE
1082rb_funcallv_scope(VALUE recv, ID mid, int argc, const VALUE *argv, call_type scope)
1083{
1084 rb_execution_context_t *ec = GET_EC();
1085
1086 struct rb_callinfo ci;
1087 scope_to_ci(scope, mid, argc, &ci);
1088
1089 const struct rb_callcache *cc = gccct_method_search(ec, recv, mid, &ci);
1090 VALUE self = ec->cfp->self;
1091
1092 if (LIKELY(cc) &&
1093 LIKELY(rb_method_call_status(ec, vm_cc_cme(cc), scope, self) == MISSING_NONE)) {
1094 // fastpath
1095 return vm_call0_cc(ec, recv, mid, argc, argv, cc, false);
1096 }
1097 else {
1098 return rb_call0(ec, recv, mid, argc, argv, scope, self);
1099 }
1100}
1101
1102#ifdef rb_funcallv
1103#undef rb_funcallv
1104#endif
1105VALUE
1106rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
1107{
1108 VM_ASSERT(ruby_thread_has_gvl_p());
1109 VM_ASSERT(!RB_TYPE_P(recv, T_IMEMO));
1110
1111 return rb_funcallv_scope(recv, mid, argc, argv, CALL_FCALL);
1112}
1113
1114VALUE
1115rb_funcallv_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
1116{
1117 VM_ASSERT(ruby_thread_has_gvl_p());
1118
1119 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_FCALL_KW : CALL_FCALL);
1120}
1121
1122VALUE
1123rb_apply(VALUE recv, ID mid, VALUE args)
1124{
1125 int argc;
1126 VALUE *argv, ret;
1127
1128 argc = RARRAY_LENINT(args);
1129 if (argc >= 0x100) {
1130 args = rb_ary_subseq(args, 0, argc);
1131 RBASIC_CLEAR_CLASS(args);
1132 OBJ_FREEZE(args);
1133 ret = rb_call(recv, mid, argc, RARRAY_CONST_PTR(args), CALL_FCALL);
1134 RB_GC_GUARD(args);
1135 return ret;
1136 }
1137 argv = ALLOCA_N(VALUE, argc);
1138 MEMCPY(argv, RARRAY_CONST_PTR(args), VALUE, argc);
1139
1140 return rb_funcallv(recv, mid, argc, argv);
1141}
1142
1143#ifdef rb_funcall
1144#undef rb_funcall
1145#endif
1146
1147VALUE
1148rb_funcall(VALUE recv, ID mid, int n, ...)
1149{
1150 VALUE *argv;
1151 va_list ar;
1152
1153 if (n > 0) {
1154 long i;
1155
1156 va_start(ar, n);
1157
1158 argv = ALLOCA_N(VALUE, n);
1159
1160 for (i = 0; i < n; i++) {
1161 argv[i] = va_arg(ar, VALUE);
1162 }
1163 va_end(ar);
1164 }
1165 else {
1166 argv = 0;
1167 }
1168 return rb_funcallv(recv, mid, n, argv);
1169}
1170
1181VALUE
1182rb_check_funcall_basic_kw(VALUE recv, ID mid, VALUE ancestor, int argc, const VALUE *argv, int kw_splat)
1183{
1184 const rb_callable_method_entry_t *cme;
1186 VALUE klass = CLASS_OF(recv);
1187 if (!klass) return Qundef; /* hidden object */
1188
1189 cme = rb_callable_method_entry(klass, mid);
1190 if (cme && METHOD_ENTRY_BASIC(cme) && RBASIC_CLASS(cme->defined_class) == ancestor) {
1191 ec = GET_EC();
1192 return rb_vm_call0(ec, recv, mid, argc, argv, cme, kw_splat);
1193 }
1194
1195 return Qundef;
1196}
1197
1198VALUE
1199rb_funcallv_public(VALUE recv, ID mid, int argc, const VALUE *argv)
1200{
1201 return rb_funcallv_scope(recv, mid, argc, argv, CALL_PUBLIC);
1202}
1203
1204VALUE
1205rb_funcallv_public_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
1206{
1207 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_PUBLIC_KW : CALL_PUBLIC);
1208}
1209
1210VALUE
1211rb_funcall_passing_block(VALUE recv, ID mid, int argc, const VALUE *argv)
1212{
1213 PASS_PASSED_BLOCK_HANDLER();
1214 return rb_funcallv_public(recv, mid, argc, argv);
1215}
1216
1217VALUE
1218rb_funcall_passing_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
1219{
1220 PASS_PASSED_BLOCK_HANDLER();
1221 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_PUBLIC_KW : CALL_PUBLIC);
1222}
1223
1224VALUE
1225rb_funcall_with_block(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE passed_procval)
1226{
1227 if (!NIL_P(passed_procval)) {
1228 vm_passed_block_handler_set(GET_EC(), passed_procval);
1229 }
1230
1231 return rb_funcallv_public(recv, mid, argc, argv);
1232}
1233
1234VALUE
1235rb_funcall_with_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE passed_procval, int kw_splat)
1236{
1237 if (!NIL_P(passed_procval)) {
1238 vm_passed_block_handler_set(GET_EC(), passed_procval);
1239 }
1240
1241 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_PUBLIC_KW : CALL_PUBLIC);
1242}
1243
1244static VALUE *
1245current_vm_stack_arg(const rb_execution_context_t *ec, const VALUE *argv)
1246{
1247 rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
1248 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, prev_cfp)) return NULL;
1249 if (prev_cfp->sp + 1 != argv) return NULL;
1250 return prev_cfp->sp + 1;
1251}
1252
1253static VALUE
1254send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope)
1255{
1256 ID id;
1257 VALUE vid;
1258 VALUE self;
1259 VALUE ret, vargv = 0;
1260 rb_execution_context_t *ec = GET_EC();
1261 int public = scope == CALL_PUBLIC || scope == CALL_PUBLIC_KW;
1262
1263 if (public) {
1264 self = Qundef;
1265 }
1266 else {
1267 self = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp)->self;
1268 }
1269
1270 if (argc == 0) {
1271 rb_raise(rb_eArgError, "no method name given");
1272 }
1273
1274 vid = *argv;
1275
1276 id = rb_check_id(&vid);
1277 if (!id) {
1278 if (rb_method_basic_definition_p(CLASS_OF(recv), idMethodMissing)) {
1279 VALUE exc = rb_make_no_method_exception(rb_eNoMethodError, 0,
1280 recv, argc, argv,
1281 !public);
1282 rb_exc_raise(exc);
1283 }
1284 if (!SYMBOL_P(*argv)) {
1285 VALUE *tmp_argv = current_vm_stack_arg(ec, argv);
1286 vid = rb_str_intern(vid);
1287 if (tmp_argv) {
1288 tmp_argv[0] = vid;
1289 }
1290 else if (argc > 1) {
1291 tmp_argv = ALLOCV_N(VALUE, vargv, argc);
1292 tmp_argv[0] = vid;
1293 MEMCPY(tmp_argv+1, argv+1, VALUE, argc-1);
1294 argv = tmp_argv;
1295 }
1296 else {
1297 argv = &vid;
1298 }
1299 }
1300 id = idMethodMissing;
1301 ec->method_missing_reason = MISSING_NOENTRY;
1302 }
1303 else {
1304 argv++; argc--;
1305 }
1306 PASS_PASSED_BLOCK_HANDLER_EC(ec);
1307 ret = rb_call0(ec, recv, id, argc, argv, scope, self);
1308 ALLOCV_END(vargv);
1309 return ret;
1310}
1311
1312static VALUE
1313send_internal_kw(int argc, const VALUE *argv, VALUE recv, call_type scope)
1314{
1315 if (rb_keyword_given_p()) {
1316 switch (scope) {
1317 case CALL_PUBLIC:
1318 scope = CALL_PUBLIC_KW;
1319 break;
1320 case CALL_FCALL:
1321 scope = CALL_FCALL_KW;
1322 break;
1323 default:
1324 break;
1325 }
1326 }
1327 return send_internal(argc, argv, recv, scope);
1328}
1329
1330/*
1331 * call-seq:
1332 * foo.send(symbol [, args...]) -> obj
1333 * foo.__send__(symbol [, args...]) -> obj
1334 * foo.send(string [, args...]) -> obj
1335 * foo.__send__(string [, args...]) -> obj
1336 *
1337 * Invokes the method identified by _symbol_, passing it any
1338 * arguments specified.
1339 * When the method is identified by a string, the string is converted
1340 * to a symbol.
1341 *
1342 * BasicObject implements +__send__+, Kernel implements +send+.
1343 * <code>__send__</code> is safer than +send+
1344 * when _obj_ has the same method name like <code>Socket</code>.
1345 * See also <code>public_send</code>.
1346 *
1347 * class Klass
1348 * def hello(*args)
1349 * "Hello " + args.join(' ')
1350 * end
1351 * end
1352 * k = Klass.new
1353 * k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
1354 */
1355
1356VALUE
1357rb_f_send(int argc, VALUE *argv, VALUE recv)
1358{
1359 return send_internal_kw(argc, argv, recv, CALL_FCALL);
1360}
1361
1362/*
1363 * call-seq:
1364 * obj.public_send(symbol [, args...]) -> obj
1365 * obj.public_send(string [, args...]) -> obj
1366 *
1367 * Invokes the method identified by _symbol_, passing it any
1368 * arguments specified. Unlike send, public_send calls public
1369 * methods only.
1370 * When the method is identified by a string, the string is converted
1371 * to a symbol.
1372 *
1373 * 1.public_send(:puts, "hello") # causes NoMethodError
1374 */
1375
1376static VALUE
1377rb_f_public_send(int argc, VALUE *argv, VALUE recv)
1378{
1379 return send_internal_kw(argc, argv, recv, CALL_PUBLIC);
1380}
1381
1382/* yield */
1383
1384static inline VALUE
1385rb_yield_0_kw(int argc, const VALUE * argv, int kw_splat)
1386{
1387 return vm_yield(GET_EC(), argc, argv, kw_splat);
1388}
1389
1390static inline VALUE
1391rb_yield_0(int argc, const VALUE * argv)
1392{
1393 return vm_yield(GET_EC(), argc, argv, RB_NO_KEYWORDS);
1394}
1395
1396VALUE
1397rb_yield_1(VALUE val)
1398{
1399 return rb_yield_0(1, &val);
1400}
1401
1402VALUE
1404{
1405 if (UNDEF_P(val)) {
1406 return rb_yield_0(0, NULL);
1407 }
1408 else {
1409 return rb_yield_0(1, &val);
1410 }
1411}
1412
1413VALUE
1414rb_ec_yield(rb_execution_context_t *ec, VALUE val)
1415{
1416 if (UNDEF_P(val)) {
1417 return vm_yield(ec, 0, NULL, RB_NO_KEYWORDS);
1418 }
1419 else {
1420 return vm_yield(ec, 1, &val, RB_NO_KEYWORDS);
1421 }
1422}
1423
1424#undef rb_yield_values
1425VALUE
1427{
1428 if (n == 0) {
1429 return rb_yield_0(0, 0);
1430 }
1431 else {
1432 int i;
1433 VALUE *argv;
1434 va_list args;
1435 argv = ALLOCA_N(VALUE, n);
1436
1437 va_start(args, n);
1438 for (i=0; i<n; i++) {
1439 argv[i] = va_arg(args, VALUE);
1440 }
1441 va_end(args);
1442
1443 return rb_yield_0(n, argv);
1444 }
1445}
1446
1447VALUE
1448rb_yield_values2(int argc, const VALUE *argv)
1449{
1450 return rb_yield_0(argc, argv);
1451}
1452
1453VALUE
1454rb_yield_values_kw(int argc, const VALUE *argv, int kw_splat)
1455{
1456 return rb_yield_0_kw(argc, argv, kw_splat);
1457}
1458
1459VALUE
1461{
1462 VALUE tmp = rb_check_array_type(values);
1463 VALUE v;
1464 if (NIL_P(tmp)) {
1465 rb_raise(rb_eArgError, "not an array");
1466 }
1467 v = rb_yield_0(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp));
1468 RB_GC_GUARD(tmp);
1469 return v;
1470}
1471
1472VALUE
1473rb_yield_splat_kw(VALUE values, int kw_splat)
1474{
1475 VALUE tmp = rb_check_array_type(values);
1476 VALUE v;
1477 if (NIL_P(tmp)) {
1478 rb_raise(rb_eArgError, "not an array");
1479 }
1480 v = rb_yield_0_kw(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp), kw_splat);
1481 RB_GC_GUARD(tmp);
1482 return v;
1483}
1484
1485VALUE
1486rb_yield_force_blockarg(VALUE values)
1487{
1488 return vm_yield_force_blockarg(GET_EC(), values);
1489}
1490
1491VALUE
1493{
1494 return vm_yield_with_block(GET_EC(), argc, argv,
1495 NIL_P(blockarg) ? VM_BLOCK_HANDLER_NONE : blockarg,
1497}
1498
1499#if VMDEBUG
1500static const char *
1501vm_frametype_name(const rb_control_frame_t *cfp);
1502#endif
1503
1504static VALUE
1505rb_iterate0(VALUE (* it_proc) (VALUE), VALUE data1,
1506 const struct vm_ifunc *const ifunc,
1507 rb_execution_context_t *ec_arg)
1508{
1509 enum ruby_tag_type state;
1510 volatile VALUE retval = Qnil;
1511 rb_execution_context_t * volatile ec = ec_arg;
1512 rb_control_frame_t *volatile const cfp = ec->cfp;
1513
1514 EC_PUSH_TAG(ec);
1515 state = EC_EXEC_TAG();
1516 if (state == 0) {
1517 iter_retry:
1518 {
1519 VALUE block_handler;
1520
1521 if (ifunc) {
1522 struct rb_captured_block *captured = VM_CFP_TO_CAPTURED_BLOCK(cfp);
1523 captured->code.ifunc = ifunc;
1524 block_handler = VM_BH_FROM_IFUNC_BLOCK(captured);
1525 }
1526 else {
1527 block_handler = VM_CF_BLOCK_HANDLER(cfp);
1528 }
1529 vm_passed_block_handler_set(ec, block_handler);
1530 }
1531 retval = (*it_proc) (data1);
1532 }
1533 else if (state == TAG_BREAK || state == TAG_RETRY) {
1534 const struct vm_throw_data *const err = (struct vm_throw_data *)ec->errinfo;
1535 const rb_control_frame_t *const escape_cfp = THROW_DATA_CATCH_FRAME(err);
1536
1537 if (cfp == escape_cfp) {
1538 rb_vm_rewind_cfp(ec, cfp);
1539
1540 state = 0;
1541 ec->tag->state = TAG_NONE;
1542 ec->errinfo = Qnil;
1543
1544 if (state == TAG_RETRY) goto iter_retry;
1545 retval = THROW_DATA_VAL(err);
1546 }
1547 else if (0) {
1548 SDR(); fprintf(stderr, "%p, %p\n", (void *)cfp, (void *)escape_cfp);
1549 }
1550 }
1551 EC_POP_TAG();
1552
1553 if (state) {
1554 EC_JUMP_TAG(ec, state);
1555 }
1556 return retval;
1557}
1558
1559static VALUE
1560rb_iterate_internal(VALUE (* it_proc)(VALUE), VALUE data1,
1561 rb_block_call_func_t bl_proc, VALUE data2)
1562{
1563 return rb_iterate0(it_proc, data1,
1564 bl_proc ? rb_vm_ifunc_proc_new(bl_proc, (void *)data2) : 0,
1565 GET_EC());
1566}
1567
1569 VALUE obj;
1570 ID mid;
1571 int argc;
1572 const VALUE *argv;
1573 int kw_splat;
1574};
1575
1576static VALUE
1577iterate_method(VALUE obj)
1578{
1579 const struct iter_method_arg * arg =
1580 (struct iter_method_arg *) obj;
1581
1582 return rb_call(arg->obj, arg->mid, arg->argc, arg->argv, arg->kw_splat ? CALL_FCALL_KW : CALL_FCALL);
1583}
1584
1585VALUE 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);
1586
1587VALUE
1588rb_block_call(VALUE obj, ID mid, int argc, const VALUE * argv,
1589 rb_block_call_func_t bl_proc, VALUE data2)
1590{
1591 return rb_block_call_kw(obj, mid, argc, argv, bl_proc, data2, RB_NO_KEYWORDS);
1592}
1593
1594VALUE
1595rb_block_call_kw(VALUE obj, ID mid, int argc, const VALUE * argv,
1596 rb_block_call_func_t bl_proc, VALUE data2, int kw_splat)
1597{
1598 struct iter_method_arg arg;
1599
1600 arg.obj = obj;
1601 arg.mid = mid;
1602 arg.argc = argc;
1603 arg.argv = argv;
1604 arg.kw_splat = kw_splat;
1605 return rb_iterate_internal(iterate_method, (VALUE)&arg, bl_proc, data2);
1606}
1607
1608/*
1609 * A flexible variant of rb_block_call and rb_block_call_kw.
1610 * This function accepts flags:
1611 *
1612 * RB_NO_KEYWORDS, RB_PASS_KEYWORDS, RB_PASS_CALLED_KEYWORDS:
1613 * Works as the same as rb_block_call_kw.
1614 *
1615 * RB_BLOCK_NO_USE_PACKED_ARGS:
1616 * The given block ("bl_proc") does not use "yielded_arg" of rb_block_call_func_t.
1617 * Instead, the block accesses the yielded arguments via "argc" and "argv".
1618 * This flag allows the called method to yield arguments without allocating an Array.
1619 */
1620VALUE
1621rb_block_call2(VALUE obj, ID mid, int argc, const VALUE *argv,
1622 rb_block_call_func_t bl_proc, VALUE data2, long flags)
1623{
1624 struct iter_method_arg arg;
1625
1626 arg.obj = obj;
1627 arg.mid = mid;
1628 arg.argc = argc;
1629 arg.argv = argv;
1630 arg.kw_splat = flags & 1;
1631
1632 struct vm_ifunc *ifunc = rb_vm_ifunc_proc_new(bl_proc, (void *)data2);
1633 if (flags & RB_BLOCK_NO_USE_PACKED_ARGS)
1634 ifunc->flags |= IFUNC_YIELD_OPTIMIZABLE;
1635
1636 return rb_iterate0(iterate_method, (VALUE)&arg, ifunc, GET_EC());
1637}
1638
1639VALUE
1640rb_lambda_call(VALUE obj, ID mid, int argc, const VALUE *argv,
1641 rb_block_call_func_t bl_proc, int min_argc, int max_argc,
1642 VALUE data2)
1643{
1644 struct iter_method_arg arg;
1645 struct vm_ifunc *block;
1646
1647 if (!bl_proc) rb_raise(rb_eArgError, "NULL lambda function");
1648 arg.obj = obj;
1649 arg.mid = mid;
1650 arg.argc = argc;
1651 arg.argv = argv;
1652 arg.kw_splat = 0;
1653 block = rb_vm_ifunc_new(bl_proc, (void *)data2, min_argc, max_argc);
1654 return rb_iterate0(iterate_method, (VALUE)&arg, block, GET_EC());
1655}
1656
1657static VALUE
1658iterate_check_method(VALUE obj)
1659{
1660 const struct iter_method_arg * arg =
1661 (struct iter_method_arg *) obj;
1662
1663 return rb_check_funcall(arg->obj, arg->mid, arg->argc, arg->argv);
1664}
1665
1666VALUE
1667rb_check_block_call(VALUE obj, ID mid, int argc, const VALUE *argv,
1668 rb_block_call_func_t bl_proc, VALUE data2)
1669{
1670 struct iter_method_arg arg;
1671
1672 arg.obj = obj;
1673 arg.mid = mid;
1674 arg.argc = argc;
1675 arg.argv = argv;
1676 arg.kw_splat = 0;
1677 return rb_iterate_internal(iterate_check_method, (VALUE)&arg, bl_proc, data2);
1678}
1679
1680VALUE
1682{
1683 return rb_call(obj, idEach, 0, 0, CALL_FCALL);
1684}
1685
1686static VALUE eval_default_path = Qfalse;
1687
1688#define EVAL_LOCATION_MARK "eval at "
1689#define EVAL_LOCATION_MARK_LEN (int)rb_strlen_lit(EVAL_LOCATION_MARK)
1690
1691static VALUE
1692get_eval_default_path(void)
1693{
1694 int location_lineno;
1695 VALUE location_path = rb_source_location(&location_lineno);
1696 if (!NIL_P(location_path)) {
1697 return rb_fstring(rb_sprintf("("EVAL_LOCATION_MARK"%"PRIsVALUE":%d)",
1698 location_path, location_lineno));
1699 }
1700
1701 if (!eval_default_path) {
1702 eval_default_path = rb_fstring_lit("(eval)");
1703 rb_vm_register_global_object(eval_default_path);
1704 }
1705 return eval_default_path;
1706}
1707
1708static inline int
1709compute_isolated_depth_from_ep(const VALUE *ep)
1710{
1711 int depth = 1;
1712 while (1) {
1713 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ISOLATED)) return depth;
1714 if (VM_ENV_LOCAL_P(ep)) return 0;
1715 ep = VM_ENV_PREV_EP(ep);
1716 depth++;
1717 }
1718}
1719
1720static inline int
1721compute_isolated_depth_from_block(const struct rb_block *blk)
1722{
1723 return compute_isolated_depth_from_ep(vm_block_ep(blk));
1724}
1725
1726static const rb_iseq_t *
1727pm_eval_make_iseq(VALUE src, VALUE fname, int line,
1728 const struct rb_block *base_block)
1729{
1730 const rb_iseq_t *const parent = vm_block_iseq(base_block);
1731 const rb_iseq_t *iseq = parent;
1732 VALUE name = rb_fstring_lit("<compiled>");
1733
1734 int coverage_enabled = ((rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) != 0) ? 1 : 0;
1735 int isolated_depth = compute_isolated_depth_from_block(base_block);
1736
1737 if (!fname) {
1738 fname = rb_source_location(&line);
1739 }
1740
1741 if (!UNDEF_P(fname)) {
1742 if (!NIL_P(fname)) fname = rb_fstring(fname);
1743 }
1744 else {
1745 fname = get_eval_default_path();
1746 coverage_enabled = 0;
1747 }
1748
1749 pm_parse_result_t result;
1750 pm_parse_result_init(&result);
1751 pm_options_line_set(result.options, line);
1752 result.node.coverage_enabled = coverage_enabled;
1753
1754 // Count scopes, one for each parent iseq, plus one for our local scope.
1755 // Also total the byte lengths of every local name so that they can all be
1756 // copied into a single buffer.
1757 int scopes_count = 0;
1758 size_t names_size = 0;
1759
1760 do {
1761 scopes_count++;
1762
1763 int locals_count = ISEQ_BODY(iseq)->local_table_size;
1764 for (int local_index = 0; local_index < locals_count; local_index++) {
1765 ID local = ISEQ_BODY(iseq)->local_table[local_index];
1766 if (rb_is_local_id(local)) names_size += RSTRING_LEN(rb_id2str(local));
1767 }
1768 } while ((iseq = ISEQ_BODY(iseq)->parent_iseq));
1769 pm_options_scopes_init(result.options, scopes_count + 1);
1770
1771 // Walk over the scope tree, adding known locals at the correct depths. The
1772 // scope array should be deepest -> shallowest. so lower indexes in the
1773 // scopes array refer to root nodes on the tree, and higher indexes are the
1774 // leaf nodes.
1775 iseq = parent;
1776
1777#define FORWARDING_POSITIONALS_CHR '*'
1778#define FORWARDING_POSITIONALS_STR "*"
1779#define FORWARDING_KEYWORDS_CHR ':'
1780#define FORWARDING_KEYWORDS_STR ":"
1781#define FORWARDING_BLOCK_CHR '&'
1782#define FORWARDING_BLOCK_STR "&"
1783#define FORWARDING_ALL_CHR '.'
1784#define FORWARDING_ALL_STR "."
1785
1786 /* The scope locals are views into this buffer, so it must outlive every
1787 * read of them, including building the parent scope nodes after parsing. */
1788 uint8_t *names_buffer = names_size > 0 ? xmalloc(names_size) : NULL;
1789 uint8_t *names_cursor = names_buffer;
1790
1791 for (int scopes_index = 0; scopes_index < scopes_count; scopes_index++) {
1792 VALUE iseq_value = (VALUE)iseq;
1793 int locals_count = ISEQ_BODY(iseq)->local_table_size;
1794
1795 pm_options_scope_t *options_scope = pm_options_scope_mut(result.options, scopes_count - scopes_index - 1);
1796 pm_options_scope_init(options_scope, locals_count);
1797
1798 uint8_t forwarding = PM_OPTIONS_SCOPE_FORWARDING_NONE;
1799
1800 for (int local_index = 0; local_index < locals_count; local_index++) {
1801 pm_string_t *scope_local = pm_options_scope_local_mut(options_scope, local_index);
1802 ID local = ISEQ_BODY(iseq)->local_table[local_index];
1803
1804 if (rb_is_local_id(local)) {
1805 VALUE name = rb_id2str(local);
1806 size_t length = RSTRING_LEN(name);
1807
1808 MEMCPY(names_cursor, RSTRING_PTR(name), char, length);
1809 RB_GC_GUARD(name);
1810
1811 pm_string_init(scope_local, names_cursor, length);
1812 names_cursor += length;
1813 }
1814 else if (local == idMULT) {
1816 pm_string_init(scope_local, (const uint8_t *) FORWARDING_POSITIONALS_STR, 1);
1817 }
1818 else if (local == idPow) {
1820 pm_string_init(scope_local, (const uint8_t *) FORWARDING_KEYWORDS_STR, 1);
1821 }
1822 else if (local == idAnd) {
1824 pm_string_init(scope_local, (const uint8_t *) FORWARDING_BLOCK_STR, 1);
1825 }
1826 else if (local == idDot3) {
1827 forwarding |= PM_OPTIONS_SCOPE_FORWARDING_ALL;
1828 pm_string_init(scope_local, (const uint8_t *) FORWARDING_ALL_STR, 1);
1829 }
1830 }
1831
1832 pm_options_scope_forwarding_set(options_scope, forwarding);
1833 iseq = ISEQ_BODY(iseq)->parent_iseq;
1834
1835 /* We need to GC guard the iseq because the code above malloc memory
1836 * which could trigger a GC. Since we only use ISEQ_BODY, the compiler
1837 * may optimize out the iseq local variable so we need to GC guard it. */
1838 RB_GC_GUARD(iseq_value);
1839 }
1840
1841 // Add our empty local scope at the very end of the array for our eval
1842 // scope's locals.
1843 pm_options_scope_init(pm_options_scope_mut(result.options, scopes_count), 0);
1844
1845 VALUE script_lines;
1846 VALUE error = pm_parse_string(&result, src, fname, ruby_vm_keep_script_lines ? &script_lines : NULL);
1847
1848 // If the parse failed, clean up and raise.
1849 if (error != Qnil) {
1850 if (names_buffer) SIZED_FREE_N(names_buffer, names_size);
1851 pm_parse_result_free(&result);
1852 rb_exc_raise(error);
1853 }
1854
1855 // Create one scope node for each scope passed in, initialize the local
1856 // lookup table with all the local variable information attached to the
1857 // scope used by the parser.
1858 pm_scope_node_t *node = &result.node;
1859 iseq = parent;
1860
1861 for (int scopes_index = 0; scopes_index < scopes_count; scopes_index++) {
1862 pm_scope_node_t *parent_scope = ruby_xcalloc(1, sizeof(pm_scope_node_t));
1863 RUBY_ASSERT(parent_scope != NULL);
1864
1865 const pm_options_scope_t *options_scope = pm_options_scope(result.options, scopes_count - scopes_index - 1);
1866 parent_scope->coverage_enabled = coverage_enabled;
1867 parent_scope->parser = result.parser;
1868 int locals_count = ISEQ_BODY(iseq)->local_table_size;
1869 pm_index_lookup_table_init_heap(&parent_scope->index_lookup_table, (int) pm_parser_constants_size(result.parser));
1870 parent_scope->local_table_for_iseq_size = locals_count;
1871 pm_constant_id_list_init(&parent_scope->locals);
1872
1873 for (int local_index = 0; local_index < locals_count; local_index++) {
1874 const pm_string_t *scope_local = pm_options_scope_local(options_scope, local_index);
1875 pm_constant_id_t constant_id = 0;
1876
1877 const uint8_t *source = pm_string_source(scope_local);
1878 size_t length = pm_string_length(scope_local);
1879
1880 if (length > 0) {
1881 if (length == 1) {
1882 switch (*source) {
1883 case FORWARDING_POSITIONALS_CHR:
1884 constant_id = PM_CONSTANT_MULT;
1885 break;
1886 case FORWARDING_KEYWORDS_CHR:
1887 constant_id = PM_CONSTANT_POW;
1888 break;
1889 case FORWARDING_BLOCK_CHR:
1890 constant_id = PM_CONSTANT_AND;
1891 break;
1892 case FORWARDING_ALL_CHR:
1893 constant_id = PM_CONSTANT_DOT3;
1894 break;
1895 default:
1896 constant_id = pm_parser_constant_find(result.parser, source, length);
1897 break;
1898 }
1899 }
1900 else {
1901 constant_id = pm_parser_constant_find(result.parser, source, length);
1902 }
1903
1904 pm_index_lookup_table_insert(&parent_scope->index_lookup_table, constant_id, local_index);
1905 }
1906
1907 pm_constant_id_list_append(result.arena, &parent_scope->locals, constant_id);
1908 }
1909
1910 node->previous = parent_scope;
1911 node = parent_scope;
1912 iseq = ISEQ_BODY(iseq)->parent_iseq;
1913 }
1914
1915 if (names_buffer) SIZED_FREE_N(names_buffer, names_size);
1916
1917#undef FORWARDING_POSITIONALS_CHR
1918#undef FORWARDING_POSITIONALS_STR
1919#undef FORWARDING_KEYWORDS_CHR
1920#undef FORWARDING_KEYWORDS_STR
1921#undef FORWARDING_BLOCK_CHR
1922#undef FORWARDING_BLOCK_STR
1923#undef FORWARDING_ALL_CHR
1924#undef FORWARDING_ALL_STR
1925
1926 int error_state;
1927 iseq = pm_iseq_new_eval(&result.node, name, fname, Qnil, line, parent, isolated_depth, &error_state);
1928
1929 pm_scope_node_t *prev = result.node.previous;
1930 while (prev) {
1931 pm_scope_node_t *next = prev->previous;
1932 pm_scope_node_destroy(prev);
1933 SIZED_FREE(prev);
1934 prev = next;
1935 }
1936
1937 pm_parse_result_free(&result);
1938
1939 // If there was an error, raise it after memory has been cleaned up
1940 if (error_state) {
1941 RUBY_ASSERT(iseq == NULL);
1942 rb_jump_tag(error_state);
1943 }
1944
1945 rb_exec_event_hook_script_compiled(GET_EC(), iseq, src);
1946
1947 return iseq;
1948}
1949
1950static const rb_iseq_t *
1951eval_make_iseq(VALUE src, VALUE fname, int line,
1952 const struct rb_block *base_block)
1953{
1954 if (rb_ruby_prism_p()) {
1955 return pm_eval_make_iseq(src, fname, line, base_block);
1956 }
1957 const VALUE parser = rb_parser_new();
1958 const rb_iseq_t *const parent = vm_block_iseq(base_block);
1959 rb_iseq_t *iseq = NULL;
1960 VALUE ast_value;
1961 rb_ast_t *ast;
1962
1963 int coverage_enabled = (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) != 0;
1964 int isolated_depth = compute_isolated_depth_from_block(base_block);
1965
1966 if (!fname) {
1967 fname = rb_source_location(&line);
1968 }
1969
1970 if (!UNDEF_P(fname)) {
1971 if (!NIL_P(fname)) fname = rb_fstring(fname);
1972 }
1973 else {
1974 fname = get_eval_default_path();
1975 coverage_enabled = FALSE;
1976 }
1977
1978 rb_parser_set_context(parser, parent, FALSE);
1979 if (ruby_vm_keep_script_lines) rb_parser_set_script_lines(parser);
1980 ast_value = rb_parser_compile_string_path(parser, fname, src, line);
1981
1982 ast = rb_ruby_ast_data_get(ast_value);
1983
1984 if (ast->body.root) {
1985 ast->body.coverage_enabled = coverage_enabled;
1986 iseq = rb_iseq_new_eval(ast_value,
1987 ISEQ_BODY(parent)->location.label,
1988 fname, Qnil, line,
1989 parent, isolated_depth);
1990 }
1991 rb_ast_dispose(ast);
1992
1993 if (iseq != NULL) {
1994 if (0 && iseq) { /* for debug */
1995 VALUE disasm = rb_iseq_disasm(iseq);
1996 printf("%s\n", StringValuePtr(disasm));
1997 }
1998
1999 rb_exec_event_hook_script_compiled(GET_EC(), iseq, src);
2000 }
2001
2002 return iseq;
2003}
2004
2005static VALUE
2006eval_string_with_cref(VALUE self, VALUE src, rb_cref_t *cref, VALUE file, int line)
2007{
2008 rb_execution_context_t *ec = GET_EC();
2009 struct rb_block block;
2010 const rb_iseq_t *iseq;
2011 rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
2012 if (!cfp) {
2013 rb_raise(rb_eRuntimeError, "Can't eval on top of Fiber or Thread");
2014 }
2015
2016 block.as.captured = *VM_CFP_TO_CAPTURED_BLOCK(cfp);
2017 block.as.captured.self = self;
2018 block.as.captured.code.iseq = CFP_ISEQ(cfp);
2019 block.type = block_type_iseq;
2020
2021 // EP is not escaped to the heap here, but captured and reused by another frame.
2022 // ZJIT's locals are incompatible with it unlike YJIT's, so invalidate the ISEQ for ZJIT.
2023 if (rb_zjit_enabled_p) rb_zjit_invalidate_no_ep_escape(CFP_ISEQ(cfp));
2024
2025 iseq = eval_make_iseq(src, file, line, &block);
2026 if (!iseq) {
2027 rb_exc_raise(ec->errinfo);
2028 }
2029
2030 /* TODO: what the code checking? */
2031 if (!cref && block.as.captured.code.val) {
2032 rb_cref_t *orig_cref = vm_get_cref(vm_block_ep(&block));
2033 cref = rb_vm_cref_dup(orig_cref);
2034 }
2035 vm_set_eval_stack(ec, iseq, cref, &block);
2036
2037 /* kick */
2038 return vm_exec(ec);
2039}
2040
2041static VALUE
2042eval_string_with_scope(VALUE scope, VALUE src, VALUE file, int line)
2043{
2044 rb_execution_context_t *ec = GET_EC();
2045 rb_binding_t *bind = Check_TypedStruct(scope, &ruby_binding_data_type);
2046 const rb_iseq_t *iseq = eval_make_iseq(src, file, line, &bind->block);
2047 if (!iseq) {
2048 rb_exc_raise(ec->errinfo);
2049 }
2050
2051 vm_set_eval_stack(ec, iseq, NULL, &bind->block);
2052
2053 /* save new env */
2054 if (ISEQ_BODY(iseq)->local_table_size > 0) {
2055 vm_bind_update_env(scope, bind, vm_make_env_object(ec, ec->cfp));
2056 }
2057
2058 /* kick */
2059 return vm_exec(ec);
2060}
2061
2062/*
2063 * call-seq:
2064 * eval(string, binding = nil, filename = default_filename, lineno = 1) -> obj
2065 *
2066 * Evaluates the Ruby expression(s) in +string+. Returns the result of the last
2067 * expression.
2068 *
2069 * str = "Hello"
2070 * eval("str + ' World'") # => "Hello World"
2071 *
2072 * If +binding+ is given, which must be a Binding object, the
2073 * evaluation is performed in its context. Otherwise, the
2074 * evaluation is performed in the context of the caller.
2075 *
2076 * def get_binding(str) = binding
2077 * str = "Hello"
2078 * eval("str + ' World'", get_binding("Bye")) # => "Bye World"
2079 *
2080 * If the optional +filename+ is given, it will be used as the
2081 * filename of the evaluation (for <tt>__FILE__</tt> and errors).
2082 * Otherwise, it will default to <tt>(eval at __FILE__:__LINE__)</tt>
2083 * where <tt>__FILE__</tt> and <tt>__LINE__</tt> are the filename and
2084 * line number of the caller, respectively.
2085 *
2086 * eval("puts __FILE__") # => "(eval at test.rb:1)"
2087 * eval("puts __FILE__", nil, "foobar.rb") # => "foobar.rb"
2088 *
2089 * If the optional +lineno+ is given, it will be used as the
2090 * line number of the evaluation (for <tt>__LINE__</tt> and errors).
2091 * Otherwise, it will default to 1.
2092 *
2093 * eval("puts __LINE__") # => 1
2094 * eval("puts __LINE__", nil, "foobar.rb", 10) # => 10
2095 */
2096
2097VALUE
2098rb_f_eval(int argc, const VALUE *argv, VALUE self)
2099{
2100 VALUE src, scope, vfile, vline;
2101 VALUE file = Qundef;
2102 int line = 1;
2103
2104 rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
2105 StringValue(src);
2106 if (argc >= 3) {
2107 StringValue(vfile);
2108 }
2109 if (argc >= 4) {
2110 line = NUM2INT(vline);
2111 }
2112
2113 if (!NIL_P(vfile))
2114 file = vfile;
2115
2116 if (NIL_P(scope))
2117 return eval_string_with_cref(self, src, NULL, file, line);
2118 else
2119 return eval_string_with_scope(scope, src, file, line);
2120}
2121
2123VALUE
2124ruby_eval_string_from_file(const char *str, const char *filename)
2125{
2126 VALUE file = filename ? rb_str_new_cstr(filename) : 0;
2127 rb_execution_context_t *ec = GET_EC();
2128 rb_control_frame_t *cfp = ec ? rb_vm_get_ruby_level_next_cfp(ec, ec->cfp) : NULL;
2129 VALUE self = cfp ? cfp->self : rb_vm_top_self();
2130 return eval_string_with_cref(self, rb_str_new2(str), NULL, file, 1);
2131}
2132
2133VALUE
2134rb_eval_string(const char *str)
2135{
2136 return ruby_eval_string_from_file(str, "eval");
2137}
2138
2139static VALUE
2140eval_string_protect(VALUE str)
2141{
2142 return rb_eval_string((char *)str);
2143}
2144
2145VALUE
2146rb_eval_string_protect(const char *str, int *pstate)
2147{
2148 return rb_protect(eval_string_protect, (VALUE)str, pstate);
2149}
2150
2152 VALUE top_self;
2153 VALUE klass;
2154 const char *str;
2155};
2156
2157static VALUE
2158eval_string_wrap_protect(VALUE data)
2159{
2160 const struct eval_string_wrap_arg *const arg = (struct eval_string_wrap_arg*)data;
2161 rb_cref_t *cref = rb_vm_cref_new_toplevel();
2162 cref->klass_or_self = arg->klass;
2163 return eval_string_with_cref(arg->top_self, rb_str_new_cstr(arg->str), cref, rb_str_new_cstr("eval"), 1);
2164}
2165
2166VALUE
2167rb_eval_string_wrap(const char *str, int *pstate)
2168{
2169 int state;
2170 rb_thread_t *th = GET_THREAD();
2171 VALUE self = th->top_self;
2172 VALUE wrapper = th->top_wrapper;
2173 VALUE val;
2174 struct eval_string_wrap_arg data;
2175
2176 th->top_wrapper = rb_module_new();
2177 th->top_self = rb_obj_clone(rb_vm_top_self());
2178 rb_extend_object(th->top_self, th->top_wrapper);
2179
2180 data.top_self = th->top_self;
2181 data.klass = th->top_wrapper;
2182 data.str = str;
2183
2184 val = rb_protect(eval_string_wrap_protect, (VALUE)&data, &state);
2185
2186 th->top_self = self;
2187 th->top_wrapper = wrapper;
2188
2189 if (pstate) {
2190 *pstate = state;
2191 }
2192 else if (state != TAG_NONE) {
2193 EC_JUMP_TAG(th->ec, state);
2194 }
2195 return val;
2196}
2197
2198VALUE
2199rb_eval_cmd_kw(VALUE cmd, VALUE arg, int kw_splat)
2200{
2201 Check_Type(arg, T_ARRAY);
2202 int argc = RARRAY_LENINT(arg);
2203 const VALUE *argv = RARRAY_CONST_PTR(arg);
2204 VALUE val = rb_eval_cmd_call_kw(cmd, argc, argv, kw_splat);
2205 RB_GC_GUARD(arg);
2206 return val;
2207}
2208
2209VALUE
2210rb_eval_cmd_call_kw(VALUE cmd, int argc, const VALUE *argv, int kw_splat)
2211{
2212 enum ruby_tag_type state;
2213 volatile VALUE val = Qnil; /* OK */
2214 rb_execution_context_t * volatile ec = GET_EC();
2215
2216 EC_PUSH_TAG(ec);
2217 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2218 if (!RB_TYPE_P(cmd, T_STRING)) {
2219 val = rb_funcallv_kw(cmd, idCall, argc, argv, kw_splat);
2220 }
2221 else {
2222 val = eval_string_with_cref(rb_vm_top_self(), cmd, NULL, 0, 0);
2223 }
2224 }
2225 EC_POP_TAG();
2226
2227 if (state) EC_JUMP_TAG(ec, state);
2228 return val;
2229}
2230
2231/* block eval under the class/module context */
2232
2233static VALUE
2234yield_under(VALUE self, int singleton, int argc, const VALUE *argv, int kw_splat)
2235{
2236 rb_execution_context_t *ec = GET_EC();
2237 rb_control_frame_t *cfp = ec->cfp;
2238 VALUE block_handler = VM_CF_BLOCK_HANDLER(cfp);
2239 VALUE new_block_handler = 0;
2240 const struct rb_captured_block *captured = NULL;
2241 struct rb_captured_block new_captured;
2242 const VALUE *ep = NULL;
2243 rb_cref_t *cref;
2244 int is_lambda = FALSE;
2245 const rb_cref_t *proc_cref = NULL;
2246
2247 if (block_handler != VM_BLOCK_HANDLER_NONE) {
2248 again:
2249 switch (vm_block_handler_type(block_handler)) {
2250 case block_handler_type_iseq:
2251 captured = VM_BH_TO_CAPT_BLOCK(block_handler);
2252 new_captured = *captured;
2253 new_block_handler = VM_BH_FROM_ISEQ_BLOCK(&new_captured);
2254 break;
2255 case block_handler_type_ifunc:
2256 captured = VM_BH_TO_CAPT_BLOCK(block_handler);
2257 new_captured = *captured;
2258 new_block_handler = VM_BH_FROM_IFUNC_BLOCK(&new_captured);
2259 break;
2260 case block_handler_type_proc:
2261 {
2262 VALUE procval = VM_BH_TO_PROC(block_handler);
2263 rb_proc_t *po;
2264 GetProcPtr(procval, po);
2265 is_lambda = po->header.is_lambda;
2266 if (po->header.is_refined) proc_cref = rb_proc_refinements_cref_for_call(procval);
2267 block_handler = vm_block_to_block_handler(&po->block);
2268 }
2269 goto again;
2270 case block_handler_type_symbol:
2271 return rb_sym_proc_call(SYM2ID(VM_BH_TO_SYMBOL(block_handler)),
2272 argc, argv, kw_splat,
2273 VM_BLOCK_HANDLER_NONE);
2274 }
2275
2276 new_captured.self = self;
2277 ep = captured->ep;
2278
2279 VM_FORCE_WRITE_SPECIAL_CONST(&VM_CF_LEP(ec->cfp)[VM_ENV_DATA_INDEX_SPECVAL], new_block_handler);
2280 }
2281
2282 VM_ASSERT(singleton || RB_TYPE_P(self, T_MODULE) || RB_TYPE_P(self, T_CLASS));
2283 cref = vm_cref_push(ec, self, ep, TRUE, singleton);
2284
2285 if (proc_cref && !NIL_P(CREF_REFINEMENTS(proc_cref))) {
2286 CREF_REFINEMENTS_SET(cref, rb_hash_dup(CREF_REFINEMENTS(proc_cref)));
2287 CREF_REFINED_PROC_SET(cref);
2288 }
2289
2290 return vm_yield_with_cref(ec, argc, argv, kw_splat, cref, is_lambda);
2291}
2292
2293VALUE
2294rb_yield_refine_block(VALUE refinement, VALUE refinements)
2295{
2296 rb_execution_context_t *ec = GET_EC();
2297 VALUE block_handler = VM_CF_BLOCK_HANDLER(ec->cfp);
2298
2299 if (vm_block_handler_type(block_handler) != block_handler_type_iseq) {
2300 rb_bug("rb_yield_refine_block: an iseq block is required");
2301 }
2302 else {
2303 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(block_handler);
2304 struct rb_captured_block new_captured = *captured;
2305 const VALUE *const argv = &new_captured.self; /* dummy to suppress nonnull warning from gcc */
2306 VALUE new_block_handler = VM_BH_FROM_ISEQ_BLOCK(&new_captured);
2307 const VALUE *ep = captured->ep;
2308 rb_cref_t *cref = vm_cref_push(ec, refinement, ep, TRUE, FALSE);
2309 CREF_REFINEMENTS_SET(cref, refinements);
2310 VM_FORCE_WRITE_SPECIAL_CONST(&VM_CF_LEP(ec->cfp)[VM_ENV_DATA_INDEX_SPECVAL], new_block_handler);
2311 new_captured.self = refinement;
2312 return vm_yield_with_cref(ec, 0, argv, RB_NO_KEYWORDS, cref, FALSE);
2313 }
2314}
2315
2316/* string eval under the class/module context */
2317static VALUE
2318eval_under(VALUE self, int singleton, VALUE src, VALUE file, int line)
2319{
2320 rb_cref_t *cref = vm_cref_push(GET_EC(), self, NULL, FALSE, singleton);
2321 StringValue(src);
2322
2323 return eval_string_with_cref(self, src, cref, file, line);
2324}
2325
2326static VALUE
2327specific_eval(int argc, const VALUE *argv, VALUE self, int singleton, int kw_splat)
2328{
2329 if (rb_block_given_p()) {
2330 rb_check_arity(argc, 0, 0);
2331 return yield_under(self, singleton, 1, &self, kw_splat);
2332 }
2333 else {
2334 VALUE file = Qnil;
2335 int line = 1;
2336 VALUE code;
2337
2338 rb_check_arity(argc, 1, 3);
2339 code = argv[0];
2340 StringValue(code);
2341 if (argc > 2)
2342 line = NUM2INT(argv[2]);
2343 if (argc > 1) {
2344 file = argv[1];
2345 if (!NIL_P(file)) StringValue(file);
2346 }
2347
2348 if (NIL_P(file)) {
2349 file = get_eval_default_path();
2350 }
2351
2352 return eval_under(self, singleton, code, file, line);
2353 }
2354}
2355
2356/*
2357 * call-seq:
2358 * obj.instance_eval(string [, filename [, lineno]] ) -> obj
2359 * obj.instance_eval {|obj| block } -> obj
2360 *
2361 * Evaluates a string containing Ruby source code, or the given block,
2362 * within the context of the receiver (_obj_). In order to set the
2363 * context, the variable +self+ is set to _obj_ while
2364 * the code is executing, giving the code access to _obj_'s
2365 * instance variables and private methods.
2366 *
2367 * When <code>instance_eval</code> is given a block, _obj_ is also
2368 * passed in as the block's only argument.
2369 *
2370 * When <code>instance_eval</code> is given a +String+, the optional
2371 * second and third parameters supply a filename and starting line number
2372 * that are used when reporting compilation errors.
2373 *
2374 * class KlassWithSecret
2375 * def initialize
2376 * @secret = 99
2377 * end
2378 * private
2379 * def the_secret
2380 * "Ssssh! The secret is #{@secret}."
2381 * end
2382 * end
2383 * k = KlassWithSecret.new
2384 * k.instance_eval { @secret } #=> 99
2385 * k.instance_eval { the_secret } #=> "Ssssh! The secret is 99."
2386 * k.instance_eval {|obj| obj == self } #=> true
2387 */
2388
2389static VALUE
2390rb_obj_instance_eval_internal(int argc, const VALUE *argv, VALUE self)
2391{
2392 return specific_eval(argc, argv, self, TRUE, RB_PASS_CALLED_KEYWORDS);
2393}
2394
2395VALUE
2396rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self)
2397{
2398 return specific_eval(argc, argv, self, TRUE, RB_NO_KEYWORDS);
2399}
2400
2401/*
2402 * call-seq:
2403 * obj.instance_exec(arg...) {|var...| block } -> obj
2404 *
2405 * Executes the given block within the context of the receiver
2406 * (_obj_). In order to set the context, the variable +self+ is set
2407 * to _obj_ while the code is executing, giving the code access to
2408 * _obj_'s instance variables. Arguments are passed as block parameters.
2409 *
2410 * class KlassWithSecret
2411 * def initialize
2412 * @secret = 99
2413 * end
2414 * end
2415 * k = KlassWithSecret.new
2416 * k.instance_exec(5) {|x| @secret+x } #=> 104
2417 */
2418
2419static VALUE
2420rb_obj_instance_exec_internal(int argc, const VALUE *argv, VALUE self)
2421{
2422 return yield_under(self, TRUE, argc, argv, RB_PASS_CALLED_KEYWORDS);
2423}
2424
2425VALUE
2426rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self)
2427{
2428 return yield_under(self, TRUE, argc, argv, RB_NO_KEYWORDS);
2429}
2430
2431/*
2432 * call-seq:
2433 * class_eval(string, filename = nil, lineno = 1) -> obj
2434 * class_eval { |mod| ... } -> obj
2435 * module_eval(string, filename = nil, lineno = 1) -> obj
2436 * module_eval { |mod| ... } -> obj
2437 *
2438 * Evaluates the +string+ or block in the context of +self+.
2439 * Returns the result of the last expression.
2440 *
2441 * When +string+ is given, evaluates the given string in the
2442 * context of +self+:
2443 *
2444 * class Foo; end
2445 *
2446 * Foo.module_eval("def greeting = puts 'hello'")
2447 *
2448 * Foo.new.greeting # => "hello"
2449 *
2450 * If the optional +filename+ is given, it will be used as the
2451 * filename of the evaluation (for <tt>__FILE__</tt> and errors).
2452 * Otherwise, it will default to <tt>(eval at __FILE__:__LINE__)</tt>
2453 * where <tt>__FILE__</tt> and <tt>__LINE__</tt> are the filename and
2454 * line number of the caller, respectively:
2455 *
2456 * class Foo; end
2457 *
2458 * Foo.module_eval("puts __FILE__") # => "(eval at ../test.rb:3)"
2459 * Foo.module_eval("puts __FILE__", "foobar.rb") # => "foobar.rb"
2460 *
2461 * If the optional +lineno+ is given, it will be used as the
2462 * line number of the evaluation (for <tt>__LINE__</tt> and errors).
2463 * Otherwise, it will default to 1:
2464 *
2465 * class Foo; end
2466 *
2467 * Foo.module_eval("puts __LINE__") # => 1
2468 * Foo.module_eval("puts __LINE__", nil, 10) # => 10
2469 *
2470 * When a block is given, evaluates the block in the context
2471 * of +self+:
2472 *
2473 * class Foo; end
2474 *
2475 * Foo.module_eval do
2476 * def greeting = puts "hello"
2477 * end
2478 *
2479 * Foo.new.greeting
2480 *
2481 * However, constant and class variable lookup differs between
2482 * +string+ and block. When +string+ is given, constant and class
2483 * variables are looked up in the context of +self+. When a block
2484 * is given, the context of the lookup is not changed:
2485 *
2486 * class Foo
2487 * GREETING = "hello"
2488 * end
2489 *
2490 * Foo.module_eval("puts GREETING") # => "hello"
2491 *
2492 * Foo.module_eval { puts GREETING } # => NameError: uninitialized constant GREETING
2493 */
2494
2495static VALUE
2496rb_mod_module_eval_internal(int argc, const VALUE *argv, VALUE mod)
2497{
2498 return specific_eval(argc, argv, mod, FALSE, RB_PASS_CALLED_KEYWORDS);
2499}
2500
2501VALUE
2502rb_mod_module_eval(int argc, const VALUE *argv, VALUE mod)
2503{
2504 return specific_eval(argc, argv, mod, FALSE, RB_NO_KEYWORDS);
2505}
2506
2507/*
2508 * call-seq:
2509 * mod.module_exec(arg...) {|var...| block } -> obj
2510 * mod.class_exec(arg...) {|var...| block } -> obj
2511 *
2512 * Evaluates the given block in the context of the class/module.
2513 * The method defined in the block will belong to the receiver.
2514 * Any arguments passed to the method will be passed to the block.
2515 * This can be used if the block needs to access instance variables.
2516 *
2517 * class Thing
2518 * end
2519 * Thing.class_exec{
2520 * def hello() "Hello there!" end
2521 * }
2522 * puts Thing.new.hello()
2523 *
2524 * <em>produces:</em>
2525 *
2526 * Hello there!
2527 */
2528
2529static VALUE
2530rb_mod_module_exec_internal(int argc, const VALUE *argv, VALUE mod)
2531{
2532 return yield_under(mod, FALSE, argc, argv, RB_PASS_CALLED_KEYWORDS);
2533}
2534
2535VALUE
2536rb_mod_module_exec(int argc, const VALUE *argv, VALUE mod)
2537{
2538 return yield_under(mod, FALSE, argc, argv, RB_NO_KEYWORDS);
2539}
2540
2541/*
2542 * Document-class: UncaughtThrowError
2543 *
2544 * Raised when +throw+ is called with a _tag_ which does not have
2545 * corresponding +catch+ block.
2546 *
2547 * throw "foo", "bar"
2548 *
2549 * <em>raises the exception:</em>
2550 *
2551 * UncaughtThrowError: uncaught throw "foo"
2552 */
2553
2554static VALUE
2555uncaught_throw_init(int argc, const VALUE *argv, VALUE exc)
2556{
2558 rb_call_super(argc - 2, argv + 2);
2559 rb_ivar_set(exc, id_tag, argv[0]);
2560 rb_ivar_set(exc, id_value, argv[1]);
2561 return exc;
2562}
2563
2564/*
2565 * call-seq:
2566 * uncaught_throw.tag -> obj
2567 *
2568 * Return the tag object which was called for.
2569 */
2570
2571static VALUE
2572uncaught_throw_tag(VALUE exc)
2573{
2574 return rb_ivar_get(exc, id_tag);
2575}
2576
2577/*
2578 * call-seq:
2579 * uncaught_throw.value -> obj
2580 *
2581 * Return the return value which was called for.
2582 */
2583
2584static VALUE
2585uncaught_throw_value(VALUE exc)
2586{
2587 return rb_ivar_get(exc, id_value);
2588}
2589
2590/*
2591 * call-seq:
2592 * uncaught_throw.to_s -> string
2593 *
2594 * Returns formatted message with the inspected tag.
2595 */
2596
2597static VALUE
2598uncaught_throw_to_s(VALUE exc)
2599{
2600 VALUE mesg = rb_attr_get(exc, id_mesg);
2601 VALUE tag = uncaught_throw_tag(exc);
2602 return rb_str_format(1, &tag, mesg);
2603}
2604
2605/*
2606 * call-seq:
2607 * throw(tag [, obj])
2608 *
2609 * Transfers control to the end of the active +catch+ block
2610 * waiting for _tag_. Raises +UncaughtThrowError+ if there
2611 * is no +catch+ block for the _tag_. The optional second
2612 * parameter supplies a return value for the +catch+ block,
2613 * which otherwise defaults to +nil+. For examples, see
2614 * Kernel::catch.
2615 */
2616
2617static VALUE
2618rb_f_throw(int argc, VALUE *argv, VALUE _)
2619{
2620 VALUE tag, value;
2621
2622 rb_scan_args(argc, argv, "11", &tag, &value);
2623 rb_throw_obj(tag, value);
2625}
2626
2627void
2629{
2630 rb_execution_context_t *ec = GET_EC();
2631 struct rb_vm_tag *tt = ec->tag;
2632
2633 while (tt) {
2634 if (tt->tag == tag) {
2635 tt->retval = value;
2636 break;
2637 }
2638 tt = tt->prev;
2639 }
2640 if (!tt) {
2641 VALUE desc[3];
2642 desc[0] = tag;
2643 desc[1] = value;
2644 desc[2] = rb_str_new_cstr("uncaught throw %p");
2645 rb_exc_raise(rb_class_new_instance(numberof(desc), desc, rb_eUncaughtThrow));
2646 }
2647
2648 ec->errinfo = (VALUE)THROW_DATA_NEW(tag, NULL, TAG_THROW);
2649 EC_JUMP_TAG(ec, TAG_THROW);
2650}
2651
2652void
2653rb_throw(const char *tag, VALUE val)
2654{
2655 rb_throw_obj(rb_sym_intern_ascii_cstr(tag), val);
2656}
2657
2658static VALUE
2659catch_i(RB_BLOCK_CALL_FUNC_ARGLIST(tag, _))
2660{
2661 return rb_yield_0(1, &tag);
2662}
2663
2664/*
2665 * call-seq:
2666 * catch([tag]) {|tag| block } -> obj
2667 *
2668 * +catch+ executes its block. If +throw+ is not called, the block executes
2669 * normally, and +catch+ returns the value of the last expression evaluated.
2670 *
2671 * catch(1) { 123 } # => 123
2672 *
2673 * If <code>throw(tag2, val)</code> is called, Ruby searches up its stack for
2674 * a +catch+ block whose +tag+ has the same +object_id+ as _tag2_. When found,
2675 * the block stops executing and returns _val_ (or +nil+ if no second argument
2676 * was given to +throw+).
2677 *
2678 * catch(1) { throw(1, 456) } # => 456
2679 * catch(1) { throw(1) } # => nil
2680 *
2681 * When +tag+ is passed as the first argument, +catch+ yields it as the
2682 * parameter of the block.
2683 *
2684 * catch(1) {|x| x + 2 } # => 3
2685 *
2686 * When no +tag+ is given, +catch+ yields a new unique object (as from
2687 * +Object.new+) as the block parameter. This object can then be used as the
2688 * argument to +throw+, and will match the correct +catch+ block.
2689 *
2690 * catch do |obj_A|
2691 * catch do |obj_B|
2692 * throw(obj_B, 123)
2693 * puts "This puts is not reached"
2694 * end
2695 *
2696 * puts "This puts is displayed"
2697 * 456
2698 * end
2699 *
2700 * # => 456
2701 *
2702 * catch do |obj_A|
2703 * catch do |obj_B|
2704 * throw(obj_A, 123)
2705 * puts "This puts is still not reached"
2706 * end
2707 *
2708 * puts "Now this puts is also not reached"
2709 * 456
2710 * end
2711 *
2712 * # => 123
2713 */
2714
2715static VALUE
2716rb_f_catch(int argc, VALUE *argv, VALUE self)
2717{
2718 VALUE tag = rb_check_arity(argc, 0, 1) ? argv[0] : rb_obj_alloc(rb_cObject);
2719 return rb_catch_obj(tag, catch_i, 0);
2720}
2721
2722VALUE
2723rb_catch(const char *tag, rb_block_call_func_t func, VALUE data)
2724{
2725 VALUE vtag = tag ? rb_sym_intern_ascii_cstr(tag) : rb_obj_alloc(rb_cObject);
2726 return rb_catch_obj(vtag, func, data);
2727}
2728
2729static VALUE
2730vm_catch_protect(VALUE tag, rb_block_call_func *func, VALUE data,
2731 enum ruby_tag_type *stateptr_arg, rb_execution_context_t *volatile ec)
2732{
2733 enum ruby_tag_type state;
2734 VALUE val = Qnil; /* OK */
2735 rb_control_frame_t *volatile saved_cfp = ec->cfp;
2736 enum ruby_tag_type * volatile stateptr = stateptr_arg;
2737
2738 EC_PUSH_TAG(ec);
2739
2740 _tag.tag = tag;
2741
2742 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2743 /* call with argc=1, argv = [tag], block = Qnil to insure compatibility */
2744 val = (*func)(tag, data, 1, (const VALUE *)&tag, Qnil);
2745 }
2746 else if (state == TAG_THROW && THROW_DATA_VAL((struct vm_throw_data *)ec->errinfo) == tag) {
2747 rb_vm_rewind_cfp(ec, saved_cfp);
2748 val = ec->tag->retval;
2749 ec->errinfo = Qnil;
2750 state = 0;
2751 }
2752 EC_POP_TAG();
2753 if (stateptr)
2754 *stateptr = state;
2755
2756 return val;
2757}
2758
2759VALUE
2760rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, enum ruby_tag_type *stateptr)
2761{
2762 return vm_catch_protect(t, func, data, stateptr, GET_EC());
2763}
2764
2765VALUE
2767{
2768 enum ruby_tag_type state;
2769 rb_execution_context_t *ec = GET_EC();
2770 VALUE val = vm_catch_protect(t, (rb_block_call_func *)func, data, &state, ec);
2771 if (state) EC_JUMP_TAG(ec, state);
2772 return val;
2773}
2774
2775static void
2776local_var_list_init(struct local_var_list *vars)
2777{
2778 vars->tbl = rb_ident_hash_new();
2779 RBASIC_CLEAR_CLASS(vars->tbl);
2780}
2781
2782static VALUE
2783local_var_list_finish(struct local_var_list *vars)
2784{
2785 /* TODO: not to depend on the order of st_table */
2786 VALUE ary = rb_hash_keys(vars->tbl);
2787 rb_hash_clear(vars->tbl);
2788 vars->tbl = 0;
2789 return ary;
2790}
2791
2792static int
2793local_var_list_update(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
2794{
2795 if (existing) return ST_STOP;
2796 *value = (st_data_t)Qtrue; /* INT2FIX(arg) */
2797 return ST_CONTINUE;
2798}
2799
2800extern int rb_numparam_id_p(ID id);
2801
2802static void
2803local_var_list_add(const struct local_var_list *vars, ID lid)
2804{
2805 /* should skip temporary variable */
2806 if (!lid) return;
2807 if (!rb_is_local_id(lid)) return;
2808
2809 /* should skip numbered parameters as well */
2810 if (rb_numparam_id_p(lid)) return;
2811
2812 st_data_t idx = 0; /* tbl->num_entries */
2813 rb_hash_stlike_update(vars->tbl, ID2SYM(lid), local_var_list_update, idx);
2814}
2815
2816static void
2817numparam_list_add(const struct local_var_list *vars, ID lid)
2818{
2819 /* should skip temporary variable */
2820 if (!lid) return;
2821 if (!rb_is_local_id(lid)) return;
2822
2823 /* should skip anything but numbered parameters */
2824 if (rb_numparam_id_p(lid)) {
2825 st_data_t idx = 0; /* tbl->num_entries */
2826 rb_hash_stlike_update(vars->tbl, ID2SYM(lid), local_var_list_update, idx);
2827 }
2828}
2829
2830/*
2831 * call-seq:
2832 * local_variables -> array
2833 *
2834 * Returns the names of the current local variables.
2835 *
2836 * fred = 1
2837 * for i in 1..10
2838 * # ...
2839 * end
2840 * local_variables #=> [:fred, :i]
2841 */
2842
2843static VALUE
2844rb_f_local_variables(VALUE _)
2845{
2846 struct local_var_list vars;
2847 rb_execution_context_t *ec = GET_EC();
2848 rb_control_frame_t *cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp));
2849 unsigned int i;
2850
2851 local_var_list_init(&vars);
2852 while (cfp) {
2853 if (CFP_ISEQ(cfp)) {
2854 for (i = 0; i < ISEQ_BODY(CFP_ISEQ(cfp))->local_table_size; i++) {
2855 local_var_list_add(&vars, ISEQ_BODY(CFP_ISEQ(cfp))->local_table[i]);
2856 }
2857 }
2858 if (!VM_ENV_LOCAL_P(cfp->ep)) {
2859 /* block */
2860 const VALUE *ep = VM_CF_PREV_EP(cfp);
2861
2862 if (vm_collect_local_variables_in_heap(ep, &vars)) {
2863 break;
2864 }
2865 else {
2866 while (cfp->ep != ep) {
2867 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2868 }
2869 }
2870 }
2871 else {
2872 break;
2873 }
2874 }
2875 return local_var_list_finish(&vars);
2876}
2877
2878/*
2879 * call-seq:
2880 * block_given? -> true or false
2881 *
2882 * Returns <code>true</code> if <code>yield</code> would execute a
2883 * block in the current context. The <code>iterator?</code> form
2884 * is mildly deprecated.
2885 *
2886 * def try
2887 * if block_given?
2888 * yield
2889 * else
2890 * "no block"
2891 * end
2892 * end
2893 * try #=> "no block"
2894 * try { "hello" } #=> "hello"
2895 * try do "hello" end #=> "hello"
2896 */
2897
2898static VALUE
2899rb_f_block_given_p(VALUE _)
2900{
2901 rb_execution_context_t *ec = GET_EC();
2902 rb_control_frame_t *cfp = ec->cfp;
2903 cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
2904
2905 return RBOOL(cfp != NULL && VM_CF_BLOCK_HANDLER(cfp) != VM_BLOCK_HANDLER_NONE);
2906}
2907
2908/*
2909 * call-seq:
2910 * iterator? -> true or false
2911 *
2912 * Deprecated. Use block_given? instead.
2913 */
2914
2915static VALUE
2916rb_f_iterator_p(VALUE self)
2917{
2918 rb_warn_deprecated("iterator?", "block_given?");
2919 return rb_f_block_given_p(self);
2920}
2921
2922VALUE
2923rb_current_realfilepath(void)
2924{
2925 const rb_execution_context_t *ec = GET_EC();
2926 rb_control_frame_t *cfp = ec->cfp;
2927 cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
2928 if (cfp != NULL) {
2929 const rb_iseq_t *iseq = CFP_ISEQ(cfp);
2930 VALUE path = rb_iseq_realpath(iseq);
2931 if (RTEST(path)) return path;
2932 // eval context
2933 path = rb_iseq_path(iseq);
2934 if (path == eval_default_path) {
2935 return Qnil;
2936 }
2937
2938 // [Feature #19755] implicit eval location is "(eval at #{__FILE__}:#{__LINE__})"
2939 const rb_len_t len = RSTRING_LEN(path);
2940 if (len > EVAL_LOCATION_MARK_LEN+1) {
2941 const char *const ptr = RSTRING_PTR(path);
2942 if (ptr[len - 1] == ')' &&
2943 memcmp(ptr, "("EVAL_LOCATION_MARK, EVAL_LOCATION_MARK_LEN+1) == 0) {
2944 return Qnil;
2945 }
2946 }
2947
2948 return path;
2949 }
2950 return Qnil;
2951}
2952
2953// Assert that an internal function is running and return
2954// the imemo object that represents it.
2955struct vm_ifunc *
2956rb_current_ifunc(void)
2957{
2958 // Search VM_FRAME_MAGIC_IFUNC to see ifunc imemos put on the iseq field.
2959 VALUE ifunc = (VALUE)CFP_ISEQ(GET_EC()->cfp);
2960 RUBY_ASSERT_ALWAYS(imemo_type_p(ifunc, imemo_ifunc));
2961 return (struct vm_ifunc *)ifunc;
2962}
2963
2964void
2965Init_vm_eval(void)
2966{
2967 rb_define_global_function("eval", rb_f_eval, -1);
2968 rb_define_global_function("local_variables", rb_f_local_variables, 0);
2969 rb_define_global_function("iterator?", rb_f_iterator_p, 0);
2970 rb_define_global_function("block_given?", rb_f_block_given_p, 0);
2971
2972 rb_define_global_function("catch", rb_f_catch, -1);
2973 rb_define_global_function("throw", rb_f_throw, -1);
2974
2975 rb_define_method(rb_cBasicObject, "instance_eval", rb_obj_instance_eval_internal, -1);
2976 rb_define_method(rb_cBasicObject, "instance_exec", rb_obj_instance_exec_internal, -1);
2977 rb_define_private_method(rb_cBasicObject, "method_missing", rb_method_missing, -1);
2978
2979#if 1
2980 rb_add_method(rb_cBasicObject, id__send__,
2981 VM_METHOD_TYPE_OPTIMIZED, (void *)OPTIMIZED_METHOD_TYPE_SEND, METHOD_VISI_PUBLIC);
2982 rb_add_method(rb_mKernel, idSend,
2983 VM_METHOD_TYPE_OPTIMIZED, (void *)OPTIMIZED_METHOD_TYPE_SEND, METHOD_VISI_PUBLIC);
2984#else
2985 rb_define_method(rb_cBasicObject, "__send__", rb_f_send, -1);
2986 rb_define_method(rb_mKernel, "send", rb_f_send, -1);
2987#endif
2988 rb_define_method(rb_mKernel, "public_send", rb_f_public_send, -1);
2989
2990 rb_define_method(rb_cModule, "module_exec", rb_mod_module_exec_internal, -1);
2991 rb_define_method(rb_cModule, "class_exec", rb_mod_module_exec_internal, -1);
2992 rb_define_method(rb_cModule, "module_eval", rb_mod_module_eval_internal, -1);
2993 rb_define_method(rb_cModule, "class_eval", rb_mod_module_eval_internal, -1);
2994
2995 rb_eUncaughtThrow = rb_define_class("UncaughtThrowError", rb_eArgError);
2996 rb_define_method(rb_eUncaughtThrow, "initialize", uncaught_throw_init, -1);
2997 rb_define_method(rb_eUncaughtThrow, "tag", uncaught_throw_tag, 0);
2998 rb_define_method(rb_eUncaughtThrow, "value", uncaught_throw_value, 0);
2999 rb_define_method(rb_eUncaughtThrow, "to_s", uncaught_throw_to_s, 0);
3000
3001 id_result = rb_intern_const("result");
3002 id_tag = rb_intern_const("tag");
3003 id_value = rb_intern_const("value");
3004}
#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:1628
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:3350
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:2274
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition object.c:2315
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:240
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:507
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:912
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:2167
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:1211
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1148
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:1115
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:1106
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:1225
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:2146
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:1199
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:1218
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:1235
VALUE rb_eval_string(const char *str)
Evaluates the given string.
Definition vm_eval.c:2134
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:1205
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:2150
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:1645
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:3593
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:716
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:710
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:2502
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:2536
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:2426
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:2199
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:1123
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:2396
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:985
#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:1681
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:1426
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:1460
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:2653
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:1448
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1403
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:1454
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:2628
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:1595
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:1473
#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:49
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_ALL
When the scope is forwarding with the ... parameter.
Definition options.h:61
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS
When the scope is forwarding with the * parameter.
Definition options.h:52
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS
When the scope is forwarding with the ** parameter.
Definition options.h:55
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_BLOCK
When the scope is forwarding with the & parameter.
Definition options.h:58
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 view of a sequence of bytes.
Definition stringy.h:23
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:142
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