2003/12/07 08:45:04
authordavid <david@xwt.org>
Fri, 30 Jan 2004 07:42:38 +0000 (07:42 +0000)
committerdavid <david@xwt.org>
Fri, 30 Jan 2004 07:42:38 +0000 (07:42 +0000)
darcs-hash:20040130074238-0c9ea-25141bdd2f32215e626b76c5c1e30fa05c9ea0b9.gz

src/org/xwt/js/JS.java
src/org/xwt/js/Trap.java

index d187ecb..f2a7c20 100644 (file)
@@ -13,7 +13,7 @@ public class JS extends org.xwt.util.BalancedTree {
 
     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];
+        return c == null || c.f == null || c.pc < 0 || c.pc >= c.f.size ? -1 : c.f.line[c.pc];
     }
 
     public static String getSourceName() {
@@ -156,37 +156,41 @@ public class JS extends org.xwt.util.BalancedTree {
 
     // 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 false; }
 
     /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
     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 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)));
     }
index e92bad7..60c34c6 100644 (file)
@@ -19,29 +19,32 @@ class Trap {
     JSFunction f = null;       ///< the function for this trap
     Trap next = null;          ///< the next trap down the trap stack
 
-    Trap(JS b, String n, JSFunction f, Trap nx) { trapee = b; name = n; this.f = f; this.next = nx; }
+    Trap(JS b, String n, JSFunction f, Trap nx) {
+        trapee = b; name = n; this.f = f; this.next = nx;
+    }
 
     private static final JSFunction putInvoker = new JSFunction("putInvoker", 0, null);
     private static final JSFunction getInvoker = new JSFunction("getInvoker", 0, null);
+
     static {
         putInvoker.add(-1, ByteCodes.PUT, null);
         putInvoker.add(-1, Tokens.RETURN, null);
         getInvoker.add(-1, ByteCodes.GET, null);
-        putInvoker.add(-1, Tokens.RETURN, null);
+        getInvoker.add(-1, Tokens.RETURN, null);
     }
     
-    void invoke(Object key, Object value) throws JSExn {
+    void invoke(Object value) throws JSExn {
         Interpreter i = new Interpreter(putInvoker, false, null);
         i.stack.push(trapee);
-        i.stack.push(key);
+        i.stack.push(name);
         i.stack.push(value);
         i.resume();
     }
 
-    Object invoke(Object key) throws JSExn {
+    Object invoke() throws JSExn {
         Interpreter i = new Interpreter(getInvoker, false, null);
         i.stack.push(this);
-        i.stack.push(key);
+        i.stack.push(name);
         i.resume();
         return i.stack.pop();
     }
@@ -54,6 +57,7 @@ class Trap {
         public TrapScope(JSScope parent, Trap t, Object val) { super(parent); this.t = t; this.val = val; }
         public Object get(Object key) throws JSExn {
             if (key.equals("trapee")) return t.trapee;
+            if (key.equals("callee")) return t.f;
             if (key.equals("trapname")) return t.name;
             return super.get(key);
         }