Ruby 4.1.0dev (2026-11-10 revision d9bbf7af11227361911849907728f055a9e49db0)
vm_eval.c (d9bbf7af11227361911849907728f055a9e49db0)
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 stack_check(ec);
1096 return vm_call0_cc(ec, recv, mid, argc, argv, cc, false);
1097 }
1098 else {
1099 return rb_call0(ec, recv, mid, argc, argv, scope, self);
1100 }
1101}
1102
1103#ifdef rb_funcallv
1104#undef rb_funcallv
1105#endif
1106VALUE
1107rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
1108{
1109 VM_ASSERT(ruby_thread_has_gvl_p());
1110 VM_ASSERT(!RB_TYPE_P(recv, T_IMEMO));
1111
1112 return rb_funcallv_scope(recv, mid, argc, argv, CALL_FCALL);
1113}
1114
1115VALUE
1116rb_funcallv_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
1117{
1118 VM_ASSERT(ruby_thread_has_gvl_p());
1119
1120 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_FCALL_KW : CALL_FCALL);
1121}
1122
1123VALUE
1124rb_apply(VALUE recv, ID mid, VALUE args)
1125{
1126 int argc;
1127 VALUE *argv, ret;
1128
1129 argc = RARRAY_LENINT(args);
1130 if (argc >= 0x100) {
1131 args = rb_ary_subseq(args, 0, argc);
1132 RBASIC_CLEAR_CLASS(args);
1133 OBJ_FREEZE(args);
1134 ret = rb_call(recv, mid, argc, RARRAY_CONST_PTR(args), CALL_FCALL);
1135 RB_GC_GUARD(args);
1136 return ret;
1137 }
1138 argv = ALLOCA_N(VALUE, argc);
1139 MEMCPY(argv, RARRAY_CONST_PTR(args), VALUE, argc);
1140
1141 return rb_funcallv(recv, mid, argc, argv);
1142}
1143
1144#ifdef rb_funcall
1145#undef rb_funcall
1146#endif
1147
1148VALUE
1149rb_funcall(VALUE recv, ID mid, int n, ...)
1150{
1151 VALUE *argv;
1152 va_list ar;
1153
1154 if (n > 0) {
1155 long i;
1156
1157 va_start(ar, n);
1158
1159 argv = ALLOCA_N(VALUE, n);
1160
1161 for (i = 0; i < n; i++) {
1162 argv[i] = va_arg(ar, VALUE);
1163 }
1164 va_end(ar);
1165 }
1166 else {
1167 argv = 0;
1168 }
1169 return rb_funcallv(recv, mid, n, argv);
1170}
1171
1182VALUE
1183rb_check_funcall_basic_kw(VALUE recv, ID mid, VALUE ancestor, int argc, const VALUE *argv, int kw_splat)
1184{
1185 const rb_callable_method_entry_t *cme;
1187 VALUE klass = CLASS_OF(recv);
1188 if (!klass) return Qundef; /* hidden object */
1189
1190 cme = rb_callable_method_entry(klass, mid);
1191 if (cme && METHOD_ENTRY_BASIC(cme) && RBASIC_CLASS(cme->defined_class) == ancestor) {
1192 ec = GET_EC();
1193 return rb_vm_call0(ec, recv, mid, argc, argv, cme, kw_splat);
1194 }
1195
1196 return Qundef;
1197}
1198
1199VALUE
1200rb_funcallv_public(VALUE recv, ID mid, int argc, const VALUE *argv)
1201{
1202 return rb_funcallv_scope(recv, mid, argc, argv, CALL_PUBLIC);
1203}
1204
1205VALUE
1206rb_funcallv_public_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
1207{
1208 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_PUBLIC_KW : CALL_PUBLIC);
1209}
1210
1211VALUE
1212rb_funcall_passing_block(VALUE recv, ID mid, int argc, const VALUE *argv)
1213{
1214 PASS_PASSED_BLOCK_HANDLER();
1215 return rb_funcallv_public(recv, mid, argc, argv);
1216}
1217
1218VALUE
1219rb_funcall_passing_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
1220{
1221 PASS_PASSED_BLOCK_HANDLER();
1222 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_PUBLIC_KW : CALL_PUBLIC);
1223}
1224
1225VALUE
1226rb_funcall_with_block(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE passed_procval)
1227{
1228 if (!NIL_P(passed_procval)) {
1229 vm_passed_block_handler_set(GET_EC(), passed_procval);
1230 }
1231
1232 return rb_funcallv_public(recv, mid, argc, argv);
1233}
1234
1235VALUE
1236rb_funcall_with_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE passed_procval, int kw_splat)
1237{
1238 if (!NIL_P(passed_procval)) {
1239 vm_passed_block_handler_set(GET_EC(), passed_procval);
1240 }
1241
1242 return rb_call(recv, mid, argc, argv, kw_splat ? CALL_PUBLIC_KW : CALL_PUBLIC);
1243}
1244
1245static VALUE *
1246current_vm_stack_arg(const rb_execution_context_t *ec, const VALUE *argv)
1247{
1248 rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
1249 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, prev_cfp)) return NULL;
1250 if (prev_cfp->sp + 1 != argv) return NULL;
1251 return prev_cfp->sp + 1;
1252}
1253
1254static VALUE
1255send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope)
1256{
1257 ID id;
1258 VALUE vid;
1259 VALUE self;
1260 VALUE ret, vargv = 0;
1261 rb_execution_context_t *ec = GET_EC();
1262 int public = scope == CALL_PUBLIC || scope == CALL_PUBLIC_KW;
1263
1264 if (public) {
1265 self = Qundef;
1266 }
1267 else {
1268 self = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp)->self;
1269 }
1270
1271 if (argc == 0) {
1272 rb_raise(rb_eArgError, "no method name given");
1273 }
1274
1275 vid = *argv;
1276
1277 id = rb_check_id(&vid);
1278 if (!id) {
1279 if (rb_method_basic_definition_p(CLASS_OF(recv), idMethodMissing)) {
1280 VALUE exc = rb_make_no_method_exception(rb_eNoMethodError, 0,
1281 recv, argc, argv,
1282 !public);
1283 rb_exc_raise(exc);
1284 }
1285 if (!SYMBOL_P(*argv)) {
1286 VALUE *tmp_argv = current_vm_stack_arg(ec, argv);
1287 vid = rb_str_intern(vid);
1288 if (tmp_argv) {
1289 tmp_argv[0] = vid;
1290 }
1291 else if (argc > 1) {
1292 tmp_argv = ALLOCV_N(VALUE, vargv, argc);
1293 tmp_argv[0] = vid;
1294 MEMCPY(tmp_argv+1, argv+1, VALUE, argc-1);
1295 argv = tmp_argv;
1296 }
1297 else {
1298 argv = &vid;
1299 }
1300 }
1301 id = idMethodMissing;
1302 ec->method_missing_reason = MISSING_NOENTRY;
1303 }
1304 else {
1305 argv++; argc--;
1306 }
1307 PASS_PASSED_BLOCK_HANDLER_EC(ec);
1308 ret = rb_call0(ec, recv, id, argc, argv, scope, self);
1309 ALLOCV_END(vargv);
1310 return ret;
1311}
1312
1313static VALUE
1314send_internal_kw(int argc, const VALUE *argv, VALUE recv, call_type scope)
1315{
1316 if (rb_keyword_given_p()) {
1317 switch (scope) {
1318 case CALL_PUBLIC:
1319 scope = CALL_PUBLIC_KW;
1320 break;
1321 case CALL_FCALL:
1322 scope = CALL_FCALL_KW;
1323 break;
1324 default:
1325 break;
1326 }
1327 }
1328 return send_internal(argc, argv, recv, scope);
1329}
1330
1331/*
1332 * call-seq:
1333 * foo.send(symbol [, args...]) -> obj
1334 * foo.__send__(symbol [, args...]) -> obj
1335 * foo.send(string [, args...]) -> obj
1336 * foo.__send__(string [, args...]) -> obj
1337 *
1338 * Invokes the method identified by _symbol_, passing it any
1339 * arguments specified.
1340 * When the method is identified by a string, the string is converted
1341 * to a symbol.
1342 *
1343 * BasicObject implements +__send__+, Kernel implements +send+.
1344 * <code>__send__</code> is safer than +send+
1345 * when _obj_ has the same method name like <code>Socket</code>.
1346 * See also <code>public_send</code>.
1347 *
1348 * class Klass
1349 * def hello(*args)
1350 * "Hello " + args.join(' ')
1351 * end
1352 * end
1353 * k = Klass.new
1354 * k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
1355 */
1356
1357VALUE
1358rb_f_send(int argc, VALUE *argv, VALUE recv)
1359{
1360 return send_internal_kw(argc, argv, recv, CALL_FCALL);
1361}
1362
1363/*
1364 * call-seq:
1365 * obj.public_send(symbol [, args...]) -> obj
1366 * obj.public_send(string [, args...]) -> obj
1367 *
1368 * Invokes the method identified by _symbol_, passing it any
1369 * arguments specified. Unlike send, public_send calls public
1370 * methods only.
1371 * When the method is identified by a string, the string is converted
1372 * to a symbol.
1373 *
1374 * 1.public_send(:puts, "hello") # causes NoMethodError
1375 */
1376
1377static VALUE
1378rb_f_public_send(int argc, VALUE *argv, VALUE recv)
1379{
1380 return send_internal_kw(argc, argv, recv, CALL_PUBLIC);
1381}
1382
1383/* yield */
1384
1385static inline VALUE
1386rb_yield_0_kw(int argc, const VALUE * argv, int kw_splat)
1387{
1388 return vm_yield(GET_EC(), argc, argv, kw_splat);
1389}
1390
1391static inline VALUE
1392rb_yield_0(int argc, const VALUE * argv)
1393{
1394 return vm_yield(GET_EC(), argc, argv, RB_NO_KEYWORDS);
1395}
1396
1397VALUE
1398rb_yield_1(VALUE val)
1399{
1400 return rb_yield_0(1, &val);
1401}
1402
1403VALUE
1405{
1406 if (UNDEF_P(val)) {
1407 return rb_yield_0(0, NULL);
1408 }
1409 else {
1410 return rb_yield_0(1, &val);
1411 }
1412}
1413
1414VALUE
1415rb_ec_yield(rb_execution_context_t *ec, VALUE val)
1416{
1417 if (UNDEF_P(val)) {
1418 return vm_yield(ec, 0, NULL, RB_NO_KEYWORDS);
1419 }
1420 else {
1421 return vm_yield(ec, 1, &val, RB_NO_KEYWORDS);
1422 }
1423}
1424
1425#undef rb_yield_values
1426VALUE
1428{
1429 if (n == 0) {
1430 return rb_yield_0(0, 0);
1431 }
1432 else {
1433 int i;
1434 VALUE *argv;
1435 va_list args;
1436 argv = ALLOCA_N(VALUE, n);
1437
1438 va_start(args, n);
1439 for (i=0; i<n; i++) {
1440 argv[i] = va_arg(args, VALUE);
1441 }
1442 va_end(args);
1443
1444 return rb_yield_0(n, argv);
1445 }
1446}
1447
1448VALUE
1449rb_yield_values2(int argc, const VALUE *argv)
1450{
1451 return rb_yield_0(argc, argv);
1452}
1453
1454VALUE
1455rb_yield_values_kw(int argc, const VALUE *argv, int kw_splat)
1456{
1457 return rb_yield_0_kw(argc, argv, kw_splat);
1458}
1459
1460VALUE
1462{
1463 VALUE tmp = rb_check_array_type(values);
1464 VALUE v;
1465 if (NIL_P(tmp)) {
1466 rb_raise(rb_eArgError, "not an array");
1467 }
1468 v = rb_yield_0(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp));
1469 RB_GC_GUARD(tmp);
1470 return v;
1471}
1472
1473VALUE
1474rb_yield_splat_kw(VALUE values, int kw_splat)
1475{
1476 VALUE tmp = rb_check_array_type(values);
1477 VALUE v;
1478 if (NIL_P(tmp)) {
1479 rb_raise(rb_eArgError, "not an array");
1480 }
1481 v = rb_yield_0_kw(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp), kw_splat);
1482 RB_GC_GUARD(tmp);
1483 return v;
1484}
1485
1486VALUE
1487rb_yield_force_blockarg(VALUE values)
1488{
1489 return vm_yield_force_blockarg(GET_EC(), values);
1490}
1491
1492VALUE
1494{
1495 return vm_yield_with_block(GET_EC(), argc, argv,
1496 NIL_P(blockarg) ? VM_BLOCK_HANDLER_NONE : blockarg,
1498}
1499
1500#if VMDEBUG
1501static const char *
1502vm_frametype_name(const rb_control_frame_t *cfp);
1503#endif
1504
1505static VALUE
1506rb_iterate0(VALUE (* it_proc) (VALUE), VALUE data1,
1507 const struct vm_ifunc *const ifunc,
1508 rb_execution_context_t *ec_arg)
1509{
1510 enum ruby_tag_type state;
1511 volatile VALUE retval = Qnil;
1512 rb_execution_context_t * volatile ec = ec_arg;
1513 rb_control_frame_t *volatile const cfp = ec->cfp;
1514
1515 EC_PUSH_TAG(ec);
1516 state = EC_EXEC_TAG();
1517 if (state == 0) {
1518 iter_retry:
1519 {
1520 VALUE block_handler;
1521
1522 if (ifunc) {
1523 struct rb_captured_block *captured = VM_CFP_TO_CAPTURED_BLOCK(cfp);
1524 captured->code.ifunc = ifunc;
1525 block_handler = VM_BH_FROM_IFUNC_BLOCK(captured);
1526 }
1527 else {
1528 block_handler = VM_CF_BLOCK_HANDLER(cfp);
1529 }
1530 vm_passed_block_handler_set(ec, block_handler);
1531 }
1532 retval = (*it_proc) (data1);
1533 }
1534 else if (state == TAG_BREAK || state == TAG_RETRY) {
1535 const struct vm_throw_data *const err = (struct vm_throw_data *)ec->errinfo;
1536 const rb_control_frame_t *const escape_cfp = THROW_DATA_CATCH_FRAME(err);
1537
1538 if (cfp == escape_cfp) {
1539 rb_vm_rewind_cfp(ec, cfp);
1540
1541 state = 0;
1542 ec->tag->state = TAG_NONE;
1543 ec->errinfo = Qnil;
1544
1545 if (state == TAG_RETRY) goto iter_retry;
1546 retval = THROW_DATA_VAL(err);
1547 }
1548 else if (0) {
1549 SDR(); fprintf(stderr, "%p, %p\n", (void *)cfp, (void *)escape_cfp);
1550 }
1551 }
1552 EC_POP_TAG();
1553
1554 if (state) {
1555 EC_JUMP_TAG(ec, state);
1556 }
1557 return retval;
1558}
1559
1560static VALUE
1561rb_iterate_internal(VALUE (* it_proc)(VALUE), VALUE data1,
1562 rb_block_call_func_t bl_proc, VALUE data2)
1563{
1564 return rb_iterate0(it_proc, data1,
1565 bl_proc ? rb_vm_ifunc_proc_new(bl_proc, (void *)data2) : 0,
1566 GET_EC());
1567}
1568
1570 VALUE obj;
1571 ID mid;
1572 int argc;
1573 const VALUE *argv;
1574 int kw_splat;
1575};
1576
1577static VALUE
1578iterate_method(VALUE obj)
1579{
1580 const struct iter_method_arg * arg =
1581 (struct iter_method_arg *) obj;
1582
1583 return rb_call(arg->obj, arg->mid, arg->argc, arg->argv, arg->kw_splat ? CALL_FCALL_KW : CALL_FCALL);
1584}
1585
1586VALUE 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);
1587
1588VALUE
1589rb_block_call(VALUE obj, ID mid, int argc, const VALUE * argv,
1590 rb_block_call_func_t bl_proc, VALUE data2)
1591{
1592 return rb_block_call_kw(obj, mid, argc, argv, bl_proc, data2, RB_NO_KEYWORDS);
1593}
1594
1595VALUE
1596rb_block_call_kw(VALUE obj, ID mid, int argc, const VALUE * argv,
1597 rb_block_call_func_t bl_proc, VALUE data2, int kw_splat)
1598{
1599 struct iter_method_arg arg;
1600
1601 arg.obj = obj;
1602 arg.mid = mid;
1603 arg.argc = argc;
1604 arg.argv = argv;
1605 arg.kw_splat = kw_splat;
1606 return rb_iterate_internal(iterate_method, (VALUE)&arg, bl_proc, data2);
1607}
1608
1609/*
1610 * A flexible variant of rb_block_call and rb_block_call_kw.
1611 * This function accepts flags:
1612 *
1613 * RB_NO_KEYWORDS, RB_PASS_KEYWORDS, RB_PASS_CALLED_KEYWORDS:
1614 * Works as the same as rb_block_call_kw.
1615 *
1616 * RB_BLOCK_NO_USE_PACKED_ARGS:
1617 * The given block ("bl_proc") does not use "yielded_arg" of rb_block_call_func_t.
1618 * Instead, the block accesses the yielded arguments via "argc" and "argv".
1619 * This flag allows the called method to yield arguments without allocating an Array.
1620 */
1621VALUE
1622rb_block_call2(VALUE obj, ID mid, int argc, const VALUE *argv,
1623 rb_block_call_func_t bl_proc, VALUE data2, long flags)
1624{
1625 struct iter_method_arg arg;
1626
1627 arg.obj = obj;
1628 arg.mid = mid;
1629 arg.argc = argc;
1630 arg.argv = argv;
1631 arg.kw_splat = flags & 1;
1632
1633 struct vm_ifunc *ifunc = rb_vm_ifunc_proc_new(bl_proc, (void *)data2);
1634 if (flags & RB_BLOCK_NO_USE_PACKED_ARGS)
1635 ifunc->flags |= IFUNC_YIELD_OPTIMIZABLE;
1636
1637 return rb_iterate0(iterate_method, (VALUE)&arg, ifunc, GET_EC());
1638}
1639
1640VALUE
1641rb_lambda_call(VALUE obj, ID mid, int argc, const VALUE *argv,
1642 rb_block_call_func_t bl_proc, int min_argc, int max_argc,
1643 VALUE data2)
1644{
1645 struct iter_method_arg arg;
1646 struct vm_ifunc *block;
1647
1648 if (!bl_proc) rb_raise(rb_eArgError, "NULL lambda function");
1649 arg.obj = obj;
1650 arg.mid = mid;
1651 arg.argc = argc;
1652 arg.argv = argv;
1653 arg.kw_splat = 0;
1654 block = rb_vm_ifunc_new(bl_proc, (void *)data2, min_argc, max_argc);
1655 return rb_iterate0(iterate_method, (VALUE)&arg, block, GET_EC());
1656}
1657
1658static VALUE
1659iterate_check_method(VALUE obj)
1660{
1661 const struct iter_method_arg * arg =
1662 (struct iter_method_arg *) obj;
1663
1664 return rb_check_funcall(arg->obj, arg->mid, arg->argc, arg->argv);
1665}
1666
1667VALUE
1668rb_check_block_call(VALUE obj, ID mid, int argc, const VALUE *argv,
1669 rb_block_call_func_t bl_proc, VALUE data2)
1670{
1671 struct iter_method_arg arg;
1672
1673 arg.obj = obj;
1674 arg.mid = mid;
1675 arg.argc = argc;
1676 arg.argv = argv;
1677 arg.kw_splat = 0;
1678 return rb_iterate_internal(iterate_check_method, (VALUE)&arg, bl_proc, data2);
1679}
1680
1681VALUE
1683{
1684 return rb_call(obj, idEach, 0, 0, CALL_FCALL);
1685}
1686
1687static VALUE eval_default_path = Qfalse;
1688
1689#define EVAL_LOCATION_MARK "eval at "
1690#define EVAL_LOCATION_MARK_LEN (int)rb_strlen_lit(EVAL_LOCATION_MARK)
1691
1692static VALUE
1693get_eval_default_path(void)
1694{
1695 int location_lineno;
1696 VALUE location_path = rb_source_location(&location_lineno);
1697 if (!NIL_P(location_path)) {
1698 return rb_fstring(rb_sprintf("("EVAL_LOCATION_MARK"%"PRIsVALUE":%d)",
1699 location_path, location_lineno));
1700 }
1701
1702 if (!eval_default_path) {
1703 eval_default_path = rb_fstring_lit("(eval)");
1704 rb_vm_register_global_object(eval_default_path);
1705 }
1706 return eval_default_path;
1707}
1708
1709static inline int
1710compute_isolated_depth_from_ep(const VALUE *ep)
1711{
1712 int depth = 1;
1713 while (1) {
1714 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ISOLATED)) return depth;
1715 if (VM_ENV_LOCAL_P(ep)) return 0;
1716 ep = VM_ENV_PREV_EP(ep);
1717 depth++;
1718 }
1719}
1720
1721static inline int
1722compute_isolated_depth_from_block(const struct rb_block *blk)
1723{
1724 return compute_isolated_depth_from_ep(vm_block_ep(blk));
1725}
1726
1727static const rb_iseq_t *
1728pm_eval_make_iseq(VALUE src, VALUE fname, int line,
1729 const struct rb_block *base_block)
1730{
1731 const rb_iseq_t *const parent = vm_block_iseq(base_block);
1732 const rb_iseq_t *iseq = parent;
1733 VALUE name = rb_fstring_lit("<compiled>");
1734
1735 int coverage_enabled = ((rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) != 0) ? 1 : 0;
1736 int isolated_depth = compute_isolated_depth_from_block(base_block);
1737
1738 if (!fname) {
1739 fname = rb_source_location(&line);
1740 }
1741
1742 if (!UNDEF_P(fname)) {
1743 if (!NIL_P(fname)) fname = rb_fstring(fname);
1744 }
1745 else {
1746 fname = get_eval_default_path();
1747 coverage_enabled = 0;
1748 }
1749
1750 pm_parse_result_t result;
1751 pm_parse_result_init(&result);
1752 pm_options_line_set(result.options, line);
1753 result.node.coverage_enabled = coverage_enabled;
1754
1755 // Count scopes, one for each parent iseq, plus one for our local scope.
1756 // Also total the byte lengths of every local name so that they can all be
1757 // copied into a single buffer.
1758 int scopes_count = 0;
1759 size_t names_size = 0;
1760
1761 do {
1762 scopes_count++;
1763
1764 int locals_count = ISEQ_BODY(iseq)->local_table_size;
1765 for (int local_index = 0; local_index < locals_count; local_index++) {
1766 ID local = ISEQ_BODY(iseq)->local_table[local_index];
1767 if (rb_is_local_id(local)) names_size += RSTRING_LEN(rb_id2str(local));
1768 }
1769 } while ((iseq = ISEQ_BODY(iseq)->parent_iseq));
1770 pm_options_scopes_init(result.options, scopes_count + 1);
1771
1772 // Walk over the scope tree, adding known locals at the correct depths. The
1773 // scope array should be deepest -> shallowest. so lower indexes in the
1774 // scopes array refer to root nodes on the tree, and higher indexes are the
1775 // leaf nodes.
1776 iseq = parent;
1777
1778#define FORWARDING_POSITIONALS_CHR '*'
1779#define FORWARDING_POSITIONALS_STR "*"
1780#define FORWARDING_KEYWORDS_CHR ':'
1781#define FORWARDING_KEYWORDS_STR ":"
1782#define FORWARDING_BLOCK_CHR '&'
1783#define FORWARDING_BLOCK_STR "&"
1784#define FORWARDING_ALL_CHR '.'
1785#define FORWARDING_ALL_STR "."
1786
1787 /* The scope locals are views into this buffer, so it must outlive every
1788 * read of them, including building the parent scope nodes after parsing. */
1789 uint8_t *names_buffer = names_size > 0 ? xmalloc(names_size) : NULL;
1790 uint8_t *names_cursor = names_buffer;
1791
1792 for (int scopes_index = 0; scopes_index < scopes_count; scopes_index++) {
1793 VALUE iseq_value = (VALUE)iseq;
1794 int locals_count = ISEQ_BODY(iseq)->local_table_size;
1795
1796 pm_options_scope_t *options_scope = pm_options_scope_mut(result.options, scopes_count - scopes_index - 1);
1797 pm_options_scope_init(options_scope, locals_count);
1798
1799 uint8_t forwarding = PM_OPTIONS_SCOPE_FORWARDING_NONE;
1800
1801 for (int local_index = 0; local_index < locals_count; local_index++) {
1802 pm_string_t *scope_local = pm_options_scope_local_mut(options_scope, local_index);
1803 ID local = ISEQ_BODY(iseq)->local_table[local_index];
1804
1805 if (rb_is_local_id(local)) {
1806 VALUE name = rb_id2str(local);
1807 size_t length = RSTRING_LEN(name);
1808
1809 MEMCPY(names_cursor, RSTRING_PTR(name), char, length);
1810 RB_GC_GUARD(name);
1811
1812 pm_string_init(scope_local, names_cursor, length);
1813 names_cursor += length;
1814 }
1815 else if (local == idMULT) {
1817 pm_string_init(scope_local, (const uint8_t *) FORWARDING_POSITIONALS_STR, 1);
1818 }
1819 else if (local == idPow) {
1821 pm_string_init(scope_local, (const uint8_t *) FORWARDING_KEYWORDS_STR, 1);
1822 }
1823 else if (local == idAnd) {
1825 pm_string_init(scope_local, (const uint8_t *) FORWARDING_BLOCK_STR, 1);
1826 }
1827 else if (local == idDot3) {
1828 forwarding |= PM_OPTIONS_SCOPE_FORWARDING_ALL;
1829 pm_string_init(scope_local, (const uint8_t *) FORWARDING_ALL_STR, 1);
1830 }
1831 }
1832
1833 pm_options_scope_forwarding_set(options_scope, forwarding);
1834 iseq = ISEQ_BODY(iseq)->parent_iseq;
1835
1836 /* We need to GC guard the iseq because the code above malloc memory
1837 * which could trigger a GC. Since we only use ISEQ_BODY, the compiler
1838 * may optimize out the iseq local variable so we need to GC guard it. */
1839 RB_GC_GUARD(iseq_value);
1840 }
1841
1842 // Add our empty local scope at the very end of the array for our eval
1843 // scope's locals.
1844 pm_options_scope_init(pm_options_scope_mut(result.options, scopes_count), 0);
1845
1846 VALUE script_lines;
1847 VALUE error = pm_parse_string(&result, src, fname, ruby_vm_keep_script_lines ? &script_lines : NULL);
1848
1849 // If the parse failed, clean up and raise.
1850 if (error != Qnil) {
1851 if (names_buffer) SIZED_FREE_N(names_buffer, names_size);
1852 pm_parse_result_free(&result);
1853 rb_exc_raise(error);
1854 }
1855
1856 // Create one scope node for each scope passed in, initialize the local
1857 // lookup table with all the local variable information attached to the
1858 // scope used by the parser.
1859 pm_scope_node_t *node = &result.node;
1860 iseq = parent;
1861
1862 for (int scopes_index = 0; scopes_index < scopes_count; scopes_index++) {
1863 pm_scope_node_t *parent_scope = ruby_xcalloc(1, sizeof(pm_scope_node_t));
1864 RUBY_ASSERT(parent_scope != NULL);
1865
1866 const pm_options_scope_t *options_scope = pm_options_scope(result.options, scopes_count - scopes_index - 1);
1867 parent_scope->coverage_enabled = coverage_enabled;
1868 parent_scope->parser = result.parser;
1869 int locals_count = ISEQ_BODY(iseq)->local_table_size;
1870 pm_index_lookup_table_init_heap(&parent_scope->index_lookup_table, (int) pm_parser_constants_size(result.parser));
1871 parent_scope->local_table_for_iseq_size = locals_count;
1872 pm_constant_id_list_init(&parent_scope->locals);
1873
1874 for (int local_index = 0; local_index < locals_count; local_index++) {
1875 const pm_string_t *scope_local = pm_options_scope_local(options_scope, local_index);
1876 pm_constant_id_t constant_id = 0;
1877
1878 const uint8_t *source = pm_string_source(scope_local);
1879 size_t length = pm_string_length(scope_local);
1880
1881 if (length > 0) {
1882 if (length == 1) {
1883 switch (*source) {
1884 case FORWARDING_POSITIONALS_CHR:
1885 constant_id = PM_CONSTANT_MULT;
1886 break;
1887 case FORWARDING_KEYWORDS_CHR:
1888 constant_id = PM_CONSTANT_POW;
1889 break;
1890 case FORWARDING_BLOCK_CHR:
1891 constant_id = PM_CONSTANT_AND;
1892 break;
1893 case FORWARDING_ALL_CHR:
1894 constant_id = PM_CONSTANT_DOT3;
1895 break;
1896 default:
1897 constant_id = pm_parser_constant_find(result.parser, source, length);
1898 break;
1899 }
1900 }
1901 else {
1902 constant_id = pm_parser_constant_find(result.parser, source, length);
1903 }
1904
1905 pm_index_lookup_table_insert(&parent_scope->index_lookup_table, constant_id, local_index);
1906 }
1907
1908 pm_constant_id_list_append(result.arena, &parent_scope->locals, constant_id);
1909 }
1910
1911 node->previous = parent_scope;
1912 node = parent_scope;
1913 iseq = ISEQ_BODY(iseq)->parent_iseq;
1914 }
1915
1916 if (names_buffer) SIZED_FREE_N(names_buffer, names_size);
1917
1918#undef FORWARDING_POSITIONALS_CHR
1919#undef FORWARDING_POSITIONALS_STR
1920#undef FORWARDING_KEYWORDS_CHR
1921#undef FORWARDING_KEYWORDS_STR
1922#undef FORWARDING_BLOCK_CHR
1923#undef FORWARDING_BLOCK_STR
1924#undef FORWARDING_ALL_CHR
1925#undef FORWARDING_ALL_STR
1926
1927 int error_state;
1928 iseq = pm_iseq_new_eval(&result.node, name, fname, Qnil, line, parent, isolated_depth, &error_state);
1929
1930 pm_scope_node_t *prev = result.node.previous;
1931 while (prev) {
1932 pm_scope_node_t *next = prev->previous;
1933 pm_scope_node_destroy(prev);
1934 SIZED_FREE(prev);
1935 prev = next;
1936 }
1937
1938 pm_parse_result_free(&result);
1939
1940 // If there was an error, raise it after memory has been cleaned up
1941 if (error_state) {
1942 RUBY_ASSERT(iseq == NULL);
1943 rb_jump_tag(error_state);
1944 }
1945
1946 rb_exec_event_hook_script_compiled(GET_EC(), iseq, src);
1947
1948 return iseq;
1949}
1950
1951static const rb_iseq_t *
1952eval_make_iseq(VALUE src, VALUE fname, int line,
1953 const struct rb_block *base_block)
1954{
1955 if (rb_ruby_prism_p()) {
1956 return pm_eval_make_iseq(src, fname, line, base_block);
1957 }
1958 const VALUE parser = rb_parser_new();
1959 const rb_iseq_t *const parent = vm_block_iseq(base_block);
1960 rb_iseq_t *iseq = NULL;
1961 VALUE ast_value;
1962 rb_ast_t *ast;
1963
1964 int coverage_enabled = (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) != 0;
1965 int isolated_depth = compute_isolated_depth_from_block(base_block);
1966
1967 if (!fname) {
1968 fname = rb_source_location(&line);
1969 }
1970
1971 if (!UNDEF_P(fname)) {
1972 if (!NIL_P(fname)) fname = rb_fstring(fname);
1973 }
1974 else {
1975 fname = get_eval_default_path();
1976 coverage_enabled = FALSE;
1977 }
1978
1979 rb_parser_set_context(parser, parent, FALSE);
1980 if (ruby_vm_keep_script_lines) rb_parser_set_script_lines(parser);
1981 ast_value = rb_parser_compile_string_path(parser, fname, src, line);
1982
1983 ast = rb_ruby_ast_data_get(ast_value);
1984
1985 if (ast->body.root) {
1986 ast->body.coverage_enabled = coverage_enabled;
1987 iseq = rb_iseq_new_eval(ast_value,
1988 ISEQ_BODY(parent)->location.label,
1989 fname, Qnil, line,
1990 parent, isolated_depth);
1991 }
1992 rb_ast_dispose(ast);
1993
1994 if (iseq != NULL) {
1995 if (0 && iseq) { /* for debug */
1996 VALUE disasm = rb_iseq_disasm(iseq);
1997 printf("%s\n", StringValuePtr(disasm));
1998 }
1999
2000 rb_exec_event_hook_script_compiled(GET_EC(), iseq, src);
2001 }
2002
2003 return iseq;
2004}
2005
2006static VALUE
2007eval_string_with_cref(VALUE self, VALUE src, rb_cref_t *cref, VALUE file, int line)
2008{
2009 rb_execution_context_t *ec = GET_EC();
2010 struct rb_block block;
2011 const rb_iseq_t *iseq;
2012 rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
2013 if (!cfp) {
2014 rb_raise(rb_eRuntimeError, "Can't eval on top of Fiber or Thread");
2015 }
2016
2017 block.as.captured = *VM_CFP_TO_CAPTURED_BLOCK(cfp);
2018 block.as.captured.self = self;
2019 block.as.captured.code.iseq = CFP_ISEQ(cfp);
2020 block.type = block_type_iseq;
2021
2022 // EP is not escaped to the heap here, but captured and reused by another frame.
2023 // ZJIT's locals are incompatible with it unlike YJIT's, so invalidate the ISEQ for ZJIT.
2024 if (rb_zjit_enabled_p) rb_zjit_invalidate_no_ep_escape(CFP_ISEQ(cfp));
2025
2026 iseq = eval_make_iseq(src, file, line, &block);
2027 if (!iseq) {
2028 rb_exc_raise(ec->errinfo);
2029 }
2030
2031 /* TODO: what the code checking? */
2032 if (!cref && block.as.captured.code.val) {
2033 rb_cref_t *orig_cref = vm_get_cref(vm_block_ep(&block));
2034 cref = rb_vm_cref_dup(orig_cref);
2035 }
2036 vm_set_eval_stack(ec, iseq, cref, &block);
2037
2038 /* kick */
2039 return vm_exec(ec);
2040}
2041
2042static VALUE
2043eval_string_with_scope(VALUE scope, VALUE src, VALUE file, int line)
2044{
2045 rb_execution_context_t *ec = GET_EC();
2046 rb_binding_t *bind = Check_TypedStruct(scope, &ruby_binding_data_type);
2047 const rb_iseq_t *iseq = eval_make_iseq(src, file, line, &bind->block);
2048 if (!iseq) {
2049 rb_exc_raise(ec->errinfo);
2050 }
2051
2052 vm_set_eval_stack(ec, iseq, NULL, &bind->block);
2053
2054 /* save new env */
2055 if (ISEQ_BODY(iseq)->local_table_size > 0) {
2056 vm_bind_update_env(scope, bind, vm_make_env_object(ec, ec->cfp));
2057 }
2058
2059 /* kick */
2060 return vm_exec(ec);
2061}
2062
2063/*
2064 * call-seq:
2065 * eval(string, binding = nil, filename = default_filename, lineno = 1) -> obj
2066 *
2067 * Evaluates the Ruby expression(s) in +string+. Returns the result of the last
2068 * expression.
2069 *
2070 * str = "Hello"
2071 * eval("str + ' World'") # => "Hello World"
2072 *
2073 * If +binding+ is given, which must be a Binding object, the
2074 * evaluation is performed in its context. Otherwise, the
2075 * evaluation is performed in the context of the caller.
2076 *
2077 * def get_binding(str) = binding
2078 * str = "Hello"
2079 * eval("str + ' World'", get_binding("Bye")) # => "Bye World"
2080 *
2081 * If the optional +filename+ is given, it will be used as the
2082 * filename of the evaluation (for <tt>__FILE__</tt> and errors).
2083 * Otherwise, it will default to <tt>(eval at __FILE__:__LINE__)</tt>
2084 * where <tt>__FILE__</tt> and <tt>__LINE__</tt> are the filename and
2085 * line number of the caller, respectively.
2086 *
2087 * eval("puts __FILE__") # => "(eval at test.rb:1)"
2088 * eval("puts __FILE__", nil, "foobar.rb") # => "foobar.rb"
2089 *
2090 * If the optional +lineno+ is given, it will be used as the
2091 * line number of the evaluation (for <tt>__LINE__</tt> and errors).
2092 * Otherwise, it will default to 1.
2093 *
2094 * eval("puts __LINE__") # => 1
2095 * eval("puts __LINE__", nil, "foobar.rb", 10) # => 10
2096 */
2097
2098VALUE
2099rb_f_eval(int argc, const VALUE *argv, VALUE self)
2100{
2101 VALUE src, scope, vfile, vline;
2102 VALUE file = Qundef;
2103 int line = 1;
2104
2105 rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
2106 StringValue(src);
2107 if (argc >= 3) {
2108 StringValue(vfile);
2109 }
2110 if (argc >= 4) {
2111 line = NUM2INT(vline);
2112 }
2113
2114 if (!NIL_P(vfile))
2115 file = vfile;
2116
2117 if (NIL_P(scope))
2118 return eval_string_with_cref(self, src, NULL, file, line);
2119 else
2120 return eval_string_with_scope(scope, src, file, line);
2121}
2122
2124VALUE
2125ruby_eval_string_from_file(const char *str, const char *filename)
2126{
2127 VALUE file = filename ? rb_str_new_cstr(filename) : 0;
2128 rb_execution_context_t *ec = GET_EC();
2129 rb_control_frame_t *cfp = ec ? rb_vm_get_ruby_level_next_cfp(ec, ec->cfp) : NULL;
2130 VALUE self = cfp ? cfp->self : rb_vm_top_self();
2131 return eval_string_with_cref(self, rb_str_new2(str), NULL, file, 1);
2132}
2133
2134VALUE
2135rb_eval_string(const char *str)
2136{
2137 return ruby_eval_string_from_file(str, "eval");
2138}
2139
2140static VALUE
2141eval_string_protect(VALUE str)
2142{
2143 return rb_eval_string((char *)str);
2144}
2145
2146VALUE
2147rb_eval_string_protect(const char *str, int *pstate)
2148{
2149 return rb_protect(eval_string_protect, (VALUE)str, pstate);
2150}
2151
2153 VALUE top_self;
2154 VALUE klass;
2155 const char *str;
2156};
2157
2158static VALUE
2159eval_string_wrap_protect(VALUE data)
2160{
2161 const struct eval_string_wrap_arg *const arg = (struct eval_string_wrap_arg*)data;
2162 rb_cref_t *cref = rb_vm_cref_new_toplevel();
2163 cref->klass_or_self = arg->klass;
2164 return eval_string_with_cref(arg->top_self, rb_str_new_cstr(arg->str), cref, rb_str_new_cstr("eval"), 1);
2165}
2166
2167VALUE
2168rb_eval_string_wrap(const char *str, int *pstate)
2169{
2170 int state;
2171 rb_thread_t *th = GET_THREAD();
2172 VALUE self = th->top_self;
2173 VALUE wrapper = th->top_wrapper;
2174 VALUE val;
2175 struct eval_string_wrap_arg data;
2176
2177 th->top_wrapper = rb_module_new();
2178 th->top_self = rb_obj_clone(rb_vm_top_self());
2179 rb_extend_object(th->top_self, th->top_wrapper);
2180
2181 data.top_self = th->top_self;
2182 data.klass = th->top_wrapper;
2183 data.str = str;
2184
2185 val = rb_protect(eval_string_wrap_protect, (VALUE)&data, &state);
2186
2187 th->top_self = self;
2188 th->top_wrapper = wrapper;
2189
2190 if (pstate) {
2191 *pstate = state;
2192 }
2193 else if (state != TAG_NONE) {
2194 EC_JUMP_TAG(th->ec, state);
2195 }
2196 return val;
2197}
2198
2199VALUE
2200rb_eval_cmd_kw(VALUE cmd, VALUE arg, int kw_splat)
2201{
2202 Check_Type(arg, T_ARRAY);
2203 int argc = RARRAY_LENINT(arg);
2204 const VALUE *argv = RARRAY_CONST_PTR(arg);
2205 VALUE val = rb_eval_cmd_call_kw(cmd, argc, argv, kw_splat);
2206 RB_GC_GUARD(arg);
2207 return val;
2208}
2209
2210VALUE
2211rb_eval_cmd_call_kw(VALUE cmd, int argc, const VALUE *argv, int kw_splat)
2212{
2213 enum ruby_tag_type state;
2214 volatile VALUE val = Qnil; /* OK */
2215 rb_execution_context_t * volatile ec = GET_EC();
2216
2217 EC_PUSH_TAG(ec);
2218 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2219 if (!RB_TYPE_P(cmd, T_STRING)) {
2220 val = rb_funcallv_kw(cmd, idCall, argc, argv, kw_splat);
2221 }
2222 else {
2223 val = eval_string_with_cref(rb_vm_top_self(), cmd, NULL, 0, 0);
2224 }
2225 }
2226 EC_POP_TAG();
2227
2228 if (state) EC_JUMP_TAG(ec, state);
2229 return val;
2230}
2231
2232/* block eval under the class/module context */
2233
2234static VALUE
2235yield_under(VALUE self, int singleton, int argc, const VALUE *argv, int kw_splat)
2236{
2237 rb_execution_context_t *ec = GET_EC();
2238 rb_control_frame_t *cfp = ec->cfp;
2239 VALUE block_handler = VM_CF_BLOCK_HANDLER(cfp);
2240 VALUE new_block_handler = 0;
2241 const struct rb_captured_block *captured = NULL;
2242 struct rb_captured_block new_captured;
2243 const VALUE *ep = NULL;
2244 rb_cref_t *cref;
2245 int is_lambda = FALSE;
2246 const rb_cref_t *proc_cref = NULL;
2247
2248 if (block_handler != VM_BLOCK_HANDLER_NONE) {
2249 again:
2250 switch (vm_block_handler_type(block_handler)) {
2251 case block_handler_type_iseq:
2252 captured = VM_BH_TO_CAPT_BLOCK(block_handler);
2253 new_captured = *captured;
2254 new_block_handler = VM_BH_FROM_ISEQ_BLOCK(&new_captured);
2255 break;
2256 case block_handler_type_ifunc:
2257 captured = VM_BH_TO_CAPT_BLOCK(block_handler);
2258 new_captured = *captured;
2259 new_block_handler = VM_BH_FROM_IFUNC_BLOCK(&new_captured);
2260 break;
2261 case block_handler_type_proc:
2262 {
2263 VALUE procval = VM_BH_TO_PROC(block_handler);
2264 rb_proc_t *po;
2265 GetProcPtr(procval, po);
2266 is_lambda = po->header.is_lambda;
2267 if (po->header.is_refined) proc_cref = rb_proc_refinements_cref_for_call(procval);
2268 block_handler = vm_block_to_block_handler(&po->block);
2269 }
2270 goto again;
2271 case block_handler_type_symbol:
2272 return rb_sym_proc_call(SYM2ID(VM_BH_TO_SYMBOL(block_handler)),
2273 argc, argv, kw_splat,
2274 VM_BLOCK_HANDLER_NONE);
2275 }
2276
2277 new_captured.self = self;
2278 ep = captured->ep;
2279
2280 VM_FORCE_WRITE_SPECIAL_CONST(&VM_CF_LEP(ec->cfp)[VM_ENV_DATA_INDEX_SPECVAL], new_block_handler);
2281 }
2282
2283 VM_ASSERT(singleton || RB_TYPE_P(self, T_MODULE) || RB_TYPE_P(self, T_CLASS));
2284 cref = vm_cref_push(ec, self, ep, TRUE, singleton);
2285
2286 if (proc_cref && !NIL_P(CREF_REFINEMENTS(proc_cref))) {
2287 CREF_REFINEMENTS_SET(cref, rb_hash_dup(CREF_REFINEMENTS(proc_cref)));
2288 CREF_REFINED_PROC_SET(cref);
2289 }
2290
2291 return vm_yield_with_cref(ec, argc, argv, kw_splat, cref, is_lambda);
2292}
2293
2294VALUE
2295rb_yield_refine_block(VALUE refinement, VALUE refinements)
2296{
2297 rb_execution_context_t *ec = GET_EC();
2298 VALUE block_handler = VM_CF_BLOCK_HANDLER(ec->cfp);
2299
2300 if (vm_block_handler_type(block_handler) != block_handler_type_iseq) {
2301 rb_bug("rb_yield_refine_block: an iseq block is required");
2302 }
2303 else {
2304 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(block_handler);
2305 struct rb_captured_block new_captured = *captured;
2306 const VALUE *const argv = &new_captured.self; /* dummy to suppress nonnull warning from gcc */
2307 VALUE new_block_handler = VM_BH_FROM_ISEQ_BLOCK(&new_captured);
2308 const VALUE *ep = captured->ep;
2309 rb_cref_t *cref = vm_cref_push(ec, refinement, ep, TRUE, FALSE);
2310 CREF_REFINEMENTS_SET(cref, refinements);
2311 VM_FORCE_WRITE_SPECIAL_CONST(&VM_CF_LEP(ec->cfp)[VM_ENV_DATA_INDEX_SPECVAL], new_block_handler);
2312 new_captured.self = refinement;
2313 return vm_yield_with_cref(ec, 0, argv, RB_NO_KEYWORDS, cref, FALSE);
2314 }
2315}
2316
2317/* string eval under the class/module context */
2318static VALUE
2319eval_under(VALUE self, int singleton, VALUE src, VALUE file, int line)
2320{
2321 rb_cref_t *cref = vm_cref_push(GET_EC(), self, NULL, FALSE, singleton);
2322 StringValue(src);
2323
2324 return eval_string_with_cref(self, src, cref, file, line);
2325}
2326
2327static VALUE
2328specific_eval(int argc, const VALUE *argv, VALUE self, int singleton, int kw_splat)
2329{
2330 if (rb_block_given_p()) {
2331 rb_check_arity(argc, 0, 0);
2332 return yield_under(self, singleton, 1, &self, kw_splat);
2333 }
2334 else {
2335 VALUE file = Qnil;
2336 int line = 1;
2337 VALUE code;
2338
2339 rb_check_arity(argc, 1, 3);
2340 code = argv[0];
2341 StringValue(code);
2342 if (argc > 2)
2343 line = NUM2INT(argv[2]);
2344 if (argc > 1) {
2345 file = argv[1];
2346 if (!NIL_P(file)) StringValue(file);
2347 }
2348
2349 if (NIL_P(file)) {
2350 file = get_eval_default_path();
2351 }
2352
2353 return eval_under(self, singleton, code, file, line);
2354 }
2355}
2356
2357/*
2358 * call-seq:
2359 * obj.instance_eval(string [, filename [, lineno]] ) -> obj
2360 * obj.instance_eval {|obj| block } -> obj
2361 *
2362 * Evaluates a string containing Ruby source code, or the given block,
2363 * within the context of the receiver (_obj_). In order to set the
2364 * context, the variable +self+ is set to _obj_ while
2365 * the code is executing, giving the code access to _obj_'s
2366 * instance variables and private methods.
2367 *
2368 * When <code>instance_eval</code> is given a block, _obj_ is also
2369 * passed in as the block's only argument.
2370 *
2371 * When <code>instance_eval</code> is given a +String+, the optional
2372 * second and third parameters supply a filename and starting line number
2373 * that are used when reporting compilation errors.
2374 *
2375 * class KlassWithSecret
2376 * def initialize
2377 * @secret = 99
2378 * end
2379 * private
2380 * def the_secret
2381 * "Ssssh! The secret is #{@secret}."
2382 * end
2383 * end
2384 * k = KlassWithSecret.new
2385 * k.instance_eval { @secret } #=> 99
2386 * k.instance_eval { the_secret } #=> "Ssssh! The secret is 99."
2387 * k.instance_eval {|obj| obj == self } #=> true
2388 */
2389
2390static VALUE
2391rb_obj_instance_eval_internal(int argc, const VALUE *argv, VALUE self)
2392{
2393 return specific_eval(argc, argv, self, TRUE, RB_PASS_CALLED_KEYWORDS);
2394}
2395
2396VALUE
2397rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self)
2398{
2399 return specific_eval(argc, argv, self, TRUE, RB_NO_KEYWORDS);
2400}
2401
2402/*
2403 * call-seq:
2404 * obj.instance_exec(arg...) {|var...| block } -> obj
2405 *
2406 * Executes the given block within the context of the receiver
2407 * (_obj_). In order to set the context, the variable +self+ is set
2408 * to _obj_ while the code is executing, giving the code access to
2409 * _obj_'s instance variables. Arguments are passed as block parameters.
2410 *
2411 * class KlassWithSecret
2412 * def initialize
2413 * @secret = 99
2414 * end
2415 * end
2416 * k = KlassWithSecret.new
2417 * k.instance_exec(5) {|x| @secret+x } #=> 104
2418 */
2419
2420static VALUE
2421rb_obj_instance_exec_internal(int argc, const VALUE *argv, VALUE self)
2422{
2423 return yield_under(self, TRUE, argc, argv, RB_PASS_CALLED_KEYWORDS);
2424}
2425
2426VALUE
2427rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self)
2428{
2429 return yield_under(self, TRUE, argc, argv, RB_NO_KEYWORDS);
2430}
2431
2432/*
2433 * call-seq:
2434 * class_eval(string, filename = nil, lineno = 1) -> obj
2435 * class_eval { |mod| ... } -> obj
2436 * module_eval(string, filename = nil, lineno = 1) -> obj
2437 * module_eval { |mod| ... } -> obj
2438 *
2439 * Evaluates the +string+ or block in the context of +self+.
2440 * Returns the result of the last expression.
2441 *
2442 * When +string+ is given, evaluates the given string in the
2443 * context of +self+:
2444 *
2445 * class Foo; end
2446 *
2447 * Foo.module_eval("def greeting = puts 'hello'")
2448 *
2449 * Foo.new.greeting # => "hello"
2450 *
2451 * If the optional +filename+ is given, it will be used as the
2452 * filename of the evaluation (for <tt>__FILE__</tt> and errors).
2453 * Otherwise, it will default to <tt>(eval at __FILE__:__LINE__)</tt>
2454 * where <tt>__FILE__</tt> and <tt>__LINE__</tt> are the filename and
2455 * line number of the caller, respectively:
2456 *
2457 * class Foo; end
2458 *
2459 * Foo.module_eval("puts __FILE__") # => "(eval at ../test.rb:3)"
2460 * Foo.module_eval("puts __FILE__", "foobar.rb") # => "foobar.rb"
2461 *
2462 * If the optional +lineno+ is given, it will be used as the
2463 * line number of the evaluation (for <tt>__LINE__</tt> and errors).
2464 * Otherwise, it will default to 1:
2465 *
2466 * class Foo; end
2467 *
2468 * Foo.module_eval("puts __LINE__") # => 1
2469 * Foo.module_eval("puts __LINE__", nil, 10) # => 10
2470 *
2471 * When a block is given, evaluates the block in the context
2472 * of +self+:
2473 *
2474 * class Foo; end
2475 *
2476 * Foo.module_eval do
2477 * def greeting = puts "hello"
2478 * end
2479 *
2480 * Foo.new.greeting
2481 *
2482 * However, constant and class variable lookup differs between
2483 * +string+ and block. When +string+ is given, constant and class
2484 * variables are looked up in the context of +self+. When a block
2485 * is given, the context of the lookup is not changed:
2486 *
2487 * class Foo
2488 * GREETING = "hello"
2489 * end
2490 *
2491 * Foo.module_eval("puts GREETING") # => "hello"
2492 *
2493 * Foo.module_eval { puts GREETING } # => NameError: uninitialized constant GREETING
2494 */
2495
2496static VALUE
2497rb_mod_module_eval_internal(int argc, const VALUE *argv, VALUE mod)
2498{
2499 return specific_eval(argc, argv, mod, FALSE, RB_PASS_CALLED_KEYWORDS);
2500}
2501
2502VALUE
2503rb_mod_module_eval(int argc, const VALUE *argv, VALUE mod)
2504{
2505 return specific_eval(argc, argv, mod, FALSE, RB_NO_KEYWORDS);
2506}
2507
2508/*
2509 * call-seq:
2510 * mod.module_exec(arg...) {|var...| block } -> obj
2511 * mod.class_exec(arg...) {|var...| block } -> obj
2512 *
2513 * Evaluates the given block in the context of the class/module.
2514 * The method defined in the block will belong to the receiver.
2515 * Any arguments passed to the method will be passed to the block.
2516 * This can be used if the block needs to access instance variables.
2517 *
2518 * class Thing
2519 * end
2520 * Thing.class_exec{
2521 * def hello() "Hello there!" end
2522 * }
2523 * puts Thing.new.hello()
2524 *
2525 * <em>produces:</em>
2526 *
2527 * Hello there!
2528 */
2529
2530static VALUE
2531rb_mod_module_exec_internal(int argc, const VALUE *argv, VALUE mod)
2532{
2533 return yield_under(mod, FALSE, argc, argv, RB_PASS_CALLED_KEYWORDS);
2534}
2535
2536VALUE
2537rb_mod_module_exec(int argc, const VALUE *argv, VALUE mod)
2538{
2539 return yield_under(mod, FALSE, argc, argv, RB_NO_KEYWORDS);
2540}
2541
2542/*
2543 * Document-class: UncaughtThrowError
2544 *
2545 * Raised when +throw+ is called with a _tag_ which does not have
2546 * corresponding +catch+ block.
2547 *
2548 * throw "foo", "bar"
2549 *
2550 * <em>raises the exception:</em>
2551 *
2552 * UncaughtThrowError: uncaught throw "foo"
2553 */
2554
2555static VALUE
2556uncaught_throw_init(int argc, const VALUE *argv, VALUE exc)
2557{
2559 rb_call_super(argc - 2, argv + 2);
2560 rb_ivar_set(exc, id_tag, argv[0]);
2561 rb_ivar_set(exc, id_value, argv[1]);
2562 return exc;
2563}
2564
2565/*
2566 * call-seq:
2567 * uncaught_throw.tag -> obj
2568 *
2569 * Return the tag object which was called for.
2570 */
2571
2572static VALUE
2573uncaught_throw_tag(VALUE exc)
2574{
2575 return rb_ivar_get(exc, id_tag);
2576}
2577
2578/*
2579 * call-seq:
2580 * uncaught_throw.value -> obj
2581 *
2582 * Return the return value which was called for.
2583 */
2584
2585static VALUE
2586uncaught_throw_value(VALUE exc)
2587{
2588 return rb_ivar_get(exc, id_value);
2589}
2590
2591/*
2592 * call-seq:
2593 * uncaught_throw.to_s -> string
2594 *
2595 * Returns formatted message with the inspected tag.
2596 */
2597
2598static VALUE
2599uncaught_throw_to_s(VALUE exc)
2600{
2601 VALUE mesg = rb_attr_get(exc, id_mesg);
2602 VALUE tag = uncaught_throw_tag(exc);
2603 return rb_str_format(1, &tag, mesg);
2604}
2605
2606/*
2607 * call-seq:
2608 * throw(tag [, obj])
2609 *
2610 * Transfers control to the end of the active +catch+ block
2611 * waiting for _tag_. Raises +UncaughtThrowError+ if there
2612 * is no +catch+ block for the _tag_. The optional second
2613 * parameter supplies a return value for the +catch+ block,
2614 * which otherwise defaults to +nil+. For examples, see
2615 * Kernel::catch.
2616 */
2617
2618static VALUE
2619rb_f_throw(int argc, VALUE *argv, VALUE _)
2620{
2621 VALUE tag, value;
2622
2623 rb_scan_args(argc, argv, "11", &tag, &value);
2624 rb_throw_obj(tag, value);
2626}
2627
2628void
2630{
2631 rb_execution_context_t *ec = GET_EC();
2632 struct rb_vm_tag *tt = ec->tag;
2633
2634 while (tt) {
2635 if (tt->tag == tag) {
2636 tt->retval = value;
2637 break;
2638 }
2639 tt = tt->prev;
2640 }
2641 if (!tt) {
2642 VALUE desc[3];
2643 desc[0] = tag;
2644 desc[1] = value;
2645 desc[2] = rb_str_new_cstr("uncaught throw %p");
2646 rb_exc_raise(rb_class_new_instance(numberof(desc), desc, rb_eUncaughtThrow));
2647 }
2648
2649 ec->errinfo = (VALUE)THROW_DATA_NEW(tag, NULL, TAG_THROW);
2650 EC_JUMP_TAG(ec, TAG_THROW);
2651}
2652
2653void
2654rb_throw(const char *tag, VALUE val)
2655{
2656 rb_throw_obj(rb_sym_intern_ascii_cstr(tag), val);
2657}
2658
2659static VALUE
2660catch_i(RB_BLOCK_CALL_FUNC_ARGLIST(tag, _))
2661{
2662 return rb_yield_0(1, &tag);
2663}
2664
2665/*
2666 * call-seq:
2667 * catch([tag]) {|tag| block } -> obj
2668 *
2669 * +catch+ executes its block. If +throw+ is not called, the block executes
2670 * normally, and +catch+ returns the value of the last expression evaluated.
2671 *
2672 * catch(1) { 123 } # => 123
2673 *
2674 * If <code>throw(tag2, val)</code> is called, Ruby searches up its stack for
2675 * a +catch+ block whose +tag+ has the same +object_id+ as _tag2_. When found,
2676 * the block stops executing and returns _val_ (or +nil+ if no second argument
2677 * was given to +throw+).
2678 *
2679 * catch(1) { throw(1, 456) } # => 456
2680 * catch(1) { throw(1) } # => nil
2681 *
2682 * When +tag+ is passed as the first argument, +catch+ yields it as the
2683 * parameter of the block.
2684 *
2685 * catch(1) {|x| x + 2 } # => 3
2686 *
2687 * When no +tag+ is given, +catch+ yields a new unique object (as from
2688 * +Object.new+) as the block parameter. This object can then be used as the
2689 * argument to +throw+, and will match the correct +catch+ block.
2690 *
2691 * catch do |obj_A|
2692 * catch do |obj_B|
2693 * throw(obj_B, 123)
2694 * puts "This puts is not reached"
2695 * end
2696 *
2697 * puts "This puts is displayed"
2698 * 456
2699 * end
2700 *
2701 * # => 456
2702 *
2703 * catch do |obj_A|
2704 * catch do |obj_B|
2705 * throw(obj_A, 123)
2706 * puts "This puts is still not reached"
2707 * end
2708 *
2709 * puts "Now this puts is also not reached"
2710 * 456
2711 * end
2712 *
2713 * # => 123
2714 */
2715
2716static VALUE
2717rb_f_catch(int argc, VALUE *argv, VALUE self)
2718{
2719 VALUE tag = rb_check_arity(argc, 0, 1) ? argv[0] : rb_obj_alloc(rb_cObject);
2720 return rb_catch_obj(tag, catch_i, 0);
2721}
2722
2723VALUE
2724rb_catch(const char *tag, rb_block_call_func_t func, VALUE data)
2725{
2726 VALUE vtag = tag ? rb_sym_intern_ascii_cstr(tag) : rb_obj_alloc(rb_cObject);
2727 return rb_catch_obj(vtag, func, data);
2728}
2729
2730static VALUE
2731vm_catch_protect(VALUE tag, rb_block_call_func *func, VALUE data,
2732 enum ruby_tag_type *stateptr_arg, rb_execution_context_t *volatile ec)
2733{
2734 enum ruby_tag_type state;
2735 VALUE val = Qnil; /* OK */
2736 rb_control_frame_t *volatile saved_cfp = ec->cfp;
2737 enum ruby_tag_type * volatile stateptr = stateptr_arg;
2738
2739 EC_PUSH_TAG(ec);
2740
2741 _tag.tag = tag;
2742
2743 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2744 /* call with argc=1, argv = [tag], block = Qnil to insure compatibility */
2745 val = (*func)(tag, data, 1, (const VALUE *)&tag, Qnil);
2746 }
2747 else if (state == TAG_THROW && THROW_DATA_VAL((struct vm_throw_data *)ec->errinfo) == tag) {
2748 rb_vm_rewind_cfp(ec, saved_cfp);
2749 val = ec->tag->retval;
2750 ec->errinfo = Qnil;
2751 state = 0;
2752 }
2753 EC_POP_TAG();
2754 if (stateptr)
2755 *stateptr = state;
2756
2757 return val;
2758}
2759
2760VALUE
2761rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, enum ruby_tag_type *stateptr)
2762{
2763 return vm_catch_protect(t, func, data, stateptr, GET_EC());
2764}
2765
2766VALUE
2768{
2769 enum ruby_tag_type state;
2770 rb_execution_context_t *ec = GET_EC();
2771 VALUE val = vm_catch_protect(t, (rb_block_call_func *)func, data, &state, ec);
2772 if (state) EC_JUMP_TAG(ec, state);
2773 return val;
2774}
2775
2776static void
2777local_var_list_init(struct local_var_list *vars)
2778{
2779 vars->tbl = rb_ident_hash_new();
2780 RBASIC_CLEAR_CLASS(vars->tbl);
2781}
2782
2783static VALUE
2784local_var_list_finish(struct local_var_list *vars)
2785{
2786 /* TODO: not to depend on the order of st_table */
2787 VALUE ary = rb_hash_keys(vars->tbl);
2788 rb_hash_clear(vars->tbl);
2789 vars->tbl = 0;
2790 return ary;
2791}
2792
2793static int
2794local_var_list_update(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
2795{
2796 if (existing) return ST_STOP;
2797 *value = (st_data_t)Qtrue; /* INT2FIX(arg) */
2798 return ST_CONTINUE;
2799}
2800
2801extern int rb_numparam_id_p(ID id);
2802
2803static void
2804local_var_list_add(const struct local_var_list *vars, ID lid)
2805{
2806 /* should skip temporary variable */
2807 if (!lid) return;
2808 if (!rb_is_local_id(lid)) return;
2809
2810 /* should skip numbered parameters as well */
2811 if (rb_numparam_id_p(lid)) return;
2812
2813 st_data_t idx = 0; /* tbl->num_entries */
2814 rb_hash_stlike_update(vars->tbl, ID2SYM(lid), local_var_list_update, idx);
2815}
2816
2817static void
2818numparam_list_add(const struct local_var_list *vars, ID lid)
2819{
2820 /* should skip temporary variable */
2821 if (!lid) return;
2822 if (!rb_is_local_id(lid)) return;
2823
2824 /* should skip anything but numbered parameters */
2825 if (rb_numparam_id_p(lid)) {
2826 st_data_t idx = 0; /* tbl->num_entries */
2827 rb_hash_stlike_update(vars->tbl, ID2SYM(lid), local_var_list_update, idx);
2828 }
2829}
2830
2831/*
2832 * call-seq:
2833 * local_variables -> array
2834 *
2835 * Returns the names of the current local variables.
2836 *
2837 * fred = 1
2838 * for i in 1..10
2839 * # ...
2840 * end
2841 * local_variables #=> [:fred, :i]
2842 */
2843
2844static VALUE
2845rb_f_local_variables(VALUE _)
2846{
2847 struct local_var_list vars;
2848 rb_execution_context_t *ec = GET_EC();
2849 rb_control_frame_t *cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp));
2850 unsigned int i;
2851
2852 local_var_list_init(&vars);
2853 while (cfp) {
2854 if (CFP_ISEQ(cfp)) {
2855 for (i = 0; i < ISEQ_BODY(CFP_ISEQ(cfp))->local_table_size; i++) {
2856 local_var_list_add(&vars, ISEQ_BODY(CFP_ISEQ(cfp))->local_table[i]);
2857 }
2858 }
2859 if (!VM_ENV_LOCAL_P(cfp->ep)) {
2860 /* block */
2861 const VALUE *ep = VM_CF_PREV_EP(cfp);
2862
2863 if (vm_collect_local_variables_in_heap(ep, &vars)) {
2864 break;
2865 }
2866 else {
2867 while (cfp->ep != ep) {
2868 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2869 }
2870 }
2871 }
2872 else {
2873 break;
2874 }
2875 }
2876 return local_var_list_finish(&vars);
2877}
2878
2879/*
2880 * call-seq:
2881 * block_given? -> true or false
2882 *
2883 * Returns <code>true</code> if <code>yield</code> would execute a
2884 * block in the current context. The <code>iterator?</code> form
2885 * is mildly deprecated.
2886 *
2887 * def try
2888 * if block_given?
2889 * yield
2890 * else
2891 * "no block"
2892 * end
2893 * end
2894 * try #=> "no block"
2895 * try { "hello" } #=> "hello"
2896 * try do "hello" end #=> "hello"
2897 */
2898
2899static VALUE
2900rb_f_block_given_p(VALUE _)
2901{
2902 rb_execution_context_t *ec = GET_EC();
2903 rb_control_frame_t *cfp = ec->cfp;
2904 cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
2905
2906 return RBOOL(cfp != NULL && VM_CF_BLOCK_HANDLER(cfp) != VM_BLOCK_HANDLER_NONE);
2907}
2908
2909/*
2910 * call-seq:
2911 * iterator? -> true or false
2912 *
2913 * Deprecated. Use block_given? instead.
2914 */
2915
2916static VALUE
2917rb_f_iterator_p(VALUE self)
2918{
2919 rb_warn_deprecated("iterator?", "block_given?");
2920 return rb_f_block_given_p(self);
2921}
2922
2923VALUE
2924rb_current_realfilepath(void)
2925{
2926 const rb_execution_context_t *ec = GET_EC();
2927 rb_control_frame_t *cfp = ec->cfp;
2928 cfp = vm_get_ruby_level_caller_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
2929 if (cfp != NULL) {
2930 const rb_iseq_t *iseq = CFP_ISEQ(cfp);
2931 VALUE path = rb_iseq_realpath(iseq);
2932 if (RTEST(path)) return path;
2933 // eval context
2934 path = rb_iseq_path(iseq);
2935 if (path == eval_default_path) {
2936 return Qnil;
2937 }
2938
2939 // [Feature #19755] implicit eval location is "(eval at #{__FILE__}:#{__LINE__})"
2940 const rb_len_t len = RSTRING_LEN(path);
2941 if (len > EVAL_LOCATION_MARK_LEN+1) {
2942 const char *const ptr = RSTRING_PTR(path);
2943 if (ptr[len - 1] == ')' &&
2944 memcmp(ptr, "("EVAL_LOCATION_MARK, EVAL_LOCATION_MARK_LEN+1) == 0) {
2945 return Qnil;
2946 }
2947 }
2948
2949 return path;
2950 }
2951 return Qnil;
2952}
2953
2954// Assert that an internal function is running and return
2955// the imemo object that represents it.
2956struct vm_ifunc *
2957rb_current_ifunc(void)
2958{
2959 // Search VM_FRAME_MAGIC_IFUNC to see ifunc imemos put on the iseq field.
2960 VALUE ifunc = (VALUE)CFP_ISEQ(GET_EC()->cfp);
2961 RUBY_ASSERT_ALWAYS(imemo_type_p(ifunc, imemo_ifunc));
2962 return (struct vm_ifunc *)ifunc;
2963}
2964
2965void
2966Init_vm_eval(void)
2967{
2968 rb_define_global_function("eval", rb_f_eval, -1);
2969 rb_define_global_function("local_variables", rb_f_local_variables, 0);
2970 rb_define_global_function("iterator?", rb_f_iterator_p, 0);
2971 rb_define_global_function("block_given?", rb_f_block_given_p, 0);
2972
2973 rb_define_global_function("catch", rb_f_catch, -1);
2974 rb_define_global_function("throw", rb_f_throw, -1);
2975
2976 rb_define_method(rb_cBasicObject, "instance_eval", rb_obj_instance_eval_internal, -1);
2977 rb_define_method(rb_cBasicObject, "instance_exec", rb_obj_instance_exec_internal, -1);
2978 rb_define_private_method(rb_cBasicObject, "method_missing", rb_method_missing, -1);
2979
2980#if 1
2981 rb_add_method(rb_cBasicObject, id__send__,
2982 VM_METHOD_TYPE_OPTIMIZED, (void *)OPTIMIZED_METHOD_TYPE_SEND, METHOD_VISI_PUBLIC);
2983 rb_add_method(rb_mKernel, idSend,
2984 VM_METHOD_TYPE_OPTIMIZED, (void *)OPTIMIZED_METHOD_TYPE_SEND, METHOD_VISI_PUBLIC);
2985#else
2986 rb_define_method(rb_cBasicObject, "__send__", rb_f_send, -1);
2987 rb_define_method(rb_mKernel, "send", rb_f_send, -1);
2988#endif
2989 rb_define_method(rb_mKernel, "public_send", rb_f_public_send, -1);
2990
2991 rb_define_method(rb_cModule, "module_exec", rb_mod_module_exec_internal, -1);
2992 rb_define_method(rb_cModule, "class_exec", rb_mod_module_exec_internal, -1);
2993 rb_define_method(rb_cModule, "module_eval", rb_mod_module_eval_internal, -1);
2994 rb_define_method(rb_cModule, "class_eval", rb_mod_module_eval_internal, -1);
2995
2996 rb_eUncaughtThrow = rb_define_class("UncaughtThrowError", rb_eArgError);
2997 rb_define_method(rb_eUncaughtThrow, "initialize", uncaught_throw_init, -1);
2998 rb_define_method(rb_eUncaughtThrow, "tag", uncaught_throw_tag, 0);
2999 rb_define_method(rb_eUncaughtThrow, "value", uncaught_throw_value, 0);
3000 rb_define_method(rb_eUncaughtThrow, "to_s", uncaught_throw_to_s, 0);
3001
3002 id_result = rb_intern_const("result");
3003 id_tag = rb_intern_const("tag");
3004 id_value = rb_intern_const("value");
3005}
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:408
#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:409
#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:2264
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition object.c:2305
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:230
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:497
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:902
#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
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:2168
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:1212
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1149
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:1116
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:1107
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:1226
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:2147
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:1200
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:1219
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:1236
VALUE rb_eval_string(const char *str)
Evaluates the given string.
Definition vm_eval.c:2135
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:1206
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:1271
#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:1091
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:2157
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:1650
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:3615
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:2503
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:2537
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:2427
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:2200
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:1124
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:2397
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:1295
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:1682
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:1427
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:1461
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:2654
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:1449
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1404
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:1455
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:2629
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:1596
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:1474
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:375
#define ALLOCA_N(type, n)
Definition memory.h:295
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
Definition memory.h:363
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
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