2003/07/05 02:38:15
authorbrian <brian@xwt.org>
Fri, 30 Jan 2004 07:02:45 +0000 (07:02 +0000)
committerbrian <brian@xwt.org>
Fri, 30 Jan 2004 07:02:45 +0000 (07:02 +0000)
darcs-hash:20040130070245-aa32f-51a842f76a0651ad63181a9626d821eaa7ad5989.gz

src/org/xwt/Box.java
src/org/xwt/MessageQueue.java
src/org/xwt/Trap.java
src/org/xwt/js/ArrayImpl.java
src/org/xwt/js/CompiledFunctionImpl.java
src/org/xwt/js/JS.java
src/org/xwt/js/Regexp.java

index 04bc0cc..1f6cb0c 100644 (file)
@@ -1503,6 +1503,11 @@ public final class Box extends JS.Scope {
         return this;
     }
     
+    // This is the name returned by typeof() by JS
+    public String typeName() {
+        return "box";
+    }
+    
 }
 
 
index 82934cf..ffbe064 100644 (file)
@@ -87,7 +87,8 @@ public class MessageQueue extends Thread {
                 if (Log.on) Log.log(this, "caught throwable in MessageQueue.run(); this should never happen");
                 if (Log.on) Log.log(this, "    currentlyPerforming == " + currentlyPerforming);
                 if (Log.on) Log.log(this, "    working             == " + working);
-                if (Log.on) Log.log(this, "    lastfunc            == " + lastfunc);
+                // FIXME - this currently calls compiledfunction.toString which gives more info than we need
+                // if (Log.on) Log.log(this, "    lastfunc            == " + lastfunc.getDescription());
                 if (Log.on) Log.log(this, "    lastmessage         == " + lastmessage);
                 if (Log.on) Log.log(this, t);
                 if (Log.on) Log.log(this, "resuming MessageQueue loop");
index e3b6c19..7966047 100644 (file)
@@ -15,22 +15,6 @@ public class Trap {
 
     // Static Data //////////////////////////////////////////////////////////////
 
-    /** the arguments.cascade() function */
-    public static final JS.Callable cascadeFunction = new JS.Callable() {
-            public Object call(JS.Array args) {
-                Trap currentTrap = TrapContext.get().currentTrap;
-                if (args.length() != 0) TrapContext.get().putCascadeHappened = true;
-                Trap t = currentTrap.next;
-                // if we've hit the end of the trap stack, just do a put(,,,true)
-                if (t == null) {
-                    if (args.length() == 0) return currentTrap.trapee.get(currentTrap.name, true);
-                    currentTrap.trapee.put(currentTrap.name, args.elementAt(0), true);
-                    return null;
-                }
-                return t.perform(args);
-            }
-        };
-
     /** a vector of weak references to all instances of Trap; used for retheming */
     private static Hashtable allTraps = new Hashtable(1000);
 
@@ -134,44 +118,22 @@ public class Trap {
         return null;
     }
 
-    /** called by Rhino's arguments.trapee hack */
-    public static Object currentTrapee() {
-        Trap current = TrapContext.get().currentTrap;
-        if (current == null) return null;
-        else if (current.rp != null) return current.rp;
-        else return current.trapee;
-    }
-
-    /** called by Rhino's arguments.trapname hack */
-    public static String currentTrapname() {
-        Trap current = TrapContext.get().currentTrap;
-        if (current == null) return null;
-        else return current.name;
-    }
-
-
     // Instance Methods //////////////////////////////////////////////////////////////////////////
 
     private Trap() { allTraps.put(myWeak, dummy); }
 
     /** perform this trap -- arg.length == 0 if this is a get; otherwise it contains a single element to be put */
-    public Object perform(JS.Array args) throws JS.Exn {
-        TrapContext tc = TrapContext.get();
-
-        // save both thread-locals on the stack and update their values
-        Trap save_currentTrap = tc.currentTrap;
-        tc.currentTrap = this;
-
-        boolean save_putCascadeHappened = tc.putCascadeHappened; 
-        tc.putCascadeHappened = false;
+    public Object perform(JS.Array jsArrayArgs) throws JS.Exn {
+        // TrapContext tc = TrapContext.get();
+        TrapArgs args = new TrapArgs(this,jsArrayArgs);
 
         // invoke the trap function
         try {
-            if (!isreadtrap && args.length() == 0) return cascadeFunction.call(args);
+            if (!isreadtrap && args.length() == 0) return cascade(args);
 
             if (f == null) {
                 if (Log.verbose) Log.log(this, "debug: reclaimed a dangling trap on property " + name);
-                Object ret = cascadeFunction.call(args);
+                Object ret = cascade(args);
                 delete();
                 return ret;
             }
@@ -179,16 +141,23 @@ public class Trap {
             Object ret = f.call(args);
             
             // autocascade if required
-            if (args.length() > 0 && !isreadtrap && !tc.putCascadeHappened) cascadeFunction.call(args);
+            if(args.length() > 0 && !isreadtrap && !args.cascadeHappened) cascade(args);
             
             return ret;
             
         } finally {
-            // restore the thread-locals
-            tc.putCascadeHappened = save_putCascadeHappened;
-            tc.currentTrap = save_currentTrap;
-            tc.trapDepth--;
+        
+        }
+    }
+    
+    public Object cascade(JS.Array args) {
+        // if we've hit the end of the trap stack, just do a put(,,,true)
+        if (next == null) {
+            if (args.length() == 0) return trapee.get(name, true);
+            trapee.put(name, args.elementAt(0), true);
+            return null;
         }
+        return next.perform(args);
     }
 
     /** removes this trap */
@@ -203,28 +172,33 @@ public class Trap {
             trapee.surface.keywatchers.removeElement(trapee);
         allTraps.remove(myWeak);
     }
-
-    /** per-thread storage for Traps */
-    private static class TrapContext {
-
-        private static Hash trapContextByThread = new Hash();
-        private TrapContext() { }
-
-        private boolean putCascadeHappened = false;
-        private Trap currentTrap = null;
-        private int trapDepth = 0;
-
-        /** returns the TrapContext for the current thread */
-        static TrapContext get() {
-            TrapContext ret = (TrapContext)trapContextByThread.get(java.lang.Thread.currentThread());
-            if (ret == null) {
-                ret = new TrapContext();
-                trapContextByThread.put(Thread.currentThread(), ret);
-            }
-            return ret;
+    
+    private static class TrapArgs extends JS.Array {
+        public boolean cascadeHappened;
+        private Trap t;
+        public TrapArgs(Trap t,JS.Array args) {
+            int size = args.length();
+            setSize(size);
+            for(int i=0;i<args.length();i++) setElementAt(args.elementAt(i),i);
+            cascadeHappened = false;
+            this.t = t;
+        }
+        
+        public Object get(Object key) {
+            // common case
+            if(!(key instanceof String)) return super.get(key);
+            
+            if(key.equals("trapee")) return t.trapee;
+            if(key.equals("trapname")) return t.name;
+            // FIXME: GETCALL when its available
+            if(key.equals("cascade")) return new JS.Callable() {
+                public Object call(JS.Array args) {
+                    if(args.length() != 0) cascadeHappened = true;
+                    return t.cascade(args);
+                }
+            };
+            return super.get(key);
         }
-
     }
-
 }
 
index 5fc8528..13f7d80 100644 (file)
@@ -25,10 +25,6 @@ class ArrayImpl extends JS.Obj {
     }
     // we use _get instead of get solely to work around a GCJ bug
     public Object _get(Object key) throws JS.Exn {
-        // FIXME: HACK!
-        if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
-        if (key.equals("trapee")) return org.xwt.Trap.currentTrapee();
-        if (key.equals("trapname")) return org.xwt.Trap.currentTrapname();
         if (key.equals("length")) return new Long(vec.size());
         if (key.equals("push")) return new JS.Callable() {
             public Object call(JS.Array args) {
@@ -74,5 +70,7 @@ class ArrayImpl extends JS.Obj {
     public Object elementAt(int i) { return vec.elementAt(i); }
     public void addElement(Object o) { vec.addElement(o); }
     public void setElementAt(Object o, int i) { vec.setElementAt(o, i); }
+    
+    public String typeName() { return "array"; }
 }
 
index 5aff71a..ae344d0 100644 (file)
@@ -33,7 +33,6 @@ class CompiledFunctionImpl extends JSCallable implements ByteCodes, Tokens {
     /** the scope in which this function was declared; by default this function is called in a fresh subscope of the parentScope */
     private JS.Scope parentScope;
 
-
     // Constructors ////////////////////////////////////////////////////////
 
     private CompiledFunctionImpl cloneWithNewParentScope(JS.Scope s) throws IOException {
@@ -136,11 +135,10 @@ class CompiledFunctionImpl extends JSCallable implements ByteCodes, Tokens {
             case TYPEOF: {
                 Object o = t.pop();
                 if (o == null) t.push(null);
-                else if (o instanceof org.xwt.Box) t.push("Box");
-                else if (o instanceof JS) t.push("Object");
-                else if (o instanceof String) t.push("String");
-                else if (o instanceof Number) t.push("Number");
-                else if (o.getClass().getName().startsWith("org.xwt.js.")) t.push(o.getClass().getName().substring(11));
+                else if (o instanceof JS) t.push(((JS)o).typeName());
+                else if (o instanceof String) t.push("string");
+                else if (o instanceof Number) t.push("number");
+                else if (o instanceof Boolean) t.push("boolean");
                 else t.push("unknown");
                 break;
             }
@@ -215,6 +213,8 @@ class CompiledFunctionImpl extends JSCallable implements ByteCodes, Tokens {
                     throw je("tried to put a value to the " + key + " property on the null value");
                 if (!(target instanceof JS))
                     throw je("tried to put a value to the " + key + " property on a " + target.getClass().getName());
+                if (key == null)
+                    throw je("tried to assign \"" + (val==null?"(null)":val.toString()) + "\" to the null key");
                 ((JS)target).put(key, val);
                 t.push(val);
                 break;
@@ -449,12 +449,6 @@ class CompiledFunctionImpl extends JSCallable implements ByteCodes, Tokens {
         String sourceName;
         public FunctionScope(String sourceName, Scope parentScope) { super(parentScope); this.sourceName = sourceName; }
         public String getSourceName() { return sourceName; }
-        public Object get(Object key) throws JS.Exn {
-            if (key.equals("trapee")) return org.xwt.Trap.currentTrapee();
-            else if (key.equals("trapname")) return org.xwt.Trap.currentTrapname();
-            else if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
-            return super.get(key);
-        }
     }
 
 
index 48eb61b..187cb4d 100644 (file)
@@ -69,6 +69,8 @@ public abstract class JS {
     public Number coerceToNumber() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a Number"); }
     public String coerceToString() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a String"); }
     public boolean coerceToBoolean() { throw new Error("you cannot coerce a " + this.getClass().getName() + " into a Boolean"); }
+    
+    public String typeName() { return "object"; }
 
 
     // Inner Classes /////////////////////////////////////////////////////////////////////////
index 694d3ee..dd77580 100644 (file)
@@ -350,4 +350,6 @@ public class Regexp extends JS.Obj {
     private static Boolean wrapBool(int n) {
         return wrapBool(n != 0);
     }
+    
+    public String typeName() { return "regexp"; }
 }