Skip to content

Commit ab4e82f

Browse files
committed
merge revision(s) 41342,41359,41361: [Backport ruby#8341]
test/ruby/test_proc.rb: tests for [Bug ruby#8341] * 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 ruby#8341] * test/ruby/test_proc.rb (TestProc#test_block_persist_between_calls): run related tests. * test/ruby/test_proc.rb (TestProc#test_block_given_method_to_proc): run test for r41359. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@41392 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 28e43a9 commit ab4e82f

File tree

5 files changed

+76
-6
lines changed

5 files changed

+76
-6
lines changed

ChangeLog

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
Wed Jun 19 03:06:57 2013 Kazuki Tsujimoto <[email protected]>
2+
3+
* test/ruby/test_proc.rb (TestProc#test_block_given_method_to_proc):
4+
run test for r41359.
5+
6+
Wed Jun 19 03:06:57 2013 Kazuki Tsujimoto <[email protected]>
7+
8+
* include/ruby/intern.h, proc.c (rb_method_call_with_block):
9+
new function to invoke a Method object with a block passed
10+
as an argument.
11+
12+
* proc.c (bmcall): use the above function to avoid a block sharing.
13+
[ruby-core:54626] [Bug #8341]
14+
15+
* test/ruby/test_proc.rb (TestProc#test_block_persist_between_calls):
16+
run related tests.
17+
118
Tue Jun 18 02:49:20 2013 NARUSE, Yui <[email protected]>
219

320
* test/ruby/envutil.rb (assert_separately): stop_auto_run of

include/ruby/intern.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ VALUE rb_binding_new(void);
393393
VALUE rb_obj_method(VALUE, VALUE);
394394
VALUE rb_obj_is_method(VALUE);
395395
VALUE rb_method_call(int, VALUE*, VALUE);
396+
VALUE rb_method_call_with_block(int, VALUE *, VALUE, VALUE);
396397
int rb_mod_method_arity(VALUE, ID);
397398
int rb_obj_method_arity(VALUE, ID);
398399
VALUE rb_protect(VALUE (*)(VALUE), VALUE, int*);

proc.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ VALUE rb_cMethod;
2828
VALUE rb_cBinding;
2929
VALUE rb_cProc;
3030

31-
static VALUE bmcall(VALUE, VALUE);
31+
static VALUE bmcall(VALUE, VALUE, int, VALUE *, VALUE);
3232
static int method_arity(VALUE);
3333
static int method_min_max_arity(VALUE, int *max);
3434
static ID attached;
@@ -1524,6 +1524,13 @@ method_clone(VALUE self)
15241524

15251525
VALUE
15261526
rb_method_call(int argc, VALUE *argv, VALUE method)
1527+
{
1528+
VALUE proc = rb_block_given_p() ? rb_block_proc() : Qnil;
1529+
return rb_method_call_with_block(argc, argv, method, proc);
1530+
}
1531+
1532+
VALUE
1533+
rb_method_call_with_block(int argc, VALUE *argv, VALUE method, VALUE pass_procval)
15271534
{
15281535
VALUE result = Qnil; /* OK */
15291536
struct METHOD *data;
@@ -1544,8 +1551,15 @@ rb_method_call(int argc, VALUE *argv, VALUE method)
15441551
}
15451552
if ((state = EXEC_TAG()) == 0) {
15461553
rb_thread_t *th = GET_THREAD();
1554+
rb_block_t *block = 0;
1555+
1556+
if (!NIL_P(pass_procval)) {
1557+
rb_proc_t *pass_proc;
1558+
GetProcPtr(pass_procval, pass_proc);
1559+
block = &pass_proc->block;
1560+
}
15471561

1548-
PASS_PASSED_BLOCK_TH(th);
1562+
th->passed_block = block;
15491563
result = rb_vm_call(th, data->recv, data->id, argc, argv, data->me, data->defined_class);
15501564
}
15511565
POP_TAG();
@@ -1989,11 +2003,10 @@ mlambda(VALUE method)
19892003
}
19902004

19912005
static VALUE
1992-
bmcall(VALUE args, VALUE method)
2006+
bmcall(VALUE args, VALUE method, int argc, VALUE *argv, VALUE passed_proc)
19932007
{
19942008
volatile VALUE a;
19952009
VALUE ret;
1996-
int argc;
19972010

19982011
if (CLASS_OF(args) != rb_cArray) {
19992012
args = rb_ary_new3(1, args);
@@ -2002,7 +2015,7 @@ bmcall(VALUE args, VALUE method)
20022015
else {
20032016
argc = check_argc(RARRAY_LEN(args));
20042017
}
2005-
ret = rb_method_call(argc, RARRAY_PTR(args), method);
2018+
ret = rb_method_call_with_block(argc, RARRAY_PTR(args), method, passed_proc);
20062019
RB_GC_GUARD(a) = args;
20072020
return ret;
20082021
}

test/ruby/test_proc.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,52 @@ def block
166166
method(:m2).to_proc
167167
end
168168

169+
def m1(var)
170+
var
171+
end
172+
173+
def m_block_given?
174+
m1(block_given?)
175+
end
176+
169177
# [yarv-dev:777] block made by Method#to_proc
170178
def test_method_to_proc
171179
b = block()
172180
assert_equal "OK", b.call
173181
assert_instance_of(Binding, b.binding, '[ruby-core:25589]')
174182
end
175183

184+
def test_block_given_method
185+
m = method(:m_block_given?)
186+
assert(!m.call, "without block")
187+
assert(m.call {}, "with block")
188+
assert(!m.call, "without block second")
189+
end
190+
191+
def test_block_given_method_to_proc
192+
bug8341 = '[Bug #8341]'
193+
m = method(:m_block_given?).to_proc
194+
assert(!m.call, "#{bug8341} without block")
195+
assert(m.call {}, "#{bug8341} with block")
196+
assert(!m.call, "#{bug8341} without block second")
197+
end
198+
199+
def test_block_persist_between_calls
200+
bug8341 = '[Bug #8341]'
201+
o = Object.new
202+
def o.m1(top=true)
203+
if top
204+
[block_given?, @m.call(false)]
205+
else
206+
block_given?
207+
end
208+
end
209+
m = o.method(:m1).to_proc
210+
o.instance_variable_set(:@m, m)
211+
assert_equal([true, false], m.call {}, "#{bug8341} nested with block")
212+
assert_equal([false, false], m.call, "#{bug8341} nested without block")
213+
end
214+
176215
def test_curry
177216
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
178217
assert_equal(6, b.curry[1][2][3])

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define RUBY_VERSION "2.0.0"
22
#define RUBY_RELEASE_DATE "2013-06-19"
3-
#define RUBY_PATCHLEVEL 229
3+
#define RUBY_PATCHLEVEL 230
44

55
#define RUBY_RELEASE_YEAR 2013
66
#define RUBY_RELEASE_MONTH 6

0 commit comments

Comments
 (0)