package org.ibex.js;
import org.ibex.util.*;
-import org.ibex.*;
import java.io.*;
import java.util.*;
/** The minimum set of functionality required for objects which are manipulated by JavaScript */
-public class JS extends org.ibex.util.BalancedTree {
+public abstract class JS {
public static boolean checkAssertions = false;
- public static final Object METHOD = new Object();
+ public static final JS METHOD = new JS() { };
public final JS unclone() { return _unclone(); }
- public Enumeration keys() throws JSExn { return entries == null ? emptyEnumeration : entries.keys(); }
- public Object get(Object key) throws JSExn { return entries == null ? null : entries.get(key, null); }
- public void put(Object key, Object val) throws JSExn { (entries==null?entries=new Hash():entries).put(key,null,val); }
- public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
- throw new JSExn("attempted to call the null value (method "+method+")");
- }
- public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
+ public final JS jsclone() throws JSExn { return new Clone(this); }
+
+ // FEATURE: JSEnumeration
+ public Enumeration keys() throws JSExn { throw new JSExn("you can't enumerate the keys of this object (class=" + getClass().getName() +")"); }
+ public JS get(JS key) throws JSExn { return null; }
+ public void put(JS key, JS val) throws JSExn { throw new JSExn("" + key + " is read only (class=" + getClass().getName() +")"); }
+
+ public InputStream getInputStream() throws IOException {
+ throw new IOException("this object doesn't have a stream associated with it " + getClass().getName() + ")");
+ }
+
+ public final boolean hasTrap(JS key) { return getTrap(key) != null; }
+
+ public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
+ throw new JSExn("method not found (" + JS.debugToString(method) + ")");
+ }
+
+ // FIXME: JSArgs objects, pointers into stack frame
+ public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
}
-
- JS _unclone() { return this; }
- public static class Cloneable extends JS {
- public Object jsclone() throws JSExn {
- return new Clone(this);
+
+ public final boolean equals(Object o) { return this == o || ((o instanceof JS) && jsequals((JS)o)); }
+
+ // FIXME: Remove this, eventually
+ public final String toString() { throw new Error("you shouldn't use toString() on JS objects"); }
+
+ // These are only used internally by org.ibex.js
+ Trap getTrap(JS key) { return null; }
+ void putTrap(JS key, Trap value) throws JSExn { throw new JSExn("traps cannot be placed on this object (class=" + this.getClass().getName() +")"); }
+ String coerceToString() throws JSExn { throw new JSExn("can't coerce to a string (class=" + getClass().getName() +")"); }
+ boolean jsequals(JS o) { return this == o; }
+
+ public static class O extends JS {
+ private Hash entries;
+
+ public Enumeration keys() throws JSExn { return entries == null ? emptyEnumeration : entries.keys(); }
+ public JS get(JS key) throws JSExn { return entries == null ? null : (JS)entries.get(key, null); }
+ public void put(JS key, JS val) throws JSExn { (entries==null?entries=new Hash():entries).put(key,null,val); }
+
+ /** retrieve a trap from the entries hash */
+ final Trap getTrap(JS key) {
+ return entries == null ? null : (Trap)entries.get(key, Trap.class);
}
+
+ /** retrieve a trap from the entries hash */
+ final void putTrap(JS key, Trap value) {
+ if (entries == null) entries = new Hash();
+ entries.put(key, Trap.class, value);
+ }
}
-
- public static class Clone extends JS.Cloneable {
- protected JS.Cloneable clonee = null;
+
+ public static class BT extends O {
+ private BalancedTree bt;
+ private final BalancedTree bt() { if(bt != null) return bt; return bt = new BalancedTree(); }
+ public final void insertNode(int index, Object o) { bt().insertNode(index,o); }
+ public final void clear() { bt().clear(); }
+ public final Object getNode(int i) { return bt().getNode(i); }
+ public final int treeSize() { return bt().treeSize(); }
+ public final Object deleteNode(int i) { return bt().deleteNode(i); }
+ public final void replaceNode(int index, Object o) { bt().replaceNode(index,o); }
+ public final int indexNode(Object o) { return bt().indexNode(o); }
+ }
+
+ // FEATURE: JS.StringKeys
+ /* public static StringKeys extends JS {
+ public JS get(JS key) { return JS.isString(key) ? get(JS.toString(key) : null; }
+ ...
+ */
+
+ JS _unclone() { return this; }
+
+ public interface Cloneable { }
+
+ public static class Clone extends JS.O implements Cloneable {
+ protected final JS clonee;
JS _unclone() { return clonee.unclone(); }
- public JS.Cloneable getClonee() { return clonee; }
- public Clone(JS.Cloneable clonee) { this.clonee = clonee; }
- public boolean equals(Object o) {
- if (!(o instanceof JS)) return false;
- return unclone() == ((JS)o).unclone();
+ public JS getClonee() { return clonee; }
+ public Clone(JS clonee) throws JSExn {
+ if(!(clonee instanceof Cloneable)) throw new JSExn("" + clonee.getClass().getName() + " isn't cloneable");
+ this.clonee = clonee;
}
+ public boolean jsequals(JS o) { return unclone().jsequals(o.unclone()); }
public Enumeration keys() throws JSExn { return clonee.keys(); }
- public Object get(Object key) throws JSExn { return clonee.get(key); }
- public void put(Object key, Object val) throws JSExn { clonee.put(key, val); }
- public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
+ public JS get(JS key) throws JSExn { return clonee.get(key); }
+ public void put(JS key, JS val) throws JSExn { clonee.put(key, val); }
+ public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
return clonee.callMethod(method, a0, a1, a2, rest, nargs);
- }
- public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
+ }
+ public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
return clonee.call(a0, a1, a2, rest, nargs);
}
+ public InputStream getInputStream() throws IOException { return clonee.getInputStream(); }
}
-
+
// Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
/** log a message with the current JavaScript sourceName/line */
public static UnpauseCallback pause() throws NotPauseableException {
Interpreter i = Interpreter.current();
if (i.pausecount == -1) throw new NotPauseableException();
+ boolean get;
+ switch(i.f.op[i.pc]) {
+ case Tokens.RETURN: case ByteCodes.PUT: get = false; break;
+ case ByteCodes.GET: get = true; break;
+ default: throw new Error("should never happen");
+ }
i.pausecount++;
- return new JS.UnpauseCallback(i);
+ return new JS.UnpauseCallback(i,get);
}
- public static class UnpauseCallback implements Scheduler.Task {
- Interpreter i;
- UnpauseCallback(Interpreter i) { this.i = i; }
- public void perform() throws JSExn { unpause(null); }
- public void unpause(Object o) throws JSExn {
- // FIXME: if o instanceof JSExn, throw it into the JSworld
- i.stack.push(o);
- i.resume();
+ public static class UnpauseCallback implements Task {
+ private Interpreter i;
+ private boolean get;
+ UnpauseCallback(Interpreter i, boolean get) { this.i = i; this.get = get; }
+ public void perform() throws JSExn { unpause(); }
+ public JS unpause() throws JSExn { return unpause((JS)null); }
+ public JS unpause(JS o) throws JSExn {
+ if (o == JS.METHOD) throw new JSExn("can't return a method to a paused context");
+ if(get) i.stack.push(o);
+ return i.resume();
+ }
+ public JS unpause(JSExn e) throws JSExn {
+ i.catchException(e);
+ return i.resume();
}
}
// Static Helper Methods ///////////////////////////////////////////////////////////////////////////////////
/** coerce an object to a Boolean */
- public static boolean toBoolean(Object o) {
- if (o == null) return false;
- if (o instanceof Boolean) return ((Boolean)o).booleanValue();
- if (o instanceof Long) return ((Long)o).longValue() != 0;
- if (o instanceof Integer) return ((Integer)o).intValue() != 0;
- if (o instanceof Number) {
- double d = ((Number) o).doubleValue();
- // NOTE: d == d is a test for NaN. It should be faster than Double.isNaN()
- return d != 0.0 && d == d;
- }
- if (o instanceof String) return ((String)o).length() != 0;
+ public static boolean toBoolean(JS o) {
+ if(o == null) return false;
+ if(o instanceof JSNumber) return ((JSNumber)o).toBoolean();
+ if(o instanceof JSString) return ((JSString)o).length() != 0;
return true;
}
-
- /** coerce an object to a Long */
- public static long toLong(Object o) { return toNumber(o).longValue(); }
-
- /** coerce an object to an Int */
- public static int toInt(Object o) { return toNumber(o).intValue(); }
-
- /** coerce an object to a Double */
- public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
-
+ //#repeat long/int/double/float toLong/toInt/toDouble/toFloat Long/Integer/Double/Float parseLong/parseInt/parseDouble/parseFloat
/** coerce an object to a Number */
- public static Number toNumber(Object o) {
- if (o == null) return ZERO;
- if (o instanceof Number) return ((Number)o);
-
- // NOTE: There are about 3 pages of rules in ecma262 about string to number conversions
- // We aren't even close to following all those rules. We probably never will be.
- if (o instanceof String) try { return N((String)o); } catch (NumberFormatException e) { return N(Double.NaN); }
- if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? N(1) : ZERO;
- throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle");
+ public static long toLong(JS o) throws JSExn {
+ if(o == null) return 0;
+ if(o instanceof JSNumber) return ((JSNumber)o).toLong();
+ if(o instanceof JSString) return Long.parseLong(o.coerceToString());
+ throw new JSExn("can't coerce a " + o.getClass().getName() + " to a number");
}
-
- /** coerce an object to a String */
- public static String toString(Object o) {
+ //#end
+
+ public static String toString(JS o) throws JSExn {
if(o == null) return "null";
- if(o instanceof String) return (String) o;
- if(o instanceof Integer || o instanceof Long || o instanceof Boolean) return o.toString();
- if(o instanceof JSArray) return o.toString();
- if(o instanceof JSDate) return o.toString();
- if(o instanceof Double || o instanceof Float) {
- double d = ((Number)o).doubleValue();
- if((int)d == d) return Integer.toString((int)d);
- return o.toString();
+ return o.coerceToString();
+ }
+
+ public static String debugToString(JS o) {
+ try { return toString(o); }
+ catch(JSExn e) { return "[class=" + o.getClass().getName() + "]"; }
+ }
+
+ public static boolean isInt(JS o) {
+ if(o == null) return true;
+ if(o instanceof JSNumber.I) return true;
+ if(o instanceof JSNumber.B) return false;
+ if(o instanceof JSNumber) {
+ JSNumber n = (JSNumber) o;
+ return n.toInt() == n.toDouble();
}
- throw new RuntimeException("can't coerce "+o+" [" + o.getClass().getName() + "]");
+ if(o instanceof JSString) {
+ String s = ((JSString)o).s;
+ for(int i=0;i<s.length();i++)
+ if(s.charAt(i) < '0' || s.charAt(i) > '9') return false;
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean isString(JS o) {
+ if(o instanceof JSString) return true;
+ return false;
}
// Instance Methods ////////////////////////////////////////////////////////////////////
-
- public static final Integer ZERO = new Integer(0);
- // this gets around a wierd fluke in the Java type checking rules for ?..:
- public static final Object T = Boolean.TRUE;
- public static final Object F = Boolean.FALSE;
-
- public static final Boolean B(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; }
- public static final Boolean B(int i) { return i==0 ? Boolean.FALSE : Boolean.TRUE; }
- public static final Number N(String s) { return s.indexOf('.') == -1 ? N(Integer.parseInt(s)) : new Double(s); }
- public static final Number N(double d) { return (int)d == d ? N((int)d) : new Double(d); }
- public static final Number N(long l) { return N((int)l); }
-
- private static final Integer[] smallIntCache = new Integer[65535 / 4];
- private static final Integer[] largeIntCache = new Integer[65535 / 4];
- public static final Number N(int i) {
- Integer ret = null;
+ public final static JS NaN = new JSNumber.D(Double.NaN);
+ public final static JS ZERO = new JSNumber.I(0);
+ public final static JS ONE = new JSNumber.I(1);
+
+ public static final JS T = new JSNumber.B(true);
+ public static final JS F = new JSNumber.B(false);
+
+ public static final JS B(boolean b) { return b ? T : F; }
+ public static final JS B(int i) { return i==0 ? F : T; }
+
+ private static final int CACHE_SIZE = 65536 / 4; // must be a power of two
+ private static final JSString[] stringCache = new JSString[CACHE_SIZE];
+ public static final JS S(String s) {
+ if(s == null) return null;
+ int slot = s.hashCode()&(CACHE_SIZE-1);
+ JSString ret = stringCache[slot];
+ if(ret == null || !ret.s.equals(s)) stringCache[slot] = ret = new JSString(s);
+ return ret;
+ }
+
+ public static final JS N(double d) { return new JSNumber.D(d); }
+ public static final JS N(long l) { return new JSNumber.L(l); }
+
+ public static final JS N(Number n) {
+ if(n instanceof Integer) return N(n.intValue());
+ if(n instanceof Long) return N(n.longValue());
+ return N(n.doubleValue());
+ }
+
+ private static final JSNumber.I[] smallIntCache = new JSNumber.I[CACHE_SIZE];
+ private static final JSNumber.I[] largeIntCache = new JSNumber.I[CACHE_SIZE];
+ public static final JS N(int i) {
+ JSNumber.I ret = null;
int idx = i + smallIntCache.length / 2;
- if (idx < smallIntCache.length && idx > 0) {
+ if (idx < CACHE_SIZE && idx > 0) {
ret = smallIntCache[idx];
if (ret != null) return ret;
}
- else ret = largeIntCache[Math.abs(idx % largeIntCache.length)];
- if (ret == null || ret.intValue() != i) {
- ret = new Integer(i);
+ else ret = largeIntCache[Math.abs(idx % CACHE_SIZE)];
+ if (ret == null || ret.i != i) {
+ ret = new JSNumber.I(i);
if (idx < smallIntCache.length && idx > 0) smallIntCache[idx] = ret;
- else largeIntCache[Math.abs(idx % largeIntCache.length)] = ret;
+ else largeIntCache[Math.abs(idx % CACHE_SIZE)] = ret;
}
return ret;
}
public Object nextElement() { throw new NoSuchElementException(); }
};
- private Hash entries = null;
-
public static JS fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException {
return JSFunction._fromReader(sourceName, firstLine, sourceCode);
}
// Trap support //////////////////////////////////////////////////////////////////////////////
- /** override and return true to allow placing traps on this object.
- * if isRead true, this is a read trap, otherwise write trap
- **/
- protected boolean isTrappable(Object name, boolean isRead) { return true; }
-
/** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
- public void putAndTriggerTraps(Object key, Object value) throws JSExn {
+ public void putAndTriggerTraps(JS key, JS value) throws JSExn {
Trap t = getTrap(key);
if (t != null) t.invoke(value);
else put(key, value);
}
/** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */
- public Object getAndTriggerTraps(Object key) throws JSExn {
+ public JS getAndTriggerTraps(JS key) throws JSExn {
Trap t = getTrap(key);
if (t != null) return t.invoke();
else return get(key);
}
- /** retrieve a trap from the entries hash */
- protected final Trap getTrap(Object key) {
- return entries == null ? null : (Trap)entries.get(key, Trap.class);
- }
-
- /** retrieve a trap from the entries hash */
- protected final void putTrap(Object key, Trap value) {
- if (entries == null) entries = new Hash();
- entries.put(key, Trap.class, value);
- }
-
/** adds a trap, avoiding duplicates */
- protected final void addTrap(Object name, JSFunction f) throws JSExn {
+ final void addTrap(JS key, JSFunction f) throws JSExn {
if (f.numFormalArgs > 1) throw new JSExn("traps must take either one argument (write) or no arguments (read)");
boolean isRead = f.numFormalArgs == 0;
- if (!isTrappable(name, isRead)) throw new JSExn("not allowed "+(isRead?"read":"write")+" trap on property: "+name);
- for(Trap t = getTrap(name); t != null; t = t.next) if (t.f == f) return;
- putTrap(name, new Trap(this, name.toString(), f, (Trap)getTrap(name)));
+ for(Trap t = getTrap(key); t != null; t = t.next) if (t.f == f) return;
+ putTrap(key, new Trap(this, key, f, (Trap)getTrap(key)));
}
/** deletes a trap, if present */
- protected final void delTrap(Object name, JSFunction f) {
- Trap t = (Trap)getTrap(name);
+ final void delTrap(JS key, JSFunction f) throws JSExn {
+ Trap t = (Trap)getTrap(key);
if (t == null) return;
if (t.f == f) { putTrap(t.name, t.next); return; }
for(; t.next != null; t = t.next) if (t.next.f == f) { t.next = t.next.next; return; }