be82e70f3d4a57045a794a04e18d6a5865b19121
[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 java.io.*;
6 import java.util.*;
7
8 /**
9  *  The public API for the JS engine.  JS itself is actually a class
10  *  implementing the absolute minimal amount of functionality for an
11  *  Object which can be manipulated by JavaScript code.  The static
12  *  methods, fields, and inner classes of JS define the publicly
13  *  visible API for the XWT JavaScript engine; code outside this
14  *  package should never depend on anything not defined in this file.
15  */
16 public abstract class JS { 
17
18
19     // Public Helper Methods //////////////////////////////////////////////////////////////////////
20
21     /** parse and compile a function */
22     public static CompiledFunction parse(String sourceName, int firstLine, Reader sourceCode) throws IOException {
23         return new CompiledFunction(sourceName, firstLine, sourceCode, null);
24     }
25
26     /** coerce an object to a Boolean */
27     public static boolean toBoolean(Object o) {
28         if (o == null) return false;
29         if (o instanceof Boolean) return ((Boolean)o).booleanValue();
30         if (o instanceof Long) return ((Long)o).longValue() != 0;
31         if (o instanceof Integer) return ((Integer)o).intValue() != 0;
32         if (o instanceof Number) {
33             double d = ((Number) o).doubleValue();
34             // NOTE: d == d is a test for NaN. It should be faster than Double.isNaN()
35             return d != 0.0 && d == d;
36         }
37         if (o instanceof String) return ((String)o).length() != 0;
38         return true;
39     }
40
41     /** coerce an object to a Long */
42     public static long toLong(Object o) { return toNumber(o).longValue(); }
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         // FIXME: There are about 3 pages of rules in ecma262 about string to number conversions
52         // We aren't even close to following all those rules
53         if (o instanceof String) try { return new Double((String)o); } catch (NumberFormatException e) { return new Double(Double.NaN); }
54         if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? new Long(1) : new Long(0);
55         if (o instanceof JS) return ((JS)o).coerceToNumber();
56         throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle");
57     }
58
59
60     // Instance Methods ////////////////////////////////////////////////////////////////////
61  
62     public abstract Object get(Object key) throws JS.Exn; 
63     public abstract void put(Object key, Object val) throws JS.Exn; 
64     public abstract Object[] keys(); 
65
66     public Number coerceToNumber() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a Number"); }
67     public String coerceToString() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a String"); }
68     public boolean coerceToBoolean() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a Boolean"); }
69
70
71     // Inner Classes /////////////////////////////////////////////////////////////////////////
72
73     /** A sensible implementation of the abstract methods in the JS class */
74     public static class Obj extends JS {
75         private Hash entries = new Hash();
76         private boolean sealed = false;
77         public Obj() { this(false); }
78         public Obj(boolean sealed) { this.sealed = sealed; }
79         /** a sealed object cannot have its properties modified */
80         public void setSeal(boolean sealed) { this.sealed = sealed; }
81         public Object get(Object key) { return entries.get(key); }
82         public void put(Object key, Object val) { if (!sealed) entries.put(key, val); }
83         public Object[] keys() { return(entries.keys()); }
84     }
85
86     /** An exception which can be thrown and caught by JavaScript code */
87     public static class Exn extends RuntimeException { 
88         private Object js = null; 
89         public Exn(Object js) { this.js = js; } 
90         public String toString() { return "JS.Exn: " + js; }
91         public String getMessage() { return toString(); }
92         public Object getObject() { return js; } 
93     } 
94
95     /** The publicly-visible face of JavaScript Array objects */
96     public static class Array extends ArrayImpl {
97         public Array() { }
98         public Array(int size) { super(size); }
99         public void setSize(int i) { super.setSize(i); }
100         public int length() { return super.length(); }
101         public Object elementAt(int i) { return super.elementAt(i); }
102         public void addElement(Object o) { super.addElement(o); }
103         public void setElementAt(Object o, int i) { super.setElementAt(o, i); }
104         public Object get(Object key) { return super._get(key); }
105         public void put(Object key, Object val) { super._put(key, val); }
106     }
107
108     /** Any object which becomes part of the scope chain must support this interface */ 
109     public static class Scope extends ScopeImpl { 
110         public Scope(Scope parentScope) { this(parentScope, false); }
111         public Scope(Scope parentScope, boolean sealed) { super(parentScope, sealed); }
112         /** transparent scopes are not returned by THIS */
113         public boolean isTransparent() { return super.isTransparent(); }
114         public boolean has(Object key) { return super.has(key); }
115         public Object get(Object key) { return super._get(key); }
116         public void put(Object key, Object val) { super._put(key, val); }
117         public void declare(String s) { super.declare(s); }
118     } 
119
120     /** anything that is callable with the () operator */
121     public static abstract class Callable extends JS.Obj {
122         public abstract Object call(JS.Array args) throws JS.Exn;
123     }
124
125     /** a Callable which was compiled from JavaScript code */
126     public static class CompiledFunction extends CompiledFunctionImpl {
127         CompiledFunction(String sourceName, int firstLine, Reader sourceCode, Scope scope) throws IOException {
128             super(sourceName, firstLine, sourceCode, scope);
129         }
130     }
131
132     public static final JS Math = new org.xwt.js.Math();
133  
134     /** encapsulates a single JavaScript thread; the JS.Thread->java.lang.Thread mapping is 1:1 */
135     public static class Thread {
136
137         CompiledFunction currentCompiledFunction = null;
138         Vec stack = new Vec();
139         int line = -1;
140
141         /** binds this thread to the current Java Thread */
142         public void bindToCurrentJavaThread() { javaThreadToJSThread.put(java.lang.Thread.currentThread(), this); }
143
144         /** returns the line of code that is currently executing */
145         public int getLine() { return line; }
146
147         /** returns the name of the source code file which declared the currently executing function */
148         public String getSourceName() { return currentCompiledFunction == null ? null : currentCompiledFunction.getSourceName();  }
149
150         /** fetches the currently-executing javascript function */
151         public JS.CompiledFunction getCurrentCompiledFunction() { return currentCompiledFunction; }
152
153
154         // Statics ///////////////////////////////////////////////////////////////////////
155
156         private static Hashtable javaThreadToJSThread = new Hashtable();
157
158         /** returns the JS thread for a given Java thread, creating one if necessary */
159         public static JS.Thread fromJavaThread(java.lang.Thread t) {
160             JS.Thread ret = (JS.Thread)javaThreadToJSThread.get(t);
161             if (ret == null) {
162                 ret = new JS.Thread();
163                 ret.bindToCurrentJavaThread();
164             }
165             return ret;
166         }
167         
168     }
169
170
171
172
173