2003/05/26 10:38:33
[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         private static int intVal(Object o) {
65             if (o instanceof Number) {
66                 int intVal = ((Number)o).intValue();
67                 if (intVal == ((Number)o).doubleValue()) return intVal;
68                 return Integer.MIN_VALUE;
69             }
70             if (!(o instanceof String)) return Integer.MIN_VALUE;
71             String s = (String)o;
72             for(int i=0; i<s.length(); i++) if (s.charAt(i) < '0' || s.charAt(i) > '9') return Integer.MIN_VALUE;
73             return Integer.parseInt(s);
74         }
75         public Object get(Object key) throws JS.Exn {
76             // FIXME: HACK!
77             if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
78             if (key.equals("trapee")) return org.xwt.Trap.currentTrapee();
79             if (key.equals("length")) return new Long(vec.size());
80             int i = intVal(key);
81             if (i == Integer.MIN_VALUE) return super.get(key);
82             try {
83                 return vec.elementAt(i);
84             } catch (ArrayIndexOutOfBoundsException e) {
85                 throw new JS.Exn(e.getMessage());
86             }
87         }
88         public void put(Object key, Object val) {
89             if (key.equals("length")) vec.setSize(Parser.toNumber(val).intValue());
90             int i = intVal(key);
91             if (i == Integer.MIN_VALUE) super.put(key, val);
92             else {
93                 if (i >= vec.size()) vec.setSize(i+1);
94                 vec.setElementAt(val, i);
95             }
96         }
97         public Object[] keys() {
98             Object[] sup = super.keys();
99             Object[] ret = new Object[vec.size() + 1 + sup.length];
100             System.arraycopy(sup, 0, ret, vec.size(), sup.length);
101             for(int i=0; i<vec.size(); i++) ret[i] = new Integer(i);
102             ret[vec.size()] = "length";
103             return ret;
104         }
105         public int length() { return vec.size(); }
106         public Object elementAt(int i) { return vec.elementAt(i); }
107         public void addElement(Object o) { vec.addElement(o); }
108         public void setElementAt(Object o, int i) { vec.setElementAt(o, i); }
109     }
110
111     public static Hashtable currentFunction = new Hashtable();
112     public static abstract class Function extends Obj {
113         public abstract Object _call(JS.Array args) throws JS.Exn;
114         public String getSourceName() throws JS.Exn { return "unknown"; }
115         public final Object call(JS.Array args) throws JS.Exn { return _call(args); }
116     }
117
118     public static class Script extends Function {
119         Vector e = null;
120         private Script(Vector e) { this.e = e; }
121         public String getSourceName() throws JS.Exn { return ((Parser.Expr)e.elementAt(0)).sourceName; }
122         public Object _call(JS.Array args) throws JS.Exn {
123             Scope rootScope = (Scope)args.elementAt(0);
124             Function saved = (Function)currentFunction.get(Thread.currentThread());
125             currentFunction.put(Thread.currentThread(), this);
126             try {
127                 for(int i=0; i<e.size(); i++)
128                     ((Parser.Expr)e.elementAt(i)).eval(rootScope);
129             } catch (Parser.ReturnException e) {
130                 // ignore
131             } catch (Parser.ControlTransferException e) {
132                 throw new JS.Exn("block threw a ControlTransferException: " + e);
133             } finally {
134                 if (saved == null) currentFunction.remove(Thread.currentThread());
135                 else currentFunction.put(Thread.currentThread(), saved);
136             }
137             return null;
138         }
139         public static Script parse(Reader r, String sourceName, int line) throws IOException {
140             Parser p = new Parser(r, sourceName, line);
141             try {
142                 Vector exprs = new Vector();
143                 while(true) {
144                     Parser.Expr ret = p.parseBlock(false);
145                     if (ret == null || (ret.code == Parser.LC && ret.left == null)) break;
146                     exprs.addElement(ret);
147                 }
148                 return new Script(exprs);
149             } catch (Exception e) {
150                 if (Log.on) Log.log(Parser.class, e);
151                 return null;
152             }
153         }
154     }
155
156     /** Any object which becomes part of the scope chain must support this interface */ 
157     public static class Scope extends Obj { 
158         private Scope parentScope;
159         private static Object NULL = new Object();
160         public Scope(Scope parentScope) { this(parentScope, false); }
161         public Scope(Scope parentScope, boolean sealed) { super(sealed); this.parentScope = parentScope; }
162         public Scope getParentScope() { return parentScope; }
163
164         // transparent scopes are not returned by THIS
165         public boolean isTransparent() { return false; }
166
167         public boolean has(Object key) { return super.get(key) != null; }
168         public Object get(Object key) {
169             if (!has(key)) return parentScope == null ? null : getParentScope().get(key);
170             Object ret = super.get(key); return ret == NULL ? null : ret;
171         }
172         public void put(Object key, Object val) {
173             if (!has(key) && parentScope != null) getParentScope().put(key, val);
174             else super.put(key, val == null ? NULL : val);
175         }
176         public Object[] keys() { throw new Error("you can't enumerate the properties of a Scope"); }
177         public void declare(String s) { if (isTransparent()) getParentScope().declare(s); else super.put(s, NULL);}
178     } 
179  
180
181
182
183
184
185