X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJS.java;h=a81c41950be64bf3f5bb08f4526e9ddd83805d62;hb=9aff374663aa8e336b3903c2ee97aa09f344ea0c;hp=d8d0f3367f8694936508ccca3cd8168bd275833f;hpb=4c96b0f7e40e4689434c46e4300cc9bef9d0fab9;p=org.ibex.core.git diff --git a/src/org/ibex/js/JS.java b/src/org/ibex/js/JS.java index d8d0f33..a81c419 100644 --- a/src/org/ibex/js/JS.java +++ b/src/org/ibex/js/JS.java @@ -14,18 +14,20 @@ public abstract class JS { public final JS unclone() { return _unclone(); } 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.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() +")"); } @@ -39,12 +41,13 @@ public abstract class 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() +")"); } + String debugToString() { return "[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 Enumeration keys() throws JSExn { return entries == null ? (Enumeration)EMPTY_ENUMERATION : (Enumeration)new JavaEnumeration(null,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); } @@ -87,7 +90,7 @@ public abstract class JS { JS _unclone() { return clonee.unclone(); } public JS getClonee() { return clonee; } public Clone(JS clonee) throws JSExn { - if(!(clonee instanceof Cloneable)) throw new JSExn("" + getClass().getName() + " isn't cloneable"); + 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()); } @@ -100,6 +103,40 @@ public abstract class JS { 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(); } + } + + public static abstract class Enumeration extends JS { + final Enumeration parent; + boolean done; + public Enumeration(Enumeration parent) { this.parent = parent; } + protected abstract boolean _hasMoreElements(); + protected abstract JS _nextElement() throws JSExn; + + public final boolean hasMoreElements() { + if(!done && !_hasMoreElements()) done = true; + return !done ? true : parent != null ? parent.hasMoreElements() : false; + } + public final JS nextElement() throws JSExn { return !done ? _nextElement() : parent != null ? parent.nextElement() : null; } + + public JS get(JS key) throws JSExn { + //#switch(JS.toString(key)) + case "hasMoreElements": return B(hasMoreElements()); + case "nextElement": return nextElement(); + //#end + return super.get(key); + } + } + public static class EmptyEnumeration extends Enumeration { + public EmptyEnumeration(Enumeration parent) { super(parent); } + protected boolean _hasMoreElements() { return false; } + protected JS _nextElement() { return null; } + } + public static class JavaEnumeration extends Enumeration { + private final java.util.Enumeration e; + public JavaEnumeration(Enumeration parent, java.util.Enumeration e) { super(parent); this.e = e; } + protected boolean _hasMoreElements() { return e.hasMoreElements(); } + protected JS _nextElement() { return (JS) e.nextElement(); } } // Static Interpreter Control Methods /////////////////////////////////////////////////////////////// @@ -120,7 +157,7 @@ public abstract class JS { boolean get; switch(i.f.op[i.pc]) { case Tokens.RETURN: case ByteCodes.PUT: get = false; break; - case ByteCodes.GET: get = true; break; + case ByteCodes.GET: case ByteCodes.CALL: get = true; break; default: throw new Error("should never happen"); } i.pausecount++; @@ -172,7 +209,7 @@ public abstract class JS { public static String debugToString(JS o) { try { return toString(o); } - catch(JSExn e) { return "[class=" + o.getClass().getName() + "]"; } + catch(JSExn e) { return o.debugToString(); } } public static boolean isInt(JS o) { @@ -209,7 +246,15 @@ public abstract class JS { public static final JS B(boolean b) { return b ? T : F; } public static final JS B(int i) { return i==0 ? F : T; } - public static final JS S(String s) { return new JSString(s); } + 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); } @@ -220,28 +265,25 @@ public abstract class JS { return N(n.doubleValue()); } - private static final JSNumber.I[] smallIntCache = new JSNumber.I[65535 / 4]; - private static final JSNumber.I[] largeIntCache = new JSNumber.I[65535 / 4]; + 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)]; + 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; } - private static Enumeration emptyEnumeration = new Enumeration() { - public boolean hasMoreElements() { return false; } - public Object nextElement() { throw new NoSuchElementException(); } - }; + private static Enumeration EMPTY_ENUMERATION = new EmptyEnumeration(null); public static JS fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException { return JSFunction._fromReader(sourceName, firstLine, sourceCode);