remove JS.run() and JS.pause()
[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         public InputStream getInputStream() throws IOException, JSExn { throw new JSExn(
77             "object has not associated stream, class ["+ getClass().getName() +"]"); }
78
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());
82         }
83         public String[] getFormalArgs() { return emptystr; }
84
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; }
89
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() +"]");
96             return null;
97         }
98         public String coerceToString() throws JSExn {
99             throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
100     }
101
102     public interface Cloneable {}
103
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;
109         }
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); }
115
116         // FIMXE: cloning Fountains...
117         //public InputStream getInputStream() throws IOException, JSExn { return clonee.getInputStream(); }
118
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(); }
128     }
129
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();
133
134         public Obj() { super(3, 4, 0.75F); }
135
136         public JS unclone() { return this; }
137
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; }
142
143         public Enumeration keys() throws JSExn {
144             final Object[] keys = super.dumpkeys();
145             return new Enumeration(null) {
146                     private int cur = 0;
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++];
151                     }
152                 };
153         }
154
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); }
157
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()
162             return val;
163         }
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);
167         }
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);
172         }
173
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);
180         }
181
182         public void delTrap(JS key, JS f) throws JSExn {
183             Trap t = (Trap)super.get(key, 1);
184             if (t==null) return;
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; }
188         }
189
190         public Trap getTrap(JS key) throws JSExn { return (Trap)super.get(key, 1); }
191
192         public String coerceToString() throws JSExn {
193             throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
194
195         private static final class Placeholder implements Serializable {}
196
197         private static final class TrapHolder implements Trap {
198             private final JS target, key, function;
199             private Trap next;
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(); }
211         }
212     }
213
214     public interface Trap {
215         public JS key();
216         public JS target();
217         public JS function();
218         public boolean isReadTrap();
219         public boolean isWriteTrap();
220         public Trap next();
221         public Trap nextRead();
222         public Trap nextWrite();
223
224         public Trap read(); // FIXME reconsider these function names
225         public Trap write();
226     }
227
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 {}
231
232
233     public static abstract class Enumeration extends Immutable {
234         public static final Enumeration EMPTY = new Empty(null);
235
236         private final Enumeration parent;
237
238         public Enumeration(Enumeration parent) { this.parent = parent; }
239
240         protected abstract boolean _hasNext();
241         protected abstract JS _next() throws JSExn;
242
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();
248         }
249
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();
254             //#end
255             return super.get(key);
256         }
257
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(); }
262         }
263
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(); }
269         }
270
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++); }
277         }
278     }
279
280 }