Ruby 4.1.0dev (2026-11-10 revision d9bbf7af11227361911849907728f055a9e49db0)
vm_args.c (d9bbf7af11227361911849907728f055a9e49db0)
1/**********************************************************************
2
3 vm_args.c - process method call arguments. Included into vm.c.
4
5 $Author$
6
7 Copyright (C) 2014- Yukihiro Matsumoto
8
9**********************************************************************/
10
11NORETURN(static void raise_argument_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_callable_method_entry_t *cme, const VALUE exc));
12NORETURN(static void argument_arity_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_callable_method_entry_t *cme, const int miss_argc, const int min_argc, const int max_argc));
13NORETURN(static void argument_kw_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_callable_method_entry_t *cme, const char *error, const VALUE keys));
14VALUE rb_keyword_error_new(const char *error, VALUE keys); /* class.c */
15static VALUE set_error_backtrace(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_callable_method_entry_t *cme, const VALUE exc);
16
17static VALUE method_missing(rb_execution_context_t *ec, VALUE obj, ID id, int argc, const VALUE *argv,
18 enum method_missing_reason call_status, int kw_splat);
19const rb_callable_method_entry_t *rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
20
21struct args_info {
22 /* basic args info */
23 VALUE *argv;
24 int argc;
25
26 /* additional args info */
27 int rest_index;
28 int rest_dupped;
29 const struct rb_callinfo_kwarg *kw_arg;
30 VALUE *kw_argv;
31 VALUE rest;
32};
33
34enum arg_setup_type {
35 arg_setup_method,
36 arg_setup_block
37};
38
39static inline void
40arg_rest_dup(struct args_info *args)
41{
42 if (!args->rest_dupped) {
43 args->rest = rb_ary_dup(args->rest);
44 args->rest_dupped = TRUE;
45 }
46}
47
48static inline int
49args_argc(struct args_info *args)
50{
51 if (args->rest == Qfalse) {
52 return args->argc;
53 }
54 else {
55 return args->argc + RARRAY_LENINT(args->rest) - args->rest_index;
56 }
57}
58
59static inline void
60args_extend(struct args_info *args, const int min_argc)
61{
62 int i;
63
64 if (args->rest) {
65 arg_rest_dup(args);
66 VM_ASSERT(args->rest_index == 0);
67 for (i=args->argc + RARRAY_LENINT(args->rest); i<min_argc; i++) {
68 rb_ary_push(args->rest, Qnil);
69 }
70 }
71 else {
72 for (i=args->argc; i<min_argc; i++) {
73 args->argv[args->argc++] = Qnil;
74 }
75 }
76}
77
78static inline void
79args_reduce(struct args_info *args, int over_argc)
80{
81 if (args->rest) {
82 const rb_len_t len = RARRAY_LEN(args->rest);
83
84 if (len > over_argc) {
85 arg_rest_dup(args);
86 rb_ary_resize(args->rest, len - over_argc);
87 return;
88 }
89 else {
90 args->rest = Qfalse;
91 over_argc -= len;
92 }
93 }
94
95 VM_ASSERT(args->argc >= over_argc);
96 args->argc -= over_argc;
97}
98
99static inline int
100args_check_block_arg0(struct args_info *args)
101{
102 VALUE ary = Qnil;
103
104 if (args->rest && RARRAY_LEN(args->rest) == 1) {
105 VALUE arg0 = RARRAY_AREF(args->rest, 0);
106 ary = rb_check_array_type(arg0);
107 }
108 else if (args->argc == 1) {
109 VALUE arg0 = args->argv[0];
110 ary = rb_check_array_type(arg0);
111 args->argv[0] = arg0; /* see: https://bugs.ruby-lang.org/issues/8484 */
112 }
113
114 if (!NIL_P(ary)) {
115 args->rest = ary;
116 args->rest_index = 0;
117 args->argc = 0;
118 return TRUE;
119 }
120
121 return FALSE;
122}
123
124static inline void
125args_copy(struct args_info *args)
126{
127 if (args->rest != Qfalse) {
128 int argc = args->argc;
129 args->argc = 0;
130 arg_rest_dup(args);
131
132 /*
133 * argv: [m0, m1, m2, m3]
134 * rest: [a0, a1, a2, a3, a4, a5]
135 * ^
136 * rest_index
137 *
138 * #=> first loop
139 *
140 * argv: [m0, m1]
141 * rest: [m2, m3, a2, a3, a4, a5]
142 * ^
143 * rest_index
144 *
145 * #=> 2nd loop
146 *
147 * argv: [] (argc == 0)
148 * rest: [m0, m1, m2, m3, a2, a3, a4, a5]
149 * ^
150 * rest_index
151 */
152 while (args->rest_index > 0 && argc > 0) {
153 RARRAY_ASET(args->rest, --args->rest_index, args->argv[--argc]);
154 }
155 while (argc > 0) {
156 rb_ary_unshift(args->rest, args->argv[--argc]);
157 }
158 }
159 else if (args->argc > 0) {
160 args->rest = rb_ary_new_from_values(args->argc, args->argv);
161 args->rest_index = 0;
162 args->rest_dupped = TRUE;
163 args->argc = 0;
164 }
165}
166
167static inline const VALUE *
168args_rest_argv(struct args_info *args)
169{
170 return RARRAY_CONST_PTR(args->rest) + args->rest_index;
171}
172
173static inline VALUE
174args_rest_array(struct args_info *args)
175{
176 VALUE ary;
177
178 if (args->rest) {
179 ary = rb_ary_behead(args->rest, args->rest_index);
180 args->rest_index = 0;
181 args->rest = 0;
182 }
183 else {
184 ary = rb_ary_new();
185 }
186 return ary;
187}
188
189static int
190args_kw_argv_to_hash(struct args_info *args)
191{
192 const struct rb_callinfo_kwarg *kw_arg = args->kw_arg;
193 const VALUE *const passed_keywords = kw_arg->keywords;
194 const int kw_len = kw_arg->keyword_len;
195 VALUE h = rb_hash_new_capa(kw_len);
196 const int kw_start = args->argc - kw_len;
197 const VALUE * const kw_argv = args->argv + kw_start;
198 int i;
199
200 args->argc = kw_start + 1;
201 for (i=0; i<kw_len; i++) {
202 rb_hash_aset(h, passed_keywords[i], kw_argv[i]);
203 }
204
205 args->argv[args->argc - 1] = h;
206
207 return args->argc;
208}
209
210static inline void
211args_setup_lead_parameters(struct args_info *args, int argc, VALUE *locals)
212{
213 if (args->argc >= argc) {
214 /* do noting */
215 args->argc -= argc;
216 args->argv += argc;
217 }
218 else {
219 int i, j;
220 const VALUE *argv = args_rest_argv(args);
221
222 for (i=args->argc, j=0; i<argc; i++, j++) {
223 locals[i] = argv[j];
224 }
225 args->rest_index += argc - args->argc;
226 args->argc = 0;
227 }
228}
229
230static inline void
231args_setup_post_parameters(struct args_info *args, int argc, VALUE *locals)
232{
234 len = RARRAY_LEN(args->rest);
235 MEMCPY(locals, RARRAY_CONST_PTR(args->rest) + len - argc, VALUE, argc);
236 rb_ary_resize(args->rest, len - argc);
237}
238
239static inline int
240args_setup_opt_parameters(struct args_info *args, int opt_max, VALUE *locals)
241{
242 int i;
243
244 if (args->argc >= opt_max) {
245 args->argc -= opt_max;
246 args->argv += opt_max;
247 i = opt_max;
248 }
249 else {
250 int j;
251 i = args->argc;
252 args->argc = 0;
253
254 if (args->rest) {
255 int len = RARRAY_LENINT(args->rest);
256 const VALUE *argv = RARRAY_CONST_PTR(args->rest);
257
258 for (; i<opt_max && args->rest_index < len; i++, args->rest_index++) {
259 locals[i] = argv[args->rest_index];
260 }
261 }
262
263 /* initialize by nil */
264 for (j=i; j<opt_max; j++) {
265 locals[j] = Qnil;
266 }
267 }
268
269 return i;
270}
271
272static inline void
273args_setup_rest_parameter(struct args_info *args, VALUE *locals)
274{
275 *locals = args_rest_array(args);
276}
277
278static VALUE
279make_unknown_kw_hash(const VALUE *passed_keywords, int passed_keyword_len, const VALUE *kw_argv)
280{
281 int i;
282 VALUE obj = rb_ary_hidden_new(1);
283
284 for (i=0; i<passed_keyword_len; i++) {
285 if (!UNDEF_P(kw_argv[i])) {
286 rb_ary_push(obj, passed_keywords[i]);
287 }
288 }
289 return obj;
290}
291
292static VALUE
293make_rest_kw_hash(const VALUE *passed_keywords, int passed_keyword_len, const VALUE *kw_argv)
294{
295 int i;
296 VALUE obj = rb_hash_new_capa(passed_keyword_len);
297
298 for (i=0; i<passed_keyword_len; i++) {
299 if (!UNDEF_P(kw_argv[i])) {
300 rb_hash_aset(obj, passed_keywords[i], kw_argv[i]);
301 }
302 }
303 return obj;
304}
305
306static inline int
307args_setup_kw_parameters_lookup(const ID key, VALUE *ptr, const VALUE *const passed_keywords, VALUE *passed_values, const int passed_keyword_len)
308{
309 int i;
310 const VALUE keyname = ID2SYM(key);
311
312 for (i=0; i<passed_keyword_len; i++) {
313 if (keyname == passed_keywords[i]) {
314 *ptr = passed_values[i];
315 passed_values[i] = Qundef;
316 return TRUE;
317 }
318 }
319
320 return FALSE;
321}
322
323static inline void
324args_setup_kw_parameters_not_found(const VALUE *default_values, VALUE *locals, int i, int di,
325 int *unspecified_bits, VALUE *unspecified_bits_value)
326{
327 if (UNDEF_P(default_values[di])) {
328 locals[i] = Qnil;
329
330 if (LIKELY(i < VM_KW_SPECIFIED_BITS_MAX)) {
331 *unspecified_bits |= 0x01 << di;
332 }
333 else {
334 VALUE bits_value = *unspecified_bits_value;
335 if (NIL_P(bits_value)) {
336 /* fixnum -> hash */
337 int bits = *unspecified_bits;
338 *unspecified_bits_value = bits_value = rb_ident_set_new();
339
340 for (int j=0; j<VM_KW_SPECIFIED_BITS_MAX; j++) {
341 if (bits & (0x01 << j)) {
342 rb_set_add_no_check(bits_value, INT2FIX(j));
343 }
344 }
345 }
346 rb_set_add_no_check(bits_value, INT2FIX(di));
347 }
348 }
349 else {
350 locals[i] = default_values[di];
351 }
352}
353
354/* Extends cfp->sp over the keyword locals so the GC marks them. The fast
355 * paths in vm_insnhelper.c call args_setup_kw_parameters with the locals
356 * above cfp->sp, and more than VM_KW_SPECIFIED_BITS_MAX keywords allocate
357 * a set while passed values are only referenced from the locals. */
358static void
359args_setup_kw_extend_sp(rb_execution_context_t *const ec, VALUE *const end)
360{
361 VALUE *sp = ec->cfp->sp;
362 if (sp >= end) return;
363
364 CHECK_VM_STACK_OVERFLOW0(ec->cfp, sp, end - sp);
365 for (; sp < end; sp++) {
366 *sp = Qnil;
367 }
368 ec->cfp->sp = end;
369}
370
371static void
372args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *const iseq, const rb_callable_method_entry_t *cme,
373 VALUE *const passed_values, const int passed_keyword_len, const VALUE *const passed_keywords,
374 VALUE *const locals)
375{
376 const ID *acceptable_keywords = ISEQ_BODY(iseq)->param.keyword->table;
377 const int req_key_num = ISEQ_BODY(iseq)->param.keyword->required_num;
378 const int key_num = ISEQ_BODY(iseq)->param.keyword->num;
379 const VALUE * const default_values = ISEQ_BODY(iseq)->param.keyword->default_values;
380 VALUE missing = 0;
381 int i, di, found = 0;
382 int unspecified_bits = 0;
383 VALUE unspecified_bits_value = Qnil;
384 VALUE *orig_sp = NULL;
385
386 if (UNLIKELY(key_num > VM_KW_SPECIFIED_BITS_MAX)) {
387 orig_sp = ec->cfp->sp;
388 args_setup_kw_extend_sp(ec, locals + key_num + 1);
389 }
390
391 for (i=0; i<req_key_num; i++) {
392 ID key = acceptable_keywords[i];
393 if (args_setup_kw_parameters_lookup(key, &locals[i], passed_keywords, passed_values, passed_keyword_len)) {
394 found++;
395 }
396 else {
397 if (!missing) missing = rb_ary_hidden_new(1);
398 rb_ary_push(missing, ID2SYM(key));
399 }
400 }
401
402 if (missing) argument_kw_error(ec, iseq, cme, "missing", missing);
403
404 for (di=0; i<key_num; i++, di++) {
405 if (args_setup_kw_parameters_lookup(acceptable_keywords[i], &locals[i], passed_keywords, passed_values, passed_keyword_len)) {
406 found++;
407 }
408 else {
409 args_setup_kw_parameters_not_found(default_values, locals, i, di,
410 &unspecified_bits, &unspecified_bits_value);
411 }
412 }
413
414 if (ISEQ_BODY(iseq)->param.flags.has_kwrest) {
415 const int rest_hash_index = key_num + 1;
416 locals[rest_hash_index] = make_rest_kw_hash(passed_keywords, passed_keyword_len, passed_values);
417 }
418 else {
419 if (found != passed_keyword_len) {
420 VALUE keys = make_unknown_kw_hash(passed_keywords, passed_keyword_len, passed_values);
421 argument_kw_error(ec, iseq, cme, "unknown", keys);
422 }
423 }
424
425 if (NIL_P(unspecified_bits_value)) {
426 unspecified_bits_value = INT2FIX(unspecified_bits);
427 }
428 locals[key_num] = unspecified_bits_value;
429 if (orig_sp) ec->cfp->sp = orig_sp;
430}
431
432static void
433args_setup_kw_parameters_from_kwsplat(rb_execution_context_t *const ec, const rb_iseq_t *const iseq, const rb_callable_method_entry_t *cme,
434 VALUE keyword_hash, VALUE *const locals, bool remove_hash_value)
435{
436 const ID *acceptable_keywords = ISEQ_BODY(iseq)->param.keyword->table;
437 const int req_key_num = ISEQ_BODY(iseq)->param.keyword->required_num;
438 const int key_num = ISEQ_BODY(iseq)->param.keyword->num;
439 const VALUE * const default_values = ISEQ_BODY(iseq)->param.keyword->default_values;
440 VALUE missing = 0;
441 int i, di;
442 int unspecified_bits = 0;
443 size_t keyword_size = RHASH_SIZE(keyword_hash);
444 VALUE unspecified_bits_value = Qnil;
445
446 for (i=0; i<req_key_num; i++) {
447 VALUE key = ID2SYM(acceptable_keywords[i]);
448 VALUE value;
449 if (remove_hash_value) {
450 value = rb_hash_delete_entry(keyword_hash, key);
451 }
452 else {
453 value = rb_hash_lookup2(keyword_hash, key, Qundef);
454 }
455
456 if (!UNDEF_P(value)) {
457 keyword_size--;
458 locals[i] = value;
459 }
460 else {
461 if (!missing) missing = rb_ary_hidden_new(1);
462 rb_ary_push(missing, key);
463 }
464 }
465
466 if (missing) argument_kw_error(ec, iseq, cme, "missing", missing);
467
468 for (di=0; i<key_num; i++, di++) {
469 VALUE key = ID2SYM(acceptable_keywords[i]);
470 VALUE value;
471 if (remove_hash_value) {
472 value = rb_hash_delete_entry(keyword_hash, key);
473 }
474 else {
475 value = rb_hash_lookup2(keyword_hash, key, Qundef);
476 }
477
478 if (!UNDEF_P(value)) {
479 keyword_size--;
480 locals[i] = value;
481 }
482 else {
483 args_setup_kw_parameters_not_found(default_values, locals, i, di,
484 &unspecified_bits, &unspecified_bits_value);
485 }
486 }
487
488 if (ISEQ_BODY(iseq)->param.flags.has_kwrest) {
489 const int rest_hash_index = key_num + 1;
490 locals[rest_hash_index] = keyword_hash;
491 }
492 else {
493 if (!remove_hash_value) {
494 if (keyword_size != 0) {
495 /* Recurse with duplicated keyword hash in remove mode.
496 * This is simpler than writing code to check which entries in the hash do not match.
497 * This will raise an exception, so the additional performance impact shouldn't be material.
498 */
499 args_setup_kw_parameters_from_kwsplat(ec, iseq, cme, rb_hash_dup(keyword_hash), locals, true);
500 }
501 }
502 else if (!RHASH_EMPTY_P(keyword_hash)) {
503 argument_kw_error(ec, iseq, cme, "unknown", rb_hash_keys(keyword_hash));
504 }
505 }
506
507 if (NIL_P(unspecified_bits_value)) {
508 unspecified_bits_value = INT2FIX(unspecified_bits);
509 }
510 locals[key_num] = unspecified_bits_value;
511}
512
513static inline void
514args_setup_kw_rest_parameter(VALUE keyword_hash, VALUE *locals, int kw_flag, bool anon_kwrest)
515{
516 if (NIL_P(keyword_hash)) {
517 if (!anon_kwrest) {
518 keyword_hash = rb_hash_new();
519 }
520 }
521 else if (!(kw_flag & VM_CALL_KW_SPLAT_MUT)) {
522 keyword_hash = rb_hash_dup(keyword_hash);
523 }
524 locals[0] = keyword_hash;
525}
526
527static inline void
528args_setup_block_parameter(const rb_execution_context_t *ec, struct rb_calling_info *calling, VALUE *locals)
529{
530 VALUE block_handler = calling->block_handler;
531 *locals = rb_vm_bh_to_procval(ec, block_handler);
532}
533
534static inline int
535ignore_keyword_hash_p(VALUE keyword_hash, const rb_iseq_t * const iseq, unsigned int * kw_flag, VALUE * converted_keyword_hash)
536{
537 if (keyword_hash == Qnil) {
538 goto ignore;
539 }
540 else if (!RB_TYPE_P(keyword_hash, T_HASH)) {
541 keyword_hash = rb_to_hash_type(keyword_hash);
542 }
543 else if (UNLIKELY(ISEQ_BODY(iseq)->param.flags.anon_kwrest)) {
544 if (!ISEQ_BODY(iseq)->param.flags.has_kw) {
545 *kw_flag |= VM_CALL_KW_SPLAT_MUT;
546 }
547 }
548
549 if (RHASH_EMPTY_P(keyword_hash) && !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
550 goto ignore;
551 }
552
553 if (!(*kw_flag & VM_CALL_KW_SPLAT_MUT) &&
554 (ISEQ_BODY(iseq)->param.flags.has_kwrest ||
555 ISEQ_BODY(iseq)->param.flags.ruby2_keywords)) {
556 *kw_flag |= VM_CALL_KW_SPLAT_MUT;
557 keyword_hash = rb_hash_dup(keyword_hash);
558 }
559 *converted_keyword_hash = keyword_hash;
560
561 if (!(ISEQ_BODY(iseq)->param.flags.has_kw) &&
562 !(ISEQ_BODY(iseq)->param.flags.has_kwrest) &&
563 RHASH_EMPTY_P(keyword_hash)) {
564 ignore:
565 *kw_flag &= ~(VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT);
566 return 1;
567 }
568 else {
569 return 0;
570 }
571}
572
573static VALUE
574check_kwrestarg(VALUE keyword_hash, unsigned int *kw_flag)
575{
576 if (!(*kw_flag & VM_CALL_KW_SPLAT_MUT)) {
577 *kw_flag |= VM_CALL_KW_SPLAT_MUT;
578 return rb_hash_dup(keyword_hash);
579 }
580 else {
581 return keyword_hash;
582 }
583}
584
585static void
586flatten_rest_args(rb_execution_context_t * const ec, struct args_info *args, VALUE * const locals, unsigned int *ci_flag)
587{
588 const VALUE *argv = RARRAY_CONST_PTR(args->rest);
589 int j, i=args->argc, rest_len = RARRAY_LENINT(args->rest)-1;
590 args->argc += rest_len;
591 if (rest_len) {
592 CHECK_VM_STACK_OVERFLOW(ec->cfp, rest_len+1);
593 for (j=0; rest_len > 0; rest_len--, i++, j++) {
594 locals[i] = argv[j];
595 }
596 }
597 args->rest = Qfalse;
598 *ci_flag &= ~VM_CALL_ARGS_SPLAT;
599}
600
601static int
602setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * const iseq,
603 struct rb_calling_info *const calling,
604 const struct rb_callinfo *ci,
605 VALUE * const locals, const enum arg_setup_type arg_setup_type)
606{
607 const int min_argc = ISEQ_BODY(iseq)->param.lead_num + ISEQ_BODY(iseq)->param.post_num;
608 const int max_argc = (ISEQ_BODY(iseq)->param.flags.has_rest == FALSE) ? min_argc + ISEQ_BODY(iseq)->param.opt_num : UNLIMITED_ARGUMENTS;
609 int given_argc;
610 unsigned int ci_flag = vm_ci_flag(ci);
611 unsigned int kw_flag = ci_flag & (VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT);
612 int opt_pc = 0, allow_autosplat = !kw_flag;
613 struct args_info args_body, *args;
614 VALUE keyword_hash = Qnil;
615 VALUE * const orig_sp = ec->cfp->sp;
616 unsigned int i;
617 VALUE flag_keyword_hash = 0;
618 VALUE splat_flagged_keyword_hash = 0;
619 VALUE converted_keyword_hash = 0;
620 VALUE rest_last = 0;
621 const rb_callable_method_entry_t *cme = calling->cc ? vm_cc_cme(calling->cc) : NULL;
622
623 vm_check_canary(ec, orig_sp);
624 /*
625 * Extend SP for GC.
626 *
627 * [pushed values] [uninitialized values]
628 * <- ci->argc -->
629 * <- ISEQ_BODY(iseq)->param.size------------>
630 * ^ locals ^ sp
631 *
632 * =>
633 * [pushed values] [initialized values ]
634 * <- ci->argc -->
635 * <- ISEQ_BODY(iseq)->param.size------------>
636 * ^ locals ^ sp
637 */
638 if ((unsigned int)calling->argc < ISEQ_BODY(iseq)->param.size) {
639 /* Ensure the nil-fill below and the SP extension do not run into the
640 * control frame region, clobbering frames before the stack overflow
641 * check in vm_push_frame has a chance to raise SystemStackError. */
642 CHECK_VM_STACK_OVERFLOW0(ec->cfp, locals + calling->argc,
643 ISEQ_BODY(iseq)->param.size - calling->argc);
644 }
645 for (i=calling->argc; i<ISEQ_BODY(iseq)->param.size; i++) {
646 locals[i] = Qnil;
647 }
648 ec->cfp->sp = &locals[i];
649
650 /* setup args */
651 args = &args_body;
652 given_argc = args->argc = calling->argc;
653 args->argv = locals;
654 args->rest_dupped = ci_flag & VM_CALL_ARGS_SPLAT_MUT;
655
656 if (UNLIKELY(ISEQ_BODY(iseq)->param.flags.anon_rest)) {
657 if ((ci_flag & VM_CALL_ARGS_SPLAT) &&
658 given_argc == ISEQ_BODY(iseq)->param.lead_num + (kw_flag ? 2 : 1) &&
659 !ISEQ_BODY(iseq)->param.flags.has_opt &&
660 !ISEQ_BODY(iseq)->param.flags.has_post &&
661 !ISEQ_BODY(iseq)->param.flags.ruby2_keywords) {
662 if (kw_flag) {
663 if (ISEQ_BODY(iseq)->param.flags.has_kw ||
664 ISEQ_BODY(iseq)->param.flags.has_kwrest) {
665 args->rest_dupped = true;
666 }
667 else if (kw_flag & VM_CALL_KW_SPLAT) {
668 VALUE kw_hash = locals[args->argc - 1];
669 if (kw_hash == Qnil ||
670 (RB_TYPE_P(kw_hash, T_HASH) && RHASH_EMPTY_P(kw_hash))) {
671 args->rest_dupped = true;
672 }
673 }
674
675 }
676 else if (!ISEQ_BODY(iseq)->param.flags.has_kw &&
677 !ISEQ_BODY(iseq)->param.flags.has_kwrest &&
678 !ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg) {
679 args->rest_dupped = true;
680 }
681 }
682 }
683
684 if (kw_flag & VM_CALL_KWARG) {
685 args->kw_arg = vm_ci_kwarg(ci);
686
687 if (ISEQ_BODY(iseq)->param.flags.has_kw) {
688 int kw_len = args->kw_arg->keyword_len;
689 /* copy kw_argv */
690 args->kw_argv = ALLOCA_N(VALUE, kw_len);
691 args->argc -= kw_len;
692 given_argc -= kw_len;
693 MEMCPY(args->kw_argv, locals + args->argc, VALUE, kw_len);
694 }
695 else {
696 args->kw_argv = NULL;
697 given_argc = args_kw_argv_to_hash(args);
698 kw_flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT;
699 }
700 }
701 else {
702 args->kw_arg = NULL;
703 args->kw_argv = NULL;
704 }
705
706 if ((ci_flag & VM_CALL_ARGS_SPLAT) && (ci_flag & VM_CALL_KW_SPLAT)) {
707 // f(*a, **kw)
708 args->rest_index = 0;
709 keyword_hash = locals[--args->argc];
710 args->rest = locals[--args->argc];
711
712 if (ignore_keyword_hash_p(keyword_hash, iseq, &kw_flag, &converted_keyword_hash)) {
713 keyword_hash = Qnil;
714 }
715 else if (UNLIKELY(ISEQ_BODY(iseq)->param.flags.ruby2_keywords)) {
716 converted_keyword_hash = check_kwrestarg(converted_keyword_hash, &kw_flag);
717 flag_keyword_hash = converted_keyword_hash;
718 arg_rest_dup(args);
719 rb_ary_push(args->rest, converted_keyword_hash);
720 keyword_hash = Qnil;
721 }
722 else if (!ISEQ_BODY(iseq)->param.flags.has_kwrest && !ISEQ_BODY(iseq)->param.flags.has_kw) {
723 converted_keyword_hash = check_kwrestarg(converted_keyword_hash, &kw_flag);
724 if (ISEQ_BODY(iseq)->param.flags.has_rest) {
725 arg_rest_dup(args);
726 rb_ary_push(args->rest, converted_keyword_hash);
727 keyword_hash = Qnil;
728 }
729 else {
730 // Avoid duping rest when not necessary
731 // Copy rest elements and converted keyword hash directly to VM stack
732 const VALUE *argv = RARRAY_CONST_PTR(args->rest);
733 int j, i=args->argc, rest_len = RARRAY_LENINT(args->rest);
734 if (rest_len) {
735 CHECK_VM_STACK_OVERFLOW(ec->cfp, rest_len+1);
736 given_argc += rest_len;
737 args->argc += rest_len;
738 for (j=0; rest_len > 0; rest_len--, i++, j++) {
739 locals[i] = argv[j];
740 }
741 }
742 locals[i] = converted_keyword_hash;
743 given_argc--;
744 args->argc++;
745 args->rest = Qfalse;
746 ci_flag &= ~(VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT);
747 keyword_hash = Qnil;
748 goto arg_splat_and_kw_splat_flattened;
749 }
750 }
751 else {
752 keyword_hash = converted_keyword_hash;
753 }
754
755 int len = RARRAY_LENINT(args->rest);
756 given_argc += len - 2;
757 }
758 else if (ci_flag & VM_CALL_ARGS_SPLAT) {
759 // f(*a)
760 args->rest_index = 0;
761 args->rest = locals[--args->argc];
762 int len = RARRAY_LENINT(args->rest);
763 given_argc += len - 1;
764
765 if (!kw_flag && len > 0) {
766 rest_last = RARRAY_AREF(args->rest, len - 1);
767 if (RB_TYPE_P(rest_last, T_HASH) && FL_TEST_RAW(rest_last, RHASH_PASS_AS_KEYWORDS)) {
768 // def f(**kw); a = [..., kw]; g(*a)
769 splat_flagged_keyword_hash = rest_last;
770 if (!(RHASH_EMPTY_P(rest_last) || ISEQ_BODY(iseq)->param.flags.has_kw) || (ISEQ_BODY(iseq)->param.flags.has_kwrest)) {
771 rest_last = rb_hash_dup(rest_last);
772 }
773 kw_flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT;
774
775 // Unset rest_dupped set by anon_rest as we may need to modify splat in this case
776 args->rest_dupped = false;
777
778 if (ignore_keyword_hash_p(rest_last, iseq, &kw_flag, &converted_keyword_hash)) {
779 if (ISEQ_BODY(iseq)->param.flags.has_rest) {
780 // Only duplicate/modify splat array if it will be used
781 arg_rest_dup(args);
782 rb_ary_pop(args->rest);
783 }
784 else if (arg_setup_type == arg_setup_block && !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
785 // Avoid hash allocation for empty hashes
786 // Copy rest elements except empty keyword hash directly to VM stack
787 flatten_rest_args(ec, args, locals, &ci_flag);
788 keyword_hash = Qnil;
789 kw_flag = 0;
790 }
791 given_argc--;
792 }
793 else if (!ISEQ_BODY(iseq)->param.flags.has_rest) {
794 // Avoid duping rest when not necessary
795 // Copy rest elements and converted keyword hash directly to VM stack
796 flatten_rest_args(ec, args, locals, &ci_flag);
797
798 if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
799 given_argc--;
800 keyword_hash = converted_keyword_hash;
801 }
802 else {
803 locals[args->argc] = converted_keyword_hash;
804 args->argc += 1;
805 keyword_hash = Qnil;
806 kw_flag = 0;
807 }
808 }
809 else {
810 if (rest_last != converted_keyword_hash) {
811 rest_last = converted_keyword_hash;
812 arg_rest_dup(args);
813 RARRAY_ASET(args->rest, len - 1, rest_last);
814 }
815
816 if (ISEQ_BODY(iseq)->param.flags.ruby2_keywords && rest_last) {
817 flag_keyword_hash = rest_last;
818 }
819 else if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
820 arg_rest_dup(args);
821 rb_ary_pop(args->rest);
822 given_argc--;
823 keyword_hash = rest_last;
824 }
825 }
826 }
827 }
828 }
829 else {
830 args->rest = Qfalse;
831
832 if (args->argc > 0 && (kw_flag & VM_CALL_KW_SPLAT)) {
833 // f(**kw)
834 VALUE last_arg = args->argv[args->argc-1];
835 if (ignore_keyword_hash_p(last_arg, iseq, &kw_flag, &converted_keyword_hash)) {
836 args->argc--;
837 given_argc--;
838 }
839 else {
840 if (!(kw_flag & VM_CALL_KW_SPLAT_MUT) && !ISEQ_BODY(iseq)->param.flags.has_kw) {
841 converted_keyword_hash = rb_hash_dup(converted_keyword_hash);
842 kw_flag |= VM_CALL_KW_SPLAT_MUT;
843 }
844
845 if (last_arg != converted_keyword_hash) {
846 last_arg = converted_keyword_hash;
847 args->argv[args->argc-1] = last_arg;
848 }
849
850 if (ISEQ_BODY(iseq)->param.flags.ruby2_keywords) {
851 flag_keyword_hash = last_arg;
852 }
853 else if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
854 args->argc--;
855 given_argc--;
856 keyword_hash = last_arg;
857 }
858 }
859 }
860 }
861
862 if (flag_keyword_hash) {
863 FL_SET_RAW(flag_keyword_hash, RHASH_PASS_AS_KEYWORDS);
864 }
865
866 arg_splat_and_kw_splat_flattened:
867 if (kw_flag && ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg) {
868 rb_raise(rb_eArgError, "no keywords accepted");
869 }
870
871 switch (arg_setup_type) {
872 case arg_setup_method:
873 break; /* do nothing special */
874 case arg_setup_block:
875 if (given_argc == 1 &&
876 allow_autosplat &&
877 !splat_flagged_keyword_hash &&
878 (min_argc > 0 || ISEQ_BODY(iseq)->param.opt_num > 1) &&
879 !ISEQ_BODY(iseq)->param.flags.ambiguous_param0 &&
880 !((ISEQ_BODY(iseq)->param.flags.has_kw ||
881 ISEQ_BODY(iseq)->param.flags.has_kwrest)
882 && max_argc == 1) &&
883 args_check_block_arg0(args)) {
884 given_argc = RARRAY_LENINT(args->rest);
885 }
886 break;
887 }
888
889 /* argc check */
890 if (given_argc < min_argc) {
891 if (arg_setup_type == arg_setup_block) {
892 CHECK_VM_STACK_OVERFLOW(ec->cfp, min_argc);
893 given_argc = min_argc;
894 args_extend(args, min_argc);
895 }
896 else {
897 argument_arity_error(ec, iseq, cme, given_argc, min_argc, max_argc);
898 }
899 }
900
901 if (given_argc > max_argc && max_argc != UNLIMITED_ARGUMENTS) {
902 if (arg_setup_type == arg_setup_block) {
903 /* truncate */
904 args_reduce(args, given_argc - max_argc);
905 given_argc = max_argc;
906 }
907 else {
908 argument_arity_error(ec, iseq, cme, given_argc, min_argc, max_argc);
909 }
910 }
911
912 if (ISEQ_BODY(iseq)->param.flags.has_lead) {
913 args_setup_lead_parameters(args, ISEQ_BODY(iseq)->param.lead_num, locals + 0);
914 }
915
916 if (ISEQ_BODY(iseq)->param.flags.has_rest || ISEQ_BODY(iseq)->param.flags.has_post){
917 args_copy(args);
918 }
919
920 if (ISEQ_BODY(iseq)->param.flags.has_post) {
921 args_setup_post_parameters(args, ISEQ_BODY(iseq)->param.post_num, locals + ISEQ_BODY(iseq)->param.post_start);
922 }
923
924 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
925 int opt = args_setup_opt_parameters(args, ISEQ_BODY(iseq)->param.opt_num, locals + ISEQ_BODY(iseq)->param.lead_num);
926 opt_pc = (int)ISEQ_BODY(iseq)->param.opt_table[opt];
927 }
928
929 if (ISEQ_BODY(iseq)->param.flags.has_rest) {
930 if (UNLIKELY(ISEQ_BODY(iseq)->param.flags.anon_rest && args->argc == 0 && !args->rest && !ISEQ_BODY(iseq)->param.flags.has_post)) {
931 *(locals + ISEQ_BODY(iseq)->param.rest_start) = args->rest = rb_cArray_empty_frozen;
932 args->rest_index = 0;
933 }
934 else {
935 args_setup_rest_parameter(args, locals + ISEQ_BODY(iseq)->param.rest_start);
936 VALUE ary = *(locals + ISEQ_BODY(iseq)->param.rest_start);
937 rb_len_t index = RARRAY_LEN(ary);
938 if (splat_flagged_keyword_hash &&
939 !ISEQ_BODY(iseq)->param.flags.ruby2_keywords &&
940 !ISEQ_BODY(iseq)->param.flags.has_kw &&
941 !ISEQ_BODY(iseq)->param.flags.has_kwrest &&
942 index-- > 0 &&
943 RARRAY_AREF(ary, index) == splat_flagged_keyword_hash) {
944 ((struct RHash *)rest_last)->basic.flags &= ~RHASH_PASS_AS_KEYWORDS;
945 RARRAY_ASET(ary, index, rest_last);
946 }
947 }
948 }
949
950 if (ISEQ_BODY(iseq)->param.flags.has_kw) {
951 VALUE * const klocals = locals + ISEQ_BODY(iseq)->param.keyword->bits_start - ISEQ_BODY(iseq)->param.keyword->num;
952
953 if (args->kw_argv != NULL) {
954 const struct rb_callinfo_kwarg *kw_arg = args->kw_arg;
955 args_setup_kw_parameters(ec, iseq, cme, args->kw_argv, kw_arg->keyword_len, kw_arg->keywords, klocals);
956 }
957 else if (!NIL_P(keyword_hash)) {
958 bool remove_hash_value = false;
959 if (ISEQ_BODY(iseq)->param.flags.has_kwrest) {
960 keyword_hash = check_kwrestarg(keyword_hash, &kw_flag);
961 remove_hash_value = true;
962 }
963 args_setup_kw_parameters_from_kwsplat(ec, iseq, cme, keyword_hash, klocals, remove_hash_value);
964 }
965 else {
966#if VM_CHECK_MODE > 0
967 if (args_argc(args) != 0) {
968 VM_ASSERT(ci_flag & VM_CALL_ARGS_SPLAT);
969 VM_ASSERT(!(ci_flag & (VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT)));
970 VM_ASSERT(!kw_flag);
971 VM_ASSERT(!ISEQ_BODY(iseq)->param.flags.has_rest);
972 VM_ASSERT(RARRAY_LENINT(args->rest) > 0);
973 VM_ASSERT(RB_TYPE_P(rest_last, T_HASH));
974 VM_ASSERT(FL_TEST_RAW(rest_last, RHASH_PASS_AS_KEYWORDS));
975 VM_ASSERT(args_argc(args) == 1);
976 }
977#endif
978 args_setup_kw_parameters(ec, iseq, cme, NULL, 0, NULL, klocals);
979 }
980 }
981 else if (ISEQ_BODY(iseq)->param.flags.has_kwrest) {
982 args_setup_kw_rest_parameter(keyword_hash, locals + ISEQ_BODY(iseq)->param.keyword->rest_start,
983 kw_flag, ISEQ_BODY(iseq)->param.flags.anon_kwrest);
984 }
985 else if (!NIL_P(keyword_hash) && RHASH_SIZE(keyword_hash) > 0 && arg_setup_type == arg_setup_method) {
986 argument_kw_error(ec, iseq, cme, "unknown", rb_hash_keys(keyword_hash));
987 }
988
989 if (ISEQ_BODY(iseq)->param.flags.accepts_no_block) {
990 VALUE given_block;
991 args_setup_block_parameter(ec, calling, &given_block);
992 if (!NIL_P(given_block)) {
993 VALUE exc = rb_exc_new_cstr(rb_eArgError, "no block accepted");
994 rb_exc_raise(set_error_backtrace(ec, iseq, cme, exc));
995 }
996 }
997 else if (ISEQ_BODY(iseq)->param.flags.has_block) {
998 if (ISEQ_BODY(iseq)->local_iseq == iseq) {
999 /* Do nothing */
1000 }
1001 else {
1002 args_setup_block_parameter(ec, calling, locals + ISEQ_BODY(iseq)->param.block_start);
1003 }
1004 }
1005
1006#if 0
1007 {
1008 int i;
1009 for (i=0; i<ISEQ_BODY(iseq)->param.size; i++) {
1010 ruby_debug_printf("local[%d] = %p\n", i, (void *)locals[i]);
1011 }
1012 }
1013#endif
1014
1015 ec->cfp->sp = orig_sp;
1016 return opt_pc;
1017}
1018
1019static VALUE
1020set_error_backtrace(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_callable_method_entry_t *cme, const VALUE exc)
1021{
1022 VALUE at;
1023
1024 if (iseq) {
1025 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL, Qnil /* self */,
1026 VM_BLOCK_HANDLER_NONE /* specval*/, (VALUE) cme /* me or cref */,
1027 ISEQ_BODY(iseq)->iseq_encoded,
1028 ec->cfp->sp, 0, 0 /* stack_max */);
1029 at = rb_ec_backtrace_object(ec);
1030 rb_vm_pop_frame(ec);
1031 }
1032 else {
1033 at = rb_ec_backtrace_object(ec);
1034 }
1035
1036 rb_ivar_set(exc, idBt_locations, at);
1037 rb_exc_set_backtrace(exc, at);
1038 return exc;
1039}
1040
1041static void
1042raise_argument_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_callable_method_entry_t *cme, const VALUE exc)
1043{
1044 set_error_backtrace(ec, iseq, cme, exc);
1045 rb_exc_raise(exc);
1046}
1047
1048static void
1049argument_arity_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_callable_method_entry_t *cme, const int miss_argc, const int min_argc, const int max_argc)
1050{
1051 VALUE exc = rb_arity_error_new(miss_argc, min_argc, max_argc);
1052 if (ISEQ_BODY(iseq)->param.flags.has_kw) {
1053 const struct rb_iseq_param_keyword *const kw = ISEQ_BODY(iseq)->param.keyword;
1054 const ID *keywords = kw->table;
1055 int req_key_num = kw->required_num;
1056 if (req_key_num > 0) {
1057 static const char required[] = "; required keywords";
1058 VALUE mesg = rb_attr_get(exc, idMesg);
1059 rb_str_resize(mesg, RSTRING_LEN(mesg)-1);
1060 rb_str_cat(mesg, required, sizeof(required) - 1 - (req_key_num == 1));
1061 rb_str_cat_cstr(mesg, ":");
1062 do {
1063 rb_str_cat_cstr(mesg, " ");
1064 rb_str_append(mesg, rb_id2str(*keywords++));
1065 rb_str_cat_cstr(mesg, ",");
1066 } while (--req_key_num);
1067 RSTRING_PTR(mesg)[RSTRING_LEN(mesg)-1] = ')';
1068 }
1069 }
1070 raise_argument_error(ec, iseq, cme, exc);
1071}
1072
1073static void
1074argument_kw_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_callable_method_entry_t *cme, const char *error, const VALUE keys)
1075{
1076 raise_argument_error(ec, iseq, cme, rb_keyword_error_new(error, keys));
1077}
1078
1079static VALUE
1080vm_to_proc(VALUE proc)
1081{
1082 if (UNLIKELY(!rb_obj_is_proc(proc))) {
1083 VALUE b;
1084 const rb_callable_method_entry_t *me =
1085 rb_callable_method_entry_with_refinements(CLASS_OF(proc), idTo_proc, NULL);
1086
1087 if (me) {
1088 b = rb_vm_call0(GET_EC(), proc, idTo_proc, 0, NULL, me, RB_NO_KEYWORDS);
1089 }
1090 else {
1091 /* NOTE: calling method_missing */
1092 b = rb_check_convert_type_with_id(proc, T_DATA, "Proc", idTo_proc);
1093 }
1094
1095 if (NIL_P(b) || !rb_obj_is_proc(b)) {
1096 if (me) {
1097 rb_cant_convert_invalid_return(proc, "Proc", "to_proc", b);
1098 }
1099 else {
1100 rb_no_implicit_conversion(proc, "Proc");
1101 }
1102 }
1103 return b;
1104 }
1105 else {
1106 return proc;
1107 }
1108}
1109
1110static VALUE
1111refine_sym_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
1112{
1113 VALUE obj;
1114 ID mid;
1115 const rb_callable_method_entry_t *me = 0; /* for hidden object case */
1117 const VALUE symbol = RARRAY_AREF(callback_arg, 0);
1118 const VALUE refinements = RARRAY_AREF(callback_arg, 1);
1119 int kw_splat = RB_PASS_CALLED_KEYWORDS;
1120 VALUE klass;
1121
1122 if (argc-- < 1) {
1123 rb_raise(rb_eArgError, "no receiver given");
1124 }
1125 obj = *argv++;
1126
1127 mid = SYM2ID(symbol);
1128 for (klass = CLASS_OF(obj); klass; klass = RCLASS_SUPER(klass)) {
1129 me = rb_callable_method_entry(klass, mid);
1130 if (me) {
1131 me = rb_resolve_refined_method_callable(refinements, me);
1132 if (me) break;
1133 }
1134 }
1135
1136 ec = GET_EC();
1137 if (!NIL_P(blockarg)) {
1138 vm_passed_block_handler_set(ec, blockarg);
1139 }
1140 if (!me) {
1141 return method_missing(ec, obj, mid, argc, argv, MISSING_NOENTRY, kw_splat);
1142 }
1143 return rb_vm_call0(ec, obj, mid, argc, argv, me, kw_splat);
1144}
1145
1146static VALUE
1147vm_caller_setup_arg_block(const rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
1148 const struct rb_callinfo *ci, const rb_iseq_t *blockiseq, const int is_super)
1149{
1150 if (vm_ci_flag(ci) & VM_CALL_ARGS_BLOCKARG) {
1151 VALUE block_code = *(--reg_cfp->sp);
1152
1153 if (NIL_P(block_code)) {
1154 return VM_BLOCK_HANDLER_NONE;
1155 }
1156 else if (block_code == rb_block_param_proxy) {
1157 return VM_CF_BLOCK_HANDLER(reg_cfp);
1158 }
1159 else if (SYMBOL_P(block_code) && rb_method_basic_definition_p(rb_cSymbol, idTo_proc)) {
1160 const rb_cref_t *cref = vm_env_cref(reg_cfp->ep);
1161 if (cref && !NIL_P(cref->refinements)) {
1162 VALUE ref = cref->refinements;
1163 /* the table caches funcs only for the Ractor that owns it: a func
1164 * stored from another Ractor lives in that Ractor's objspace, whose
1165 * GC does not see the table's reference to it */
1166 VALUE func = rb_gc_obj_foreign_p(ref) ? Qnil : rb_hash_lookup(ref, block_code);
1167 if (NIL_P(func)) {
1168 /* TODO: limit cached funcs */
1169 VALUE callback_arg = rb_ary_hidden_new(2);
1170 rb_ary_push(callback_arg, block_code);
1171 rb_ary_push(callback_arg, ref);
1172 OBJ_FREEZE(callback_arg);
1173 func = rb_func_lambda_new(refine_sym_proc_call, callback_arg, 1, UNLIMITED_ARGUMENTS);
1174 /* the table is frozen when it belongs to a Proc#refined memo; skip the cache then */
1175 if (!OBJ_FROZEN(ref) && !rb_gc_obj_foreign_p(ref)) {
1176 rb_hash_aset(ref, block_code, func);
1177 }
1178 }
1179 block_code = func;
1180 }
1181 return block_code;
1182 }
1183 else {
1184 return vm_to_proc(block_code);
1185 }
1186 }
1187 else if (blockiseq != NULL) { /* likely */
1188 struct rb_captured_block *captured = VM_CFP_TO_CAPTURED_BLOCK(reg_cfp);
1189 captured->code.iseq = blockiseq;
1190 return VM_BH_FROM_ISEQ_BLOCK(captured);
1191 }
1192 else {
1193 if (is_super) {
1194 return GET_BLOCK_HANDLER();
1195 }
1196 else {
1197 return VM_BLOCK_HANDLER_NONE;
1198 }
1199 }
1200}
1201
1202static void vm_adjust_stack_forwarding(const struct rb_execution_context_struct *ec, struct rb_control_frame_struct *cfp, int argc, VALUE splat);
1203
1204static VALUE
1205vm_caller_setup_fwd_args(const rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
1206 CALL_DATA cd, const rb_iseq_t *blockiseq, const int is_super,
1207 struct rb_forwarding_call_data *adjusted_cd, struct rb_callinfo *adjusted_ci)
1208{
1209 CALL_INFO site_ci = cd->ci;
1210 VALUE bh = Qundef;
1211
1212 RUBY_ASSERT(ISEQ_BODY(ISEQ_BODY(CFP_ISEQ(reg_cfp))->local_iseq)->param.flags.forwardable);
1213 CALL_INFO caller_ci = (CALL_INFO)TOPN(0);
1214
1215 unsigned int site_argc = vm_ci_argc(site_ci);
1216 unsigned int site_flag = vm_ci_flag(site_ci);
1217 ID site_mid = vm_ci_mid(site_ci);
1218
1219 unsigned int caller_argc = vm_ci_argc(caller_ci);
1220 unsigned int caller_flag = vm_ci_flag(caller_ci);
1221 const struct rb_callinfo_kwarg * kw = vm_ci_kwarg(caller_ci);
1222
1223 VALUE splat = Qfalse;
1224
1225 if (site_flag & VM_CALL_ARGS_SPLAT) {
1226 // If we're called with args_splat, the top 1 should be an array
1227 splat = TOPN(1);
1228 site_argc += (RARRAY_LEN(splat) - 1);
1229 }
1230
1231 // Need to setup the block in case of e.g. `super { :block }`
1232 if (is_super && blockiseq) {
1233 bh = vm_caller_setup_arg_block(ec, GET_CFP(), site_ci, blockiseq, is_super);
1234 }
1235 else {
1236 bh = VM_ENV_BLOCK_HANDLER(GET_LEP());
1237 }
1238
1239 vm_adjust_stack_forwarding(ec, GET_CFP(), caller_argc, splat);
1240
1241 *adjusted_ci = VM_CI_ON_STACK(
1242 site_mid,
1243 ((caller_flag & ~(VM_CALL_ARGS_SIMPLE | VM_CALL_FCALL)) |
1244 (site_flag & (VM_CALL_FCALL | VM_CALL_FORWARDING))),
1245 site_argc + caller_argc,
1246 kw
1247 );
1248
1249 adjusted_cd->cd.ci = adjusted_ci;
1250 adjusted_cd->cd.cc = cd->cc;
1251 adjusted_cd->caller_ci = caller_ci;
1252
1253 return bh;
1254}
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
Definition fl_type.h:133
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:131
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:126
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:678
VALUE rb_cSymbol
Symbol class.
Definition string.c:86
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_resize(VALUE ary, rb_len_t len)
Expands or shrinks the passed array to the passed length.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_pop(VALUE ary)
Destructively deletes an element from the end of the passed array and returns what was deleted.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_new_from_values(rb_len_t n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:386
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:4005
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1695
VALUE rb_str_cat(VALUE dst, const char *src, rb_len_t srclen)
Destructively appends the passed contents to the string.
Definition string.c:3773
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1681
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:2157
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well... Let us hesitate from describing what a "basic definition" is.
Definition vm_method.c:3615
int len
Length of the buffer.
Definition io.h:8
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
Definition iterator.h:58
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:375
#define ALLOCA_N(type, n)
Definition memory.h:295
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:50
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:280
#define RARRAY_AREF(a, i)
References an object in an array.
Definition rarray.h:421
static void RARRAY_ASET(VALUE ary, rb_len_t i, VALUE v)
Assigns an object in an array.
Definition rarray.h:385
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:51
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:57
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:67
#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
Definition hash.h:54
Definition method.h:63
CREF (Class REFerence)
Definition method.h:45
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static 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