1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
10 /** The minimum set of functionality required for objects which are manipulated by JavaScript */
13 // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
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];
20 public static String getSourceName() {
21 Interpreter c = Interpreter.current();
22 return c.f == null ? null : c.f.sourceName;
25 public static class PausedException extends Exception { PausedException() { } }
27 public static void invokePauseable(JSFunction function) throws JS.PausedException {
28 Interpreter i = new Interpreter(function, true, new JSArray());
29 int oldpausecount = i.pausecount;
31 if (i.pausecount > oldpausecount) throw new PausedException();
34 public static class NotPauseableException extends Exception { NotPauseableException() { } }
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();
41 return new JS.UnpauseCallback(i);
44 public static class UnpauseCallback {
46 UnpauseCallback(Interpreter i) { this.i = i; }
47 public void unpause(Object o) throws PausedException {
55 // Static Helper Methods ///////////////////////////////////////////////////////////////////////////////////
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;
68 if (o instanceof String) return ((String)o).length() != 0;
72 /** coerce an object to a Long */
73 public static long toLong(Object o) { return toNumber(o).longValue(); }
75 /** coerce an object to an Int */
76 public static int toInt(Object o) { return toNumber(o).intValue(); }
78 /** coerce an object to a Double */
79 public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
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);
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");
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);
110 // Instance Methods ////////////////////////////////////////////////////////////////////
112 public static final Integer ZERO = new Integer(0);
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;
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; }
126 private static Enumeration emptyEnumeration = new Enumeration() {
127 public boolean hasMoreElements() { return false; }
128 public Object nextElement() { throw new NoSuchElementException(); }
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); }
137 // Trap support //////////////////////////////////////////////////////////////////////////////
139 /** override and return true to allow placing traps on this object */
140 protected boolean isTrappable() { return false; }
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);
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);
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);
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);
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)));
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; }
183 // Call Support //////////////////////////////////////////////////////////////////////////////
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+")");
190 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
191 throw new JSExn("you cannot call this object)");
195 // Typing Support //////////////////////////////////////////////////////////////////////////////
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"); }
201 public String typeName() { return "object"; }