ruby-changes:57378
From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Thu, 29 Aug 2019 19:13:52 +0900 (JST)
Subject: [ruby-changes:57378] 卜部昌平: 7bcfd9189a (master): drop-in type check for rb_define_global_function
https://git.ruby-lang.org/ruby.git/commit/?id=7bcfd9189a From 7bcfd9189a6a0b2ad58fed988faaf795a4987893 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: Wed, 28 Aug 2019 18:19:11 +0900 Subject: drop-in type check for rb_define_global_function We can check the function pointer passed to rb_define_global_function like we do so in rb_define_method. It turns out that almost anybody is misunderstanding the API. diff --git a/class.c b/class.c index 05440f5..2f1e9d7 100644 --- a/class.c +++ b/class.c @@ -1767,7 +1767,9 @@ rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS) https://github.com/ruby/ruby/blob/trunk/class.c#L1767 rb_define_singleton_method(module, name, func, argc); } - +#ifdef rb_define_global_function +#undef rb_define_global_function +#endif /*! * Defines a global function * \param name name of the function diff --git a/eval.c b/eval.c index c2f15fb..90f36b8 100644 --- a/eval.c +++ b/eval.c @@ -769,6 +769,12 @@ rb_f_raise(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/eval.c#L769 } static VALUE +f_raise(int c, VALUE *v, VALUE _) +{ + return rb_f_raise(c, v); +} + +static VALUE make_exception(int argc, const VALUE *argv, int isstr) { VALUE mesg, exc; @@ -1883,7 +1889,7 @@ errat_setter(VALUE val, ID id, VALUE *var) https://github.com/ruby/ruby/blob/trunk/eval.c#L1889 */ static VALUE -rb_f_method_name(void) +rb_f_method_name(VALUE _) { ID fname = prev_frame_func(); /* need *method* ID */ @@ -1905,7 +1911,7 @@ rb_f_method_name(void) https://github.com/ruby/ruby/blob/trunk/eval.c#L1911 */ static VALUE -rb_f_callee_name(void) +rb_f_callee_name(VALUE _) { ID fname = prev_frame_callee(); /* need *callee* ID */ @@ -1928,7 +1934,7 @@ rb_f_callee_name(void) https://github.com/ruby/ruby/blob/trunk/eval.c#L1934 * */ static VALUE -f_current_dirname(void) +f_current_dirname(VALUE _) { VALUE base = rb_current_realfilepath(); if (NIL_P(base)) { @@ -1938,16 +1944,34 @@ f_current_dirname(void) https://github.com/ruby/ruby/blob/trunk/eval.c#L1944 return base; } +static VALUE +f_global_variables(VALUE _) +{ + return rb_f_global_variables(); +} + +static VALUE +f_trace_var(int c, const VALUE *a, VALUE _) +{ + return rb_f_trace_var(c, a); +} + +static VALUE +f_untrace_var(int c, const VALUE *a, VALUE _) +{ + return rb_f_untrace_var(c, a); +} + void Init_eval(void) { rb_define_virtual_variable("$@", errat_getter, errat_setter); rb_define_virtual_variable("$!", errinfo_getter, 0); - rb_define_global_function("raise", rb_f_raise, -1); - rb_define_global_function("fail", rb_f_raise, -1); + rb_define_global_function("raise", f_raise, -1); + rb_define_global_function("fail", f_raise, -1); - rb_define_global_function("global_variables", rb_f_global_variables, 0); /* in variable.c */ + rb_define_global_function("global_variables", f_global_variables, 0); rb_define_global_function("__method__", rb_f_method_name, 0); rb_define_global_function("__callee__", rb_f_callee_name, 0); @@ -1980,8 +2004,8 @@ Init_eval(void) https://github.com/ruby/ruby/blob/trunk/eval.c#L2004 rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1); - rb_define_global_function("trace_var", rb_f_trace_var, -1); /* in variable.c */ - rb_define_global_function("untrace_var", rb_f_untrace_var, -1); /* in variable.c */ + rb_define_global_function("trace_var", f_trace_var, -1); + rb_define_global_function("untrace_var", f_untrace_var, -1); rb_vm_register_special_exception(ruby_error_reenter, rb_eFatal, "exception reentered"); rb_vm_register_special_exception(ruby_error_stackfatal, rb_eFatal, "machine stack overflow in critical region"); diff --git a/eval_jump.c b/eval_jump.c index b010f44..75d4ad0 100644 --- a/eval_jump.c +++ b/eval_jump.c @@ -35,7 +35,7 @@ rb_call_end_proc(VALUE data) https://github.com/ruby/ruby/blob/trunk/eval_jump.c#L35 */ static VALUE -rb_f_at_exit(void) +rb_f_at_exit(VALUE _) { VALUE proc; diff --git a/file.c b/file.c index 70f3283..b33b5f6 100644 --- a/file.c +++ b/file.c @@ -5225,7 +5225,7 @@ test_check(int n, int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/file.c#L5225 */ static VALUE -rb_f_test(int argc, VALUE *argv) +rb_f_test(int argc, VALUE *argv, VALUE _) { int cmd; diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 33bafa9..61fb3b0 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -2774,6 +2774,47 @@ __attribute__((__unused__,__weakref__("rb_define_module_function"),__nonnull__(2 https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L2774 #define rb_define_module_function_choose_prototypem2(n) rb_define_method_if_constexpr((n)==-2,rb_define_module_functionm2,rb_define_module_function_choose_prototypem1(n)) #define rb_define_module_function_choose_prototypem3(n, f) rb_define_method_if_constexpr(rb_f_notimplement_p(f),rb_define_module_functionm3,rb_define_module_function_choose_prototypem2(n)) #define rb_define_module_function(klass, mid, func, arity) rb_define_module_function_choose_prototypem3((arity),(func))((klass),(mid),(func),(arity)); + +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_functionm3(const char*,VALUE(*)(ANYARGS),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_functionm2(const char*,VALUE(*)(VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_functionm1(const char*,VALUE(*)(int,union __attribute__((__transparent_union__)){VALUE*x;const VALUE*y;},VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function0 (const char*,VALUE(*)(VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function1 (const char*,VALUE(*)(VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function2 (const char*,VALUE(*)(VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function3 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function4 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function5 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function6 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function7 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function8 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function9 (const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function10(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function11(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function12(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function13(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function14(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); +__attribute__((__unused__,__weakref__("rb_define_global_function"),__nonnull__(1,2)))static void rb_define_global_function15(const char*,VALUE(*)(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE),int); + +#define rb_define_global_function_choose_prototype15(n) rb_define_method_if_constexpr((n)==15,rb_define_global_function15,rb_define_global_functionm3) +#define rb_define_global_function_choose_prototype14(n) rb_define_method_if_constexpr((n)==14,rb_define_global_function14,rb_define_global_function_choose_prototype15(n)) +#define rb_define_global_function_choose_prototype13(n) rb_define_method_if_constexpr((n)==13,rb_define_global_function13,rb_define_global_function_choose_prototype14(n)) +#define rb_define_global_function_choose_prototype12(n) rb_define_method_if_constexpr((n)==12,rb_define_global_function12,rb_define_global_function_choose_prototype13(n)) +#define rb_define_global_function_choose_prototype11(n) rb_define_method_if_constexpr((n)==11,rb_define_global_function11,rb_define_global_function_choose_prototype12(n)) +#define rb_define_global_function_choose_prototype10(n) rb_define_method_if_constexpr((n)= (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/