proc.c: check if callable
* proc.c: check the argument at composition, expect a Proc, Method, or callable object. [ruby-core:90591] [Bug #15428] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66769 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
671ef4e9e3
commit
33a75edd3c
37
proc.c
37
proc.c
@ -3063,6 +3063,21 @@ compose(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc)
|
|||||||
return rb_funcallv(f, idCall, 1, &fargs);
|
return rb_funcallv(f, idCall, 1, &fargs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
to_callable(VALUE f)
|
||||||
|
{
|
||||||
|
VALUE mesg;
|
||||||
|
|
||||||
|
if (rb_obj_is_proc(f)) return f;
|
||||||
|
if (rb_obj_is_method(f)) return f;
|
||||||
|
if (rb_obj_respond_to(f, idCall, TRUE)) return f;
|
||||||
|
mesg = rb_fstring_lit("callable object is expected");
|
||||||
|
rb_exc_raise(rb_exc_new_str(rb_eTypeError, mesg));
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE rb_proc_compose_to_left(VALUE self, VALUE g);
|
||||||
|
static VALUE rb_proc_compose_to_right(VALUE self, VALUE g);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* prc << g -> a_proc
|
* prc << g -> a_proc
|
||||||
@ -3077,6 +3092,12 @@ compose(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc)
|
|||||||
*/
|
*/
|
||||||
static VALUE
|
static VALUE
|
||||||
proc_compose_to_left(VALUE self, VALUE g)
|
proc_compose_to_left(VALUE self, VALUE g)
|
||||||
|
{
|
||||||
|
return rb_proc_compose_to_left(self, to_callable(g));
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_proc_compose_to_left(VALUE self, VALUE g)
|
||||||
{
|
{
|
||||||
VALUE proc, args, procs[2];
|
VALUE proc, args, procs[2];
|
||||||
rb_proc_t *procp;
|
rb_proc_t *procp;
|
||||||
@ -3110,6 +3131,12 @@ proc_compose_to_left(VALUE self, VALUE g)
|
|||||||
*/
|
*/
|
||||||
static VALUE
|
static VALUE
|
||||||
proc_compose_to_right(VALUE self, VALUE g)
|
proc_compose_to_right(VALUE self, VALUE g)
|
||||||
|
{
|
||||||
|
return rb_proc_compose_to_right(self, to_callable(g));
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_proc_compose_to_right(VALUE self, VALUE g)
|
||||||
{
|
{
|
||||||
VALUE proc, args, procs[2];
|
VALUE proc, args, procs[2];
|
||||||
rb_proc_t *procp;
|
rb_proc_t *procp;
|
||||||
@ -3148,8 +3175,9 @@ proc_compose_to_right(VALUE self, VALUE g)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_method_compose_to_left(VALUE self, VALUE g)
|
rb_method_compose_to_left(VALUE self, VALUE g)
|
||||||
{
|
{
|
||||||
VALUE proc = method_to_proc(self);
|
g = to_callable(g);
|
||||||
return proc_compose_to_left(proc, g);
|
self = method_to_proc(self);
|
||||||
|
return proc_compose_to_left(self, g);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -3171,8 +3199,9 @@ rb_method_compose_to_left(VALUE self, VALUE g)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_method_compose_to_right(VALUE self, VALUE g)
|
rb_method_compose_to_right(VALUE self, VALUE g)
|
||||||
{
|
{
|
||||||
VALUE proc = method_to_proc(self);
|
g = to_callable(g);
|
||||||
return proc_compose_to_right(proc, g);
|
self = method_to_proc(self);
|
||||||
|
return proc_compose_to_right(self, g);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1088,10 +1088,10 @@ class TestMethod < Test::Unit::TestCase
|
|||||||
}
|
}
|
||||||
f = c.new.method(:f)
|
f = c.new.method(:f)
|
||||||
|
|
||||||
assert_raise(NoMethodError) {
|
assert_raise(TypeError) {
|
||||||
(f << 5).call(2)
|
(f << 5).call(2)
|
||||||
}
|
}
|
||||||
assert_raise(NoMethodError) {
|
assert_raise(TypeError) {
|
||||||
(f >> 5).call(2)
|
(f >> 5).call(2)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -1474,10 +1474,10 @@ class TestProc < Test::Unit::TestCase
|
|||||||
def test_compose_with_noncallable
|
def test_compose_with_noncallable
|
||||||
f = proc {|x| x * 2}
|
f = proc {|x| x * 2}
|
||||||
|
|
||||||
assert_raise(NoMethodError) {
|
assert_raise(TypeError) {
|
||||||
(f << 5).call(2)
|
(f << 5).call(2)
|
||||||
}
|
}
|
||||||
assert_raise(NoMethodError) {
|
assert_raise(TypeError) {
|
||||||
(f >> 5).call(2)
|
(f >> 5).call(2)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user