2003/10/31 10:57:24
[org.ibex.core.git] / src / org / xwt / Trap.java
index 3e6302b..2f436c4 100644 (file)
@@ -4,6 +4,7 @@ package org.xwt;
 import java.util.*;
 import org.xwt.js.*;
 import org.xwt.util.*;
+import java.io.*;
 
 /**
  *  This class encapsulates a single trap placed on a given node. The
@@ -20,11 +21,13 @@ public class Trap {
 
     static {
         String[] p = new String[] {
-            "sizetoimage", "shrink", "hshrink", "vshrink", "x", "y", "width", "height",
-            "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"
+            "shrink", "hshrink", "vshrink", "x", "y",
+            "width", "height", "flex", "colspan", "rowspan", "cols",
+            "rows", "align", "invisible", "absolute", "globalx",
+            "globaly", "minwidth", "minheight", "height", "width",
+            "maxwidth", "maxheight", "numchildren", "hpad", "vpad",
+            "buffered", "cursor", "mousex", "mousey",
+            "mouseinside", "thisbox", "indexof", "path", "font", "fontsize"
         };
         for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
     };
@@ -54,16 +57,15 @@ public class Trap {
      *  @param f the function to place as a trap
      */
     static void addTrap(Box trapee, Object name, JS.CompiledFunction f) {
-        if (trapee.traps == null) trapee.traps = new Hash(10, 3);
 
         // check if this script has already placed a trap on this property
-        for(Trap t = (Trap)trapee.traps.get(name); t != null; t = t.next)
+        for(Trap t = (Trap)trapee.get(name, Trap.class); t != null; t = t.next)
             if (t.f == f) return;
         
         // actually place the trap
         Trap t = new Trap();
-        t.next = (Trap)trapee.traps.get(name);
-        trapee.traps.put(name, t);
+        t.next = (Trap)trapee.get(name, Trap.class);
+        trapee.put(name, Trap.class, t);
         t.trapee = trapee;
         t.name = name;
         t.f = f;
@@ -77,18 +79,16 @@ public class Trap {
      *  @param f the function to remove
      */
     static void delTrap(Box trapee, Object name, JS.CompiledFunction f) {
-        if (trapee.traps != null) {
-            Trap t = (Trap)trapee.traps.get(name);
-            if (t.f == f) {
-                trapee.traps.put(name, t.next);
+        Trap t = (Trap)trapee.get(name, Trap.class);
+        if (t.f == f) {
+            trapee.put(name, Trap.class, t.next);
+            return;
+        }
+        for(; t.next != null; t = t.next)
+            if (t.next.f == f) {
+                t.next = t.next.next;
                 return;
             }
-            for(; t.next != null; t = t.next)
-                if (t.next.f == f) {
-                    t.next = t.next.next;
-                    return;
-                }
-        }
         Log.logJS("warning: tried to remove a trap that had not been placed");
     }
 
@@ -97,21 +97,42 @@ public class Trap {
 
     private Trap() { }
 
-    /** the empty object, used for get-traps */
-    public static JS.Array emptyargs = new JS.Array();
+    public Object perform() {
+        try {
+            if (f.getNumFormalArgs() > 0) return cascade();
+            JS.Thread.current().setTailCall(f, new TrapArgs(this));
+            return null;
+        } catch (Exception e) {
+            Log.log(this, "Exception thrown from within trap: " + e);
+            return null;
+        }
+    }
 
-    public Object perform() throws JS.Exn {
-        if (f.getNumFormalArgs() > 0) return cascade();
-        return f.call(new TrapArgs(this));
+    private static JS.CompiledFunction cascadeHelper = null;
+    private static String cascadeHelperText =
+        "return function(q) { var ret = arguments.doTrap;" +
+        "if (ret != false && !arguments.didCascade) arguments.cascade = q; };";
+    static {
+        try {
+            cascadeHelper = JS.parse("cascadeHelper", 1, new StringReader(cascadeHelperText));
+            cascadeHelper = (JS.CompiledFunction)new JS.Thread(cascadeHelper).resume();
+        } catch (Exception e) {
+            Log.log(Trap.class, e);
+        }
     }
-    
-    public void perform(Object val) throws JS.Exn {
-        if (f.getNumFormalArgs()== 0) cascade(val);
-        f.call(new TrapArgs(this, val));
+
+    public void perform(Object val) {
+        try {
+            if (f.getNumFormalArgs() == 0) cascade(val);
+            else JS.Thread.current().setTailCall(cascadeHelper, new TrapArgs(this, val));
+        } catch (Exception e) {
+            Log.log(this, "Exception thrown from within trap: " + e);
+            e.printStackTrace();
+        }
     }
     
     public Object cascade() {
-        if (next != null) return next.perform();
+        if (next != null) { next.perform(); return null; }
         return trapee.get(name, true);
     }
 
@@ -122,11 +143,12 @@ public class Trap {
 
     private static class TrapArgs extends JS.Array {
         private Trap t;
+        public boolean cascadeHappened = false;
         public TrapArgs(Trap t) { this.t = t; }
         public TrapArgs(Trap t, Object value) { this.t = t; addElement(value); }
         
         public void put(Object key, Object val) {
-            if (key.equals("cascade")) t.cascade(val);
+            if (key.equals("cascade")) { cascadeHappened = true; t.cascade(val); }
             else super.put(key, val);
         }
 
@@ -134,8 +156,11 @@ public class Trap {
             // common case
             if(!(key instanceof String)) return super.get(key);
             if (key.equals("trapee")) return t.trapee;
+            if (key.equals("doTrap")) { JS.Thread.current().setTailCall(t.f, this); return null; }
+            if (key.equals("didCascade")) return cascadeHappened ? Boolean.TRUE : Boolean.FALSE;
             if (key.equals("trapname")) return t.name;
             if (key.equals("cascade")) return t.cascade();
+            if (key.equals("callee")) return t.f;
             return super.get(key);
         }
     }