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 /** Put a value to the given key, calling any write traps that have
45 * been placed on the key.
47 * This function may block for an indeterminate amount of time.
49 * Write traps may change the final value before it is put, thus
50 * be careful to read the latest value from this function's return
51 * before doing any more work with it.
53 public JS putAndTriggerTraps(JS key, JS value) throws JSExn;
55 /** Gets the value for a given key, calling any read traps that have
56 * been placed on the key.
58 * This function may block for an indeterminate amount of time.
60 public JS getAndTriggerTraps(JS key) throws JSExn;
62 /** Calls the write traps placed on a given key with the given value
63 * and returns the result without saving it to this object's key store. */
64 public JS justTriggerTraps(JS key, JS value) throws JSExn;
66 public void addTrap(JS key, JS function) throws JSExn;
67 public void delTrap(JS key, JS function) throws JSExn;
68 public Trap getTrap(JS key) throws JSExn;
70 // FIXME: consider renaming/removing these
71 public InputStream getInputStream() throws IOException, JSExn;
73 public String coerceToString();
76 // Implementations ////////////////////////////////////////////////////////
78 /** Provides the lightest possible implementation of JS, throwing
79 * exceptions on every mutable operation. */
80 public static class Immutable implements JS {
81 private static final String[] emptystr = new String[0];
83 public JS unclone() { return this; }
84 public JS.Enumeration keys() throws JSExn { throw new JSExn(
85 "object has no key set, class ["+ getClass().getName() +"]"); }
86 public JS get(JS key) throws JSExn { return null; }
87 public void put(JS key, JS val) throws JSExn { throw new JSExn(
88 "'" + key + "' is read only on class ["+ getClass().getName() +"]"); }
89 public InputStream getInputStream() throws IOException, JSExn { throw new JSExn(
90 "object has not associated stream, class ["+ getClass().getName() +"]"); }
92 public Object run(Object o) throws Exception { throw new JSExn(
93 "object cannot be called, class ["+ getClass().getName() +"]"); }
94 public void pause() { throw new NotPausableException(); }
96 public JS call(JS[] args) throws JSExn { throw new JSExn(
97 "object cannot be called, class ["+ getClass().getName() +"]"); }
98 public JS call(JS method, JS[] args) throws JSExn { throw new JSExn(
99 "method not found: " + JSU.str(method)); }
100 public String[] getFormalArgs() { return emptystr; }
102 public void declare(JS key) throws JSExn { throw new JSExn(
103 "object cannot declare key: "+ JSU.str(key)); }
104 public void undeclare(JS key) throws JSExn { } // FIXME throw error?
106 public JS putAndTriggerTraps(JS key, JS val) throws JSExn { throw new JSExn(
107 "'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); }
108 public JS getAndTriggerTraps(JS key) throws JSExn { return null; } // FIXME throw errors?
109 public JS justTriggerTraps(JS key, JS value) throws JSExn { return null; }
111 public void addTrap(JS key, JS function) throws JSExn { throw new JSExn(
112 "'" + key + "' is not trappable on class ["+ getClass().getName() +"]"); }
113 public void delTrap(JS key, JS function) throws JSExn { throw new JSExn(
114 "'" + key + "' trap is read only on class ["+ getClass().getName() +"]"); }
115 public Trap getTrap(JS key) throws JSExn { throw new JSExn(
116 "'" + key + "' is not trappable on class ["+ getClass().getName() +"]"); }
118 public String coerceToString() { return "object"; }
121 public interface Cloneable {}
123 public static class Clone implements JS {
124 protected final JS clonee;
125 public Clone(JS clonee) throws JSExn {
126 if (!(clonee instanceof Cloneable)) throw new JSExn(
127 clonee.getClass().getName() + " is not implement cloneable");
128 this.clonee = clonee;
131 public JS unclone() { return clonee.unclone(); }
132 public boolean equals(Object o) { return clonee.equals(o); }
134 public Enumeration keys() throws JSExn { return clonee.keys(); }
135 public JS get(JS k) throws JSExn { return clonee.get(k); }
136 public void put(JS k, JS v) throws JSExn { clonee.put(k, v); }
137 public InputStream getInputStream() throws IOException, JSExn {
138 return clonee.getInputStream(); }
140 public Object run(Object o) throws Exception { return clonee.run(o); }
141 public void pause() { clonee.pause(); }
143 public JS call(JS m, JS[] a) throws JSExn { return clonee.call(m, a); }
144 public JS call(JS[] a) throws JSExn { return clonee.call(a); }
145 public String[] getFormalArgs() { return clonee.getFormalArgs(); }
147 public JS putAndTriggerTraps(JS k, JS v) throws JSExn {
148 return clonee.putAndTriggerTraps(k, v); }
149 public JS getAndTriggerTraps(JS k) throws JSExn {
150 return clonee.getAndTriggerTraps(k); }
151 public JS justTriggerTraps(JS k, JS v) throws JSExn {
152 return clonee.justTriggerTraps(k, v); }
154 public void addTrap(JS k, JS f) throws JSExn { clonee.addTrap(k, f); }
155 public void delTrap(JS k, JS f) throws JSExn { clonee.delTrap(k, f); }
156 public Trap getTrap(JS k) throws JSExn { return clonee.getTrap(k); }
158 public String coerceToString() { return clonee.coerceToString(); }
161 public static class Obj extends Basket.Hash implements JS {
162 private static final String[] emptystr = new String[0];
163 private static final Placeholder holder = new Placeholder();
165 // HACK: this is hideously, disgustingly ugly.... we need N-ary Hash classes
166 /** entries[index + 0] // key
167 * entries[index + 1] // value
168 * entries[index + 2] // trap
171 protected void entryAdded(int p) {}
172 protected void entryUpdated(int p) {}
173 protected void entryRemoved(int p) {}
175 public Obj() { super(3, 4, 0.75F); }
177 public JS unclone() { return this; }
178 public InputStream getInputStream() throws IOException, JSExn { throw new JSExn(
179 "object has not associated stream, class ["+ getClass().getName() +"]"); }
181 public Object run(Object o) throws Exception { throw new JSExn(
182 "object cannot be called, class ["+ getClass().getName() +"]"); }
183 public void pause() { throw new NotPausableException(); }
185 public JS call(JS[] args) throws JSExn { throw new JSExn(
186 "object cannot be called, class ["+ getClass().getName() +"]"); }
187 public JS call(JS method, JS[] args) throws JSExn { throw new JSExn(
188 "method not found: " + JSU.str(method)); }
189 public String[] getFormalArgs() { return emptystr; }
191 public Enumeration keys() throws JSExn {
192 return new Enumeration(null) {
193 private int dest = -1, next = -1;
194 public boolean _hasNext() {
195 for (int i = Math.max(0, dest); i < usedslots; i++)
196 if (i > 0 ? entries[i * indexmultiple] != null : true &&
197 entries[i * indexmultiple] != this) { next = i; return true; }
200 public JS _next() throws JSExn {
201 if (next < 0 && !hasNext()) throw new NoSuchElementException();
202 int index = next; dest = next; next = -1;
203 return (JS)entries[index * indexmultiple];
207 public JS get(JS key) throws JSExn { int i = indexOf(key);
208 return i < 0 ? null : entries[i + 1] instanceof Placeholder ? null : (JS)entries[i + 1]; }
209 public void put(JS key, JS val) throws JSExn {
210 // NOTE: only way value can be stored as null is using declare()
211 int dest = put(indexOf(key), key);
212 if (val == null) entries[dest + 1] = holder;
213 else entries[dest + 1] = val; }
215 /*public boolean hasValue(JS key, JS value) {
216 int i = indexOf(key); return i >= 0 && entries[i + 1] != null; }
217 public boolean hasTrap(JS key, JS trap) {
218 int i = indexOf(key); return i >= 0 && entries[i + 2] != null; }*/
220 public void declare(JS key) throws JSExn { entries[put(indexOf(key), key) + 1] = null; }
221 public void undeclare(JS key) throws JSExn { remove(indexOf(key)); }
223 public JS putAndTriggerTraps(JS key, JS val) throws JSExn {
224 Trap t = null; int i = indexOf(key);
225 if (i >= 0) t = (Trap)entries[i + 2];
226 if (t != null && (t = t.write()) != null) {
227 val = (JS)new Interpreter(t, val, false).run(null);
229 put(key, val); // HACK: necessary for subclasses overriding put()
230 /*if (i < 0) i = put(i, key);
231 if (val == null) entries[i + 1] = holder;
232 else entries[i + 1] = val;*/
235 public JS getAndTriggerTraps(JS key) throws JSExn {
236 Trap t = null; int i = indexOf(key);
237 if (i < 0) return get(key); // HACK: necessary for subclasses overriding get()
238 t = (Trap)entries[i + 2];
239 return t == null ? (JS)entries[i + 1] : (JS)new Interpreter(t, null, false).run(null);
241 public JS justTriggerTraps(JS key, JS val) throws JSExn {
242 Trap t = null; int i = indexOf(key);
243 if (i >= 0) t = (Trap)entries[i + 2];
244 if (t == null || (t = t.write()) == null) return val;
245 return (JS)new Interpreter(t, val, true).run(null);
248 public void addTrap(JS key, JS f) throws JSExn {
249 if (f.getFormalArgs() == null || f.getFormalArgs().length > 1) throw new JSExn(
250 "traps must take either one argument (write) or no arguments (read)");
251 int i = indexOf(key); if (i < 0) i = put(i, key);
252 for (Trap t = (Trap)entries[i + 2]; t != null; t = t.next())
253 if (t.function().equals(f)) return;
254 entries[i + 2] = new TrapHolder(this, key, f, (Trap)entries[i + 2]);
257 public void delTrap(JS key, JS f) throws JSExn {
258 int i = indexOf(key); if (i < 0) return;
259 Trap t = (Trap)entries[i + 2];
260 if (t.function().equals(f)) { entries[i + 2] = t.next(); return; }
261 for (; t.next() != null; t = t.next())
262 if (t.next().function().equals(f)) { ((TrapHolder)t).next = t.next().next(); return; }
265 public Trap getTrap(JS key) throws JSExn {
266 int i = indexOf(key); return i < 0 ? null : (Trap)entries[i + 2];
269 public String coerceToString() { return "object"; }
271 private static final class Placeholder implements Serializable {}
273 private static final class TrapHolder implements Trap {
274 private final JS target, key, function;
276 TrapHolder(JS t, JS k, JS f, Trap n) { target = t; key = k; function = f; next = n; }
278 public JS key() { return key; }
279 public JS target() { return target; }
280 public JS function() { return function; }
282 public boolean isReadTrap() {
283 return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
284 public boolean isWriteTrap() {
285 return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
287 public Trap next() { return next; }
288 public Trap nextRead() {
289 Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
290 public Trap nextWrite() {
291 Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
292 public Trap read() { return isReadTrap() ? this : nextRead(); }
293 public Trap write() { return isWriteTrap() ? this : nextWrite(); }
297 public interface Trap {
300 public JS function();
301 public boolean isReadTrap();
302 public boolean isWriteTrap();
304 public Trap nextRead();
305 public Trap nextWrite();
307 public Trap read(); // FIXME reconsider these function names
311 /** An empty class used for instanceof matching the result of JS.get()
312 * to decide if the key is a method (to be called with JS.callMethod(). */
313 public static final class Method extends Immutable {}
316 public static abstract class Enumeration extends Immutable {
317 public static final Enumeration EMPTY = new Empty(null);
319 private final Enumeration parent;
321 public Enumeration(Enumeration parent) { this.parent = parent; }
323 protected abstract boolean _hasNext();
324 protected abstract JS _next() throws JSExn;
326 public final boolean hasNext() {
327 return _hasNext() || parent != null ? parent.hasNext() : false; }
328 public final JS next() throws JSExn {
329 if (_hasNext()) return _next();
330 if (parent == null) throw new NoSuchElementException("reached end of set");
331 return parent.next();
334 public JS get(JS key) throws JSExn {
335 //#switch(JSU.str(key))
336 case "hasNext": return JSU.B(hasNext());
337 case "next": return next();
339 return super.get(key);
342 public static final class Empty extends Enumeration {
343 public Empty(Enumeration parent) { super(parent); }
344 protected boolean _hasNext() { return false; }
345 protected JS _next() { throw new NoSuchElementException(); }
348 public static final class JavaIterator extends Enumeration {
349 private final Iterator i;
350 public JavaIterator(Enumeration parent, Iterator i) { super(parent); this.i = i; }
351 protected boolean _hasNext() { return i.hasNext(); }
352 protected JS _next() { return (JS)i.next(); }
355 public static final class RandomAccessList extends Enumeration {
356 private final List l;
357 private int pos = 0, size;
358 public RandomAccessList(Enumeration parent, List list) {
359 super(parent); l = list; size = l.size(); }
360 protected boolean _hasNext() { return pos < size; }
361 protected JS _next() { return (JS)l.get(pos++); }