From b0986a8f827cc97d7cf19b15c38fbdab794e34b9 Mon Sep 17 00:00:00 2001 From: ko1 Date: Tue, 27 Nov 2007 04:43:54 +0000 Subject: * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14031 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 ++++++++++ bootstraptest/test_syntax.rb | 2 +- compile.c | 4 +++- insns.def | 11 +++-------- test/ruby/test_defined.rb | 29 +++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3ad79e2891..94910dd5e4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Tue Nov 27 12:47:23 2007 Koichi Sasada + + * 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 * insns.def: fix typo. diff --git a/bootstraptest/test_syntax.rb b/bootstraptest/test_syntax.rb index 9541d67089..b1517e697b 100644 --- a/bootstraptest/test_syntax.rb +++ b/bootstraptest/test_syntax.rb @@ -248,7 +248,7 @@ assert_equal %q{["method", "method", "method", "method", nil, nil, "method", "me 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)] diff --git a/compile.c b/compile.c index 9a096ccf61..2c27d60d6e 100644 --- a/compile.c +++ b/compile.c @@ -2333,10 +2333,12 @@ defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *ret, 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: diff --git a/insns.def b/insns.def index 4e7ba8b352..489a90da09 100644 --- a/insns.def +++ b/insns.def @@ -795,8 +795,6 @@ defined { VALUE klass; char *expr_type = 0; - char buf[0x10]; - val = Qnil; switch (type) { @@ -868,12 +866,9 @@ defined 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; } diff --git a/test/ruby/test_defined.rb b/test/ruby/test_defined.rb index 84b9bf7b9e..bfcd7fb667 100644 --- a/test/ruby/test_defined.rb +++ b/test/ruby/test_defined.rb @@ -48,5 +48,34 @@ class TestDefined < Test::Unit::TestCase 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 -- cgit v1.2.3