diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-05-21 13:24:33 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-05-21 13:24:33 +0000 |
commit | cc410660021e1c8f68b9799efea66cf1aa2267b9 (patch) | |
tree | c4f8c35b29dd7b8441d0dba11a9da2b2961bf69c /include | |
parent | ceea7ecef997411098fc0db3726897debbd9cdbf (diff) | |
download | ruby-cc410660021e1c8f68b9799efea66cf1aa2267b9.tar.gz |
* include/ruby/ruby.h (rb_scan_args): use __VA_ARGS__ instead of
va_arg to allow compilers optimize more aggressive.
https://gustedt.wordpress.com/2011/07/10/avoid-writing-va_arg-functions/
rb_scan_args is now expected to be statically resolved.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55102 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'include')
-rw-r--r-- | include/ruby/ruby.h | 139 |
1 files changed, 138 insertions, 1 deletions
diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index e09d4a739b..5a94e312cb 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -1767,7 +1767,6 @@ VALUE rb_funcallv_public(VALUE, ID, int, const VALUE*); #define rb_funcall3 rb_funcallv_public VALUE rb_funcall_passing_block(VALUE, ID, int, const VALUE*); VALUE rb_funcall_with_block(VALUE, ID, int, const VALUE*, VALUE); -int rb_scan_args(int, const VALUE*, const char*, ...); VALUE rb_call_super(int, const VALUE*); VALUE rb_current_receiver(void); int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *); @@ -2150,6 +2149,144 @@ unsigned long ruby_strtoul(const char *str, char **endptr, int base); PRINTF_ARGS(int ruby_snprintf(char *str, size_t n, char const *fmt, ...), 3, 4); int ruby_vsnprintf(char *str, size_t n, char const *fmt, va_list ap); +#define RB_NARG0(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,...) _19 +#define RB_NARG(...) RB_NARG0(__VA_ARGS__,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0) +#define rb_scan_args(argc,argvp,fmt,...) \ + rb_scan_args0(argc,argv,fmt,RB_NARG(__VA_ARGS__),(VALUE*[]){__VA_ARGS__}) +ALWAYS_INLINE(static inline int +rb_scan_args0(int argc, const VALUE *argv, const char *fmt, int varc, VALUE *vars[])); +static inline int +rb_scan_args0(int argc, const VALUE *argv, const char *fmt, int varc, VALUE *vars[]) +{ + int i; + const char *p = fmt; + VALUE *var; + int f_var = 0, f_hash = 0, f_block = 0; + int n_lead = 0, n_opt = 0, n_trail = 0, n_mand; + int argi = 0, vari = 0; + VALUE hash = Qnil; + + if (ISDIGIT(*p)) { + n_lead = *p - '0'; + p++; + if (ISDIGIT(*p)) { + n_opt = *p - '0'; + p++; + if (ISDIGIT(*p)) { + n_trail = *p - '0'; + p++; + goto block_arg; + } + } + } + if (*p == '*') { + f_var = 1; + p++; + if (ISDIGIT(*p)) { + n_trail = *p - '0'; + p++; + } + } + block_arg: + if (*p == ':') { + f_hash = 1; + p++; + } + if (*p == '&') { + f_block = 1; + p++; + } + if (*p != '\0') { + rb_fatal("bad scan arg format: %s", fmt); + } + n_mand = n_lead + n_trail; + + if (argc < n_mand) + goto argc_error; + + /* capture an option hash - phase 1: pop */ + if (f_hash && n_mand < argc) { + VALUE last = argv[argc - 1]; + + if (NIL_P(last)) { + /* nil is taken as an empty option hash only if it is not + ambiguous; i.e. '*' is not specified and arguments are + given more than sufficient */ + if (!f_var && n_mand + n_opt < argc) + argc--; + } + else { + hash = rb_check_hash_type(last); + if (!NIL_P(hash)) { + VALUE opts = rb_extract_keywords(&hash); + if (!hash) argc--; + hash = opts ? opts : Qnil; + } + } + } + /* capture leading mandatory arguments */ + for (i = n_lead; i-- > 0; ) { + var = vars[vari++]; + if (var) *var = 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]; + argi++; + } + else { + if (var) *var = Qnil; + } + } + /* capture variable length arguments */ + if (f_var) { + int n_var = argc - argi - n_trail; + + var = vars[vari++]; + if (0 < n_var) { + if (var) *var = rb_ary_new4(n_var, &argv[argi]); + argi += n_var; + } + else { + if (var) *var = rb_ary_new(); + } + } + /* capture trailing mandatory arguments */ + for (i = n_trail; i-- > 0; ) { + var = vars[vari++]; + if (var) *var = argv[argi]; + argi++; + } + /* capture an option hash - phase 2: assignment */ + if (f_hash) { + var = vars[vari++]; + if (var) *var = hash; + } + /* capture iterator block */ + if (f_block) { + var = vars[vari++]; + if (rb_block_given_p()) { + *var = rb_block_proc(); + } + else { + *var = Qnil; + } + } + + if (argi < argc) { + argc_error: + rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt); + } + if (vari != varc) { + rb_raise(rb_eRuntimeError, "variable argument length doesn't match* %d %d", vari, varc); + } + + return argc; +} + #ifndef RUBY_DONT_SUBST #include "ruby/subst.h" #endif |