f1f67264141745d87602655c0cddc3e70015e66e
[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 abstract 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     public abstract Object get(Object key) throws JS.Exn; 
78     public void put(Object key, Object val) throws JS.Exn { throw new JS.Exn("you cannot put to this object"); }
79
80     public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); }
81
82     public Number coerceToNumber() { throw new JS.Exn("tried to coerce a JavaScript object to a Number"); }
83     public String coerceToString() { throw new JS.Exn("tried to coerce a JavaScript object to a String"); }
84     public boolean coerceToBoolean() { throw new JS.Exn("tried to coerce a JavaScript object to a Boolean"); }
85
86     public String typeName() { return "object"; }
87
88
89     // Inner Classes /////////////////////////////////////////////////////////////////////////
90
91     /** An exception which can be thrown and caught by JavaScript code */
92     public static class Exn extends RuntimeException { 
93         private Object js = null; 
94         public Exn(Object js) { this.js = js; } 
95         public String toString() { return "JS.Exn: " + js; }
96         public String getMessage() { return toString(); }
97         public Object getObject() { return js; } 
98     } 
99
100     /** the result of a graft */
101     public static class Graft extends JSCallable {
102         private JS graftee;
103         private Object replaced_key;
104         private Object replaced_val;
105         public Graft(Object graftee, Object key, Object val) {
106             if (graftee instanceof JSArray) throw new JS.Exn("can't graft onto JSArrays (yet)");
107             if (graftee instanceof JSCallable) throw new JS.Exn("can't graft onto JSCallables (yet)");
108             if (graftee instanceof JSScope) throw new JS.Exn("can't graft onto JSScopes (yet)");
109             this.graftee = (JS)graftee;
110             replaced_key = key;
111             replaced_val = val;
112         }
113         public boolean equals(Object o) { return (this == o || graftee.equals(o)); }
114         public int hashCode() { return graftee.hashCode(); }
115         public Object get(Object key) { return replaced_key.equals(key) ? replaced_val : graftee.get(key); }
116         public void put(Object key, Object val) { graftee.put(key, val); }
117         public Object call(Object method, JSArray args) {
118             if (replaced_key.equals(method)) return ((JSCallable)replaced_val).call(null, args);
119             else if (graftee instanceof JSCallable) return ((JSCallable)graftee).call(method, args);
120             else throw new JS.Exn("cannot call this value");
121         }
122         public Number coerceToNumber() { return graftee.coerceToNumber(); }
123         public String coerceToString() { return graftee.coerceToString(); }
124         public boolean coerceToBoolean() { return graftee.coerceToBoolean(); }
125         public String typeName() { return graftee.typeName(); }
126         public Enumeration keys() {
127             return new Enumeration() {
128                     Enumeration graftee_enumeration = graftee.keys();
129                     boolean returned_replaced = false;
130                     public boolean hasMoreElements() {
131                         if (graftee_enumeration.hasMoreElements()) return true;
132                         return !returned_replaced;
133                     }
134                     public Object nextElement() {
135                         if (!graftee_enumeration.hasMoreElements()) {
136                             if (returned_replaced) throw new NoSuchElementException();
137                             returned_replaced = true;
138                             return replaced_key;
139                         } else {
140                             Object ret = graftee_enumeration.nextElement();
141                             if (!returned_replaced && ret.equals(replaced_key)) returned_replaced = true;
142                             return ret;
143                         }
144                     }
145                 };
146         }
147     }
148
149
150
151
152