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