KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > RubyObjectSpace


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 Alan Moore <alan_moore@gmx.net>
15  * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
16  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
17  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
18  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
19  *
20  * Alternatively, the contents of this file may be used under the terms of
21  * either of the GNU General Public License Version 2 or later (the "GPL"),
22  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23  * in which case the provisions of the GPL or the LGPL are applicable instead
24  * of those above. If you wish to allow use of your version of this file only
25  * under the terms of either the GPL or the LGPL, and not to allow others to
26  * use your version of this file under the terms of the CPL, indicate your
27  * decision by deleting the provisions above and replace them with the notice
28  * and other provisions required by the GPL or the LGPL. If you do not delete
29  * the provisions above, a recipient may use your version of this file under
30  * the terms of any one of the CPL, the GPL or the LGPL.
31  ***** END LICENSE BLOCK *****/

32 package org.jruby;
33
34 import java.util.Iterator JavaDoc;
35
36 import org.jruby.runtime.Block;
37 import org.jruby.runtime.CallbackFactory;
38 import org.jruby.runtime.ThreadContext;
39 import org.jruby.runtime.builtin.IRubyObject;
40
41 public class RubyObjectSpace {
42
43     /** Create the ObjectSpace module and add it to the Ruby runtime.
44      *
45      */

46     public static RubyModule createObjectSpaceModule(Ruby runtime) {
47         RubyModule objectSpaceModule = runtime.defineModule("ObjectSpace");
48         CallbackFactory callbackFactory = runtime.callbackFactory(RubyObjectSpace.class);
49         objectSpaceModule.defineModuleFunction("each_object", callbackFactory.getOptSingletonMethod("each_object"));
50         objectSpaceModule.defineFastModuleFunction("garbage_collect", callbackFactory.getFastSingletonMethod("garbage_collect"));
51         objectSpaceModule.defineFastModuleFunction("_id2ref", callbackFactory.getFastSingletonMethod("id2ref", RubyFixnum.class));
52         objectSpaceModule.defineModuleFunction("define_finalizer",
53                 callbackFactory.getOptSingletonMethod("define_finalizer"));
54         objectSpaceModule.defineModuleFunction("undefine_finalizer",
55                 callbackFactory.getOptSingletonMethod("undefine_finalizer"));
56
57         return objectSpaceModule;
58     }
59
60     public static IRubyObject define_finalizer(IRubyObject recv, IRubyObject[] args, Block block) {
61         RubyProc proc = null;
62         if(recv.checkArgumentCount(args,1,2) == 2) {
63             if(args[1] instanceof RubyProc) {
64                 proc = (RubyProc)args[1];
65             } else {
66                 proc = (RubyProc)args[1].convertToType("Proc","to_proc",true);
67             }
68         } else {
69             proc = recv.getRuntime().newProc(false, block);
70         }
71         IRubyObject obj = args[0];
72         long id = RubyNumeric.fix2long(obj.id());
73         recv.getRuntime().getObjectSpace().addFinalizer(obj,id,proc);
74         return recv;
75     }
76
77     public static IRubyObject undefine_finalizer(IRubyObject recv, IRubyObject[] args, Block block) {
78         recv.checkArgumentCount(args,1,1);
79         recv.getRuntime().getObjectSpace().removeFinalizers(RubyNumeric.fix2long(args[0].id()));
80         return recv;
81     }
82
83     public static IRubyObject id2ref(IRubyObject recv, RubyFixnum id) {
84         Ruby runtime = id.getRuntime();
85         long longId = id.getLongValue();
86         if (longId == 0) {
87             return runtime.getFalse();
88         } else if (longId == 2) {
89             return runtime.getTrue();
90         } else if (longId == 4) {
91             return runtime.getNil();
92         } else if (longId % 2 != 0) { // odd
93
return runtime.newFixnum((longId - 1) / 2);
94         } else {
95             IRubyObject object = runtime.getObjectSpace().id2ref(longId);
96             if (object == null)
97                 runtime.newRangeError("not an id value");
98             return object;
99         }
100     }
101     
102     public static IRubyObject each_object(IRubyObject recv, IRubyObject[] args, Block block) {
103         RubyModule rubyClass;
104         if (args.length == 0) {
105             rubyClass = recv.getRuntime().getObject();
106         } else {
107             rubyClass = (RubyModule) args[0];
108         }
109         int count = 0;
110         Iterator JavaDoc iter = recv.getRuntime().getObjectSpace().iterator(rubyClass);
111         IRubyObject obj = null;
112         ThreadContext context = recv.getRuntime().getCurrentContext();
113         while ((obj = (IRubyObject)iter.next()) != null) {
114             count++;
115             context.yield(obj, block);
116         }
117         return recv.getRuntime().newFixnum(count);
118     }
119
120     public static IRubyObject garbage_collect(IRubyObject recv) {
121         return RubyGC.start(recv);
122     }
123 }
124
Popular Tags