1 28 29 package com.caucho.es; 30 31 import com.caucho.util.IntMap; 32 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 36 39 public class ESBase { 40 private static ESFactory factory; 41 42 String className; 43 ESBase prototype; 44 45 public static ESBase esBase; 46 public static ESBase esNull; 47 public static ESBase esUndefined; 48 public static ESBase esEmpty; 49 50 final static int NONE = 0; 51 final static int STRING = 1; 52 final static int NUMBER = 2; 53 54 public static final int READ_ONLY = 0x1; 55 public static final int DONT_DELETE = 0x2; 56 public static final int DONT_ENUM = 0x4; 57 static final int WATCH = 0x8; 58 59 static void init(ESFactory factory) 60 { 61 if (esBase != null) 62 return; 63 64 esBase = new ESBase(null); 65 esBase.prototype = esBase; 66 esBase.className = "base"; 67 68 ESBase.factory = factory; 69 70 if (factory != null) { 71 esNull = factory.createNull(); 72 73 esUndefined = factory.createUndefined(); 74 esEmpty = factory.createUndefined(); 75 } else { 76 esNull = new ESNull(); 77 78 esUndefined = new ESUndefined(); 79 esEmpty = new ESUndefined(); 80 } 81 } 82 83 86 protected ESBase() 87 { 88 } 89 90 93 ESBase(ESBase prototype) 94 { 95 if (prototype == null) 96 prototype = esBase; 97 98 this.prototype = prototype; 99 } 100 101 public ESBase typeof() throws ESException 102 { 103 throw new ESException("no typeof"); 104 } 105 106 public Class getJavaType() 107 { 108 return void.class; 109 } 110 111 public ESBase getProperty(ESString key) throws Throwable 112 { 113 return esEmpty; 114 } 115 116 boolean canPut(ESString name) 117 { 118 return true; 119 } 120 121 124 public void setProperty(ESString key, ESBase value) throws Throwable 125 { 126 } 127 128 public ESBase delete(ESString key) throws Throwable 129 { 130 return ESBoolean.TRUE; 131 } 132 133 public ESBase toPrimitive(int type) throws Throwable 134 { 135 return this; 136 } 137 138 public ESBase toPrimitive() throws Throwable 139 { 140 return toPrimitive(NONE); 141 } 142 143 public boolean isBoolean() 144 { 145 return false; 146 } 147 148 public boolean toBoolean() 149 { 150 return false; 151 } 152 153 public boolean isNum() 154 { 155 return false; 156 } 157 158 public double toNum() throws Throwable 159 { 160 throw new ESException("no number: " + getClass().getName()); 161 } 162 163 public boolean isString() 164 { 165 return false; 166 } 167 168 public ESString toStr() throws Throwable 169 { 170 throw new ESException("no string: " + getClass().getName()); 171 } 172 173 public ESBase valueOf() throws Throwable 174 { 175 return toPrimitive(NONE); 176 } 177 178 public ESString toSource(IntMap map, boolean isLoopPass) throws Throwable 179 { 180 if (isLoopPass) 181 return null; 182 183 return toStr(); 184 } 185 186 public ESObject toObject() throws ESException 187 { 188 throw new ESNullException(className + " has no properties"); 189 } 190 191 public Object toJavaObject() throws ESException 192 { 193 return null; 194 } 195 196 Object copy(HashMap refs) 197 { 198 return this; 199 } 200 201 public ESBase call(Call eval, int length) throws Throwable 202 { 203 throw new ESNullException(toStr() + " is not a function"); 204 } 205 206 public ESBase call(Call eval, int length, ESString key) throws Throwable 207 { 208 ESBase call = hasProperty(key); 209 210 if (call != null) { 211 eval.callee = call; 212 return call.call(eval, length); 213 } 214 215 if (prototype != null && prototype != this) 216 return prototype.call(eval, length, key); 217 else 218 throw new ESUndefinedException("undefined call `" + key + "'"); 219 } 220 221 public ESBase construct(Call eval, int length) throws Throwable 222 { 223 throw new ESNullException(toStr() + " is not a constructor"); 224 } 225 226 public Iterator keys() throws Throwable 227 { 228 return toObject().keys(); 229 } 230 231 233 String getClassName() 234 { 235 return className; 236 } 237 238 public ESBase hasProperty(ESString key) throws Throwable 239 { 240 ESBase value = getProperty(key); 241 242 return value == esEmpty ? null : value; 243 } 244 245 ESBase hasProperty(int i) throws Throwable 246 { 247 return hasProperty(ESString.create(i)); 248 } 249 250 253 public ESBase getProperty(String key) throws Throwable 254 { 255 return getProperty(ESString.create(key)); 256 } 257 258 261 ESBase getProperty(int i) throws Throwable 262 { 263 return getProperty(ESString.create(i)); 264 } 265 266 public void setProperty(String key, ESBase value) throws Throwable 267 { 268 setProperty(ESString.create(key), value); 269 } 270 271 274 public void setProperty(int i, ESBase value) throws Throwable 275 { 276 setProperty(ESString.create(i), value); 277 } 278 279 ESBase delete(String key) throws Throwable 280 { 281 return delete(ESString.create(key)); 282 } 283 284 ESBase delete(int i) throws Throwable 285 { 286 return delete(ESString.create(i)); 287 } 288 289 public ESBase plus(ESBase b) throws Throwable 290 { 291 ESBase primA = toPrimitive(NONE); 292 ESBase primB = b.toPrimitive(NONE); 293 294 if (primA instanceof ESString || primB instanceof ESString) { 295 return ESString.create(primA.toStr().toString() + 296 primB.toStr().toString()); 297 } 298 else { 299 return ESNumber.create(primA.toNum() + primB.toNum()); 300 } 301 } 302 303 public boolean lessThan(ESBase ob, boolean neg) throws Throwable 304 { 305 ESBase a = toPrimitive(NONE); 306 ESBase b = ob.toPrimitive(NONE); 307 308 if (a instanceof ESString && b instanceof ESString) { 309 return ((((ESString) a).compareTo((ESString) b) < 0) != neg); 310 } else { 311 double da = a.toNum(); 312 double db = b.toNum(); 313 314 if (Double.isNaN(da) || Double.isNaN(db)) 315 return false; 316 else 317 return (da < db) != neg; 318 } 319 } 320 321 public boolean greaterThan(ESBase ob, boolean neg) throws Throwable 322 { 323 return ob.lessThan(this, neg); 324 } 325 326 public int toInt32() throws Throwable 327 { 328 double value = toNum(); 329 330 if (Double.isInfinite(value)) 331 return 0; 332 else 333 return (int) ((long) value & 0xffffffffL); 334 } 335 336 public String toString() 337 { 338 try { 339 return toStr().toString(); 340 } catch (Throwable e) { 341 System.out.println("Exception: " + e); 342 e.printStackTrace(); 343 return ""; 344 } 345 } 346 347 public String toJavaString() throws Throwable 348 { 349 return toStr().toString(); 350 } 351 352 public boolean ecmaEquals(ESBase b) throws Throwable 353 { 354 return this == b; 355 } 356 } 357 | Popular Tags |