4 import java.io.IOException;
5 import org.ibex.util.*;
9 public static final JS[] emptyArgs = new JS[] { };
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();
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;
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");
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();
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();
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;
61 public static boolean isString(JS o) {
62 if(o instanceof JSString) return true;
66 // Instance Methods ////////////////////////////////////////////////////////////////////
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();
73 public static final JS T = new JSNumber.B(true);
74 public static final JS F = new JSNumber.B(false);
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; }
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);
88 public static final JS S(String s, boolean intern) { return intern ? JSString.intern(s) : S(s); }
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); }
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());
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);
106 /** Internal method for coercing to String without throwing a JSExn. */
107 public static String str(JS o) {
108 try { return toString(o); }
109 catch(JSExn e) { return o.toString(); }
113 // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
115 public static JS fromReader(String sourceName, int firstLine, Reader source) throws IOException {
116 return Parser.fromReader(sourceName, firstLine, source); }
119 public static JS cloneWithNewGlobalScope(JS js, JS s) {
120 if(js instanceof JSFunction)
121 return ((JSFunction)js)._cloneWithNewParentScope(new JSScope.Top(s));
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); }