KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > runtime > builtin > meta > ProcMetaClass


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) 2005 Thomas E Enebo <enebo@acm.org>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jruby.runtime.builtin.meta;
29
30 import org.jruby.Ruby;
31 import org.jruby.RubyClass;
32 import org.jruby.RubyProc;
33 import org.jruby.runtime.Arity;
34 import org.jruby.runtime.Block;
35 import org.jruby.runtime.ObjectAllocator;
36 import org.jruby.runtime.builtin.IRubyObject;
37 import org.jruby.util.collections.SinglyLinkedList;
38
39 public class ProcMetaClass extends ObjectMetaClass {
40     public ProcMetaClass(Ruby runtime) {
41         super("Proc", RubyProc.class, runtime.getObject(), PROC_ALLOCATOR);
42     }
43     
44     public ProcMetaClass(String JavaDoc name, RubyClass superClass, ObjectAllocator allocator, SinglyLinkedList parentCRef) {
45         super(name, RubyProc.class, superClass, allocator, parentCRef);
46     }
47
48     protected class ProcMeta extends Meta {
49         protected void initializeClass() {
50             defineFastMethod("arity", Arity.noArguments(), "arity");
51             defineFastMethod("binding", Arity.noArguments(), "binding");
52             defineMethod("call", Arity.optional(), "call");
53             defineAlias("[]", "call");
54             defineFastMethod("to_proc", Arity.noArguments(), "to_proc");
55             defineMethod("initialize", Arity.optional());
56
57             defineSingletonMethod("new", Arity.optional(), "newInstance");
58         }
59     };
60     
61     protected Meta getMeta() {
62         return new ProcMeta();
63     }
64     
65     /**
66      * Create a new instance of a Proc object. We override this method (from RubyClass)
67      * since we need to deal with special case of Proc.new with no arguments or block arg. In
68      * this case, we need to check previous frame for a block to consume.
69      */

70     public IRubyObject newInstance(IRubyObject[] args, Block block) {
71         IRubyObject obj = (IRubyObject) allocate();
72         
73         // No passed in block, lets check next outer frame for one ('Proc.new')
74
if (!block.isGiven()) {
75             block = getRuntime().getCurrentContext().getPreviousFrame().getBlock();
76         }
77         
78         obj.callInit(args, block);
79         return obj;
80     }
81     
82     public RubyClass newSubClass(String JavaDoc name, SinglyLinkedList parentCRef) {
83         return new ProcMetaClass(name, this, PROC_ALLOCATOR, parentCRef);
84     }
85     
86     private static ObjectAllocator PROC_ALLOCATOR = new ObjectAllocator() {
87         public IRubyObject allocate(Ruby runtime, RubyClass klass) {
88             RubyProc instance = RubyProc.newProc(runtime, false);
89
90             instance.setMetaClass(klass);
91
92             return instance;
93         }
94     };
95 }
96
Popular Tags