2003/07/05 02:38:15
[org.ibex.core.git] / src / org / xwt / js / ArrayImpl.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] 
2
3 package org.xwt.js; 
4 import org.xwt.util.*; 
5 import java.io.*;
6 import java.util.*;
7
8 // FIXME: could use some cleaning up...
9
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;
20         }
21         if (!(o instanceof String)) return Integer.MIN_VALUE;
22         String s = (String)o;
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);
25     }
26     // we use _get instead of get solely to work around a GCJ bug
27     public Object _get(Object key) throws JS.Exn {
28         if (key.equals("length")) return new Long(vec.size());
29         if (key.equals("push")) return new JS.Callable() {
30             public Object call(JS.Array args) {
31                 for(int i=0;i<args.length();i++)
32                     vec.push(args.elementAt(i));
33                 return new Long(vec.size());
34             }
35         };
36         if (key.equals("pop")) return new JS.Callable() {
37             public Object call(JS.Array args) {
38                 return vec.pop(); // this'll return null on size()==0 
39             }
40         };
41                 
42         int i = intVal(key);
43         if (i == Integer.MIN_VALUE) return super.get(key);
44         try {
45             return vec.elementAt(i);
46         } catch (ArrayIndexOutOfBoundsException e) {
47             return null;
48         }
49     }
50     // we use _put instead of put solely to work around a GCJ bug
51     public void _put(Object key, Object val) {
52         if (key.equals("length")) vec.setSize(toNumber(val).intValue());
53         int i = intVal(key);
54         if (i == Integer.MIN_VALUE) super.put(key, val);
55         else {
56             if (i >= vec.size()) vec.setSize(i+1);
57             vec.setElementAt(val, i);
58         }
59     }
60     public Object[] keys() {
61         Object[] sup = super.keys();
62         Object[] ret = new Object[vec.size() + 1 + sup.length];
63         System.arraycopy(sup, 0, ret, vec.size(), sup.length);
64         for(int i=0; i<vec.size(); i++) ret[i] = new Integer(i);
65         ret[vec.size()] = "length";
66         return ret;
67     }
68     public void setSize(int i) { vec.setSize(i); }
69     public int length() { return vec.size(); }
70     public Object elementAt(int i) { return vec.elementAt(i); }
71     public void addElement(Object o) { vec.addElement(o); }
72     public void setElementAt(Object o, int i) { vec.setElementAt(o, i); }
73     
74     public String typeName() { return "array"; }
75 }
76