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