KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > engines > javascript > JsContextStub


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf.engines.javascript;
57
58 import org.mozilla.javascript.*;
59 import org.mozilla.javascript.debug.*;
60
61 import org.apache.bsf.debug.*;
62 import org.apache.bsf.debug.jsdi.*;
63
64 import java.rmi.RemoteException JavaDoc;
65 /**
66 * Insert the type's description here.
67 * Creation date: (8/23/2001 4:16:50 PM)
68 * @author: Administrator
69 */

70
71 public class JsContextStub
72 extends org.apache.bsf.debug.util.Skeleton
73    implements JsContext {
74
75     RhinoContextProxy m_rcp;
76     RhinoEngineDebugger m_rhinoDbg;
77     DebugFrame m_frame;
78     int m_frameno;
79     boolean m_atBreakpoint;
80     boolean m_invalid;
81
82     /**
83      * JsContextStub constructor comment.
84      */

85     public JsContextStub(RhinoContextProxy rcp, DebugFrame frame, int frameno)
86     throws RemoteException JavaDoc {
87             super(org.apache.bsf.debug.util.DebugConstants.JS_CONTEXT_TID);
88         
89         m_rhinoDbg = rcp.getRhinoEngineDebugger();
90         m_rcp = rcp;
91         m_frame = frame;
92         m_frameno = frameno;
93         m_invalid = false;
94         m_atBreakpoint = true;
95     }
96     //--------------------------------------------------
97
void atBreakpoint(boolean atbrkpt) {
98         m_atBreakpoint = atbrkpt;
99     }
100     public JsObject bind(String JavaDoc id) throws RemoteException JavaDoc {
101         try {
102             Context.enter();
103             Scriptable obj = m_frame.getVariableObject();
104             Object JavaDoc prop;
105             while (obj != null) {
106                 Scriptable m = obj;
107                 do {
108                     if (m.has(id, obj))
109                         return m_rhinoDbg.marshallScriptable(obj);
110                     m = m.getPrototype();
111                 } while (m != null);
112                 obj = obj.getParentScope();
113             }
114             throw new JsdiException("Name not in scope.");
115         } finally {
116             Context.exit();
117         }
118     }
119     //--------------------------------------------------
120
public JsCode getCode() {
121         if (m_invalid)
122             throw new JsdiException("This context no longer exists.");
123         if (!m_atBreakpoint)
124             throw new JsdiException("Resumed context, can't get the code.");
125
126         try {
127             Context.enter();
128             return null;
129         } finally {
130             Context.exit();
131         }
132     }
133     public int getDepth() {
134         return m_frameno;
135     }
136     //--------------------------------------------------
137
public JsEngine getEngine() {
138         RhinoEngineDebugger redbg;
139         redbg = m_rcp.getRhinoEngineDebugger();
140         return (JsEngine) redbg.getDebugInterface();
141         
142     }
143     //--------------------------------------------------
144
public int getLineNumber() {
145         if (m_invalid)
146             throw new JsdiException("This context no longer exists.");
147         if (!m_atBreakpoint)
148             throw new JsdiException("Resumed context, can't get line number.");
149
150         try {
151             Context.enter();
152             return m_frame.getLineNumber();
153         } finally {
154             Context.exit();
155         }
156     }
157     //------------------------------------------------------
158
public JsObject getScope() throws RemoteException JavaDoc {
159
160         if (m_invalid)
161             throw new JsdiException("This context no longer exists.");
162         if (!m_atBreakpoint)
163             throw new JsdiException("Resumed context, can't get line number.");
164
165         try {
166             Context.enter();
167             Scriptable varobj = m_frame.getVariableObject();
168             JsObject scope = m_rhinoDbg.marshallScriptable(varobj);
169             return scope;
170         } finally {
171             Context.exit();
172         }
173     }
174     //------------------------------------------------------
175
public String JavaDoc getSourceName() {
176         if (m_invalid)
177             throw new JsdiException("This context no longer exists.");
178         if (!m_atBreakpoint)
179             throw new JsdiException("Resumed context, can't get line number.");
180
181         try {
182             Context.enter();
183             return m_frame.getSourceName();
184         } finally {
185             Context.exit();
186         }
187     }
188     //------------------------------------------------------
189
public JsObject getThis() throws RemoteException JavaDoc {
190
191         if (m_invalid)
192             throw new JsdiException("This context no longer exists.");
193         if (!m_atBreakpoint)
194             throw new JsdiException("Resumed context, can't get line number.");
195
196         try {
197             Context.enter();
198             JsObject thisobj = null;
199             Scriptable obj = null;
200             NativeCall call = null;
201             Scriptable varobj = m_frame.getVariableObject();
202             if (varobj instanceof NativeCall) {
203                 call = (NativeCall) varobj;
204                 obj = call.getThisObj();
205                 thisobj = m_rhinoDbg.marshallScriptable(varobj);
206             }
207             return thisobj;
208         } finally {
209             Context.exit();
210         }
211     }
212     //--------------------------------------------------
213
void invalidate() {
214         m_invalid = true;
215     }
216     //------------------------------------------------------
217
public boolean isEvalContext() {
218         if (m_invalid)
219             throw new JsdiException("This context no longer exists.");
220         if (!m_atBreakpoint)
221             throw new JsdiException("Resumed context, can't get line number.");
222
223         try {
224             Context.enter();
225             return false;
226         } finally {
227             Context.exit();
228         }
229     }
230     //------------------------------------------------------
231
public boolean isFunctionContext() {
232         if (m_invalid)
233             throw new JsdiException("This context no longer exists.");
234         if (!m_atBreakpoint)
235             throw new JsdiException("Resumed context, can't get line number.");
236
237         try {
238             Context.enter();
239             return false;
240         } finally {
241             Context.exit();
242         }
243     }
244     //------------------------------------------------------
245
public boolean isScriptContext() {
246         if (m_invalid)
247             throw new JsdiException("This context no longer exists.");
248         if (!m_atBreakpoint)
249             throw new JsdiException("Resumed context, can't get line number.");
250         try {
251             Context.enter();
252             return true;
253         } finally {
254             Context.exit();
255         }
256     }
257     public Object JavaDoc lookupName(String JavaDoc name) {
258
259         try {
260             Context.enter();
261             Scriptable obj = m_frame.getVariableObject();
262             Object JavaDoc prop;
263             while (obj != null) {
264                 Scriptable m = obj;
265                 do {
266                     Object JavaDoc result = m.get(name, obj);
267                     if (result != Scriptable.NOT_FOUND)
268                         return result;
269                     m = m.getPrototype();
270                 } while (m != null);
271                 obj = obj.getParentScope();
272             }
273             throw new JsdiException("Name is not in scope.");
274         } finally {
275             Context.exit();
276         }
277     }
278     /**
279      * Looks up a name in the scope chain and returns its value.
280      */

281     public Object JavaDoc lookupName(Scriptable scopeChain, String JavaDoc id) {
282
283         try {
284             Context.enter();
285             Scriptable obj = scopeChain;
286             Object JavaDoc prop;
287             while (obj != null) {
288                 Scriptable m = obj;
289                 do {
290                     Object JavaDoc result = m.get(id, obj);
291                     if (result != Scriptable.NOT_FOUND)
292                         return result;
293                     m = m.getPrototype();
294                 } while (m != null);
295                 obj = obj.getParentScope();
296             }
297             return null;
298         } finally {
299             Context.exit();
300         }
301     }
302 }
303
Popular Tags