KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JavaScript object
38  */

39 class ESThunk extends ESBase {
40   static final int OBJ_PROTO_THUNK = 0;
41   static final int OBJ_THUNK = OBJ_PROTO_THUNK + 1;
42   static final int FUN_PROTO_THUNK = OBJ_THUNK + 1;
43   static final int FUN_THUNK = FUN_PROTO_THUNK + 1;
44   static final int ARRAY_PROTO_THUNK = FUN_THUNK + 1;
45   static final int ARRAY_THUNK = ARRAY_PROTO_THUNK + 1;
46   static final int STRING_PROTO_THUNK = ARRAY_THUNK + 1;
47   static final int STRING_THUNK = STRING_PROTO_THUNK + 1;
48   static final int BOOL_PROTO_THUNK = STRING_THUNK + 1;
49   static final int BOOL_THUNK = BOOL_PROTO_THUNK + 1;
50   static final int NUM_PROTO_THUNK = BOOL_THUNK + 1;
51   static final int NUM_THUNK = NUM_PROTO_THUNK + 1;
52   static final int DATE_PROTO_THUNK = NUM_THUNK + 1;
53   static final int DATE_THUNK = DATE_PROTO_THUNK + 1;
54   static final int MATH_THUNK = DATE_THUNK + 1;
55   static final int REGEXP_PROTO_THUNK = MATH_THUNK + 1;
56   static final int REGEXP_THUNK = REGEXP_PROTO_THUNK + 1;
57
58   private int index;
59
60   /**
61    * Simple constructor for parentless objects.
62    */

63   ESThunk(int index)
64   {
65     this.index = index;
66   }
67
68   ESBase getObject()
69   {
70     return getThunk(index);
71   }
72
73   /**
74    * Puts a new value in the property table with the appropriate flags
75    */

76   public ESBase getProperty(ESString name) throws Throwable JavaDoc
77   {
78     ESBase object = getThunk(index);
79
80     return object.getProperty(name);
81   }
82   /**
83    * Puts a new value in the property table with the appropriate flags
84    */

85   public ESBase hasProperty(ESString name) throws Throwable JavaDoc
86   {
87     ESBase object = getThunk(index);
88
89     return object.hasProperty(name);
90   }
91
92   /**
93    * Puts a new value in the property table with the appropriate flags
94    */

95   public void setProperty(ESString name, ESBase value) throws Throwable JavaDoc
96   {
97     ESBase object = getThunk(index);
98
99     object.setProperty(name, value);
100   }
101
102   /**
103    * Deletes the entry. Returns true if successful.
104    */

105   public ESBase delete(ESString name) throws Throwable JavaDoc
106   {
107     ESBase object = getThunk(index);
108
109     return object.delete(name);
110   }
111
112   public Iterator JavaDoc keys() throws Throwable JavaDoc
113   {
114     ESBase object = getThunk(index);
115
116     return object.keys();
117   }
118
119   public ESBase typeof() throws ESException
120   {
121     ESBase object = getThunk(index);
122
123     return object.typeof();
124   }
125
126   /**
127    * XXX: not right
128    */

129   public ESBase toPrimitive(int hint) throws Throwable JavaDoc
130   {
131     ESBase object = getThunk(index);
132
133     return object.toPrimitive(hint);
134   }
135
136   public ESObject toObject() throws ESException
137   {
138     ESBase object = getThunk(index);
139
140     return object.toObject();
141   }
142
143   public Object JavaDoc toJavaObject() throws ESException
144   {
145     ESBase object = getThunk(index);
146
147     return object.toJavaObject();
148   }
149
150   /**
151    * Returns a string rep of the object
152    */

153   public double toNum() throws Throwable JavaDoc
154   {
155     ESBase object = getThunk(index);
156
157     return object.toNum();
158   }
159
160   /**
161    * Returns a string rep of the object
162    */

163   public ESString toStr() throws Throwable JavaDoc
164   {
165     ESBase object = getThunk(index);
166
167     return object.toStr();
168   }
169
170   public ESString toSource(IntMap map, boolean isLoopPass) throws Throwable JavaDoc
171   {
172     ESBase object = getThunk(index);
173
174     return object.toSource(map, isLoopPass);
175   }
176
177   public boolean toBoolean()
178   {
179     ESBase object = getThunk(index);
180
181     return object.toBoolean();
182   }
183   
184   public Object JavaDoc copy(HashMap JavaDoc refs)
185   {
186     return this;
187   }
188
189   public boolean ecmaEquals(ESBase b) throws Throwable JavaDoc
190   {
191     ESBase object = getThunk(index);
192
193     if (this == b)
194       return true;
195     else
196       return object.ecmaEquals(b);
197   }
198
199   public ESBase call(Call call, int length) throws Throwable JavaDoc
200   {
201     ESBase object = getThunk(index);
202
203     return object.call(call, length);
204   }
205
206   public ESBase construct(Call call, int length) throws Throwable JavaDoc
207   {
208     ESBase object = getThunk(index);
209
210     return object.construct(call, length);
211   }
212
213   static ESBase getThunk(int index)
214   {
215     Global resin = Global.getGlobalProto();
216
217     switch (index) {
218     case OBJ_PROTO_THUNK:
219       return resin.objProto;
220
221     case OBJ_THUNK:
222       return resin.object;
223
224     case FUN_PROTO_THUNK:
225       return resin.funProto;
226
227     case FUN_THUNK:
228       return resin.fun;
229
230     case ARRAY_PROTO_THUNK:
231       return resin.arrayProto;
232       
233     case ARRAY_THUNK:
234       return resin.array;
235
236     case STRING_PROTO_THUNK:
237       return resin.stringProto;
238
239     case STRING_THUNK:
240       return resin.string;
241
242     case BOOL_PROTO_THUNK:
243       return resin.boolProto;
244
245     case BOOL_THUNK:
246       return resin.bool;
247
248     case NUM_PROTO_THUNK:
249       return resin.numProto;
250
251     case NUM_THUNK:
252       return resin.num;
253
254     case DATE_PROTO_THUNK:
255       return resin.dateProto;
256
257     case DATE_THUNK:
258       return resin.date;
259
260     case MATH_THUNK:
261       return resin.math;
262
263     case REGEXP_PROTO_THUNK:
264       return resin.getRegexpProto();
265
266     case REGEXP_THUNK:
267       return resin.getRegexp();
268
269     default:
270       throw new RuntimeException JavaDoc();
271     }
272   }
273 }
274
Popular Tags