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);
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);
40 if (!args->rest_dupped) {
42 args->rest_dupped = TRUE;
49 if (args->rest ==
Qfalse) {
53 return args->argc +
RARRAY_LENINT(args->rest) - args->rest_index;
58args_extend(
struct args_info *args,
const int min_argc)
64 VM_ASSERT(args->rest_index == 0);
65 for (i=args->argc +
RARRAY_LENINT(args->rest); i<min_argc; i++) {
70 for (i=args->argc; i<min_argc; i++) {
71 args->argv[args->argc++] =
Qnil;
77args_reduce(
struct args_info *args,
int over_argc)
82 if (
len > over_argc) {
93 VM_ASSERT(args->argc >= over_argc);
94 args->argc -= over_argc;
98args_check_block_arg0(
struct args_info *args)
102 if (args->rest &&
RARRAY_LEN(args->rest) == 1) {
106 else if (args->argc == 1) {
107 VALUE arg0 = args->argv[0];
109 args->argv[0] = arg0;
114 args->rest_index = 0;
125 if (args->rest !=
Qfalse) {
126 int argc = args->argc;
150 while (args->rest_index > 0 && argc > 0) {
151 RARRAY_ASET(args->rest, --args->rest_index, args->argv[--argc]);
154 rb_ary_unshift(args->rest, args->argv[--argc]);
157 else if (args->argc > 0) {
159 args->rest_index = 0;
160 args->rest_dupped = TRUE;
165static inline const VALUE *
177 ary = rb_ary_behead(args->rest, args->rest_index);
178 args->rest_index = 0;
188args_kw_argv_to_hash(
struct args_info *args)
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;
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]);
203 args->argv[args->argc - 1] = h;
209args_setup_lead_parameters(
struct args_info *args,
int argc,
VALUE *locals)
211 if (args->argc >= argc) {
218 const VALUE *argv = args_rest_argv(args);
220 for (i=args->argc, j=0; i<argc; i++, j++) {
223 args->rest_index += argc - args->argc;
229args_setup_post_parameters(
struct args_info *args,
int argc,
VALUE *locals)
238args_setup_opt_parameters(
struct args_info *args,
int opt_max,
VALUE *locals)
242 if (args->argc >= opt_max) {
243 args->argc -= opt_max;
244 args->argv += opt_max;
256 for (; i<opt_max && args->rest_index <
len; i++, args->rest_index++) {
257 locals[i] = argv[args->rest_index];
262 for (j=i; j<opt_max; j++) {
273 *locals = args_rest_array(args);
277make_unknown_kw_hash(
const VALUE *passed_keywords,
int passed_keyword_len,
const VALUE *kw_argv)
282 for (i=0; i<passed_keyword_len; i++) {
283 if (!UNDEF_P(kw_argv[i])) {
291make_rest_kw_hash(
const VALUE *passed_keywords,
int passed_keyword_len,
const VALUE *kw_argv)
294 VALUE obj = rb_hash_new_with_size(passed_keyword_len);
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]);
305args_setup_kw_parameters_lookup(
const ID key,
VALUE *
ptr,
const VALUE *
const passed_keywords,
VALUE *passed_values,
const int passed_keyword_len)
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;
326args_setup_kw_extend_sp(rb_execution_context_t *
const ec,
VALUE *
const end)
328 VALUE *sp = ec->cfp->sp;
329 if (sp >= end)
return;
331 CHECK_VM_STACK_OVERFLOW0(ec->cfp, sp, end - sp);
332 for (; sp < end; sp++) {
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,
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;
348 int i, di, found = 0;
349 int unspecified_bits = 0;
351 VALUE *orig_sp = NULL;
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);
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)) {
369 if (missing) argument_kw_error(ec, iseq, cme,
"missing", missing);
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)) {
376 if (UNDEF_P(default_values[di])) {
379 if (LIKELY(i < VM_KW_SPECIFIED_BITS_MAX)) {
380 unspecified_bits |= 0x01 << di;
383 if (
NIL_P(unspecified_bits_value)) {
386 unspecified_bits_value = rb_ident_hash_new();
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);
394 rb_hash_aset(unspecified_bits_value,
INT2FIX(di),
Qtrue);
398 locals[i] = default_values[di];
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);
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);
414 if (
NIL_P(unspecified_bits_value)) {
415 unspecified_bits_value =
INT2FIX(unspecified_bits);
417 locals[key_num] = unspecified_bits_value;
418 if (orig_sp) ec->cfp->sp = orig_sp;
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)
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;
431 int unspecified_bits = 0;
432 size_t keyword_size =
RHASH_SIZE(keyword_hash);
435 for (i=0; i<req_key_num; i++) {
438 if (remove_hash_value) {
439 value = rb_hash_delete_entry(keyword_hash, key);
442 value = rb_hash_lookup2(keyword_hash, key,
Qundef);
445 if (!UNDEF_P(value)) {
455 if (missing) argument_kw_error(ec, iseq, cme,
"missing", missing);
457 for (di=0; i<key_num; i++, di++) {
460 if (remove_hash_value) {
461 value = rb_hash_delete_entry(keyword_hash, key);
464 value = rb_hash_lookup2(keyword_hash, key,
Qundef);
467 if (!UNDEF_P(value)) {
472 if (UNDEF_P(default_values[di])) {
475 if (LIKELY(i < VM_KW_SPECIFIED_BITS_MAX)) {
476 unspecified_bits |= 0x01 << di;
479 if (
NIL_P(unspecified_bits_value)) {
482 unspecified_bits_value = rb_ident_hash_new();
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);
490 rb_hash_aset(unspecified_bits_value,
INT2FIX(di),
Qtrue);
494 locals[i] = default_values[di];
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;
504 if (!remove_hash_value) {
505 if (keyword_size != 0) {
510 args_setup_kw_parameters_from_kwsplat(ec, iseq, cme, rb_hash_dup(keyword_hash), locals,
true);
514 argument_kw_error(ec, iseq, cme,
"unknown", rb_hash_keys(keyword_hash));
518 if (
NIL_P(unspecified_bits_value)) {
519 unspecified_bits_value =
INT2FIX(unspecified_bits);
521 locals[key_num] = unspecified_bits_value;
525args_setup_kw_rest_parameter(
VALUE keyword_hash,
VALUE *locals,
int kw_flag,
bool anon_kwrest)
527 if (
NIL_P(keyword_hash)) {
529 keyword_hash = rb_hash_new();
532 else if (!(kw_flag & VM_CALL_KW_SPLAT_MUT)) {
533 keyword_hash = rb_hash_dup(keyword_hash);
535 locals[0] = keyword_hash;
539args_setup_block_parameter(
const rb_execution_context_t *ec,
struct rb_calling_info *calling,
VALUE *locals)
541 VALUE block_handler = calling->block_handler;
542 *locals = rb_vm_bh_to_procval(ec, block_handler);
546ignore_keyword_hash_p(
VALUE keyword_hash,
const rb_iseq_t *
const iseq,
unsigned int * kw_flag,
VALUE * converted_keyword_hash)
548 if (keyword_hash ==
Qnil) {
551 else if (!RB_TYPE_P(keyword_hash,
T_HASH)) {
552 keyword_hash = rb_to_hash_type(keyword_hash);
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;
560 if (
RHASH_EMPTY_P(keyword_hash) && !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
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);
570 *converted_keyword_hash = keyword_hash;
572 if (!(ISEQ_BODY(iseq)->param.flags.has_kw) &&
573 !(ISEQ_BODY(iseq)->param.flags.has_kwrest) &&
576 *kw_flag &= ~(VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT);
585check_kwrestarg(
VALUE keyword_hash,
unsigned int *kw_flag)
587 if (!(*kw_flag & VM_CALL_KW_SPLAT_MUT)) {
588 *kw_flag |= VM_CALL_KW_SPLAT_MUT;
589 return rb_hash_dup(keyword_hash);
597flatten_rest_args(rb_execution_context_t *
const ec,
struct args_info *args,
VALUE *
const locals,
unsigned int *ci_flag)
601 args->argc += rest_len;
603 CHECK_VM_STACK_OVERFLOW(ec->cfp, rest_len+1);
604 for (j=0; rest_len > 0; rest_len--, i++, j++) {
609 *ci_flag &= ~VM_CALL_ARGS_SPLAT;
613setup_parameters_complex(rb_execution_context_t *
const ec,
const rb_iseq_t *
const iseq,
616 VALUE *
const locals,
const enum arg_setup_type arg_setup_type)
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;
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;
626 VALUE *
const orig_sp = ec->cfp->sp;
628 VALUE flag_keyword_hash = 0;
629 VALUE splat_flagged_keyword_hash = 0;
630 VALUE converted_keyword_hash = 0;
632 const rb_callable_method_entry_t *cme = calling->cc ? vm_cc_cme(calling->cc) : NULL;
634 vm_check_canary(ec, orig_sp);
649 if ((
unsigned int)calling->argc < ISEQ_BODY(iseq)->param.size) {
653 CHECK_VM_STACK_OVERFLOW0(ec->cfp, locals + calling->argc,
654 ISEQ_BODY(iseq)->param.size - calling->argc);
656 for (i=calling->argc; i<ISEQ_BODY(iseq)->param.size; i++) {
659 ec->cfp->sp = &locals[i];
663 given_argc = args->argc = calling->argc;
665 args->rest_dupped = ci_flag & VM_CALL_ARGS_SPLAT_MUT;
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) {
674 if (ISEQ_BODY(iseq)->param.flags.has_kw ||
675 ISEQ_BODY(iseq)->param.flags.has_kwrest) {
676 args->rest_dupped =
true;
678 else if (kw_flag & VM_CALL_KW_SPLAT) {
679 VALUE kw_hash = locals[args->argc - 1];
680 if (kw_hash ==
Qnil ||
682 args->rest_dupped =
true;
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;
695 if (kw_flag & VM_CALL_KWARG) {
696 args->kw_arg = vm_ci_kwarg(ci);
698 if (ISEQ_BODY(iseq)->param.flags.has_kw) {
699 int kw_len = args->kw_arg->keyword_len;
702 args->argc -= kw_len;
703 given_argc -= kw_len;
704 MEMCPY(args->kw_argv, locals + args->argc,
VALUE, kw_len);
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;
714 args->kw_argv = NULL;
717 if ((ci_flag & VM_CALL_ARGS_SPLAT) && (ci_flag & VM_CALL_KW_SPLAT)) {
719 args->rest_index = 0;
720 keyword_hash = locals[--args->argc];
721 args->rest = locals[--args->argc];
723 if (ignore_keyword_hash_p(keyword_hash, iseq, &kw_flag, &converted_keyword_hash)) {
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;
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) {
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++) {
753 locals[i] = converted_keyword_hash;
757 ci_flag &= ~(VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT);
759 goto arg_splat_and_kw_splat_flattened;
763 keyword_hash = converted_keyword_hash;
767 given_argc +=
len - 2;
769 else if (ci_flag & VM_CALL_ARGS_SPLAT) {
771 args->rest_index = 0;
772 args->rest = locals[--args->argc];
774 given_argc +=
len - 1;
776 if (!kw_flag &&
len > 0) {
778 if (RB_TYPE_P(rest_last,
T_HASH) &&
FL_TEST_RAW(rest_last, RHASH_PASS_AS_KEYWORDS)) {
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);
784 kw_flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT;
787 args->rest_dupped =
false;
789 if (ignore_keyword_hash_p(rest_last, iseq, &kw_flag, &converted_keyword_hash)) {
790 if (ISEQ_BODY(iseq)->param.flags.has_rest) {
795 else if (arg_setup_type == arg_setup_block && !ISEQ_BODY(iseq)->param.flags.has_kwrest) {
798 flatten_rest_args(ec, args, locals, &ci_flag);
804 else if (!ISEQ_BODY(iseq)->param.flags.has_rest) {
807 flatten_rest_args(ec, args, locals, &ci_flag);
809 if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
811 keyword_hash = converted_keyword_hash;
814 locals[args->argc] = converted_keyword_hash;
821 if (rest_last != converted_keyword_hash) {
822 rest_last = converted_keyword_hash;
824 RARRAY_ASET(args->rest,
len - 1, rest_last);
827 if (ISEQ_BODY(iseq)->param.flags.ruby2_keywords && rest_last) {
828 flag_keyword_hash = rest_last;
830 else if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
834 keyword_hash = rest_last;
843 if (args->argc > 0 && (kw_flag & VM_CALL_KW_SPLAT)) {
845 VALUE last_arg = args->argv[args->argc-1];
846 if (ignore_keyword_hash_p(last_arg, iseq, &kw_flag, &converted_keyword_hash)) {
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;
856 if (last_arg != converted_keyword_hash) {
857 last_arg = converted_keyword_hash;
858 args->argv[args->argc-1] = last_arg;
861 if (ISEQ_BODY(iseq)->param.flags.ruby2_keywords) {
862 flag_keyword_hash = last_arg;
864 else if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) {
867 keyword_hash = last_arg;
873 if (flag_keyword_hash) {
874 FL_SET_RAW(flag_keyword_hash, RHASH_PASS_AS_KEYWORDS);
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");
882 switch (arg_setup_type) {
883 case arg_setup_method:
885 case arg_setup_block:
886 if (given_argc == 1 &&
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)
894 args_check_block_arg0(args)) {
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);
908 argument_arity_error(ec, iseq, cme, given_argc, min_argc, max_argc);
913 if (arg_setup_type == arg_setup_block) {
915 args_reduce(args, given_argc - max_argc);
916 given_argc = max_argc;
919 argument_arity_error(ec, iseq, cme, given_argc, min_argc, max_argc);
923 if (ISEQ_BODY(iseq)->param.flags.has_lead) {
924 args_setup_lead_parameters(args, ISEQ_BODY(iseq)->param.lead_num, locals + 0);
927 if (ISEQ_BODY(iseq)->param.flags.has_rest || ISEQ_BODY(iseq)->param.flags.has_post){
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);
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];
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;
946 args_setup_rest_parameter(args, locals + ISEQ_BODY(iseq)->param.rest_start);
947 VALUE ary = *(locals + ISEQ_BODY(iseq)->param.rest_start);
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 &&
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);
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;
964 if (args->kw_argv != NULL) {
966 args_setup_kw_parameters(ec, iseq, cme, args->kw_argv, kw_arg->keyword_len, kw_arg->keywords, klocals);
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;
974 args_setup_kw_parameters_from_kwsplat(ec, iseq, cme, keyword_hash, klocals, remove_hash_value);
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)));
982 VM_ASSERT(!ISEQ_BODY(iseq)->param.flags.has_rest);
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);
989 args_setup_kw_parameters(ec, iseq, cme, NULL, 0, NULL, klocals);
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);
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));
1000 if (ISEQ_BODY(iseq)->param.flags.has_block) {
1001 if (ISEQ_BODY(iseq)->local_iseq == iseq) {
1005 args_setup_block_parameter(ec, calling, locals + ISEQ_BODY(iseq)->param.block_start);
1012 for (i=0; i<ISEQ_BODY(iseq)->param.size; i++) {
1013 ruby_debug_printf(
"local[%d] = %p\n", i, (
void *)locals[i]);
1018 ec->cfp->sp = orig_sp;
1023raise_argument_error(rb_execution_context_t *ec,
const rb_iseq_t *iseq,
const rb_callable_method_entry_t *cme,
const VALUE exc)
1028 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL,
Qnil ,
1029 VM_BLOCK_HANDLER_NONE , (
VALUE) cme ,
1030 ISEQ_BODY(iseq)->iseq_encoded,
1031 ec->cfp->sp, 0, 0 );
1032 at = rb_ec_backtrace_object(ec);
1033 rb_vm_pop_frame(ec);
1036 at = rb_ec_backtrace_object(ec);
1040 rb_exc_set_backtrace(exc, at);
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)
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));
1062 }
while (--req_key_num);
1063 RSTRING_PTR(mesg)[RSTRING_LEN(mesg)-1] =
')';
1066 raise_argument_error(ec, iseq, cme, exc);
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)
1072 raise_argument_error(ec, iseq, cme, rb_keyword_error_new(error, keys));
1076vm_to_proc(
VALUE proc)
1080 const rb_callable_method_entry_t *me =
1081 rb_callable_method_entry_with_refinements(
CLASS_OF(proc), idTo_proc, NULL);
1084 b = rb_vm_call0(GET_EC(), proc, idTo_proc, 0, NULL, me,
RB_NO_KEYWORDS);
1088 b = rb_check_convert_type_with_id(proc,
T_DATA,
"Proc", idTo_proc);
1095 "can't convert %"PRIsVALUE
" to Proc (%"PRIsVALUE
"#to_proc gives %"PRIsVALUE
")",
1100 "no implicit conversion of %s into Proc",
1116 const rb_callable_method_entry_t *me = 0;
1117 rb_execution_context_t *ec;
1124 rb_raise(rb_eArgError,
"no receiver given");
1130 me = rb_callable_method_entry(klass, mid);
1132 me = rb_resolve_refined_method_callable(refinements, me);
1138 if (!
NIL_P(blockarg)) {
1139 vm_passed_block_handler_set(ec, blockarg);
1142 return method_missing(ec, obj, mid, argc, argv, MISSING_NOENTRY, kw_splat);
1144 return rb_vm_call0(ec, obj, mid, argc, argv, me, kw_splat);
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)
1151 if (vm_ci_flag(ci) & VM_CALL_ARGS_BLOCKARG) {
1152 VALUE block_code = *(--reg_cfp->sp);
1154 if (
NIL_P(block_code)) {
1155 return VM_BLOCK_HANDLER_NONE;
1157 else if (block_code == rb_block_param_proxy) {
1158 return VM_CF_BLOCK_HANDLER(reg_cfp);
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);
1172 rb_hash_aset(ref, block_code, func);
1179 return vm_to_proc(block_code);
1182 else if (blockiseq != NULL) {
1184 captured->code.iseq = blockiseq;
1185 return VM_BH_FROM_ISEQ_BLOCK(captured);
1189 return GET_BLOCK_HANDLER();
1192 return VM_BLOCK_HANDLER_NONE;
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,
1204 CALL_INFO site_ci = cd->ci;
1207 RUBY_ASSERT(ISEQ_BODY(ISEQ_BODY(GET_ISEQ())->local_iseq)->param.flags.forwardable);
1208 CALL_INFO caller_ci = (CALL_INFO)TOPN(0);
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);
1214 unsigned int caller_argc = vm_ci_argc(caller_ci);
1215 unsigned int caller_flag = vm_ci_flag(caller_ci);
1220 if (site_flag & VM_CALL_ARGS_SPLAT) {
1227 if (is_super && blockiseq) {
1228 bh = vm_caller_setup_arg_block(ec, GET_CFP(), site_ci, blockiseq, is_super);
1231 bh = VM_ENV_BLOCK_HANDLER(GET_LEP());
1234 vm_adjust_stack_forwarding(ec, GET_CFP(), caller_argc, splat);
1236 *adjusted_ci = VM_CI_ON_STACK(
1238 ((caller_flag & ~(VM_CALL_ARGS_SIMPLE | VM_CALL_FCALL)) |
1239 (site_flag & (VM_CALL_FCALL | VM_CALL_FORWARDING))),
1240 site_argc + caller_argc,
1244 adjusted_cd->cd.ci = adjusted_ci;
1245 adjusted_cd->cd.cc = cd->cc;
1246 adjusted_cd->caller_ci = caller_ci;
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define SYM2ID
Old name of RB_SYM2ID.
#define T_DATA
Old name of RUBY_T_DATA.
#define CLASS_OF
Old name of rb_class_of.
#define T_HASH
Old name of RUBY_T_HASH.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#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.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cSymbol
Symbol class.
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().
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
#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.
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.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int len
Length of the buffer.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define ALLOCA_N(type, n)
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
#define RB_NO_KEYWORDS
Do not pass keywords.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.