summaryrefslogtreecommitdiff
path: root/class.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-21 14:01:23 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-21 14:01:23 +0000
commit8becb1e465ad96a4aff3ebb45a9d26972bd2c663 (patch)
tree31273e08ac860d934d5c34ea4607030091e3c00c /class.c
parent3caa1776689e108e9c701c2f52754185385368f7 (diff)
Revert r50102
This reverts "* include/ruby/ruby.h (rb_scan_args): don't use ALWAYS_INLINE with" This rb_scan_args macro is GCCism. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55104 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'class.c')
-rw-r--r--class.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/class.c b/class.c
index 62be074..0261838 100644
--- a/class.c
+++ b/class.c
@@ -1754,6 +1754,141 @@ rb_obj_basic_to_s_p(VALUE obj)
return 0;
}
+#include <stdarg.h>
+
+int
+rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
+{
+ int i;
+ const char *p = fmt;
+ VALUE *var;
+ va_list vargs;
+ 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;
+ 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;
+
+ va_start(vargs, fmt);
+
+ /* 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 = va_arg(vargs, VALUE *);
+ if (var) *var = argv[argi];
+ argi++;
+ }
+ /* capture optional arguments */
+ for (i = n_opt; i-- > 0; ) {
+ var = va_arg(vargs, VALUE *);
+ 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 = va_arg(vargs, VALUE *);
+ 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 = va_arg(vargs, VALUE *);
+ if (var) *var = argv[argi];
+ argi++;
+ }
+ /* capture an option hash - phase 2: assignment */
+ if (f_hash) {
+ var = va_arg(vargs, VALUE *);
+ if (var) *var = hash;
+ }
+ /* capture iterator block */
+ if (f_block) {
+ var = va_arg(vargs, VALUE *);
+ if (rb_block_given_p()) {
+ *var = rb_block_proc();
+ }
+ else {
+ *var = Qnil;
+ }
+ }
+ va_end(vargs);
+
+ if (argi < argc) {
+ argc_error:
+ rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
+ }
+
+ return argc;
+}
+
VALUE
rb_keyword_error_new(const char *error, VALUE keys)
{