make JSMath and JSArray public
[org.ibex.js.git] / src / org / ibex / js / JSArray.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.js; 
6
7 import java.io.InputStream;
8 import org.ibex.util.*; 
9
10 /** A JavaScript JSArray */
11 public class JSArray extends Basket.Array implements JS, Basket.CompareFunc {
12     private static final JS.Method METHOD = new JS.Method();
13     private static final String[] empty = new String[0];
14
15     JSArray() { }
16     JSArray(int size) { super(size); }
17     JSArray(JS[] args) { super(args.length); addAll(args); }
18     JSArray(JS arg) { super(1); add(arg); }
19
20     public JS unclone() { return this; }
21     public JS.Enumeration keys() throws JSExn {
22         return new Enumeration(null) {
23             private int n = 0;
24             public boolean _hasNext() { return n < size(); }
25             public JS _next() { return JSU.N(n++); }
26         };
27     }
28     public JS get(JS key) throws JSExn {
29         if (key == null || !(key instanceof JSNumber.I)) {
30             //#switch(JSU.str(key))
31             case "pop": return METHOD;
32             case "reverse": return METHOD;
33             case "toString": return METHOD;
34             case "shift": return METHOD;
35             case "join": return METHOD;
36             case "sort": return METHOD;
37             case "slice": return METHOD;
38             case "push": return METHOD;
39             case "unshift": return METHOD;
40             case "splice": return METHOD;
41             case "length": return JSU.N(size());
42             //#end
43             throw new JSExn("arrays only support positive integer keys, can not use: "+JSU.str(key));
44         }
45         return (JS)get(((JSNumber.I)key).toInt());
46     }
47     public void put(JS key, JS val) throws JSExn {
48         if (JSU.str(key).equals("length")) { setSize(JSU.toInt(val)); }
49
50         if (key == null || !(key instanceof JSNumber.I)) throw new JSExn(
51             "arrays only support positive integer keys, can not use: "+JSU.str(key));
52         int i = ((JSNumber.I)key).toInt();
53         if (i < 0) throw new JSExn("arrays can not use negative integer keys "+i);
54         size(i + 1); while (size() < i) add(null);
55         set(i, val);
56     }
57     public InputStream getInputStream() { return null; }
58
59     public String[] getFormalArgs() { return empty; }
60     public String coerceToString() { return "array"; } // FIXME
61
62     public boolean hasKey(JS key) {
63         if (key == null || !(key instanceof JSNumber.I)) return false;
64         int i = ((JSNumber.I)key).toInt();
65         return 0 <= i && i < size();
66     }
67
68     public Object run(Object o) throws JSExn { return call(null); }
69     public void pause() throws NotPausableException { throw new NotPausableException(); }
70     public JS call(JS[] args) throws JSExn { throw new JSExn("can not call an array as a function"); }
71     public JS call(JS method, JS[] args) throws JSExn {
72         //#switch(JSU.str(method))
73         case "pop": return size() == 0 ? null : (JS)remove(size() - 1);
74         case "push": addAll(args); return JSU.N(size());
75         case "reverse": reverse(); return this;
76         case "toString": return join(",");
77         case "shift": return size() == 0 ? null : (JS)remove(0);
78         case "join": return join(args.length == 0 ? "," : JSU.str(args[0]));
79         case "sort": return sort(args.length == 0 ? null : args[0]);
80         case "slice":
81             int start = JSU.toInt(args.length < 1 ? null : args[0]);
82             int end = args.length < 2 ? size() : JSU.toInt(args[1]);
83             return slice(start, end);
84         case "unshift":
85             for (int i=0; i < args.length; i++) add(i, args[i]);
86             return JSU.N(size());
87         case "splice": return splice(args);
88         //#end
89
90         throw new JSExn("arrays have no function: "+JSU.str(method));
91     }
92
93     public JS putAndTriggerTraps(JS key, JS val) throws JSExn { put(key, val); return val; }
94     public JS getAndTriggerTraps(JS key) throws JSExn { return get(key); }
95     public JS justTriggerTraps(JS key, JS val) throws JSExn { return val; }
96
97     public void addTrap(JS k, JS f) throws JSExn { throw new JSExn("arrays do not support traps"); }
98     public void delTrap(JS k, JS f) throws JSExn { throw new JSExn("arrays do not support traps"); }
99     public JS.Trap getTrap(JS k) throws JSExn { throw new JSExn("arrays do not support traps"); }
100
101     /** FIXME: move to specialised ArrayStore superclass. */
102     public void addAll(JS[] entries) { for (int i=0; i < entries.length; i++) add(entries[i]); }
103
104     public void setSize(int newSize) {
105         size(newSize);
106         for (int i=size(); i < newSize; i++) add(null);
107         for (int i=size() - 1; i >= newSize; i--) remove(i);
108     }
109
110
111     // ECMA Implementation ////////////////////////////////////////////////////
112
113     private JS join(String sep) throws JSExn {
114         int length = size();
115         if(length == 0) return JSU.S("");
116         StringBuffer sb = new StringBuffer(64);
117         int i=0;
118         while(true) {
119             JS o = (JS)get(i);
120             if(o != null) sb.append(JSU.toString(o));
121             if(++i == length) break;
122             sb.append(sep);
123         }
124         return JSU.S(sb.toString());
125     }
126  
127     private JS slice(int start, int end) {
128         int length = size();
129         if(start < 0) start = length+start;
130         if(end < 0) end = length+end;
131         if(start < 0) start = 0;
132         if(end < 0) end = 0;
133         if(start > length) start = length;
134         if(end > length) end = length;
135         JSArray a = new JSArray(end-start);
136         for(int i=0;i<end-start;i++) // FIXME: with ArrayStore do System.arraycopy()
137             a.set(i, get(start+i));
138         return a;
139     }
140
141     private JS splice(JS[] args) throws JSExn {
142         int oldLength = size();
143         int start = JSU.toInt(args.length < 1 ? null : args[0]);
144         int deleteCount = JSU.toInt(args.length < 2 ? null : args[1]);
145         int newCount = args.length - 2;
146         if(newCount < 0) newCount = 0;
147         if(start < 0) start = oldLength+start;
148         if(start < 0) start = 0;
149         if(start > oldLength) start = oldLength;
150         if(deleteCount < 0) deleteCount = 0;
151         if(deleteCount > oldLength-start) deleteCount = oldLength-start;
152         int newLength = oldLength - deleteCount + newCount;
153         int lengthChange = newLength - oldLength;
154         JSArray ret = new JSArray(deleteCount);
155         for(int i=0;i<deleteCount;i++) // FIXME: ArrayStore System.arraycopy()
156             ret.set(i, get(start+i));
157         if(lengthChange > 0) {
158             setSize(newLength);
159             for(int i=newLength-1;i>=start+newCount;i--)
160                 set(i, get(i-lengthChange));
161         } else if(lengthChange < 0) {
162             for(int i=start+newCount;i<newLength;i++)
163                 set(i, get(i-lengthChange));
164             setSize(newLength);
165         }
166         for(int i=0;i<newCount;i++)
167             set(start+i, args[i+2]);
168         return ret;
169     }
170
171     private static final Basket.CompareFunc defaultSort = new Basket.CompareFunc() {
172         public int compare(Object a, Object b) {
173             try { return JSU.toString((JS)a).compareTo(JSU.toString((JS)b)); }
174             catch (JSExn e) { throw new JSExn.Wrapper(e); }
175         }
176     };
177     private JS sort(JS comparator) throws JSExn {
178         try {
179             if (comparator == null) sort(defaultSort);
180             else { sort = comparator; sort((CompareFunc)this); }
181             return this;
182         } catch (JSExn.Wrapper w) { throw w.unwrap();
183         } finally { sort = null; }
184     }
185
186     private JS sort = null;
187     private final JS[] sortargs = new JS[2];
188     public int compare(Object a, Object b) throws JSExn.Wrapper {
189         try {
190             sortargs[0] = (JS)a; sortargs[1] = (JS)b;
191             return JSU.toInt(sort.call(sortargs));
192         } catch (JSExn e) { throw new JSExn.Wrapper(e);
193         } finally { sortargs[0] = null; sortargs[1] = null; }
194     }
195
196 }