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