327d35c4875523188324556b26a32776d02d1bd6
[org.ibex.core.git] / src / org / xwt / js / JS.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] 
2
3 package org.xwt.js; 
4 import org.xwt.util.*; 
5 import org.xwt.*; 
6 import java.io.*;
7 import java.util.*;
8
9 /**
10  *  The public API for the JS engine.  JS itself is actually a class
11  *  implementing the absolute minimal amount of functionality for an
12  *  Object which can be manipulated by JavaScript code.
13  */
14 public class JS { 
15
16     // Public Helper Methods //////////////////////////////////////////////////////////////////////
17
18     /** parse and compile a function */
19     public static JSFunction parse(String sourceName, int firstLine, Reader sourceCode) throws IOException {
20         return new JSFunction(sourceName, firstLine, sourceCode, null);
21     }
22
23     /** coerce an object to a Boolean */
24     public static boolean toBoolean(Object o) {
25         if (o == null) return false;
26         if (o instanceof Boolean) return ((Boolean)o).booleanValue();
27         if (o instanceof Long) return ((Long)o).longValue() != 0;
28         if (o instanceof Integer) return ((Integer)o).intValue() != 0;
29         if (o instanceof Number) {
30             double d = ((Number) o).doubleValue();
31             // NOTE: d == d is a test for NaN. It should be faster than Double.isNaN()
32             return d != 0.0 && d == d;
33         }
34         if (o instanceof String) return ((String)o).length() != 0;
35         return true;
36     }
37
38     /** coerce an object to a Long */
39     public static long toLong(Object o) { return toNumber(o).longValue(); }
40
41     /** coerce an object to an Int */
42     public static int toInt(Object o) { return toNumber(o).intValue(); }
43
44     /** coerce an object to a Double */
45     public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
46
47     /** coerce an object to a Number */
48     public static Number toNumber(Object o) {
49         if (o == null) return new Long(0);
50         if (o instanceof Number) return ((Number)o);
51
52         // NOTE: There are about 3 pages of rules in ecma262 about string to number conversions
53         //       We aren't even close to following all those rules.  We probably never will be.
54         if (o instanceof String)
55             try { return new Double((String)o); } catch (NumberFormatException e) { return new Double(Double.NaN); }
56         if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? new Long(1) : new Long(0);
57         if (o instanceof JS) return ((JS)o).coerceToNumber();
58         throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle");
59     }
60
61     /** coerce an object to a String */
62     public static String toString(Object o) {
63         if(o == null) return "null";
64         if(o instanceof String) return (String) o;
65         if(o instanceof Integer || o instanceof Long || o instanceof Boolean) return o.toString();
66         if(o instanceof JS) return ((JS)o).coerceToString();
67         if(o instanceof Double || o instanceof Float) {
68             double d = ((Number)o).doubleValue();
69             if((int)d == d) return Integer.toString((int)d);
70             return o.toString();
71         }
72         return o.toString();
73     }
74     
75     // Instance Methods ////////////////////////////////////////////////////////////////////
76  
77     // this gets around a wierd fluke in the Java type checking rules for ?..:
78     public static final Object T = Boolean.TRUE;
79     public static final Object F = Boolean.FALSE;
80
81     // FEATURE: be smart here; perhaps intern
82     public static final Number N(int i) { return new Integer(i); }
83     public static final Number N(long l) { return new Long(l); }
84     public static final Number N(double d) { return new Double(d); }
85     public static final Boolean B(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; }
86     
87     private static Enumeration emptyEnumeration = new Enumeration() {
88             public boolean hasMoreElements() { return false; }
89             public Object nextElement() { throw new NoSuchElementException(); }
90         };
91     
92     private Hash entries = null;
93     public Enumeration keys() { return entries == null ? emptyEnumeration : entries.keys(); }
94     public Object get(Object key) { return entries == null ? null : entries.get(key, null); }
95     public void put(Object key, Object val) { if (entries == null) entries = new Hash(); entries.put(key, null, val); }
96
97     // note that we don't actually implement trappable... we just provide these for subclasses which choose to
98     public JSTrap getTrap(Object key) { return entries == null ? null : (JSTrap)entries.get(key, JSTrap.class); }
99     public void putTrap(Object key, JSTrap t) { if (entries == null) entries = new Hash(); entries.put(key, JSTrap.class, t); }
100
101     public Number coerceToNumber() { throw new JS.Exn("tried to coerce a JavaScript object to a Number"); }
102     public String coerceToString() { throw new JS.Exn("tried to coerce a JavaScript object to a String"); }
103     public boolean coerceToBoolean() { throw new JS.Exn("tried to coerce a JavaScript object to a Boolean"); }
104
105     public String typeName() { return "object"; }
106
107
108     // Inner Classes /////////////////////////////////////////////////////////////////////////
109
110     /** An exception which can be thrown and caught by JavaScript code */
111     public static class Exn extends RuntimeException { 
112         private Object js = null; 
113         public Exn(Object js) { this.js = js; } 
114         public String toString() { return "JS.Exn: " + js; }
115         public String getMessage() { return toString(); }
116         public Object getObject() { return js; } 
117     } 
118
119     /** the result of a graft */
120     // FIXME: uber-broken
121     public static class Graft extends JSCallable {
122         private JS graftee;
123         private Object replaced_key;
124         private Object replaced_val;
125         public Graft(Object graftee, Object key, Object val) {
126             if (graftee instanceof JSArray) throw new JS.Exn("can't graft onto JSArrays (yet)");
127             if (graftee instanceof JSCallable) throw new JS.Exn("can't graft onto JSCallables (yet)");
128             if (graftee instanceof JSScope) throw new JS.Exn("can't graft onto JSScopes (yet)");
129             this.graftee = (JS)graftee;
130             replaced_key = key;
131             replaced_val = val;
132         }
133         public boolean equals(Object o) { return (this == o || graftee.equals(o)); }
134         public int hashCode() { return graftee.hashCode(); }
135         public Object get(Object key) { return replaced_key.equals(key) ? replaced_val : graftee.get(key); }
136         public void put(Object key, Object val) { graftee.put(key, val); }
137         /*
138         public Object call(Object method, JSArray args) {
139             if (replaced_key.equals(method)) return ((JSCallable)replaced_val).call(null, args);
140             else if (graftee instanceof JSCallable) return ((JSCallable)graftee).call(method, args);
141             else throw new JS.Exn("cannot call this value");
142         }
143         */
144         public Number coerceToNumber() { return graftee.coerceToNumber(); }
145         public String coerceToString() { return graftee.coerceToString(); }
146         public boolean coerceToBoolean() { return graftee.coerceToBoolean(); }
147         public String typeName() { return graftee.typeName(); }
148         public Enumeration keys() {
149             return new Enumeration() {
150                     Enumeration graftee_enumeration = graftee.keys();
151                     boolean returned_replaced = false;
152                     public boolean hasMoreElements() {
153                         if (graftee_enumeration.hasMoreElements()) return true;
154                         return !returned_replaced;
155                     }
156                     public Object nextElement() {
157                         if (!graftee_enumeration.hasMoreElements()) {
158                             if (returned_replaced) throw new NoSuchElementException();
159                             returned_replaced = true;
160                             return replaced_key;
161                         } else {
162                             Object ret = graftee_enumeration.nextElement();
163                             if (!returned_replaced && ret.equals(replaced_key)) returned_replaced = true;
164                             return ret;
165                         }
166                     }
167                 };
168         }
169     }
170
171
172
173
174