remove JS.call(JS[]), JS.getInputStream()
[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     /** Executes or unpauses the task, running it in a
24      *  <i>pausable</i> context. */
25     public Object run(Object o) throws Exception, AlreadyRunningException;
26
27     /** Pauses the running task at its convienience. */
28     public void pause() throws NotPausableException;
29
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;
34
35     /** Returns the names of the formal arguments, if any.
36      *  This function must never return a null object. */
37     public String[] getFormalArgs();
38
39     /** Put a value to the given key, calling any write traps that have
40      *  been placed on the key.
41      *
42      *  This function may block for an indeterminate amount of time.
43      *
44      *  Write traps may change the final value  before it is put, thus
45      *  be careful to read the latest value from this function's return
46      *  before doing any more work with it.
47      */
48     public JS putAndTriggerTraps(JS key, JS value) throws JSExn;
49
50     /** Gets the value for a given key, calling any read traps that have
51      *  been placed on the key.
52      *
53      *  This function may block for an indeterminate amount of time.
54      */
55     public JS getAndTriggerTraps(JS key) throws JSExn;
56
57     /** Calls the write traps placed on a given key with the given value
58      *  and returns the result without saving it to this object's key store. */
59     public JS justTriggerTraps(JS key, JS value) throws JSExn;
60
61     public void addTrap(JS key, JS function) throws JSExn;
62     public void delTrap(JS key, JS function) throws JSExn;
63     public Trap getTrap(JS key) throws JSExn;
64
65     // FIXME: consider renaming/removing these
66     public JS unclone();
67     public String coerceToString() throws JSExn;
68
69
70     // Implementations ////////////////////////////////////////////////////////
71
72     /** Provides the lightest possible implementation of JS, throwing
73      *  exceptions on every mutable operation. */
74     public static class Immutable implements JS {
75         private static final String[] emptystr = new String[0];
76
77         public JS unclone() { return this; }
78         public JS.Enumeration keys() throws JSExn {
79             throw new JSExn("object has no key set, class ["+ getClass().getName() +"]"); }
80         public JS get(JS key) throws JSExn { return null; }
81         public void put(JS key, JS val) throws JSExn {
82             throw new JSExn("'" + key + "' is read only on class ["+ getClass().getName() +"]"); }
83         public InputStream getInputStream() throws IOException, JSExn { throw new JSExn(
84             "object has not associated stream, class ["+ getClass().getName() +"]"); }
85
86         public Object run(Object o) throws Exception { throw new JSExn(
87             "object cannot be called, class ["+ getClass().getName() +"]"); }
88         public void pause() { throw new NotPausableException(); }
89
90         public JS call(JS method, JS[] args) throws JSExn {
91             if (method == null) throw new JSExn( "object cannot be called, class ["+ getClass().getName() +"]");
92             throw new JSExn("method not found: " + JSU.str(method) + " class="+this.getClass().getName());
93         }
94         public String[] getFormalArgs() { return emptystr; }
95
96         public JS putAndTriggerTraps(JS key, JS val) throws JSExn { throw new JSExn(
97             "'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); }
98         public JS getAndTriggerTraps(JS key) throws JSExn { return null; } // FIXME throw errors?
99         public JS justTriggerTraps(JS key, JS value) throws JSExn { return null; }
100
101         public void addTrap(JS key, JS function) throws JSExn {
102             Log.warn(this, "'" + JSU.str(key) + "' is not trappable on class ["+ getClass().getName() +"]"); }
103         public void delTrap(JS key, JS function) throws JSExn {
104             Log.warn(this, "'" + JSU.str(key) + "' trap is read only on class ["+ getClass().getName() +"]"); }
105         public Trap getTrap(JS key) throws JSExn {
106             //Log.warn(this, "'" + JSU.str(key) + "' is not trappable on class ["+ getClass().getName() +"]");
107             return null;
108         }
109         public String coerceToString() throws JSExn {
110             throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
111     }
112
113     public interface Cloneable {}
114
115     public static class Clone implements JS {
116         protected final JS clonee;
117         public Clone(JS clonee) throws JSExn {
118             if (!(clonee instanceof Cloneable)) throw new JSExn(clonee.getClass().getName() + " is not implement cloneable");
119             this.clonee = clonee;
120         }
121         public JS unclone() { return clonee.unclone(); }
122         public boolean equals(Object o) { return clonee.equals(o); }
123         public Enumeration keys() throws JSExn { return clonee.keys(); }
124         public JS get(JS k) throws JSExn { return clonee.get(k); }
125         public void put(JS k, JS v) throws JSExn { clonee.put(k, v); }
126
127         // FIMXE: cloning Fountains...
128         //public InputStream getInputStream() throws IOException, JSExn { return clonee.getInputStream(); }
129
130         public Object run(Object o) throws Exception { return clonee.run(o); }
131         public void pause() { clonee.pause(); }
132         public JS call(JS m, JS[] a) throws JSExn { return clonee.call(m, a); }
133         public String[] getFormalArgs() { return clonee.getFormalArgs(); }
134         public JS putAndTriggerTraps(JS k, JS v) throws JSExn { return clonee.putAndTriggerTraps(k, v); }
135         public JS getAndTriggerTraps(JS k) throws JSExn { return clonee.getAndTriggerTraps(k); }
136         public JS justTriggerTraps(JS k, JS v) throws JSExn { return clonee.justTriggerTraps(k, v); }
137         public void addTrap(JS k, JS f) throws JSExn { clonee.addTrap(k, f); }
138         public void delTrap(JS k, JS f) throws JSExn { clonee.delTrap(k, f); }
139         public Trap getTrap(JS k) throws JSExn { return clonee.getTrap(k); }
140         public String coerceToString() throws JSExn { return clonee.coerceToString(); }
141     }
142
143     public static class Obj extends Basket.HashMap implements JS {
144         private static final String[] emptystr = new String[0];
145         private static final Placeholder holder = new Placeholder();
146
147         public Obj() { super(3, 4, 0.75F); }
148
149         public JS unclone() { return this; }
150
151         public Object run(Object o) throws Exception {
152             throw new JSExn("object cannot be called, class ["+ getClass().getName() +"]"); }
153         public void pause() { throw new NotPausableException(); }
154
155         public JS call(JS method, JS[] args) throws JSExn {
156             if (method==null) throw new JSExn("cannot call a " + getClass().getName());
157             throw new JSExn("method not found: " + JSU.str(method)); }
158         public String[] getFormalArgs() { return emptystr; }
159
160         public Enumeration keys() throws JSExn {
161             final Object[] keys = super.dumpkeys();
162             return new Enumeration(null) {
163                     private int cur = 0;
164                     public boolean _hasNext() { return cur < keys.length; }
165                     public JS _next() throws JSExn {
166                         if (cur >= keys.length) throw new NoSuchElementException();
167                         return (JS)keys[cur++];
168                     }
169                 };
170         }
171
172         public JS get(JS key) throws JSExn { return (JS)super.get(key, 0); }
173         public void put(JS key, JS val) throws JSExn { super.put(key, val, 0); }
174
175         public JS putAndTriggerTraps(JS key, JS val) throws JSExn {
176             Trap t = (Trap)super.get(key, 1);
177             if (t != null && (t = t.write()) != null) val = (JS)new Interpreter(t, val, false).run(null);
178             put(key, val); // HACK: necessary for subclasses overriding put()
179             return val;
180         }
181         public JS getAndTriggerTraps(JS key) throws JSExn {
182             Trap t = (Trap)super.get(key, 1);
183             return t == null ? (JS)super.get(key) : (JS)new Interpreter(t, null, false).run(null);
184         }
185         public JS justTriggerTraps(JS key, JS val) throws JSExn {
186             Trap t = (Trap)super.get(key, 1);
187             if (t == null || (t = t.write()) == null) return val;
188             return (JS)new Interpreter(t, val, true).run(null);
189         }
190
191         public void addTrap(JS key, JS f) throws JSExn {
192             if (f.getFormalArgs() == null || f.getFormalArgs().length > 1) throw new JSExn(
193                 "traps must take either one argument (write) or no arguments (read)");
194             for (Trap t = (Trap)super.get(key, 1); t != null; t = t.next())
195                 if (t.function().equals(f)) return;
196             super.put(key, new TrapHolder(this, key, f, (Trap)super.get(key, 1)), 1);
197         }
198
199         public void delTrap(JS key, JS f) throws JSExn {
200             Trap t = (Trap)super.get(key, 1);
201             if (t==null) return;
202             if (t.function().equals(f)) { super.put(key, t.next(), 2); return; }
203             for (; t.next() != null; t = t.next())
204                 if (t.next().function().equals(f)) { ((TrapHolder)t).next = t.next().next(); return; }
205         }
206
207         public Trap getTrap(JS key) throws JSExn { return (Trap)super.get(key, 1); }
208
209         public String coerceToString() throws JSExn {
210             throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
211
212         private static final class Placeholder implements Serializable {}
213
214         private static final class TrapHolder implements Trap {
215             private final JS target, key, function;
216             private Trap next;
217             TrapHolder(JS t, JS k, JS f, Trap n) { target = t; key = k; function = f; next = n; }
218             public JS key() { return key; }
219             public JS target() { return target; }
220             public JS function() { return function; }
221             public boolean isReadTrap()  { return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
222             public boolean isWriteTrap() { return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
223             public Trap next() { return next; }
224             public Trap nextRead() { Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
225             public Trap nextWrite() { Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
226             public Trap read() { return isReadTrap() ? this : nextRead(); }
227             public Trap write() { return isWriteTrap() ? this : nextWrite(); }
228         }
229     }
230
231     public interface Trap {
232         public JS key();
233         public JS target();
234         public JS function();
235         public boolean isReadTrap();
236         public boolean isWriteTrap();
237         public Trap next();
238         public Trap nextRead();
239         public Trap nextWrite();
240
241         public Trap read(); // FIXME reconsider these function names
242         public Trap write();
243     }
244
245     /** An empty class used for instanceof matching the result of JS.get()
246      *  to decide if the key is a method (to be called with JS.callMethod(). */
247     public static final class Method extends Immutable {}
248
249
250     public static abstract class Enumeration extends Immutable {
251         public static final Enumeration EMPTY = new Empty(null);
252
253         private final Enumeration parent;
254
255         public Enumeration(Enumeration parent) { this.parent = parent; }
256
257         protected abstract boolean _hasNext();
258         protected abstract JS _next() throws JSExn;
259
260         public final boolean hasNext() { return _hasNext() || (parent != null ? parent.hasNext() : false); }
261         public final JS next() throws JSExn {
262             if (_hasNext()) return _next();
263             if (parent == null) throw new NoSuchElementException("reached end of set");
264             return parent.next();
265         }
266
267         public JS get(JS key) throws JSExn {
268             //#switch(JSU.str(key))
269             case "hasNext": return JSU.B(hasNext());
270             case "next": return next();
271             //#end
272             return super.get(key);
273         }
274
275         public static final class Empty extends Enumeration {
276             public Empty(Enumeration parent) { super(parent); }
277             protected boolean _hasNext() { return false; }
278             protected JS _next() { throw new NoSuchElementException(); }
279         }
280
281         public static final class JavaIterator extends Enumeration {
282             private final Iterator i;
283             public JavaIterator(Enumeration parent, Iterator i) { super(parent); this.i = i; }
284             protected boolean _hasNext() { return i.hasNext(); }
285             protected JS _next() { return (JS)i.next(); }
286         }
287
288         public static final class RandomAccessList extends Enumeration {
289             private final List l;
290             private int pos = 0, size;
291             public RandomAccessList(Enumeration parent, List list) { super(parent); l = list; size = l.size(); }
292             protected boolean _hasNext() { return  pos < size; }
293             protected JS _next() { return (JS)l.get(pos++); }
294         }
295     }
296
297 }