X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJS.java;h=cd63638a547ad418c556b4957697a04c20ef08b5;hp=65ba3f63c420e5ba0bd6055d4effe080c7478fb2;hb=a19b897271a8ab6b25aba63e4b30223c2477c28d;hpb=85c8f402be33df8440511492fb597fba9c2eb45f diff --git a/src/org/ibex/js/JS.java b/src/org/ibex/js/JS.java index 65ba3f6..cd63638 100644 --- a/src/org/ibex/js/JS.java +++ b/src/org/ibex/js/JS.java @@ -7,9 +7,6 @@ import java.util.*; /** The minimum set of functionality required for objects which are manipulated by JavaScript */ public abstract class JS { - - public static boolean checkAssertions = false; - public static final JS METHOD = new JS() { }; public final JS unclone() { return _unclone(); } public final JS jsclone() throws JSExn { return new Clone(this); } @@ -34,15 +31,19 @@ public abstract class JS { public final boolean equals(Object o) { return this == o || ((o instanceof JS) && jsequals((JS)o)); } - // FIXME: Remove this, eventually - public final String toString() { throw new Error("you shouldn't use toString() on JS objects"); } + public final String toString() { + // Discourage people from using toString() + String s = "JS Object [class=" + getClass().getName() + "]"; + String ext = extendedToString(); + return ext == null ? s : s + " " + ext; + } // These are only used internally by org.ibex.js Trap getTrap(JS key) { return null; } void putTrap(JS key, Trap value) throws JSExn { throw new JSExn("traps cannot be placed on this object (class=" + this.getClass().getName() +")"); } String coerceToString() throws JSExn { throw new JSExn("can't coerce to a string (class=" + getClass().getName() +")"); } - String debugToString() { return "[class=" + getClass().getName() + "]"; } boolean jsequals(JS o) { return this == o; } + String extendedToString() { return null; } public static class O extends JS implements Cloneable { private Hash entries; @@ -183,7 +184,7 @@ public abstract class JS { public static boolean toBoolean(JS o) { if(o == null) return false; if(o instanceof JSNumber) return ((JSNumber)o).toBoolean(); - if(o instanceof JSString) return ((JSString)o).length() != 0; + if(o instanceof JSString) return ((JSString)o).s.length() != 0; return true; } //#repeat long/int/double/float toLong/toInt/toDouble/toFloat Long/Integer/Double/Float parseLong/parseInt/parseDouble/parseFloat @@ -203,7 +204,7 @@ public abstract class JS { public static String debugToString(JS o) { try { return toString(o); } - catch(JSExn e) { return o.debugToString(); } + catch(JSExn e) { return o.toString(); } } public static boolean isInt(JS o) { @@ -227,12 +228,16 @@ public abstract class JS { if(o instanceof JSString) return true; return false; } + + public static JS newArray() { return new JSArray(); } + public static JS newRegexp(JS pat, JS flags) throws JSExn { return new JSRegexp(pat,flags); } // Instance Methods //////////////////////////////////////////////////////////////////// public final static JS NaN = new JSNumber.D(Double.NaN); public final static JS ZERO = new JSNumber.I(0); public final static JS ONE = new JSNumber.I(1); + public final static JS MATH = new JSMath(); public static final JS T = new JSNumber.B(true); public static final JS F = new JSNumber.B(false); @@ -242,13 +247,14 @@ public abstract class JS { 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) { + 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 S(String s, boolean intern) { return intern ? JSString.intern(s) : S(s); } public static final JS N(double d) { return new JSNumber.D(d); } public static final JS N(long l) { return new JSNumber.L(l); }