automatically try call(JS[]) if method
[org.ibex.js.git] / src / org / ibex / js / JS.java
index 3a11127..a727048 100644 (file)
@@ -1,4 +1,7 @@
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] 
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
 package org.ibex.js; 
 
 import org.ibex.util.*; 
@@ -6,236 +9,358 @@ import java.io.*;
 import java.util.*;
 
 /** The minimum set of functionality required for objects which are manipulated by JavaScript */
-public class JS /*extends org.ibex.util.BalancedTree*/ implements Serializable { 
-
-    public static boolean checkAssertions = false;
-
-    public static final Object METHOD = new Object();
-    public final JS unclone() { return _unclone(); }
-    public Enumeration keys() throws JSExn { return entries == null ? emptyEnumeration : entries.keys(); }
-    public Object get(Object key) throws JSExn { return entries == null ? null : entries.get(key, null); }
-    public void put(Object key, Object val) throws JSExn { (entries==null?entries=new Hash():entries).put(key,null,val); }
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-        throw new JSExn("attempted to call the null value (method "+method+")");
-    }    
-    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-        throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
-    }
-
-    JS _unclone() { return this; }
-    public static class Cloneable extends JS {
-        public Object jsclone() throws JSExn {
-            return new Clone(this);
-        }
-    }
-
-    public static class Clone extends JS.Cloneable {
-        protected JS.Cloneable clonee = null;
-        JS _unclone() { return clonee.unclone(); }
-        public JS.Cloneable getClonee() { return clonee; }
-        public Clone(JS.Cloneable clonee) { this.clonee = clonee; }
-        public boolean equals(Object o) {
-            if (!(o instanceof JS)) return false;
-            return unclone() == ((JS)o).unclone();
-        }
-        public Enumeration keys() throws JSExn { return clonee.keys(); }
-        public Object get(Object key) throws JSExn { return clonee.get(key); }
-        public void put(Object key, Object val) throws JSExn { clonee.put(key, val); }
-        public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-            return clonee.callMethod(method, a0, a1, a2, rest, nargs);
-        }    
-        public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-            return clonee.call(a0, a1, a2, rest, nargs);
+public interface JS extends Pausable {
+
+    /** Returns an enumeration of the keys in this object. */
+    public JS.Enumeration keys() throws JSExn;
+
+    /** Return the value associated with the given key. */
+    public JS get(JS key) throws JSExn;
+
+    /** 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
+     *  <i>pausable</i> 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
+     *  <i>unpausable</i> 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();
+
+    /** Put a value to the given key, calling any write traps that have
+     *  been placed on the key.
+     *
+     *  This function may block for an indeterminate amount of time.
+     *
+     *  Write traps may change the final value  before it is put, thus
+     *  be careful to read the latest value from this function's return
+     *  before doing any more work with it.
+     */
+    public JS putAndTriggerTraps(JS key, JS value) throws JSExn;
+
+    /** Gets the value for a given key, calling any read traps that have
+     *  been placed on the key.
+     *
+     *  This function may block for an indeterminate amount of time.
+     */
+    public JS getAndTriggerTraps(JS key) throws JSExn;
+
+    /** Calls the write traps placed on a given key with the given value
+     *  and returns the result without saving it to this object's key store. */
+    public JS justTriggerTraps(JS key, JS value) throws JSExn;
+
+    public void addTrap(JS key, JS function) throws JSExn;
+    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;
+    public JS unclone();
+    public String coerceToString();
+
+
+    // Implementations ////////////////////////////////////////////////////////
+
+    /** Provides the lightest possible implementation of JS, throwing
+     *  exceptions on every mutable operation. */
+    public static class Immutable implements JS {
+        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 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[] args) throws JSExn { throw new JSExn( "object cannot be called, class ["+ getClass().getName() +"]"); }
+        public JS call(JS method, JS[] args) throws JSExn {
+            if (method == null) return call(args);
+            throw new JSExn("method not found: " + JSU.str(method) + " class="+this.getClass().getName());
         }
-    }
+        public String[] getFormalArgs() { return emptystr; }
 
-    // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
+        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?
 
-    /** log a message with the current JavaScript sourceName/line */
-    public static void log(Object message) { info(message); }
-    public static void debug(Object message) { Log.debug(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
-    public static void info(Object message) { Log.info(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
-    public static void warn(Object message) { Log.warn(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
-    public static void error(Object message) { Log.error(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
+        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 justTriggerTraps(JS key, JS value) throws JSExn { return null; }
 
-    public static class NotPauseableException extends Exception { NotPauseableException() { } }
+        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() +"]"); }
 
-    /** returns a callback which will restart the context; expects a value to be pushed onto the stack when unpaused */
-    public static UnpauseCallback pause() throws NotPauseableException {
-        Interpreter i = Interpreter.current();
-        if (i.pausecount == -1) throw new NotPauseableException();
-        i.pausecount++;
-        return new JS.UnpauseCallback(i);
+        public String coerceToString() { return "object"; }
     }
 
-    public static class UnpauseCallback implements Task {
-        Interpreter i;
-        UnpauseCallback(Interpreter i) { this.i = i; }
-        public void perform() throws JSExn { unpause(null); }
-        public void unpause(Object o) throws JSExn {
-            // FIXME: if o instanceof JSExn, throw it into the JSworld
-            i.stack.push(o);
-            i.resume();
+    public interface Cloneable {}
+
+    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");
+            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 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 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 Enumeration keys() throws JSExn {
+            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];
+                }
+            };
+        }
+        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 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 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);
+            }
+            put(key, val); // HACK: necessary for subclasses overriding put()
+            /*if (i < 0) i = put(i, key);
+            if (val == null) entries[i + 1] = holder;
+            else entries[i + 1] = val;*/
+            return val;
+        }
+        public JS getAndTriggerTraps(JS key) throws JSExn {
+            Trap t = null; int i = indexOf(key);
+            if (i < 0) return get(key); // HACK: necessary for subclasses overriding get()
+            t = (Trap)entries[i + 2];
+            return t == null ? (JS)entries[i + 1] : (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];
+            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 (t.function().equals(f)) return;
+            entries[i + 2] = new TrapHolder(this, key, f, (Trap)entries[i + 2]);
+        }
 
-    // Static Helper Methods ///////////////////////////////////////////////////////////////////////////////////
+        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; }
+            for (; t.next() != null; t = t.next())
+                if (t.next().function().equals(f)) { ((TrapHolder)t).next = t.next().next(); return; }
+        }
 
-    /** coerce an object to a Boolean */
-    public static boolean toBoolean(Object o) {
-        if (o == null) return false;
-        if (o instanceof Boolean) return ((Boolean)o).booleanValue();
-        if (o instanceof Long) return ((Long)o).longValue() != 0;
-        if (o instanceof Integer) return ((Integer)o).intValue() != 0;
-        if (o instanceof Number) {
-            double d = ((Number) o).doubleValue();
-            // NOTE: d == d is a test for NaN. It should be faster than Double.isNaN()
-            return d != 0.0 && d == d;
+        public Trap getTrap(JS key) throws JSExn {
+            int i = indexOf(key); return i < 0 ? null : (Trap)entries[i + 2];
         }
-        if (o instanceof String) return ((String)o).length() != 0;
-        return true;
-    }
 
-    /** coerce an object to a Long */
-    public static long toLong(Object o) { return toNumber(o).longValue(); }
+        public String coerceToString() { return "object"; }
 
-    /** coerce an object to an Int */
-    public static int toInt(Object o) { return toNumber(o).intValue(); }
+        private static final class Placeholder implements Serializable {}
 
-    /** coerce an object to a Double */
-    public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
+        private static final class TrapHolder implements Trap {
+            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; }
 
-    /** coerce an object to a Number */
-    public static Number toNumber(Object o) {
-        if (o == null) return ZERO;
-        if (o instanceof Number) return ((Number)o);
+            public JS key() { return key; }
+            public JS target() { return target; }
+            public JS function() { return function; }
 
-        // NOTE: There are about 3 pages of rules in ecma262 about string to number conversions
-        //       We aren't even close to following all those rules.  We probably never will be.
-        if (o instanceof String) try { return N((String)o); } catch (NumberFormatException e) { return N(Double.NaN); }
-        if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? N(1) : ZERO;
-        throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle");
-    }
+            public boolean isReadTrap()  {
+                return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
+            public boolean isWriteTrap() {
+                return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
 
-    /** coerce an object to a String */
-    public static String toString(Object o) {
-        if(o == null) return "null";
-        if(o instanceof String) return (String) o;
-        if(o instanceof Integer || o instanceof Long || o instanceof Boolean) return o.toString();
-        if(o instanceof JSArray) return o.toString();
-        if(o instanceof JSDate) return o.toString();
-        if(o instanceof Double || o instanceof Float) {
-            double d = ((Number)o).doubleValue();
-            if((int)d == d) return Integer.toString((int)d);
-            return o.toString();
+            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 read() { return isReadTrap() ? this : nextRead(); }
+            public Trap write() { return isWriteTrap() ? this : nextWrite(); }
         }
-        if (o instanceof JS) return ((JS)o).coerceToString();   // HACK for now, this will probably go away
-        throw new RuntimeException("can't coerce "+o+" [" + o.getClass().getName() + "] to type String.");
     }
 
-    public String coerceToString() {
-        throw new RuntimeException("can't coerce "+this+" [" + getClass().getName() + "] to type String.");
+    public interface Trap {
+        public JS key();
+        public JS target();
+        public JS function();
+        public boolean isReadTrap();
+        public boolean isWriteTrap();
+        public Trap next();
+        public Trap nextRead();
+        public Trap nextWrite();
+
+        public Trap read(); // FIXME reconsider these function names
+        public Trap write();
     }
 
-    // Instance Methods ////////////////////////////////////////////////////////////////////
-
-    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;
-        }
-        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;
-    }
-    
-    private static Enumeration emptyEnumeration = new Enumeration() {
-            public boolean hasMoreElements() { return false; }
-            public Object nextElement() { throw new NoSuchElementException(); }
-        };
-    
-    private Hash entries = null;
-
-    public static JS fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException {
-        return JSFunction._fromReader(sourceName, firstLine, sourceCode);
-    }
+    /** An empty class used for instanceof matching the result of JS.get()
+     *  to decide if the key is a method (to be called with JS.callMethod(). */
+    public static final class Method extends Immutable {}
 
-    // HACK: caller can't know if the argument is a JSFunction or not...
-    public static JS cloneWithNewParentScope(JS j, JSScope s) {
-        return ((JSFunction)j)._cloneWithNewParentScope(s);
-    }
 
+    public static abstract class Enumeration extends Immutable {
+        public static final Enumeration EMPTY = new Empty(null);
 
-    // Trap support //////////////////////////////////////////////////////////////////////////////
+        private final Enumeration parent;
 
-    /** override and return true to allow placing traps on this object.
-     *  if isRead true, this is a read trap, otherwise write trap
-     **/
-    protected boolean isTrappable(Object name, boolean isRead) { return true; }
+        public Enumeration(Enumeration parent) { this.parent = parent; }
 
-    /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
-    public void putAndTriggerTraps(Object key, Object value) throws JSExn {
-        Trap t = getTrap(key);
-        if (t != null) t.invoke(value);
-        else put(key, value);
-    }
+        protected abstract boolean _hasNext();
+        protected abstract JS _next() throws JSExn;
 
-    /** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */
-    public Object getAndTriggerTraps(Object key) throws JSExn {
-        Trap t = getTrap(key);
-        if (t != null) return t.invoke();
-        else return get(key);
-    }
+        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");
+            return parent.next();
+        }
 
-    /** retrieve a trap from the entries hash */
-    protected final Trap getTrap(Object key) {
-        return entries == null ? null : (Trap)entries.get(key, Trap.class);
-    }
+        public JS get(JS key) throws JSExn {
+            //#switch(JSU.str(key))
+            case "hasNext": return JSU.B(hasNext());
+            case "next": return next();
+            //#end
+            return super.get(key);
+        }
 
-    /** retrieve a trap from the entries hash */
-    protected final void putTrap(Object key, Trap value) {
-        if (entries == null) entries = new Hash();
-        entries.put(key, Trap.class, value);
-    }
+        public static final class Empty extends Enumeration {
+            public Empty(Enumeration parent) { super(parent); }
+            protected boolean _hasNext() { return false; }
+            protected JS _next() { throw new NoSuchElementException(); }
+        }
 
-    /** adds a trap, avoiding duplicates */
-    protected final void addTrap(Object name, JSFunction f) throws JSExn {
-        if (f.numFormalArgs > 1) throw new JSExn("traps must take either one argument (write) or no arguments (read)");
-        boolean isRead = f.numFormalArgs == 0;
-        if (!isTrappable(name, isRead)) throw new JSExn("not allowed "+(isRead?"read":"write")+" trap on property: "+name);
-        for(Trap t = getTrap(name); t != null; t = t.next) if (t.f == f) return;
-        putTrap(name, new Trap(this, name.toString(), f, (Trap)getTrap(name)));
-    }
+        public static final class JavaIterator extends Enumeration {
+            private 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(); }
+        }
 
-    /** deletes a trap, if present */
-    protected final void delTrap(Object name, JSFunction f) {
-        Trap t = (Trap)getTrap(name);
-        if (t == null) return;
-        if (t.f == f) { putTrap(t.name, t.next); return; }
-        for(; t.next != null; t = t.next) if (t.next.f == f) { t.next = t.next.next; return; }
+        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(); }
+            protected boolean _hasNext() { return  pos < size; }
+            protected JS _next() { return (JS)l.get(pos++); }
+        }
     }
 
-
-} 
+}