X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Fjs%2FJS.java;h=d187ecbee0331f46ae060fdb67ae189191f71284;hb=08f2221ce8b1a1107ac939997d1ba5386620cf7e;hp=f227f53d88f98916b616cc6540f9b9d932d08c91;hpb=c2a138c3882a4bd8dce0212ac59765af5cb126c6;p=org.ibex.core.git diff --git a/src/org/xwt/js/JS.java b/src/org/xwt/js/JS.java index f227f53..d187ecb 100644 --- a/src/org/xwt/js/JS.java +++ b/src/org/xwt/js/JS.java @@ -6,9 +6,8 @@ import org.xwt.*; import java.io.*; import java.util.*; -// FIXME: grafts /** The minimum set of functionality required for objects which are manipulated by JavaScript */ -public class JS { +public class JS extends org.xwt.util.BalancedTree { // Static Interpreter Control Methods /////////////////////////////////////////////////////////////// @@ -19,12 +18,12 @@ public class JS { public static String getSourceName() { Interpreter c = Interpreter.current(); - return c.f == null ? null : c.f.sourceName; + return c == null || c.f == null ? null : c.f.sourceName; } public static class PausedException extends Exception { PausedException() { } } - public static void invokePauseable(JSFunction function) throws JS.PausedException { + public static void invokePauseable(JSFunction function) throws JS.PausedException, JSExn { Interpreter i = new Interpreter(function, true, new JSArray()); int oldpausecount = i.pausecount; i.resume(); @@ -44,7 +43,7 @@ public class JS { public static class UnpauseCallback { Interpreter i; UnpauseCallback(Interpreter i) { this.i = i; } - public void unpause(Object o) throws PausedException { + public void unpause(Object o) throws PausedException, JSExn { i.stack.push(o); i.resume(); } @@ -115,13 +114,29 @@ public class JS { public static final Object T = Boolean.TRUE; public static final Object F = Boolean.FALSE; - // FIXME: be much smarter here - public static final Number N(int i) { return new Integer(i); } - public static final Number N(long l) { return new Long(l); } - public static final Number N(double d) { return new Double(d); } - public static final Number N(String s) { return new Double(s); } public static final Boolean B(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; } public static final Boolean B(int i) { return i==0 ? Boolean.FALSE : Boolean.TRUE; } + public static final Number N(String s) { return s.indexOf('.') == -1 ? N(Integer.parseInt(s)) : new Double(s); } + public static final Number N(double d) { return (int)d == d ? N((int)d) : new Double(d); } + public static final Number N(long l) { return N((int)l); } + + private static final Integer[] smallIntCache = new Integer[65535 / 4]; + private static final Integer[] largeIntCache = new Integer[65535 / 4]; + public static final Number N(int i) { + Integer ret = null; + int idx = i + smallIntCache.length / 2; + if (idx < smallIntCache.length && idx > 0) { + ret = smallIntCache[idx]; + if (ret != null) return ret; + } + else ret = largeIntCache[Math.abs(idx % largeIntCache.length)]; + if (ret == null || ret.intValue() != i) { + ret = new Integer(i); + if (idx < smallIntCache.length && idx > 0) smallIntCache[idx] = ret; + else largeIntCache[Math.abs(idx % largeIntCache.length)] = ret; + } + return ret; + } private static Enumeration emptyEnumeration = new Enumeration() { public boolean hasMoreElements() { return false; } @@ -129,9 +144,14 @@ public class JS { }; private Hash entries = null; - public Enumeration keys() { return entries == null ? emptyEnumeration : entries.keys(); } - public Object get(Object key) { return entries == null ? null : entries.get(key, null); } - public void put(Object key, Object val) { if (entries == null) entries = new Hash(); entries.put(key, null, val); } + public Enumeration keys() throws JSExn { + return entries == null ? emptyEnumeration : entries.keys(); + } + public Object get(Object key) throws JSExn { return entries == null ? null : entries.get(key, null); } + public void put(Object key, Object val) throws JSExn { + if (entries == null) entries = new Hash(); + entries.put(key, null, val); + } // Trap support ////////////////////////////////////////////////////////////////////////////// @@ -140,14 +160,14 @@ public class JS { protected boolean isTrappable() { return false; } /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */ - public final void putAndTriggerTraps(Object key, Object value) { + public void putAndTriggerTraps(Object key, Object value) throws JSExn { Trap t = getTrap(key); if (t != null) t.invoke(key, value); else put(key, value); } /** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */ - public final Object getAndTriggerTraps(Object key) { + public Object getAndTriggerTraps(Object key) throws JSExn { Trap t = getTrap(key); if (t != null) return t.invoke(key); else return get(key); @@ -184,19 +204,19 @@ public class JS { // return this from get() if the key was actually a method. public static final Object METHOD = new Object(); - public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) { + public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { throw new JSExn("attempted to call the null value (method "+method+")"); - } - public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) { - throw new JSExn("you cannot call this object)"); + } + public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { + throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")"); } // Typing Support ////////////////////////////////////////////////////////////////////////////// - public Number coerceToNumber() { throw new JSExn("tried to coerce a JavaScript object to a Number"); } - public String coerceToString() { throw new JSExn("tried to coerce a JavaScript object to a String"); } - public boolean coerceToBoolean() { throw new JSExn("tried to coerce a JavaScript object to a Boolean"); } + public Number coerceToNumber() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Number"); } + public String coerceToString() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a String"); } + public boolean coerceToBoolean() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Boolean"); } public String typeName() { return "object"; }