From 5d8acb25a71ac41bb2dc2f5b6c4b8ae04227b40f Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 3 Oct 2016 07:10:49 +0000 Subject: readline.c: mustbe_callable * ext/readline/readline.c (mustbe_callable): extract to check a given argument to be callable. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56324 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/readline/readline.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'ext') diff --git a/ext/readline/readline.c b/ext/readline/readline.c index c3b98a8dc9..14b66ea6c1 100644 --- a/ext/readline/readline.c +++ b/ext/readline/readline.c @@ -58,7 +58,7 @@ static VALUE mReadline; #define COMPLETION_PROC "completion_proc" #define COMPLETION_CASE_FOLD "completion_case_fold" -static ID completion_proc, completion_case_fold; +static ID id_call, completion_proc, completion_case_fold; #if USE_INSERT_IGNORE_ESCAPE static ID id_orig_prompt, id_last_prompt; #endif @@ -132,6 +132,13 @@ static VALUE readline_outstream; static FILE *readline_rl_instream; static FILE *readline_rl_outstream; +static void +mustbe_callable(VALUE proc) +{ + if (!NIL_P(proc) && !rb_respond_to(proc, id_call)) + rb_raise(rb_eArgError, "argument must respond to `call'"); +} + #if defined HAVE_RL_GETC_FUNCTION #ifndef HAVE_RL_GETC @@ -618,8 +625,7 @@ readline_s_set_output(VALUE self, VALUE output) static VALUE readline_s_set_pre_input_hook(VALUE self, VALUE proc) { - if (!NIL_P(proc) && !rb_respond_to(proc, rb_intern("call"))) - rb_raise(rb_eArgError, "argument must respond to `call'"); + mustbe_callable(proc); return rb_ivar_set(mReadline, id_pre_input_hook, proc); } @@ -646,7 +652,7 @@ readline_pre_input_hook(void) proc = rb_attr_get(mReadline, id_pre_input_hook); if (!NIL_P(proc)) - rb_funcall(proc, rb_intern("call"), 0); + rb_funcall(proc, id_call, 0); return 0; } #else @@ -815,8 +821,7 @@ readline_s_redisplay(VALUE self) static VALUE readline_s_set_completion_proc(VALUE self, VALUE proc) { - if (!NIL_P(proc) && !rb_respond_to(proc, rb_intern("call"))) - rb_raise(rb_eArgError, "argument must respond to `call'"); + mustbe_callable(proc); return rb_ivar_set(mReadline, completion_proc, proc); } @@ -953,7 +958,7 @@ readline_attempted_completion_function(const char *text, int start, int end) rl_attempted_completion_over = 1; #endif case_fold = RTEST(rb_attr_get(mReadline, completion_case_fold)); - ary = rb_funcall(proc, rb_intern("call"), 1, rb_locale_str_new_cstr(text)); + ary = rb_funcall(proc, id_call, 1, rb_locale_str_new_cstr(text)); if (!RB_TYPE_P(ary, T_ARRAY)) ary = rb_Array(ary); matches = RARRAY_LEN(ary); @@ -1794,6 +1799,7 @@ username_completion_proc_call(VALUE self, VALUE str) return result; } +#undef rb_intern void Init_readline(void) { @@ -1813,6 +1819,7 @@ Init_readline(void) using_history(); + id_call = rb_intern("call"); completion_proc = rb_intern(COMPLETION_PROC); completion_case_fold = rb_intern(COMPLETION_CASE_FOLD); #if defined(HAVE_RL_PRE_INPUT_HOOK) @@ -1979,7 +1986,7 @@ Init_readline(void) rl_attempted_completion_function = readline_attempted_completion_function; #if defined(HAVE_RL_PRE_INPUT_HOOK) - rl_pre_input_hook = (rl_hook_func_t *)readline_pre_input_hook; + rl_pre_input_hook = readline_pre_input_hook; #endif #ifdef HAVE_RL_CATCH_SIGNALS rl_catch_signals = 0; -- cgit v1.2.3