2171e4dd259bd23f7508135c519675a64e0aeac0
[org.ibex.core.git] / src / org / ibex / js / JSArray.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] 
2 package org.ibex.js; 
3
4 import org.ibex.util.*; 
5 import java.util.*;
6
7 /** A JavaScript JSArray */
8 public class JSArray extends JS.BT {
9     private static final Object NULL = new Object();
10     
11     public JSArray() { }
12     public JSArray(int size) { setSize(size); }
13     
14     /*private static int intVal(Object o) {
15         if (o instanceof Number) {
16             int intVal = ((Number)o).intValue();
17             if (intVal == ((Number)o).doubleValue()) return intVal;
18             return Integer.MIN_VALUE;
19         }
20         if (!(o instanceof String)) return Integer.MIN_VALUE;
21         String s = (String)o;
22         for(int i=0; i<s.length(); i++) if (s.charAt(i) < '0' || s.charAt(i) > '9') return Integer.MIN_VALUE;
23         return Integer.parseInt(s);
24     }*/
25     
26     public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
27         if(isString(method)) {
28             //#switch(JS.toString(method))
29             case "pop": {
30                 int oldSize = size();
31                 if(oldSize == 0) return null;
32                 return removeElementAt(oldSize-1);
33             }
34             case "reverse": return reverse();
35             case "toString": return join(",");
36             case "shift":
37                 if(length() == 0) return null;
38                 return removeElementAt(0);
39             case "join":
40                 return join(nargs == 0 ? "," : JS.toString(a0));
41             case "sort":
42                 return sort(nargs < 1 ? null : a0);
43             case "slice":
44                 int start = toInt(nargs < 1 ? null : a0);
45                 int end = nargs < 2 ? length() : toInt(a1);
46                 return slice(start, end);
47             case "push": {
48                 int oldSize = size();
49                 for(int i=0; i<nargs; i++) insertElementAt(i==0?a0:i==1?a1:i==2?a2:rest[i-3],oldSize+i);
50                 return N(oldSize + nargs);
51             }
52             case "unshift":
53                 for(int i=0; i<nargs; i++) insertElementAt(i==0?a0:i==1?a1:i==2?a2:rest[i-3],i);
54                 return N(size());
55             case "splice":
56                 JSArray array = new JSArray();
57                 for(int i=0; i<nargs; i++) array.addElement(i==0?a0:i==1?a1:i==2?a2:rest[i-3]);
58                 return splice(array);
59             //#end
60         }
61         return super.callMethod(method, a0, a1, a2, rest, nargs);
62     }
63         
64     public JS get(JS key) throws JSExn {
65         if (isInt(key)) {
66             int i = toInt(key);
67             if (i < 0 || i >= size()) return null;
68             return elementAt(i);
69         }
70         if(isString(key)) {
71             //#switch(JS.toString(key))
72             case "pop": return METHOD;
73             case "reverse": return METHOD;
74             case "toString": return METHOD;
75             case "shift": return METHOD;
76             case "join": return METHOD;
77             case "sort": return METHOD;
78             case "slice": return METHOD;
79             case "push": return METHOD;
80             case "unshift": return METHOD;
81             case "splice": return METHOD;
82             case "length": return N(size());
83             //#end
84         }
85         return super.get(key);
86     }
87
88     public void put(JS key, JS val) throws JSExn {
89         if (isInt(key)) {
90             int i = toInt(key);
91             int oldSize = size();
92             if(i < oldSize) {
93                 setElementAt(val,i);
94             } else {
95                 if(i > oldSize) setSize(i);
96                 insertElementAt(val,i);
97             }
98             return;
99         }
100         if(isString(key)) {
101             if (JS.toString(key).equals("length")) {
102                 setSize(JS.toInt(val));
103                 return;
104             }
105         }
106         super.put(key,val);
107     }
108
109     public Enumeration keys() throws JSExn {
110         return new Enumeration(super.keys()) {
111                 private int n = 0;
112                 public boolean _hasMoreElements() { return n < size(); }
113                 public JS _nextElement() {
114                     return n >= size() ? null : JS.N(n++);
115                 }
116             };
117     }
118
119     public final void setSize(int newSize) {
120         // FEATURE: This could be done a lot more efficiently in BalancedTree
121         int oldSize = size();
122         for(int i=oldSize;i<newSize;i++) insertElementAt(null,i);
123         for(int i=oldSize-1;i>=newSize;i--) removeElementAt(i);
124     }
125     
126     public final int length() { return size(); }
127     public final JS elementAt(int i) { 
128         if(i < 0 || i >= size()) throw new ArrayIndexOutOfBoundsException(i);
129         Object o = getNode(i);
130         return o == NULL ? (JS)null : (JS)o;
131     }
132     public final void addElement(JS o) { 
133         insertNode(size(),o==null ? NULL : o);
134     }
135     public final void setElementAt(JS o, int i) {
136         if(i < 0 || i >= size()) throw new ArrayIndexOutOfBoundsException(i);
137         replaceNode(i,o==null ? NULL : o);
138     }
139     public final void insertElementAt(JS o, int i) {
140         if(i < 0 || i > size()) throw new ArrayIndexOutOfBoundsException(i);
141         insertNode(i,o==null ? NULL : o);
142     }
143     public final JS removeElementAt(int i) {
144         if(i < 0 || i >= size()) throw new ArrayIndexOutOfBoundsException(i);
145         Object o = deleteNode(i);
146         return o == NULL ? (JS)null : (JS)o;
147     }
148     
149     public final int size() { return treeSize(); }
150     
151     private JS join(String sep) throws JSExn {
152         int length = size();
153         if(length == 0) return JS.S("");
154         StringBuffer sb = new StringBuffer(64);
155         int i=0;
156         while(true) {
157             JS o = elementAt(i);
158             if(o != null) sb.append(JS.toString(o));
159             if(++i == length) break;
160             sb.append(sep);
161         }
162         return JS.S(sb.toString());
163     }
164     
165     // FEATURE: Implement this more efficiently
166     private JS reverse() {
167         int size = size();
168         if(size < 2) return this;
169         Vec vec = toVec();
170         clear();
171         for(int i=size-1,j=0;i>=0;i--,j++) insertElementAt((JS)vec.elementAt(i),j);
172         return this;
173     }
174     
175     private JS slice(int start, int end) {
176         int length = length();
177         if(start < 0) start = length+start;
178         if(end < 0) end = length+end;
179         if(start < 0) start = 0;
180         if(end < 0) end = 0;
181         if(start > length) start = length;
182         if(end > length) end = length;
183         JSArray a = new JSArray(end-start);
184         for(int i=0;i<end-start;i++)
185             a.setElementAt(elementAt(start+i),i);
186         return a;
187     }
188     
189     private static final Vec.CompareFunc defaultSort = new Vec.CompareFunc() {
190         public int compare(Object a, Object b) {
191             try {
192                 return JS.toString((JS)a).compareTo(JS.toString((JS)b));
193             } catch(JSExn e) {
194                 // FIXME: See emca about this
195                 throw new RuntimeException(e.toString());
196             }
197         }
198     };
199     private JS sort(JS tmp) throws JSExn {
200         Vec vec = toVec();
201         if(tmp instanceof JS) {
202             final JS jsFunc = (JS) tmp;
203             vec.sort(new Vec.CompareFunc() {
204                 public int compare(Object a, Object b) {
205                     try {
206                         return JS.toInt(jsFunc.call((JS)a, (JS)b, null, null, 2));
207                     } catch (JSExn e) {
208                         // FIXME: Check ecma to see what we should do here
209                         throw new RuntimeException(e.toString());
210                     }
211                 }
212             });
213         } else {
214             vec.sort(defaultSort);
215         }
216         setFromVec(vec);
217         return this;
218     }
219     
220     private JS splice(JSArray args) throws JSExn {
221         int oldLength = length();
222         int start = JS.toInt(args.length() < 1 ? null : args.elementAt(0));
223         int deleteCount = JS.toInt(args.length() < 2 ? null : args.elementAt(1));
224         int newCount = args.length() - 2;
225         if(newCount < 0) newCount = 0;
226         if(start < 0) start = oldLength+start;
227         if(start < 0) start = 0;
228         if(start > oldLength) start = oldLength;
229         if(deleteCount < 0) deleteCount = 0;
230         if(deleteCount > oldLength-start) deleteCount = oldLength-start;
231         int newLength = oldLength - deleteCount + newCount;
232         int lengthChange = newLength - oldLength;
233         JSArray ret = new JSArray(deleteCount);
234         for(int i=0;i<deleteCount;i++)
235             ret.setElementAt(elementAt(start+i),i);
236         if(lengthChange > 0) {
237             setSize(newLength);
238             for(int i=newLength-1;i>=start+newCount;i--)
239                 setElementAt(elementAt(i-lengthChange),i);
240         } else if(lengthChange < 0) {
241             for(int i=start+newCount;i<newLength;i++)
242                 setElementAt(elementAt(i-lengthChange),i);
243             setSize(newLength);
244         }
245         for(int i=0;i<newCount;i++)
246             setElementAt(args.elementAt(i+2),start+i);
247         return ret;
248     }
249     
250     protected Vec toVec() {
251         int count = size();
252         Vec vec = new Vec();
253         vec.setSize(count);
254         for(int i=0;i<count;i++) {
255             Object o = getNode(i);
256             vec.setElementAt(o == NULL ? null : o,i);
257         }
258         return vec;
259     }
260     
261     protected void setFromVec(Vec vec) {
262         int count = vec.size();
263         clear();
264         for(int i=0;i<count;i++) {
265             Object o = vec.elementAt(i);
266             insertNode(i,o==null ? NULL : o);
267         }
268     }
269     
270     String coerceToString() throws JSExn { return JS.toString(join(",")); }
271 }