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 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() throws JSExn {
144 return entries == null ? emptyEnumeration : entries.keys();
146 public Object get(Object key) throws JSExn { return entries == null ? null : entries.get(key, null); }
147 public void put(Object key, Object val) throws JSExn {
148 if (entries == null) entries = new Hash();
149 entries.put(key, null, val);
153 // Trap support //////////////////////////////////////////////////////////////////////////////
155 /** override and return true to allow placing traps on this object */
156 protected boolean isTrappable() { return false; }
158 /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
159 public void putAndTriggerTraps(Object key, Object value) throws JSExn {
160 Trap t = getTrap(key);
161 if (t != null) t.invoke(key, value);
162 else put(key, value);
165 /** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */
166 public Object getAndTriggerTraps(Object key) throws JSExn {
167 Trap t = getTrap(key);
168 if (t != null) return t.invoke(key);
169 else return get(key);
172 /** retrieve a trap from the entries hash */
173 protected final Trap getTrap(Object key) {
174 return !isTrappable() || entries == null ? null : (Trap)entries.get(key, Trap.class);
177 /** retrieve a trap from the entries hash */
178 protected final void putTrap(Object key, Trap value) {
179 if (!isTrappable()) return;
180 if (entries == null) entries = new Hash();
181 entries.put(key, Trap.class, value);
184 /** adds a trap, avoiding duplicates */
185 protected final void addTrap(Object name, JSFunction f) {
186 for(Trap t = getTrap(name); t != null; t = t.next) if (t.f == f) return;
187 putTrap(name, new Trap(this, name.toString(), f, (Trap)getTrap(name)));
190 /** deletes a trap, if present */
191 protected final void delTrap(Object name, JSFunction f) {
192 Trap t = (Trap)getTrap(name);
193 if (t == null) return;
194 if (t.f == f) { putTrap(t.name, t.next); return; }
195 for(; t.next != null; t = t.next) if (t.next.f == f) { t.next = t.next.next; return; }
199 // Call Support //////////////////////////////////////////////////////////////////////////////
201 // return this from get() if the key was actually a method.
202 public static final Object METHOD = new Object();
203 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
204 throw new JSExn("attempted to call the null value (method "+method+")");
206 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
207 throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
211 // Typing Support //////////////////////////////////////////////////////////////////////////////
213 public Number coerceToNumber() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Number"); }
214 public String coerceToString() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a String"); }
215 public boolean coerceToBoolean() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Boolean"); }
217 public String typeName() { return "object"; }