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) {
77 if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
78 if (key.equals("length")) return new Long(vec.size());
80 if (i == Integer.MIN_VALUE) return super.get(key);
81 return vec.elementAt(i);
83 public void put(Object key, Object val) {
84 if (key.equals("length")) vec.setSize(Parser.toNumber(val).intValue());
86 if (i == Integer.MIN_VALUE) super.put(key, val);
87 else vec.setElementAt(val, i);
89 public Object[] keys() {
90 Object[] sup = super.keys();
91 Object[] ret = new Object[vec.size() + 1 + sup.length];
92 System.arraycopy(sup, 0, ret, vec.size(), sup.length);
93 for(int i=0; i<vec.size(); i++) ret[i] = new Integer(i);
94 ret[vec.size()] = "length";
97 public int length() { return vec.size(); }
98 public Object elementAt(int i) { return vec.elementAt(i); }
99 public void addElement(Object o) { vec.addElement(o); }
100 public void setElementAt(Object o, int i) { vec.setElementAt(o, i); }
103 public static Hashtable currentFunction = new Hashtable();
104 public static abstract class Function extends Obj {
105 public abstract Object _call(JS.Array args) throws JS.Exn;
106 public String getSourceName() throws JS.Exn { return "unknown"; }
107 public final Object call(JS.Array args) throws JS.Exn { return _call(args); }
110 public static class Script extends Function {
111 Parser.Expr e = null;
112 private Script(Parser.Expr e) { this.e = e; }
113 public Object _call(JS.Array args) throws JS.Exn {
114 Scope rootScope = (Scope)args.elementAt(0);
117 } catch (Parser.ReturnException e) {
119 } catch (Parser.ControlTransferException e) {
120 throw new JS.Exn("block threw a ControlTransferException: " + e);
124 public static Script parse(Reader r, String sourceName, int line) throws IOException {
125 Parser p = new Parser(r, sourceName, line);
126 p.pushBackToken(Lexer.LC); // FIXME: hack
128 return new Script(p.parseBlock(true));
129 } catch (Exception e) {
130 if (Log.on) Log.log(Parser.class, e);
136 /** Any object which becomes part of the scope chain must support this interface */
137 public static class Scope extends Obj {
138 private Scope parentScope;
139 private static Object NULL = new Object();
140 public Scope(Scope parentScope) { this(parentScope, false); }
141 public Scope(Scope parentScope, boolean sealed) { super(sealed); this.parentScope = parentScope; }
142 public Scope getParentScope() { return parentScope; }
144 // transparent scopes are not returned by THIS
145 public boolean isTransparent() { return false; }
147 public boolean has(Object key) { return super.get(key) != null; }
148 public Object get(Object key) {
149 if (!has(key)) return parentScope == null ? null : getParentScope().get(key);
150 Object ret = super.get(key); return ret == NULL ? null : ret;
152 public void put(Object key, Object val) {
153 if (!has(key) && parentScope != null) getParentScope().put(key, val);
154 else super.put(key, val == null ? NULL : val);
156 public Object[] keys() { throw new Error("you can't enumerate the properties of a Scope"); }
157 public void declare(String s) { if (isTransparent()) getParentScope().declare(s); else super.put(s, NULL);}