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