2003/06/03 08:56:56
[org.ibex.core.git] / src / org / xwt / js / JS.java
1 // Copyright 2002 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 /** The public API for the JS engine */
9 // FEATURE: try using mutable, recycled 'Num' objects
10 public abstract class JS { 
11
12     // Static Methods //////////////////////////////////////////////////////////////////////
13
14     public static Function getCurrentFunction() {
15         return (Function)currentFunction.get(Thread.currentThread());
16     }
17     public static String getCurrentFunctionSourceName() {
18         return getCurrentFunctionSourceName(Thread.currentThread());
19     }
20     public static String getCurrentFunctionSourceName(Thread t) {
21         Function f = (Function)currentFunction.get(t);
22         if (f == null) return "null";
23         return f.getSourceName();
24     }
25     public static String getFileAndLine() {
26         return "unknown:??";
27     }
28
29
30     // Instance Methods ////////////////////////////////////////////////////////////////////
31  
32     public abstract Object get(Object key) throws JS.Exn; 
33     public abstract void put(Object key, Object val) throws JS.Exn; 
34     public abstract Object[] keys(); 
35
36     public Number coerceToNumber() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a Number"); }
37     public String coerceToString() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a String"); }
38     public boolean coerceToBoolean() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a Boolean"); }
39
40
41     // Subclasses /////////////////////////////////////////////////////////////////////////
42
43     public static class Obj extends JS {
44         private Hash entries = new Hash();
45         private boolean sealed = false;
46         public Obj() { this(false); }
47         public Obj(boolean sealed) { this.sealed = sealed; }
48         public void setSeal(boolean sealed) { this.sealed = sealed; }
49         public Object get(Object key) { return entries.get(key); }
50         public void put(Object key, Object val) { if (!sealed) entries.put(key, val); }
51         public Object[] keys() { return(entries.keys()); }
52     }
53
54     public static class Exn extends RuntimeException { 
55         private Object js = null; 
56         public Exn(Object js) { this.js = js; } 
57         public String toString() { return "JS.Exn: " + js.toString(); }
58         public String getMessage() { return toString(); }
59         public Object getObject() { return js; } 
60     } 
61
62     public static class Array extends Obj {
63         private Vec vec = new Vec();
64         public Array() { }
65         public Array(int size) { vec.setSize(size); }
66         private static int intVal(Object o) {
67             if (o instanceof Number) {
68                 int intVal = ((Number)o).intValue();
69                 if (intVal == ((Number)o).doubleValue()) return intVal;
70                 return Integer.MIN_VALUE;
71             }
72             if (!(o instanceof String)) return Integer.MIN_VALUE;
73             String s = (String)o;
74             for(int i=0; i<s.length(); i++) if (s.charAt(i) < '0' || s.charAt(i) > '9') return Integer.MIN_VALUE;
75             return Integer.parseInt(s);
76         }
77         public Object get(Object key) throws JS.Exn {
78             // FIXME: HACK!
79             if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
80             if (key.equals("trapee")) return org.xwt.Trap.currentTrapee();
81             if (key.equals("length")) return new Long(vec.size());
82             int i = intVal(key);
83             if (i == Integer.MIN_VALUE) return super.get(key);
84             try {
85                 return vec.elementAt(i);
86             } catch (ArrayIndexOutOfBoundsException e) {
87                 throw new JS.Exn(e.getMessage());
88             }
89         }
90         public void put(Object key, Object val) {
91             if (key.equals("length")) vec.setSize(Parser.toNumber(val).intValue());
92             int i = intVal(key);
93             if (i == Integer.MIN_VALUE) super.put(key, val);
94             else {
95                 if (i >= vec.size()) vec.setSize(i+1);
96                 vec.setElementAt(val, i);
97             }
98         }
99         public Object[] keys() {
100             Object[] sup = super.keys();
101             Object[] ret = new Object[vec.size() + 1 + sup.length];
102             System.arraycopy(sup, 0, ret, vec.size(), sup.length);
103             for(int i=0; i<vec.size(); i++) ret[i] = new Integer(i);
104             ret[vec.size()] = "length";
105             return ret;
106         }
107         public void setSize(int i) { vec.setSize(i); }
108         public int length() { return vec.size(); }
109         public Object elementAt(int i) { return vec.elementAt(i); }
110         public void addElement(Object o) { vec.addElement(o); }
111         public void setElementAt(Object o, int i) { vec.setElementAt(o, i); }
112     }
113
114     public static Hashtable currentFunction = new Hashtable();
115     public static abstract class Function extends Obj {
116         public abstract Object _call(JS.Array args) throws JS.Exn;
117         public String getSourceName() throws JS.Exn { return "unknown"; }
118         public final Object call(JS.Array args) throws JS.Exn { return _call(args); }
119     }
120
121     public static class Script extends Function {
122         Vector e = null;
123         private Script(Vector e) { this.e = e; }
124         public String getSourceName() throws JS.Exn { return ((Parser.Expr)e.elementAt(0)).sourceName; }
125         public Object _call(JS.Array args) throws JS.Exn {
126             Scope rootScope = (Scope)args.elementAt(0);
127             Function saved = (Function)currentFunction.get(Thread.currentThread());
128             currentFunction.put(Thread.currentThread(), this);
129             try {
130                 for(int i=0; i<e.size(); i++)
131                     ((Parser.Expr)e.elementAt(i)).eval(rootScope);
132             } catch (Parser.ReturnException e) {
133                 // ignore
134             } catch (Parser.ControlTransferException e) {
135                 throw new JS.Exn("block threw a ControlTransferException: " + e);
136             } finally {
137                 if (saved == null) currentFunction.remove(Thread.currentThread());
138                 else currentFunction.put(Thread.currentThread(), saved);
139             }
140             return null;
141         }
142         public static Script parse(Reader r, String sourceName, int line) throws IOException {
143             Parser p = new Parser(r, sourceName, line);
144             try {
145                 Vector exprs = new Vector();
146                 while(true) {
147                     Parser.Expr ret = p.parseBlock(false);
148                     if (ret == null) break;
149                     exprs.addElement(ret);
150                 }
151                 return new Script(exprs);
152             } catch (Exception e) {
153                 if (Log.on) Log.log(Parser.class, e);
154                 return null;
155             }
156         }
157     }
158
159     /** Any object which becomes part of the scope chain must support this interface */ 
160     public static class Scope extends Obj { 
161         private Scope parentScope;
162         private static Object NULL = new Object();
163         public Scope(Scope parentScope) { this(parentScope, false); }
164         public Scope(Scope parentScope, boolean sealed) { super(sealed); this.parentScope = parentScope; }
165         public Scope getParentScope() { return parentScope; }
166
167         // transparent scopes are not returned by THIS
168         public boolean isTransparent() { return false; }
169
170         public boolean has(Object key) { return super.get(key) != null; }
171         public Object get(Object key) {
172             if (!has(key)) return parentScope == null ? null : getParentScope().get(key);
173             Object ret = super.get(key); return ret == NULL ? null : ret;
174         }
175         public void put(Object key, Object val) {
176             if (!has(key) && parentScope != null) getParentScope().put(key, val);
177             else super.put(key, val == null ? NULL : val);
178         }
179         public Object[] keys() { throw new Error("you can't enumerate the properties of a Scope"); }
180         public void declare(String s) { if (isTransparent()) getParentScope().declare(s); else super.put(s, NULL);}
181     } 
182  
183
184
185
186
187
188