X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Fjs%2FJS.java;h=c82435b450c1506fa28d87823ae05de818a9c6bc;hb=16ad8b9430571d806f2aeb18ec472a277ff69423;hp=0f5373cf11243e6f3763ae5a79cdf4e45bb225cb;hpb=e8762b25b82d2113ce27869e7f2f096eca10239e;p=org.ibex.core.git diff --git a/src/org/xwt/js/JS.java b/src/org/xwt/js/JS.java index 0f5373c..c82435b 100644 --- a/src/org/xwt/js/JS.java +++ b/src/org/xwt/js/JS.java @@ -1,4 +1,4 @@ -// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] package org.xwt.js; import org.xwt.util.*; @@ -7,28 +7,56 @@ import java.io.*; import java.util.*; /** 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 /////////////////////////////////////////////////////////////// + public static final Object METHOD = new Object(); + public final JS unclone() { return _unclone(); } + 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 { (entries==null?entries=new Hash():entries).put(key,null,val); } + 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) throws JSExn { + throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")"); + } - public static int getLine() { - Interpreter c = Interpreter.current(); - return c.f == null || c.pc < 0 || c.pc >= c.f.size ? -1 : c.f.line[c.pc]; + JS _unclone() { return this; } + public static class Cloneable extends JS { + public Cloneable() { } + public Object jsclone() throws JSExn { + throw new JSExn("cloning not yet implemented"); + } } - public static String getSourceName() { - Interpreter c = Interpreter.current(); - return c.f == null ? null : c.f.sourceName; - } + public static class Clone extends JS.Cloneable { + protected JS.Cloneable clonee = null; + JS _unclone() { return clonee.unclone(); } + public JS.Cloneable getClonee() { return clonee; } + public Clone(JS.Cloneable clonee) { this.clonee = clonee; } + public boolean equals(Object o) { + if (!(o instanceof JS)) return false; + return unclone() == ((JS)o).unclone(); + } + public Enumeration keys() throws JSExn { return clonee.keys(); } + public Object get(Object key) throws JSExn { return clonee.get(key); } + public void put(Object key, Object val) throws JSExn { clonee.put(key, val); } + public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { + return clonee.callMethod(method, a0, a1, a2, rest, nargs); + } + public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { + return clonee.call(a0, a1, a2, rest, nargs); + } + } - public static class PausedException extends Exception { PausedException() { } } + // Static Interpreter Control Methods /////////////////////////////////////////////////////////////// - public static void invokePauseable(JSFunction function) throws JS.PausedException { - Interpreter i = new Interpreter(function, true, new JSArray()); - int oldpausecount = i.pausecount; - i.resume(); - if (i.pausecount > oldpausecount) throw new PausedException(); - } + /** log a message with the current JavaScript sourceName/line */ + public static void log(Object message) { info(message); } + public static void debug(Object message) { Log.debug(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); } + public static void info(Object message) { Log.info(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); } + public static void warn(Object message) { Log.warn(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); } + public static void error(Object message) { Log.error(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); } public static class NotPauseableException extends Exception { NotPauseableException() { } } @@ -40,10 +68,12 @@ public class JS { return new JS.UnpauseCallback(i); } - public static class UnpauseCallback { + public static class UnpauseCallback implements Scheduler.Task { Interpreter i; UnpauseCallback(Interpreter i) { this.i = i; } - public void unpause(Object o) throws PausedException { + public void perform() throws JSExn { unpause(null); } + public void unpause(Object o) throws JSExn { + // FIXME: if o instanceof JSExn, throw it into the JSworld i.stack.push(o); i.resume(); } @@ -84,10 +114,8 @@ public class JS { // NOTE: There are about 3 pages of rules in ecma262 about string to number conversions // We aren't even close to following all those rules. We probably never will be. - if (o instanceof String) - try { return N((String)o); } catch (NumberFormatException e) { return N(Double.NaN); } + if (o instanceof String) try { return N((String)o); } catch (NumberFormatException e) { return N(Double.NaN); } if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? N(1) : ZERO; - if (o instanceof JS) return ((JS)o).coerceToNumber(); throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle"); } @@ -96,16 +124,16 @@ public class JS { if(o == null) return "null"; if(o instanceof String) return (String) o; if(o instanceof Integer || o instanceof Long || o instanceof Boolean) return o.toString(); - if(o instanceof JS) return ((JS)o).coerceToString(); + if(o instanceof JSArray) return o.toString(); + if(o instanceof JSDate) return o.toString(); if(o instanceof Double || o instanceof Float) { double d = ((Number)o).doubleValue(); if((int)d == d) return Integer.toString((int)d); return o.toString(); } - return o.toString(); + throw new RuntimeException("can't coerce that!"); } - // Instance Methods //////////////////////////////////////////////////////////////////// public static final Integer ZERO = new Integer(0); @@ -114,12 +142,29 @@ public class JS { public static final Object T = Boolean.TRUE; public static final Object F = Boolean.FALSE; - 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; } @@ -127,44 +172,54 @@ 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 static JS fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException { + return JSFunction._fromReader(sourceName, firstLine, sourceCode); + } + + // HACK: caller can't know if the argument is a JSFunction or not... + public static JS cloneWithNewParentScope(JS j, JSScope s) { + return ((JSFunction)j)._cloneWithNewParentScope(s); + } // Trap support ////////////////////////////////////////////////////////////////////////////// - /** override and return true to allow placing traps on this object */ - protected boolean isTrappable() { return false; } + /** override and return true to allow placing traps on this object. + * if isRead true, this is a read trap, otherwise write trap + **/ + protected boolean isTrappable(Object name, boolean isRead) { return true; } /** 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); + if (t != null) t.invoke(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); + if (t != null) return t.invoke(); else return get(key); } /** retrieve a trap from the entries hash */ protected final Trap getTrap(Object key) { - return !isTrappable() || entries == null ? null : (Trap)entries.get(key, Trap.class); + return entries == null ? null : (Trap)entries.get(key, Trap.class); } /** retrieve a trap from the entries hash */ protected final void putTrap(Object key, Trap value) { - if (!isTrappable()) return; if (entries == null) entries = new Hash(); entries.put(key, Trap.class, value); } /** adds a trap, avoiding duplicates */ - protected final void addTrap(Object name, JSFunction f) { + protected final void addTrap(Object name, JSFunction f) throws JSExn { + if (f.numFormalArgs > 1) throw new JSExn("traps must take either one argument (write) or no arguments (read)"); + boolean isRead = f.numFormalArgs == 0; + if (!isTrappable(name, isRead)) throw new JSExn("not allowed "+(isRead?"read":"write")+" trap on property: "+name); for(Trap t = getTrap(name); t != null; t = t.next) if (t.f == f) return; putTrap(name, new Trap(this, name.toString(), f, (Trap)getTrap(name))); } @@ -178,24 +233,4 @@ public class JS { } - // Call Support ////////////////////////////////////////////////////////////////////////////// - - // 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) { - 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)"); - } - - - // 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 String typeName() { return "object"; } - }