ruby-changes:57310
From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Tue, 27 Aug 2019 16:03:35 +0900 (JST)
Subject: [ruby-changes:57310] 卜部昌平: 50f5a0a8d6 (master): rb_hash_foreach now free from ANYARGS
https://git.ruby-lang.org/ruby.git/commit/?id=50f5a0a8d6 From 50f5a0a8d6e7ad89d6caff695a08dbd38edb7a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= <shyouhei@r...> Date: Tue, 27 Aug 2019 11:53:39 +0900 Subject: rb_hash_foreach now free from ANYARGS After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, 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. diff --git a/compile.c b/compile.c index 29965df..cf29da3 100644 --- a/compile.c +++ b/compile.c @@ -1827,7 +1827,7 @@ struct cdhash_set_label_struct { https://github.com/ruby/ruby/blob/trunk/compile.c#L1827 }; 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; LABEL *lobj = (LABEL *)(val & ~1); diff --git a/hash.c b/hash.c index 9a86396..4671adf 100644 --- a/hash.c +++ b/hash.c @@ -868,7 +868,7 @@ ar_add_direct_with_hash(VALUE hash, st_data_t key, st_data_t val, st_hash_t hash https://github.com/ruby/ruby/blob/trunk/hash.c#L868 } 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) { 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 https://github.com/ruby/ruby/blob/trunk/hash.c#L909 } 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); } +struct functor { + st_foreach_callback_func *func; + st_data_t arg; +}; + +static int +apply_functor(st_data_t k, st_data_t v, st_data_t d, int _) +{ + const struct functor *f = (void *)d; + return f->func(k, v, f->arg); +} + static int -ar_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg) +ar_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg) { - return ar_general_foreach(hash, func, NULL, 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, int (*func)(ANYARGS), st_data_t arg, +ar_foreach_check(VALUE hash, st_foreach_check_callback_func *func, st_data_t arg, st_data_t never) { 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) https://github.com/ruby/ruby/blob/trunk/hash.c#L1280 } 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; @@ -1415,7 +1428,7 @@ hash_foreach_ensure(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L1428 } 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)) { return ar_foreach(hash, func, arg); @@ -1426,7 +1439,7 @@ rb_hash_stlike_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg) https://github.com/ruby/ruby/blob/trunk/hash.c#L1439 } 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)) { return ar_foreach_with_replace(hash, func, replace, arg); @@ -1456,7 +1469,7 @@ hash_foreach_call(VALUE arg) https://github.com/ruby/ruby/blob/trunk/hash.c#L1469 } 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; @@ -2923,7 +2936,7 @@ rb_hash_empty_p(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L2936 } static int -each_value_i(VALUE key, VALUE value) +each_value_i(VALUE key, VALUE value, VALUE _) { rb_yield(value); return ST_CONTINUE; @@ -2957,7 +2970,7 @@ rb_hash_each_value(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L2970 } static int -each_key_i(VALUE key, VALUE value) +each_key_i(VALUE key, VALUE value, VALUE _) { rb_yield(key); return ST_CONTINUE; @@ -2990,14 +3003,14 @@ rb_hash_each_key(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3003 } static int -each_pair_i(VALUE key, VALUE value) +each_pair_i(VALUE key, VALUE value, VALUE _) { rb_yield(rb_assoc_new(key, value)); return ST_CONTINUE; } static int -each_pair_i_fast(VALUE key, VALUE value) +each_pair_i_fast(VALUE key, VALUE value, VALUE _) { VALUE argv[2]; argv[0] = key; @@ -5918,7 +5931,7 @@ env_replace(VALUE env, VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L5931 } static int -env_update_i(VALUE key, VALUE val) +env_update_i(VALUE key, VALUE val, VALUE _) { if (rb_block_given_p()) { val = rb_yield_values(3, key, rb_f_getenv(Qnil, key), val); diff --git a/include/ruby/intern.h b/include/ruby/intern.h index aaf73a1..51d4132 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -529,9 +529,9 @@ size_t rb_gc_stat(VALUE); https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L529 VALUE rb_gc_latest_gc_info(VALUE); void rb_gc_adjust_memory_usage(ssize_t); /* 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); -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_new(void); VALUE rb_hash_dup(VALUE); diff --git a/internal.h b/internal.h index bd9ee44..5e73c11 100644 --- a/internal.h +++ b/internal.h @@ -1661,8 +1661,8 @@ VALUE rb_hash_set_pair(VALUE hash, VALUE pair); https://github.com/ruby/ruby/blob/trunk/internal.h#L1661 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_foreach(VALUE hash, int (*func)(ANYARGS), 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(VALUE hash, st_foreach_callback_func *func, 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); /* inits.c */ diff --git a/marshal.c b/marshal.c index 0d7cbc1..b21db26 100644 --- a/marshal.c +++ b/marshal.c @@ -500,8 +500,9 @@ w_unique(VALUE s, struct dump_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L500 static void w_object(VALUE,struct dump_arg*,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(value, arg->arg, arg->limit); return ST_CONTINUE; -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/