add JSU.emptyArgs
[org.ibex.js.git] / src / org / ibex / js / JSU.java
1 package org.ibex.js;
2
3 import java.io.Reader;
4 import java.io.IOException;
5 import org.ibex.util.*;
6
7 public class JSU {
8
9     public static final JS[] emptyArgs = new JS[] { };
10
11     /** returns a Pausable which will restart the context;
12      *  expects a value to be pushed onto the stack when unpaused. */
13     public static Pausable pause() throws Pausable.NotPausableException {
14         Interpreter i = Interpreter.current();
15         i.pause();
16         return i;
17     }
18
19     /** Coerce a JS object into a boolean. */
20     public static boolean toBoolean(JS o) {
21         if(o == null) return false;
22         if(o instanceof JSNumber) return ((JSNumber)o).toBoolean();
23         if(o instanceof JSString) return ((JSString)o).s.length() != 0;
24         return true;
25     }
26
27     //#repeat long/int/double/float toLong/toInt/toDouble/toFloat Long/Integer/Double/Float parseLong/parseInt/parseDouble/parseFloat
28     /** Coerce a JS object to a long. */
29     public static long toLong(JS o) throws JSExn {
30         if(o == null) return 0;
31         if(o instanceof JSNumber) return ((JSNumber)o).toLong();
32         if(o instanceof JSString) return Long.parseLong(o.coerceToString());
33         throw new JSExn("can't coerce a " + o.getClass().getName() + " to a number");
34     }
35     //#end
36     
37     /** Coerce a JS object to a String. */
38     public static String toString(JS o) throws JSExn {
39         if(o == null) return "null";
40         return o.coerceToString();
41     }
42
43     
44     public static boolean isInt(JS o) {
45         if(o == null) return true;
46         if(o instanceof JSNumber.I) return true;
47         if(o instanceof JSNumber.B) return false;
48         if(o instanceof JSNumber) {
49             JSNumber n = (JSNumber) o;
50             return n.toInt() == n.toDouble();
51         }
52         if(o instanceof JSString) {
53             String s = ((JSString)o).s;
54             for(int i=0;i<s.length();i++)
55                 if(s.charAt(i) < '0' || s.charAt(i) > '9') return false;
56             return true;
57         }
58         return false;
59     }
60     
61     public static boolean isString(JS o) {
62         if(o instanceof JSString) return true;
63         return false;
64     }
65     
66     // Instance Methods ////////////////////////////////////////////////////////////////////
67  
68     public final static JS NaN = new JSNumber.D(Double.NaN);
69     public final static JS ZERO = new JSNumber.I(0);
70     public final static JS ONE = new JSNumber.I(1);
71     public final static JS MATH = new JSMath();
72         
73     public static final JS T = new JSNumber.B(true);
74     public static final JS F = new JSNumber.B(false);
75
76     public static final JS B(boolean b) { return b ? T : F; }
77     public static final JS B(int i) { return i==0 ? F : T; }
78     
79     private static final int CACHE_SIZE = 65536 / 4; // must be a power of two
80     private static final JSString[] stringCache = new JSString[CACHE_SIZE];
81     public static final JS S(String s) {
82         if(s == null) return null;
83         int slot = s.hashCode()&(CACHE_SIZE-1);
84         JSString ret = stringCache[slot];
85         if(ret == null || !ret.s.equals(s)) stringCache[slot] = ret = new JSString(s);
86         return ret;
87     }
88     public static final JS S(String s, boolean intern) { return intern ? JSString.intern(s) : S(s); }
89
90     public static final JS N(double d) { return new JSNumber.D(d); }
91     public static final JS N(long l) { return new JSNumber.L(l); }
92     
93     public static final JS N(Number n) {
94         if(n instanceof Integer) return N(n.intValue());
95         if(n instanceof Long) return N(n.longValue());
96         return N(n.doubleValue());
97     }
98
99     private static final JSNumber.I negone = new JSNumber.I(-1);
100     private static final JSNumber.I[] icache = new JSNumber.I[128];
101     static { for (int i=0; i < icache.length; i++) icache[i] = new JSNumber.I(i); }
102     public static final JS N(int i) {
103         return i == -1 ? negone : i >= 0 && i < icache.length ? icache[i] : new JSNumber.I(i);
104     }
105
106     /** Internal method for coercing to String without throwing a JSExn. */
107     static String str(JS o) {
108         try { return toString(o); }
109         catch(JSExn e) { return o.toString(); }
110     }
111
112
113     // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
114
115     public static JS fromReader(String sourceName, int firstLine, Reader source) throws IOException {
116         return Parser.fromReader(sourceName, firstLine, source); }
117
118     // FIXME
119     public static JS cloneWithNewGlobalScope(JS js, JS s) {
120         if(js instanceof JSFunction)
121             return ((JSFunction)js)._cloneWithNewParentScope(new JSScope.Top(s));
122         else
123             return js;
124     }
125
126     /** log a message with the current JavaScript sourceName/line */
127     public static void log(Object message) { info(message); }
128     public static void debug(Object message) { Log.debug(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
129     public static void info(Object message) { Log.info(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
130     public static void warn(Object message) { Log.warn(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
131     public static void error(Object message) { Log.error(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
132 }