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