diff --git a/ChangeLog b/ChangeLog index 58987bb6d69b16..9beefb869dfca4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +Wed Jun 26 16:52:57 2013 Kazuki Tsujimoto + + * test/ruby/test_proc.rb (TestProc#test_block_given_method_to_proc): + run test for r41359. + +Wed Jun 26 16:52:57 2013 Kazuki Tsujimoto + + * include/ruby/intern.h, proc.c (rb_method_call_with_block): + new function to invoke a Method object with a block passed + as an argument. + + * proc.c (bmcall): use the above function to avoid a block sharing. + [ruby-core:54626] [Bug #8341] + + * test/ruby/test_proc.rb (TestProc#test_block_persist_between_calls): + run related tests. + Wed Jun 26 16:36:39 2013 Nobuyoshi Nakada * defs/id.def (predefined): add "idProc". diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 98ef95230f4691..5851f2083b700e 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -361,6 +361,7 @@ VALUE rb_binding_new(void); VALUE rb_obj_method(VALUE, VALUE); VALUE rb_obj_is_method(VALUE); VALUE rb_method_call(int, VALUE*, VALUE); +VALUE rb_method_call_with_block(int, VALUE *, VALUE, VALUE); int rb_mod_method_arity(VALUE, ID); int rb_obj_method_arity(VALUE, ID); VALUE rb_protect(VALUE (*)(VALUE), VALUE, int*); diff --git a/proc.c b/proc.c index fdd148ebe94dd0..ca6d3c47697029 100644 --- a/proc.c +++ b/proc.c @@ -27,7 +27,7 @@ VALUE rb_cMethod; VALUE rb_cBinding; VALUE rb_cProc; -static VALUE bmcall(VALUE, VALUE); +static VALUE bmcall(VALUE, VALUE, int, VALUE *, VALUE); static int method_arity(VALUE); /* Proc */ @@ -1406,6 +1406,13 @@ method_clone(VALUE self) VALUE rb_method_call(int argc, VALUE *argv, VALUE method) +{ + VALUE proc = rb_block_given_p() ? rb_block_proc() : Qnil; + return rb_method_call_with_block(argc, argv, method, proc); +} + +VALUE +rb_method_call_with_block(int argc, VALUE *argv, VALUE method, VALUE pass_procval) { VALUE result = Qnil; /* OK */ struct METHOD *data; @@ -1425,8 +1432,15 @@ rb_method_call(int argc, VALUE *argv, VALUE method) } if ((state = EXEC_TAG()) == 0) { rb_thread_t *th = GET_THREAD(); + rb_block_t *block = 0; + + if (!NIL_P(pass_procval)) { + rb_proc_t *pass_proc; + GetProcPtr(pass_procval, pass_proc); + block = &pass_proc->block; + } - PASS_PASSED_BLOCK_TH(th); + th->passed_block = block; result = rb_vm_call(th, data->recv, data->id, argc, argv, data->me); } POP_TAG(); @@ -1802,11 +1816,10 @@ mlambda(VALUE method) } static VALUE -bmcall(VALUE args, VALUE method) +bmcall(VALUE args, VALUE method, int argc, VALUE *argv, VALUE passed_proc) { volatile VALUE a; VALUE ret; - int argc; if (CLASS_OF(args) != rb_cArray) { args = rb_ary_new3(1, args); @@ -1815,7 +1828,7 @@ bmcall(VALUE args, VALUE method) else { argc = check_argc(RARRAY_LEN(args)); } - ret = rb_method_call(argc, RARRAY_PTR(args), method); + ret = rb_method_call_with_block(argc, RARRAY_PTR(args), method, passed_proc); RB_GC_GUARD(a) = args; return ret; } diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index 31180bab8f68f0..7560ce94efc053 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -141,6 +141,14 @@ def block method(:m2).to_proc end + def m1(var) + var + end + + def m_block_given? + m1(block_given?) + end + # [yarv-dev:777] block made by Method#to_proc def test_method_to_proc b = block() @@ -148,6 +156,37 @@ def test_method_to_proc assert_instance_of(Binding, b.binding, '[ruby-core:25589]') end + def test_block_given_method + m = method(:m_block_given?) + assert(!m.call, "without block") + assert(m.call {}, "with block") + assert(!m.call, "without block second") + end + + def test_block_given_method_to_proc + bug8341 = '[Bug #8341]' + m = method(:m_block_given?).to_proc + assert(!m.call, "#{bug8341} without block") + assert(m.call {}, "#{bug8341} with block") + assert(!m.call, "#{bug8341} without block second") + end + + def test_block_persist_between_calls + bug8341 = '[Bug #8341]' + o = Object.new + def o.m1(top=true) + if top + [block_given?, @m.call(false)] + else + block_given? + end + end + m = o.method(:m1).to_proc + o.instance_variable_set(:@m, m) + assert_equal([true, false], m.call {}, "#{bug8341} nested with block") + assert_equal([false, false], m.call, "#{bug8341} nested without block") + end + def test_curry b = proc {|x, y, z| (x||0) + (y||0) + (z||0) } assert_equal(6, b.curry[1][2][3]) diff --git a/version.h b/version.h index 937e808e64ed86..81d2b327552ef0 100644 --- a/version.h +++ b/version.h @@ -1,5 +1,5 @@ #define RUBY_VERSION "1.9.3" -#define RUBY_PATCHLEVEL 441 +#define RUBY_PATCHLEVEL 442 #define RUBY_RELEASE_DATE "2013-06-26" #define RUBY_RELEASE_YEAR 2013