Ruby: vm_args.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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);
17#if !defined(_MSC_VER) || !defined(MJIT_HEADER)
18MJIT_FUNC_EXPORTED
19#endif
20const rb_callable_method_entry_t *rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
21
22 struct args_info {
23 /* basic args info */
24 VALUE *argv;
25 int argc;
26
27 /* additional args info */
28 int rest_index;
29 int rest_dupped;
30 const struct rb_callinfo_kwarg *kw_arg;
31 VALUE *kw_argv;
32 VALUE rest;
33};
34
35enum arg_setup_type {
36 arg_setup_method,
37 arg_setup_block
38};
39
40static inline void
41arg_rest_dup(struct args_info *args)
42{
43 if (!args->rest_dupped) {
44 args->rest = rb_ary_dup(args->rest);
45 args->rest_dupped = TRUE;
46 }
47}
48
49static inline int
50args_argc(struct args_info *args)
51{
52 if (args->rest == Qfalse) {
53 return args->argc;
54 }
55 else {
56 return args->argc + RARRAY_LENINT(args->rest) - args->rest_index;
57 }
58}
59
60static inline void
61args_extend(struct args_info *args, const int min_argc)
62{
63 int i;
64
65 if (args->rest) {
66 arg_rest_dup(args);
67 VM_ASSERT(args->rest_index == 0);
68 for (i=args->argc + RARRAY_LENINT(args->rest); i<min_argc; i++) {
69 rb_ary_push(args->rest, Qnil);
70 }
71 }
72 else {
73 for (i=args->argc; i<min_argc; i++) {
74 args->argv[args->argc++] = Qnil;
75 }
76 }
77}
78
79static inline void
80args_reduce(struct args_info *args, int over_argc)
81{
82 if (args->rest) {
83 const long len = RARRAY_LEN(args->rest);
84
85 if (len > over_argc) {
86 arg_rest_dup(args);
87 rb_ary_resize(args->rest, len - over_argc);
88 return;
89 }
90 else {
91 args->rest = Qfalse;
92 over_argc -= len;
93 }
94 }
95
96 VM_ASSERT(args->argc >= over_argc);
97 args->argc -= over_argc;
98}
99
100static inline int
101args_check_block_arg0(struct args_info *args)
102{
103 VALUE ary = Qnil;
104
105 if (args->rest && RARRAY_LEN(args->rest) == 1) {
106 VALUE arg0 = RARRAY_AREF(args->rest, 0);
107 ary = rb_check_array_type(arg0);
108 }
109 else if (args->argc == 1) {
110 VALUE arg0 = args->argv[0];
111 ary = rb_check_array_type(arg0);
112 args->argv[0] = arg0; /* see: https://bugs.ruby-lang.org/issues/8484 */
113 }
114
115 if (!NIL_P(ary)) {
116 args->rest = ary;
117 args->rest_index = 0;
118 args->argc = 0;
119 return TRUE;
120 }
121
122 return FALSE;
123}
124
125static inline void
126args_copy(struct args_info *args)
127{
128 if (args->rest != Qfalse) {
129 int argc = args->argc;
130 args->argc = 0;
131 arg_rest_dup(args);
132
133 /*
134 * argv: [m0, m1, m2, m3]
135 * rest: [a0, a1, a2, a3, a4, a5]
136 * ^
137 * rest_index
138 *
139 * #=> first loop
140 *
141 * argv: [m0, m1]
142 * rest: [m2, m3, a2, a3, a4, a5]
143 * ^
144 * rest_index
145 *
146 * #=> 2nd loop
147 *
148 * argv: [] (argc == 0)
149 * rest: [m0, m1, m2, m3, a2, a3, a4, a5]
150 * ^
151 * rest_index
152 */
153 while (args->rest_index > 0 && argc > 0) {
154 RARRAY_ASET(args->rest, --args->rest_index, args->argv[--argc]);
155 }
156 while (argc > 0) {
157 rb_ary_unshift(args->rest, args->argv[--argc]);
158 }
159 }
160 else if (args->argc > 0) {
161 args->rest = rb_ary_new_from_values(args->argc, args->argv);
162 args->rest_index = 0;
163 args->rest_dupped = TRUE;
164 args->argc = 0;
165 }
166}
167
168static inline const VALUE *
169args_rest_argv(struct args_info *args)
170{
171 return RARRAY_CONST_PTR_TRANSIENT(args->rest) + args->rest_index;
172}
173
174static inline VALUE
175args_rest_array(struct args_info *args)
176{
177 VALUE ary;
178
179 if (args->rest) {
180 ary = rb_ary_behead(args->rest, args->rest_index);
181 args->rest_index = 0;
182 args->rest = 0;
183 }
184 else {
185 ary = rb_ary_new();
186 }
187 return ary;
188}
189
190static int
191args_kw_argv_to_hash(struct args_info *args)
192{
193 const struct rb_callinfo_kwarg *kw_arg = args->kw_arg;
194 const VALUE *const passed_keywords = kw_arg->keywords;
195 const int kw_len = kw_arg->keyword_len;
196 VALUE h = rb_hash_new_with_size(kw_len);
197 const int kw_start = args->argc - kw_len;
198 const VALUE * const kw_argv = args->argv + kw_start;
199 int i;
200
201 args->argc = kw_start + 1;
202 for (i=0; i<kw_len; i++) {
203 rb_hash_aset(h, passed_keywords[i], kw_argv[i]);
204 }
205
206 args->argv[args->argc - 1] = h;
207
208 return args->argc;
209}
210
211static inline void
212args_setup_lead_parameters(struct args_info *args, int argc, VALUE *locals)
213{
214 if (args->argc >= argc) {
215 /* do noting */
216 args->argc -= argc;
217 args->argv += argc;
218 }
219 else {
220 int i, j;
221 const VALUE *argv = args_rest_argv(args);
222
223 for (i=args->argc, j=0; i<argc; i++, j++) {
224 locals[i] = argv[j];
225 }
226 args->rest_index += argc - args->argc;
227 args->argc = 0;
228 }
229}
230
231static inline void
232args_setup_post_parameters(struct args_info *args, int argc, VALUE *locals)
233{
234 long len;
235 len = RARRAY_LEN(args->rest);
236 MEMCPY(locals, RARRAY_CONST_PTR_TRANSIENT(args->rest) + len - argc, VALUE, argc);
237 rb_ary_resize(args->rest, len - argc);
238}
239
240static inline int
241args_setup_opt_parameters(struct args_info *args, int opt_max, VALUE *locals)
242{
243 int i;
244
245 if (args->argc >= opt_max) {
246 args->argc -= opt_max;
247 args->argv += opt_max;
248 i = opt_max;
249 }
250 else {
251 int j;
252 i = args->argc;
253 args->argc = 0;
254
255 if (args->rest) {
256 int len = RARRAY_LENINT(args->rest);
257 const VALUE *argv = RARRAY_CONST_PTR_TRANSIENT(args->rest);
258
259 for (; i<opt_max && args->rest_index < len; i++, args->rest_index++) {
260 locals[i] = argv[args->rest_index];
261 }
262 }
263
264 /* initialize by nil */
265 for (j=i; j<opt_max; j++) {
266 locals[j] = Qnil;
267 }
268 }
269
270 return i;
271}
272
273static inline void
274args_setup_rest_parameter(struct args_info *args, VALUE *locals)
275{
276 *locals = args_rest_array(args);
277}
278
279static VALUE
280make_unknown_kw_hash(const VALUE *passed_keywords, int passed_keyword_len, const VALUE *kw_argv)
281{
282 int i;
283 VALUE obj = rb_ary_hidden_new(1);
284
285 for (i=0; i<passed_keyword_len; i++) {
286 if (!UNDEF_P(kw_argv[i])) {
287 rb_ary_push(obj, passed_keywords[i]);
288 }
289 }
290 return obj;
291}
292
293static VALUE
294make_rest_kw_hash(const VALUE *passed_keywords, int passed_keyword_len, const VALUE *kw_argv)
295{
296 int i;
297 VALUE obj = rb_hash_new_with_size(passed_keyword_len);
298
299 for (i=0; i<passed_keyword_len; i++) {
300 if (!UNDEF_P(kw_argv[i])) {
301 rb_hash_aset(obj, passed_keywords[i], kw_argv[i]);
302 }
303 }
304 return obj;
305}
306
307static inline int
308args_setup_kw_parameters_lookup(const ID key, VALUE *ptr, const VALUE *const passed_keywords, VALUE *passed_values, const int passed_keyword_len)
309{
310 int i;
311 const VALUE keyname = ID2SYM(key);
312
313 for (i=0; i<passed_keyword_len; i++) {
314 if (keyname == passed_keywords[i]) {
315 *ptr = passed_values[i];
316 passed_values[i] = Qundef;
317 return TRUE;
318 }
319 }
320
321 return FALSE;
322}
323
324#define KW_SPECIFIED_BITS_MAX (32-1) /* TODO: 32 -> Fixnum's max bits */
325
326static void
327args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *const iseq,
328 VALUE *const passed_values, const int passed_keyword_len, const VALUE *const passed_keywords,
329 VALUE *const locals)
330{
331 const ID *acceptable_keywords = ISEQ_BODY(iseq)->param.keyword->table;
332 const int req_key_num = ISEQ_BODY(iseq)->param.keyword->required_num;
333 const int key_num = ISEQ_BODY(iseq)->param.keyword->num;
334 const VALUE * const default_values = ISEQ_BODY(iseq)->param.keyword->default_values;
335 VALUE missing = 0;
336 int i, di, found = 0;
337 int unspecified_bits = 0;
338 VALUE unspecified_bits_value = Qnil;
339
340 for (i=0; i<req_key_num; i++) {
341 ID key = acceptable_keywords[i];
342 if (args_setup_kw_parameters_lookup(key, &locals[i], passed_keywords, passed_values, passed_keyword_len)) {
343 found++;
344 }
345 else {
346 if (!missing) missing = rb_ary_hidden_new(1);
347 rb_ary_push(missing, ID2SYM(key));
348 }
349 }
350
351 if (missing) argument_kw_error(ec, iseq, "missing", missing);
352
353 for (di=0; i<key_num; i++, di++) {
354 if (args_setup_kw_parameters_lookup(acceptable_keywords[i], &locals[i], passed_keywords, passed_values, passed_keyword_len)) {
355 found++;
356 }
357 else {
358 if (UNDEF_P(default_values[di])) {
359 locals[i] = Qnil;
360
361 if (LIKELY(i < KW_SPECIFIED_BITS_MAX)) {
362 unspecified_bits |= 0x01 << di;
363 }
364 else {
365 if (NIL_P(unspecified_bits_value)) {
366 /* fixnum -> hash */
367 int j;
368 unspecified_bits_value = rb_hash_new();
369
370 for (j=0; j<KW_SPECIFIED_BITS_MAX; j++) {
371 if (unspecified_bits & (0x01 << j)) {
372 rb_hash_aset(unspecified_bits_value, INT2FIX(j), Qtrue);
373 }
374 }
375 }
376 rb_hash_aset(unspecified_bits_value, INT2FIX(di), Qtrue);
377 }
378 }
379 else {
380 locals[i] = default_values[di];
381 }
382 }
383 }
384
385 if (ISEQ_BODY(iseq)->param.flags.has_kwrest) {
386 const int rest_hash_index = key_num + 1;
387 locals[rest_hash_index] = make_rest_kw_hash(passed_keywords, passed_keyword_len, passed_values);
388 }
389 else {
390 if (found != passed_keyword_len) {
391 VALUE keys = make_unknown_kw_hash(passed_keywords, passed_keyword_len, passed_values);
392 argument_kw_error(ec, iseq, "unknown", keys);
393 }
394 }
395
396 if (NIL_P(unspecified_bits_value)) {
397 unspecified_bits_value = INT2FIX(unspecified_bits);
398 }
399 locals[key_num] = unspecified_bits_value;
400}
401
402static inline void
403args_setup_kw_rest_parameter(VALUE keyword_hash, VALUE *locals, int kw_flag)
404{
405 if (NIL_P(keyword_hash)) {
406 keyword_hash = rb_hash_new();
407 }
408 else if (!(kw_flag & VM_CALL_KW_SPLAT_MUT)) {
409 keyword_hash = rb_hash_dup(keyword_hash);
410 }
411 locals[0] = keyword_hash;
412}
413
414static inline void
415args_setup_block_parameter(const rb_execution_context_t *ec, struct rb_calling_info *calling, VALUE *locals)
416{
417 VALUE block_handler = calling->block_handler;
418 *locals = rb_vm_bh_to_procval(ec, block_handler);
419}
420
421 struct fill_values_arg {
422 VALUE *keys;
423 VALUE *vals;
424 int argc;
425};
426
427static int
428fill_keys_values(st_data_t key, st_data_t val, st_data_t ptr)
429{
430 struct fill_values_arg *arg = (struct fill_values_arg *)ptr;
431 int i = arg->argc++;
432 arg->keys[i] = (VALUE)key;
433 arg->vals[i] = (VALUE)val;
434 return ST_CONTINUE;
435}
436
437static inline int
438ignore_keyword_hash_p(VALUE keyword_hash, const rb_iseq_t * const iseq, unsigned int * kw_flag, VALUE * converted_keyword_hash)
439{
440 if (!RB_TYPE_P(keyword_hash, T_HASH)) {
441 keyword_hash = rb_to_hash_type(keyword_hash);
442 }
443 if (!(*kw_flag & VM_CALL_KW_SPLAT_MUT) &&
444 (ISEQ_BODY(iseq)->param.flags.has_kwrest ||
445 ISEQ_BODY(iseq)->param.flags.ruby2_keywords)) {
446 *kw_flag |= VM_CALL_KW_SPLAT_MUT;
447 keyword_hash = rb_hash_dup(keyword_hash);
448 }
449 *converted_keyword_hash = keyword_hash;
450 return !(ISEQ_BODY(iseq)->param.flags.has_kw) &&
451 !(ISEQ_BODY(iseq)->param.flags.has_kwrest) &&
452 RHASH_EMPTY_P(keyword_hash);
453}
454
455static int
456setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * const iseq,
457 struct rb_calling_info *const calling,
458 const struct rb_callinfo *ci,
459 VALUE * const locals, const enum arg_setup_type arg_setup_type)
460{
461 const int min_argc = ISEQ_BODY(iseq)->param.lead_num + ISEQ_BODY(iseq)->param.post_num;
462 const int max_argc = (ISEQ_BODY(iseq)->param.flags.has_rest == FALSE) ? min_argc + ISEQ_BODY(iseq)->param.opt_num : UNLIMITED_ARGUMENTS;
463 int given_argc;
464 unsigned int kw_flag = vm_ci_flag(ci) & (VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT);
465 int opt_pc = 0, allow_autosplat = !kw_flag;
466 struct args_info args_body, *args;
467 VALUE keyword_hash = Qnil;
468 VALUE * const orig_sp = ec->cfp->sp;
469 unsigned int i;
470 VALUE flag_keyword_hash = 0;
471 VALUE splat_flagged_keyword_hash = 0;
472 VALUE converted_keyword_hash = 0;
473 VALUE rest_last = 0;
474
475 vm_check_canary(ec, orig_sp);
476 /*
477 * Extend SP for GC.
478 *
479 * [pushed values] [uninitialized values]
480 * <- ci->argc -->
481 * <- ISEQ_BODY(iseq)->param.size------------>
482 * ^ locals ^ sp
483 *
484 * =>
485 * [pushed values] [initialized values ]
486 * <- ci->argc -->
487 * <- ISEQ_BODY(iseq)->param.size------------>
488 * ^ locals ^ sp
489 */
490 for (i=calling->argc; i<ISEQ_BODY(iseq)->param.size; i++) {
491 locals[i] = Qnil;
492 }
493 ec->cfp->sp = &locals[i];
494
495 /* setup args */
496 args = &args_body;
497 given_argc = args->argc = calling->argc;
498 args->argv = locals;
499 args->rest_dupped = FALSE;
500
501 if (kw_flag & VM_CALL_KWARG) {
502 args->kw_arg = vm_ci_kwarg(ci);
503
504 if (ISEQ_BODY(iseq)->param.flags.has_kw) {
505 int kw_len = args->kw_arg->keyword_len;
506 /* copy kw_argv */
507 args->kw_argv = ALLOCA_N(VALUE, kw_len);
508 args->argc -= kw_len;
509 given_argc -= kw_len;
510 MEMCPY(args->kw_argv, locals + args->argc, VALUE, kw_len);
511 }
512 else {
513 args->kw_argv = NULL;
514 given_argc = args_kw_argv_to_hash(args);
515 kw_flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT;
516 }
517 }
518 else {
519 args->kw_arg = NULL;
520 args->kw_argv = NULL;
521 }
522
523 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
524 int len;
525 args->rest = locals[--args->argc];
526 args->rest_index = 0;
527 len = RARRAY_LENINT(args->rest);
528 given_argc += len - 1;
529 rest_last = RARRAY_AREF(args->rest, len - 1);
530
531 if (!kw_flag && len > 0) {
532 if (RB_TYPE_P(rest_last, T_HASH) &&
533 (((struct RHash *)rest_last)->basic.flags & RHASH_PASS_AS_KEYWORDS)) {
534 splat_flagged_keyword_hash = rest_last;
535 rest_last = rb_hash_dup(rest_last);
536 kw_flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT;
537 }
538 else {
539 rest_last = 0;
540 }
541 }
542
543 if (kw_flag & VM_CALL_KW_SPLAT) {
544 if (ignore_keyword_hash_p(rest_last, iseq, &kw_flag, &converted_keyword_hash)) {
545 arg_rest_dup(args);
546 rb_ary_pop(args->rest);
547 given_argc--;
548 kw_flag &= ~(VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT);
549 }
550 else {
551 if (rest_last != converted_keyword_hash) {
552 rest_last = converted_keyword_hash;
553 arg_rest_dup(args);
554 RARRAY_ASET(args->rest, len - 1, rest_last);
555 }
556
557 if (ISEQ_BODY(iseq)->param.flags.ruby2_keywords && rest_last) {
558 flag_keyword_hash = rest_last;
559 }
560 else if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
561 arg_rest_dup(args);
562 rb_ary_pop(args->rest);
563 given_argc--;
564 keyword_hash = rest_last;
565 }
566 }
567 }
568 }
569 else {
570 if (kw_flag & VM_CALL_KW_SPLAT) {
571 VALUE last_arg = args->argv[args->argc-1];
572 if (ignore_keyword_hash_p(last_arg, iseq, &kw_flag, &converted_keyword_hash)) {
573 args->argc--;
574 given_argc--;
575 kw_flag &= ~(VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT);
576 }
577 else {
578 if (last_arg != converted_keyword_hash) {
579 last_arg = converted_keyword_hash;
580 args->argv[args->argc-1] = last_arg;
581 }
582
583 if (ISEQ_BODY(iseq)->param.flags.ruby2_keywords) {
584 flag_keyword_hash = last_arg;
585 }
586 else if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
587 args->argc--;
588 given_argc--;
589 keyword_hash = last_arg;
590 }
591 }
592 }
593 args->rest = Qfalse;
594 }
595
596 if (flag_keyword_hash && RB_TYPE_P(flag_keyword_hash, T_HASH)) {
597 ((struct RHash *)flag_keyword_hash)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
598 }
599
600 if (kw_flag && ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg) {
601 rb_raise(rb_eArgError, "no keywords accepted");
602 }
603
604 switch (arg_setup_type) {
605 case arg_setup_method:
606 break; /* do nothing special */
607 case arg_setup_block:
608 if (given_argc == 1 &&
609 allow_autosplat &&
610 !splat_flagged_keyword_hash &&
611 (min_argc > 0 || ISEQ_BODY(iseq)->param.opt_num > 1) &&
612 !ISEQ_BODY(iseq)->param.flags.ambiguous_param0 &&
613 !((ISEQ_BODY(iseq)->param.flags.has_kw ||
614 ISEQ_BODY(iseq)->param.flags.has_kwrest)
615 && max_argc == 1) &&
616 args_check_block_arg0(args)) {
617 given_argc = RARRAY_LENINT(args->rest);
618 }
619 break;
620 }
621
622 /* argc check */
623 if (given_argc < min_argc) {
624 if (arg_setup_type == arg_setup_block) {
625 CHECK_VM_STACK_OVERFLOW(ec->cfp, min_argc);
626 given_argc = min_argc;
627 args_extend(args, min_argc);
628 }
629 else {
630 argument_arity_error(ec, iseq, given_argc, min_argc, max_argc);
631 }
632 }
633
634 if (given_argc > max_argc && max_argc != UNLIMITED_ARGUMENTS) {
635 if (arg_setup_type == arg_setup_block) {
636 /* truncate */
637 args_reduce(args, given_argc - max_argc);
638 given_argc = max_argc;
639 }
640 else {
641 argument_arity_error(ec, iseq, given_argc, min_argc, max_argc);
642 }
643 }
644
645 if (ISEQ_BODY(iseq)->param.flags.has_lead) {
646 args_setup_lead_parameters(args, ISEQ_BODY(iseq)->param.lead_num, locals + 0);
647 }
648
649 if (ISEQ_BODY(iseq)->param.flags.has_rest || ISEQ_BODY(iseq)->param.flags.has_post){
650 args_copy(args);
651 }
652
653 if (ISEQ_BODY(iseq)->param.flags.has_post) {
654 args_setup_post_parameters(args, ISEQ_BODY(iseq)->param.post_num, locals + ISEQ_BODY(iseq)->param.post_start);
655 }
656
657 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
658 int opt = args_setup_opt_parameters(args, ISEQ_BODY(iseq)->param.opt_num, locals + ISEQ_BODY(iseq)->param.lead_num);
659 opt_pc = (int)ISEQ_BODY(iseq)->param.opt_table[opt];
660 }
661
662 if (ISEQ_BODY(iseq)->param.flags.has_rest) {
663 args_setup_rest_parameter(args, locals + ISEQ_BODY(iseq)->param.rest_start);
664 VALUE ary = *(locals + ISEQ_BODY(iseq)->param.rest_start);
665 VALUE index = RARRAY_LEN(ary) - 1;
666 if (splat_flagged_keyword_hash &&
667 !ISEQ_BODY(iseq)->param.flags.ruby2_keywords &&
668 !ISEQ_BODY(iseq)->param.flags.has_kw &&
669 !ISEQ_BODY(iseq)->param.flags.has_kwrest &&
670 RARRAY_AREF(ary, index) == splat_flagged_keyword_hash) {
671 ((struct RHash *)rest_last)->basic.flags &= ~RHASH_PASS_AS_KEYWORDS;
672 RARRAY_ASET(ary, index, rest_last);
673 }
674 }
675
676 if (ISEQ_BODY(iseq)->param.flags.has_kw) {
677 VALUE * const klocals = locals + ISEQ_BODY(iseq)->param.keyword->bits_start - ISEQ_BODY(iseq)->param.keyword->num;
678
679 if (args->kw_argv != NULL) {
680 const struct rb_callinfo_kwarg *kw_arg = args->kw_arg;
681 args_setup_kw_parameters(ec, iseq, args->kw_argv, kw_arg->keyword_len, kw_arg->keywords, klocals);
682 }
683 else if (!NIL_P(keyword_hash)) {
684 int kw_len = rb_long2int(RHASH_SIZE(keyword_hash));
685 struct fill_values_arg arg;
686 /* copy kw_argv */
687 arg.keys = args->kw_argv = ALLOCA_N(VALUE, kw_len * 2);
688 arg.vals = arg.keys + kw_len;
689 arg.argc = 0;
690 rb_hash_foreach(keyword_hash, fill_keys_values, (VALUE)&arg);
691 VM_ASSERT(arg.argc == kw_len);
692 args_setup_kw_parameters(ec, iseq, arg.vals, kw_len, arg.keys, klocals);
693 }
694 else {
695 VM_ASSERT(args_argc(args) == 0);
696 args_setup_kw_parameters(ec, iseq, NULL, 0, NULL, klocals);
697 }
698 }
699 else if (ISEQ_BODY(iseq)->param.flags.has_kwrest) {
700 args_setup_kw_rest_parameter(keyword_hash, locals + ISEQ_BODY(iseq)->param.keyword->rest_start, kw_flag);
701 }
702 else if (!NIL_P(keyword_hash) && RHASH_SIZE(keyword_hash) > 0 && arg_setup_type == arg_setup_method) {
703 argument_kw_error(ec, iseq, "unknown", rb_hash_keys(keyword_hash));
704 }
705
706 if (ISEQ_BODY(iseq)->param.flags.has_block) {
707 if (ISEQ_BODY(iseq)->local_iseq == iseq) {
708 /* Do nothing */
709 }
710 else {
711 args_setup_block_parameter(ec, calling, locals + ISEQ_BODY(iseq)->param.block_start);
712 }
713 }
714
715#if 0
716 {
717 int i;
718 for (i=0; i<ISEQ_BODY(iseq)->param.size; i++) {
719 ruby_debug_printf("local[%d] = %p\n", i, (void *)locals[i]);
720 }
721 }
722#endif
723
724 ec->cfp->sp = orig_sp;
725 return opt_pc;
726}
727
728static void
729raise_argument_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const VALUE exc)
730{
731 VALUE at;
732
733 if (iseq) {
734 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL, Qnil /* self */,
735 VM_BLOCK_HANDLER_NONE /* specval*/, Qfalse /* me or cref */,
736 ISEQ_BODY(iseq)->iseq_encoded,
737 ec->cfp->sp, 0, 0 /* stack_max */);
738 at = rb_ec_backtrace_object(ec);
739 rb_backtrace_use_iseq_first_lineno_for_last_location(at);
740 rb_vm_pop_frame(ec);
741 }
742 else {
743 at = rb_ec_backtrace_object(ec);
744 }
745
746 rb_ivar_set(exc, idBt_locations, at);
747 rb_exc_set_backtrace(exc, at);
748 rb_exc_raise(exc);
749}
750
751static void
752argument_arity_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const int miss_argc, const int min_argc, const int max_argc)
753{
754 VALUE exc = rb_arity_error_new(miss_argc, min_argc, max_argc);
755 if (ISEQ_BODY(iseq)->param.flags.has_kw) {
756 const struct rb_iseq_param_keyword *const kw = ISEQ_BODY(iseq)->param.keyword;
757 const ID *keywords = kw->table;
758 int req_key_num = kw->required_num;
759 if (req_key_num > 0) {
760 static const char required[] = "; required keywords";
761 VALUE mesg = rb_attr_get(exc, idMesg);
762 rb_str_resize(mesg, RSTRING_LEN(mesg)-1);
763 rb_str_cat(mesg, required, sizeof(required) - 1 - (req_key_num == 1));
764 rb_str_cat_cstr(mesg, ":");
765 do {
766 rb_str_cat_cstr(mesg, " ");
767 rb_str_append(mesg, rb_id2str(*keywords++));
768 rb_str_cat_cstr(mesg, ",");
769 } while (--req_key_num);
770 RSTRING_PTR(mesg)[RSTRING_LEN(mesg)-1] = ')';
771 }
772 }
773 raise_argument_error(ec, iseq, exc);
774}
775
776static void
777argument_kw_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const char *error, const VALUE keys)
778{
779 raise_argument_error(ec, iseq, rb_keyword_error_new(error, keys));
780}
781
782static inline void
783vm_caller_setup_arg_splat(rb_control_frame_t *cfp, struct rb_calling_info *calling)
784{
785 int argc = calling->argc;
786 VALUE *argv = cfp->sp - argc;
787 VALUE ary = argv[argc-1];
788
789 vm_check_canary(GET_EC(), cfp->sp);
790 cfp->sp--;
791
792 if (!NIL_P(ary)) {
793 const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
794 long len = RARRAY_LEN(ary), i;
795
796 CHECK_VM_STACK_OVERFLOW(cfp, len);
797
798 for (i = 0; i < len; i++) {
799 *cfp->sp++ = ptr[i];
800 }
801 calling->argc += i - 1;
802 }
803}
804
805static inline void
806vm_caller_setup_arg_kw(rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_callinfo *ci)
807{
808 const VALUE *const passed_keywords = vm_ci_kwarg(ci)->keywords;
809 const int kw_len = vm_ci_kwarg(ci)->keyword_len;
810 const VALUE h = rb_hash_new_with_size(kw_len);
811 VALUE *sp = cfp->sp;
812 int i;
813
814 for (i=0; i<kw_len; i++) {
815 rb_hash_aset(h, passed_keywords[i], (sp - kw_len)[i]);
816 }
817 (sp-kw_len)[0] = h;
818
819 cfp->sp -= kw_len - 1;
820 calling->argc -= kw_len - 1;
821 calling->kw_splat = 1;
822}
823
824static VALUE
825vm_to_proc(VALUE proc)
826{
827 if (UNLIKELY(!rb_obj_is_proc(proc))) {
828 VALUE b;
829 const rb_callable_method_entry_t *me =
830 rb_callable_method_entry_with_refinements(CLASS_OF(proc), idTo_proc, NULL);
831
832 if (me) {
833 b = rb_vm_call0(GET_EC(), proc, idTo_proc, 0, NULL, me, RB_NO_KEYWORDS);
834 }
835 else {
836 /* NOTE: calling method_missing */
837 b = rb_check_convert_type_with_id(proc, T_DATA, "Proc", idTo_proc);
838 }
839
840 if (NIL_P(b) || !rb_obj_is_proc(b)) {
841 rb_raise(rb_eTypeError,
842 "wrong argument type %s (expected Proc)",
843 rb_obj_classname(proc));
844 }
845 return b;
846 }
847 else {
848 return proc;
849 }
850}
851
852static VALUE
853refine_sym_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
854{
855 VALUE obj;
856 ID mid;
857 const rb_callable_method_entry_t *me = 0; /* for hidden object case */
858 rb_execution_context_t *ec;
859 const VALUE symbol = RARRAY_AREF(callback_arg, 0);
860 const VALUE refinements = RARRAY_AREF(callback_arg, 1);
861 int kw_splat = RB_PASS_CALLED_KEYWORDS;
862 VALUE klass;
863
864 if (argc-- < 1) {
865 rb_raise(rb_eArgError, "no receiver given");
866 }
867 obj = *argv++;
868
869 mid = SYM2ID(symbol);
870 for (klass = CLASS_OF(obj); klass; klass = RCLASS_SUPER(klass)) {
871 me = rb_callable_method_entry(klass, mid);
872 if (me) {
873 me = rb_resolve_refined_method_callable(refinements, me);
874 if (me) break;
875 }
876 }
877
878 ec = GET_EC();
879 if (!NIL_P(blockarg)) {
880 vm_passed_block_handler_set(ec, blockarg);
881 }
882 if (!me) {
883 return method_missing(ec, obj, mid, argc, argv, MISSING_NOENTRY, kw_splat);
884 }
885 return rb_vm_call0(ec, obj, mid, argc, argv, me, kw_splat);
886}
887
888static VALUE
889vm_caller_setup_arg_block(const rb_execution_context_t *ec, rb_control_frame_t *reg_cfp,
890 const struct rb_callinfo *ci, const rb_iseq_t *blockiseq, const int is_super)
891{
892 if (vm_ci_flag(ci) & VM_CALL_ARGS_BLOCKARG) {
893 VALUE block_code = *(--reg_cfp->sp);
894
895 if (NIL_P(block_code)) {
896 return VM_BLOCK_HANDLER_NONE;
897 }
898 else if (block_code == rb_block_param_proxy) {
899 VM_ASSERT(!VM_CFP_IN_HEAP_P(GET_EC(), reg_cfp));
900 VALUE handler = VM_CF_BLOCK_HANDLER(reg_cfp);
901 reg_cfp->block_code = (const void *) handler;
902 return handler;
903 }
904 else if (SYMBOL_P(block_code) && rb_method_basic_definition_p(rb_cSymbol, idTo_proc)) {
905 const rb_cref_t *cref = vm_env_cref(reg_cfp->ep);
906 if (cref && !NIL_P(cref->refinements)) {
907 VALUE ref = cref->refinements;
908 VALUE func = rb_hash_lookup(ref, block_code);
909 if (NIL_P(func)) {
910 /* TODO: limit cached funcs */
911 VALUE callback_arg = rb_ary_hidden_new(2);
912 rb_ary_push(callback_arg, block_code);
913 rb_ary_push(callback_arg, ref);
914 OBJ_FREEZE_RAW(callback_arg);
915 func = rb_func_lambda_new(refine_sym_proc_call, callback_arg, 1, UNLIMITED_ARGUMENTS);
916 rb_hash_aset(ref, block_code, func);
917 }
918 block_code = func;
919 }
920 return block_code;
921 }
922 else {
923 return vm_to_proc(block_code);
924 }
925 }
926 else if (blockiseq != NULL) { /* likely */
927 struct rb_captured_block *captured = VM_CFP_TO_CAPTURED_BLOCK(reg_cfp);
928 captured->code.iseq = blockiseq;
929 return VM_BH_FROM_ISEQ_BLOCK(captured);
930 }
931 else {
932 if (is_super) {
933 return GET_BLOCK_HANDLER();
934 }
935 else {
936 return VM_BLOCK_HANDLER_NONE;
937 }
938 }
939}
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:144
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
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
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition error.c:3150
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:688
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1091
rb_eArgError
VALUE rb_eArgError
ArgumentError exception.
Definition error.c:1092
rb_cSymbol
VALUE rb_cSymbol
Sumbol class.
Definition string.c:80
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:175
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:3353
rb_str_cat
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
Definition string.c:3177
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_str_resize
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3064
rb_attr_get
VALUE rb_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
Definition variable.c:1226
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:1606
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:68
RARRAY_CONST_PTR_TRANSIENT
#define RARRAY_CONST_PTR_TRANSIENT
Just another name of rb_array_const_ptr_transient.
Definition rarray.h:70
RARRAY_LENINT
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:343
RARRAY_ASET
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
Definition rarray.h:566
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:583
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:82
RHASH_EMPTY_P
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:92
RSTRING_LEN
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition rstring.h:484
RSTRING_PTR
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition rstring.h:498
rb_obj_classname
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:325
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:43
args_info
Definition vm_args.c:22
fill_values_arg
Definition vm_args.c:421
rb_callable_method_entry_struct
Definition method.h:62
rb_callinfo_kwarg
Definition vm_callinfo.h:46
rb_callinfo
Definition vm_callinfo.h:62
rb_calling_info
Definition vm_core.h:294
rb_captured_block
Definition vm_core.h:785
rb_control_frame_struct
Definition vm_core.h:818
rb_cref_struct
CREF (Class REFerence)
Definition method.h:44
rb_execution_context_struct
Definition vm_core.h:899
rb_iseq_struct
Definition vm_core.h:526
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
RB_TYPE_P
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:375