1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
8 // FIXME: could use some cleaning up...
10 /** A JavaScript Array */
11 class ArrayImpl extends JS.Obj {
12 private Vec vec = new Vec();
13 public ArrayImpl() { }
14 public ArrayImpl(int size) { vec.setSize(size); }
15 private static int intVal(Object o) {
16 if (o instanceof Number) {
17 int intVal = ((Number)o).intValue();
18 if (intVal == ((Number)o).doubleValue()) return intVal;
19 return Integer.MIN_VALUE;
21 if (!(o instanceof String)) return Integer.MIN_VALUE;
23 for(int i=0; i<s.length(); i++) if (s.charAt(i) < '0' || s.charAt(i) > '9') return Integer.MIN_VALUE;
24 return Integer.parseInt(s);
26 // we use _get instead of get solely to work around a GCJ bug
27 public Object _get(Object key) throws JS.Exn {
29 if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
30 if (key.equals("trapee")) return org.xwt.Trap.currentTrapee();
31 if (key.equals("trapname")) return org.xwt.Trap.currentTrapname();
32 if (key.equals("length")) return new Long(vec.size());
33 if (key.equals("push")) return new JS.Callable() {
34 public Object call(JS.Array args) {
35 for(int i=0;i<args.length();i++)
36 vec.push(args.elementAt(i));
37 return new Long(vec.size());
40 if (key.equals("pop")) return new JS.Callable() {
41 public Object call(JS.Array args) {
42 return vec.pop(); // this'll return null on size()==0
47 if (i == Integer.MIN_VALUE) return super.get(key);
49 return vec.elementAt(i);
50 } catch (ArrayIndexOutOfBoundsException e) {
54 // we use _put instead of put solely to work around a GCJ bug
55 public void _put(Object key, Object val) {
56 if (key.equals("length")) vec.setSize(toNumber(val).intValue());
58 if (i == Integer.MIN_VALUE) super.put(key, val);
60 if (i >= vec.size()) vec.setSize(i+1);
61 vec.setElementAt(val, i);
64 public Object[] keys() {
65 Object[] sup = super.keys();
66 Object[] ret = new Object[vec.size() + 1 + sup.length];
67 System.arraycopy(sup, 0, ret, vec.size(), sup.length);
68 for(int i=0; i<vec.size(); i++) ret[i] = new Integer(i);
69 ret[vec.size()] = "length";
72 public void setSize(int i) { vec.setSize(i); }
73 public int length() { return vec.size(); }
74 public Object elementAt(int i) { return vec.elementAt(i); }
75 public void addElement(Object o) { vec.addElement(o); }
76 public void setElementAt(Object o, int i) { vec.setElementAt(o, i); }