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