- public abstract Object get(Object key) throws JS.Exn;
- public abstract void put(Object key, Object val) throws JS.Exn;
- public abstract Object[] keys();
-
- 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"); }
-
-
- // Subclasses /////////////////////////////////////////////////////////////////////////
-
- /** A slightly more featureful version of JS */
- 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; }
- 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 JavaScripts */
- 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; }
- }
-
- /** Any object which becomes part of the scope chain must support this interface */
- public static class Scope extends Obj {
- private Scope parentScope;
- private static Object NULL = new Object();
- public Scope(Scope parentScope) { this(parentScope, false); }
- public Scope(Scope parentScope, boolean sealed) { super(sealed); this.parentScope = parentScope; }
- public Scope getParentScope() { return parentScope; }
-
- // transparent scopes are not returned by THIS
- public boolean isTransparent() { return false; }
-
- public boolean has(Object key) { return super.get(key) != null; }
- public Object get(Object key) {
- if (!has(key)) return parentScope == null ? null : getParentScope().get(key);
- Object ret = super.get(key); return ret == NULL ? null : ret;
- }
- public void put(Object key, Object val) {
- if (!has(key) && parentScope != null) getParentScope().put(key, val);
- else super.put(key, val == null ? NULL : val);
- }
- public Object[] keys() { throw new Error("you can't enumerate the properties of a Scope"); }
- public void declare(String s) {
- if (isTransparent()) getParentScope().declare(s);
- else super.put(s, NULL);
- }
- }
-
- public static CompiledFunction parse(String sourceName, int firstLine, Reader sourceCode, JS.Scope parentScope) throws IOException {
- return new CompiledFunction(sourceName, firstLine, sourceCode, parentScope);
+ // 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;
+ }
+ else ret = largeIntCache[Math.abs(idx % largeIntCache.length)];
+ if (ret == null || ret.intValue() != i) {
+ ret = new Integer(i);
+ if (idx < smallIntCache.length && idx > 0) smallIntCache[idx] = ret;
+ else largeIntCache[Math.abs(idx % largeIntCache.length)] = ret;
+ }
+ return ret;