001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es;
030:
031: import com.caucho.util.IntMap;
032:
033: import java.util.HashMap;
034: import java.util.Iterator;
035:
036: /**
037: * JavaScript object
038: */
039: class ESThunk extends ESBase {
040: static final int OBJ_PROTO_THUNK = 0;
041: static final int OBJ_THUNK = OBJ_PROTO_THUNK + 1;
042: static final int FUN_PROTO_THUNK = OBJ_THUNK + 1;
043: static final int FUN_THUNK = FUN_PROTO_THUNK + 1;
044: static final int ARRAY_PROTO_THUNK = FUN_THUNK + 1;
045: static final int ARRAY_THUNK = ARRAY_PROTO_THUNK + 1;
046: static final int STRING_PROTO_THUNK = ARRAY_THUNK + 1;
047: static final int STRING_THUNK = STRING_PROTO_THUNK + 1;
048: static final int BOOL_PROTO_THUNK = STRING_THUNK + 1;
049: static final int BOOL_THUNK = BOOL_PROTO_THUNK + 1;
050: static final int NUM_PROTO_THUNK = BOOL_THUNK + 1;
051: static final int NUM_THUNK = NUM_PROTO_THUNK + 1;
052: static final int DATE_PROTO_THUNK = NUM_THUNK + 1;
053: static final int DATE_THUNK = DATE_PROTO_THUNK + 1;
054: static final int MATH_THUNK = DATE_THUNK + 1;
055: static final int REGEXP_PROTO_THUNK = MATH_THUNK + 1;
056: static final int REGEXP_THUNK = REGEXP_PROTO_THUNK + 1;
057:
058: private int index;
059:
060: /**
061: * Simple constructor for parentless objects.
062: */
063: ESThunk(int index) {
064: this .index = index;
065: }
066:
067: ESBase getObject() {
068: return getThunk(index);
069: }
070:
071: /**
072: * Puts a new value in the property table with the appropriate flags
073: */
074: public ESBase getProperty(ESString name) throws Throwable {
075: ESBase object = getThunk(index);
076:
077: return object.getProperty(name);
078: }
079:
080: /**
081: * Puts a new value in the property table with the appropriate flags
082: */
083: public ESBase hasProperty(ESString name) throws Throwable {
084: ESBase object = getThunk(index);
085:
086: return object.hasProperty(name);
087: }
088:
089: /**
090: * Puts a new value in the property table with the appropriate flags
091: */
092: public void setProperty(ESString name, ESBase value)
093: throws Throwable {
094: ESBase object = getThunk(index);
095:
096: object.setProperty(name, value);
097: }
098:
099: /**
100: * Deletes the entry. Returns true if successful.
101: */
102: public ESBase delete(ESString name) throws Throwable {
103: ESBase object = getThunk(index);
104:
105: return object.delete(name);
106: }
107:
108: public Iterator keys() throws Throwable {
109: ESBase object = getThunk(index);
110:
111: return object.keys();
112: }
113:
114: public ESBase typeof() throws ESException {
115: ESBase object = getThunk(index);
116:
117: return object.typeof();
118: }
119:
120: /**
121: * XXX: not right
122: */
123: public ESBase toPrimitive(int hint) throws Throwable {
124: ESBase object = getThunk(index);
125:
126: return object.toPrimitive(hint);
127: }
128:
129: public ESObject toObject() throws ESException {
130: ESBase object = getThunk(index);
131:
132: return object.toObject();
133: }
134:
135: public Object toJavaObject() throws ESException {
136: ESBase object = getThunk(index);
137:
138: return object.toJavaObject();
139: }
140:
141: /**
142: * Returns a string rep of the object
143: */
144: public double toNum() throws Throwable {
145: ESBase object = getThunk(index);
146:
147: return object.toNum();
148: }
149:
150: /**
151: * Returns a string rep of the object
152: */
153: public ESString toStr() throws Throwable {
154: ESBase object = getThunk(index);
155:
156: return object.toStr();
157: }
158:
159: public ESString toSource(IntMap map, boolean isLoopPass)
160: throws Throwable {
161: ESBase object = getThunk(index);
162:
163: return object.toSource(map, isLoopPass);
164: }
165:
166: public boolean toBoolean() {
167: ESBase object = getThunk(index);
168:
169: return object.toBoolean();
170: }
171:
172: public Object copy(HashMap refs) {
173: return this ;
174: }
175:
176: public boolean ecmaEquals(ESBase b) throws Throwable {
177: ESBase object = getThunk(index);
178:
179: if (this == b)
180: return true;
181: else
182: return object.ecmaEquals(b);
183: }
184:
185: public ESBase call(Call call, int length) throws Throwable {
186: ESBase object = getThunk(index);
187:
188: return object.call(call, length);
189: }
190:
191: public ESBase construct(Call call, int length) throws Throwable {
192: ESBase object = getThunk(index);
193:
194: return object.construct(call, length);
195: }
196:
197: static ESBase getThunk(int index) {
198: Global resin = Global.getGlobalProto();
199:
200: switch (index) {
201: case OBJ_PROTO_THUNK:
202: return resin.objProto;
203:
204: case OBJ_THUNK:
205: return resin.object;
206:
207: case FUN_PROTO_THUNK:
208: return resin.funProto;
209:
210: case FUN_THUNK:
211: return resin.fun;
212:
213: case ARRAY_PROTO_THUNK:
214: return resin.arrayProto;
215:
216: case ARRAY_THUNK:
217: return resin.array;
218:
219: case STRING_PROTO_THUNK:
220: return resin.stringProto;
221:
222: case STRING_THUNK:
223: return resin.string;
224:
225: case BOOL_PROTO_THUNK:
226: return resin.boolProto;
227:
228: case BOOL_THUNK:
229: return resin.bool;
230:
231: case NUM_PROTO_THUNK:
232: return resin.numProto;
233:
234: case NUM_THUNK:
235: return resin.num;
236:
237: case DATE_PROTO_THUNK:
238: return resin.dateProto;
239:
240: case DATE_THUNK:
241: return resin.date;
242:
243: case MATH_THUNK:
244: return resin.math;
245:
246: case REGEXP_PROTO_THUNK:
247: return resin.getRegexpProto();
248:
249: case REGEXP_THUNK:
250: return resin.getRegexp();
251:
252: default:
253: throw new RuntimeException();
254: }
255: }
256: }
|