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 */
12 public interface JS extends Pausable {
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 // FIXME: 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; }
83 public JS putAndTriggerTraps(JS key, JS val) throws JSExn { throw new JSExn(
84 "'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); }
85 public JS getAndTriggerTraps(JS key) throws JSExn { return null; } // FIXME throw errors?
86 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 {
93 //Log.warn(this, "'" + JSU.str(key) + "' is not trappable on class ["+ getClass().getName() +"]");
96 public String coerceToString() throws JSExn {
97 throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
100 public interface Cloneable {}
102 public static class Clone implements JS {
103 protected final JS clonee;
104 public Clone(JS clonee) throws JSExn {
105 if (!(clonee instanceof Cloneable)) throw new JSExn(clonee.getClass().getName() + " is not implement cloneable");
106 this.clonee = clonee;
108 public JS unclone() { return clonee.unclone(); }
109 public boolean equals(Object o) { return clonee.equals(o); }
110 public Enumeration keys() throws JSExn { return clonee.keys(); }
111 public JS get(JS k) throws JSExn { return clonee.get(k); }
112 public void put(JS k, JS v) throws JSExn { clonee.put(k, v); }
113 public JS call(JS m, JS[] a) throws JSExn { return clonee.call(m, a); }
114 public String[] getFormalArgs() { return clonee.getFormalArgs(); }
115 public JS putAndTriggerTraps(JS k, JS v) throws JSExn { return clonee.putAndTriggerTraps(k, v); }
116 public JS getAndTriggerTraps(JS k) throws JSExn { return clonee.getAndTriggerTraps(k); }
117 public JS justTriggerTraps(JS k, JS v) throws JSExn { return clonee.justTriggerTraps(k, v); }
118 public void addTrap(JS k, JS f) throws JSExn { clonee.addTrap(k, f); }
119 public void delTrap(JS k, JS f) throws JSExn { clonee.delTrap(k, f); }
120 public Trap getTrap(JS k) throws JSExn { return clonee.getTrap(k); }
121 public String coerceToString() throws JSExn { return clonee.coerceToString(); }
124 public static class Obj extends Basket.HashMap implements JS {
125 private static final String[] emptystr = new String[0];
126 private static final Placeholder holder = new Placeholder();
128 public Obj() { super(3, 4, 0.75F); }
130 public JS unclone() { return this; }
132 public JS call(JS method, JS[] args) throws JSExn {
133 if (method==null) throw new JSExn("cannot call a " + getClass().getName());
134 throw new JSExn("method not found: " + JSU.str(method)); }
135 public String[] getFormalArgs() { return emptystr; }
137 public Enumeration keys() throws JSExn {
138 final Object[] keys = super.dumpkeys();
139 return new Enumeration(null) {
141 public boolean _hasNext() { return cur < keys.length; }
142 public JS _next() throws JSExn {
143 if (cur >= keys.length) throw new NoSuchElementException();
144 return (JS)keys[cur++];
149 public JS get(JS key) throws JSExn { return (JS)super.get(key, 0); }
150 public void put(JS key, JS val) throws JSExn { super.put(key, val, 0); }
152 public JS putAndTriggerTraps(JS key, JS val) throws JSExn {
153 Trap t = (Trap)super.get(key, 1);
154 if (t != null && (t = t.write()) != null) val = (JS)new Interpreter(t, val, false).run(null);
155 put(key, val); // HACK: necessary for subclasses overriding put()
158 public JS getAndTriggerTraps(JS key) throws JSExn {
159 Trap t = (Trap)super.get(key, 1);
160 return t == null ? (JS)super.get(key) : (JS)new Interpreter(t, null, false).run(null);
162 public JS justTriggerTraps(JS key, JS val) throws JSExn {
163 Trap t = (Trap)super.get(key, 1);
164 if (t == null || (t = t.write()) == null) return val;
165 return (JS)new Interpreter(t, val, true).run(null);
168 public void addTrap(JS key, JS f) throws JSExn {
169 if (f.getFormalArgs() == null || f.getFormalArgs().length > 1) throw new JSExn(
170 "traps must take either one argument (write) or no arguments (read)");
171 for (Trap t = (Trap)super.get(key, 1); t != null; t = t.next())
172 if (t.function().equals(f)) return;
173 super.put(key, new TrapHolder(this, key, f, (Trap)super.get(key, 1)), 1);
176 public void delTrap(JS key, JS f) throws JSExn {
177 Trap t = (Trap)super.get(key, 1);
179 if (t.function().equals(f)) { super.put(key, t.next(), 2); return; }
180 for (; t.next() != null; t = t.next())
181 if (t.next().function().equals(f)) { ((TrapHolder)t).next = t.next().next(); return; }
184 public Trap getTrap(JS key) throws JSExn { return (Trap)super.get(key, 1); }
186 public String coerceToString() throws JSExn {
187 throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
189 private static final class Placeholder implements Serializable {}
191 private static final class TrapHolder implements Trap {
192 private final JS target, key, function;
194 TrapHolder(JS t, JS k, JS f, Trap n) { target = t; key = k; function = f; next = n; }
195 public JS key() { return key; }
196 public JS target() { return target; }
197 public JS function() { return function; }
198 public boolean isReadTrap() { return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
199 public boolean isWriteTrap() { return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
200 public Trap next() { return next; }
201 public Trap nextRead() { Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
202 public Trap nextWrite() { Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
203 public Trap read() { return isReadTrap() ? this : nextRead(); }
204 public Trap write() { return isWriteTrap() ? this : nextWrite(); }
208 public interface Trap {
211 public JS function();
212 public boolean isReadTrap();
213 public boolean isWriteTrap();
215 public Trap nextRead();
216 public Trap nextWrite();
218 public Trap read(); // FIXME reconsider these function names
222 /** An empty class used for instanceof matching the result of JS.get()
223 * to decide if the key is a method (to be called with JS.callMethod(). */
224 public static final class Method extends Immutable {}
227 public static abstract class Enumeration extends Immutable {
228 public static final Enumeration EMPTY = new Empty(null);
230 private final Enumeration parent;
232 public Enumeration(Enumeration parent) { this.parent = parent; }
234 protected abstract boolean _hasNext();
235 protected abstract JS _next() throws JSExn;
237 public final boolean hasNext() { return _hasNext() || (parent != null ? parent.hasNext() : false); }
238 public final JS next() throws JSExn {
239 if (_hasNext()) return _next();
240 if (parent == null) throw new NoSuchElementException("reached end of set");
241 return parent.next();
244 public JS get(JS key) throws JSExn {
245 //#switch(JSU.str(key))
246 case "hasNext": return JSU.B(hasNext());
247 case "next": return next();
249 return super.get(key);
252 public static final class Empty extends Enumeration {
253 public Empty(Enumeration parent) { super(parent); }
254 protected boolean _hasNext() { return false; }
255 protected JS _next() { throw new NoSuchElementException(); }
258 public static final class JavaIterator extends Enumeration {
259 private final Iterator i;
260 public JavaIterator(Enumeration parent, Iterator i) { super(parent); this.i = i; }
261 protected boolean _hasNext() { return i.hasNext(); }
262 protected JS _next() { return (JS)i.next(); }
265 public static final class RandomAccessList extends Enumeration {
266 private final List l;
267 private int pos = 0, size;
268 public RandomAccessList(Enumeration parent, List list) { super(parent); l = list; size = l.size(); }
269 protected boolean _hasNext() { return pos < size; }
270 protected JS _next() { return (JS)l.get(pos++); }