1 // Copyright 2003 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() {
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));
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();
44 throw new Error("toNumber() got object of type " + o.getClass().getName());
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 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()); }
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; }
79 public static class Array extends Obj {
80 private Vec vec = new Vec();
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;
89 if (!(o instanceof String)) return Integer.MIN_VALUE;
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);
94 public Object get(Object key) throws JS.Exn {
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());
100 if (i == Integer.MIN_VALUE) return super.get(key);
102 return vec.elementAt(i);
103 } catch (ArrayIndexOutOfBoundsException e) {
104 throw new JS.Exn(e.getMessage());
107 public void put(Object key, Object val) {
108 if (key.equals("length")) vec.setSize(toNumber(val).intValue());
110 if (i == Integer.MIN_VALUE) super.put(key, val);
112 if (i >= vec.size()) vec.setSize(i+1);
113 vec.setElementAt(val, i);
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";
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); }
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 int getLine() throws JS.Exn { return -1; }
136 public final Object call(JS.Array args) throws JS.Exn { return _call(args); }
139 public static class Script extends Function {
141 private Script(Vector e) { this.e = e; }
142 public String getSourceName() throws JS.Exn { return ((ForthBlock)e.elementAt(0)).sourceName; }
143 public Object _call(JS.Array args) throws JS.Exn {
144 Scope rootScope = (Scope)args.elementAt(0);
145 Function saved = (Function)currentFunction.get(Thread.currentThread());
146 currentFunction.put(Thread.currentThread(), this);
148 for(int i=0; i<e.size(); i++)
149 ((ForthBlock)e.elementAt(i)).eval(rootScope);
150 } catch (ForthBlock.ReturnException e) {
152 } catch (ForthBlock.ControlTransferException e) {
153 throw new JS.Exn("block threw a ControlTransferException: " + e);
155 if (saved == null) currentFunction.remove(Thread.currentThread());
156 else currentFunction.put(Thread.currentThread(), saved);
160 public static Script parse(Reader r, String sourceName, int line) throws IOException {
161 Parser p = new Parser(r, sourceName, line);
163 Vector exprs = new Vector();
165 ForthBlock ret = p.parseStatement();
166 if (ret == null) break;
167 exprs.addElement(ret);
169 return new Script(exprs);
170 } catch (Exception e) {
171 if (Log.on) Log.log(Parser.class, e);
177 /** Any object which becomes part of the scope chain must support this interface */
178 public static class Scope extends Obj {
179 private Scope parentScope;
180 private static Object NULL = new Object();
181 public Scope(Scope parentScope) { this(parentScope, false); }
182 public Scope(Scope parentScope, boolean sealed) { super(sealed); this.parentScope = parentScope; }
183 public Scope getParentScope() { return parentScope; }
185 // transparent scopes are not returned by THIS
186 public boolean isTransparent() { return false; }
188 public boolean has(Object key) { return super.get(key) != null; }
189 public Object get(Object key) {
190 if (!has(key)) return parentScope == null ? null : getParentScope().get(key);
191 Object ret = super.get(key); return ret == NULL ? null : ret;
193 public void put(Object key, Object val) {
194 if (!has(key) && parentScope != null) getParentScope().put(key, val);
195 else super.put(key, val == null ? NULL : val);
197 public Object[] keys() { throw new Error("you can't enumerate the properties of a Scope"); }
198 public void declare(String s) {
199 if (isTransparent()) getParentScope().declare(s);
200 else super.put(s, NULL);