KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > runtime > Frame


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
15  * Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
16  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
17  * Copyright (C) 2004-2007 Thomas E Enebo <enebo@acm.org>
18  * Copyright (C) 2006 Charles O Nutter <headius@headius.com>
19  * Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * either of the GNU General Public License Version 2 or later (the "GPL"),
23  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24  * in which case the provisions of the GPL or the LGPL are applicable instead
25  * of those above. If you wish to allow use of your version of this file only
26  * under the terms of either the GPL or the LGPL, and not to allow others to
27  * use your version of this file under the terms of the CPL, indicate your
28  * decision by deleting the provisions above and replace them with the notice
29  * and other provisions required by the GPL or the LGPL. If you do not delete
30  * the provisions above, a recipient may use your version of this file under
31  * the terms of any one of the CPL, the GPL or the LGPL.
32  ***** END LICENSE BLOCK *****/

33 package org.jruby.runtime;
34
35 import org.jruby.RubyModule;
36 import org.jruby.lexer.yacc.ISourcePosition;
37 import org.jruby.runtime.builtin.IRubyObject;
38
39 /**
40  * <p>Frame for a full (read: not 'fast') Ruby method invocation. Any Ruby method which calls
41  * another Ruby method (or yields to a block) will get a Frame. A fast method by contrast does
42  * not get a Frame because we know that we will not be calling/yielding.</p>
43  *
44  * A Frame is also needed for a few special cases:
45  * <ul>
46  * <li>Proc.new must check previous frame to get the block it is getting constructed for
47  * <li>block_given? must check the previous frame to see if a block is active
48  * </li>
49  *
50  */

51 public class Frame {
52     /**
53      * The class for the method we are invoking for this frame. Note: This may not be the
54      * class where the implementation of the method lives.
55      */

56     private RubyModule klazz;
57     
58     /**
59      * The 'self' for this frame.
60      */

61     private IRubyObject self;
62     
63     /**
64      * The name of the method being invoked in this frame. Note: Blocks are backed by frames
65      * and do not have a name.
66      */

67     private String JavaDoc name;
68     
69     /**
70      * The arguments passed into the method of this frame. The frame captures arguments
71      * so that they can be reused for things like super/zsuper.
72      */

73     private IRubyObject[] args;
74
75     /**
76      * The block that was passed in for this frame (as either a block or a &amp;block argument).
77      * The frame captures the block for super/zsuper, but also for Proc.new (with no arguments)
78      * and also for block_given?. Both of those methods needs access to the block of the
79      * previous frame to work.
80      */

81     private Block block;
82
83     /**
84      * The current visibility for anything defined under this frame
85      */

86     private Visibility visibility = Visibility.PUBLIC;
87
88     /**
89      * The location in source where this block/method invocation is happening
90      */

91     private final ISourcePosition position;
92
93     public Frame(ISourcePosition position) {
94         this(null, null, null, IRubyObject.NULL_ARRAY, Block.NULL_BLOCK, position);
95     }
96
97     public Frame(RubyModule klazz, IRubyObject self, String JavaDoc name,
98                  IRubyObject[] args, Block block, ISourcePosition position) {
99         assert block != null : "Block uses null object pattern. It should NEVER be null";
100         
101         this.self = self;
102         this.args = args;
103         this.name = name;
104         this.klazz = klazz;
105         this.position = position;
106         this.block = block;
107     }
108
109     /** Getter for property args.
110      * @return Value of property args.
111      */

112     IRubyObject[] getArgs() {
113         return args;
114     }
115
116     /** Setter for property args.
117      * @param args New value of property args.
118      */

119     void setArgs(IRubyObject[] args) {
120         this.args = args;
121     }
122
123     /**
124      * @return the frames current position
125      */

126     ISourcePosition getPosition() {
127         return position;
128     }
129
130     /**
131      * Return class that we are supposedly calling for this invocation
132      *
133      * @return the current class
134      */

135     RubyModule getKlazz() {
136         return klazz;
137     }
138
139     /**
140      * Set class that this method is supposedly calling on. Note: This is different than
141      * a native method's implementation class.
142      *
143      * @param klazz the new class
144      */

145     public void setKlazz(RubyModule klazz) {
146         this.klazz = klazz;
147     }
148
149     /**
150      * Set the method name associated with this frame
151      *
152      * @param name the new name
153      */

154     public void setName(String JavaDoc name) {
155         this.name = name;
156     }
157
158     /**
159      * Get the method name associated with this frame
160      *
161      * @return the method name
162      */

163     String JavaDoc getName() {
164         return name;
165     }
166
167     /**
168      * Get the self associated with this frame
169      *
170      * @return the self
171      */

172     IRubyObject getSelf() {
173         return self;
174     }
175
176     /**
177      * Set the self associated with this frame
178      *
179      * @param self is the new value of self
180      */

181     void setSelf(IRubyObject self) {
182         this.self = self;
183     }
184     
185     /**
186      * Get the visibility at the time of this frame
187      *
188      * @return the visibility
189      */

190     public Visibility getVisibility() {
191         return visibility;
192     }
193     
194     /**
195      * Change the visibility associated with this frame
196      *
197      * @param visibility the new visibility
198      */

199     public void setVisibility(Visibility visibility) {
200         this.visibility = visibility;
201     }
202     
203     /**
204      * What block is associated with this frame?
205      *
206      * @return the block of this frame or NULL_BLOCK if no block given
207      */

208     public Block getBlock() {
209         return block;
210     }
211
212     public Frame duplicate() {
213         IRubyObject[] newArgs;
214         if (args.length != 0) {
215             newArgs = new IRubyObject[args.length];
216             System.arraycopy(args, 0, newArgs, 0, args.length);
217         } else {
218             newArgs = args;
219         }
220
221         return new Frame(klazz, self, name, newArgs, block, position);
222     }
223
224     /* (non-Javadoc)
225      * @see java.lang.Object#toString()
226      */

227     public String JavaDoc toString() {
228         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(50);
229         sb.append(position != null ? position.toString() : "-1");
230         sb.append(':');
231         sb.append(klazz + " " + name);
232         if (name != null) {
233             sb.append("in ");
234             sb.append(name);
235         }
236         return sb.toString();
237     }
238 }
239
Popular Tags