KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
34
35 /**
36  * JavaScript object
37  */

38 class ESArrayWrap extends ESJavaWrapper {
39   static ESId LENGTH = ESId.intern("length");
40
41   protected ESArrayWrap() {}
42
43   protected int length() { return 0; }
44   public ESBase getProperty(int i) throws Throwable JavaDoc { return esEmpty; }
45   public ESBase delete(int i) throws ESException { return ESBoolean.FALSE; }
46   public void setProperty(int i, ESBase value) throws Throwable JavaDoc { }
47
48   public ESBase getProperty(ESString name) throws Throwable JavaDoc
49   {
50     if (name.equals(LENGTH))
51       return ESNumber.create(length());
52
53     try { // XXX: to fix mips bugs
54
double value = name.toNum();
55       int iValue = (int) value;
56
57       if (iValue == value)
58     return getProperty(iValue);
59
60       return super.getProperty(name);
61     } catch (Exception JavaDoc e) {
62       return super.getProperty(name);
63     }
64   }
65
66   public void setProperty(ESString name, ESBase value) throws Throwable JavaDoc
67   {
68     if (name.equals(LENGTH))
69       return;
70     
71     try { // XXX: to fix mips bugs
72
double dIndex = name.toNum();
73       int index = (int) dIndex;
74
75       if (index == dIndex)
76     setProperty(index, value);
77       else
78     super.setProperty(name, value);
79     } catch (Exception JavaDoc e) {
80       super.setProperty(name, value);
81     }
82   }
83
84   public ESBase delete(ESString name) throws Throwable JavaDoc
85   {
86     if (name.equals(LENGTH))
87       return ESBoolean.FALSE;
88
89     try {
90       double dIndex = name.toNum();
91       int index = (int) dIndex;
92
93       if (index == dIndex)
94     return delete(index);
95       else
96     return super.delete(name);
97     } catch (Exception JavaDoc e) {
98       return super.delete(name);
99     }
100   }
101   
102   class ArrayIterator implements Iterator JavaDoc {
103     int length;
104     int i;
105
106     public boolean hasNext() { return i < length; }
107     public Object JavaDoc next()
108     {
109       try {
110     return i < length ? getProperty(i++) : ESBase.esNull;
111       } catch (Throwable JavaDoc e) {
112     return ESBase.esNull;
113       }
114     }
115
116     public void remove() { }
117
118     ArrayIterator()
119     {
120       length = length();
121     }
122   }
123
124   public Iterator JavaDoc keys() throws ESException
125   {
126     return new ArrayIterator();
127   }
128
129   public ESString toStr() throws Throwable JavaDoc
130   {
131     return (ESString) NativeArray.toString(this);
132   }
133
134   public ESString toSource(IntMap map, boolean isLoopPass) throws Throwable JavaDoc
135   {
136     return ESArray.arrayToSource(this, map, isLoopPass);
137   }
138
139   protected ESArrayWrap(ESBase proto, Object JavaDoc value)
140   {
141     super(proto, value);
142   }
143 }
144
Popular Tags