Ruby: vm_eval.c Source File

Ruby 3.3.5p100 (2024-09-03 revision ef084cc8f4958c1b6e4ead99136631bef6d8ddba)

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