Exemplo n.º 1
0
        static internal RClass SingletonClass(object obj, NetRuby ruby)
        {
            if (obj is int || obj is long || obj is double || Symbol.IsSymbol(obj))
            {
                throw new eTypeError("can't define singleton");
            }
            if (RBasic.IsSpecialConstType(obj))
            {
                if (obj == null)
                {
                    if (ruby.cNilClass.IsSingleton == false)
                    {
                        ruby.cNilClass = new RSingletonClass(ruby.cNilClass);
                        return(ruby.cNilClass);
                    }
                }
                bool   b = (bool)obj;
                RClass o = (b) ? ruby.cTrueClass : ruby.cFalseClass;
                if (o.IsSingleton == false)
                {
                    if (b)
                    {
                        o = ruby.cTrueClass = new RSingletonClass(o);
                    }
                    else
                    {
                        o = ruby.cFalseClass = new RSingletonClass(o);
                    }
                }
                return(o);
            }
            if (obj is RBasic == false)
            {
                ruby.bug("Unknown object " + obj.ToString());
            }
            RBasic bas   = (RBasic)obj;
            RClass klass = null;

            if (bas is RSingletonClass)
            {
                klass = (RClass)bas.klass;
            }
            else
            {
                klass     = new RSingletonClass(bas.klass);
                bas.klass = klass;
                klass.AttachSingleton((RObjectBase)obj);
            }
            if (bas.Test(FL.TAINT))
            {
                klass.Set(FL.TAINT);
            }
            else
            {
                klass.Unset(FL.TAINT);
            }
            if (bas.Test(FL.FREEZE))
            {
                klass.Set(FL.FREEZE);
            }

            return(klass);
        }