1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
9 /** The minimum set of functionality required for objects which are manipulated by JavaScript */
10 public class JS extends org.xwt.util.BalancedTree {
12 // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
14 public static int getLine() {
15 Interpreter c = Interpreter.current();
16 return c.f == null || c.pc < 0 || c.pc >= c.f.size ? -1 : c.f.line[c.pc];
19 public static String getSourceName() {
20 Interpreter c = Interpreter.current();
21 return c == null || c.f == null ? null : c.f.sourceName;
24 public static class PausedException extends Exception { PausedException() { } }
26 public static void invokePauseable(JSFunction function) throws JS.PausedException {
27 Interpreter i = new Interpreter(function, true, new JSArray());
28 int oldpausecount = i.pausecount;
30 if (i.pausecount > oldpausecount) throw new PausedException();
33 public static class NotPauseableException extends Exception { NotPauseableException() { } }
35 /** returns a callback which will restart the context; expects a value to be pushed onto the stack when unpaused */
36 public static UnpauseCallback pause() throws NotPauseableException {
37 Interpreter i = Interpreter.current();
38 if (i.pausecount == -1) throw new NotPauseableException();
40 return new JS.UnpauseCallback(i);
43 public static class UnpauseCallback {
45 UnpauseCallback(Interpreter i) { this.i = i; }
46 public void unpause(Object o) throws PausedException {
54 // Static Helper Methods ///////////////////////////////////////////////////////////////////////////////////
56 /** coerce an object to a Boolean */
57 public static boolean toBoolean(Object o) {
58 if (o == null) return false;
59 if (o instanceof Boolean) return ((Boolean)o).booleanValue();
60 if (o instanceof Long) return ((Long)o).longValue() != 0;
61 if (o instanceof Integer) return ((Integer)o).intValue() != 0;
62 if (o instanceof Number) {
63 double d = ((Number) o).doubleValue();
64 // NOTE: d == d is a test for NaN. It should be faster than Double.isNaN()
65 return d != 0.0 && d == d;
67 if (o instanceof String) return ((String)o).length() != 0;
71 /** coerce an object to a Long */
72 public static long toLong(Object o) { return toNumber(o).longValue(); }
74 /** coerce an object to an Int */
75 public static int toInt(Object o) { return toNumber(o).intValue(); }
77 /** coerce an object to a Double */
78 public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
80 /** coerce an object to a Number */
81 public static Number toNumber(Object o) {
82 if (o == null) return ZERO;
83 if (o instanceof Number) return ((Number)o);
85 // NOTE: There are about 3 pages of rules in ecma262 about string to number conversions
86 // We aren't even close to following all those rules. We probably never will be.
87 if (o instanceof String)
88 try { return N((String)o); } catch (NumberFormatException e) { return N(Double.NaN); }
89 if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? N(1) : ZERO;
90 if (o instanceof JS) return ((JS)o).coerceToNumber();
91 throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle");
94 /** coerce an object to a String */
95 public static String toString(Object o) {
96 if(o == null) return "null";
97 if(o instanceof String) return (String) o;
98 if(o instanceof Integer || o instanceof Long || o instanceof Boolean) return o.toString();
99 if(o instanceof JS) return ((JS)o).coerceToString();
100 if(o instanceof Double || o instanceof Float) {
101 double d = ((Number)o).doubleValue();
102 if((int)d == d) return Integer.toString((int)d);
109 // Instance Methods ////////////////////////////////////////////////////////////////////
111 public static final Integer ZERO = new Integer(0);
113 // this gets around a wierd fluke in the Java type checking rules for ?..:
114 public static final Object T = Boolean.TRUE;
115 public static final Object F = Boolean.FALSE;
117 public static final Boolean B(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; }
118 public static final Boolean B(int i) { return i==0 ? Boolean.FALSE : Boolean.TRUE; }
119 public static final Number N(String s) { return s.indexOf('.') == -1 ? N(Integer.parseInt(s)) : new Double(s); }
120 public static final Number N(double d) { return (int)d == d ? N((int)d) : new Double(d); }
121 public static final Number N(long l) { return N((int)l); }
123 private static final Integer[] smallIntCache = new Integer[65535 / 4];
124 private static final Integer[] largeIntCache = new Integer[65535 / 4];
125 public static final Number N(int i) {
127 if (i < smallIntCache.length) { ret = smallIntCache[i]; if (ret != null) return ret; }
128 else ret = largeIntCache[i % largeIntCache.length];
129 if (ret == null || ret.intValue() != i) {
130 ret = new Integer(i);
131 if (i < smallIntCache.length) smallIntCache[i] = ret;
132 else largeIntCache[i % largeIntCache.length] = ret;
137 private static Enumeration emptyEnumeration = new Enumeration() {
138 public boolean hasMoreElements() { return false; }
139 public Object nextElement() { throw new NoSuchElementException(); }
142 private Hash entries = null;
143 public Enumeration keys() { return entries == null ? emptyEnumeration : entries.keys(); }
144 public Object get(Object key) { return entries == null ? null : entries.get(key, null); }
145 public void put(Object key, Object val) { if (entries == null) entries = new Hash(); entries.put(key, null, val); }
148 // Trap support //////////////////////////////////////////////////////////////////////////////
150 /** override and return true to allow placing traps on this object */
151 protected boolean isTrappable() { return false; }
153 /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
154 public final void putAndTriggerTraps(Object key, Object value) {
155 Trap t = getTrap(key);
156 if (t != null) t.invoke(key, value);
157 else put(key, value);
160 /** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */
161 public final Object getAndTriggerTraps(Object key) {
162 Trap t = getTrap(key);
163 if (t != null) return t.invoke(key);
164 else return get(key);
167 /** retrieve a trap from the entries hash */
168 protected final Trap getTrap(Object key) {
169 return !isTrappable() || entries == null ? null : (Trap)entries.get(key, Trap.class);
172 /** retrieve a trap from the entries hash */
173 protected final void putTrap(Object key, Trap value) {
174 if (!isTrappable()) return;
175 if (entries == null) entries = new Hash();
176 entries.put(key, Trap.class, value);
179 /** adds a trap, avoiding duplicates */
180 protected final void addTrap(Object name, JSFunction f) {
181 for(Trap t = getTrap(name); t != null; t = t.next) if (t.f == f) return;
182 putTrap(name, new Trap(this, name.toString(), f, (Trap)getTrap(name)));
185 /** deletes a trap, if present */
186 protected final void delTrap(Object name, JSFunction f) {
187 Trap t = (Trap)getTrap(name);
188 if (t == null) return;
189 if (t.f == f) { putTrap(t.name, t.next); return; }
190 for(; t.next != null; t = t.next) if (t.next.f == f) { t.next = t.next.next; return; }
194 // Call Support //////////////////////////////////////////////////////////////////////////////
196 // return this from get() if the key was actually a method.
197 public static final Object METHOD = new Object();
198 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
199 throw new JSExn("attempted to call the null value (method "+method+")");
201 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
202 throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
206 // Typing Support //////////////////////////////////////////////////////////////////////////////
208 public Number coerceToNumber() { throw new JSExn("tried to coerce a JavaScript object to a Number"); }
209 public String coerceToString() { throw new JSExn("tried to coerce a JavaScript object to a String"); }
210 public boolean coerceToBoolean() { throw new JSExn("tried to coerce a JavaScript object to a Boolean"); }
212 public String typeName() { return "object"; }