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 /** Executes or unpauses the task, running it in a
24 * <i>pausable</i> context. */
25 public Object run(Object o) throws Exception, AlreadyRunningException;
27 /** Pauses the running task at its convienience. */
28 public void pause() throws NotPausableException;
30 /** Calls a specific method with given arguments on this object.
31 * An exception is thrown if there is no such method or the
32 * arguments are invalid. */
33 public JS call(JS method, JS[] args) throws JSExn;
35 /** Calls this object with the given arguments, running it in an
36 * <i>unpausable</i> context. An exception is thrown if this
37 * object is not callable or the arguments are invalid. */
38 public JS call(JS[] args) throws JSExn;
40 /** Returns the names of the formal arguments, if any.
41 * This function must never return a null object. */
42 public String[] getFormalArgs();
44 /** Returns true if the specific key is found in this object. */
45 public boolean hasKey(JS key);
47 /** Put a value to the given key, calling any write traps that have
48 * been placed on the key.
50 * This function may block for an indeterminate amount of time.
52 * Write traps may change the final value before it is put, thus
53 * be careful to read the latest value from this function's return
54 * before doing any more work with it.
56 public JS putAndTriggerTraps(JS key, JS value) throws JSExn;
58 /** Gets the value for a given key, calling any read traps that have
59 * been placed on the key.
61 * This function may block for an indeterminate amount of time.
63 public JS getAndTriggerTraps(JS key) throws JSExn;
65 /** Calls the write traps placed on a given key with the given value
66 * and returns the result without saving it to this object's key store. */
67 public JS justTriggerTraps(JS key, JS value) throws JSExn;
69 public void addTrap(JS key, JS function) throws JSExn;
70 public void delTrap(JS key, JS function) throws JSExn;
71 public Trap getTrap(JS key) throws JSExn;
73 // FIXME: consider renaming/removing these
74 public InputStream getInputStream() throws IOException, JSExn;
76 public String coerceToString();
79 // Implementations ////////////////////////////////////////////////////////
81 /** Provides the lightest possible implementation of JS, throwing
82 * exceptions on every mutable operation. */
83 public static class Immutable implements JS {
84 private static final String[] emptystr = new String[0];
86 public JS unclone() { return this; }
87 public JS.Enumeration keys() throws JSExn { throw new JSExn(
88 "object has no key set, class ["+ getClass().getName() +"]"); }
89 public JS get(JS key) throws JSExn { return null; }
90 public void put(JS key, JS val) throws JSExn { throw new JSExn(
91 "'" + key + "' is read only on class ["+ getClass().getName() +"]"); }
92 public InputStream getInputStream() throws IOException, JSExn { throw new JSExn(
93 "object has not associated stream, class ["+ getClass().getName() +"]"); }
95 public boolean hasKey(JS key) { return false; }
97 public Object run(Object o) throws Exception { throw new JSExn(
98 "object cannot be called, class ["+ getClass().getName() +"]"); }
99 public void pause() { throw new NotPausableException(); }
101 public JS call(JS[] args) throws JSExn { throw new JSExn(
102 "object cannot be called, class ["+ getClass().getName() +"]"); }
103 public JS call(JS method, JS[] args) throws JSExn { throw new JSExn(
104 "method not found: " + JSU.str(method)); }
105 public String[] getFormalArgs() { return emptystr; }
107 public void declare(JS key) throws JSExn { throw new JSExn(
108 "object cannot declare key: "+ JSU.str(key)); }
109 public void undeclare(JS key) throws JSExn { } // FIXME throw error?
111 public JS putAndTriggerTraps(JS key, JS val) throws JSExn { throw new JSExn(
112 "'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); }
113 public JS getAndTriggerTraps(JS key) throws JSExn { return null; } // FIXME throw errors?
114 public JS justTriggerTraps(JS key, JS value) throws JSExn { return null; }
116 public void addTrap(JS key, JS function) throws JSExn { throw new JSExn(
117 "'" + key + "' is not trappable on class ["+ getClass().getName() +"]"); }
118 public void delTrap(JS key, JS function) throws JSExn { throw new JSExn(
119 "'" + key + "' trap is read only on class ["+ getClass().getName() +"]"); }
120 public Trap getTrap(JS key) throws JSExn { throw new JSExn(
121 "'" + key + "' is not trappable on class ["+ getClass().getName() +"]"); }
123 public String coerceToString() { return "object"; }
126 public interface Cloneable {}
128 public static class Clone implements JS {
129 protected final JS clonee;
130 public Clone(JS clonee) throws JSExn {
131 if (!(clonee instanceof Cloneable)) throw new JSExn(
132 clonee.getClass().getName() + " is not implement cloneable");
133 this.clonee = clonee;
136 public JS unclone() { return clonee.unclone(); }
137 public boolean equals(Object o) { return clonee.equals(o); }
139 public Enumeration keys() throws JSExn { return clonee.keys(); }
140 public JS get(JS k) throws JSExn { return clonee.get(k); }
141 public void put(JS k, JS v) throws JSExn { clonee.put(k, v); }
142 public InputStream getInputStream() throws IOException, JSExn {
143 return clonee.getInputStream(); }
145 public Object run(Object o) throws Exception { return clonee.run(o); }
146 public void pause() { clonee.pause(); }
148 public JS call(JS m, JS[] a) throws JSExn { return clonee.call(m, a); }
149 public JS call(JS[] a) throws JSExn { return clonee.call(a); }
150 public String[] getFormalArgs() { return clonee.getFormalArgs(); }
152 public boolean hasKey(JS k) { return clonee.hasKey(k); }
154 public JS putAndTriggerTraps(JS k, JS v) throws JSExn {
155 return clonee.putAndTriggerTraps(k, v); }
156 public JS getAndTriggerTraps(JS k) throws JSExn {
157 return clonee.getAndTriggerTraps(k); }
158 public JS justTriggerTraps(JS k, JS v) throws JSExn {
159 return clonee.justTriggerTraps(k, v); }
161 public void addTrap(JS k, JS f) throws JSExn { clonee.addTrap(k, f); }
162 public void delTrap(JS k, JS f) throws JSExn { clonee.delTrap(k, f); }
163 public Trap getTrap(JS k) throws JSExn { return clonee.getTrap(k); }
165 public String coerceToString() { return clonee.coerceToString(); }
168 public static class Obj extends Basket.Hash implements JS {
169 private static final String[] emptystr = new String[0];
170 private static final Placeholder holder = new Placeholder();
172 // HACK: this is hideously, disgustingly ugly.... we need N-ary Hash classes
173 /** entries[index + 0] // key
174 * entries[index + 1] // value
175 * entries[index + 2] // trap
177 protected final int indexmultiple = 3;
179 protected void entryAdded(int p) {}
180 protected void entryUpdated(int p) {}
181 protected void entryRemoved(int p) {}
183 public Obj() { super(4, 0.75F); }
185 public JS unclone() { return this; }
186 public InputStream getInputStream() throws IOException, JSExn { throw new JSExn(
187 "object has not associated stream, class ["+ getClass().getName() +"]"); }
189 public Object run(Object o) throws Exception { throw new JSExn(
190 "object cannot be called, class ["+ getClass().getName() +"]"); }
191 public void pause() { throw new NotPausableException(); }
193 public JS call(JS[] args) throws JSExn { throw new JSExn(
194 "object cannot be called, class ["+ getClass().getName() +"]"); }
195 public JS call(JS method, JS[] args) throws JSExn { throw new JSExn(
196 "method not found: " + JSU.str(method)); }
197 public String[] getFormalArgs() { return emptystr; }
199 public Enumeration keys() throws JSExn {
200 return new Enumeration(null) {
201 private int dest = -1, next = -1;
202 public boolean _hasNext() {
203 for (int i = Math.max(0, dest); i < usedslots; i++)
204 if (i > 0 ? entries[i * indexmultiple] != null : true &&
205 entries[i * indexmultiple] != this) { next = i; return true; }
208 public JS _next() throws JSExn {
209 if (next < 0 && !hasNext()) throw new NoSuchElementException();
210 int index = next; dest = next; next = -1;
211 return (JS)entries[index * indexmultiple];
215 public JS get(JS key) throws JSExn { int i = indexOf(key);
216 return i < 0 ? null : entries[i + 1] instanceof Placeholder ? null : (JS)entries[i + 1]; }
217 public void put(JS key, JS val) throws JSExn {
218 // NOTE: only way value can be stored as null is using declare()
219 int dest = put(indexOf(key), key);
220 if (val == null) entries[dest + 1] = holder;
221 else entries[dest + 1] = val; }
223 public boolean hasKey(JS key) { return indexOf(key) >= 0; }
224 /*public boolean hasValue(JS key, JS value) {
225 int i = indexOf(key); return i >= 0 && entries[i + 1] != null; }
226 public boolean hasTrap(JS key, JS trap) {
227 int i = indexOf(key); return i >= 0 && entries[i + 2] != null; }*/
229 public void declare(JS key) throws JSExn { entries[put(indexOf(key), key) + 1] = null; }
230 public void undeclare(JS key) throws JSExn { remove(indexOf(key)); }
232 public JS putAndTriggerTraps(JS key, JS val) throws JSExn {
233 Trap t = null; int i = indexOf(key);
234 if (i >= 0) t = (Trap)entries[i + 2];
235 if (t != null && (t = t.write()) != null) {
236 val = (JS)new Interpreter(t, val, false).run(null);
238 if (val == null) entries[i + 1] = holder;
239 else entries[i + 1] = val;
242 public JS getAndTriggerTraps(JS key) throws JSExn {
243 Trap t = null; int i = indexOf(key);
244 if (i < 0) return null;
245 t = (Trap)entries[i + 2];
246 return t == null ? (JS)entries[i + 1] : (JS)new Interpreter(t, null, false).run(null);
248 public JS justTriggerTraps(JS key, JS val) throws JSExn {
249 Trap t = null; int i = indexOf(key);
250 if (i >= 0) t = (Trap)entries[i + 2];
251 if (t == null || (t = t.write()) == null) return val;
252 return (JS)new Interpreter(t, val, true).run(null);
255 public void addTrap(JS key, JS f) throws JSExn {
256 if (f.getFormalArgs() == null || f.getFormalArgs().length > 1) throw new JSExn(
257 "traps must take either one argument (write) or no arguments (read)");
258 int i = indexOf(key); if (i < 0) i = put(i, key);
259 for (Trap t = (Trap)entries[i + 2]; t != null; t = t.next())
260 if (t.function().equals(f)) return;
261 entries[i + 2] = new TrapHolder(this, key, f, (Trap)entries[i + 2]);
264 public void delTrap(JS key, JS f) throws JSExn {
265 int i = indexOf(key); if (i < 0) return;
266 Trap t = (Trap)entries[i + 2];
267 if (t.function().equals(f)) { entries[i + 2] = t.next(); return; }
268 for (; t.next() != null; t = t.next())
269 if (t.next().function().equals(f)) { ((TrapHolder)t).next = t.next().next(); return; }
272 public Trap getTrap(JS key) throws JSExn {
273 int i = indexOf(key); return i < 0 ? null : (Trap)entries[i + 2];
276 public String coerceToString() { return "object"; }
278 private static final class Placeholder implements Serializable {}
280 private static final class TrapHolder implements Trap {
281 private final JS target, key, function;
283 TrapHolder(JS t, JS k, JS f, Trap n) { target = t; key = k; function = f; next = n; }
285 public JS key() { return key; }
286 public JS target() { return target; }
287 public JS function() { return function; }
289 public boolean isReadTrap() {
290 return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
291 public boolean isWriteTrap() {
292 return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
294 public Trap next() { return next; }
295 public Trap nextRead() {
296 Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
297 public Trap nextWrite() {
298 Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
299 public Trap read() { return isReadTrap() ? this : nextRead(); }
300 public Trap write() { return isWriteTrap() ? this : nextWrite(); }
304 public interface Trap {
307 public JS function();
308 public boolean isReadTrap();
309 public boolean isWriteTrap();
311 public Trap nextRead();
312 public Trap nextWrite();
314 public Trap read(); // FIXME reconsider these function names
318 /** An empty class used for instanceof matching the result of JS.get()
319 * to decide if the key is a method (to be called with JS.callMethod(). */
320 public static final class Method extends Immutable {}
323 public static abstract class Enumeration extends Immutable {
324 public static final Enumeration EMPTY = new Empty(null);
326 private final Enumeration parent;
328 public Enumeration(Enumeration parent) { this.parent = parent; }
330 protected abstract boolean _hasNext();
331 protected abstract JS _next() throws JSExn;
333 public final boolean hasNext() {
334 return _hasNext() || parent != null ? parent.hasNext() : false; }
335 public final JS next() throws JSExn {
336 if (_hasNext()) return _next();
337 if (parent == null) throw new NoSuchElementException("reached end of set");
338 return parent.next();
341 public JS get(JS key) throws JSExn {
342 //#switch(JSU.str(key))
343 case "hasNext": return JSU.B(hasNext());
344 case "next": return next();
346 return super.get(key);
349 public static final class Empty extends Enumeration {
350 public Empty(Enumeration parent) { super(parent); }
351 protected boolean _hasNext() { return false; }
352 protected JS _next() { throw new NoSuchElementException(); }
355 public static final class JavaIterator extends Enumeration {
356 private final Iterator i;
357 public JavaIterator(Enumeration parent, Iterator i) { super(parent); this.i = i; }
358 protected boolean _hasNext() { return i.hasNext(); }
359 protected JS _next() { return (JS)i.next(); }
362 public static final class RandomAccessList extends Enumeration {
363 private final List l;
364 private int pos = 0, size;
365 public RandomAccessList(Enumeration parent, List list) {
366 super(parent); l = list; size = l.size(); }
367 protected boolean _hasNext() { return pos < size; }
368 protected JS _next() { return (JS)l.get(pos++); }