formatting changes
[org.ibex.js.git] / src / org / ibex / js / JS.java
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.
4
5 package org.ibex.js; 
6
7 import org.ibex.util.*; 
8 import java.io.*;
9 import java.util.*;
10
11 /** The minimum set of functionality required for objects which are manipulated by JavaScript */
12 public interface JS extends Pausable {
13
14     /** Returns an enumeration of the keys in this object. */
15     public JS.Enumeration keys() throws JSExn;
16
17     /** Return the value associated with the given key. */
18     public JS get(JS key) throws JSExn;
19
20     /** Store a specific value against the given key. */
21     public void put(JS key, JS value) throws JSExn;
22
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;
27
28     /** Returns the names of the formal arguments, if any.
29      *  This function must never return a null object. */
30     public String[] getFormalArgs();
31
32     /** Put a value to the given key, calling any write traps that have
33      *  been placed on the key.
34      *
35      *  This function may block for an indeterminate amount of time.
36      *
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.
40      */
41     public JS putAndTriggerTraps(JS key, JS value) throws JSExn;
42
43     /** Gets the value for a given key, calling any read traps that have
44      *  been placed on the key.
45      *
46      *  This function may block for an indeterminate amount of time.
47      */
48     public JS getAndTriggerTraps(JS key) throws JSExn;
49
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;
53
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;
57
58     // FIXME: consider renaming/removing these
59     public JS unclone();
60     public String coerceToString() throws JSExn;
61
62
63     // Implementations ////////////////////////////////////////////////////////
64
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];
69
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
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());
80         }
81         public String[] getFormalArgs() { return emptystr; }
82         public String coerceToString() throws JSExn { throw new JSExn("cannot coerce a "+getClass().getName()+" to a string"); }
83
84         public JS putAndTriggerTraps(JS key, JS val) throws JSExn { throw new JSExn(
85             "'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); }
86         public JS getAndTriggerTraps(JS key) throws JSExn { return null; } // FIXME throw errors?
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; }
93     }
94
95     public interface Cloneable {}
96
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;
102         }
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(); }
117     }
118
119     public static class Obj extends Basket.HashMap implements JS {
120         private static final String[] emptystr = new String[0];
121         private static final Placeholder holder = new Placeholder();
122
123         public Obj() { super(3, 4, 0.75F); }
124
125         public JS unclone() { return this; }
126         public String[] getFormalArgs() { return emptystr; }
127
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)); }
130
131         public Enumeration keys() throws JSExn {
132             final Object[] keys = super.dumpkeys();
133             return new Enumeration(null) {
134                     private int cur = 0;
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++];
139                     }
140                 };
141         }
142
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); }
145
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()
150             return val;
151         }
152         public JS getAndTriggerTraps(JS key) throws JSExn {
153             Trap t = (Trap)super.get(key, 1);
154             return t == null ? (JS)super.get(key) : (JS)new Interpreter(t, null, false).run(null);
155         }
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);
160         }
161
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);
168         }
169
170         public void delTrap(JS key, JS f) throws JSExn {
171             Trap t = (Trap)super.get(key, 1);
172             if (t==null) return;
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; }
176         }
177
178         public Trap getTrap(JS key) throws JSExn { return (Trap)super.get(key, 1); }
179
180         public String coerceToString() throws JSExn { throw new JSExn("cannot coerce a "+getClass().getName()+" to a string"); }
181
182         private static final class Placeholder implements Serializable {}
183
184         private static final class TrapHolder implements Trap {
185             private final JS target, key, function;
186             private Trap next;
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(); }
198         }
199     }
200
201     public interface Trap {
202         public JS key();
203         public JS target();
204         public JS function();
205         public boolean isReadTrap();
206         public boolean isWriteTrap();
207         public Trap next();
208         public Trap nextRead();
209         public Trap nextWrite();
210
211         public Trap read(); // FIXME reconsider these function names
212         public Trap write();
213     }
214
215     /** An empty class used for instanceof matching the result of JS.get()
216      *  to decide if the key is a method (to be called with JS.callMethod(). */
217     public static final class Method extends Immutable {}
218
219
220     public static abstract class Enumeration extends Immutable {
221         public static final Enumeration EMPTY = new Empty(null);
222
223         private final Enumeration parent;
224
225         public Enumeration(Enumeration parent) { this.parent = parent; }
226
227         protected abstract boolean _hasNext();
228         protected abstract JS _next() throws JSExn;
229
230         public final boolean hasNext() { return _hasNext() || (parent != null ? parent.hasNext() : false); }
231         public final JS next() throws JSExn {
232             if (_hasNext()) return _next();
233             if (parent == null) throw new NoSuchElementException("reached end of set");
234             return parent.next();
235         }
236
237         public JS get(JS key) throws JSExn {
238             //#switch(JSU.str(key))
239             case "hasNext": return JSU.B(hasNext());
240             case "next": return next();
241             //#end
242             return super.get(key);
243         }
244
245         public static final class Empty extends Enumeration {
246             public Empty(Enumeration parent) { super(parent); }
247             protected boolean _hasNext() { return false; }
248             protected JS _next() { throw new NoSuchElementException(); }
249         }
250
251         public static final class JavaIterator extends Enumeration {
252             private final Iterator i;
253             public JavaIterator(Enumeration parent, Iterator i) { super(parent); this.i = i; }
254             protected boolean _hasNext() { return i.hasNext(); }
255             protected JS _next() { return (JS)i.next(); }
256         }
257
258         public static final class RandomAccessList extends Enumeration {
259             private final List l;
260             private int pos = 0, size;
261             public RandomAccessList(Enumeration parent, List list) { super(parent); l = list; size = l.size(); }
262             protected boolean _hasNext() { return  pos < size; }
263             protected JS _next() { return (JS)l.get(pos++); }
264         }
265     }
266
267 }