7c2cc19d2e23a1fdbdb9f34fdd38c26db7633fa6
[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
30     // Instance Methods ////////////////////////////////////////////////////////////////////
31  
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(); 
35
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"); }
39
40
41     // Subclasses /////////////////////////////////////////////////////////////////////////
42
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()); }
52     }
53
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; } 
60     } 
61
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;
69             }
70             if (!(o instanceof String)) return Integer.MIN_VALUE;
71             String s = (String)o;
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);
74         }
75         public Object get(Object key) {
76             // FIXME: HACK!
77             if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
78             if (key.equals("length")) return new Long(vec.size());
79             int i = intVal(key);
80             if (i == Integer.MIN_VALUE) return super.get(key);
81             return vec.elementAt(i);
82         }
83         public void put(Object key, Object val) {
84             if (key.equals("length")) vec.setSize(Parser.toNumber(val).intValue());
85             int i = intVal(key);
86             if (i == Integer.MIN_VALUE) super.put(key, val);
87             else vec.setElementAt(val, i);
88         }
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";
95             return ret;
96         }
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); }
101     }
102
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); }
108     }
109
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);
115             try {
116                 e.eval(rootScope);
117             } catch (Parser.ReturnException e) {
118                 // ignore
119             } catch (Parser.ControlTransferException e) {
120                 throw new JS.Exn("block threw a ControlTransferException: " + e);
121             }
122             return null;
123         }
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
127             try {
128                 return new Script(p.parseBlock(true));
129             } catch (Exception e) {
130                 if (Log.on) Log.log(Parser.class, e);
131                 return null;
132             }
133         }
134     }
135
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; }
143
144         // transparent scopes are not returned by THIS
145         public boolean isTransparent() { return false; }
146
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;
151         }
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);
155         }
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);}
158     } 
159  
160
161
162
163
164
165