1 /*****************************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one                *
3  * or more contributor license agreements.  See the NOTICE file              *
4  * distributed with this work for additional information                     *
5  * regarding copyright ownership.  The ASF licenses this file                *
6  * to you under the Apache License, Version 2.0 (the                         *
7  * "License"); you may not use this file except in compliance                *
8  * with the License.  You may obtain a copy of the License at                *
9  *                                                                           *
10  *     http://www.apache.org/licenses/LICENSE-2.0                            *
11  *                                                                           *
12  * Unless required by applicable law or agreed to in writing,                *
13  * software distributed under the License is distributed on an               *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY                    *
15  * KIND, either express or implied.  See the License for the                 *
16  * specific language governing permissions and limitations                   *
17  * under the License.                                                        *
18  *                                                                           *
19  * This file is part of the BeanShell Java Scripting distribution.           *
20  * Documentation and updates may be found at http://www.beanshell.org/       *
21  * Patrick Niemeyer (pat@pat.net)                                            *
22  * Author of Learning Java, O'Reilly & Associates                            *
23  *                                                                           *
24  *****************************************************************************/
25 
26 package bsh;
27 
28 import java.io.Serializable;
29 import java.util.Stack;
30 import java.util.EmptyStackException;
31 
32 /**
33 	A stack of NameSpaces representing the call path.
34 	Each method invocation, for example, pushes a new NameSpace onto the stack.
35 	The top of the stack is always the current namespace of evaluation.
36 	<p>
37 
38 	This is used to support the this.caller magic reference and to print
39 	script "stack traces" when evaluation errors occur.
40 	<p>
41 
42 	Note: How can this be thread safe, you might ask?  Wouldn't a thread
43 	executing various beanshell methods be mutating the callstack?  Don't we
44 	need one CallStack per Thread in the interpreter?  The answer is that we do.
45 	Any java.lang.Thread enters our script via an external (hard) Java
46 	reference via a This type interface, e.g.  the Runnable interface
47 	implemented by This or an arbitrary interface implemented by XThis.
48 	In that case the This invokeMethod() method (called by any interface that
49 	it exposes) creates a new CallStack for each external call.
50 	<p>
51 */
52 public final class CallStack implements Serializable {
53 
54 	private static final long serialVersionUID = 0L;
55 
56 	private final Stack<NameSpace> stack = new Stack<NameSpace>();
57 
58 
CallStack()59 	public CallStack() { }
60 
CallStack( NameSpace namespace )61 	public CallStack( NameSpace namespace ) {
62 		push( namespace );
63 	}
64 
clear()65 	public void clear() {
66 		stack.removeAllElements();
67 	}
68 
push( NameSpace ns )69 	public void push( NameSpace ns ) {
70 		stack.push( ns );
71 	}
72 
top()73 	public NameSpace top() {
74 		return stack.peek();
75 	}
76 
77 	/**
78 		zero based.
79 	*/
get(int depth)80 	public NameSpace get(int depth) {
81 		int size = stack.size();
82 		if ( depth >= size )
83 			return NameSpace.JAVACODE;
84 		else
85 			return stack.get(size-1-depth);
86 	}
87 
88 	/**
89 		This is kind of crazy, but used by the setNameSpace command.
90 		zero based.
91 	*/
set(int depth, NameSpace ns)92 	public void set(int depth, NameSpace ns) {
93 		stack.set( stack.size()-1-depth, ns );
94 	}
95 
pop()96 	public NameSpace pop() {
97 		try {
98 			return stack.pop();
99 		} catch(EmptyStackException e) {
100 			throw new InterpreterError("pop on empty CallStack");
101 		}
102 	}
103 
104 	/**
105 		Swap in the value as the new top of the stack and return the old
106 		value.
107 	*/
swap( NameSpace newTop )108 	public NameSpace swap( NameSpace newTop ) {
109 		int last = stack.size() - 1;
110 		NameSpace oldTop = stack.get(last);
111 		stack.set( last, newTop );
112 		return oldTop;
113 	}
114 
depth()115 	public int depth() {
116 		return stack.size();
117 	}
118 /*
119 	public NameSpace [] toArray() {
120 		NameSpace [] nsa = new NameSpace [ depth() ];
121 		stack.copyInto( nsa );
122 		return nsa;
123 	}
124 */
toString()125 	public String toString() {
126 		StringBuilder sb = new StringBuilder();
127 		sb.append("CallStack:\n");
128 		for( int i=stack.size()-1; i>=0; i-- )
129 			sb.append("\t"+stack.get(i)+"\n");
130 
131 		return sb.toString();
132 	}
133 
134 	/**
135 		Occasionally we need to freeze the callstack for error reporting
136 		purposes, etc.
137 	*/
copy()138 	public CallStack copy() {
139 		CallStack cs = new CallStack();
140 		cs.stack.addAll(this.stack);
141 		return cs;
142 	}
143 }
144