1
2 package org.xwt.js;
3
4 import org.xwt.util.*;
5 import org.xwt.*;
6 import java.io.*;
7 import java.util.*;
8
9
10 public class JS extends org.xwt.util.BalancedTree {
11
12 public static final Object METHOD = new Object();
13 public final JS unclone() { return _unclone(); }
14 public Enumeration keys() throws JSExn { return entries == null ? emptyEnumeration : entries.keys(); }
15 public Object get(Object key) throws JSExn { return entries == null ? null : entries.get(key, null); }
16 public void put(Object key, Object val) throws JSExn { (entries==null?entries=new Hash():entries).put(key,null,val); }
17 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
18 throw new JSExn("attempted to call the null value (method "+method+")");
19 }
20 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
21 throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
22 }
23
24 JS _unclone() { return this; }
25 public static class Cloneable extends JS {
26 public Cloneable() { }
27 public Object jsclone() throws JSExn {
28 throw new JSExn("cloning not yet implemented");
29 }
30 }
31
32 public static class Clone extends JS.Cloneable {
33 protected JS.Cloneable clonee = null;
34 JS _unclone() { return clonee.unclone(); }
35 public JS.Cloneable getClonee() { return clonee; }
36 public Clone(JS.Cloneable clonee) { this.clonee = clonee; }
37 public boolean equals(Object o) {
38 if (!(o instanceof JS)) return false;
39 return unclone() == ((JS)o).unclone();
40 }
41 public Enumeration keys() throws JSExn { return clonee.keys(); }
42 public Object get(Object key) throws JSExn { return clonee.get(key); }
43 public void put(Object key, Object val) throws JSExn { clonee.put(key, val); }
44 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
45 return clonee.callMethod(method, a0, a1, a2, rest, nargs);
46 }
47 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
48 return clonee.call(a0, a1, a2, rest, nargs);
49 }
50 }
51
52
53
54
55 public static void log(Object message) { info(message); }
56 public static void debug(Object message) { Log.debug(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
57 public static void info(Object message) { Log.info(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
58 public static void warn(Object message) { Log.warn(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
59 public static void error(Object message) { Log.error(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
60
61 public static class NotPauseableException extends Exception { NotPauseableException() { } }
62
63
64 public static UnpauseCallback pause() throws NotPauseableException {
65 Interpreter i = Interpreter.current();
66 if (i.pausecount == -1) throw new NotPauseableException();
67 i.pausecount++;
68 return new JS.UnpauseCallback(i);
69 }
70
71 public static class UnpauseCallback implements Scheduler.Task {
72 Interpreter i;
73 UnpauseCallback(Interpreter i) { this.i = i; }
74 public void perform() throws JSExn { unpause(null); }
75 public void unpause(Object o) throws JSExn {
76
77 i.stack.push(o);
78 i.resume();
79 }
80 }
81
82
83
84
85
86
87 public static boolean toBoolean(Object o) {
88 if (o == null) return false;
89 if (o instanceof Boolean) return ((Boolean)o).booleanValue();
90 if (o instanceof Long) return ((Long)o).longValue() != 0;
91 if (o instanceof Integer) return ((Integer)o).intValue() != 0;
92 if (o instanceof Number) {
93 double d = ((Number) o).doubleValue();
94
95 return d != 0.0 && d == d;
96 }
97 if (o instanceof String) return ((String)o).length() != 0;
98 return true;
99 }
100
101
102 public static long toLong(Object o) { return toNumber(o).longValue(); }
103
104
105 public static int toInt(Object o) { return toNumber(o).intValue(); }
106
107
108 public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
109
110
111 public static Number toNumber(Object o) {
112 if (o == null) return ZERO;
113 if (o instanceof Number) return ((Number)o);
114
115
116
117 if (o instanceof String) try { return N((String)o); } catch (NumberFormatException e) { return N(Double.NaN); }
118 if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? N(1) : ZERO;
119 throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle");
120 }
121
122
123 public static String toString(Object o) {
124 if(o == null) return "null";
125 if(o instanceof String) return (String) o;
126 if(o instanceof Integer || o instanceof Long || o instanceof Boolean) return o.toString();
127 if(o instanceof JSArray) return o.toString();
128 if(o instanceof JSDate) return o.toString();
129 if(o instanceof Double || o instanceof Float) {
130 double d = ((Number)o).doubleValue();
131 if((int)d == d) return Integer.toString((int)d);
132 return o.toString();
133 }
134 throw new RuntimeException("can't coerce that!");
135 }
136
137
138
139 public static final Integer ZERO = new Integer(0);
140
141
142 public static final Object T = Boolean.TRUE;
143 public static final Object F = Boolean.FALSE;
144
145 public static final Boolean B(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; }
146 public static final Boolean B(int i) { return i==0 ? Boolean.FALSE : Boolean.TRUE; }
147 public static final Number N(String s) { return s.indexOf('.') == -1 ? N(Integer.parseInt(s)) : new Double(s); }
148 public static final Number N(double d) { return (int)d == d ? N((int)d) : new Double(d); }
149 public static final Number N(long l) { return N((int)l); }
150
151 private static final Integer[] smallIntCache = new Integer[65535 / 4];
152 private static final Integer[] largeIntCache = new Integer[65535 / 4];
153 public static final Number N(int i) {
154 Integer ret = null;
155 int idx = i + smallIntCache.length / 2;
156 if (idx < smallIntCache.length && idx > 0) {
157 ret = smallIntCache[idx];
158 if (ret != null) return ret;
159 }
160 else ret = largeIntCache[Math.abs(idx % largeIntCache.length)];
161 if (ret == null || ret.intValue() != i) {
162 ret = new Integer(i);
163 if (idx < smallIntCache.length && idx > 0) smallIntCache[idx] = ret;
164 else largeIntCache[Math.abs(idx % largeIntCache.length)] = ret;
165 }
166 return ret;
167 }
168
169 private static Enumeration emptyEnumeration = new Enumeration() {
170 public boolean hasMoreElements() { return false; }
171 public Object nextElement() { throw new NoSuchElementException(); }
172 };
173
174 private Hash entries = null;
175
176 public static JS fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException {
177 return JSFunction._fromReader(sourceName, firstLine, sourceCode);
178 }
179
180
181 public static JS cloneWithNewParentScope(JS j, JSScope s) {
182 return ((JSFunction)j)._cloneWithNewParentScope(s);
183 }
184
185
186
187
188
191 protected boolean isTrappable(Object name, boolean isRead) { return true; }
192
193
194 public void putAndTriggerTraps(Object key, Object value) throws JSExn {
195 Trap t = getTrap(key);
196 if (t != null) t.invoke(value);
197 else put(key, value);
198 }
199
200
201 public Object getAndTriggerTraps(Object key) throws JSExn {
202 Trap t = getTrap(key);
203 if (t != null) return t.invoke();
204 else return get(key);
205 }
206
207
208 protected final Trap getTrap(Object key) {
209 return entries == null ? null : (Trap)entries.get(key, Trap.class);
210 }
211
212
213 protected final void putTrap(Object key, Trap value) {
214 if (entries == null) entries = new Hash();
215 entries.put(key, Trap.class, value);
216 }
217
218
219 protected final void addTrap(Object name, JSFunction f) throws JSExn {
220 if (f.numFormalArgs > 1) throw new JSExn("traps must take either one argument (write) or no arguments (read)");
221 boolean isRead = f.numFormalArgs == 0;
222 if (!isTrappable(name, isRead)) throw new JSExn("not allowed "+(isRead?"read":"write")+" trap on property: "+name);
223 for(Trap t = getTrap(name); t != null; t = t.next) if (t.f == f) return;
224 putTrap(name, new Trap(this, name.toString(), f, (Trap)getTrap(name)));
225 }
226
227
228 protected final void delTrap(Object name, JSFunction f) {
229 Trap t = (Trap)getTrap(name);
230 if (t == null) return;
231 if (t.f == f) { putTrap(t.name, t.next); return; }
232 for(; t.next != null; t = t.next) if (t.next.f == f) { t.next = t.next.next; return; }
233 }
234
235
236 }
237