removed more getInputStream() stuff
[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
83         public JS putAndTriggerTraps(JS key, JS val) throws JSExn { throw new JSExn(
84             "'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); }
85         public JS getAndTriggerTraps(JS key) throws JSExn { return null; } // FIXME throw errors?
86         public JS justTriggerTraps(JS key, JS value) throws JSExn { return null; }
87
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 {
93             //Log.warn(this, "'" + JSU.str(key) + "' is not trappable on class ["+ getClass().getName() +"]");
94             return null;
95         }
96         public String coerceToString() throws JSExn {
97             throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
98     }
99
100     public interface Cloneable {}
101
102     public static class Clone implements JS {
103         protected final JS clonee;
104         public Clone(JS clonee) throws JSExn {
105             if (!(clonee instanceof Cloneable)) throw new JSExn(clonee.getClass().getName() + " is not implement cloneable");
106             this.clonee = clonee;
107         }
108         public JS unclone() { return clonee.unclone(); }
109         public boolean equals(Object o) { return clonee.equals(o); }
110         public Enumeration keys() throws JSExn { return clonee.keys(); }
111         public JS get(JS k) throws JSExn { return clonee.get(k); }
112         public void put(JS k, JS v) throws JSExn { clonee.put(k, v); }
113         public JS call(JS m, JS[] a) throws JSExn { return clonee.call(m, a); }
114         public String[] getFormalArgs() { return clonee.getFormalArgs(); }
115         public JS putAndTriggerTraps(JS k, JS v) throws JSExn { return clonee.putAndTriggerTraps(k, v); }
116         public JS getAndTriggerTraps(JS k) throws JSExn { return clonee.getAndTriggerTraps(k); }
117         public JS justTriggerTraps(JS k, JS v) throws JSExn { return clonee.justTriggerTraps(k, v); }
118         public void addTrap(JS k, JS f) throws JSExn { clonee.addTrap(k, f); }
119         public void delTrap(JS k, JS f) throws JSExn { clonee.delTrap(k, f); }
120         public Trap getTrap(JS k) throws JSExn { return clonee.getTrap(k); }
121         public String coerceToString() throws JSExn { return clonee.coerceToString(); }
122     }
123
124     public static class Obj extends Basket.HashMap implements JS {
125         private static final String[] emptystr = new String[0];
126         private static final Placeholder holder = new Placeholder();
127
128         public Obj() { super(3, 4, 0.75F); }
129
130         public JS unclone() { return this; }
131
132         public JS call(JS method, JS[] args) throws JSExn {
133             if (method==null) throw new JSExn("cannot call a " + getClass().getName());
134             throw new JSExn("method not found: " + JSU.str(method)); }
135         public String[] getFormalArgs() { return emptystr; }
136
137         public Enumeration keys() throws JSExn {
138             final Object[] keys = super.dumpkeys();
139             return new Enumeration(null) {
140                     private int cur = 0;
141                     public boolean _hasNext() { return cur < keys.length; }
142                     public JS _next() throws JSExn {
143                         if (cur >= keys.length) throw new NoSuchElementException();
144                         return (JS)keys[cur++];
145                     }
146                 };
147         }
148
149         public JS get(JS key) throws JSExn { return (JS)super.get(key, 0); }
150         public void put(JS key, JS val) throws JSExn { super.put(key, val, 0); }
151
152         public JS putAndTriggerTraps(JS key, JS val) throws JSExn {
153             Trap t = (Trap)super.get(key, 1);
154             if (t != null && (t = t.write()) != null) val = (JS)new Interpreter(t, val, false).run(null);
155             put(key, val); // HACK: necessary for subclasses overriding put()
156             return val;
157         }
158         public JS getAndTriggerTraps(JS key) throws JSExn {
159             Trap t = (Trap)super.get(key, 1);
160             return t == null ? (JS)super.get(key) : (JS)new Interpreter(t, null, false).run(null);
161         }
162         public JS justTriggerTraps(JS key, JS val) throws JSExn {
163             Trap t = (Trap)super.get(key, 1);
164             if (t == null || (t = t.write()) == null) return val;
165             return (JS)new Interpreter(t, val, true).run(null);
166         }
167
168         public void addTrap(JS key, JS f) throws JSExn {
169             if (f.getFormalArgs() == null || f.getFormalArgs().length > 1) throw new JSExn(
170                 "traps must take either one argument (write) or no arguments (read)");
171             for (Trap t = (Trap)super.get(key, 1); t != null; t = t.next())
172                 if (t.function().equals(f)) return;
173             super.put(key, new TrapHolder(this, key, f, (Trap)super.get(key, 1)), 1);
174         }
175
176         public void delTrap(JS key, JS f) throws JSExn {
177             Trap t = (Trap)super.get(key, 1);
178             if (t==null) return;
179             if (t.function().equals(f)) { super.put(key, t.next(), 2); return; }
180             for (; t.next() != null; t = t.next())
181                 if (t.next().function().equals(f)) { ((TrapHolder)t).next = t.next().next(); return; }
182         }
183
184         public Trap getTrap(JS key) throws JSExn { return (Trap)super.get(key, 1); }
185
186         public String coerceToString() throws JSExn {
187             throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
188
189         private static final class Placeholder implements Serializable {}
190
191         private static final class TrapHolder implements Trap {
192             private final JS target, key, function;
193             private Trap next;
194             TrapHolder(JS t, JS k, JS f, Trap n) { target = t; key = k; function = f; next = n; }
195             public JS key() { return key; }
196             public JS target() { return target; }
197             public JS function() { return function; }
198             public boolean isReadTrap()  { return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
199             public boolean isWriteTrap() { return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
200             public Trap next() { return next; }
201             public Trap nextRead() { Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
202             public Trap nextWrite() { Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
203             public Trap read() { return isReadTrap() ? this : nextRead(); }
204             public Trap write() { return isWriteTrap() ? this : nextWrite(); }
205         }
206     }
207
208     public interface Trap {
209         public JS key();
210         public JS target();
211         public JS function();
212         public boolean isReadTrap();
213         public boolean isWriteTrap();
214         public Trap next();
215         public Trap nextRead();
216         public Trap nextWrite();
217
218         public Trap read(); // FIXME reconsider these function names
219         public Trap write();
220     }
221
222     /** An empty class used for instanceof matching the result of JS.get()
223      *  to decide if the key is a method (to be called with JS.callMethod(). */
224     public static final class Method extends Immutable {}
225
226
227     public static abstract class Enumeration extends Immutable {
228         public static final Enumeration EMPTY = new Empty(null);
229
230         private final Enumeration parent;
231
232         public Enumeration(Enumeration parent) { this.parent = parent; }
233
234         protected abstract boolean _hasNext();
235         protected abstract JS _next() throws JSExn;
236
237         public final boolean hasNext() { return _hasNext() || (parent != null ? parent.hasNext() : false); }
238         public final JS next() throws JSExn {
239             if (_hasNext()) return _next();
240             if (parent == null) throw new NoSuchElementException("reached end of set");
241             return parent.next();
242         }
243
244         public JS get(JS key) throws JSExn {
245             //#switch(JSU.str(key))
246             case "hasNext": return JSU.B(hasNext());
247             case "next": return next();
248             //#end
249             return super.get(key);
250         }
251
252         public static final class Empty extends Enumeration {
253             public Empty(Enumeration parent) { super(parent); }
254             protected boolean _hasNext() { return false; }
255             protected JS _next() { throw new NoSuchElementException(); }
256         }
257
258         public static final class JavaIterator extends Enumeration {
259             private final Iterator i;
260             public JavaIterator(Enumeration parent, Iterator i) { super(parent); this.i = i; }
261             protected boolean _hasNext() { return i.hasNext(); }
262             protected JS _next() { return (JS)i.next(); }
263         }
264
265         public static final class RandomAccessList extends Enumeration {
266             private final List l;
267             private int pos = 0, size;
268             public RandomAccessList(Enumeration parent, List list) { super(parent); l = list; size = l.size(); }
269             protected boolean _hasNext() { return  pos < size; }
270             protected JS _next() { return (JS)l.get(pos++); }
271         }
272     }
273
274 }