1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
6 /** The public API for the JS engine */
9 public Object get(Object key) throws JS.Exn;
10 public Object put(Object key, Object val) throws JS.Exn;
11 public Object[] enumerateProperties();
12 public String coerceToString() throws JS.Exn;
14 public Num coerceToNumber() throws JS.Exn;
15 public boolean coerceToBoolean() throws JS.Exn;
16 public Object call(Object[] args) throws JS.Exn;
18 /** if JS calls a Java method, and the Java method throws an exception, it can only be caught by JS if it is a subclass of Exn. */
19 public static class Exn extends RuntimeException {
20 private Object js = null;
21 public Exn(Object js) { this.js = js; }
22 public Object getObject() { return js; }
25 /** Any object which becomes part of the scope chain must support this interface */
26 public static interface Scope extends JS {
27 public boolean has(Object key);
28 public void declare(String s);
29 public JS getParentScope();
32 /** A mutable, boxed numeric value. These are recycled -- never duplicate references -- use duplicate() instead. */
34 public static class Num implements Cloneable, JS {
37 public boolean isDouble = false;
38 public long longVal = -1;
39 public double doubleVal = -1;
41 private static Vec pool = new Vec();
42 public static synchronized void recycle(Num n) { pool.push(n); }
43 public static synchronized Num getOne() { return (pool.size() > 0) ? (Num)pool.pop() : new Num(); }
45 public Num duplicate() { try { return (Num)clone(); } catch (CloneNotSupportedException c) { throw new Error(c); } }
47 public Object get(Object key) throws JS.Exn { return null; }
48 public Object put(Object key, Object val) throws JS.Exn { throw new JS.Exn("attempt to set a property on a Number"); }
49 public Object[] enumerateProperties() { return new Object[] { }; }
50 public String coerceToString() throws JS.Exn { return isDouble ? String.valueOf(doubleVal) : String.valueOf(longVal); }
51 public Num coerceToNumber() throws JS.Exn { return duplicate(); }
52 public Object call(Object[] args) throws JS.Exn { throw new JS.Exn("attempt to apply the () operator to a Number"); }