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: * Implementation class for the base of the JavaScript object hierarchy.
038: */
039: public class ESBase {
040: private static ESFactory factory;
041:
042: String className;
043: ESBase prototype;
044:
045: public static ESBase esBase;
046: public static ESBase esNull;
047: public static ESBase esUndefined;
048: public static ESBase esEmpty;
049:
050: final static int NONE = 0;
051: final static int STRING = 1;
052: final static int NUMBER = 2;
053:
054: public static final int READ_ONLY = 0x1;
055: public static final int DONT_DELETE = 0x2;
056: public static final int DONT_ENUM = 0x4;
057: static final int WATCH = 0x8;
058:
059: static void init(ESFactory factory) {
060: if (esBase != null)
061: return;
062:
063: esBase = new ESBase(null);
064: esBase.prototype = esBase;
065: esBase.className = "base";
066:
067: ESBase.factory = factory;
068:
069: if (factory != null) {
070: esNull = factory.createNull();
071:
072: esUndefined = factory.createUndefined();
073: esEmpty = factory.createUndefined();
074: } else {
075: esNull = new ESNull();
076:
077: esUndefined = new ESUndefined();
078: esEmpty = new ESUndefined();
079: }
080: }
081:
082: /**
083: * Create a new object based on a prototype
084: */
085: protected ESBase() {
086: }
087:
088: /**
089: * Create a new object based on a prototype
090: */
091: ESBase(ESBase prototype) {
092: if (prototype == null)
093: prototype = esBase;
094:
095: this .prototype = prototype;
096: }
097:
098: public ESBase typeof() throws ESException {
099: throw new ESException("no typeof");
100: }
101:
102: public Class getJavaType() {
103: return void.class;
104: }
105:
106: public ESBase getProperty(ESString key) throws Throwable {
107: return esEmpty;
108: }
109:
110: boolean canPut(ESString name) {
111: return true;
112: }
113:
114: /**
115: * Sets the named property
116: */
117: public void setProperty(ESString key, ESBase value)
118: throws Throwable {
119: }
120:
121: public ESBase delete(ESString key) throws Throwable {
122: return ESBoolean.TRUE;
123: }
124:
125: public ESBase toPrimitive(int type) throws Throwable {
126: return this ;
127: }
128:
129: public ESBase toPrimitive() throws Throwable {
130: return toPrimitive(NONE);
131: }
132:
133: public boolean isBoolean() {
134: return false;
135: }
136:
137: public boolean toBoolean() {
138: return false;
139: }
140:
141: public boolean isNum() {
142: return false;
143: }
144:
145: public double toNum() throws Throwable {
146: throw new ESException("no number: " + getClass().getName());
147: }
148:
149: public boolean isString() {
150: return false;
151: }
152:
153: public ESString toStr() throws Throwable {
154: throw new ESException("no string: " + getClass().getName());
155: }
156:
157: public ESBase valueOf() throws Throwable {
158: return toPrimitive(NONE);
159: }
160:
161: public ESString toSource(IntMap map, boolean isLoopPass)
162: throws Throwable {
163: if (isLoopPass)
164: return null;
165:
166: return toStr();
167: }
168:
169: public ESObject toObject() throws ESException {
170: throw new ESNullException(className + " has no properties");
171: }
172:
173: public Object toJavaObject() throws ESException {
174: return null;
175: }
176:
177: Object copy(HashMap refs) {
178: return this ;
179: }
180:
181: public ESBase call(Call eval, int length) throws Throwable {
182: throw new ESNullException(toStr() + " is not a function");
183: }
184:
185: public ESBase call(Call eval, int length, ESString key)
186: throws Throwable {
187: ESBase call = hasProperty(key);
188:
189: if (call != null) {
190: eval.callee = call;
191: return call.call(eval, length);
192: }
193:
194: if (prototype != null && prototype != this )
195: return prototype.call(eval, length, key);
196: else
197: throw new ESUndefinedException("undefined call `" + key
198: + "'");
199: }
200:
201: public ESBase construct(Call eval, int length) throws Throwable {
202: throw new ESNullException(toStr() + " is not a constructor");
203: }
204:
205: public Iterator keys() throws Throwable {
206: return toObject().keys();
207: }
208:
209: // useful junk
210:
211: String getClassName() {
212: return className;
213: }
214:
215: public ESBase hasProperty(ESString key) throws Throwable {
216: ESBase value = getProperty(key);
217:
218: return value == esEmpty ? null : value;
219: }
220:
221: ESBase hasProperty(int i) throws Throwable {
222: return hasProperty(ESString.create(i));
223: }
224:
225: /**
226: * Returns the text object for the lexeme.
227: */
228: public ESBase getProperty(String key) throws Throwable {
229: return getProperty(ESString.create(key));
230: }
231:
232: /**
233: * Returns the text object for the lexeme.
234: */
235: ESBase getProperty(int i) throws Throwable {
236: return getProperty(ESString.create(i));
237: }
238:
239: public void setProperty(String key, ESBase value) throws Throwable {
240: setProperty(ESString.create(key), value);
241: }
242:
243: /**
244: * Sets the named property
245: */
246: public void setProperty(int i, ESBase value) throws Throwable {
247: setProperty(ESString.create(i), value);
248: }
249:
250: ESBase delete(String key) throws Throwable {
251: return delete(ESString.create(key));
252: }
253:
254: ESBase delete(int i) throws Throwable {
255: return delete(ESString.create(i));
256: }
257:
258: public ESBase plus(ESBase b) throws Throwable {
259: ESBase primA = toPrimitive(NONE);
260: ESBase primB = b.toPrimitive(NONE);
261:
262: if (primA instanceof ESString || primB instanceof ESString) {
263: return ESString.create(primA.toStr().toString()
264: + primB.toStr().toString());
265: } else {
266: return ESNumber.create(primA.toNum() + primB.toNum());
267: }
268: }
269:
270: public boolean lessThan(ESBase ob, boolean neg) throws Throwable {
271: ESBase a = toPrimitive(NONE);
272: ESBase b = ob.toPrimitive(NONE);
273:
274: if (a instanceof ESString && b instanceof ESString) {
275: return ((((ESString) a).compareTo((ESString) b) < 0) != neg);
276: } else {
277: double da = a.toNum();
278: double db = b.toNum();
279:
280: if (Double.isNaN(da) || Double.isNaN(db))
281: return false;
282: else
283: return (da < db) != neg;
284: }
285: }
286:
287: public boolean greaterThan(ESBase ob, boolean neg) throws Throwable {
288: return ob.lessThan(this , neg);
289: }
290:
291: public int toInt32() throws Throwable {
292: double value = toNum();
293:
294: if (Double.isInfinite(value))
295: return 0;
296: else
297: return (int) ((long) value & 0xffffffffL);
298: }
299:
300: public String toString() {
301: try {
302: return toStr().toString();
303: } catch (Throwable e) {
304: System.out.println("Exception: " + e);
305: e.printStackTrace();
306: return "";
307: }
308: }
309:
310: public String toJavaString() throws Throwable {
311: return toStr().toString();
312: }
313:
314: public boolean ecmaEquals(ESBase b) throws Throwable {
315: return this == b;
316: }
317: }
|