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() +"]"); }
76 public InputStream getInputStream() throws IOException, JSExn { throw new JSExn(
77 "object has not associated stream, class ["+ getClass().getName() +"]"); }
79 public JS call(JS method, JS[] args) throws JSExn {
80 if (method == null) throw new JSExn( "object cannot be called, class ["+ getClass().getName() +"]");
81 throw new JSExn("method not found: " + JSU.str(method) + " class="+this.getClass().getName());
83 public String[] getFormalArgs() { return emptystr; }
85 public JS putAndTriggerTraps(JS key, JS val) throws JSExn { throw new JSExn(
86 "'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); }
87 public JS getAndTriggerTraps(JS key) throws JSExn { return null; } // FIXME throw errors?
88 public JS justTriggerTraps(JS key, JS value) throws JSExn { return null; }
90 public void addTrap(JS key, JS function) throws JSExn {
91 Log.warn(this, "'" + JSU.str(key) + "' is not trappable on class ["+ getClass().getName() +"]"); }
92 public void delTrap(JS key, JS function) throws JSExn {
93 Log.warn(this, "'" + JSU.str(key) + "' trap is read only on class ["+ getClass().getName() +"]"); }
94 public Trap getTrap(JS key) throws JSExn {
95 //Log.warn(this, "'" + JSU.str(key) + "' is not trappable on class ["+ getClass().getName() +"]");
98 public String coerceToString() throws JSExn {
99 throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
102 public interface Cloneable {}
104 public static class Clone implements JS {
105 protected final JS clonee;
106 public Clone(JS clonee) throws JSExn {
107 if (!(clonee instanceof Cloneable)) throw new JSExn(clonee.getClass().getName() + " is not implement cloneable");
108 this.clonee = clonee;
110 public JS unclone() { return clonee.unclone(); }
111 public boolean equals(Object o) { return clonee.equals(o); }
112 public Enumeration keys() throws JSExn { return clonee.keys(); }
113 public JS get(JS k) throws JSExn { return clonee.get(k); }
114 public void put(JS k, JS v) throws JSExn { clonee.put(k, v); }
116 // FIMXE: cloning Fountains...
117 //public InputStream getInputStream() throws IOException, JSExn { return clonee.getInputStream(); }
119 public JS call(JS m, JS[] a) throws JSExn { return clonee.call(m, a); }
120 public String[] getFormalArgs() { return clonee.getFormalArgs(); }
121 public JS putAndTriggerTraps(JS k, JS v) throws JSExn { return clonee.putAndTriggerTraps(k, v); }
122 public JS getAndTriggerTraps(JS k) throws JSExn { return clonee.getAndTriggerTraps(k); }
123 public JS justTriggerTraps(JS k, JS v) throws JSExn { return clonee.justTriggerTraps(k, v); }
124 public void addTrap(JS k, JS f) throws JSExn { clonee.addTrap(k, f); }
125 public void delTrap(JS k, JS f) throws JSExn { clonee.delTrap(k, f); }
126 public Trap getTrap(JS k) throws JSExn { return clonee.getTrap(k); }
127 public String coerceToString() throws JSExn { return clonee.coerceToString(); }
130 public static class Obj extends Basket.HashMap implements JS {
131 private static final String[] emptystr = new String[0];
132 private static final Placeholder holder = new Placeholder();
134 public Obj() { super(3, 4, 0.75F); }
136 public JS unclone() { return this; }
138 public JS call(JS method, JS[] args) throws JSExn {
139 if (method==null) throw new JSExn("cannot call a " + getClass().getName());
140 throw new JSExn("method not found: " + JSU.str(method)); }
141 public String[] getFormalArgs() { return emptystr; }
143 public Enumeration keys() throws JSExn {
144 final Object[] keys = super.dumpkeys();
145 return new Enumeration(null) {
147 public boolean _hasNext() { return cur < keys.length; }
148 public JS _next() throws JSExn {
149 if (cur >= keys.length) throw new NoSuchElementException();
150 return (JS)keys[cur++];
155 public JS get(JS key) throws JSExn { return (JS)super.get(key, 0); }
156 public void put(JS key, JS val) throws JSExn { super.put(key, val, 0); }
158 public JS putAndTriggerTraps(JS key, JS val) throws JSExn {
159 Trap t = (Trap)super.get(key, 1);
160 if (t != null && (t = t.write()) != null) val = (JS)new Interpreter(t, val, false).run(null);
161 put(key, val); // HACK: necessary for subclasses overriding put()
164 public JS getAndTriggerTraps(JS key) throws JSExn {
165 Trap t = (Trap)super.get(key, 1);
166 return t == null ? (JS)super.get(key) : (JS)new Interpreter(t, null, false).run(null);
168 public JS justTriggerTraps(JS key, JS val) throws JSExn {
169 Trap t = (Trap)super.get(key, 1);
170 if (t == null || (t = t.write()) == null) return val;
171 return (JS)new Interpreter(t, val, true).run(null);
174 public void addTrap(JS key, JS f) throws JSExn {
175 if (f.getFormalArgs() == null || f.getFormalArgs().length > 1) throw new JSExn(
176 "traps must take either one argument (write) or no arguments (read)");
177 for (Trap t = (Trap)super.get(key, 1); t != null; t = t.next())
178 if (t.function().equals(f)) return;
179 super.put(key, new TrapHolder(this, key, f, (Trap)super.get(key, 1)), 1);
182 public void delTrap(JS key, JS f) throws JSExn {
183 Trap t = (Trap)super.get(key, 1);
185 if (t.function().equals(f)) { super.put(key, t.next(), 2); return; }
186 for (; t.next() != null; t = t.next())
187 if (t.next().function().equals(f)) { ((TrapHolder)t).next = t.next().next(); return; }
190 public Trap getTrap(JS key) throws JSExn { return (Trap)super.get(key, 1); }
192 public String coerceToString() throws JSExn {
193 throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
195 private static final class Placeholder implements Serializable {}
197 private static final class TrapHolder implements Trap {
198 private final JS target, key, function;
200 TrapHolder(JS t, JS k, JS f, Trap n) { target = t; key = k; function = f; next = n; }
201 public JS key() { return key; }
202 public JS target() { return target; }
203 public JS function() { return function; }
204 public boolean isReadTrap() { return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
205 public boolean isWriteTrap() { return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
206 public Trap next() { return next; }
207 public Trap nextRead() { Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
208 public Trap nextWrite() { Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
209 public Trap read() { return isReadTrap() ? this : nextRead(); }
210 public Trap write() { return isWriteTrap() ? this : nextWrite(); }
214 public interface Trap {
217 public JS function();
218 public boolean isReadTrap();
219 public boolean isWriteTrap();
221 public Trap nextRead();
222 public Trap nextWrite();
224 public Trap read(); // FIXME reconsider these function names
228 /** An empty class used for instanceof matching the result of JS.get()
229 * to decide if the key is a method (to be called with JS.callMethod(). */
230 public static final class Method extends Immutable {}
233 public static abstract class Enumeration extends Immutable {
234 public static final Enumeration EMPTY = new Empty(null);
236 private final Enumeration parent;
238 public Enumeration(Enumeration parent) { this.parent = parent; }
240 protected abstract boolean _hasNext();
241 protected abstract JS _next() throws JSExn;
243 public final boolean hasNext() { return _hasNext() || (parent != null ? parent.hasNext() : false); }
244 public final JS next() throws JSExn {
245 if (_hasNext()) return _next();
246 if (parent == null) throw new NoSuchElementException("reached end of set");
247 return parent.next();
250 public JS get(JS key) throws JSExn {
251 //#switch(JSU.str(key))
252 case "hasNext": return JSU.B(hasNext());
253 case "next": return next();
255 return super.get(key);
258 public static final class Empty extends Enumeration {
259 public Empty(Enumeration parent) { super(parent); }
260 protected boolean _hasNext() { return false; }
261 protected JS _next() { throw new NoSuchElementException(); }
264 public static final class JavaIterator extends Enumeration {
265 private final Iterator i;
266 public JavaIterator(Enumeration parent, Iterator i) { super(parent); this.i = i; }
267 protected boolean _hasNext() { return i.hasNext(); }
268 protected JS _next() { return (JS)i.next(); }
271 public static final class RandomAccessList extends Enumeration {
272 private final List l;
273 private int pos = 0, size;
274 public RandomAccessList(Enumeration parent, List list) { super(parent); l = list; size = l.size(); }
275 protected boolean _hasNext() { return pos < size; }
276 protected JS _next() { return (JS)l.get(pos++); }