Project

General

Profile

Feature #6972 » patch2.diff

Glass_saga (Masaki Matsushita), 09/05/2012 10:07 PM

View differences:

compile.c
1872 1872
		switch (mid) {
1873 1873
		  case idLength: SP_INSN(length); break;
1874 1874
		  case idSize:	 SP_INSN(size);	  break;
1875
		  case idEmptyP: SP_INSN(empty_p);break;
1875 1876
		  case idSucc:	 SP_INSN(succ);	  break;
1876 1877
		  case idNot:	 SP_INSN(not);	  break;
1877 1878
		}
id.c
41 41
    REGISTER_SYMID(idEach, "each");
42 42
    REGISTER_SYMID(idLength, "length");
43 43
    REGISTER_SYMID(idSize, "size");
44
    REGISTER_SYMID(idEmptyP, "empty?");
44 45
    REGISTER_SYMID(idLambda, "lambda");
45 46
    REGISTER_SYMID(idIntern, "intern");
46 47
    REGISTER_SYMID(idGets, "gets");
insns.def
1991 1991

  
1992 1992
/**
1993 1993
  @c optimize
1994
  @e optimized empty?
1995
  @j 最適化された recv.empty?()。
1996
 */
1997
DEFINE_INSN
1998
opt_empty_p
1999
(IC ic)
2000
(VALUE recv)
2001
(VALUE val)
2002
{
2003
    if (!SPECIAL_CONST_P(recv)) {
2004
	if (HEAP_CLASS_OF(recv) == rb_cString &&
2005
	    BASIC_OP_UNREDEFINED_P(BOP_EMPTY_P, STRING_REDEFINED_OP_FLAG)) {
2006
	    if (RSTRING_LEN(recv) == 0) val = Qtrue;
2007
	    else val = Qfalse;
2008
	}
2009
	else if (HEAP_CLASS_OF(recv) == rb_cArray &&
2010
		 BASIC_OP_UNREDEFINED_P(BOP_EMPTY_P, ARRAY_REDEFINED_OP_FLAG)) {
2011
	    if (RARRAY_LEN(recv) == 0) val = Qtrue;
2012
	    else val = Qfalse;
2013
	}
2014
	else if (HEAP_CLASS_OF(recv) == rb_cHash &&
2015
		 BASIC_OP_UNREDEFINED_P(BOP_EMPTY_P, HASH_REDEFINED_OP_FLAG)) {
2016
	    if (RHASH_EMPTY_P(recv)) val = Qtrue;
2017
	    else val = Qfalse;
2018
	}
2019
	else {
2020
	    goto INSN_LABEL(normal_dispatch);
2021
	}
2022
    }
2023
    else {
2024
      INSN_LABEL(normal_dispatch):
2025
	PUSH(recv);
2026
	CALL_SIMPLE_METHOD(0, idEmptyP, recv);
2027
    }
2028
}
2029

  
2030
/**
2031
  @c optimize
1994 2032
  @e optimized succ
1995 2033
  @j 最適化された recv.succ()。
1996 2034
 */
template/id.h.tmpl
20 20
  MethodMissing
21 21
  Length
22 22
  Size
23
  EmptyP
23 24
  Gets
24 25
  Succ
25 26
  Each
test/ruby/test_optimization.rb
85 85
    assert_equal 6, "string".length
86 86
  end
87 87

  
88
  def test_string_empty?
89
    assert_equal true, "".empty?
90
    assert_equal false, "string".empty?
91
    assert_nil redefine_method('String', 'empty?') { "string".empty? }
92
    assert_equal true, "".empty?
93
    assert_equal false, "string".empty?
94
  end
95

  
88 96
  def test_string_plus
89 97
    assert_equal "", "" + ""
90 98
    assert_equal "x", "x" + ""
......
116 124
    assert_equal 3, [1,2,3].length
117 125
  end
118 126

  
127
  def test_array_empty?
128
    assert_equal true, [].empty?
129
    assert_equal false, [1,2,3].empty?
130
  end
131

  
119 132
  def test_hash_length
120 133
    assert_equal 0, {}.length
121 134
    assert_equal 1, {1=>1}.length
122 135
  end
123 136

  
137
  def test_hash_empty?
138
    assert_equal true, {}.empty?
139
    assert_equal false, {1=>1}.empty?
140
  end
141

  
124 142
  class MyObj
125 143
    def ==(other)
126 144
      true
vm.c
1005 1005
    OP(ASET, ASET), (C(Array), C(Hash));
1006 1006
    OP(Length, LENGTH), (C(Array), C(String), C(Hash));
1007 1007
    OP(Size, SIZE), (C(Array), C(String), C(Hash));
1008
    OP(EmptyP, EMPTY_P), (C(Array), C(String), C(Hash));
1008 1009
    OP(Succ, SUCC), (C(Fixnum), C(String), C(Time));
1009 1010
#undef C
1010 1011
#undef OP
vm_insnhelper.h
49 49
  BOP_ASET,
50 50
  BOP_LENGTH,
51 51
  BOP_SIZE,
52
  BOP_EMPTY_P,
52 53
  BOP_SUCC,
53 54
  BOP_GT,
54 55
  BOP_GE,