From a1afdedf94350c89007df2f65d79be6fc7b835e3 Mon Sep 17 00:00:00 2001 From: nagachika Date: Sun, 10 Sep 2017 03:49:10 +0000 Subject: merge revision(s) 59624,59626: [Backport #13830] ruby.h: fix rb_scan_args_trail_idx * include/ruby/ruby.h (rb_scan_args_trail_idx): fix the case both of optional and rest arguments are defined. [ruby-core:82427] [Bug #13830] * include/ruby/ruby.h (rb_scan_args_n_trail): ditto. non-keywords hash * class.c (rb_scan_args), include/ruby/ruby.h (rb_scan_args_set): return non-keywords elements only in the last hash when keyword arguments are extracted from it, as well as methods defined in ruby level. [ruby-core:82427] [Bug #13830] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@59811 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- include/ruby/ruby.h | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) (limited to 'include') diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 34004a13c3..9dc0b3000e 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -2258,29 +2258,16 @@ ALWAYS_INLINE(static int rb_scan_args_trail_idx(const char *fmt)); static inline int rb_scan_args_trail_idx(const char *fmt) { - return (rb_scan_args_lead_p(fmt) ? - (rb_scan_args_isdigit(fmt[1]) || fmt[1]=='*')+1 : - (fmt[0]=='*')); -} - -ALWAYS_INLINE(static int rb_scan_args_trail_p(const char *fmt)); -static inline int -rb_scan_args_trail_p(const char *fmt) -{ - return (rb_scan_args_lead_p(fmt) ? - (rb_scan_args_isdigit(fmt[1]) || fmt[1]=='*') && - rb_scan_args_isdigit(fmt[2]) : - fmt[0]=='*' && rb_scan_args_isdigit(fmt[1])); + const int idx = rb_scan_args_var_idx(fmt); + return idx+(fmt[idx]=='*'); } ALWAYS_INLINE(static int rb_scan_args_n_trail(const char *fmt)); static inline int rb_scan_args_n_trail(const char *fmt) { - return (rb_scan_args_lead_p(fmt) ? - ((rb_scan_args_isdigit(fmt[1]) || fmt[1]=='*') && - rb_scan_args_isdigit(fmt[2]) ? fmt[2]-'0' : 0) : - (fmt[0]=='*' && rb_scan_args_isdigit(fmt[1]) ? fmt[1]-'0' : 0)); + const int idx = rb_scan_args_trail_idx(fmt); + return (rb_scan_args_isdigit(fmt[idx]) ? fmt[idx]-'0' : 0); } ALWAYS_INLINE(static int rb_scan_args_hash_idx(const char *fmt)); @@ -2343,8 +2330,8 @@ rb_scan_args_set(int argc, const VALUE *argv, int f_var, int f_hash, int f_block, VALUE *vars[]) { - int i, argi = 0, vari = 0; - VALUE *var, hash = Qnil; + int i, argi = 0, vari = 0, last_idx = -1; + VALUE *var, hash = Qnil, last_hash = 0; const int n_mand = n_lead + n_trail; /* capture an option hash - phase 1: pop */ @@ -2362,7 +2349,8 @@ rb_scan_args_set(int argc, const VALUE *argv, hash = rb_check_hash_type(last); if (!RB_NIL_P(hash)) { VALUE opts = rb_extract_keywords(&hash); - if (!hash) argc--; + if (!(last_hash = hash)) argc--; + else last_idx = argc - 1; hash = opts ? opts : Qnil; } } @@ -2373,14 +2361,14 @@ rb_scan_args_set(int argc, const VALUE *argv, /* capture leading mandatory arguments */ for (i = n_lead; i-- > 0; ) { var = vars[vari++]; - if (var) *var = argv[argi]; + if (var) *var = (argi == last_idx) ? last_hash : argv[argi]; argi++; } /* capture optional arguments */ for (i = n_opt; i-- > 0; ) { var = vars[vari++]; if (argi < argc - n_trail) { - if (var) *var = argv[argi]; + if (var) *var = (argi == last_idx) ? last_hash : argv[argi]; argi++; } else { @@ -2393,7 +2381,11 @@ rb_scan_args_set(int argc, const VALUE *argv, var = vars[vari++]; if (0 < n_var) { - if (var) *var = rb_ary_new4(n_var, &argv[argi]); + if (var) { + int f_last = (last_idx + 1 == argc - n_trail); + *var = rb_ary_new4(n_var-f_last, &argv[argi]); + if (f_last) rb_ary_push(*var, last_hash); + } argi += n_var; } else { @@ -2403,7 +2395,7 @@ rb_scan_args_set(int argc, const VALUE *argv, /* capture trailing mandatory arguments */ for (i = n_trail; i-- > 0; ) { var = vars[vari++]; - if (var) *var = argv[argi]; + if (var) *var = (argi == last_idx) ? last_hash : argv[argi]; argi++; } /* capture an option hash - phase 2: assignment */ -- cgit v1.2.3