diff options
Diffstat (limited to 'vm_args.c')
| -rw-r--r-- | vm_args.c | 613 |
1 files changed, 443 insertions, 170 deletions
@@ -1,6 +1,6 @@ /********************************************************************** - vm_args.c - process method call arguments. + vm_args.c - process method call arguments. Included into vm.c. $Author$ @@ -8,15 +8,12 @@ **********************************************************************/ -NORETURN(static void raise_argument_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const VALUE exc)); -NORETURN(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)); -NORETURN(static void argument_kw_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const char *error, const VALUE keys)); +NORETURN(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)); +NORETURN(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)); +NORETURN(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)); VALUE rb_keyword_error_new(const char *error, VALUE keys); /* class.c */ static VALUE method_missing(rb_execution_context_t *ec, VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status, int kw_splat); -#if !defined(_MSC_VER) || !defined(MJIT_HEADER) -MJIT_FUNC_EXPORTED -#endif const rb_callable_method_entry_t *rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me); struct args_info { @@ -168,7 +165,7 @@ args_copy(struct args_info *args) static inline const VALUE * args_rest_argv(struct args_info *args) { - return RARRAY_CONST_PTR_TRANSIENT(args->rest) + args->rest_index; + return RARRAY_CONST_PTR(args->rest) + args->rest_index; } static inline VALUE @@ -233,7 +230,7 @@ args_setup_post_parameters(struct args_info *args, int argc, VALUE *locals) { long len; len = RARRAY_LEN(args->rest); - MEMCPY(locals, RARRAY_CONST_PTR_TRANSIENT(args->rest) + len - argc, VALUE, argc); + MEMCPY(locals, RARRAY_CONST_PTR(args->rest) + len - argc, VALUE, argc); rb_ary_resize(args->rest, len - argc); } @@ -248,23 +245,17 @@ args_setup_opt_parameters(struct args_info *args, int opt_max, VALUE *locals) i = opt_max; } else { - int j; i = args->argc; args->argc = 0; if (args->rest) { int len = RARRAY_LENINT(args->rest); - const VALUE *argv = RARRAY_CONST_PTR_TRANSIENT(args->rest); + const VALUE *argv = RARRAY_CONST_PTR(args->rest); for (; i<opt_max && args->rest_index < len; i++, args->rest_index++) { locals[i] = argv[args->rest_index]; } } - - /* initialize by nil */ - for (j=i; j<opt_max; j++) { - locals[j] = Qnil; - } } return i; @@ -321,10 +312,8 @@ args_setup_kw_parameters_lookup(const ID key, VALUE *ptr, const VALUE *const pas return FALSE; } -#define KW_SPECIFIED_BITS_MAX (32-1) /* TODO: 32 -> Fixnum's max bits */ - static void -args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *const iseq, +args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *const iseq, const rb_callable_method_entry_t *cme, VALUE *const passed_values, const int passed_keyword_len, const VALUE *const passed_keywords, VALUE *const locals) { @@ -348,7 +337,7 @@ args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *cons } } - if (missing) argument_kw_error(ec, iseq, "missing", missing); + if (missing) argument_kw_error(ec, iseq, cme, "missing", missing); for (di=0; i<key_num; i++, di++) { if (args_setup_kw_parameters_lookup(acceptable_keywords[i], &locals[i], passed_keywords, passed_values, passed_keyword_len)) { @@ -358,7 +347,7 @@ args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *cons if (UNDEF_P(default_values[di])) { locals[i] = Qnil; - if (LIKELY(i < KW_SPECIFIED_BITS_MAX)) { + if (LIKELY(i < VM_KW_SPECIFIED_BITS_MAX)) { unspecified_bits |= 0x01 << di; } else { @@ -367,7 +356,7 @@ args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *cons int j; unspecified_bits_value = rb_hash_new(); - for (j=0; j<KW_SPECIFIED_BITS_MAX; j++) { + for (j=0; j<VM_KW_SPECIFIED_BITS_MAX; j++) { if (unspecified_bits & (0x01 << j)) { rb_hash_aset(unspecified_bits_value, INT2FIX(j), Qtrue); } @@ -389,7 +378,110 @@ args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *cons else { if (found != passed_keyword_len) { VALUE keys = make_unknown_kw_hash(passed_keywords, passed_keyword_len, passed_values); - argument_kw_error(ec, iseq, "unknown", keys); + argument_kw_error(ec, iseq, cme, "unknown", keys); + } + } + + if (NIL_P(unspecified_bits_value)) { + unspecified_bits_value = INT2FIX(unspecified_bits); + } + locals[key_num] = unspecified_bits_value; +} + +static void +args_setup_kw_parameters_from_kwsplat(rb_execution_context_t *const ec, const rb_iseq_t *const iseq, const rb_callable_method_entry_t *cme, + VALUE keyword_hash, VALUE *const locals, bool remove_hash_value) +{ + const ID *acceptable_keywords = ISEQ_BODY(iseq)->param.keyword->table; + const int req_key_num = ISEQ_BODY(iseq)->param.keyword->required_num; + const int key_num = ISEQ_BODY(iseq)->param.keyword->num; + const VALUE * const default_values = ISEQ_BODY(iseq)->param.keyword->default_values; + VALUE missing = 0; + int i, di; + int unspecified_bits = 0; + size_t keyword_size = RHASH_SIZE(keyword_hash); + VALUE unspecified_bits_value = Qnil; + + for (i=0; i<req_key_num; i++) { + VALUE key = ID2SYM(acceptable_keywords[i]); + VALUE value; + if (remove_hash_value) { + value = rb_hash_delete_entry(keyword_hash, key); + } + else { + value = rb_hash_lookup2(keyword_hash, key, Qundef); + } + + if (!UNDEF_P(value)) { + keyword_size--; + locals[i] = value; + } + else { + if (!missing) missing = rb_ary_hidden_new(1); + rb_ary_push(missing, key); + } + } + + if (missing) argument_kw_error(ec, iseq, cme, "missing", missing); + + for (di=0; i<key_num; i++, di++) { + VALUE key = ID2SYM(acceptable_keywords[i]); + VALUE value; + if (remove_hash_value) { + value = rb_hash_delete_entry(keyword_hash, key); + } + else { + value = rb_hash_lookup2(keyword_hash, key, Qundef); + } + + if (!UNDEF_P(value)) { + keyword_size--; + locals[i] = value; + } + else { + if (UNDEF_P(default_values[di])) { + locals[i] = Qnil; + + if (LIKELY(i < VM_KW_SPECIFIED_BITS_MAX)) { + unspecified_bits |= 0x01 << di; + } + else { + if (NIL_P(unspecified_bits_value)) { + /* fixnum -> hash */ + int j; + unspecified_bits_value = rb_hash_new(); + + for (j=0; j<VM_KW_SPECIFIED_BITS_MAX; j++) { + if (unspecified_bits & (0x01 << j)) { + rb_hash_aset(unspecified_bits_value, INT2FIX(j), Qtrue); + } + } + } + rb_hash_aset(unspecified_bits_value, INT2FIX(di), Qtrue); + } + } + else { + locals[i] = default_values[di]; + } + } + } + + if (ISEQ_BODY(iseq)->param.flags.has_kwrest) { + const int rest_hash_index = key_num + 1; + locals[rest_hash_index] = keyword_hash; + } + else { + if (!remove_hash_value) { + if (keyword_size != 0) { + /* Recurse with duplicated keyword hash in remove mode. + * This is simpler than writing code to check which entries in the hash do not match. + * This will raise an exception, so the additional performance impact shouldn't be material. + */ + args_setup_kw_parameters_from_kwsplat(ec, iseq, cme, rb_hash_dup(keyword_hash), locals, true); + } + } + else if (!RHASH_EMPTY_P(keyword_hash)) { + argument_kw_error(ec, iseq, cme, "unknown", rb_hash_keys(keyword_hash)); } } @@ -400,10 +492,12 @@ args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *cons } static inline void -args_setup_kw_rest_parameter(VALUE keyword_hash, VALUE *locals, int kw_flag) +args_setup_kw_rest_parameter(VALUE keyword_hash, VALUE *locals, int kw_flag, bool anon_kwrest) { if (NIL_P(keyword_hash)) { - keyword_hash = rb_hash_new(); + if (!anon_kwrest) { + keyword_hash = rb_hash_new(); + } } else if (!(kw_flag & VM_CALL_KW_SPLAT_MUT)) { keyword_hash = rb_hash_dup(keyword_hash); @@ -418,38 +512,71 @@ args_setup_block_parameter(const rb_execution_context_t *ec, struct rb_calling_i *locals = rb_vm_bh_to_procval(ec, block_handler); } -struct fill_values_arg { - VALUE *keys; - VALUE *vals; - int argc; -}; - -static int -fill_keys_values(st_data_t key, st_data_t val, st_data_t ptr) -{ - struct fill_values_arg *arg = (struct fill_values_arg *)ptr; - int i = arg->argc++; - arg->keys[i] = (VALUE)key; - arg->vals[i] = (VALUE)val; - return ST_CONTINUE; -} - static inline int ignore_keyword_hash_p(VALUE keyword_hash, const rb_iseq_t * const iseq, unsigned int * kw_flag, VALUE * converted_keyword_hash) { - if (!RB_TYPE_P(keyword_hash, T_HASH)) { + if (keyword_hash == Qnil) { + goto ignore; + } + else if (!RB_TYPE_P(keyword_hash, T_HASH)) { keyword_hash = rb_to_hash_type(keyword_hash); } + else if (UNLIKELY(ISEQ_BODY(iseq)->param.flags.anon_kwrest)) { + if (!ISEQ_BODY(iseq)->param.flags.has_kw) { + *kw_flag |= VM_CALL_KW_SPLAT_MUT; + } + } + + if (RHASH_EMPTY_P(keyword_hash) && !ISEQ_BODY(iseq)->param.flags.has_kwrest) { + goto ignore; + } + if (!(*kw_flag & VM_CALL_KW_SPLAT_MUT) && - (ISEQ_BODY(iseq)->param.flags.has_kwrest || - ISEQ_BODY(iseq)->param.flags.ruby2_keywords)) { + (ISEQ_BODY(iseq)->param.flags.has_kwrest || + ISEQ_BODY(iseq)->param.flags.ruby2_keywords)) { *kw_flag |= VM_CALL_KW_SPLAT_MUT; keyword_hash = rb_hash_dup(keyword_hash); } *converted_keyword_hash = keyword_hash; - return !(ISEQ_BODY(iseq)->param.flags.has_kw) && - !(ISEQ_BODY(iseq)->param.flags.has_kwrest) && - RHASH_EMPTY_P(keyword_hash); + + if (!(ISEQ_BODY(iseq)->param.flags.has_kw) && + !(ISEQ_BODY(iseq)->param.flags.has_kwrest) && + RHASH_EMPTY_P(keyword_hash)) { + ignore: + *kw_flag &= ~(VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT); + return 1; + } + else { + return 0; + } +} + +static VALUE +check_kwrestarg(VALUE keyword_hash, unsigned int *kw_flag) +{ + if (!(*kw_flag & VM_CALL_KW_SPLAT_MUT)) { + *kw_flag |= VM_CALL_KW_SPLAT_MUT; + return rb_hash_dup(keyword_hash); + } + else { + return keyword_hash; + } +} + +static void +flatten_rest_args(rb_execution_context_t * const ec, struct args_info *args, VALUE * const locals, unsigned int *ci_flag) +{ + const VALUE *argv = RARRAY_CONST_PTR(args->rest); + int j, i=args->argc, rest_len = RARRAY_LENINT(args->rest)-1; + args->argc += rest_len; + if (rest_len) { + CHECK_VM_STACK_OVERFLOW(ec->cfp, rest_len+1); + for (j=0; rest_len > 0; rest_len--, i++, j++) { + locals[i] = argv[j]; + } + } + args->rest = Qfalse; + *ci_flag &= ~VM_CALL_ARGS_SPLAT; } static int @@ -461,7 +588,8 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co const int min_argc = ISEQ_BODY(iseq)->param.lead_num + ISEQ_BODY(iseq)->param.post_num; const int max_argc = (ISEQ_BODY(iseq)->param.flags.has_rest == FALSE) ? min_argc + ISEQ_BODY(iseq)->param.opt_num : UNLIMITED_ARGUMENTS; int given_argc; - unsigned int kw_flag = vm_ci_flag(ci) & (VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT); + unsigned int ci_flag = vm_ci_flag(ci); + unsigned int kw_flag = ci_flag & (VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT); int opt_pc = 0, allow_autosplat = !kw_flag; struct args_info args_body, *args; VALUE keyword_hash = Qnil; @@ -471,6 +599,7 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co VALUE splat_flagged_keyword_hash = 0; VALUE converted_keyword_hash = 0; VALUE rest_last = 0; + const rb_callable_method_entry_t *cme = calling->cc ? vm_cc_cme(calling->cc) : NULL; vm_check_canary(ec, orig_sp); /* @@ -496,7 +625,35 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co args = &args_body; given_argc = args->argc = calling->argc; args->argv = locals; - args->rest_dupped = FALSE; + args->rest_dupped = ci_flag & VM_CALL_ARGS_SPLAT_MUT; + + if (UNLIKELY(ISEQ_BODY(iseq)->param.flags.anon_rest)) { + if ((ci_flag & VM_CALL_ARGS_SPLAT) && + given_argc == ISEQ_BODY(iseq)->param.lead_num + (kw_flag ? 2 : 1) && + !ISEQ_BODY(iseq)->param.flags.has_opt && + !ISEQ_BODY(iseq)->param.flags.has_post && + !ISEQ_BODY(iseq)->param.flags.ruby2_keywords) { + if (kw_flag) { + if (ISEQ_BODY(iseq)->param.flags.has_kw || + ISEQ_BODY(iseq)->param.flags.has_kwrest) { + args->rest_dupped = true; + } + else if (kw_flag & VM_CALL_KW_SPLAT) { + VALUE kw_hash = locals[args->argc - 1]; + if (kw_hash == Qnil || + (RB_TYPE_P(kw_hash, T_HASH) && RHASH_EMPTY_P(kw_hash))) { + args->rest_dupped = true; + } + } + + } + else if (!ISEQ_BODY(iseq)->param.flags.has_kw && + !ISEQ_BODY(iseq)->param.flags.has_kwrest && + !ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg) { + args->rest_dupped = true; + } + } + } if (kw_flag & VM_CALL_KWARG) { args->kw_arg = vm_ci_kwarg(ci); @@ -520,61 +677,145 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co args->kw_argv = NULL; } - if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) { - int len; - args->rest = locals[--args->argc]; + if ((ci_flag & VM_CALL_ARGS_SPLAT) && (ci_flag & VM_CALL_KW_SPLAT)) { + // f(*a, **kw) args->rest_index = 0; - len = RARRAY_LENINT(args->rest); - given_argc += len - 1; - rest_last = RARRAY_AREF(args->rest, len - 1); + keyword_hash = locals[--args->argc]; + args->rest = locals[--args->argc]; - if (!kw_flag && len > 0) { - if (RB_TYPE_P(rest_last, T_HASH) && - (((struct RHash *)rest_last)->basic.flags & RHASH_PASS_AS_KEYWORDS)) { - splat_flagged_keyword_hash = rest_last; - rest_last = rb_hash_dup(rest_last); - kw_flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT; - } - else { - rest_last = 0; - } + if (ignore_keyword_hash_p(keyword_hash, iseq, &kw_flag, &converted_keyword_hash)) { + keyword_hash = Qnil; } - - if (kw_flag & VM_CALL_KW_SPLAT) { - if (ignore_keyword_hash_p(rest_last, iseq, &kw_flag, &converted_keyword_hash)) { + else if (UNLIKELY(ISEQ_BODY(iseq)->param.flags.ruby2_keywords)) { + converted_keyword_hash = check_kwrestarg(converted_keyword_hash, &kw_flag); + flag_keyword_hash = converted_keyword_hash; + arg_rest_dup(args); + rb_ary_push(args->rest, converted_keyword_hash); + keyword_hash = Qnil; + } + else if (!ISEQ_BODY(iseq)->param.flags.has_kwrest && !ISEQ_BODY(iseq)->param.flags.has_kw) { + converted_keyword_hash = check_kwrestarg(converted_keyword_hash, &kw_flag); + if (ISEQ_BODY(iseq)->param.flags.has_rest) { arg_rest_dup(args); - rb_ary_pop(args->rest); - given_argc--; - kw_flag &= ~(VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT); + rb_ary_push(args->rest, converted_keyword_hash); + keyword_hash = Qnil; } else { - if (rest_last != converted_keyword_hash) { - rest_last = converted_keyword_hash; - arg_rest_dup(args); - RARRAY_ASET(args->rest, len - 1, rest_last); + // Avoid duping rest when not necessary + // Copy rest elements and converted keyword hash directly to VM stack + const VALUE *argv = RARRAY_CONST_PTR(args->rest); + int j, i=args->argc, rest_len = RARRAY_LENINT(args->rest); + if (rest_len) { + CHECK_VM_STACK_OVERFLOW(ec->cfp, rest_len+1); + given_argc += rest_len; + args->argc += rest_len; + for (j=0; rest_len > 0; rest_len--, i++, j++) { + locals[i] = argv[j]; + } } + locals[i] = converted_keyword_hash; + given_argc--; + args->argc++; + args->rest = Qfalse; + ci_flag &= ~(VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT); + keyword_hash = Qnil; + goto arg_splat_and_kw_splat_flattened; + } + } + else { + keyword_hash = converted_keyword_hash; + } + + int len = RARRAY_LENINT(args->rest); + given_argc += len - 2; + } + else if (ci_flag & VM_CALL_ARGS_SPLAT) { + // f(*a) + args->rest_index = 0; + args->rest = locals[--args->argc]; + int len = RARRAY_LENINT(args->rest); + given_argc += len - 1; - if (ISEQ_BODY(iseq)->param.flags.ruby2_keywords && rest_last) { - flag_keyword_hash = rest_last; + if (!kw_flag && len > 0) { + rest_last = RARRAY_AREF(args->rest, len - 1); + if (RB_TYPE_P(rest_last, T_HASH) && FL_TEST_RAW(rest_last, RHASH_PASS_AS_KEYWORDS)) { + // def f(**kw); a = [..., kw]; g(*a) + splat_flagged_keyword_hash = rest_last; + if (!(RHASH_EMPTY_P(rest_last) || ISEQ_BODY(iseq)->param.flags.has_kw) || (ISEQ_BODY(iseq)->param.flags.has_kwrest)) { + rest_last = rb_hash_dup(rest_last); } - else if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) { - arg_rest_dup(args); - rb_ary_pop(args->rest); + kw_flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT; + + // Unset rest_dupped set by anon_rest as we may need to modify splat in this case + args->rest_dupped = false; + + if (ignore_keyword_hash_p(rest_last, iseq, &kw_flag, &converted_keyword_hash)) { + if (ISEQ_BODY(iseq)->param.flags.has_rest) { + // Only duplicate/modify splat array if it will be used + arg_rest_dup(args); + rb_ary_pop(args->rest); + } + else if (arg_setup_type == arg_setup_block && !ISEQ_BODY(iseq)->param.flags.has_kwrest) { + // Avoid hash allocation for empty hashes + // Copy rest elements except empty keyword hash directly to VM stack + flatten_rest_args(ec, args, locals, &ci_flag); + keyword_hash = Qnil; + kw_flag = 0; + } given_argc--; - keyword_hash = rest_last; + } + else if (!ISEQ_BODY(iseq)->param.flags.has_rest) { + // Avoid duping rest when not necessary + // Copy rest elements and converted keyword hash directly to VM stack + flatten_rest_args(ec, args, locals, &ci_flag); + + if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) { + given_argc--; + keyword_hash = converted_keyword_hash; + } + else { + locals[args->argc] = converted_keyword_hash; + args->argc += 1; + keyword_hash = Qnil; + kw_flag = 0; + } + } + else { + if (rest_last != converted_keyword_hash) { + rest_last = converted_keyword_hash; + arg_rest_dup(args); + RARRAY_ASET(args->rest, len - 1, rest_last); + } + + if (ISEQ_BODY(iseq)->param.flags.ruby2_keywords && rest_last) { + flag_keyword_hash = rest_last; + } + else if (ISEQ_BODY(iseq)->param.flags.has_kw || ISEQ_BODY(iseq)->param.flags.has_kwrest) { + arg_rest_dup(args); + rb_ary_pop(args->rest); + given_argc--; + keyword_hash = rest_last; + } } } } } else { - if (kw_flag & VM_CALL_KW_SPLAT) { + args->rest = Qfalse; + + if (args->argc > 0 && (kw_flag & VM_CALL_KW_SPLAT)) { + // f(**kw) VALUE last_arg = args->argv[args->argc-1]; if (ignore_keyword_hash_p(last_arg, iseq, &kw_flag, &converted_keyword_hash)) { args->argc--; given_argc--; - kw_flag &= ~(VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT); } else { + if (!(kw_flag & VM_CALL_KW_SPLAT_MUT) && !ISEQ_BODY(iseq)->param.flags.has_kw) { + converted_keyword_hash = rb_hash_dup(converted_keyword_hash); + kw_flag |= VM_CALL_KW_SPLAT_MUT; + } + if (last_arg != converted_keyword_hash) { last_arg = converted_keyword_hash; args->argv[args->argc-1] = last_arg; @@ -590,13 +831,13 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co } } } - args->rest = Qfalse; } - if (flag_keyword_hash && RB_TYPE_P(flag_keyword_hash, T_HASH)) { - ((struct RHash *)flag_keyword_hash)->basic.flags |= RHASH_PASS_AS_KEYWORDS; + if (flag_keyword_hash) { + FL_SET_RAW(flag_keyword_hash, RHASH_PASS_AS_KEYWORDS); } + arg_splat_and_kw_splat_flattened: if (kw_flag && ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg) { rb_raise(rb_eArgError, "no keywords accepted"); } @@ -605,8 +846,9 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co case arg_setup_method: break; /* do nothing special */ case arg_setup_block: - if (given_argc == (NIL_P(keyword_hash) ? 1 : 2) && + if (given_argc == 1 && allow_autosplat && + !splat_flagged_keyword_hash && (min_argc > 0 || ISEQ_BODY(iseq)->param.opt_num > 1) && !ISEQ_BODY(iseq)->param.flags.ambiguous_param0 && !((ISEQ_BODY(iseq)->param.flags.has_kw || @@ -626,7 +868,7 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co args_extend(args, min_argc); } else { - argument_arity_error(ec, iseq, given_argc, min_argc, max_argc); + argument_arity_error(ec, iseq, cme, given_argc, min_argc, max_argc); } } @@ -637,7 +879,7 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co given_argc = max_argc; } else { - argument_arity_error(ec, iseq, given_argc, min_argc, max_argc); + argument_arity_error(ec, iseq, cme, given_argc, min_argc, max_argc); } } @@ -659,16 +901,22 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co } if (ISEQ_BODY(iseq)->param.flags.has_rest) { - args_setup_rest_parameter(args, locals + ISEQ_BODY(iseq)->param.rest_start); - VALUE ary = *(locals + ISEQ_BODY(iseq)->param.rest_start); - VALUE index = RARRAY_LEN(ary) - 1; - if (splat_flagged_keyword_hash && - !ISEQ_BODY(iseq)->param.flags.ruby2_keywords && - !ISEQ_BODY(iseq)->param.flags.has_kw && - !ISEQ_BODY(iseq)->param.flags.has_kwrest && - RARRAY_AREF(ary, index) == splat_flagged_keyword_hash) { - ((struct RHash *)rest_last)->basic.flags &= ~RHASH_PASS_AS_KEYWORDS; - RARRAY_ASET(ary, index, rest_last); + if (UNLIKELY(ISEQ_BODY(iseq)->param.flags.anon_rest && args->argc == 0 && !args->rest && !ISEQ_BODY(iseq)->param.flags.has_post)) { + *(locals + ISEQ_BODY(iseq)->param.rest_start) = args->rest = rb_cArray_empty_frozen; + args->rest_index = 0; + } + else { + args_setup_rest_parameter(args, locals + ISEQ_BODY(iseq)->param.rest_start); + VALUE ary = *(locals + ISEQ_BODY(iseq)->param.rest_start); + VALUE index = RARRAY_LEN(ary) - 1; + if (splat_flagged_keyword_hash && + !ISEQ_BODY(iseq)->param.flags.ruby2_keywords && + !ISEQ_BODY(iseq)->param.flags.has_kw && + !ISEQ_BODY(iseq)->param.flags.has_kwrest && + RARRAY_AREF(ary, index) == splat_flagged_keyword_hash) { + ((struct RHash *)rest_last)->basic.flags &= ~RHASH_PASS_AS_KEYWORDS; + RARRAY_ASET(ary, index, rest_last); + } } } @@ -677,29 +925,38 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co if (args->kw_argv != NULL) { const struct rb_callinfo_kwarg *kw_arg = args->kw_arg; - args_setup_kw_parameters(ec, iseq, args->kw_argv, kw_arg->keyword_len, kw_arg->keywords, klocals); + args_setup_kw_parameters(ec, iseq, cme, args->kw_argv, kw_arg->keyword_len, kw_arg->keywords, klocals); } else if (!NIL_P(keyword_hash)) { - int kw_len = rb_long2int(RHASH_SIZE(keyword_hash)); - struct fill_values_arg arg; - /* copy kw_argv */ - arg.keys = args->kw_argv = ALLOCA_N(VALUE, kw_len * 2); - arg.vals = arg.keys + kw_len; - arg.argc = 0; - rb_hash_foreach(keyword_hash, fill_keys_values, (VALUE)&arg); - VM_ASSERT(arg.argc == kw_len); - args_setup_kw_parameters(ec, iseq, arg.vals, kw_len, arg.keys, klocals); + bool remove_hash_value = false; + if (ISEQ_BODY(iseq)->param.flags.has_kwrest) { + keyword_hash = check_kwrestarg(keyword_hash, &kw_flag); + remove_hash_value = true; + } + args_setup_kw_parameters_from_kwsplat(ec, iseq, cme, keyword_hash, klocals, remove_hash_value); } else { - VM_ASSERT(args_argc(args) == 0); - args_setup_kw_parameters(ec, iseq, NULL, 0, NULL, klocals); +#if VM_CHECK_MODE > 0 + if (args_argc(args) != 0) { + VM_ASSERT(ci_flag & VM_CALL_ARGS_SPLAT); + VM_ASSERT(!(ci_flag & (VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT))); + VM_ASSERT(!kw_flag); + VM_ASSERT(!ISEQ_BODY(iseq)->param.flags.has_rest); + VM_ASSERT(RARRAY_LENINT(args->rest) > 0); + VM_ASSERT(RB_TYPE_P(rest_last, T_HASH)); + VM_ASSERT(FL_TEST_RAW(rest_last, RHASH_PASS_AS_KEYWORDS)); + VM_ASSERT(args_argc(args) == 1); + } +#endif + args_setup_kw_parameters(ec, iseq, cme, NULL, 0, NULL, klocals); } } else if (ISEQ_BODY(iseq)->param.flags.has_kwrest) { - args_setup_kw_rest_parameter(keyword_hash, locals + ISEQ_BODY(iseq)->param.keyword->rest_start, kw_flag); + args_setup_kw_rest_parameter(keyword_hash, locals + ISEQ_BODY(iseq)->param.keyword->rest_start, + kw_flag, ISEQ_BODY(iseq)->param.flags.anon_kwrest); } else if (!NIL_P(keyword_hash) && RHASH_SIZE(keyword_hash) > 0 && arg_setup_type == arg_setup_method) { - argument_kw_error(ec, iseq, "unknown", rb_hash_keys(keyword_hash)); + argument_kw_error(ec, iseq, cme, "unknown", rb_hash_keys(keyword_hash)); } if (ISEQ_BODY(iseq)->param.flags.has_block) { @@ -725,17 +982,16 @@ setup_parameters_complex(rb_execution_context_t * const ec, const rb_iseq_t * co } static void -raise_argument_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const VALUE exc) +raise_argument_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_callable_method_entry_t *cme, const VALUE exc) { VALUE at; if (iseq) { vm_push_frame(ec, iseq, VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL, Qnil /* self */, - VM_BLOCK_HANDLER_NONE /* specval*/, Qfalse /* me or cref */, + VM_BLOCK_HANDLER_NONE /* specval*/, (VALUE) cme /* me or cref */, ISEQ_BODY(iseq)->iseq_encoded, ec->cfp->sp, 0, 0 /* stack_max */); at = rb_ec_backtrace_object(ec); - rb_backtrace_use_iseq_first_lineno_for_last_location(at); rb_vm_pop_frame(ec); } else { @@ -748,7 +1004,7 @@ raise_argument_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const VA } 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) +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) { VALUE exc = rb_arity_error_new(miss_argc, min_argc, max_argc); if (ISEQ_BODY(iseq)->param.flags.has_kw) { @@ -769,55 +1025,13 @@ argument_arity_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const in RSTRING_PTR(mesg)[RSTRING_LEN(mesg)-1] = ')'; } } - raise_argument_error(ec, iseq, exc); + raise_argument_error(ec, iseq, cme, exc); } static void -argument_kw_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const char *error, const VALUE keys) -{ - raise_argument_error(ec, iseq, rb_keyword_error_new(error, keys)); -} - -static inline void -vm_caller_setup_arg_splat(rb_control_frame_t *cfp, struct rb_calling_info *calling) +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) { - int argc = calling->argc; - VALUE *argv = cfp->sp - argc; - VALUE ary = argv[argc-1]; - - vm_check_canary(GET_EC(), cfp->sp); - cfp->sp--; - - if (!NIL_P(ary)) { - const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary); - long len = RARRAY_LEN(ary), i; - - CHECK_VM_STACK_OVERFLOW(cfp, len); - - for (i = 0; i < len; i++) { - *cfp->sp++ = ptr[i]; - } - calling->argc += i - 1; - } -} - -static inline void -vm_caller_setup_arg_kw(rb_control_frame_t *cfp, struct rb_calling_info *calling, const struct rb_callinfo *ci) -{ - const VALUE *const passed_keywords = vm_ci_kwarg(ci)->keywords; - const int kw_len = vm_ci_kwarg(ci)->keyword_len; - const VALUE h = rb_hash_new_with_size(kw_len); - VALUE *sp = cfp->sp; - int i; - - for (i=0; i<kw_len; i++) { - rb_hash_aset(h, passed_keywords[i], (sp - kw_len)[i]); - } - (sp-kw_len)[0] = h; - - cfp->sp -= kw_len - 1; - calling->argc -= kw_len - 1; - calling->kw_splat = 1; + raise_argument_error(ec, iseq, cme, rb_keyword_error_new(error, keys)); } static VALUE @@ -837,9 +1051,17 @@ vm_to_proc(VALUE proc) } if (NIL_P(b) || !rb_obj_is_proc(b)) { - rb_raise(rb_eTypeError, - "wrong argument type %s (expected Proc)", - rb_obj_classname(proc)); + if (me) { + VALUE cname = rb_obj_class(proc); + rb_raise(rb_eTypeError, + "can't convert %"PRIsVALUE" to Proc (%"PRIsVALUE"#to_proc gives %"PRIsVALUE")", + cname, cname, rb_obj_class(b)); + } + else { + rb_raise(rb_eTypeError, + "no implicit conversion of %s into Proc", + rb_obj_classname(proc)); + } } return b; } @@ -895,10 +1117,7 @@ vm_caller_setup_arg_block(const rb_execution_context_t *ec, rb_control_frame_t * return VM_BLOCK_HANDLER_NONE; } else if (block_code == rb_block_param_proxy) { - VM_ASSERT(!VM_CFP_IN_HEAP_P(GET_EC(), reg_cfp)); - VALUE handler = VM_CF_BLOCK_HANDLER(reg_cfp); - reg_cfp->block_code = (const void *) handler; - return handler; + return VM_CF_BLOCK_HANDLER(reg_cfp); } else if (SYMBOL_P(block_code) && rb_method_basic_definition_p(rb_cSymbol, idTo_proc)) { const rb_cref_t *cref = vm_env_cref(reg_cfp->ep); @@ -910,7 +1129,7 @@ vm_caller_setup_arg_block(const rb_execution_context_t *ec, rb_control_frame_t * VALUE callback_arg = rb_ary_hidden_new(2); rb_ary_push(callback_arg, block_code); rb_ary_push(callback_arg, ref); - OBJ_FREEZE_RAW(callback_arg); + OBJ_FREEZE(callback_arg); func = rb_func_lambda_new(refine_sym_proc_call, callback_arg, 1, UNLIMITED_ARGUMENTS); rb_hash_aset(ref, block_code, func); } @@ -936,3 +1155,57 @@ vm_caller_setup_arg_block(const rb_execution_context_t *ec, rb_control_frame_t * } } } + +static void vm_adjust_stack_forwarding(const struct rb_execution_context_struct *ec, struct rb_control_frame_struct *cfp, int argc, VALUE splat); + +static VALUE +vm_caller_setup_fwd_args(const rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, + CALL_DATA cd, const rb_iseq_t *blockiseq, const int is_super, + struct rb_forwarding_call_data *adjusted_cd, struct rb_callinfo *adjusted_ci) +{ + CALL_INFO site_ci = cd->ci; + VALUE bh = Qundef; + + RUBY_ASSERT(ISEQ_BODY(ISEQ_BODY(GET_ISEQ())->local_iseq)->param.flags.forwardable); + CALL_INFO caller_ci = (CALL_INFO)TOPN(0); + + unsigned int site_argc = vm_ci_argc(site_ci); + unsigned int site_flag = vm_ci_flag(site_ci); + ID site_mid = vm_ci_mid(site_ci); + + unsigned int caller_argc = vm_ci_argc(caller_ci); + unsigned int caller_flag = vm_ci_flag(caller_ci); + const struct rb_callinfo_kwarg * kw = vm_ci_kwarg(caller_ci); + + VALUE splat = Qfalse; + + if (site_flag & VM_CALL_ARGS_SPLAT) { + // If we're called with args_splat, the top 1 should be an array + splat = TOPN(1); + site_argc += (RARRAY_LEN(splat) - 1); + } + + // Need to setup the block in case of e.g. `super { :block }` + if (is_super && blockiseq) { + bh = vm_caller_setup_arg_block(ec, GET_CFP(), site_ci, blockiseq, is_super); + } + else { + bh = VM_ENV_BLOCK_HANDLER(GET_LEP()); + } + + vm_adjust_stack_forwarding(ec, GET_CFP(), caller_argc, splat); + + *adjusted_ci = VM_CI_ON_STACK( + site_mid, + ((caller_flag & ~(VM_CALL_ARGS_SIMPLE | VM_CALL_FCALL)) | + (site_flag & (VM_CALL_FCALL | VM_CALL_FORWARDING))), + site_argc + caller_argc, + kw + ); + + adjusted_cd->cd.ci = adjusted_ci; + adjusted_cd->cd.cc = cd->cc; + adjusted_cd->caller_ci = caller_ci; + + return bh; +} |
