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, JSExn {
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, JSExn {
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 int idx = i + smallIntCache.length / 2;
128 if (idx < smallIntCache.length && idx > 0) {
129 ret = smallIntCache[idx];
130 if (ret != null) return ret;
132 else ret = largeIntCache[Math.abs(idx % largeIntCache.length)];
133 if (ret == null || ret.intValue() != i) {
134 ret = new Integer(i);
135 if (idx < smallIntCache.length && idx > 0) smallIntCache[idx] = ret;
136 else largeIntCache[Math.abs(idx % largeIntCache.length)] = ret;
141 private static Enumeration emptyEnumeration = new Enumeration() {
142 public boolean hasMoreElements() { return false; }
143 public Object nextElement() { throw new NoSuchElementException(); }
146 private Hash entries = null;
147 public Enumeration keys() throws JSExn {
148 return entries == null ? emptyEnumeration : entries.keys();
150 public Object get(Object key) throws JSExn { return entries == null ? null : entries.get(key, null); }
151 public void put(Object key, Object val) throws JSExn {
152 if (entries == null) entries = new Hash();
153 entries.put(key, null, val);
157 // Trap support //////////////////////////////////////////////////////////////////////////////
159 /** override and return true to allow placing traps on this object */
160 protected boolean isTrappable() { return false; }
162 /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
163 public void putAndTriggerTraps(Object key, Object value) throws JSExn {
164 Trap t = getTrap(key);
165 if (t != null) t.invoke(key, value);
166 else put(key, value);
169 /** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */
170 public Object getAndTriggerTraps(Object key) throws JSExn {
171 Trap t = getTrap(key);
172 if (t != null) return t.invoke(key);
173 else return get(key);
176 /** retrieve a trap from the entries hash */
177 protected final Trap getTrap(Object key) {
178 return !isTrappable() || entries == null ? null : (Trap)entries.get(key, Trap.class);
181 /** retrieve a trap from the entries hash */
182 protected final void putTrap(Object key, Trap value) {
183 if (!isTrappable()) return;
184 if (entries == null) entries = new Hash();
185 entries.put(key, Trap.class, value);
188 /** adds a trap, avoiding duplicates */
189 protected final void addTrap(Object name, JSFunction f) {
190 for(Trap t = getTrap(name); t != null; t = t.next) if (t.f == f) return;
191 putTrap(name, new Trap(this, name.toString(), f, (Trap)getTrap(name)));
194 /** deletes a trap, if present */
195 protected final void delTrap(Object name, JSFunction f) {
196 Trap t = (Trap)getTrap(name);
197 if (t == null) return;
198 if (t.f == f) { putTrap(t.name, t.next); return; }
199 for(; t.next != null; t = t.next) if (t.next.f == f) { t.next = t.next.next; return; }
203 // Call Support //////////////////////////////////////////////////////////////////////////////
205 // return this from get() if the key was actually a method.
206 public static final Object METHOD = new Object();
207 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
208 throw new JSExn("attempted to call the null value (method "+method+")");
210 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
211 throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
215 // Typing Support //////////////////////////////////////////////////////////////////////////////
217 public Number coerceToNumber() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Number"); }
218 public String coerceToString() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a String"); }
219 public boolean coerceToBoolean() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Boolean"); }
221 public String typeName() { return "object"; }