summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2013-09-19 13:40:32 -0700
committerYukihiro "Matz" Matsumoto <[email protected]>2013-09-19 13:40:32 -0700
commite7082a5092590082f6591738ed2ff2e0e26ce2d1 (patch)
tree1ec8694657d173523cf60e43f2776855ae8bfb78
parentf65a39f4d19b1de019b0b805ccfb081757e5b7b5 (diff)
parentc4e5a8b1675a187afaf04fb54b09d6a35c83e045 (diff)
downloadmruby-e7082a5092590082f6591738ed2ff2e0e26ce2d1.tar.gz
mruby-e7082a5092590082f6591738ed2ff2e0e26ce2d1.zip
Merge pull request #1505 from ktaobo/return-breaks-self
Fixed self value in a block is changed with return value
-rw-r--r--src/codegen.c13
-rw-r--r--test/t/proc.rb37
2 files changed, 42 insertions, 8 deletions
diff --git a/src/codegen.c b/src/codegen.c
index 1efc44ba0..cb25c61b6 100644
--- a/src/codegen.c
+++ b/src/codegen.c
@@ -278,16 +278,14 @@ genop_peep(codegen_scope *s, mrb_code i, int val)
s->iseq[s->pc-1] = MKOP_AB(OP_RETURN, GETARG_B(i0), OP_R_NORMAL);
return;
case OP_LOADI:
- s->iseq[s->pc-1] = MKOP_AsBx(OP_LOADI, 0, GETARG_sBx(i0));
- genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
+ genop(s, MKOP_AB(OP_RETURN, GETARG_A(i0), OP_R_NORMAL));
return;
case OP_ARRAY:
case OP_HASH:
case OP_RANGE:
case OP_AREF:
case OP_GETUPVAR:
- s->iseq[s->pc-1] = MKOP_ABC(c0, 0, GETARG_B(i0), GETARG_C(i0));
- genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
+ genop(s, MKOP_AB(OP_RETURN, GETARG_A(i0), OP_R_NORMAL));
return;
case OP_SETIV:
case OP_SETCV:
@@ -308,8 +306,8 @@ genop_peep(codegen_scope *s, mrb_code i, int val)
case OP_GETSPECIAL:
case OP_LOADL:
case OP_STRING:
- s->iseq[s->pc-1] = MKOP_ABx(c0, 0, GETARG_Bx(i0));
- genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
+ s->iseq[s->pc-1] = MKOP_ABx(c0, GETARG_A(i0), GETARG_Bx(i0));
+ genop(s, MKOP_AB(OP_RETURN, GETARG_A(i0), OP_R_NORMAL));
return;
case OP_SCLASS:
s->iseq[s->pc-1] = MKOP_AB(c0, GETARG_A(i), GETARG_B(i0));
@@ -320,8 +318,7 @@ genop_peep(codegen_scope *s, mrb_code i, int val)
case OP_LOADT:
case OP_LOADF:
case OP_OCLASS:
- s->iseq[s->pc-1] = MKOP_A(c0, 0);
- genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
+ genop(s, MKOP_AB(OP_RETURN, GETARG_A(i0), OP_R_NORMAL));
return;
#if 0
case OP_SEND:
diff --git a/test/t/proc.rb b/test/t/proc.rb
index 4227772dd..151e1df86 100644
--- a/test/t/proc.rb
+++ b/test/t/proc.rb
@@ -54,3 +54,40 @@ assert('Proc#call', '15.2.17.4.3') do
assert_equal 1, a
assert_equal 5, a2
end
+
+assert('Proc#return_does_not_break_self') do
+ class TestClass
+ attr_accessor :block
+ def initialize
+ end
+ def return_array
+ @block = Proc.new { self }
+ return []
+ end
+ def return_instance_variable
+ @block = Proc.new { self }
+ return @block
+ end
+ def return_const_fixnum
+ @block = Proc.new { self }
+ return 123
+ end
+ def return_nil
+ @block = Proc.new { self }
+ return nil
+ end
+ end
+
+ c = TestClass.new
+ assert_equal [], c.return_array
+ assert_equal c, c.block.call
+
+ c.return_instance_variable
+ assert_equal c, c.block.call
+
+ assert_equal 123, c.return_const_fixnum
+ assert_equal c, c.block.call
+
+ assert_equal nil, c.return_nil
+ assert_equal c, c.block.call
+end