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