1 28 29 package com.caucho.es; 30 31 import com.caucho.util.IntMap; 32 33 import java.util.Iterator ; 34 35 38 class ESArrayWrap extends ESJavaWrapper { 39 static ESId LENGTH = ESId.intern("length"); 40 41 protected ESArrayWrap() {} 42 43 protected int length() { return 0; } 44 public ESBase getProperty(int i) throws Throwable { return esEmpty; } 45 public ESBase delete(int i) throws ESException { return ESBoolean.FALSE; } 46 public void setProperty(int i, ESBase value) throws Throwable { } 47 48 public ESBase getProperty(ESString name) throws Throwable 49 { 50 if (name.equals(LENGTH)) 51 return ESNumber.create(length()); 52 53 try { double value = name.toNum(); 55 int iValue = (int) value; 56 57 if (iValue == value) 58 return getProperty(iValue); 59 60 return super.getProperty(name); 61 } catch (Exception e) { 62 return super.getProperty(name); 63 } 64 } 65 66 public void setProperty(ESString name, ESBase value) throws Throwable 67 { 68 if (name.equals(LENGTH)) 69 return; 70 71 try { double dIndex = name.toNum(); 73 int index = (int) dIndex; 74 75 if (index == dIndex) 76 setProperty(index, value); 77 else 78 super.setProperty(name, value); 79 } catch (Exception e) { 80 super.setProperty(name, value); 81 } 82 } 83 84 public ESBase delete(ESString name) throws Throwable 85 { 86 if (name.equals(LENGTH)) 87 return ESBoolean.FALSE; 88 89 try { 90 double dIndex = name.toNum(); 91 int index = (int) dIndex; 92 93 if (index == dIndex) 94 return delete(index); 95 else 96 return super.delete(name); 97 } catch (Exception e) { 98 return super.delete(name); 99 } 100 } 101 102 class ArrayIterator implements Iterator { 103 int length; 104 int i; 105 106 public boolean hasNext() { return i < length; } 107 public Object next() 108 { 109 try { 110 return i < length ? getProperty(i++) : ESBase.esNull; 111 } catch (Throwable e) { 112 return ESBase.esNull; 113 } 114 } 115 116 public void remove() { } 117 118 ArrayIterator() 119 { 120 length = length(); 121 } 122 } 123 124 public Iterator keys() throws ESException 125 { 126 return new ArrayIterator(); 127 } 128 129 public ESString toStr() throws Throwable 130 { 131 return (ESString) NativeArray.toString(this); 132 } 133 134 public ESString toSource(IntMap map, boolean isLoopPass) throws Throwable 135 { 136 return ESArray.arrayToSource(this, map, isLoopPass); 137 } 138 139 protected ESArrayWrap(ESBase proto, Object value) 140 { 141 super(proto, value); 142 } 143 } 144 | Popular Tags |