1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
9 * The public API for the JS engine; JS itself is actually a class
10 * implementing the minimal amount of functionality for an Object
11 * which can be manipulated by JavaScript code.
13 public abstract class JS {
16 // Static Methods //////////////////////////////////////////////////////////////////////
18 private static Hashtable currentFunction = new Hashtable();
19 public static Function getCurrentFunction() { return (Function)currentFunction.get(Thread.currentThread()); }
20 public static String getCurrentFunctionSourceName() { return getCurrentFunctionSourceName(Thread.currentThread()); }
21 public static String getFileAndLine() { return getCurrentFunctionSourceName() + ":" + getCurrentFunction().getLine(); }
22 public static String getCurrentFunctionSourceName(Thread t) {
23 Function f = (Function)currentFunction.get(t);
24 if (f == null) return "null";
25 return f.getSourceName();
28 public static boolean toBoolean(Object o) {
29 if (o == null) return false;
30 if (o instanceof Boolean) return ((Boolean)o).booleanValue();
31 if (o instanceof Number) return o.equals(new Integer(0));
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 throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle");
47 // Instance Methods ////////////////////////////////////////////////////////////////////
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();
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"); }
58 // Subclasses /////////////////////////////////////////////////////////////////////////
60 /** A slightly more featureful version of JS */
61 public static class Obj extends JS {
62 private Hash entries = new Hash();
63 private boolean sealed = false;
64 public Obj() { this(false); }
65 public Obj(boolean sealed) { this.sealed = sealed; }
66 public void setSeal(boolean sealed) { this.sealed = sealed; }
67 public Object get(Object key) { return entries.get(key); }
68 public void put(Object key, Object val) { if (!sealed) entries.put(key, val); }
69 public Object[] keys() { return(entries.keys()); }
72 /** An exception which can be thrown and caught by JavaScripts */
73 public static class Exn extends RuntimeException {
74 private Object js = null;
75 public Exn(Object js) { this.js = js; }
76 public String toString() { return "JS.Exn: " + js.toString(); }
77 public String getMessage() { return toString(); }
78 public Object getObject() { return js; }
81 /** A JavaScript Array */
82 public static class Array extends Obj {
83 private Vec vec = new Vec();
85 public Array(int size) { vec.setSize(size); }
86 private static int intVal(Object o) {
87 if (o instanceof Number) {
88 int intVal = ((Number)o).intValue();
89 if (intVal == ((Number)o).doubleValue()) return intVal;
90 return Integer.MIN_VALUE;
92 if (!(o instanceof String)) return Integer.MIN_VALUE;
94 for(int i=0; i<s.length(); i++) if (s.charAt(i) < '0' || s.charAt(i) > '9') return Integer.MIN_VALUE;
95 return Integer.parseInt(s);
97 public Object get(Object key) throws JS.Exn {
99 if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
100 if (key.equals("trapee")) return org.xwt.Trap.currentTrapee();
101 if (key.equals("length")) return new Long(vec.size());
103 if (i == Integer.MIN_VALUE) return super.get(key);
105 return vec.elementAt(i);
106 } catch (ArrayIndexOutOfBoundsException e) {
107 throw new JS.Exn(e.getMessage());
110 public void put(Object key, Object val) {
111 if (key.equals("length")) vec.setSize(toNumber(val).intValue());
113 if (i == Integer.MIN_VALUE) super.put(key, val);
115 if (i >= vec.size()) vec.setSize(i+1);
116 vec.setElementAt(val, i);
119 public Object[] keys() {
120 Object[] sup = super.keys();
121 Object[] ret = new Object[vec.size() + 1 + sup.length];
122 System.arraycopy(sup, 0, ret, vec.size(), sup.length);
123 for(int i=0; i<vec.size(); i++) ret[i] = new Integer(i);
124 ret[vec.size()] = "length";
127 public void setSize(int i) { vec.setSize(i); }
128 public int length() { return vec.size(); }
129 public Object elementAt(int i) { return vec.elementAt(i); }
130 public void addElement(Object o) { vec.addElement(o); }
131 public void setElementAt(Object o, int i) { vec.setElementAt(o, i); }
134 /** Anything that is callable */
135 public static abstract class Function extends Obj {
136 public String getSourceName() throws JS.Exn { return "unknown"; }
137 public int getLine() throws JS.Exn { return -1; }
138 public abstract Object _call(JS.Array args) throws JS.Exn, ByteCodeBlock.ControlTransferException;
139 public final Object call(JS.Array args) throws JS.Exn {
140 Function saved = (Function)currentFunction.get(Thread.currentThread());
141 currentFunction.put(Thread.currentThread(), this);
144 } catch (ByteCodeBlock.ReturnException e) { // ignore
146 } catch (ByteCodeBlock.ControlTransferException e) {
147 throw new RuntimeException(getSourceName() + ":" + getLine() +
148 " error, ControlTransferException tried to leave a function");
150 if (saved == null) currentFunction.remove(Thread.currentThread());
151 else currentFunction.put(Thread.currentThread(), saved);
156 public static class Script extends Function {
158 private Script(Vector e) { this.e = e; }
159 public String getSourceName() throws JS.Exn { return ((ByteCodeBlock)e.elementAt(0)).getSourceName(); }
160 public Object _call(JS.Array args) throws JS.Exn, ByteCodeBlock.ControlTransferException {
161 Scope rootScope = (Scope)args.elementAt(0);
162 for(int i=0; i<e.size(); i++) ((ByteCodeBlock)e.elementAt(i)).eval(rootScope);
165 public static Script parse(Reader r, String sourceName, int line) throws IOException {
166 Parser p = new Parser(r, sourceName, line);
168 Vector exprs = new Vector();
170 ByteCodeBlock ret = p.parseStatement();
171 if (ret == null) break;
172 exprs.addElement(ret);
174 return new Script(exprs);
175 } catch (Exception e) {
176 if (Log.on) Log.log(Parser.class, e);
182 /** Any object which becomes part of the scope chain must support this interface */
183 public static class Scope extends Obj {
184 private Scope parentScope;
185 private static Object NULL = new Object();
186 public Scope(Scope parentScope) { this(parentScope, false); }
187 public Scope(Scope parentScope, boolean sealed) { super(sealed); this.parentScope = parentScope; }
188 public Scope getParentScope() { return parentScope; }
190 // transparent scopes are not returned by THIS
191 public boolean isTransparent() { return false; }
193 public boolean has(Object key) { return super.get(key) != null; }
194 public Object get(Object key) {
195 if (!has(key)) return parentScope == null ? null : getParentScope().get(key);
196 Object ret = super.get(key); return ret == NULL ? null : ret;
198 public void put(Object key, Object val) {
199 if (!has(key) && parentScope != null) getParentScope().put(key, val);
200 else super.put(key, val == null ? NULL : val);
202 public Object[] keys() { throw new Error("you can't enumerate the properties of a Scope"); }
203 public void declare(String s) {
204 if (isTransparent()) getParentScope().declare(s);
205 else super.put(s, NULL);