1 28 29 package com.caucho.es; 30 31 import com.caucho.util.CharBuffer; 32 import com.caucho.util.IntMap; 33 34 import java.util.ArrayList ; 35 import java.util.Iterator ; 36 37 40 class ESArray extends ESObject { 41 static ESId LENGTH = ESId.intern("length"); 42 43 46 ESArray() 47 { 48 super("Array", null); 49 put(LENGTH, ESNumber.create(0), DONT_ENUM|DONT_DELETE); 50 } 51 52 public void setProperty(int i, ESBase value) throws Throwable 53 { 54 super.setProperty(ESString.create(i), value); 55 56 int length = getProperty(LENGTH).toInt32(); 57 if (i >= length) 58 super.setProperty(LENGTH, ESNumber.create(i + 1)); 59 } 60 61 public void setProperty(ESString key, ESBase value) throws Throwable 62 { 63 if (key.equals(LENGTH)) { 64 int oldLength; 65 int newLength; 66 67 try { 68 oldLength = getProperty(LENGTH).toInt32(); 69 newLength = value.toInt32(); 70 } catch (ESException e) { 71 return; 72 } 73 74 for (int i = newLength; i < oldLength; i++) { 75 try { 76 delete(ESString.create(i)); 77 } catch (Exception e) { 78 } 79 } 80 81 if (newLength < 0) 82 newLength = 0; 83 84 super.setProperty(LENGTH, ESNumber.create(newLength)); 85 return; 86 } 87 88 super.setProperty(key, value); 89 90 try { 91 int keyValue = Integer.parseInt(key.toString()); 92 int length = getProperty(LENGTH).toInt32(); 93 94 if (keyValue >= length) 95 super.setProperty(LENGTH, ESNumber.create(keyValue + 1)); 96 } catch (Exception e) { 97 } 98 } 99 100 static ESString arrayToSource(ESObject obj, IntMap map, boolean isLoopPass) 101 throws Throwable 102 { 103 Global resin = Global.getGlobalProto(); 104 CharBuffer cb = new CharBuffer(); 105 106 int mark = map.get(obj); 107 108 if (mark > 0 && isLoopPass) 109 return null; 110 else if (mark > 0) { 111 cb.append("#" + mark + "="); 112 map.put(obj, -mark); 113 } else if (mark == 0 && isLoopPass) { 114 map.put(obj, resin.addMark()); 115 return null; 116 } else if (mark < 0 && ! isLoopPass) { 117 return ESString.create("#" + -mark + "#"); 118 } 119 120 if (isLoopPass) 121 map.put(obj, 0); 122 123 int len = obj.getProperty(LENGTH).toInt32(); 124 125 boolean noValue = true; 126 cb.append("["); 127 for (int i = 0; i < len; i++) { 128 if (i != 0) 129 cb.append(", "); 130 131 ESBase value = obj.hasProperty(i); 132 133 if (value != null && value != esNull && 134 value != esUndefined && value != esEmpty) { 135 if (isLoopPass) 136 value.toSource(map, isLoopPass); 137 else 138 cb.append(value.toSource(map, isLoopPass)); 139 } 140 else if (i + 1 == len) 141 cb.append(","); 142 } 143 cb.append("]"); 144 145 return ESString.create(cb.toString()); 146 } 147 148 public Iterator keys() throws ESException 149 { 150 ArrayList values = new ArrayList (); 151 152 try { 153 int len = getProperty(LENGTH).toInt32(); 154 for (int i = 0; i < len; i++) { 155 Object prop = getProperty(i); 156 values.add(String.valueOf(i)); 158 } 159 } catch (Throwable e) { 160 throw ESWrapperException.create(e); 161 } 162 163 return values.iterator(); 164 } 165 166 public ESString toSource(IntMap map, boolean isLoopPass) throws Throwable 167 { 168 return arrayToSource(this, map, isLoopPass); 169 } 170 171 public ESObject dup() 172 { 173 return new ESArray(false); 174 } 175 176 protected ESArray(boolean dummy) {} 177 } 178 | Popular Tags |