2003/06/07 11:04:13
[org.ibex.core.git] / src / org / xwt / Trap.java
index d2b3954..8d9404e 100644 (file)
@@ -1,9 +1,9 @@
 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt;
 
-import org.xwt.util.*;
 import java.util.*;
-import org.mozilla.javascript.*;
+import org.xwt.js.*;
+import org.xwt.util.*;
 
 /**
  *  This class encapsulates a single trap placed on a given node. The
@@ -16,7 +16,7 @@ public class Trap {
     // Static Data //////////////////////////////////////////////////////////////
 
     /** a vector of weak references to all instances of Trap; used for retheming */
-    private static Vec allTraps = new Vec(1000);
+    private static Hashtable allTraps = new Hashtable(1000);
 
     /** List of properties that cannot be trapped */
     private static final Hash PROHIBITED = new Hash(120, 3);
@@ -32,16 +32,19 @@ public class Trap {
         for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
     };
 
+    private static Object dummy = new Object();
+
+
     // Instance Members ////////////////////////////////////////////////////////
 
     /** the box on which this trap was placed */
     private Box trapee = null;
 
     /** If this trap was actually placed on a root proxy, this is a reference to the proxy */
-    private Scriptable rp = null;
+    private JS rp = null;
     
     /** the function for this trap */
-    Function f = null;
+    JS.Function f = null;
 
     /** the name of the property that this trap was placed on */
     private String name = null;
@@ -55,8 +58,9 @@ public class Trap {
     /** The nodeName of the Template whose script placed the trap. See Template.nodeName for more detail. */
     private String placerNodeName = null;
 
-    /** the index of this Trap in the allTraps vector */
-    private int indexInAllTraps = 0;
+    /** our weak reference in the allTraps Hash */
+    private Weak myWeak = Platform.getWeak(this);
+
 
     // Static Methods //////////////////////////////////////////////////////////////////////////
 
@@ -68,15 +72,15 @@ 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, Function f, boolean isreadtrap, Scriptable rp) {
+    static void addTrap(Box trapee, String name, JS.Function f, boolean isreadtrap, JS rp) {
 
         if (PROHIBITED.get(name) != null || name.startsWith("xwt_")) {
-            System.out.println("Error: you cannot place traps on special property \"" + name + "\"");
+            Log.log(Trap.class, "Error: you cannot place traps on special property \"" + name + "\"");
             return;
         }
 
         // find out what script is currently running
-        String placerNodeName = JSObject.getCurrentFunctionSourceName();
+        String placerNodeName = JS.getCurrentFunctionSourceName();
 
         // check if this script has already placed a trap on this property
         if (trapee.traps == null) trapee.traps = new Hash(10, 3);
@@ -104,7 +108,7 @@ public class Trap {
     public static Trap getTrap(Box b, String name) {
         if (b.traps == null) return null;
 
-        String currentFunctionNodeName = JSObject.getCurrentFunctionSourceName();
+        String currentFunctionNodeName = JS.getCurrentFunctionSourceName();
         for(Trap cur = (Trap)b.traps.get(name); cur != null; cur = cur.next)
             if (cur.placerNodeName.equals(currentFunctionNodeName))
                 return cur;
@@ -113,23 +117,27 @@ public class Trap {
     }
 
     /** 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 Function cascadeFunction = new CascadeFunction();
-    private static class CascadeFunction extends JSObject implements Function {
+    public static final CascadeFunction cascadeFunction = new CascadeFunction();
+    private static class CascadeFunction extends JS.Function {
         CascadeFunction() { setSeal(true); }
-        public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
-        public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) {
+        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 (currentTrap == null || (cx != null && Context.getCurrentContext().currentFunction != currentTrap.f)) {
+           /*
+            if (currentTrap == null || (currentFunction != currentTrap.f)) {
                 if (Log.on) Log.log(this, "attempt to cascade() by a function that was not invoked as a trap at " +
-                                    cx.interpreterSourceFile + ":" + cx.interpreterLine);
+                                   currentFunction.getSourceName());
+                if (Log.on) Log.log(this, "currentfunction == " + currentFunction);
+                if (Log.on) Log.log(this, "currentTrap.f == " + currentTrap.f);
                 return null;
             }
-            if (args.length != 0) TrapContext.get().putCascadeHappened = true;
+           */
+            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, currentTrap.trapee, true);
-                currentTrap.trapee.put(currentTrap.name, currentTrap.trapee, args[0], true);
+                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);
@@ -144,30 +152,20 @@ public class Trap {
         else return current.trapee;
     }
 
-    /** removes all traps whose function's ultimate parent scope is <tt>b</tt>. Used for retheming */
-    public static void removeAllTrapsByBox(Box b) {
-        for(int i=0; i<allTraps.size(); i++) {
-            Trap t = (Trap)((Weak)allTraps.elementAt(i)).get();
-            if (t == null) continue;
-            for(Scriptable cur = t.f; cur != null; cur = cur.getParentScope()) {
-                if (cur == b) {
-                    t.delete();
-                    i--;
-                    break;
-                }
-            }
-        }
+    /** 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() {
-        indexInAllTraps = allTraps.size();
-        allTraps.addElement(Platform.getWeak(this));
-    }
+    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(Object[] arg) {
+    public Object perform(JS.Array args) {
         TrapContext tc = TrapContext.get();
 
         // save both thread-locals on the stack and update their values
@@ -179,28 +177,25 @@ public class Trap {
 
         // invoke the trap function
         try {
-            if (!isreadtrap && arg.length == 0) return cascadeFunction.call(null, null, null, arg);
+            if (!isreadtrap && args.length() == 0) return cascadeFunction._call(args, f);
 
             if (f == null) {
                 if (Log.verbose) Log.log(this, "debug: reclaimed a dangling trap on property " + name);
-                Object ret = cascadeFunction.call(null, null, null, arg);
+                Object ret = cascadeFunction._call(args, f);
                 delete();
                 return ret;
             }
             
-            Object ret = f.call(Context.enter(), f.getParentScope(), f.getParentScope(), arg);
+            Object ret = f._call(args);
             
             // autocascade if required
-            if (arg.length > 0 && !isreadtrap && !tc.putCascadeHappened) cascadeFunction.call(null, null, null, arg);
+            if (args.length() > 0 && !isreadtrap && !tc.putCascadeHappened) cascadeFunction._call(args, f);
             
             return ret;
 
-        } catch (EcmaError e) {
-            if (Log.on) Log.log(this, "WARNING: uncaught interpreter exception: " + e.getMessage());
-            if (Log.on) Log.log(this, "         thrown from within trap '" + name + "' at " + e.getSourceName() + ":" + e.getLineNumber());
-        } catch (JavaScriptException e) {
-            if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
-            if (Log.on) Log.log(this, "         thrown from within trap '" + name + "' at " + e.sourceFile + ":" + e.line);
+        } catch (JS.Exn e) {
+           if (Log.on) Log.log(this, e);
+
         } finally {
             // restore the thread-locals
             tc.putCascadeHappened = save_putCascadeHappened;
@@ -220,11 +215,7 @@ public class Trap {
         }
         if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased"))
             trapee.surface.keywatchers.removeElement(trapee);
-        if (allTraps.size() == 1) allTraps.setSize(0);
-        else {
-            allTraps.setElementAt(allTraps.elementAt(allTraps.size() - 1), indexInAllTraps);
-            allTraps.setSize(allTraps.size() - 1);
-        }
+        allTraps.remove(myWeak);
     }
 
     /** per-thread storage for Traps */
@@ -250,3 +241,4 @@ public class Trap {
     }
 
 }
+