X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJS.java;h=9bd8abcf4a5acd34c03f9bc98db52ea906aad99d;hb=a6fb49b55117ea4cf330b412d2a2fff403fcd053;hp=c98874662d5f315710f1b2443366601189e58589;hpb=7307f7fb11ee03b9990b38b214bbab08ddcd900e;p=org.ibex.js.git
diff --git a/src/org/ibex/js/JS.java b/src/org/ibex/js/JS.java
index c988746..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,30 +20,15 @@ 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. */
public JS call(JS method, JS[] args) throws JSExn;
- /** Calls this object with the given arguments, running it in an
- * unpausable context. An exception is thrown if this
- * object is not callable or the arguments are invalid. */
- public JS call(JS[] args) throws JSExn;
-
/** Returns the names of the formal arguments, if any.
* This function must never return a null object. */
public String[] getFormalArgs();
- /** Returns true if the specific key is found in this object. */
- public boolean hasKey(JS key);
-
/** Put a value to the given key, calling any write traps that have
* been placed on the key.
*
@@ -70,10 +55,9 @@ 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
- public InputStream getInputStream() throws IOException, JSExn;
+ // FEATURE: consider renaming/removing these
public JS unclone();
- public String coerceToString();
+ public String coerceToString() throws JSExn;
// Implementations ////////////////////////////////////////////////////////
@@ -84,43 +68,28 @@ public interface JS extends Pausable {
private static final String[] emptystr = new String[0];
public JS unclone() { return this; }
- public JS.Enumeration keys() throws JSExn { throw new JSExn(
- "object has no key set, class ["+ getClass().getName() +"]"); }
+ public JS.Enumeration keys() throws JSExn {
+ throw new JSExn("object has no key set, 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 on class ["+ getClass().getName() +"]"); }
- public InputStream getInputStream() throws IOException, JSExn { throw new JSExn(
- "object has not associated stream, class ["+ getClass().getName() +"]"); }
-
- public boolean hasKey(JS key) { return false; }
-
- public Object run(Object o) throws Exception { throw new JSExn(
- "object cannot be called, class ["+ getClass().getName() +"]"); }
- public void pause() { throw new NotPausableException(); }
+ public void put(JS key, JS val) throws JSExn {
+ throw new JSExn("'" + key + "' is read only on class ["+ getClass().getName() +"]"); }
- public JS call(JS[] args) throws JSExn { throw new JSExn(
- "object cannot be called, class ["+ getClass().getName() +"]"); }
- public JS call(JS method, JS[] args) throws JSExn { throw new JSExn(
- "method not found: " + JSU.str(method)); }
+ 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 void declare(JS key) throws JSExn { throw new JSExn(
- "object cannot declare key: "+ JSU.str(key)); }
- public void undeclare(JS key) throws JSExn { } // FIXME throw error?
-
- 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 { throw new JSExn(
- "'" + key + "' is not trappable on class ["+ getClass().getName() +"]"); }
- public void delTrap(JS key, JS function) throws JSExn { throw new JSExn(
- "'" + key + "' trap is read only on class ["+ getClass().getName() +"]"); }
- public Trap getTrap(JS key) throws JSExn { throw new JSExn(
- "'" + key + "' is not trappable on class ["+ getClass().getName() +"]"); }
-
- public String coerceToString() { return "object"; }
+ 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 { return null; }
}
public interface Cloneable {}
@@ -128,151 +97,87 @@ public interface JS extends Pausable {
public static class Clone implements JS {
protected final JS clonee;
public Clone(JS clonee) throws JSExn {
- if (!(clonee instanceof Cloneable)) throw new JSExn(
- clonee.getClass().getName() + " is not implement cloneable");
+ if (!(clonee instanceof Cloneable)) throw new JSExn(clonee.getClass().getName() + " is not implement cloneable");
this.clonee = clonee;
}
-
public JS unclone() { return clonee.unclone(); }
public boolean equals(Object o) { return clonee.equals(o); }
-
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); }
- 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 JS call(JS[] a) throws JSExn { return clonee.call(a); }
public String[] getFormalArgs() { return clonee.getFormalArgs(); }
-
- public boolean hasKey(JS k) { return clonee.hasKey(k); }
-
- public JS putAndTriggerTraps(JS k, JS v) throws JSExn {
- return clonee.putAndTriggerTraps(k, v); }
- public JS getAndTriggerTraps(JS k) throws JSExn {
- return clonee.getAndTriggerTraps(k); }
- public JS justTriggerTraps(JS k, JS v) throws JSExn {
- return clonee.justTriggerTraps(k, v); }
-
+ public JS putAndTriggerTraps(JS k, JS v) throws JSExn { return clonee.putAndTriggerTraps(k, v); }
+ public JS getAndTriggerTraps(JS k) throws JSExn { return clonee.getAndTriggerTraps(k); }
+ public JS justTriggerTraps(JS k, JS v) throws JSExn { return clonee.justTriggerTraps(k, v); }
public void addTrap(JS k, JS f) throws JSExn { clonee.addTrap(k, f); }
public void delTrap(JS k, JS f) throws JSExn { clonee.delTrap(k, f); }
public Trap getTrap(JS k) throws JSExn { return clonee.getTrap(k); }
-
- public String coerceToString() { return clonee.coerceToString(); }
+ public String coerceToString() throws JSExn { return clonee.coerceToString(); }
}
public static class Obj extends Basket.Hash implements JS {
private static final String[] emptystr = new String[0];
private static final Placeholder holder = new Placeholder();
- // HACK: this is hideously, disgustingly ugly.... we need N-ary Hash classes
- /** entries[index + 0] // key
- * entries[index + 1] // value
- * entries[index + 2] // trap
- */
-
- protected void entryAdded(int p) {}
- protected void entryUpdated(int p) {}
- protected void entryRemoved(int p) {}
-
public Obj() { super(3, 4, 0.75F); }
public JS unclone() { return this; }
- 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[] args) throws JSExn { throw new JSExn(
- "object cannot be called, class ["+ getClass().getName() +"]"); }
- public JS call(JS method, JS[] args) throws JSExn { throw new JSExn(
- "method not found: " + JSU.str(method)); }
public String[] getFormalArgs() { return emptystr; }
+ public JS call(JS method, JS[] args) throws JSExn {
+ 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();
return new Enumeration(null) {
- private int dest = -1, next = -1;
- public boolean _hasNext() {
- for (int i = Math.max(0, dest); i < usedslots; i++)
- if (i > 0 ? entries[i * indexmultiple] != null : true &&
- entries[i * indexmultiple] != this) { next = i; return true; }
- return false;
- }
- public JS _next() throws JSExn {
- if (next < 0 && !hasNext()) throw new NoSuchElementException();
- int index = next; dest = next; next = -1;
- return (JS)entries[index * indexmultiple];
- }
- };
+ private int cur = 0;
+ public boolean _hasNext() { return cur < keys.length; }
+ public JS _next() throws JSExn {
+ if (cur >= keys.length) throw new NoSuchElementException();
+ return (JS)keys[cur++];
+ }
+ };
}
- public JS get(JS key) throws JSExn { int i = indexOf(key);
- return i < 0 ? null : entries[i + 1] instanceof Placeholder ? null : (JS)entries[i + 1]; }
- public void put(JS key, JS val) throws JSExn {
- // NOTE: only way value can be stored as null is using declare()
- int dest = put(indexOf(key), key);
- if (val == null) entries[dest + 1] = holder;
- else entries[dest + 1] = val; }
- public boolean hasKey(JS key) { return indexOf(key) >= 0; }
- /*public boolean hasValue(JS key, JS value) {
- int i = indexOf(key); return i >= 0 && entries[i + 1] != null; }
- public boolean hasTrap(JS key, JS trap) {
- int i = indexOf(key); return i >= 0 && entries[i + 2] != null; }*/
-
- public void declare(JS key) throws JSExn { entries[put(indexOf(key), key) + 1] = null; }
- public void undeclare(JS key) throws JSExn { remove(indexOf(key)); }
+ public JS get(JS key) throws JSExn { return (JS)super.get(key, 0); }
+ public void put(JS key, JS val) throws JSExn { super.put(key, val, 0); }
public JS putAndTriggerTraps(JS key, JS val) throws JSExn {
- Trap t = null; int i = indexOf(key);
- if (i >= 0) t = (Trap)entries[i + 2];
- if (t != null && (t = t.write()) != null) {
- val = (JS)new Interpreter(t, val, false).run(null);
- }
- if (val == null) entries[i + 1] = holder;
- else entries[i + 1] = val;
+ Trap t = (Trap)super.get(key, 1);
+ if (t != null && (t = t.write()) != null) val = (JS)new Interpreter(t, val, false).run(null);
+ put(key, val); // HACK: necessary for subclasses overriding put()
return val;
}
public JS getAndTriggerTraps(JS key) throws JSExn {
- Trap t = null; int i = indexOf(key);
- if (i < 0) return null;
- t = (Trap)entries[i + 2];
- return t == null ? (JS)entries[i + 1] : (JS)new Interpreter(t, null, false).run(null);
+ Trap t = (Trap)super.get(key, 1);
+ 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 = null; int i = indexOf(key);
- if (i >= 0) t = (Trap)entries[i + 2];
+ Trap t = (Trap)super.get(key, 1);
if (t == null || (t = t.write()) == null) return val;
return (JS)new Interpreter(t, val, true).run(null);
}
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)");
- int i = indexOf(key); if (i < 0) i = put(i, key);
- for (Trap t = (Trap)entries[i + 2]; t != null; t = t.next())
+ 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;
- entries[i + 2] = new TrapHolder(this, key, f, (Trap)entries[i + 2]);
+ super.put(key, new TrapHolder(this, key, f, (Trap)super.get(key, 1)), 1);
}
public void delTrap(JS key, JS f) throws JSExn {
- int i = indexOf(key); if (i < 0) return;
- Trap t = (Trap)entries[i + 2];
- if (t.function().equals(f)) { entries[i + 2] = t.next(); return; }
+ Trap t = (Trap)super.get(key, 1);
+ if (t==null) return;
+ if (t.function().equals(f)) { super.put(key, t.next(), 2); return; }
for (; t.next() != null; t = t.next())
if (t.next().function().equals(f)) { ((TrapHolder)t).next = t.next().next(); return; }
}
- public Trap getTrap(JS key) throws JSExn {
- int i = indexOf(key); return i < 0 ? null : (Trap)entries[i + 2];
- }
+ public Trap getTrap(JS key) throws JSExn { return (Trap)super.get(key, 1); }
- public String coerceToString() { return "object"; }
+ public String coerceToString() throws JSExn { throw new JSExn("cannot coerce a "+getClass().getName()+" to a string"); }
private static final class Placeholder implements Serializable {}
@@ -280,21 +185,14 @@ public interface JS extends Pausable {
private final JS target, key, function;
private Trap next;
TrapHolder(JS t, JS k, JS f, Trap n) { target = t; key = k; function = f; next = n; }
-
public JS key() { return key; }
public JS target() { return target; }
public JS function() { return function; }
-
- public boolean isReadTrap() {
- return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
- public boolean isWriteTrap() {
- return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
-
+ public boolean isReadTrap() { return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
+ public boolean isWriteTrap() { return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
public Trap next() { return next; }
- public Trap nextRead() {
- Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
- public Trap nextWrite() {
- Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
+ public Trap nextRead() { Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
+ public Trap nextWrite() { Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
public Trap read() { return isReadTrap() ? this : nextRead(); }
public Trap write() { return isWriteTrap() ? this : nextWrite(); }
}
@@ -309,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();
}
@@ -324,13 +221,13 @@ public interface JS extends Pausable {
private final Enumeration parent;
+ public Enumeration() { this(null); }
public Enumeration(Enumeration parent) { this.parent = parent; }
protected abstract boolean _hasNext();
protected abstract JS _next() throws JSExn;
- public final boolean hasNext() {
- return _hasNext() || parent != null ? parent.hasNext() : false; }
+ public final boolean hasNext() { return _hasNext() || (parent != null ? parent.hasNext() : false); }
public final JS next() throws JSExn {
if (_hasNext()) return _next();
if (parent == null) throw new NoSuchElementException("reached end of set");
@@ -351,18 +248,24 @@ 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;
- public RandomAccessList(Enumeration parent, List list) {
- super(parent); l = list; size = l.size(); }
+ public RandomAccessList(Enumeration parent, List list) { super(parent); l = list; size = l.size(); }
protected boolean _hasNext() { return pos < size; }
protected JS _next() { return (JS)l.get(pos++); }
}