- public Number coerceToNumber() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a Number"); }
- public String coerceToString() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a String"); }
- public boolean coerceToBoolean() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a Boolean"); }
-
- public String typeName() { return "object"; }
-
-
- // Inner Classes /////////////////////////////////////////////////////////////////////////
-
- /** A sensible implementation of the abstract methods in the JS class */
- public static class Obj extends JS {
- private Hash entries = new Hash();
- private boolean sealed = false;
- public Obj() { this(false); }
- public Obj(boolean sealed) { this.sealed = sealed; }
- /** a sealed object cannot have its properties modified */
- public void setSeal(boolean sealed) { this.sealed = sealed; }
- public Object get(Object key) { return entries.get(key); }
- public void put(Object key, Object val) { if (!sealed) entries.put(key, val); }
- public Object[] keys() { return(entries.keys()); }
- }
-
- /** An exception which can be thrown and caught by JavaScript code */
- public static class Exn extends RuntimeException {
- private Object js = null;
- public Exn(Object js) { this.js = js; }
- public String toString() { return "JS.Exn: " + js; }
- public String getMessage() { return toString(); }
- public Object getObject() { return js; }
- }
-
- /** The publicly-visible face of JavaScript Array objects */
- public static class Array extends ArrayImpl {
- public Array() { }
- public Array(int size) { super(size); }
- public void setSize(int i) { super.setSize(i); }
- public int length() { return super.length(); }
- public Object elementAt(int i) { return super.elementAt(i); }
- public void addElement(Object o) { super.addElement(o); }
- public void setElementAt(Object o, int i) { super.setElementAt(o, i); }
- public Object get(Object key) { return super._get(key); }
- public void put(Object key, Object val) { super._put(key, val); }
- }
-
- /** Any object which becomes part of the scope chain must support this interface */
- public static class Scope extends ScopeImpl {
- public Scope(Scope parentScope) { this(parentScope, false); }
- public Scope(Scope parentScope, boolean sealed) { super(parentScope, sealed); }
- /** transparent scopes are not returned by THIS */
- public boolean isTransparent() { return super.isTransparent(); }
- public boolean has(Object key) { return super.has(key); }
- public Object get(Object key) { return super._get(key); }
- public void put(Object key, Object val) { super._put(key, val); }
- public void declare(String s) { super.declare(s); }
- }
-
- /** anything that is callable with the () operator */
- public static abstract class Callable extends JS.Obj {
- public abstract Object call(JS.Array args) throws JS.Exn;
- }
-
- /** a Callable which was compiled from JavaScript code */
- public static class CompiledFunction extends CompiledFunctionImpl {
- CompiledFunction(String sourceName, int firstLine, Reader sourceCode, Scope scope) throws IOException {
- super(sourceName, firstLine, sourceCode, scope);
+ 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;
+ int idx = i + smallIntCache.length / 2;
+ if (idx < smallIntCache.length && idx > 0) {
+ ret = smallIntCache[idx];
+ if (ret != null) return ret;