[前][次][番号順一覧][スレッド一覧]

ruby-changes:13012

From: nobu <ko1@a...>
Date: Sat, 5 Sep 2009 10:39:35 +0900 (JST)
Subject: [ruby-changes:13012] Ruby:r24757 (trunk): * compile.c (iseq_compile_each): &&= and ||= should return rhs.

nobu	2009-09-05 10:38:47 +0900 (Sat, 05 Sep 2009)

  New Revision: 24757

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24757

  Log:
    * compile.c (iseq_compile_each): &&= and ||= should return rhs.
      [ruby-dev:39163] (#1996), [ruby-core:25143]

  Modified files:
    trunk/ChangeLog
    trunk/compile.c
    trunk/test/ruby/test_basicinstructions.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 24756)
+++ ChangeLog	(revision 24757)
@@ -1,3 +1,8 @@
+Sat Sep  5 10:38:46 2009  Nobuyoshi Nakada  <nobu@r...>
+
+	* compile.c (iseq_compile_each): &&= and ||= should return rhs.
+	  [ruby-dev:39163] (#1996), [ruby-core:25143]
+
 Sat Sep  5 08:51:43 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* re.c (update_char_offset): position should be long.
Index: compile.c
===================================================================
--- compile.c	(revision 24756)
+++ compile.c	(revision 24757)
@@ -3842,22 +3842,26 @@
 	  if lcfin  # r o
 	  pop       # r
 	  eval v    # r v
-	  send a=   # v
-	  jump lfin # v
+	  swap      # v r
+	  topn 1    # v r v
+	  send a=   # v ?
+	  jump lfin # v ?
 
 	  lcfin:      # r o
 	  swap      # o r
+
+	  lfin:       # o ?
 	  pop       # o
 
-	  lfin:       # v
-
 	  # and
 	  dup       # r o o
 	  unless lcfin
 	  pop       # r
 	  eval v    # r v
-	  send a=   # v
-	  jump lfin # v
+	  swap      # v r
+	  topn 1    # v r v
+	  send a=   # v ?
+	  jump lfin # v ?
 
 	  # others
 	  eval v    # r o v
@@ -3881,26 +3885,32 @@
 	    }
 	    ADD_INSN(ret, nd_line(node), pop);
 	    COMPILE(ret, "NODE_OP_ASGN2 val", node->nd_value);
+	    ADD_INSN(ret, nd_line(node), swap);
+	    ADD_INSN1(ret, nd_line(node), topn, INT2FIX(1));
 	    ADD_SEND(ret, nd_line(node), ID2SYM(node->nd_next->nd_aid),
 		     INT2FIX(1));
 	    ADD_INSNL(ret, nd_line(node), jump, lfin);
 
 	    ADD_LABEL(ret, lcfin);
 	    ADD_INSN(ret, nd_line(node), swap);
-	    ADD_INSN(ret, nd_line(node), pop);
 
 	    ADD_LABEL(ret, lfin);
+	    ADD_INSN(ret, nd_line(node), pop);
+	    if (poped) {
+		/* we can apply more optimize */
+		ADD_INSN(ret, nd_line(node), pop);
+	    }
 	}
 	else {
 	    COMPILE(ret, "NODE_OP_ASGN2 val", node->nd_value);
 	    ADD_SEND(ret, nd_line(node), ID2SYM(node->nd_next->nd_mid),
 		     INT2FIX(1));
+	    if (!poped) {
+		ADD_INSN(ret, nd_line(node), swap);
+		ADD_INSN1(ret, nd_line(node), topn, INT2FIX(1));
+	    }
 	    ADD_SEND(ret, nd_line(node), ID2SYM(node->nd_next->nd_aid),
 		     INT2FIX(1));
-	}
-
-	if (poped) {
-	    /* we can apply more optimize */
 	    ADD_INSN(ret, nd_line(node), pop);
 	}
 	break;
Index: test/ruby/test_basicinstructions.rb
===================================================================
--- test/ruby/test_basicinstructions.rb	(revision 24756)
+++ test/ruby/test_basicinstructions.rb	(revision 24757)
@@ -499,53 +499,74 @@
   end
 
   class OP
-    attr_accessor :x
+    attr_reader :x
+    def x=(x)
+      @x = x
+      :Bug1996
+    end
+    Bug1996 = '[ruby-dev:39163], [ruby-core:25143]'
   end
 
   def test_opassign
     x = nil
-    x ||= 1
+    assert_equal 1, x ||= 1
     assert_equal 1, x
-    x &&= 2
+    assert_equal 2, x &&= 2
     assert_equal 2, x
-    x ||= 3
+    assert_equal 2, x ||= 3
     assert_equal 2, x
-    x &&= 4
+    assert_equal 4, x &&= 4
     assert_equal 4, x
+    assert_equal 5, x += 1
+    assert_equal 5, x
+    assert_equal 4, x -= 1
+    assert_equal 4, x
 
     y = OP.new
     y.x = nil
-    y.x ||= 1
+    assert_equal 1, y.x ||= 1, OP::Bug1996
     assert_equal 1, y.x
-    y.x &&= 2
+    assert_equal 2, y.x &&= 2, OP::Bug1996
     assert_equal 2, y.x
-    y.x ||= 3
+    assert_equal 2, y.x ||= 3
     assert_equal 2, y.x
-    y.x &&= 4
+    assert_equal 4, y.x &&= 4, OP::Bug1996
     assert_equal 4, y.x
+    assert_equal 5, y.x += 1, OP::Bug1996
+    assert_equal 5, y.x
+    assert_equal 4, y.x -= 1, OP::Bug1996
+    assert_equal 4, y.x
 
     z = OP.new
     z.x = y
     z.x.x = nil
-    z.x.x ||= 1
+    assert_equal 1, z.x.x ||= 1, OP::Bug1996
     assert_equal 1, z.x.x
-    z.x.x &&= 2
+    assert_equal 2, z.x.x &&= 2, OP::Bug1996
     assert_equal 2, z.x.x
-    z.x.x ||= 3
+    assert_equal 2, z.x.x ||= 3
     assert_equal 2, z.x.x
-    z.x.x &&= 4
+    assert_equal 4, z.x.x &&= 4, OP::Bug1996
     assert_equal 4, z.x.x
+    assert_equal 5, z.x.x += 1, OP::Bug1996
+    assert_equal 5, z.x.x
+    assert_equal 4, z.x.x -= 1, OP::Bug1996
+    assert_equal 4, z.x.x
 
     a = []
     a[0] = nil
-    a[0] ||= 1
+    assert_equal 1, a[0] ||= 1
     assert_equal 1, a[0]
-    a[0] &&= 2
+    assert_equal 2, a[0] &&= 2
     assert_equal 2, a[0]
-    a[0] ||= 3
+    assert_equal 2, a[0] ||= 3
     assert_equal 2, a[0]
-    a[0] &&= 4
+    assert_equal 4, a[0] &&= 4
     assert_equal 4, a[0]
+    assert_equal 5, a[0] += 1
+    assert_equal 5, a[0]
+    assert_equal 4, a[0] -= 1
+    assert_equal 4, a[0]
   end
 
   def test_backref

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]