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 */
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 Number N(int i) { return new Integer(i); }
118 public static final Number N(long l) { return new Long(l); }
119 public static final Number N(double d) { return new Double(d); }
120 public static final Number N(String s) { return new Double(s); }
121 public static final Boolean B(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; }
122 public static final Boolean B(int i) { return i==0 ? Boolean.FALSE : Boolean.TRUE; }
124 private static Enumeration emptyEnumeration = new Enumeration() {
125 public boolean hasMoreElements() { return false; }
126 public Object nextElement() { throw new NoSuchElementException(); }
129 private Hash entries = null;
130 public Enumeration keys() { return entries == null ? emptyEnumeration : entries.keys(); }
131 public Object get(Object key) { return entries == null ? null : entries.get(key, null); }
132 public void put(Object key, Object val) { if (entries == null) entries = new Hash(); entries.put(key, null, val); }
135 // Trap support //////////////////////////////////////////////////////////////////////////////
137 /** override and return true to allow placing traps on this object */
138 protected boolean isTrappable() { return false; }
140 /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
141 public final void putAndTriggerTraps(Object key, Object value) {
142 Trap t = getTrap(key);
143 if (t != null) t.invoke(key, value);
144 else put(key, value);
147 /** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */
148 public final Object getAndTriggerTraps(Object key) {
149 Trap t = getTrap(key);
150 if (t != null) return t.invoke(key);
151 else return get(key);
154 /** retrieve a trap from the entries hash */
155 protected final Trap getTrap(Object key) {
156 return !isTrappable() || entries == null ? null : (Trap)entries.get(key, Trap.class);
159 /** retrieve a trap from the entries hash */
160 protected final void putTrap(Object key, Trap value) {
161 if (!isTrappable()) return;
162 if (entries == null) entries = new Hash();
163 entries.put(key, Trap.class, value);
166 /** adds a trap, avoiding duplicates */
167 protected final void addTrap(Object name, JSFunction f) {
168 for(Trap t = getTrap(name); t != null; t = t.next) if (t.f == f) return;
169 putTrap(name, new Trap(this, name.toString(), f, (Trap)getTrap(name)));
172 /** deletes a trap, if present */
173 protected final void delTrap(Object name, JSFunction f) {
174 Trap t = (Trap)getTrap(name);
175 if (t == null) return;
176 if (t.f == f) { putTrap(t.name, t.next); return; }
177 for(; t.next != null; t = t.next) if (t.next.f == f) { t.next = t.next.next; return; }
181 // Call Support //////////////////////////////////////////////////////////////////////////////
183 // return this from get() if the key was actually a method.
184 public static final Object METHOD = new Object();
185 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
186 throw new JSExn("attempted to call the null value (method "+method+")");
188 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
189 throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
193 // Typing Support //////////////////////////////////////////////////////////////////////////////
195 public Number coerceToNumber() { throw new JSExn("tried to coerce a JavaScript object to a Number"); }
196 public String coerceToString() { throw new JSExn("tried to coerce a JavaScript object to a String"); }
197 public boolean coerceToBoolean() { throw new JSExn("tried to coerce a JavaScript object to a Boolean"); }
199 public String typeName() { return "object"; }