public static final JS B(boolean b) { return b ? T : F; }
public static final JS B(int i) { return i==0 ? F : T; }
- public static final JS S(String s) { return new JSString(s); }
+ private static final int CACHE_SIZE = 65536 / 4; // must be a power of two
+ private static final JSString[] stringCache = new JSString[CACHE_SIZE];
+ public static final JS S(String s) {
+ if(s == null) return null;
+ int slot = s.hashCode()&(CACHE_SIZE-1);
+ JSString ret = stringCache[slot];
+ if(ret == null || !ret.s.equals(s)) stringCache[slot] = ret = new JSString(s);
+ return ret;
+ }
public static final JS N(double d) { return new JSNumber.D(d); }
public static final JS N(long l) { return new JSNumber.L(l); }
return N(n.doubleValue());
}
- private static final JSNumber.I[] smallIntCache = new JSNumber.I[65535 / 4];
- private static final JSNumber.I[] largeIntCache = new JSNumber.I[65535 / 4];
+ private static final JSNumber.I[] smallIntCache = new JSNumber.I[CACHE_SIZE];
+ private static final JSNumber.I[] largeIntCache = new JSNumber.I[CACHE_SIZE];
public static final JS N(int i) {
JSNumber.I ret = null;
int idx = i + smallIntCache.length / 2;
- if (idx < smallIntCache.length && idx > 0) {
+ if (idx < CACHE_SIZE && idx > 0) {
ret = smallIntCache[idx];
if (ret != null) return ret;
}
- else ret = largeIntCache[Math.abs(idx % largeIntCache.length)];
+ else ret = largeIntCache[Math.abs(idx % CACHE_SIZE)];
if (ret == null || ret.i != i) {
ret = new JSNumber.I(i);
if (idx < smallIntCache.length && idx > 0) smallIntCache[idx] = ret;
- else largeIntCache[Math.abs(idx % largeIntCache.length)] = ret;
+ else largeIntCache[Math.abs(idx % CACHE_SIZE)] = ret;
}
return ret;
}