From e68406904298201bb7948f23364a330b38393383 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 9 Mar 2016 07:17:03 +0000 Subject: [PATCH] hash.c: SET_PROC_DEFAULT * hash.c (SET_PROC_DEFAULT): new macro to set the default proc. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54058 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- hash.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/hash.c b/hash.c index 491aa17b23..1cb9a898b1 100644 --- a/hash.c +++ b/hash.c @@ -36,6 +36,8 @@ FL_UNSET_RAW(hash, HASH_PROC_DEFAULT), \ RHASH_SET_IFNONE(hash, ifnone)) +#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc) + static VALUE has_extra_methods(VALUE klass) { @@ -528,14 +530,19 @@ tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg) RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg) static void -default_proc_arity_check(VALUE proc) +set_proc_default(VALUE hash, VALUE proc) { - int n = rb_proc_arity(proc); + if (rb_proc_lambda_p(proc)) { + int n = rb_proc_arity(proc); - if (rb_proc_lambda_p(proc) && n != 2 && (n >= 0 || n < -3)) { - if (n < 0) n = -n-1; - rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n); + if (n != 2 && (n >= 0 || n < -3)) { + if (n < 0) n = -n-1; + rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n); + } } + + FL_SET_RAW(hash, HASH_PROC_DEFAULT); + RHASH_SET_IFNONE(hash, proc); } /* @@ -582,9 +589,7 @@ rb_hash_initialize(int argc, VALUE *argv, VALUE hash) if (rb_block_given_p()) { rb_check_arity(argc, 0, 0); ifnone = rb_block_proc(); - default_proc_arity_check(ifnone); - RHASH_SET_IFNONE(hash, ifnone); - FL_SET(hash, HASH_PROC_DEFAULT); + SET_PROC_DEFAULT(hash, ifnone); } else { rb_check_arity(argc, 0, 1); @@ -1006,9 +1011,7 @@ rb_hash_set_default_proc(VALUE hash, VALUE proc) rb_obj_classname(proc)); } proc = b; - default_proc_arity_check(proc); - RHASH_SET_IFNONE(hash, proc); - FL_SET(hash, HASH_PROC_DEFAULT); + SET_PROC_DEFAULT(hash, proc); return proc; }