mass rename and rebranding from xwt to ibex - fixed to use ixt files
[org.ibex.core.git] / src / org / ibex / js / Trap.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex.js;
3
4 /**
5  *  This class encapsulates a single trap placed on a given node. The
6  *  traps for a given property name on a given box are maintained as a
7  *  linked list stack, with the most recently placed trap at the head
8  *  of the list.
9  */
10 class Trap {
11
12     JS trapee = null;          ///< the box on which this trap was placed
13     Object name = null;        ///< the property that the trap was placed on
14
15     JSFunction f = null;       ///< the function for this trap
16     Trap next = null;          ///< the next trap down the trap stack
17
18     Trap(JS b, String n, JSFunction f, Trap nx) {
19         trapee = b; name = n; this.f = f; this.next = nx;
20     }
21
22     private static final JSFunction putInvoker = new JSFunction("putInvoker", 0, null);
23     private static final JSFunction getInvoker = new JSFunction("getInvoker", 0, null);
24
25     static {
26         putInvoker.add(1, ByteCodes.PUT, null);
27         putInvoker.add(2, Tokens.RETURN, null);
28         getInvoker.add(1, ByteCodes.GET, null);
29         getInvoker.add(2, Tokens.RETURN, null);
30     }
31     
32     void invoke(Object value) throws JSExn {
33         Interpreter i = new Interpreter(putInvoker, false, null);
34         i.stack.push(trapee);
35         i.stack.push(name);
36         i.stack.push(value);
37         i.resume();
38     }
39
40     Object invoke() throws JSExn {
41         Interpreter i = new Interpreter(getInvoker, false, null);
42         i.stack.push(trapee);
43         i.stack.push(name);
44         return i.resume();
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) throws JSExn {
54             if (key.equals("trapee")) return t.trapee;
55             if (key.equals("callee")) return t.f;
56             if (key.equals("trapname")) return t.name;
57             return super.get(key);
58         }
59     }
60 }
61