X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJS.java;h=9bd8abcf4a5acd34c03f9bc98db52ea906aad99d;hb=a6fb49b55117ea4cf330b412d2a2fff403fcd053;hp=53555803dd36dc97a001840aa2f1e81e847eeb7a;hpb=55be062f7146f8ca2899e1234e4546a0756520ee;p=org.ibex.js.git diff --git a/src/org/ibex/js/JS.java b/src/org/ibex/js/JS.java index 5355580..9bd8abc 100644 --- a/src/org/ibex/js/JS.java +++ b/src/org/ibex/js/JS.java @@ -9,7 +9,7 @@ import java.io.*; import java.util.*; /** The minimum set of functionality required for objects which are manipulated by JavaScript */ -public interface JS extends Pausable { +public interface JS { /** Returns an enumeration of the keys in this object. */ public JS.Enumeration keys() throws JSExn; @@ -20,13 +20,6 @@ public interface JS extends Pausable { /** Store a specific value against the given key. */ public void put(JS key, JS value) throws JSExn; - /** Executes or unpauses the task, running it in a - * pausable context. */ - public Object run(Object o) throws Exception, AlreadyRunningException; - - /** Pauses the running task at its convienience. */ - public void pause() throws NotPausableException; - /** Calls a specific method with given arguments on this object. * An exception is thrown if there is no such method or the * arguments are invalid. */ @@ -62,7 +55,7 @@ public interface JS extends Pausable { public void delTrap(JS key, JS function) throws JSExn; public Trap getTrap(JS key) throws JSExn; - // FIXME: consider renaming/removing these + // FEATURE: consider renaming/removing these public JS unclone(); public String coerceToString() throws JSExn; @@ -80,34 +73,23 @@ public interface JS extends Pausable { 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 on class ["+ getClass().getName() +"]"); } - public InputStream getInputStream() throws IOException, JSExn { throw new JSExn( - "object has not associated stream, class ["+ getClass().getName() +"]"); } - - public Object run(Object o) throws Exception { throw new JSExn( - "object cannot be called, class ["+ getClass().getName() +"]"); } - public void pause() { throw new NotPausableException(); } public JS call(JS method, JS[] args) throws JSExn { if (method == null) throw new JSExn( "object cannot be called, class ["+ getClass().getName() +"]"); throw new JSExn("method not found: " + JSU.str(method) + " class="+this.getClass().getName()); } public String[] getFormalArgs() { return emptystr; } + public String coerceToString() throws JSExn { throw new JSExn("cannot coerce a "+getClass().getName()+" to a string"); } - public JS putAndTriggerTraps(JS key, JS val) throws JSExn { throw new JSExn( - "'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); } - public JS getAndTriggerTraps(JS key) throws JSExn { return null; } // FIXME throw errors? + public JS putAndTriggerTraps(JS key, JS val) throws JSExn { + throw new JSExn("'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); } + public JS getAndTriggerTraps(JS key) throws JSExn { return null; } public JS justTriggerTraps(JS key, JS value) throws JSExn { return null; } - public void addTrap(JS key, JS function) throws JSExn { Log.warn(this, "'" + JSU.str(key) + "' is not trappable on class ["+ getClass().getName() +"]"); } public void delTrap(JS key, JS function) throws JSExn { Log.warn(this, "'" + JSU.str(key) + "' trap is read only on class ["+ getClass().getName() +"]"); } - public Trap getTrap(JS key) throws JSExn { - //Log.warn(this, "'" + JSU.str(key) + "' is not trappable on class ["+ getClass().getName() +"]"); - return null; - } - public String coerceToString() throws JSExn { - throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); } + public Trap getTrap(JS key) throws JSExn { return null; } } public interface Cloneable {} @@ -123,12 +105,6 @@ public interface JS extends Pausable { public Enumeration keys() throws JSExn { return clonee.keys(); } public JS get(JS k) throws JSExn { return clonee.get(k); } public void put(JS k, JS v) throws JSExn { clonee.put(k, v); } - - // FIMXE: cloning Fountains... - //public InputStream getInputStream() throws IOException, JSExn { return clonee.getInputStream(); } - - public Object run(Object o) throws Exception { return clonee.run(o); } - public void pause() { clonee.pause(); } public JS call(JS m, JS[] a) throws JSExn { return clonee.call(m, a); } public String[] getFormalArgs() { return clonee.getFormalArgs(); } public JS putAndTriggerTraps(JS k, JS v) throws JSExn { return clonee.putAndTriggerTraps(k, v); } @@ -140,22 +116,17 @@ public interface JS extends Pausable { public String coerceToString() throws JSExn { return clonee.coerceToString(); } } - public static class Obj extends Basket.HashMap implements JS { + public static class Obj extends Basket.Hash implements JS { private static final String[] emptystr = new String[0]; private static final Placeholder holder = new Placeholder(); public Obj() { super(3, 4, 0.75F); } public JS unclone() { return this; } - - public Object run(Object o) throws Exception { - throw new JSExn("object cannot be called, class ["+ getClass().getName() +"]"); } - public void pause() { throw new NotPausableException(); } + public String[] getFormalArgs() { return emptystr; } public JS call(JS method, JS[] args) throws JSExn { - if (method==null) throw new JSExn("cannot call a " + getClass().getName()); - throw new JSExn("method not found: " + JSU.str(method)); } - public String[] getFormalArgs() { return emptystr; } + throw new JSExn(method==null ? "cannot call a " + getClass().getName() : "method not found: " + JSU.str(method)); } public Enumeration keys() throws JSExn { final Object[] keys = super.dumpkeys(); @@ -180,7 +151,7 @@ public interface JS extends Pausable { } public JS getAndTriggerTraps(JS key) throws JSExn { Trap t = (Trap)super.get(key, 1); - return t == null ? (JS)super.get(key) : (JS)new Interpreter(t, null, false).run(null); + return t == null ? (JS)get(key) : (JS)new Interpreter(t, null, false).run(null); } public JS justTriggerTraps(JS key, JS val) throws JSExn { Trap t = (Trap)super.get(key, 1); @@ -189,8 +160,8 @@ public interface JS extends Pausable { } public void addTrap(JS key, JS f) throws JSExn { - if (f.getFormalArgs() == null || f.getFormalArgs().length > 1) throw new JSExn( - "traps must take either one argument (write) or no arguments (read)"); + if (f.getFormalArgs() == null || f.getFormalArgs().length > 1) + throw new JSExn("traps must take either one argument (write) or no arguments (read)"); for (Trap t = (Trap)super.get(key, 1); t != null; t = t.next()) if (t.function().equals(f)) return; super.put(key, new TrapHolder(this, key, f, (Trap)super.get(key, 1)), 1); @@ -206,8 +177,7 @@ public interface JS extends Pausable { public Trap getTrap(JS key) throws JSExn { return (Trap)super.get(key, 1); } - public String coerceToString() throws JSExn { - throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); } + public String coerceToString() throws JSExn { throw new JSExn("cannot coerce a "+getClass().getName()+" to a string"); } private static final class Placeholder implements Serializable {} @@ -237,8 +207,7 @@ public interface JS extends Pausable { public Trap next(); public Trap nextRead(); public Trap nextWrite(); - - public Trap read(); // FIXME reconsider these function names + public Trap read(); // FEATURE reconsider these function names public Trap write(); } @@ -252,6 +221,7 @@ public interface JS extends Pausable { private final Enumeration parent; + public Enumeration() { this(null); } public Enumeration(Enumeration parent) { this.parent = parent; } protected abstract boolean _hasNext(); @@ -278,13 +248,20 @@ public interface JS extends Pausable { protected JS _next() { throw new NoSuchElementException(); } } - public static final class JavaIterator extends Enumeration { - private final Iterator i; + public static class JavaIterator extends Enumeration { + protected final Iterator i; public JavaIterator(Enumeration parent, Iterator i) { super(parent); this.i = i; } protected boolean _hasNext() { return i.hasNext(); } protected JS _next() { return (JS)i.next(); } } + public static class JavaStringEnumeration extends Enumeration { + protected final java.util.Enumeration e; + public JavaStringEnumeration(java.util.Enumeration e) { this.e = e; } + protected boolean _hasNext() { return e.hasMoreElements(); } + protected JS _next() { return JSU.S(e.nextElement().toString()); } + } + public static final class RandomAccessList extends Enumeration { private final List l; private int pos = 0, size;