2003/06/18 06:22:49
[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 Number) return o.equals(new Integer(0));
31         return true;
32     }
33
34     /** coerce an object to a Long */
35     public static long toLong(Object o) { return toNumber(o).longValue(); }
36
37     /** coerce an object to a Double */
38     public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
39
40     /** coerce an object to a Number */
41     public static Number toNumber(Object o) {
42         if (o == null) return new Long(0);
43         if (o instanceof Number) return ((Number)o);
44         if (o instanceof String) try { return new Double((String)o); } catch (NumberFormatException e) { return new Double(0); }
45         if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? new Long(1) : new Long(0);
46         if (o instanceof JS) return ((JS)o).coerceToNumber();
47         throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle");
48     }
49
50
51     // Instance Methods ////////////////////////////////////////////////////////////////////
52  
53     public abstract Object get(Object key) throws JS.Exn; 
54     public abstract void put(Object key, Object val) throws JS.Exn; 
55     public abstract Object[] keys(); 
56
57     public Number coerceToNumber() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a Number"); }
58     public String coerceToString() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a String"); }
59     public boolean coerceToBoolean() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a Boolean"); }
60
61
62     // Inner Classes /////////////////////////////////////////////////////////////////////////
63
64     /** A sensible implementation of the abstract methods in the JS class */
65     public static class Obj extends JS {
66         private Hash entries = new Hash();
67         private boolean sealed = false;
68         public Obj() { this(false); }
69         public Obj(boolean sealed) { this.sealed = sealed; }
70         /** a sealed object cannot have its properties modified */
71         public void setSeal(boolean sealed) { this.sealed = sealed; }
72         public Object get(Object key) { return entries.get(key); }
73         public void put(Object key, Object val) { if (!sealed) entries.put(key, val); }
74         public Object[] keys() { return(entries.keys()); }
75     }
76
77     /** An exception which can be thrown and caught by JavaScript code */
78     public static class Exn extends RuntimeException { 
79         private Object js = null; 
80         public Exn(Object js) { this.js = js; } 
81         public String toString() { return "JS.Exn: " + js; }
82         public String getMessage() { return toString(); }
83         public Object getObject() { return js; } 
84     } 
85
86     /** The publicly-visible face of JavaScript Array objects */
87     public static class Array extends ArrayImpl {
88         public Array() { }
89         public Array(int size) { super(size); }
90         public void setSize(int i) { super.setSize(i); }
91         public int length() { return super.length(); }
92         public Object elementAt(int i) { return super.elementAt(i); }
93         public void addElement(Object o) { super.addElement(o); }
94         public void setElementAt(Object o, int i) { super.setElementAt(o, i); }
95         public Object get(Object key) { return super.get(key); }
96         public void put(Object key, Object val) { super.put(key, val); }
97     }
98
99     /** Any object which becomes part of the scope chain must support this interface */ 
100     public static class Scope extends ScopeImpl { 
101         public Scope(Scope parentScope) { this(parentScope, false); }
102         public Scope(Scope parentScope, boolean sealed) { super(parentScope, sealed); }
103         /** transparent scopes are not returned by THIS */
104         public boolean isTransparent() { return super.isTransparent(); }
105         public boolean has(Object key) { return super.has(key); }
106         public Object get(Object key) { return super.get(key); }
107         public void put(Object key, Object val) { super.put(key, val); }
108         public void declare(String s) { super.declare(s); }
109     } 
110
111     /** anything that is callable with the () operator */
112     public static abstract class Callable extends JS.Obj {
113         public abstract Object call(JS.Array args) throws JS.Exn;
114     }
115
116     /** a Callable which was compiled from JavaScript code */
117     public static class CompiledFunction extends CompiledFunctionImpl {
118         public Object call(JS.Array args, JS.Scope scope) throws JS.Exn { return super.call(args, scope); }
119         CompiledFunction(String sourceName, int firstLine, Reader sourceCode, Scope scope) throws IOException {
120             super(sourceName, firstLine, sourceCode, scope);
121         }
122     }
123
124     public static final JS Math = new org.xwt.js.Math();
125  
126     /** encapsulates a single JavaScript thread; the JS.Thread->java.lang.Thread mapping is 1:1 */
127     public static class Thread {
128
129         CompiledFunction currentCompiledFunction = null;
130         Vec stack = new Vec();
131         int line = -1;
132
133         /** binds this thread to the current Java Thread */
134         public void bindToCurrentJavaThread() { javaThreadToJSThread.put(java.lang.Thread.currentThread(), this); }
135
136         /** returns the line of code that is currently executing */
137         public int getLine() { return line; }
138
139         /** returns the name of the source code file which declared the currently executing function */
140         public String getSourceName() { return currentCompiledFunction == null ? null : currentCompiledFunction.getSourceName();  }
141
142         /** fetches the currently-executing javascript function */
143         public JS.CompiledFunction getCurrentCompiledFunction() { return currentCompiledFunction; }
144
145
146         // Statics ///////////////////////////////////////////////////////////////////////
147
148         private static Hashtable javaThreadToJSThread = new Hashtable();
149
150         /** returns the JS thread for a given Java thread, creating one if necessary */
151         public static JS.Thread fromJavaThread(java.lang.Thread t) {
152             JS.Thread ret = (JS.Thread)javaThreadToJSThread.get(t);
153             if (ret == null) {
154                 ret = new JS.Thread();
155                 ret.bindToCurrentJavaThread();
156             }
157             return ret;
158         }
159         
160     }
161
162
163
164
165