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

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
This commit is contained in:
nobu 2016-10-03 07:10:49 +00:00
parent 91430022d6
commit 5d8acb25a7

View file

@ -58,7 +58,7 @@ static VALUE mReadline;
#define COMPLETION_PROC "completion_proc" #define COMPLETION_PROC "completion_proc"
#define COMPLETION_CASE_FOLD "completion_case_fold" #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 #if USE_INSERT_IGNORE_ESCAPE
static ID id_orig_prompt, id_last_prompt; static ID id_orig_prompt, id_last_prompt;
#endif #endif
@ -132,6 +132,13 @@ static VALUE readline_outstream;
static FILE *readline_rl_instream; static FILE *readline_rl_instream;
static FILE *readline_rl_outstream; 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 #if defined HAVE_RL_GETC_FUNCTION
#ifndef HAVE_RL_GETC #ifndef HAVE_RL_GETC
@ -618,8 +625,7 @@ readline_s_set_output(VALUE self, VALUE output)
static VALUE static VALUE
readline_s_set_pre_input_hook(VALUE self, VALUE proc) readline_s_set_pre_input_hook(VALUE self, VALUE proc)
{ {
if (!NIL_P(proc) && !rb_respond_to(proc, rb_intern("call"))) mustbe_callable(proc);
rb_raise(rb_eArgError, "argument must respond to `call'");
return rb_ivar_set(mReadline, id_pre_input_hook, 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); proc = rb_attr_get(mReadline, id_pre_input_hook);
if (!NIL_P(proc)) if (!NIL_P(proc))
rb_funcall(proc, rb_intern("call"), 0); rb_funcall(proc, id_call, 0);
return 0; return 0;
} }
#else #else
@ -815,8 +821,7 @@ readline_s_redisplay(VALUE self)
static VALUE static VALUE
readline_s_set_completion_proc(VALUE self, VALUE proc) readline_s_set_completion_proc(VALUE self, VALUE proc)
{ {
if (!NIL_P(proc) && !rb_respond_to(proc, rb_intern("call"))) mustbe_callable(proc);
rb_raise(rb_eArgError, "argument must respond to `call'");
return rb_ivar_set(mReadline, completion_proc, 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; rl_attempted_completion_over = 1;
#endif #endif
case_fold = RTEST(rb_attr_get(mReadline, completion_case_fold)); 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)) if (!RB_TYPE_P(ary, T_ARRAY))
ary = rb_Array(ary); ary = rb_Array(ary);
matches = RARRAY_LEN(ary); matches = RARRAY_LEN(ary);
@ -1794,6 +1799,7 @@ username_completion_proc_call(VALUE self, VALUE str)
return result; return result;
} }
#undef rb_intern
void void
Init_readline(void) Init_readline(void)
{ {
@ -1813,6 +1819,7 @@ Init_readline(void)
using_history(); using_history();
id_call = rb_intern("call");
completion_proc = rb_intern(COMPLETION_PROC); completion_proc = rb_intern(COMPLETION_PROC);
completion_case_fold = rb_intern(COMPLETION_CASE_FOLD); completion_case_fold = rb_intern(COMPLETION_CASE_FOLD);
#if defined(HAVE_RL_PRE_INPUT_HOOK) #if defined(HAVE_RL_PRE_INPUT_HOOK)
@ -1979,7 +1986,7 @@ Init_readline(void)
rl_attempted_completion_function = readline_attempted_completion_function; rl_attempted_completion_function = readline_attempted_completion_function;
#if defined(HAVE_RL_PRE_INPUT_HOOK) #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 #endif
#ifdef HAVE_RL_CATCH_SIGNALS #ifdef HAVE_RL_CATCH_SIGNALS
rl_catch_signals = 0; rl_catch_signals = 0;