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