001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
015: * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
016: * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
017: * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
018: * Copyright (C) 2004 Charles O Nutter <headius@headius.com>
019: * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * either of the GNU General Public License Version 2 or later (the "GPL"),
023: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
024: * in which case the provisions of the GPL or the LGPL are applicable instead
025: * of those above. If you wish to allow use of your version of this file only
026: * under the terms of either the GPL or the LGPL, and not to allow others to
027: * use your version of this file under the terms of the CPL, indicate your
028: * decision by deleting the provisions above and replace them with the notice
029: * and other provisions required by the GPL or the LGPL. If you do not delete
030: * the provisions above, a recipient may use your version of this file under
031: * the terms of any one of the CPL, the GPL or the LGPL.
032: ***** END LICENSE BLOCK *****/package org.jruby;
033:
034: import org.jruby.runtime.CallbackFactory;
035: import org.jruby.runtime.ClassIndex;
036: import org.jruby.runtime.ObjectAllocator;
037: import org.jruby.runtime.builtin.IRubyObject;
038:
039: /**
040: *
041: * @author jpetersen
042: */
043: public class RubyNil extends RubyObject {
044: private final Ruby runtime;
045:
046: public RubyNil(Ruby runtime) {
047: super (runtime, runtime.getNilClass());
048: this .runtime = runtime;
049: this .isTrue = false;
050: }
051:
052: public Ruby getRuntime() {
053: return runtime;
054: }
055:
056: public static ObjectAllocator NIL_ALLOCATOR = new ObjectAllocator() {
057: public IRubyObject allocate(Ruby runtime, RubyClass klass) {
058: return runtime.getNil();
059: }
060: };
061:
062: public static RubyClass createNilClass(Ruby runtime) {
063: RubyClass nilClass = runtime.defineClass("NilClass", runtime
064: .getObject(), NIL_ALLOCATOR);
065: nilClass.index = ClassIndex.NIL;
066:
067: CallbackFactory callbackFactory = runtime
068: .callbackFactory(RubyNil.class);
069: nilClass.defineFastMethod("type", callbackFactory
070: .getFastSingletonMethod("type"));
071: nilClass.defineFastMethod("to_i", callbackFactory
072: .getFastSingletonMethod("to_i"));
073: nilClass.defineFastMethod("to_s", callbackFactory
074: .getFastSingletonMethod("to_s"));
075: nilClass.defineFastMethod("to_a", callbackFactory
076: .getFastSingletonMethod("to_a"));
077: nilClass.defineFastMethod("to_f", callbackFactory
078: .getFastSingletonMethod("to_f"));
079: nilClass.defineFastMethod("inspect", callbackFactory
080: .getFastSingletonMethod("inspect"));
081:
082: nilClass.defineFastMethod("&", callbackFactory
083: .getFastSingletonMethod("op_and",
084: RubyKernel.IRUBY_OBJECT));
085: nilClass.defineFastMethod("|", callbackFactory
086: .getFastSingletonMethod("op_or",
087: RubyKernel.IRUBY_OBJECT));
088: nilClass.defineFastMethod("^", callbackFactory
089: .getFastSingletonMethod("op_xor",
090: RubyKernel.IRUBY_OBJECT));
091: nilClass.defineFastMethod("nil?", callbackFactory
092: .getFastMethod("nil_p"));
093: nilClass.defineFastMethod("id", callbackFactory
094: .getFastSingletonMethod("id"));
095: nilClass.defineFastMethod("taint", callbackFactory
096: .getFastMethod("taint"));
097: nilClass.defineFastMethod("freeze", callbackFactory
098: .getFastMethod("freeze"));
099:
100: nilClass.getMetaClass().undefineMethod("new");
101:
102: return nilClass;
103: }
104:
105: public int getNativeTypeIndex() {
106: return ClassIndex.NIL;
107: }
108:
109: // public RubyClass getMetaClass() {
110: // return runtime.getNilClass();
111: // }
112:
113: public boolean isImmediate() {
114: return true;
115: }
116:
117: public boolean safeHasInstanceVariables() {
118: return false;
119: }
120:
121: // Methods of the Nil Class (nil_*):
122:
123: /** nil_to_i
124: *
125: */
126: public static RubyFixnum to_i(IRubyObject recv) {
127: return RubyFixnum.zero(recv.getRuntime());
128: }
129:
130: /**
131: * nil_to_f
132: *
133: */
134: public static RubyFloat to_f(IRubyObject recv) {
135: return RubyFloat.newFloat(recv.getRuntime(), 0.0D);
136: }
137:
138: /** nil_to_s
139: *
140: */
141: public static RubyString to_s(IRubyObject recv) {
142: return recv.getRuntime().newString("");
143: }
144:
145: /** nil_to_a
146: *
147: */
148: public static RubyArray to_a(IRubyObject recv) {
149: return recv.getRuntime().newArray(0);
150: }
151:
152: /** nil_inspect
153: *
154: */
155: public static RubyString inspect(IRubyObject recv) {
156: return recv.getRuntime().newString("nil");
157: }
158:
159: /** nil_type
160: *
161: */
162: public static RubyClass type(IRubyObject recv) {
163: return recv.getRuntime().getClass("NilClass");
164: }
165:
166: /** nil_and
167: *
168: */
169: public static RubyBoolean op_and(IRubyObject recv, IRubyObject obj) {
170: return recv.getRuntime().getFalse();
171: }
172:
173: /** nil_or
174: *
175: */
176: public static RubyBoolean op_or(IRubyObject recv, IRubyObject obj) {
177: return recv.getRuntime().newBoolean(obj.isTrue());
178: }
179:
180: /** nil_xor
181: *
182: */
183: public static RubyBoolean op_xor(IRubyObject recv, IRubyObject obj) {
184: return recv.getRuntime().newBoolean(obj.isTrue());
185: }
186:
187: public static RubyFixnum id(IRubyObject recv) {
188: return recv.getRuntime().newFixnum(4);
189: }
190:
191: public boolean isNil() {
192: return true;
193: }
194:
195: public IRubyObject freeze() {
196: return this ;
197: }
198:
199: public IRubyObject nil_p() {
200: return getRuntime().getTrue();
201: }
202:
203: public IRubyObject taint() {
204: return this ;
205: }
206:
207: public RubyFixnum id() {
208: return getRuntime().newFixnum(4);
209: }
210: }
|