[前][次][番号順一覧][スレッド一覧]

ruby-changes:57313

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Tue, 27 Aug 2019 16:18:39 +0900 (JST)
Subject: [ruby-changes:57313] 卜部昌平: 6dd60cf114 (master): st_foreach now free from ANYARGS

https://git.ruby-lang.org/ruby.git/commit/?id=6dd60cf114

From 6dd60cf114701f1ff3526381c0e742c588af2f91 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: Mon, 26 Aug 2019 16:06:40 +0900
Subject: st_foreach now free from ANYARGS

After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is
dangerous and should be extinct.  This commit deletes ANYARGS from
st_foreach.  I strongly believe that this commit should have had come
with b0af0592fdd9e9d4e4b863fde006d67ccefeac21, which added extra
parameter to st_foreach callbacks.

diff --git a/ext/-test-/st/foreach/foreach.c b/ext/-test-/st/foreach/foreach.c
index 209b535..27ac180 100644
--- a/ext/-test-/st/foreach/foreach.c
+++ b/ext/-test-/st/foreach/foreach.c
@@ -106,7 +106,7 @@ unp_fec(VALUE self, VALUE test) https://github.com/ruby/ruby/blob/trunk/ext/-test-/st/foreach/foreach.c#L106
 }
 
 static int
-unp_fe_i(st_data_t key, st_data_t val, st_data_t args, int error)
+unp_fe_i(st_data_t key, st_data_t val, st_data_t args)
 {
     struct checker *c = (struct checker *)args;
 
diff --git a/ext/-test-/st/numhash/numhash.c b/ext/-test-/st/numhash/numhash.c
index fc35f47..71eeed4 100644
--- a/ext/-test-/st/numhash/numhash.c
+++ b/ext/-test-/st/numhash/numhash.c
@@ -57,7 +57,7 @@ numhash_aset(VALUE self, VALUE key, VALUE data) https://github.com/ruby/ruby/blob/trunk/ext/-test-/st/numhash/numhash.c#L57
 }
 
 static int
-numhash_i(st_data_t key, st_data_t value, st_data_t arg)
+numhash_i(st_data_t key, st_data_t value, st_data_t arg, int _)
 {
     VALUE ret;
     ret = rb_yield_values(3, (VALUE)key, (VALUE)value, (VALUE)arg);
@@ -135,4 +135,3 @@ Init_numhash(void) https://github.com/ruby/ruby/blob/trunk/ext/-test-/st/numhash/numhash.c#L135
     rb_define_method(st, "size", numhash_size, 0);
     rb_define_method(st, "delete_safe", numhash_delete_safe, 1);
 }
-
diff --git a/ext/objspace/object_tracing.c b/ext/objspace/object_tracing.c
index 7c35449..a057ac2 100644
--- a/ext/objspace/object_tracing.c
+++ b/ext/objspace/object_tracing.c
@@ -138,14 +138,14 @@ freeobj_i(VALUE tpval, void *data) https://github.com/ruby/ruby/blob/trunk/ext/objspace/object_tracing.c#L138
 }
 
 static int
-free_keys_i(st_data_t key, st_data_t value, void *data)
+free_keys_i(st_data_t key, st_data_t value, st_data_t data)
 {
     ruby_xfree((void *)key);
     return ST_CONTINUE;
 }
 
 static int
-free_values_i(st_data_t key, st_data_t value, void *data)
+free_values_i(st_data_t key, st_data_t value, st_data_t data)
 {
     ruby_xfree((void *)value);
     return ST_CONTINUE;
diff --git a/gc.c b/gc.c
index 42559eb..32b7807 100644
--- a/gc.c
+++ b/gc.c
@@ -5620,7 +5620,7 @@ gc_check_after_marks_i(st_data_t k, st_data_t v, void *ptr) https://github.com/ruby/ruby/blob/trunk/gc.c#L5620
 }
 
 static void
-gc_marks_check(rb_objspace_t *objspace, int (*checker_func)(ANYARGS), const char *checker_name)
+gc_marks_check(rb_objspace_t *objspace, st_foreach_callback_func *checker_func, const char *checker_name)
 {
     size_t saved_malloc_increase = objspace->malloc_params.increase;
 #if RGENGC_ESTIMATE_OLDMALLOC
diff --git a/include/ruby/st.h b/include/ruby/st.h
index a7eb0c6..9b48d51 100644
--- a/include/ruby/st.h
+++ b/include/ruby/st.h
@@ -118,9 +118,11 @@ typedef int st_update_callback_func(st_data_t *key, st_data_t *value, st_data_t https://github.com/ruby/ruby/blob/trunk/include/ruby/st.h#L118
  * results of hash() are same and compare() returns 0, otherwise the
  * behavior is undefined */
 int st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg);
-int st_foreach_with_replace(st_table *tab, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg);
-int st_foreach(st_table *, int (*)(ANYARGS), st_data_t);
-int st_foreach_check(st_table *, int (*)(ANYARGS), st_data_t, st_data_t);
+typedef int st_foreach_callback_func(st_data_t, st_data_t, st_data_t);
+typedef int st_foreach_check_callback_func(st_data_t, st_data_t, st_data_t, int);
+int st_foreach_with_replace(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg);
+int st_foreach(st_table *, st_foreach_callback_func *, st_data_t);
+int st_foreach_check(st_table *, st_foreach_check_callback_func *, st_data_t, st_data_t);
 st_index_t st_keys(st_table *table, st_data_t *keys, st_index_t size);
 st_index_t st_keys_check(st_table *table, st_data_t *keys, st_index_t size, st_data_t never);
 st_index_t st_values(st_table *table, st_data_t *values, st_index_t size);
diff --git a/marshal.c b/marshal.c
index b6de650..0d7cbc1 100644
--- a/marshal.c
+++ b/marshal.c
@@ -112,7 +112,7 @@ static VALUE rb_marshal_dump_limited(VALUE obj, VALUE port, int limit); https://github.com/ruby/ruby/blob/trunk/marshal.c#L112
 static VALUE rb_marshal_load_with_proc(VALUE port, VALUE proc);
 
 static int
-mark_marshal_compat_i(st_data_t key, st_data_t value)
+mark_marshal_compat_i(st_data_t key, st_data_t value, st_data_t _)
 {
     marshal_compat_t *p = (marshal_compat_t *)value;
     rb_gc_mark(p->newclass);
diff --git a/regparse.c b/regparse.c
index 574a07e..5f11890 100644
--- a/regparse.c
+++ b/regparse.c
@@ -493,7 +493,7 @@ onig_print_names(FILE* fp, regex_t* reg) https://github.com/ruby/ruby/blob/trunk/regparse.c#L493
 
   if (IS_NOT_NULL(t)) {
     fprintf(fp, "name table\n");
-    onig_st_foreach(t, i_print_name_entry, (HashDataType )fp);
+    onig_st_foreach(t, (st_foreach_callback_func *)i_print_name_entry, (HashDataType )fp);
     fputs("\n", fp);
   }
   return 0;
@@ -516,7 +516,7 @@ names_clear(regex_t* reg) https://github.com/ruby/ruby/blob/trunk/regparse.c#L516
   NameTable* t = (NameTable* )reg->name_table;
 
   if (IS_NOT_NULL(t)) {
-    onig_st_foreach(t, i_free_name_entry, 0);
+    onig_st_foreach(t, (st_foreach_callback_func *)i_free_name_entry, 0);
   }
   return 0;
 }
@@ -585,7 +585,7 @@ onig_foreach_name(regex_t* reg, https://github.com/ruby/ruby/blob/trunk/regparse.c#L585
     narg.reg  = reg;
     narg.arg  = arg;
     narg.enc  = reg->enc; /* should be pattern encoding. */
-    onig_st_foreach(t, i_names, (HashDataType )&narg);
+    onig_st_foreach(t, (st_foreach_callback_func *)i_names, (HashDataType )&narg);
   }
   return narg.ret;
 }
@@ -613,7 +613,7 @@ onig_renumber_name_table(regex_t* reg, GroupNumRemap* map) https://github.com/ruby/ruby/blob/trunk/regparse.c#L613
   NameTable* t = (NameTable* )reg->name_table;
 
   if (IS_NOT_NULL(t)) {
-    onig_st_foreach(t, i_renumber_name, (HashDataType )map);
+    onig_st_foreach(t, (st_foreach_callback_func *)i_renumber_name, (HashDataType )map);
   }
   return 0;
 }
diff --git a/st.c b/st.c
index cecc2ac..c32a7ed 100644
--- a/st.c
+++ b/st.c
@@ -1548,7 +1548,7 @@ st_update(st_table *tab, st_data_t key, https://github.com/ruby/ruby/blob/trunk/st.c#L1548
    different for ST_CHECK and when the current element is removed
    during traversing.  */
 static inline int
-st_general_foreach(st_table *tab, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg,
+st_general_foreach(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg,
 		   int check_p)
 {
     st_index_t bin;
@@ -1659,20 +1659,33 @@ st_general_foreach(st_table *tab, int (*func)(ANYARGS), st_update_callback_func https://github.com/ruby/ruby/blob/trunk/st.c#L1659
 }
 
 int
-st_foreach_with_replace(st_table *tab, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg)
+st_foreach_with_replace(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
 {
     return st_general_foreach(tab, func, replace, arg, TRUE);
 }
 
+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);
+}
+
 int
-st_foreach(st_table *tab, int (*func)(ANYARGS), st_data_t arg)
+st_foreach(st_table *tab, st_foreach_callback_func *func, st_data_t arg)
 {
-    return st_general_foreach(tab, func, NULL, arg, FALSE);
+    const struct functor f = { func, arg };
+    return st_general_foreach(tab, apply_functor, NULL, (st_data_t)&f, FALSE);
 }
 
 /* See comments for function st_delete_safe.  */
 int
-st_foreach_check(st_table *tab, int (*func)(ANYARGS), st_data_t arg,
+st_foreach_check(st_table *tab, st_foreach_check_callback_func *func, st_data_t arg,
                  st_data_t never ATTRIBUTE_UNUSED)
 {
     return st_general_foreach(tab, func, NULL, arg, TRUE);
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]