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