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

ruby-changes:2540

From: ko1@a...
Date: 27 Nov 2007 13:44:09 +0900
Subject: [ruby-changes:2540] ko1 - Ruby:r14031 (trunk): * compile.c, insns.def: change return value of "defined?"

ko1	2007-11-27 13:43:54 +0900 (Tue, 27 Nov 2007)

  New Revision: 14031

  Modified files:
    trunk/ChangeLog
    trunk/bootstraptest/test_syntax.rb
    trunk/compile.c
    trunk/insns.def
    trunk/test/ruby/test_defined.rb

  Log:
    * compile.c, insns.def: change return value of "defined?"
      for $&, $1, ... .  If such variables are defined,
      return "global-variable".
    * test/ruby/test_defined.rb: add tests.
    * bootstraptest/test_syntax.rb: fix a test.
    


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/compile.c?r1=14031&r2=14030
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=14031&r2=14030
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/bootstraptest/test_syntax.rb?r1=14031&r2=14030
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_defined.rb?r1=14031&r2=14030
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/insns.def?r1=14031&r2=14030

Index: insns.def
===================================================================
--- insns.def	(revision 14030)
+++ insns.def	(revision 14031)
@@ -795,8 +795,6 @@
 {
     VALUE klass;
     char *expr_type = 0;
-    char buf[0x10];
-    
     val = Qnil;
 
     switch (type) {
@@ -868,12 +866,9 @@
 	  break;
       }
       case DEFINED_REF:{
-	  int nth = FIX2INT(obj);
-	  VALUE backref = lfp_svar_get(th, GET_LFP(), 1);
-	  
-	  if (rb_reg_nth_match(nth, backref) != Qnil) {
-	      snprintf(buf, 0x10, "$%d", nth);
-	      expr_type = buf;
+	  val = vm_getspecial(th, GET_LFP(), Qfalse, FIX2INT(obj));
+	  if (val != Qnil) {
+	      expr_type = "global-variable";
 	  }
 	  break;
       }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 14030)
+++ ChangeLog	(revision 14031)
@@ -1,3 +1,13 @@
+Tue Nov 27 12:47:23 2007  Koichi Sasada  <ko1@a...>
+
+	* compile.c, insns.def: change return value of "defined?"
+	  for $&, $1, ... .  If such variables are defined,
+	  return "global-variable".
+
+	* test/ruby/test_defined.rb: add tests.
+
+	* bootstraptest/test_syntax.rb: fix a test.
+
 Tue Nov 27 11:54:46 2007  Koichi Sasada  <ko1@a...>
 
 	* insns.def: fix typo.
Index: bootstraptest/test_syntax.rb
===================================================================
--- bootstraptest/test_syntax.rb	(revision 14030)
+++ bootstraptest/test_syntax.rb	(revision 14031)
@@ -248,7 +248,7 @@
   end
   C.new.test + [defined?(C.new.m3)]
 }
-assert_equal %q{[nil, nil, nil, nil, "$1", "$2", nil, nil]}, %q{
+assert_equal %q{[nil, nil, nil, nil, "global-variable", "global-variable", nil, nil]}, %q{
   $ans = [defined?($1), defined?($2), defined?($3), defined?($4)]
   /(a)(b)/ =~ 'ab'
   $ans + [defined?($1), defined?($2), defined?($3), defined?($4)]
Index: compile.c
===================================================================
--- compile.c	(revision 14030)
+++ compile.c	(revision 14031)
@@ -2333,10 +2333,12 @@
 		  needstr);
 	return 1;
 
+      case NODE_BACK_REF:
       case NODE_NTH_REF:
 	ADD_INSN(ret, nd_line(node), putnil);
 	ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_REF),
-		  INT2FIX(node->nd_nth), needstr);
+		  INT2FIX(node->nd_nth << 1 | type == NODE_BACK_REF),
+		  needstr);
 	return 1;
 
       case NODE_ZSUPER:
Index: test/ruby/test_defined.rb
===================================================================
--- test/ruby/test_defined.rb	(revision 14030)
+++ test/ruby/test_defined.rb	(revision 14031)
@@ -48,5 +48,34 @@
 
     assert(defined_test)		# not iterator
     assert(!defined_test{})	        # called as iterator
+
+    /a/ =~ ''
+    assert_equal nil, defined?($&)
+    assert_equal nil, defined?($`)
+    assert_equal nil, defined?($')
+    assert_equal nil, defined?($+)
+    assert_equal nil, defined?($1)
+    assert_equal nil, defined?($2)
+    /a/ =~ 'a'
+    assert_equal 'global-variable', defined?($&)
+    assert_equal 'global-variable', defined?($`)
+    assert_equal 'global-variable', defined?($')
+    assert_equal nil, defined?($+)
+    assert_equal nil, defined?($1)
+    assert_equal nil, defined?($2)
+    /(a)/ =~ 'a'
+    assert_equal 'global-variable', defined?($&)
+    assert_equal 'global-variable', defined?($`)
+    assert_equal 'global-variable', defined?($')
+    assert_equal 'global-variable', defined?($+)
+    assert_equal 'global-variable', defined?($1)
+    assert_equal nil, defined?($2)
+    /(a)b/ =~ 'ab'
+    assert_equal 'global-variable', defined?($&)
+    assert_equal 'global-variable', defined?($`)
+    assert_equal 'global-variable', defined?($')
+    assert_equal 'global-variable', defined?($+)
+    assert_equal 'global-variable', defined?($1)
+    assert_equal nil, defined?($2)
   end
 end

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

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