1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

rb_hash_foreach now free from ANYARGS

After 5e86b005c0, I now think ANYARGS is
dangerous and should be extinct.  This commit adds function prototypes
for rb_hash_foreach / st_foreach_safe.  Also fixes some prototype
mismatches.
This commit is contained in:
卜部昌平 2019-08-27 11:53:39 +09:00
parent ae2dc3f217
commit 50f5a0a8d6
5 changed files with 34 additions and 20 deletions

View file

@ -1827,7 +1827,7 @@ struct cdhash_set_label_struct {
}; };
static int static int
cdhash_set_label_i(VALUE key, VALUE val, void *ptr) cdhash_set_label_i(VALUE key, VALUE val, VALUE ptr)
{ {
struct cdhash_set_label_struct *data = (struct cdhash_set_label_struct *)ptr; struct cdhash_set_label_struct *data = (struct cdhash_set_label_struct *)ptr;
LABEL *lobj = (LABEL *)(val & ~1); LABEL *lobj = (LABEL *)(val & ~1);

41
hash.c
View file

@ -868,7 +868,7 @@ ar_add_direct_with_hash(VALUE hash, st_data_t key, st_data_t val, st_hash_t hash
} }
static int static int
ar_general_foreach(VALUE hash, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg) ar_general_foreach(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
{ {
if (RHASH_AR_TABLE_SIZE(hash) > 0) { if (RHASH_AR_TABLE_SIZE(hash) > 0) {
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash); unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
@ -909,19 +909,32 @@ ar_general_foreach(VALUE hash, int (*func)(ANYARGS), st_update_callback_func *re
} }
static int static int
ar_foreach_with_replace(VALUE hash, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg) ar_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
{ {
return ar_general_foreach(hash, func, replace, arg); return ar_general_foreach(hash, func, replace, arg);
} }
struct functor {
st_foreach_callback_func *func;
st_data_t arg;
};
static int static int
ar_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg) apply_functor(st_data_t k, st_data_t v, st_data_t d, int _)
{ {
return ar_general_foreach(hash, func, NULL, arg); const struct functor *f = (void *)d;
return f->func(k, v, f->arg);
} }
static int static int
ar_foreach_check(VALUE hash, int (*func)(ANYARGS), st_data_t arg, ar_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
{
const struct functor f = { func, arg };
return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
}
static int
ar_foreach_check(VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
st_data_t never) st_data_t never)
{ {
if (RHASH_AR_TABLE_SIZE(hash) > 0) { if (RHASH_AR_TABLE_SIZE(hash) > 0) {
@ -1267,7 +1280,7 @@ foreach_safe_i(st_data_t key, st_data_t value, st_data_t args, int error)
} }
void void
st_foreach_safe(st_table *table, int (*func)(ANYARGS), st_data_t a) st_foreach_safe(st_table *table, st_foreach_func *func, st_data_t a)
{ {
struct foreach_safe_arg arg; struct foreach_safe_arg arg;
@ -1415,7 +1428,7 @@ hash_foreach_ensure(VALUE hash)
} }
int int
rb_hash_stlike_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg) rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
{ {
if (RHASH_AR_TABLE_P(hash)) { if (RHASH_AR_TABLE_P(hash)) {
return ar_foreach(hash, func, arg); return ar_foreach(hash, func, arg);
@ -1426,7 +1439,7 @@ rb_hash_stlike_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg)
} }
int int
rb_hash_stlike_foreach_with_replace(VALUE hash, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg) rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
{ {
if (RHASH_AR_TABLE_P(hash)) { if (RHASH_AR_TABLE_P(hash)) {
return ar_foreach_with_replace(hash, func, replace, arg); return ar_foreach_with_replace(hash, func, replace, arg);
@ -1456,7 +1469,7 @@ hash_foreach_call(VALUE arg)
} }
void void
rb_hash_foreach(VALUE hash, int (*func)(ANYARGS), VALUE farg) rb_hash_foreach(VALUE hash, rb_foreach_func *func, VALUE farg)
{ {
struct hash_foreach_arg arg; struct hash_foreach_arg arg;
@ -2923,7 +2936,7 @@ rb_hash_empty_p(VALUE hash)
} }
static int static int
each_value_i(VALUE key, VALUE value) each_value_i(VALUE key, VALUE value, VALUE _)
{ {
rb_yield(value); rb_yield(value);
return ST_CONTINUE; return ST_CONTINUE;
@ -2957,7 +2970,7 @@ rb_hash_each_value(VALUE hash)
} }
static int static int
each_key_i(VALUE key, VALUE value) each_key_i(VALUE key, VALUE value, VALUE _)
{ {
rb_yield(key); rb_yield(key);
return ST_CONTINUE; return ST_CONTINUE;
@ -2990,14 +3003,14 @@ rb_hash_each_key(VALUE hash)
} }
static int static int
each_pair_i(VALUE key, VALUE value) each_pair_i(VALUE key, VALUE value, VALUE _)
{ {
rb_yield(rb_assoc_new(key, value)); rb_yield(rb_assoc_new(key, value));
return ST_CONTINUE; return ST_CONTINUE;
} }
static int static int
each_pair_i_fast(VALUE key, VALUE value) each_pair_i_fast(VALUE key, VALUE value, VALUE _)
{ {
VALUE argv[2]; VALUE argv[2];
argv[0] = key; argv[0] = key;
@ -5918,7 +5931,7 @@ env_replace(VALUE env, VALUE hash)
} }
static int static int
env_update_i(VALUE key, VALUE val) env_update_i(VALUE key, VALUE val, VALUE _)
{ {
if (rb_block_given_p()) { if (rb_block_given_p()) {
val = rb_yield_values(3, key, rb_f_getenv(Qnil, key), val); val = rb_yield_values(3, key, rb_f_getenv(Qnil, key), val);

View file

@ -529,9 +529,9 @@ size_t rb_gc_stat(VALUE);
VALUE rb_gc_latest_gc_info(VALUE); VALUE rb_gc_latest_gc_info(VALUE);
void rb_gc_adjust_memory_usage(ssize_t); void rb_gc_adjust_memory_usage(ssize_t);
/* hash.c */ /* hash.c */
void st_foreach_safe(struct st_table *, int (*)(ANYARGS), st_data_t); void st_foreach_safe(struct st_table *, int (*)(st_data_t, st_data_t, st_data_t), st_data_t);
VALUE rb_check_hash_type(VALUE); VALUE rb_check_hash_type(VALUE);
void rb_hash_foreach(VALUE, int (*)(ANYARGS), VALUE); void rb_hash_foreach(VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
VALUE rb_hash(VALUE); VALUE rb_hash(VALUE);
VALUE rb_hash_new(void); VALUE rb_hash_new(void);
VALUE rb_hash_dup(VALUE); VALUE rb_hash_dup(VALUE);

View file

@ -1661,8 +1661,8 @@ VALUE rb_hash_set_pair(VALUE hash, VALUE pair);
int rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval); int rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval);
int rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval); int rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval);
int rb_hash_stlike_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg); int rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg);
int rb_hash_stlike_foreach_with_replace(VALUE hash, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg); int rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg);
int rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func func, st_data_t arg); int rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func func, st_data_t arg);
/* inits.c */ /* inits.c */

View file

@ -500,8 +500,9 @@ w_unique(VALUE s, struct dump_arg *arg)
static void w_object(VALUE,struct dump_arg*,int); static void w_object(VALUE,struct dump_arg*,int);
static int static int
hash_each(VALUE key, VALUE value, struct dump_call_arg *arg) hash_each(VALUE key, VALUE value, VALUE v)
{ {
struct dump_call_arg *arg = (void *)v;
w_object(key, arg->arg, arg->limit); w_object(key, arg->arg, arg->limit);
w_object(value, arg->arg, arg->limit); w_object(value, arg->arg, arg->limit);
return ST_CONTINUE; return ST_CONTINUE;