2003/11/18 10:47:27
[org.ibex.core.git] / src / org / xwt / js / Trap.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.js;
3
4 import java.util.*;
5 import org.xwt.util.*;
6 import java.io.*;
7
8 /**
9  *  This class encapsulates a single trap placed on a given node. The
10  *  traps for a given property name on a given box are maintained as a
11  *  linked list stack, with the most recently placed trap at the head
12  *  of the list.
13  */
14 class Trap {
15
16     JS trapee = null;          ///< the box on which this trap was placed
17     Object name = null;        ///< the property that the trap was placed on
18
19     JSFunction f = null;       ///< the function for this trap
20     Trap next = null;          ///< the next trap down the trap stack
21
22     Trap(JS b, String n, JSFunction f, Trap nx) { trapee = b; name = n; this.f = f; this.next = nx; }
23
24     private static final JSFunction putInvoker = new JSFunction("putInvoker", 0, null);
25     private static final JSFunction getInvoker = new JSFunction("getInvoker", 0, null);
26     static {
27         putInvoker.add(-1, ByteCodes.PUT, null);
28         getInvoker.add(-1, ByteCodes.GET, null);
29     }
30     
31     void invoke(Object key, Object value) {
32         Interpreter i = new Interpreter(putInvoker, false, null);
33         i.stack.push(this);
34         i.stack.push(key);
35         i.stack.push(value);
36         i.resume();
37     }
38
39     Object invoke(Object key) {
40         Interpreter i = new Interpreter(getInvoker, false, null);
41         i.stack.push(this);
42         i.stack.push(key);
43         i.resume();
44         return i.stack.pop();
45     }
46
47     // FIXME: review; is necessary?
48     static class TrapScope extends JSScope {
49         Trap t;
50         Object val = null;
51         boolean cascadeHappened = false;
52         public TrapScope(JSScope parent, Trap t, Object val) { super(parent); this.t = t; this.val = val; }
53         public Object get(Object key) {
54             if (key.equals("trapee")) return t.trapee;
55             if (key.equals("trapname")) return t.name;
56             return super.get(key);
57         }
58     }
59 }
60