4 import java.io.IOException;
5 import org.ibex.util.*;
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();
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;
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");
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();
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();
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;
58 public static boolean isString(JS o) {
59 if(o instanceof JSString) return true;
63 // Instance Methods ////////////////////////////////////////////////////////////////////
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();
70 public static final JS T = new JSNumber.B(true);
71 public static final JS F = new JSNumber.B(false);
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; }
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);
85 public static final JS S(String s, boolean intern) { return intern ? JSString.intern(s) : S(s); }
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); }
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());
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);
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(); }
110 // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
112 public static JS fromReader(String sourceName, int firstLine, Reader source) throws IOException {
113 return Parser.fromReader(sourceName, firstLine, source); }
116 public static JS cloneWithNewGlobalScope(JS js, JS s) {
117 if(js instanceof JSFunction)
118 return ((JSFunction)js)._cloneWithNewParentScope(new JSScope.Top(s));
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); }