1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
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.
16 public abstract class JS {
19 // Public Helper Methods //////////////////////////////////////////////////////////////////////
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);
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));
34 /** coerce an object to a Long */
35 public static long toLong(Object o) { return toNumber(o).longValue(); }
37 /** coerce an object to a Double */
38 public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
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");
51 // Instance Methods ////////////////////////////////////////////////////////////////////
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();
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"); }
62 // Inner Classes /////////////////////////////////////////////////////////////////////////
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()); }
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; }
86 /** The publicly-visible face of JavaScript Array objects */
87 public static class Array extends ArrayImpl {
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); }
97 /** Any object which becomes part of the scope chain must support this interface */
98 public static class Scope extends ScopeImpl {
99 public Scope(Scope parentScope) { this(parentScope, false); }
100 public Scope(Scope parentScope, boolean sealed) { super(parentScope, sealed); }
101 /** transparent scopes are not returned by THIS */
102 public boolean isTransparent() { return super.isTransparent(); }
103 public boolean has(Object key) { return super.has(key); }
104 public boolean get(Object key) { return super.get(key); }
105 public boolean put(Object key, Object val) { super.put(key, val); }
106 public void declare(String s) { super.declare(s); }
109 /** anything that is callable with the () operator */
110 public static abstract class Callable extends JS.Obj {
111 public abstract Object call(JS.Array args) throws JS.Exn;
114 /** a Callable which was compiled from JavaScript code */
115 public static class CompiledFunction extends CompiledFunctionImpl {
116 public Object call(JS.Array args, JS.Scope scope) throws JS.Exn { return super.call(args, scope); }
117 CompiledFunction(String sourceName, int firstLine, Reader sourceCode, Scope scope) throws IOException {
118 super(sourceName, firstLine, sourceCode, scope);
122 public static final JS Math = new org.xwt.js.Math();
124 /** encapsulates a single JavaScript thread; the JS.Thread->java.lang.Thread mapping is 1:1 */
125 public static class Thread {
127 CompiledFunction currentCompiledFunction = null;
128 Vec stack = new Vec();
131 /** binds this thread to the current Java Thread */
132 public void bindToCurrentJavaThread() { javaThreadToJSThread.put(java.lang.Thread.currentThread(), this); }
134 /** returns the line of code that is currently executing */
135 public int getLine() { return line; }
137 /** returns the name of the source code file which declared the currently executing function */
138 public String getSourceName() { return currentCompiledFunction == null ? null : currentCompiledFunction.getSourceName(); }
140 /** fetches the currently-executing javascript function */
141 public JS.CompiledFunction getCurrentCompiledFunction() { return currentCompiledFunction; }
144 // Statics ///////////////////////////////////////////////////////////////////////
146 private static Hashtable javaThreadToJSThread = new Hashtable();
148 /** returns the JS thread for a given Java thread, creating one if necessary */
149 public static JS.Thread fromJavaThread(java.lang.Thread t) {
150 JS.Thread ret = (JS.Thread)javaThreadToJSThread.get(t);
152 ret = new JS.Thread();
153 ret.bindToCurrentJavaThread();