1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
ko1 2007-11-27 04:43:54 +00:00
parent 8e09ab045f
commit b0986a8f82
5 changed files with 46 additions and 10 deletions

View file

@ -1,3 +1,13 @@
Tue Nov 27 12:47:23 2007 Koichi Sasada <ko1@atdot.net>
* 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@atdot.net>
* insns.def: fix typo.

View file

@ -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)]

View file

@ -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:

View file

@ -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;
}

View file

@ -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