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