2003/08/12 09:59:55
[org.ibex.core.git] / src / org / xwt / Trap.java
index c0dd670..a9023e7 100644 (file)
@@ -24,7 +24,7 @@ public class Trap {
     static {
         String[] p = new String[] {
             "sizetoimage", "shrink", "hshrink", "vshrink", "x", "y", "width", "height",
-            "flex", "align", "invisible", "absolute", "globalx", "globaly",
+            "flex", "hflex", "vflex", /*"cols", "rows",*/ "align", "invisible", "absolute", "globalx", "globaly",
             "minwidth", "minheight", "height", "width", "maxwidth", "maxheight", 
             "numchildren", "hpad", "vpad", "doublebuffered", "cursor",
             "mousex", "mousey", "xwt", "static", "mouseinside", "root", "thisbox", "indexof", "svg"
@@ -44,7 +44,7 @@ public class Trap {
     private JS rp = null;
     
     /** the function for this trap */
-    JS.Function f = null;
+    JS.CompiledFunction f = null;
 
     /** the name of the property that this trap was placed on */
     private String name = null;
@@ -72,7 +72,7 @@ public class Trap {
      *  @param isreadtrap true iff this is a read (double-underscore) trap
      *  @param rp if this trap is being placed via a rootproxy, this is that proxy object.
      */
-    static void addTrap(Box trapee, String name, JS.Function f, boolean isreadtrap, JS rp) {
+    static void addTrap(Box trapee, String name, JS.CompiledFunction f, boolean isreadtrap, JS rp) {
 
         if (PROHIBITED.get(name) != null || name.startsWith("xwt_")) {
             Log.log(Trap.class, "Error: you cannot place traps on special property \"" + name + "\"");
@@ -80,7 +80,9 @@ public class Trap {
         }
 
         // find out what script is currently running
-        String placerNodeName = JS.getCurrentFunctionSourceName();
+        JS.CompiledFunction placer = JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction();
+        if (placer == null) { Log.log(Trap.class, "placer is null"); return; }
+        String placerNodeName = placer.getSourceName();
 
         // check if this script has already placed a trap on this property
         if (trapee.traps == null) trapee.traps = new Hash(10, 3);
@@ -108,7 +110,7 @@ public class Trap {
     public static Trap getTrap(Box b, String name) {
         if (b.traps == null) return null;
 
-        String currentFunctionNodeName = JS.getCurrentFunctionSourceName();
+        String currentFunctionNodeName = JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
         for(Trap cur = (Trap)b.traps.get(name); cur != null; cur = cur.next)
             if (cur.placerNodeName.equals(currentFunctionNodeName))
                 return cur;
@@ -116,84 +118,50 @@ public class Trap {
         return null;
     }
 
-    /** Called by Rhino's arguments.cascade. Note: cx will be null if this was invoked from perform() rather than from a script. */
-    public static final CascadeFunction cascadeFunction = new CascadeFunction();
-    private static class CascadeFunction extends JS.Function {
-        CascadeFunction() { setSeal(true); }
-        public Object _call(JS.Array args) { return _call(args, JS.getCurrentFunction()); }
-        public Object _call(JS.Array args, Function currentFunction) {
-            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);
-        }
-    };
-
-    /** 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) {
-        TrapContext tc = TrapContext.get();
-
-        // save both thread-locals on the stack and update their values
-        Trap save_currentTrap = tc.currentTrap;
-        tc.currentTrap = this;
+    /** the empty object, used for get-traps */
+    public static JS.Array emptyargs = new JS.Array();
 
-        boolean save_putCascadeHappened = tc.putCascadeHappened; 
-        tc.putCascadeHappened = false;
+    /** 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 jsArrayArgs) throws JS.Exn {
+        // TrapContext tc = TrapContext.get();
+        if (jsArrayArgs == null) jsArrayArgs = emptyargs;
+        TrapArgs args = new TrapArgs(this,jsArrayArgs);
 
         // invoke the trap function
         try {
-            if (!isreadtrap && args.length() == 0) return cascadeFunction._call(args, f);
+            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, f);
+                Object ret = cascade(args);
                 delete();
                 return ret;
             }
             
-            Object ret = f._call(args);
+            Object ret = f.call(args);
             
             // autocascade if required
-            if (args.length() > 0 && !isreadtrap && !tc.putCascadeHappened) cascadeFunction._call(args, f);
+            if(args.length() > 0 && !isreadtrap && !args.cascadeHappened) cascade(args);
             
             return ret;
-
-        } catch (JS.Exn e) {
-           if (Log.on) Log.log(this, e);
-
+            
         } finally {
-            // restore the thread-locals
-            tc.putCascadeHappened = save_putCascadeHappened;
-            tc.currentTrap = save_currentTrap;
-            tc.trapDepth--;
+        
         }
-        return null;
+    }
+    
+    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 */
@@ -204,32 +172,39 @@ public class Trap {
             else if (cur.next == null) trapee.traps.remove(name);
             else trapee.traps.put(name, cur.next);
         }
+        /* FIXME
         if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased"))
             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(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);
         }
-
     }
-
 }