2003/11/18 10:47:26
[org.ibex.core.git] / src / org / xwt / js / JS.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] 
2 package org.xwt.js; 
3
4 import org.xwt.util.*; 
5 import org.xwt.*; 
6 import java.io.*;
7 import java.util.*;
8
9 // FIXME: grafts
10 /** The minimum set of functionality required for objects which are manipulated by JavaScript */
11 public class JS { 
12
13     // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
14
15     public static int getLine() {
16         Interpreter c = Interpreter.current();
17         return c.f == null || c.pc < 0 || c.pc >= c.f.size ? -1 : c.f.line[c.pc];
18     }
19
20     public static String getSourceName() {
21         Interpreter c = Interpreter.current();
22         return c.f == null ? null : c.f.sourceName;
23     } 
24
25     public static class PausedException extends Exception { PausedException() { } }
26
27     public static void invokePauseable(JSFunction function) throws JS.PausedException {
28         Interpreter i = new Interpreter(function, true, new JSArray());
29         int oldpausecount = i.pausecount;
30         i.resume();
31         if (i.pausecount > oldpausecount) throw new PausedException();
32     }
33
34     public static class NotPauseableException extends Exception { NotPauseableException() { } }
35
36     /** returns a callback which will restart the context; expects a value to be pushed onto the stack when unpaused */
37     public static UnpauseCallback pause() throws NotPauseableException {
38         Interpreter i = Interpreter.current();
39         if (i.pausecount == -1) throw new NotPauseableException();
40         i.pausecount++;
41         return new JS.UnpauseCallback(i);
42     }
43
44     public static class UnpauseCallback {
45         Interpreter i;
46         UnpauseCallback(Interpreter i) { this.i = i; }
47         public void unpause(Object o) throws PausedException {
48             i.stack.push(o);
49             i.resume();
50         }
51     }
52
53
54
55     // Static Helper Methods ///////////////////////////////////////////////////////////////////////////////////
56
57     /** coerce an object to a Boolean */
58     public static boolean toBoolean(Object o) {
59         if (o == null) return false;
60         if (o instanceof Boolean) return ((Boolean)o).booleanValue();
61         if (o instanceof Long) return ((Long)o).longValue() != 0;
62         if (o instanceof Integer) return ((Integer)o).intValue() != 0;
63         if (o instanceof Number) {
64             double d = ((Number) o).doubleValue();
65             // NOTE: d == d is a test for NaN. It should be faster than Double.isNaN()
66             return d != 0.0 && d == d;
67         }
68         if (o instanceof String) return ((String)o).length() != 0;
69         return true;
70     }
71
72     /** coerce an object to a Long */
73     public static long toLong(Object o) { return toNumber(o).longValue(); }
74
75     /** coerce an object to an Int */
76     public static int toInt(Object o) { return toNumber(o).intValue(); }
77
78     /** coerce an object to a Double */
79     public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
80
81     /** coerce an object to a Number */
82     public static Number toNumber(Object o) {
83         if (o == null) return ZERO;
84         if (o instanceof Number) return ((Number)o);
85
86         // NOTE: There are about 3 pages of rules in ecma262 about string to number conversions
87         //       We aren't even close to following all those rules.  We probably never will be.
88         if (o instanceof String)
89             try { return N((String)o); } catch (NumberFormatException e) { return N(Double.NaN); }
90         if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? N(1) : ZERO;
91         if (o instanceof JS) return ((JS)o).coerceToNumber();
92         throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle");
93     }
94
95     /** coerce an object to a String */
96     public static String toString(Object o) {
97         if(o == null) return "null";
98         if(o instanceof String) return (String) o;
99         if(o instanceof Integer || o instanceof Long || o instanceof Boolean) return o.toString();
100         if(o instanceof JS) return ((JS)o).coerceToString();
101         if(o instanceof Double || o instanceof Float) {
102             double d = ((Number)o).doubleValue();
103             if((int)d == d) return Integer.toString((int)d);
104             return o.toString();
105         }
106         return o.toString();
107     }
108
109     
110     // Instance Methods ////////////////////////////////////////////////////////////////////
111
112     public static final Integer ZERO = new Integer(0);
113  
114     // this gets around a wierd fluke in the Java type checking rules for ?..:
115     public static final Object T = Boolean.TRUE;
116     public static final Object F = Boolean.FALSE;
117
118     // FIXME: be much smarter here
119     public static final Number N(int i) { return new Integer(i); }
120     public static final Number N(long l) { return new Long(l); }
121     public static final Number N(double d) { return new Double(d); }
122     public static final Number N(String s) { return new Double(s); }
123     public static final Boolean B(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; }
124     public static final Boolean B(int i) { return i==0 ? Boolean.FALSE : Boolean.TRUE; }
125     
126     private static Enumeration emptyEnumeration = new Enumeration() {
127             public boolean hasMoreElements() { return false; }
128             public Object nextElement() { throw new NoSuchElementException(); }
129         };
130     
131     private Hash entries = null;
132     public Enumeration keys() { return entries == null ? emptyEnumeration : entries.keys(); }
133     public Object get(Object key) { return entries == null ? null : entries.get(key, null); }
134     public void put(Object key, Object val) { if (entries == null) entries = new Hash(); entries.put(key, null, val); }
135
136
137     // Trap support //////////////////////////////////////////////////////////////////////////////
138
139     /** override and return true to allow placing traps on this object */
140     protected boolean isTrappable() { return false; }
141
142     /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
143     public final void putAndTriggerTraps(Object key, Object value) {
144         Trap t = getTrap(key);
145         if (t != null) t.invoke(key, value);
146         else put(key, value);
147     }
148
149     /** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */
150     public final Object getAndTriggerTraps(Object key) {
151         Trap t = getTrap(key);
152         if (t != null) return t.invoke(key);
153         else return get(key);
154     }
155
156     /** retrieve a trap from the entries hash */
157     protected final Trap getTrap(Object key) {
158         return !isTrappable() || entries == null ? null : (Trap)entries.get(key, Trap.class);
159     }
160
161     /** retrieve a trap from the entries hash */
162     protected final void putTrap(Object key, Trap value) {
163         if (!isTrappable()) return;
164         if (entries == null) entries = new Hash();
165         entries.put(key, Trap.class, value);
166     }
167
168     /** adds a trap, avoiding duplicates */
169     protected final void addTrap(Object name, JSFunction f) {
170         for(Trap t = getTrap(name); t != null; t = t.next) if (t.f == f) return;
171         putTrap(name, new Trap(this, name.toString(), f, (Trap)getTrap(name)));
172     }
173
174     /** deletes a trap, if present */
175     protected final void delTrap(Object name, JSFunction f) {
176         Trap t = (Trap)getTrap(name);
177         if (t == null) return;
178         if (t.f == f) { putTrap(t.name, t.next); return; }
179         for(; t.next != null; t = t.next) if (t.next.f == f) { t.next = t.next.next; return; }
180     }
181
182
183     // Call Support //////////////////////////////////////////////////////////////////////////////
184
185     // return this from get() if the key was actually a method.
186     public static final Object METHOD = new Object();
187     public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
188         throw new JSExn("attempted to call the null value (method "+method+")");
189     }
190     public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
191         throw new JSExn("you cannot call this object)");
192     }
193
194
195     // Typing Support //////////////////////////////////////////////////////////////////////////////
196
197     public Number coerceToNumber() { throw new JSExn("tried to coerce a JavaScript object to a Number"); }
198     public String coerceToString() { throw new JSExn("tried to coerce a JavaScript object to a String"); }
199     public boolean coerceToBoolean() { throw new JSExn("tried to coerce a JavaScript object to a Boolean"); }
200
201     public String typeName() { return "object"; }
202
203