ruby-changes:54554
From: nobu <ko1@a...>
Date: Thu, 10 Jan 2019 15:01:37 +0900 (JST)
Subject: [ruby-changes:54554] nobu:r66769 (trunk): proc.c: check if callable
nobu 2019-01-10 15:01:32 +0900 (Thu, 10 Jan 2019) New Revision: 66769 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66769 Log: proc.c: check if callable * proc.c: check the argument at composition, expect a Proc, Method, or callable object. [ruby-core:90591] [Bug #15428] Modified files: trunk/proc.c trunk/test/ruby/test_method.rb trunk/test/ruby/test_proc.rb Index: test/ruby/test_method.rb =================================================================== --- test/ruby/test_method.rb (revision 66768) +++ test/ruby/test_method.rb (revision 66769) @@ -1088,10 +1088,10 @@ class TestMethod < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_method.rb#L1088 } f = c.new.method(:f) - assert_raise(NoMethodError) { + assert_raise(TypeError) { (f << 5).call(2) } - assert_raise(NoMethodError) { + assert_raise(TypeError) { (f >> 5).call(2) } end Index: test/ruby/test_proc.rb =================================================================== --- test/ruby/test_proc.rb (revision 66768) +++ test/ruby/test_proc.rb (revision 66769) @@ -1474,10 +1474,10 @@ class TestProc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_proc.rb#L1474 def test_compose_with_noncallable f = proc {|x| x * 2} - assert_raise(NoMethodError) { + assert_raise(TypeError) { (f << 5).call(2) } - assert_raise(NoMethodError) { + assert_raise(TypeError) { (f >> 5).call(2) } end Index: proc.c =================================================================== --- proc.c (revision 66768) +++ proc.c (revision 66769) @@ -3063,6 +3063,21 @@ compose(VALUE dummy, VALUE args, int arg https://github.com/ruby/ruby/blob/trunk/proc.c#L3063 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: * prc << g -> a_proc @@ -3078,6 +3093,12 @@ compose(VALUE dummy, VALUE args, int arg https://github.com/ruby/ruby/blob/trunk/proc.c#L3093 static VALUE 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]; rb_proc_t *procp; int is_lambda; @@ -3111,6 +3132,12 @@ proc_compose_to_left(VALUE self, VALUE g https://github.com/ruby/ruby/blob/trunk/proc.c#L3132 static VALUE 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]; rb_proc_t *procp; int is_lambda; @@ -3148,8 +3175,9 @@ proc_compose_to_right(VALUE self, VALUE https://github.com/ruby/ruby/blob/trunk/proc.c#L3175 static VALUE rb_method_compose_to_left(VALUE self, VALUE g) { - VALUE proc = method_to_proc(self); - return proc_compose_to_left(proc, g); + g = to_callable(g); + self = method_to_proc(self); + return proc_compose_to_left(self, g); } /* @@ -3171,8 +3199,9 @@ rb_method_compose_to_left(VALUE self, VA https://github.com/ruby/ruby/blob/trunk/proc.c#L3199 static VALUE rb_method_compose_to_right(VALUE self, VALUE g) { - VALUE proc = method_to_proc(self); - return proc_compose_to_right(proc, g); + g = to_callable(g); + self = method_to_proc(self); + return proc_compose_to_right(self, g); } /* -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/