1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
8 /** The public API for the JS engine */
9 // FEATURE: try using mutable, recycled 'Num' objects
10 public abstract class JS {
12 // Static Methods //////////////////////////////////////////////////////////////////////
14 public static Function getCurrentFunction() {
15 return (Function)currentFunction.get(Thread.currentThread());
17 public static String getCurrentFunctionSourceName() {
18 return getCurrentFunctionSourceName(Thread.currentThread());
20 public static String getCurrentFunctionSourceName(Thread t) {
21 Function f = (Function)currentFunction.get(t);
22 if (f == null) return "null";
23 return f.getSourceName();
25 public static String getFileAndLine() {
30 // Instance Methods ////////////////////////////////////////////////////////////////////
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();
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"); }
41 // Subclasses /////////////////////////////////////////////////////////////////////////
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()); }
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; }
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;
70 if (!(o instanceof String)) return Integer.MIN_VALUE;
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);
75 public Object get(Object key) throws JS.Exn {
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());
81 if (i == Integer.MIN_VALUE) return super.get(key);
83 return vec.elementAt(i);
84 } catch (ArrayIndexOutOfBoundsException e) {
85 throw new JS.Exn(e.getMessage());
88 public void put(Object key, Object val) {
89 if (key.equals("length")) vec.setSize(Parser.toNumber(val).intValue());
91 if (i == Integer.MIN_VALUE) super.put(key, val);
93 if (i >= vec.size()) vec.setSize(i+1);
94 vec.setElementAt(val, i);
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";
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); }
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); }
118 public static class Script extends Function {
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);
127 for(int i=0; i<e.size(); i++)
128 ((Parser.Expr)e.elementAt(i)).eval(rootScope);
129 } catch (Parser.ReturnException e) {
131 } catch (Parser.ControlTransferException e) {
132 throw new JS.Exn("block threw a ControlTransferException: " + e);
134 if (saved == null) currentFunction.remove(Thread.currentThread());
135 else currentFunction.put(Thread.currentThread(), saved);
139 public static Script parse(Reader r, String sourceName, int line) throws IOException {
140 Parser p = new Parser(r, sourceName, line);
142 Vector exprs = new Vector();
144 Parser.Expr ret = p.parseBlock(false);
145 if (ret == null || (ret.code == Parser.LC && ret.left == null)) break;
146 exprs.addElement(ret);
148 return new Script(exprs);
149 } catch (Exception e) {
150 if (Log.on) Log.log(Parser.class, e);
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; }
164 // transparent scopes are not returned by THIS
165 public boolean isTransparent() { return false; }
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;
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);
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);}