Ruby: vm_args.c Source File

Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)

vm_args.c
1/**********************************************************************
2
3 vm_args.c - process method call arguments.
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 VALUE exc));
12NORETURN(static void argument_arity_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, 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 char *error, const VALUE keys));
14 VALUE 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
19 struct 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#define KW_SPECIFIED_BITS_MAX (32-1) /* TODO: 32 -> Fixnum's max bits */
322
323static void
324args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *const iseq,
325 VALUE *const passed_values, const int passed_keyword_len, const VALUE *const passed_keywords,
326 VALUE *const locals)
327{
328 const ID *acceptable_keywords = ISEQ_BODY(iseq)->param.keyword->table;
329 const int req_key_num = ISEQ_BODY(iseq)->param.keyword->required_num;
330 const int key_num = ISEQ_BODY(iseq)->param.keyword->num;
331 const VALUE * const default_values = ISEQ_BODY(iseq)->param.keyword->default_values;
332 VALUE missing = 0;
333 int i, di, found = 0;
334 int unspecified_bits = 0;
335 VALUE unspecified_bits_value = Qnil;
336
337 for (i=0; i<req_key_num; i++) {
338 ID key = acceptable_keywords[i];
339 if (args_setup_kw_parameters_lookup(key, &locals[i], passed_keywords, passed_values, passed_keyword_len)) {
340 found++;
341 }
342 else {
343 if (!missing) missing = rb_ary_hidden_new(1);
344 rb_ary_push(missing, ID2SYM(key));
345 }
346 }
347
348 if (missing) argument_kw_error(ec, iseq, "missing", missing);
349
350 for (di=0; i<key_num; i++, di++) {
351 if (args_setup_kw_parameters_lookup(acceptable_keywords[i], &locals[i], passed_keywords, passed_values, passed_keyword_len)) {
352 found++;
353 }
354 else {
355 if (UNDEF_P(default_values[di])) {
356 locals[i] = Qnil;
357
358 if (LIKELY(i < KW_SPECIFIED_BITS_MAX)) {
359 unspecified_bits |= 0x01 << di;
360 }
361 else {
362 if (NIL_P(unspecified_bits_value)) {
363 /* fixnum -> hash */
364 int j;
365 unspecified_bits_value = rb_hash_new();
366
367 for (j=0; j<KW_SPECIFIED_BITS_MAX; j++) {
368 if (unspecified_bits & (0x01 << j)) {
369 rb_hash_aset(unspecified_bits_value, INT2FIX(j), Qtrue);
370 }
371 }
372 }
373 rb_hash_aset(unspecified_bits_value, INT2FIX(di), Qtrue);
374 }
375 }
376 else {
377 locals[i] = default_values[di];
378 }
379 }
380 }
381
382 if (ISEQ_BODY(iseq)->param.flags.has_kwrest) {
383 const int rest_hash_index = key_num + 1;
384 locals[rest_hash_index] = make_rest_kw_hash(passed_keywords, passed_keyword_len, passed_values);
385 }
386 else {
387 if (found != passed_keyword_len) {
388 VALUE keys = make_unknown_kw_hash(passed_keywords, passed_keyword_len, passed_values);
389 argument_kw_error(ec, iseq, "unknown", keys);
390 }
391 }
392
393 if (NIL_P(unspecified_bits_value)) {
394 unspecified_bits_value = INT2FIX(unspecified_bits);
395 }
396 locals[key_num] = unspecified_bits_value;
397}
398
399static inline void
400args_setup_kw_rest_parameter(VALUE keyword_hash, VALUE *locals, int kw_flag)
401{
402 if (NIL_P(keyword_hash)) {
403 keyword_hash = rb_hash_new();
404 }
405 else if (!(kw_flag & VM_CALL_KW_SPLAT_MUT)) {
406 keyword_hash = rb_hash_dup(keyword_hash);
407 }
408 locals[0] = keyword_hash;
409}
410
411static inline void
412args_setup_block_parameter(const rb_execution_context_t *ec, struct rb_calling_info *calling, VALUE *locals)
413{
414 VALUE block_handler = calling->block_handler;
415 *locals = rb_vm_bh_to_procval(ec, block_handler);
416}
417
418 struct fill_values_arg {
419 VALUE *keys;
420 VALUE *vals;
421 int argc;
422};
423
424static int
425fill_keys_values(st_data_t key, st_data_t val, st_data_t ptr)
426{
427 struct fill_values_arg *arg = (struct fill_values_arg *)ptr;
428 int i = arg->argc++;
429 arg->keys[i] = (VALUE)key;
430 arg->vals[i] = (VALUE)val;
431 return ST_CONTINUE;
432}
433
434static inline int
435ignore_keyword_hash_p(VALUE keyword_hash, const rb_iseq_t * const iseq, unsigned int * kw_flag, VALUE * converted_keyword_hash)
436{
437 if (keyword_hash == Qnil) {
438 goto ignore;
439 }
440 else if (!RB_TYPE_P(keyword_hash, T_HASH)) {
441 keyword_hash = rb_to_hash_type(keyword_hash);
442 }
443
444 if (!(*kw_flag & VM_CALL_KW_SPLAT_MUT) &&
445 (ISEQ_BODY(iseq)->param.flags.has_kwrest ||
446 ISEQ_BODY(iseq)->param.flags.ruby2_keywords)) {
447 *kw_flag |= VM_CALL_KW_SPLAT_MUT;
448 keyword_hash = rb_hash_dup(keyword_hash);
449 }
450 *converted_keyword_hash = keyword_hash;
451
452 if (!(ISEQ_BODY(iseq)->param.flags.has_kw) &&
453 !(ISEQ_BODY(iseq)->param.flags.has_kwrest) &&
454 RHASH_EMPTY_P(keyword_hash)) {
455 ignore:
456 *kw_flag &= ~(VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT);
457 return 1;
458 }
459 else {
460 return 0;
461 }
462}
463
464static VALUE
465check_kwrestarg(VALUE keyword_hash, unsigned int *kw_flag)
466{
467 if (!(*kw_flag & VM_CALL_KW_SPLAT_MUT)) {
468 *kw_flag |= VM_CALL_KW_SPLAT_MUT;
469 return rb_hash_dup(keyword_hash);
470 }
471 else {
472 return keyword_hash;
473 }
474}
475
476static int
477setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * const iseq,
478 struct rb_calling_info *const calling,
479 const struct rb_callinfo *ci,
480 VALUE * const locals, const enum arg_setup_type arg_setup_type)
481{
482 const int min_argc = ISEQ_BODY(iseq)->param.lead_num + ISEQ_BODY(iseq)->param.post_num;
483 const int max_argc = (ISEQ_BODY(iseq)->param.flags.has_rest == FALSE) ? min_argc + ISEQ_BODY(iseq)->param.opt_num : UNLIMITED_ARGUMENTS;
484 int given_argc;
485 unsigned int kw_flag = vm_ci_flag(ci) & (VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT);
486 int opt_pc = 0, allow_autosplat = !kw_flag;
487 struct args_info args_body, *args;
488 VALUE keyword_hash = Qnil;
489 VALUE * const orig_sp = ec->cfp->sp;
490 unsigned int i;
491 VALUE flag_keyword_hash = 0;
492 VALUE splat_flagged_keyword_hash = 0;
493 VALUE converted_keyword_hash = 0;
494 VALUE rest_last = 0;
495
496 vm_check_canary(ec, orig_sp);
497 /*
498 * Extend SP for GC.
499 *
500 * [pushed values] [uninitialized values]
501 * <- ci->argc -->
502 * <- ISEQ_BODY(iseq)->param.size------------>
503 * ^ locals ^ sp
504 *
505 * =>
506 * [pushed values] [initialized values ]
507 * <- ci->argc -->
508 * <- ISEQ_BODY(iseq)->param.size------------>
509 * ^ locals ^ sp
510 */
511 for (i=calling->argc; i<ISEQ_BODY(iseq)->param.size; i++) {
512 locals[i] = Qnil;
513 }
514 ec->cfp->sp = &locals[i];
515
516 /* setup args */
517 args = &args_body;
518 given_argc = args->argc = calling->argc;
519 args->argv = locals;
520 args->rest_dupped = FALSE;
521
522 if (kw_flag & VM_CALL_KWARG) {
523 args->kw_arg = vm_ci_kwarg(ci);
524
525 if (ISEQ_BODY(iseq)->param.flags.has_kw) {
526 int kw_len = args->kw_arg->keyword_len;
527 /* copy kw_argv */
528 args->kw_argv = ALLOCA_N(VALUE, kw_len);
529 args->argc -= kw_len;
530 given_argc -= kw_len;
531 MEMCPY(args->kw_argv, locals + args->argc, VALUE, kw_len);
532 }
533 else {
534 args->kw_argv = NULL;
535 given_argc = args_kw_argv_to_hash(args);
536 kw_flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT;
537 }
538 }
539 else {
540 args->kw_arg = NULL;
541 args->kw_argv = NULL;
542 }
543
544 if ((vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) && (vm_ci_flag(ci) & VM_CALL_KW_SPLAT)) {
545 // f(*a, **kw)
546 args->rest_index = 0;
547 keyword_hash = locals[--args->argc];
548 args->rest = locals[--args->argc];
549
550 if (ignore_keyword_hash_p(keyword_hash, iseq, &kw_flag, &converted_keyword_hash)) {
551 keyword_hash = Qnil;
552 }
553 else if (UNLIKELY(ISEQ_BODY(iseq)->param.flags.ruby2_keywords)) {
554 converted_keyword_hash = check_kwrestarg(converted_keyword_hash, &kw_flag);
555 flag_keyword_hash = converted_keyword_hash;
556 arg_rest_dup(args);
557 rb_ary_push(args->rest, converted_keyword_hash);
558 keyword_hash = Qnil;
559 }
560 else if (!ISEQ_BODY(iseq)->param.flags.has_kwrest && !ISEQ_BODY(iseq)->param.flags.has_kw) {
561 converted_keyword_hash = check_kwrestarg(converted_keyword_hash, &kw_flag);
562 arg_rest_dup(args);
563 rb_ary_push(args->rest, converted_keyword_hash);
564 keyword_hash = Qnil;
565 } else {
566 keyword_hash = converted_keyword_hash;
567 }
568
569 int len = RARRAY_LENINT(args->rest);
570 given_argc += len - 2;
571 }
572 else if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
573 // f(*a)
574 args->rest_index = 0;
575 args->rest = locals[--args->argc];
576 int len = RARRAY_LENINT(args->rest);
577 given_argc += len - 1;
578
579 if (!kw_flag && len > 0) {
580 rest_last = RARRAY_AREF(args->rest, len - 1);
581 if (RB_TYPE_P(rest_last, T_HASH) && FL_TEST_RAW(rest_last, RHASH_PASS_AS_KEYWORDS)) {
582 // def f(**kw); a = [..., kw]; g(*a)
583 splat_flagged_keyword_hash = rest_last;
584 rest_last = rb_hash_dup(rest_last);
585 kw_flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT;
586
587 if (ignore_keyword_hash_p(rest_last, iseq, &kw_flag, &converted_keyword_hash)) {
588 arg_rest_dup(args);
589 rb_ary_pop(args->rest);
590 given_argc--;
591 }
592 else {
593 if (rest_last != converted_keyword_hash) {
594 rest_last = converted_keyword_hash;
595 arg_rest_dup(args);
596 RARRAY_ASET(args->rest, len - 1, rest_last);
597 }
598
599 if (ISEQ_BODY(iseq)->param.flags.ruby2_keywords && rest_last) {
600 flag_keyword_hash = rest_last;
601 }
602 else if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
603 arg_rest_dup(args);
604 rb_ary_pop(args->rest);
605 given_argc--;
606 keyword_hash = rest_last;
607 }
608 }
609 }
610 }
611 }
612 else {
613 args->rest = Qfalse;
614
615 if (args->argc > 0 && (kw_flag & VM_CALL_KW_SPLAT)) {
616 // f(**kw)
617 VALUE last_arg = args->argv[args->argc-1];
618 if (ignore_keyword_hash_p(last_arg, iseq, &kw_flag, &converted_keyword_hash)) {
619 args->argc--;
620 given_argc--;
621 }
622 else {
623 if (!(kw_flag & VM_CALL_KW_SPLAT_MUT)) {
624 converted_keyword_hash = rb_hash_dup(converted_keyword_hash);
625 }
626
627 if (last_arg != converted_keyword_hash) {
628 last_arg = converted_keyword_hash;
629 args->argv[args->argc-1] = last_arg;
630 }
631
632 if (ISEQ_BODY(iseq)->param.flags.ruby2_keywords) {
633 flag_keyword_hash = last_arg;
634 }
635 else if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
636 args->argc--;
637 given_argc--;
638 keyword_hash = last_arg;
639 }
640 }
641 }
642 }
643
644 if (flag_keyword_hash) {
645 FL_SET_RAW(flag_keyword_hash, RHASH_PASS_AS_KEYWORDS);
646 }
647
648 if (kw_flag && ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg) {
649 rb_raise(rb_eArgError, "no keywords accepted");
650 }
651
652 switch (arg_setup_type) {
653 case arg_setup_method:
654 break; /* do nothing special */
655 case arg_setup_block:
656 if (given_argc == 1 &&
657 allow_autosplat &&
658 !splat_flagged_keyword_hash &&
659 (min_argc > 0 || ISEQ_BODY(iseq)->param.opt_num > 1) &&
660 !ISEQ_BODY(iseq)->param.flags.ambiguous_param0 &&
661 !((ISEQ_BODY(iseq)->param.flags.has_kw ||
662 ISEQ_BODY(iseq)->param.flags.has_kwrest)
663 && max_argc == 1) &&
664 args_check_block_arg0(args)) {
665 given_argc = RARRAY_LENINT(args->rest);
666 }
667 break;
668 }
669
670 /* argc check */
671 if (given_argc < min_argc) {
672 if (arg_setup_type == arg_setup_block) {
673 CHECK_VM_STACK_OVERFLOW(ec->cfp, min_argc);
674 given_argc = min_argc;
675 args_extend(args, min_argc);
676 }
677 else {
678 argument_arity_error(ec, iseq, given_argc, min_argc, max_argc);
679 }
680 }
681
682 if (given_argc > max_argc && max_argc != UNLIMITED_ARGUMENTS) {
683 if (arg_setup_type == arg_setup_block) {
684 /* truncate */
685 args_reduce(args, given_argc - max_argc);
686 given_argc = max_argc;
687 }
688 else {
689 argument_arity_error(ec, iseq, given_argc, min_argc, max_argc);
690 }
691 }
692
693 if (ISEQ_BODY(iseq)->param.flags.has_lead) {
694 args_setup_lead_parameters(args, ISEQ_BODY(iseq)->param.lead_num, locals + 0);
695 }
696
697 if (ISEQ_BODY(iseq)->param.flags.has_rest || ISEQ_BODY(iseq)->param.flags.has_post){
698 args_copy(args);
699 }
700
701 if (ISEQ_BODY(iseq)->param.flags.has_post) {
702 args_setup_post_parameters(args, ISEQ_BODY(iseq)->param.post_num, locals + ISEQ_BODY(iseq)->param.post_start);
703 }
704
705 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
706 int opt = args_setup_opt_parameters(args, ISEQ_BODY(iseq)->param.opt_num, locals + ISEQ_BODY(iseq)->param.lead_num);
707 opt_pc = (int)ISEQ_BODY(iseq)->param.opt_table[opt];
708 }
709
710 if (ISEQ_BODY(iseq)->param.flags.has_rest) {
711 args_setup_rest_parameter(args, locals + ISEQ_BODY(iseq)->param.rest_start);
712 VALUE ary = *(locals + ISEQ_BODY(iseq)->param.rest_start);
713 VALUE index = RARRAY_LEN(ary) - 1;
714 if (splat_flagged_keyword_hash &&
715 !ISEQ_BODY(iseq)->param.flags.ruby2_keywords &&
716 !ISEQ_BODY(iseq)->param.flags.has_kw &&
717 !ISEQ_BODY(iseq)->param.flags.has_kwrest &&
718 RARRAY_AREF(ary, index) == splat_flagged_keyword_hash) {
719 ((struct RHash *)rest_last)->basic.flags &= ~RHASH_PASS_AS_KEYWORDS;
720 RARRAY_ASET(ary, index, rest_last);
721 }
722 }
723
724 if (ISEQ_BODY(iseq)->param.flags.has_kw) {
725 VALUE * const klocals = locals + ISEQ_BODY(iseq)->param.keyword->bits_start - ISEQ_BODY(iseq)->param.keyword->num;
726
727 if (args->kw_argv != NULL) {
728 const struct rb_callinfo_kwarg *kw_arg = args->kw_arg;
729 args_setup_kw_parameters(ec, iseq, args->kw_argv, kw_arg->keyword_len, kw_arg->keywords, klocals);
730 }
731 else if (!NIL_P(keyword_hash)) {
732 int kw_len = rb_long2int(RHASH_SIZE(keyword_hash));
733 struct fill_values_arg arg;
734 /* copy kw_argv */
735 arg.keys = args->kw_argv = ALLOCA_N(VALUE, kw_len * 2);
736 arg.vals = arg.keys + kw_len;
737 arg.argc = 0;
738 rb_hash_foreach(keyword_hash, fill_keys_values, (VALUE)&arg);
739 VM_ASSERT(arg.argc == kw_len);
740 args_setup_kw_parameters(ec, iseq, arg.vals, kw_len, arg.keys, klocals);
741 }
742 else {
743 VM_ASSERT(args_argc(args) == 0);
744 args_setup_kw_parameters(ec, iseq, NULL, 0, NULL, klocals);
745 }
746 }
747 else if (ISEQ_BODY(iseq)->param.flags.has_kwrest) {
748 args_setup_kw_rest_parameter(keyword_hash, locals + ISEQ_BODY(iseq)->param.keyword->rest_start, kw_flag);
749 }
750 else if (!NIL_P(keyword_hash) && RHASH_SIZE(keyword_hash) > 0 && arg_setup_type == arg_setup_method) {
751 argument_kw_error(ec, iseq, "unknown", rb_hash_keys(keyword_hash));
752 }
753
754 if (ISEQ_BODY(iseq)->param.flags.has_block) {
755 if (ISEQ_BODY(iseq)->local_iseq == iseq) {
756 /* Do nothing */
757 }
758 else {
759 args_setup_block_parameter(ec, calling, locals + ISEQ_BODY(iseq)->param.block_start);
760 }
761 }
762
763#if 0
764 {
765 int i;
766 for (i=0; i<ISEQ_BODY(iseq)->param.size; i++) {
767 ruby_debug_printf("local[%d] = %p\n", i, (void *)locals[i]);
768 }
769 }
770#endif
771
772 ec->cfp->sp = orig_sp;
773 return opt_pc;
774}
775
776static void
777raise_argument_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const VALUE exc)
778{
779 VALUE at;
780
781 if (iseq) {
782 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL, Qnil /* self */,
783 VM_BLOCK_HANDLER_NONE /* specval*/, Qfalse /* me or cref */,
784 ISEQ_BODY(iseq)->iseq_encoded,
785 ec->cfp->sp, 0, 0 /* stack_max */);
786 at = rb_ec_backtrace_object(ec);
787 rb_backtrace_use_iseq_first_lineno_for_last_location(at);
788 rb_vm_pop_frame(ec);
789 }
790 else {
791 at = rb_ec_backtrace_object(ec);
792 }
793
794 rb_ivar_set(exc, idBt_locations, at);
795 rb_exc_set_backtrace(exc, at);
796 rb_exc_raise(exc);
797}
798
799static void
800argument_arity_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const int miss_argc, const int min_argc, const int max_argc)
801{
802 VALUE exc = rb_arity_error_new(miss_argc, min_argc, max_argc);
803 if (ISEQ_BODY(iseq)->param.flags.has_kw) {
804 const struct rb_iseq_param_keyword *const kw = ISEQ_BODY(iseq)->param.keyword;
805 const ID *keywords = kw->table;
806 int req_key_num = kw->required_num;
807 if (req_key_num > 0) {
808 static const char required[] = "; required keywords";
809 VALUE mesg = rb_attr_get(exc, idMesg);
810 rb_str_resize(mesg, RSTRING_LEN(mesg)-1);
811 rb_str_cat(mesg, required, sizeof(required) - 1 - (req_key_num == 1));
812 rb_str_cat_cstr(mesg, ":");
813 do {
814 rb_str_cat_cstr(mesg, " ");
815 rb_str_append(mesg, rb_id2str(*keywords++));
816 rb_str_cat_cstr(mesg, ",");
817 } while (--req_key_num);
818 RSTRING_PTR(mesg)[RSTRING_LEN(mesg)-1] = ')';
819 }
820 }
821 raise_argument_error(ec, iseq, exc);
822}
823
824static void
825argument_kw_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const char *error, const VALUE keys)
826{
827 raise_argument_error(ec, iseq, rb_keyword_error_new(error, keys));
828}
829
830static VALUE
831vm_to_proc(VALUE proc)
832{
833 if (UNLIKELY(!rb_obj_is_proc(proc))) {
834 VALUE b;
835 const rb_callable_method_entry_t *me =
836 rb_callable_method_entry_with_refinements(CLASS_OF(proc), idTo_proc, NULL);
837
838 if (me) {
839 b = rb_vm_call0(GET_EC(), proc, idTo_proc, 0, NULL, me, RB_NO_KEYWORDS);
840 }
841 else {
842 /* NOTE: calling method_missing */
843 b = rb_check_convert_type_with_id(proc, T_DATA, "Proc", idTo_proc);
844 }
845
846 if (NIL_P(b) || !rb_obj_is_proc(b)) {
847 rb_raise(rb_eTypeError,
848 "wrong argument type %s (expected Proc)",
849 rb_obj_classname(proc));
850 }
851 return b;
852 }
853 else {
854 return proc;
855 }
856}
857
858static VALUE
859refine_sym_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
860{
861 VALUE obj;
862 ID mid;
863 const rb_callable_method_entry_t *me = 0; /* for hidden object case */
864 rb_execution_context_t *ec;
865 const VALUE symbol = RARRAY_AREF(callback_arg, 0);
866 const VALUE refinements = RARRAY_AREF(callback_arg, 1);
867 int kw_splat = RB_PASS_CALLED_KEYWORDS;
868 VALUE klass;
869
870 if (argc-- < 1) {
871 rb_raise(rb_eArgError, "no receiver given");
872 }
873 obj = *argv++;
874
875 mid = SYM2ID(symbol);
876 for (klass = CLASS_OF(obj); klass; klass = RCLASS_SUPER(klass)) {
877 me = rb_callable_method_entry(klass, mid);
878 if (me) {
879 me = rb_resolve_refined_method_callable(refinements, me);
880 if (me) break;
881 }
882 }
883
884 ec = GET_EC();
885 if (!NIL_P(blockarg)) {
886 vm_passed_block_handler_set(ec, blockarg);
887 }
888 if (!me) {
889 return method_missing(ec, obj, mid, argc, argv, MISSING_NOENTRY, kw_splat);
890 }
891 return rb_vm_call0(ec, obj, mid, argc, argv, me, kw_splat);
892}
893
894static VALUE
895vm_caller_setup_arg_block(const rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
896 const struct rb_callinfo *ci, const rb_iseq_t *blockiseq, const int is_super)
897{
898 if (vm_ci_flag(ci) & VM_CALL_ARGS_BLOCKARG) {
899 VALUE block_code = *(--reg_cfp->sp);
900
901 if (NIL_P(block_code)) {
902 return VM_BLOCK_HANDLER_NONE;
903 }
904 else if (block_code == rb_block_param_proxy) {
905 return VM_CF_BLOCK_HANDLER(reg_cfp);
906 }
907 else if (SYMBOL_P(block_code) && rb_method_basic_definition_p(rb_cSymbol, idTo_proc)) {
908 const rb_cref_t *cref = vm_env_cref(reg_cfp->ep);
909 if (cref && !NIL_P(cref->refinements)) {
910 VALUE ref = cref->refinements;
911 VALUE func = rb_hash_lookup(ref, block_code);
912 if (NIL_P(func)) {
913 /* TODO: limit cached funcs */
914 VALUE callback_arg = rb_ary_hidden_new(2);
915 rb_ary_push(callback_arg, block_code);
916 rb_ary_push(callback_arg, ref);
917 OBJ_FREEZE_RAW(callback_arg);
918 func = rb_func_lambda_new(refine_sym_proc_call, callback_arg, 1, UNLIMITED_ARGUMENTS);
919 rb_hash_aset(ref, block_code, func);
920 }
921 block_code = func;
922 }
923 return block_code;
924 }
925 else {
926 return vm_to_proc(block_code);
927 }
928 }
929 else if (blockiseq != NULL) { /* likely */
930 struct rb_captured_block *captured = VM_CFP_TO_CAPTURED_BLOCK(reg_cfp);
931 captured->code.iseq = blockiseq;
932 return VM_BH_FROM_ISEQ_BLOCK(captured);
933 }
934 else {
935 if (is_super) {
936 return GET_BLOCK_HANDLER();
937 }
938 else {
939 return VM_BLOCK_HANDLER_NONE;
940 }
941 }
942}
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
OBJ_FREEZE_RAW
#define OBJ_FREEZE_RAW
Old name of RB_OBJ_FREEZE_RAW.
Definition fl_type.h:136
SYM2ID
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
T_DATA
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
T_HASH
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
FL_TEST_RAW
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:132
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
FL_SET_RAW
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:130
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1344
rb_cSymbol
VALUE rb_cSymbol
Symbol class.
Definition string.c:79
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
rb_obj_is_proc
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:119
rb_str_append
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:3411
rb_str_cat_cstr
#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:1656
rb_ivar_set
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1854
len
int len
Length of the buffer.
Definition io.h:8
RB_BLOCK_CALL_FUNC_ARGLIST
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
Definition iterator.h:58
rb_long2int
#define rb_long2int
Just another name of rb_long2int_inline.
Definition long.h:62
MEMCPY
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:366
ALLOCA_N
#define ALLOCA_N(type, n)
Definition memory.h:286
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
RARRAY_LENINT
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:281
RARRAY_ASET
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
Definition rarray.h:386
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:403
RARRAY_CONST_PTR
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
RCLASS_SUPER
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
RHASH_SIZE
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:69
RHASH_EMPTY_P
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:79
rb_obj_classname
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:417
RB_PASS_CALLED_KEYWORDS
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
Definition scan_args.h:78
RB_NO_KEYWORDS
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
RHash
Definition hash.h:53
args_info
Definition vm_args.c:19
fill_values_arg
Definition vm_args.c:418
rb_callinfo_kwarg
Definition vm_callinfo.h:44
rb_callinfo
Definition vm_callinfo.h:61
rb_calling_info
Definition vm_core.h:280
rb_captured_block
Definition vm_core.h:821
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40