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