1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
7 import org.ibex.util.*;
11 /** The minimum set of functionality required for objects which are manipulated by JavaScript */
14 /** Returns an enumeration of the keys in this object. */
15 public JS.Enumeration keys() throws JSExn;
17 /** Return the value associated with the given key. */
18 public JS get(JS key) throws JSExn;
20 /** Store a specific value against the given key. */
21 public void put(JS key, JS value) throws JSExn;
23 /** Calls a specific method with given arguments on this object.
24 * An exception is thrown if there is no such method or the
25 * arguments are invalid. */
26 public JS call(JS method, JS[] args) throws JSExn;
28 /** Returns the names of the formal arguments, if any.
29 * This function must never return a null object. */
30 public String[] getFormalArgs();
32 /** Put a value to the given key, calling any write traps that have
33 * been placed on the key.
35 * This function may block for an indeterminate amount of time.
37 * Write traps may change the final value before it is put, thus
38 * be careful to read the latest value from this function's return
39 * before doing any more work with it.
41 public JS putAndTriggerTraps(JS key, JS value) throws JSExn;
43 /** Gets the value for a given key, calling any read traps that have
44 * been placed on the key.
46 * This function may block for an indeterminate amount of time.
48 public JS getAndTriggerTraps(JS key) throws JSExn;
50 /** Calls the write traps placed on a given key with the given value
51 * and returns the result without saving it to this object's key store. */
52 public JS justTriggerTraps(JS key, JS value) throws JSExn;
54 public void addTrap(JS key, JS function) throws JSExn;
55 public void delTrap(JS key, JS function) throws JSExn;
56 public Trap getTrap(JS key) throws JSExn;
58 // FEATURE: consider renaming/removing these
60 public String coerceToString() throws JSExn;
63 // Implementations ////////////////////////////////////////////////////////
65 /** Provides the lightest possible implementation of JS, throwing
66 * exceptions on every mutable operation. */
67 public static class Immutable implements JS {
68 private static final String[] emptystr = new String[0];
70 public JS unclone() { return this; }
71 public JS.Enumeration keys() throws JSExn {
72 throw new JSExn("object has no key set, class ["+ getClass().getName() +"]"); }
73 public JS get(JS key) throws JSExn { return null; }
74 public void put(JS key, JS val) throws JSExn {
75 throw new JSExn("'" + key + "' is read only on class ["+ getClass().getName() +"]"); }
77 public JS call(JS method, JS[] args) throws JSExn {
78 if (method == null) throw new JSExn( "object cannot be called, class ["+ getClass().getName() +"]");
79 throw new JSExn("method not found: " + JSU.str(method) + " class="+this.getClass().getName());
81 public String[] getFormalArgs() { return emptystr; }
82 public String coerceToString() throws JSExn { throw new JSExn("cannot coerce a "+getClass().getName()+" to a string"); }
84 public JS putAndTriggerTraps(JS key, JS val) throws JSExn {
85 throw new JSExn("'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); }
86 public JS getAndTriggerTraps(JS key) throws JSExn { return null; }
87 public JS justTriggerTraps(JS key, JS value) throws JSExn { return null; }
88 public void addTrap(JS key, JS function) throws JSExn {
89 Log.warn(this, "'" + JSU.str(key) + "' is not trappable on class ["+ getClass().getName() +"]"); }
90 public void delTrap(JS key, JS function) throws JSExn {
91 Log.warn(this, "'" + JSU.str(key) + "' trap is read only on class ["+ getClass().getName() +"]"); }
92 public Trap getTrap(JS key) throws JSExn { return null; }
95 public interface Cloneable {}
97 public static class Clone implements JS {
98 protected final JS clonee;
99 public Clone(JS clonee) throws JSExn {
100 if (!(clonee instanceof Cloneable)) throw new JSExn(clonee.getClass().getName() + " is not implement cloneable");
101 this.clonee = clonee;
103 public JS unclone() { return clonee.unclone(); }
104 public boolean equals(Object o) { return clonee.equals(o); }
105 public Enumeration keys() throws JSExn { return clonee.keys(); }
106 public JS get(JS k) throws JSExn { return clonee.get(k); }
107 public void put(JS k, JS v) throws JSExn { clonee.put(k, v); }
108 public JS call(JS m, JS[] a) throws JSExn { return clonee.call(m, a); }
109 public String[] getFormalArgs() { return clonee.getFormalArgs(); }
110 public JS putAndTriggerTraps(JS k, JS v) throws JSExn { return clonee.putAndTriggerTraps(k, v); }
111 public JS getAndTriggerTraps(JS k) throws JSExn { return clonee.getAndTriggerTraps(k); }
112 public JS justTriggerTraps(JS k, JS v) throws JSExn { return clonee.justTriggerTraps(k, v); }
113 public void addTrap(JS k, JS f) throws JSExn { clonee.addTrap(k, f); }
114 public void delTrap(JS k, JS f) throws JSExn { clonee.delTrap(k, f); }
115 public Trap getTrap(JS k) throws JSExn { return clonee.getTrap(k); }
116 public String coerceToString() throws JSExn { return clonee.coerceToString(); }
119 public static class Obj extends Basket.Hash implements JS {
120 private static final String[] emptystr = new String[0];
121 private static final Placeholder holder = new Placeholder();
123 public Obj() { super(3, 4, 0.75F); }
125 public JS unclone() { return this; }
126 public String[] getFormalArgs() { return emptystr; }
128 public JS call(JS method, JS[] args) throws JSExn {
129 throw new JSExn(method==null ? "cannot call a " + getClass().getName() : "method not found: " + JSU.str(method)); }
131 public Enumeration keys() throws JSExn {
132 final Object[] keys = super.dumpkeys();
133 return new Enumeration(null) {
135 public boolean _hasNext() { return cur < keys.length; }
136 public JS _next() throws JSExn {
137 if (cur >= keys.length) throw new NoSuchElementException();
138 return (JS)keys[cur++];
143 public JS get(JS key) throws JSExn { return (JS)super.get(key, 0); }
144 public void put(JS key, JS val) throws JSExn { super.put(key, val, 0); }
146 public JS putAndTriggerTraps(JS key, JS val) throws JSExn {
147 Trap t = (Trap)super.get(key, 1);
148 if (t != null && (t = t.write()) != null) val = (JS)new Interpreter(t, val, false).run(null);
149 put(key, val); // HACK: necessary for subclasses overriding put()
152 public JS getAndTriggerTraps(JS key) throws JSExn {
153 Trap t = (Trap)super.get(key, 1);
154 return t == null ? (JS)get(key) : (JS)new Interpreter(t, null, false).run(null);
156 public JS justTriggerTraps(JS key, JS val) throws JSExn {
157 Trap t = (Trap)super.get(key, 1);
158 if (t == null || (t = t.write()) == null) return val;
159 return (JS)new Interpreter(t, val, true).run(null);
162 public void addTrap(JS key, JS f) throws JSExn {
163 if (f.getFormalArgs() == null || f.getFormalArgs().length > 1)
164 throw new JSExn("traps must take either one argument (write) or no arguments (read)");
165 for (Trap t = (Trap)super.get(key, 1); t != null; t = t.next())
166 if (t.function().equals(f)) return;
167 super.put(key, new TrapHolder(this, key, f, (Trap)super.get(key, 1)), 1);
170 public void delTrap(JS key, JS f) throws JSExn {
171 Trap t = (Trap)super.get(key, 1);
173 if (t.function().equals(f)) { super.put(key, t.next(), 2); return; }
174 for (; t.next() != null; t = t.next())
175 if (t.next().function().equals(f)) { ((TrapHolder)t).next = t.next().next(); return; }
178 public Trap getTrap(JS key) throws JSExn { return (Trap)super.get(key, 1); }
180 public String coerceToString() throws JSExn { throw new JSExn("cannot coerce a "+getClass().getName()+" to a string"); }
182 private static final class Placeholder implements Serializable {}
184 private static final class TrapHolder implements Trap {
185 private final JS target, key, function;
187 TrapHolder(JS t, JS k, JS f, Trap n) { target = t; key = k; function = f; next = n; }
188 public JS key() { return key; }
189 public JS target() { return target; }
190 public JS function() { return function; }
191 public boolean isReadTrap() { return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
192 public boolean isWriteTrap() { return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
193 public Trap next() { return next; }
194 public Trap nextRead() { Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
195 public Trap nextWrite() { Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
196 public Trap read() { return isReadTrap() ? this : nextRead(); }
197 public Trap write() { return isWriteTrap() ? this : nextWrite(); }
201 public interface Trap {
204 public JS function();
205 public boolean isReadTrap();
206 public boolean isWriteTrap();
208 public Trap nextRead();
209 public Trap nextWrite();
210 public Trap read(); // FEATURE reconsider these function names
214 /** An empty class used for instanceof matching the result of JS.get()
215 * to decide if the key is a method (to be called with JS.callMethod(). */
216 public static final class Method extends Immutable {}
219 public static abstract class Enumeration extends Immutable {
220 public static final Enumeration EMPTY = new Empty(null);
222 private final Enumeration parent;
224 public Enumeration(Enumeration parent) { this.parent = parent; }
226 protected abstract boolean _hasNext();
227 protected abstract JS _next() throws JSExn;
229 public final boolean hasNext() { return _hasNext() || (parent != null ? parent.hasNext() : false); }
230 public final JS next() throws JSExn {
231 if (_hasNext()) return _next();
232 if (parent == null) throw new NoSuchElementException("reached end of set");
233 return parent.next();
236 public JS get(JS key) throws JSExn {
237 //#switch(JSU.str(key))
238 case "hasNext": return JSU.B(hasNext());
239 case "next": return next();
241 return super.get(key);
244 public static final class Empty extends Enumeration {
245 public Empty(Enumeration parent) { super(parent); }
246 protected boolean _hasNext() { return false; }
247 protected JS _next() { throw new NoSuchElementException(); }
250 public static final class JavaIterator extends Enumeration {
251 private final Iterator i;
252 public JavaIterator(Enumeration parent, Iterator i) { super(parent); this.i = i; }
253 protected boolean _hasNext() { return i.hasNext(); }
254 protected JS _next() { return (JS)i.next(); }
257 public static final class RandomAccessList extends Enumeration {
258 private final List l;
259 private int pos = 0, size;
260 public RandomAccessList(Enumeration parent, List list) { super(parent); l = list; size = l.size(); }
261 protected boolean _hasNext() { return pos < size; }
262 protected JS _next() { return (JS)l.get(pos++); }