2003/05/03 08:46:05
[org.ibex.core.git] / src / org / xwt / js / JS.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] 
2  
3 package org.xwt.js; 
4 import org.xwt.util.*; 
5  
6 /** The public API for the JS engine */
7 public interface JS { 
8  
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; 
13     /*
14     public Num coerceToNumber() throws JS.Exn; 
15     public boolean coerceToBoolean() throws JS.Exn; 
16     public Object call(Object[] args) throws JS.Exn; 
17     */
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; } 
23     } 
24  
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(); 
30     } 
31  
32     /** A mutable, boxed numeric value.  These are recycled -- never duplicate references -- use duplicate() instead. */ 
33         /*
34     public static class Num implements Cloneable, JS { 
35          
36         private Num() { } 
37         public boolean isDouble = false; 
38         public long longVal = -1; 
39         public double doubleVal = -1; 
40          
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(); } 
44          
45         public Num duplicate() { try { return (Num)clone(); } catch (CloneNotSupportedException c) { throw new Error(c); } } 
46  
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"); } 
53     } 
54         */
55