KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > ESBase


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es;
30
31 import com.caucho.util.IntMap;
32
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 /**
37  * Implementation class for the base of the JavaScript object hierarchy.
38  */

39 public class ESBase {
40   private static ESFactory factory;
41
42   String JavaDoc 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   /**
84    * Create a new object based on a prototype
85    */

86   protected ESBase()
87   {
88   }
89
90   /**
91    * Create a new object based on a prototype
92    */

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 JavaDoc getJavaType()
107   {
108     return void.class;
109   }
110
111   public ESBase getProperty(ESString key) throws Throwable JavaDoc
112   {
113     return esEmpty;
114   }
115
116   boolean canPut(ESString name)
117   {
118     return true;
119   }
120
121   /**
122    * Sets the named property
123    */

124   public void setProperty(ESString key, ESBase value) throws Throwable JavaDoc
125   {
126   }
127
128   public ESBase delete(ESString key) throws Throwable JavaDoc
129   {
130     return ESBoolean.TRUE;
131   }
132
133   public ESBase toPrimitive(int type) throws Throwable JavaDoc
134   {
135     return this;
136   }
137
138   public ESBase toPrimitive() throws Throwable JavaDoc
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 JavaDoc
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 JavaDoc
169   {
170     throw new ESException("no string: " + getClass().getName());
171   }
172
173   public ESBase valueOf() throws Throwable JavaDoc
174   {
175     return toPrimitive(NONE);
176   }
177
178   public ESString toSource(IntMap map, boolean isLoopPass) throws Throwable JavaDoc
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 JavaDoc toJavaObject() throws ESException
192   {
193     return null;
194   }
195
196   Object JavaDoc copy(HashMap JavaDoc refs)
197   {
198     return this;
199   }
200
201   public ESBase call(Call eval, int length) throws Throwable JavaDoc
202   {
203     throw new ESNullException(toStr() + " is not a function");
204   }
205
206   public ESBase call(Call eval, int length, ESString key) throws Throwable JavaDoc
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 JavaDoc
222   {
223     throw new ESNullException(toStr() + " is not a constructor");
224   }
225
226   public Iterator JavaDoc keys() throws Throwable JavaDoc
227   {
228     return toObject().keys();
229   }
230
231   // useful junk
232

233   String JavaDoc getClassName()
234   {
235     return className;
236   }
237
238   public ESBase hasProperty(ESString key) throws Throwable JavaDoc
239   {
240     ESBase value = getProperty(key);
241
242     return value == esEmpty ? null : value;
243   }
244       
245   ESBase hasProperty(int i) throws Throwable JavaDoc
246   {
247     return hasProperty(ESString.create(i));
248   }
249
250   /**
251    * Returns the text object for the lexeme.
252    */

253   public ESBase getProperty(String JavaDoc key) throws Throwable JavaDoc
254   {
255     return getProperty(ESString.create(key));
256   }
257
258   /**
259    * Returns the text object for the lexeme.
260    */

261   ESBase getProperty(int i) throws Throwable JavaDoc
262   {
263     return getProperty(ESString.create(i));
264   }
265
266   public void setProperty(String JavaDoc key, ESBase value) throws Throwable JavaDoc
267   {
268     setProperty(ESString.create(key), value);
269   }
270
271   /**
272    * Sets the named property
273    */

274   public void setProperty(int i, ESBase value) throws Throwable JavaDoc
275   {
276     setProperty(ESString.create(i), value);
277   }
278
279   ESBase delete(String JavaDoc key) throws Throwable JavaDoc
280   {
281     return delete(ESString.create(key));
282   }
283
284   ESBase delete(int i) throws Throwable JavaDoc
285   {
286     return delete(ESString.create(i));
287   }
288
289   public ESBase plus(ESBase b) throws Throwable JavaDoc
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 JavaDoc
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 JavaDoc
322   {
323     return ob.lessThan(this, neg);
324   }
325   
326   public int toInt32() throws Throwable JavaDoc
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 JavaDoc toString()
337   {
338     try {
339       return toStr().toString();
340     } catch (Throwable JavaDoc e) {
341       System.out.println("Exception: " + e);
342       e.printStackTrace();
343       return "";
344     }
345   }
346
347   public String JavaDoc toJavaString() throws Throwable JavaDoc
348   {
349     return toStr().toString();
350   }
351
352   public boolean ecmaEquals(ESBase b) throws Throwable JavaDoc
353   {
354     return this == b;
355   }
356 }
357
Popular Tags