Ejemplo n.º 1
0
        public static Exception /*!*/ MakeCoercionError(RubyContext /*!*/ context, object self, object other)
        {
            string selfClass  = context.GetClassOf(self).Name;
            string otherClass = context.GetClassOf(other).Name;

            return(CreateTypeError("{0} can't be coerced into {1}", selfClass, otherClass));
        }
Ejemplo n.º 2
0
        public static Exception /*!*/ MakeComparisonError(RubyContext /*!*/ context, object self, object other, Exception x)
        {
            string selfClass  = context.GetClassOf(self).Name;
            string otherClass = context.GetClassOf(other).Name;

            return(RubyExceptions.CreateArgumentError("comparison of " + selfClass + " with " + otherClass + " failed", x));
        }
Ejemplo n.º 3
0
        public static Exception /*!*/ MakeComparisonError(RubyContext /*!*/ context, object self, object other)
        {
            string selfClass  = context.GetClassOf(self).Name;
            string otherClass = context.GetClassOf(other).Name;

            return(CreateArgumentError("comparison of {0} with {1} failed", selfClass, otherClass));
        }
Ejemplo n.º 4
0
 public static bool Eql(RubyContext /*!*/ context, object self, object other)
 {
     if (context.GetClassOf(self) != context.GetClassOf(other))
     {
         return(false);
     }
     return(Protocols.IsEqual(context, self, other));
 }
Ejemplo n.º 5
0
 public static RubyArray Coerce(RubyContext /*!*/ context, object self, object other)
 {
     if (context.GetClassOf(self) == context.GetClassOf(other))
     {
         return(RubyOps.MakeArray2(other, self));
     }
     return(RubyOps.MakeArray2(Protocols.ConvertToFloat(context, other), Protocols.ConvertToFloat(context, self)));
 }
Ejemplo n.º 6
0
            private void WriteSubclassData(object /*!*/ obj, Type type)
            {
                RubyClass libClass = _context.GetClass(type);
                RubyClass theClass = _context.GetClassOf(obj);

                if (libClass != theClass && !(obj is RubyStruct))
                {
                    _writer.Write((byte)'C');
                    WriteModuleName(theClass);
                }
            }
Ejemplo n.º 7
0
        public static object InitializeCopy(RubyContext/*!*/ context, object self, object source) {
            RubyClass selfClass = context.GetClassOf(self);
            RubyClass sourceClass = context.GetClassOf(source);
            if (sourceClass != selfClass) {
                throw RubyExceptions.CreateTypeError("initialize_copy should take same class object");
            }
            
            if (context.IsObjectFrozen(self)) {
                throw RubyExceptions.CreateTypeError(String.Format("can't modify frozen {0}", selfClass.Name));
            }

            return self;
        }
Ejemplo n.º 8
0
            private RubyStruct /*!*/ ReadStruct()
            {
                RubyStruct obj = (UnmarshalNewObject() as RubyStruct);

                if (obj == null)
                {
                    throw RubyExceptions.CreateArgumentError("non-initialized struct");
                }

                var names = obj.GetNames();
                int count = ReadInt32();

                if (count != names.Count)
                {
                    throw RubyExceptions.CreateArgumentError("struct size differs");
                }

                for (int i = 0; i < count; i++)
                {
                    string name = ReadSymbol();
                    if (name != names[i])
                    {
                        RubyClass theClass = _context.GetClassOf(obj);
                        string    message  = String.Format("struct {0} not compatible ({1} for {2})", theClass.Name, name, names[i]);
                        throw RubyExceptions.CreateTypeError(message);
                    }
                    obj[i] = ReadAnObject(false);
                }

                return(obj);
            }
Ejemplo n.º 9
0
        public static MutableString /*!*/ Inspect(RubyStruct /*!*/ self)
        {
            RubyContext context = self.ImmediateClass.Context;

            using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) {
                // #<struct Struct::Foo name=nil, val=nil>
                var result = MutableString.CreateMutable(RubyEncoding.Binary);
                result.Append("#<struct ");
                result.Append(context.Inspect(context.GetClassOf(self)));

                if (handle == null)
                {
                    return(result.Append(":...>"));
                }
                result.Append(' ');

                object[] data    = self.Values;
                var      members = self.GetNames();
                for (int i = 0; i < data.Length; i++)
                {
                    if (i != 0)
                    {
                        result.Append(", ");
                    }
                    // TODO (encoding):
                    result.Append(members[i]);
                    result.Append('=');
                    result.Append(context.Inspect(data[i]));
                }
                result.Append('>');
                return(result);
            }
        }
Ejemplo n.º 10
0
        public static RubyClass /*!*/ Include(RubyContext /*!*/ context, object /*!*/ self, params RubyModule[] /*!*/ modules)
        {
            RubyClass result = context.GetClassOf(self);

            result.IncludeModules(modules);
            return(result);
        }
Ejemplo n.º 11
0
        public static bool TryDuplicateObject(RubyContext /*!*/ context, object obj, bool cloneSemantics, out object copy)
        {
            // Ruby value types can't be cloned
            if (RubyUtils.IsRubyValueType(obj))
            {
                copy = null;
                return(false);
            }

            IDuplicable clonable = obj as IDuplicable;

            if (clonable != null)
            {
                copy = clonable.Duplicate(context, cloneSemantics);
            }
            else
            {
                // .NET classes and library clases that doesn't implement IDuplicable:
                copy = RubySites.Allocate(context.GetClassOf(obj));
                context.CopyInstanceData(obj, copy, cloneSemantics);
            }

            // TODO: optimize
            _InitializeCopySharedSite.Target(_InitializeCopySharedSite, context, copy, obj);
            if (cloneSemantics)
            {
                context.FreezeObjectBy(copy, obj);
            }

            return(true);
        }
Ejemplo n.º 12
0
        public static MutableString /*!*/ Inspect(RubyStruct /*!*/ self)
        {
            RubyContext context = self.Class.Context;

            using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) {
                // #<struct Struct::Foo name=nil, val=nil>
                MutableString str = MutableString.Create("#<struct ");
                str.Append(RubySites.Inspect(context, context.GetClassOf(self)));

                if (handle == null)
                {
                    return(str.Append(":...>"));
                }
                str.Append(' ');

                object[] data    = self.Values;
                var      members = self.GetNames();
                for (int i = 0; i < data.Length; i++)
                {
                    if (i != 0)
                    {
                        str.Append(", ");
                    }
                    str.Append(members[i]);
                    str.Append("=");
                    str.Append(RubySites.Inspect(context, data[i]));
                }
                str.Append('>');
                return(str);
            }
        }
Ejemplo n.º 13
0
        public static object DeepCopy(RubyContext /*!*/ context, object obj)
        {
            using (IDisposable handle = _infiniteCopyTracker.TrackObject(obj)) {
                if (handle == null)
                {
                    return(RubyExceptions.CreateArgumentError("unable to deep copy recursive structure"));
                }
                else
                {
                    RubyContext ec = RubyUtils.GetExecutionContext(context);

                    if (RubyUtils.IsRubyValueType(obj))
                    {
                        return(obj);
                    }

                    object copy;

                    // TODO: special case class objects:
                    RubyClass classObject = obj as RubyClass;
                    if (classObject != null)
                    {
                        copy = classObject.Duplicate();
                    }
                    else
                    {
                        copy = RubySites.Allocate(context, ec.GetClassOf(obj));
                    }

                    SymbolId[]       names   = ec.GetInstanceVariableNames(obj);
                    RubyInstanceData newVars = (names.Length > 0) ? ec.GetInstanceData(copy) : null;
                    foreach (SymbolId name in names)
                    {
                        object value;
                        if (!ec.TryGetInstanceVariable(obj, name, out value))
                        {
                            value = null;
                        }
                        else
                        {
                            value = DeepCopy(context, value);
                        }
                        newVars.SetInstanceVariable(name, value);
                    }

                    if (classObject == null)
                    {
                        // do any special copying needed for library types
                        // TODO: we still need to implement copy semantics for .NET types in general
                        IDuplicable duplicable = copy as IDuplicable;
                        if (duplicable != null)
                        {
                            duplicable.InitializeFrom(obj);
                        }
                    }
                    return(copy);
                }
            }
        }
Ejemplo n.º 14
0
        public static RubyClass/*!*/ Include(RubyContext/*!*/ context, object/*!*/ self, params RubyModule[]/*!*/ modules) {
            ContractUtils.RequiresNotNullItems(modules, "modules");
            RubyUtils.RequireNonClasses(modules);

            RubyClass result = context.GetClassOf(self);
            result.IncludeModules(modules);
            return result;
        }
Ejemplo n.º 15
0
        public static Exception /*!*/ ReinitializeException(RubyContext /*!*/ context, Exception /*!*/ self, [DefaultParameterValue(null)] object message)
        {
            var instance = RubyExceptionData.GetInstance(self);

            instance.Backtrace = null;
            instance.Message   = message ?? MutableString.Create(context.GetClassOf(self).Name, context.GetIdentifierEncoding());
            return(self);
        }
Ejemplo n.º 16
0
 public static MutableString/*!*/ TagUri(RubyContext/*!*/ context, object self) {
     var result = MutableString.Create(Tags.RubyObject, context.GetIdentifierEncoding());
     var selfClass = context.GetClassOf(self);
     if (selfClass != context.ObjectClass) {
         return result.Append(':').Append(context.GetClassName(self));
     } else {
         return result;
     }
 }
Ejemplo n.º 17
0
        public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, BigDecimal /*!*/ self)
        {
            MutableString str = MutableString.CreateMutable();

            str.AppendFormat("#<{0}:", context.GetClassOf(self).Name);
            RubyUtils.AppendFormatHexObjectId(str, RubyUtils.GetObjectId(context, self));
            str.AppendFormat(",'{0}',", self.ToString(10));
            str.AppendFormat("{0}({1})>", self.PrecisionDigits.ToString(), self.MaxPrecisionDigits.ToString());
            return(str);
        }
Ejemplo n.º 18
0
        public static RubyClass /*!*/ Include(RubyContext /*!*/ context, object /*!*/ self, params RubyModule[] /*!*/ modules)
        {
            ContractUtils.RequiresNotNullItems(modules, "modules");
            RubyUtils.RequireNonClasses(modules);

            RubyClass result = context.GetClassOf(self);

            result.IncludeModules(modules);
            return(result);
        }
Ejemplo n.º 19
0
        //  declared private instance methods:
        //    initialize
        //  declared protected instance methods:
        //  declared public instance methods:

        private static Exception MakeKeyTypeException(RubyContext /*!*/ context, object key)
        {
            if (key == null)
            {
                return(RubyExceptions.CreateTypeError("nil is not a symbol"));
            }
            else
            {
                // MRI calls RubyUtils.InspectObject, but this should be good enought as an error message:
                return(RubyExceptions.CreateArgumentError("{0} is not a symbol", context.GetClassOf(key).Name));
            }
        }
Ejemplo n.º 20
0
        public static RubyMethod /*!*/ Bind(UnboundMethod /*!*/ self, object target)
        {
            RubyContext ec = self._targetConstraint.Context;

            if (!ec.GetClassOf(target).HasAncestor(self._targetConstraint))
            {
                throw RubyExceptions.CreateTypeError(
                          String.Format("bind argument must be an instance of {0}", self._targetConstraint.Name)
                          );
            }

            return(new RubyMethod(target, self._info, self._name));
        }
Ejemplo n.º 21
0
        public sealed override T BindDelegate <T>(CallSite <T> site, object[] args)
        {
            object        firstArg     = args[0];
            ArgumentArray argArray     = firstArg as ArgumentArray;
            Type          delegateType = typeof(T);
            T             result;

            if (argArray != null)
            {
                firstArg = argArray.GetArgument(0);
            }
            else
            {
                object precompiled = BindPrecompiled(delegateType, args);
                if (precompiled != null)
                {
                    result = (T)precompiled;
                    CacheTarget(result);
                    return(result);
                }
            }

            RubyContext context = _context ?? ((Signature.HasScope) ? ((RubyScope)firstArg).RubyContext : (RubyContext)firstArg);

            if (context.Options.NoAdaptiveCompilation)
            {
                return(null);
            }

#if DEBUG
            if (RubyOptions.ShowRules)
            {
                var sb = new StringBuilder();
                for (int i = 1; i < args.Length; i++)
                {
                    sb.Append(RubyUtils.ObjectToMutableString(context, args[i]));
                    sb.Append(", ");
                }

                Utils.Log(String.Format(
                              "{0}: {1}; {2}",
                              this,
                              sb,
                              args.Length > 1 ? context.GetClassOf(args[1]).DebugName : null
                              ), "BIND");
            }
#endif
            result = this.LightBind(site, args, context.Options.CompilationThreshold);
            CacheTarget(result);
            return(result);
        }
Ejemplo n.º 22
0
            private void WriteObject(object /*!*/ obj)
            {
                _writer.Write((byte)'o');
                RubyClass theClass = _context.GetClassOf(obj);

                TestForAnonymous(theClass);
                WriteSymbol(theClass.Name);

#if !SILVERLIGHT
                ISerializable serializableObj = (obj as ISerializable);
                if (serializableObj != null)
                {
                    SerializationInfo info = new SerializationInfo(theClass.GetUnderlyingSystemType(), new FormatterConverter());
                    serializableObj.GetObjectData(info, _streamingContext);
                    int count = info.MemberCount;
                    try {
                        // We need this attribute for CLR serialization but it's not compatible with MRI serialization
                        // Unfortunately, there's no way to test for a value without either iterating over all values
                        // or throwing an exception if it's not present
                        if (info.GetValue("#class", typeof(RubyClass)) != null)
                        {
                            count--;
                        }
                    } catch (Exception) {
                    }
                    WriteInt32(count);
                    foreach (SerializationEntry entry in info)
                    {
                        if (!entry.Name.Equals("#class"))
                        {
                            WriteSymbol(entry.Name);
                            WriteAnObject(entry.Value);
                        }
                    }
                    return;
                }
#endif
            }
Ejemplo n.º 23
0
        public static MutableString /*!*/ TagUri(RubyContext /*!*/ context, object self)
        {
            var result    = MutableString.Create(Tags.RubyObject, context.GetIdentifierEncoding());
            var selfClass = context.GetClassOf(self);

            if (selfClass != context.ObjectClass)
            {
                return(result.Append(':').Append(context.GetClassName(self)));
            }
            else
            {
                return(result);
            }
        }
Ejemplo n.º 24
0
        public static MutableString /*!*/ ObjectToMutableString(RubyContext /*!*/ context, object obj)
        {
            using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(obj)) {
                if (handle == null)
                {
                    return(MutableString.Create("..."));
                }

                MutableString str = MutableString.CreateMutable();
                str.Append("#<");
                str.Append(context.GetClassOf(obj).Name);

                // Ruby prints 2*object_id for objects
                str.Append(':');
                AppendFormatHexObjectId(str, GetObjectId(context, obj));

                // display instance variables
                RubyInstanceData data = context.TryGetInstanceData(obj);
                if (data != null)
                {
                    var  vars  = data.GetInstanceVariablePairs();
                    bool first = true;
                    foreach (KeyValuePair <string, object> var in vars)
                    {
                        if (first)
                        {
                            str.Append(" ");
                            first = false;
                        }
                        else
                        {
                            str.Append(", ");
                        }
                        str.Append(var.Key);
                        str.Append("=");
                        str.Append(RubySites.Inspect(context, var.Value));
                    }
                }
                str.Append(">");

                return(str);
            }
        }
Ejemplo n.º 25
0
 private static DateTime MakeTime(RubyContext /*!*/ context, object obj)
 {
     if (obj == null)
     {
         return(DateTime.Now);
     }
     else if (obj is DateTime)
     {
         return((DateTime)obj);
     }
     else if (obj is int)
     {
         return(TimeOps.Create(typeof(RubyFileOps), (int)obj));
     }
     else
     {
         string name = context.GetClassOf(obj).Name;
         throw RubyExceptions.CreateTypeConversionError(name, "time");
     }
 }
Ejemplo n.º 26
0
                public static MutableString ToString(RubyContext /*!*/ context, Certificate /*!*/ self)
                {
                    using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) {
                        // #<OpenSSL::X509::Certificate subject=, issuer=, serial=0, not_before=nil, not_after=nil>
                        var result = MutableString.CreateEmpty();
                        result.Append("#<");
                        result.Append(context.Inspect(context.GetClassOf(self)));

                        if (handle == null)
                        {
                            return(result.Append(":...>"));
                        }
                        bool empty = self.IsEmpty;
                        result.AppendFormat(" subject={0}, issuer={1}, serial={2}, not_before=nil, not_after=nil>",
                                            empty ? "" : OpenSSLFormat(self._certificate.Subject),
                                            empty ? "" : OpenSSLFormat(self._certificate.Issuer),
                                            empty ? 0 : self.SerailNumber
                                            );
                        return(result);
                    }
                }
Ejemplo n.º 27
0
 private static RubyTime MakeTime(RubyContext /*!*/ context, object obj)
 {
     if (obj == null)
     {
         return(new RubyTime(DateTime.Now));
     }
     else if (obj is RubyTime)
     {
         return((RubyTime)obj);
     }
     else if (obj is int)
     {
         return(new RubyTime(RubyTime.Epoch.AddSeconds((int)obj)));
     }
     else if (obj is double)
     {
         return(new RubyTime(RubyTime.Epoch.AddSeconds((double)obj)));
     }
     else
     {
         string name = context.GetClassOf(obj).Name;
         throw RubyExceptions.CreateTypeConversionError(name, "time");
     }
 }
Ejemplo n.º 28
0
 public static RubyClass Include(RubyContext/*!*/ context, object/*!*/ self, params RubyModule[]/*!*/ modules)
 {
     RubyClass result = context.GetClassOf(self);
     result.IncludeModules(modules);
     return result;
 }
Ejemplo n.º 29
0
 public static RubyClass/*!*/ GetClassObsolete(RubyContext/*!*/ context, object self) {
     context.ReportWarning("Object#type will be deprecated; use Object#class");
     return context.GetClassOf(self);
 }
Ejemplo n.º 30
0
 public static bool Eql(BinaryOpStorage/*!*/ equals, RubyContext/*!*/ context, object self, object other) {
     if (context.GetClassOf(self) != context.GetClassOf(other)) {
         return false;
     }
     return Protocols.IsEqual(equals, context, self, other);
 }
Ejemplo n.º 31
0
 public static Exception/*!*/ MakeComparisonError(RubyContext/*!*/ context, object self, object other, Exception x) {
     string selfClass = context.GetClassOf(self).Name;
     string otherClass = context.GetClassOf(other).Name;
     return RubyExceptions.CreateArgumentError("comparison of " + selfClass+ " with " + otherClass + " failed", x);
 }
Ejemplo n.º 32
0
 public static MutableString/*!*/ Inspect(RubyContext/*!*/ context, BigDecimal/*!*/ self) {
     MutableString str = MutableString.CreateMutable();
     str.AppendFormat("#<{0}:", context.GetClassOf(self).Name);
     RubyUtils.AppendFormatHexObjectId(str, RubyUtils.GetObjectId(context, self));
     str.AppendFormat(",'{0}',", self.ToString(10));
     str.AppendFormat("{0}({1})>", self.PrecisionDigits.ToString(), self.MaxPrecisionDigits.ToString());
     return str;
 }
Ejemplo n.º 33
0
        public static RubyArray Coerce(ConversionStorage<double>/*!*/ tof1, ConversionStorage<double>/*!*/ tof2, 
            RubyContext/*!*/ context, object self, object other) {

            if (context.GetClassOf(self) == context.GetClassOf(other)) {
                return RubyOps.MakeArray2(other, self);
            }

            var site1 = tof1.GetSite(ConvertToFAction.Instance);
            var site2 = tof2.GetSite(ConvertToFAction.Instance);
            return RubyOps.MakeArray2(site1.Target(site1, context, other), site2.Target(site2, context, self));
        }
Ejemplo n.º 34
0
        public static RubyMethod/*!*/ GetClrMember(RubyContext/*!*/ context, object self, [DefaultParameterValue(null), NotNull]object asType, 
            [DefaultProtocol, NotNull]string/*!*/ name) {
            RubyMemberInfo info;

            RubyClass cls = context.GetClassOf(self);
            Type type = (asType != null) ? Protocols.ToType(context, asType) : null;
            if (!cls.TryGetClrMember(name, type, out info)) {
                throw RubyExceptions.CreateNameError("undefined CLR method `{0}' for class `{1}'", name, cls.Name);
            }

            return new RubyMethod(self, info, name);
        }
Ejemplo n.º 35
0
 public static RubyMethod/*!*/ GetMethod(RubyContext/*!*/ context, object self, [DefaultProtocol, NotNull]string/*!*/ name) {
     RubyMemberInfo info = context.ResolveMethod(self, name, VisibilityContext.AllVisible).Info;
     if (info == null) {
         throw RubyExceptions.CreateUndefinedMethodError(context.GetClassOf(self), name);
     }
     return new RubyMethod(self, info, name);
 }
Ejemplo n.º 36
0
        private static RubyClass/*!*/ ToSuperClass(RubyContext/*!*/ ec, object superClassObject) {
            if (superClassObject != null) {
                RubyClass superClass = superClassObject as RubyClass;
                if (superClass == null) {
                    throw RubyExceptions.CreateTypeError(String.Format("superclass must be a Class ({0} given)", ec.GetClassOf(superClassObject).Name));
                }

                if (superClass.IsSingletonClass) {
                    throw RubyExceptions.CreateTypeError("can't make subclass of virtual class");
                }

                return superClass;
            } else {
                return ec.ObjectClass;
            }
        }
Ejemplo n.º 37
0
        public static MutableString ToString(RubyContext/*!*/ context, Certificate/*!*/ self) {
          using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) {
            // #<OpenSSL::X509::Certificate subject=, issuer=, serial=0, not_before=nil, not_after=nil>
            var result = MutableString.CreateEmpty();
            result.Append("#<");
            result.Append(context.Inspect(context.GetClassOf(self)));

            if (handle == null) {
              return result.Append(":...>");
            }
            result.Append(' ');
            if (self._certificate.Handle == IntPtr.Zero) {
              result.Append("subject=, issuer=, serial=0, not_before=nil, not_after=nil");
            } else {
              result.Append(string.Format("subject={0}", OpenSSLFormat(self._certificate.Subject)));
              result.Append(string.Format(", issuer={0}", OpenSSLFormat(self._certificate.Issuer)));
              result.Append(string.Format(", serial={0}", self.SerailNumber));
              result.Append(string.Concat(", not_before=", "nil"));
              result.Append(string.Concat(", not_after=", "nil"));
            }
            result.Append('>');
            return result;
          }
        }
Ejemplo n.º 38
0
 public static Exception/*!*/ ReinitializeException(RubyContext/*!*/ context, Exception/*!*/ self, [DefaultParameterValue(null)]object message) {
     return ReinitializeException(self, message ?? context.GetClassOf(self).Name);
 }
Ejemplo n.º 39
0
 private static IList/*!*/ CreateResultArray(RubyContext/*!*/ context, IList/*!*/ self) {
     return _CreateArraySite.Target(_CreateArraySite, context, context.GetClassOf(self));
 }
Ejemplo n.º 40
0
 public static string/*!*/ GetClassName(RubyContext/*!*/ context, object self) {
     return context.GetClassOf(self).Name;
 }
Ejemplo n.º 41
0
 public static RubyClass/*!*/ GetClass(RubyContext/*!*/ context, object self) {
     return context.GetClassOf(self);
 }
Ejemplo n.º 42
0
        private static IList/*!*/ CreateResultArray(CallSiteStorage<Func<CallSite, RubyContext, RubyClass, object>>/*!*/ allocateStorage,
            RubyContext/*!*/ context, IList/*!*/ list) {
            
            // RubyArray:
            var array = list as RubyArray;
            if (array != null) {
                return array.CreateInstance();
            }
            
            // interop - call a default ctor to get an instance:
            var allocate = allocateStorage.GetCallSite("allocate", 0);
            var cls = context.GetClassOf(list);
            var result = allocate.Target(allocate, context, cls) as IList;
            if (result != null) {
                return result;
            }

            throw RubyExceptions.CreateTypeError(String.Format("{0}#allocate should return IList", cls.Name));
        }
Ejemplo n.º 43
0
        public static RubyMethod/*!*/ GetClrMember(RubyContext/*!*/ context, object self, [DefaultProtocol, NotNull]string/*!*/ name) {
            RubyMemberInfo info;

            RubyClass cls = context.GetClassOf(self);
            if (!cls.TryGetClrMember(name, out info)) {
                throw RubyExceptions.CreateNameError(String.Format("undefined CLR method `{0}' for class `{1}'", name, cls.Name));
            }

            return new RubyMethod(self, info, name);
        }
Ejemplo n.º 44
0
 public static Exception /*!*/ ReinitializeException(RubyContext /*!*/ context, Exception /*!*/ self, [DefaultParameterValue(null)] object message)
 {
     return(ReinitializeException(self, message ?? context.GetClassOf(self).Name));
 }
Ejemplo n.º 45
0
        public static bool TryDuplicateObject(RubyContext/*!*/ context, object obj, bool cloneSemantics, out object copy) {
            // Ruby value types can't be cloned
            if (RubyUtils.IsRubyValueType(obj)) {
                copy = null;
                return false;
            }

            IDuplicable clonable = obj as IDuplicable;
            if (clonable != null) {
                copy = clonable.Duplicate(context, cloneSemantics);
            } else {
                // .NET classes and library clases that doesn't implement IDuplicable:
                copy = RubySites.Allocate(context.GetClassOf(obj));
                context.CopyInstanceData(obj, copy, cloneSemantics);
            }

            // TODO: optimize
            _InitializeCopySharedSite.Target(_InitializeCopySharedSite, context, copy, obj);
            if (cloneSemantics) {
                context.FreezeObjectBy(copy, obj);
            }

            return true;
        }
Ejemplo n.º 46
0
        public static MutableString/*!*/ ObjectToMutableString(RubyContext/*!*/ context, object obj) {
            using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(obj)) {
                if (handle == null) {
                    return MutableString.Create("...");
                }

                MutableString str = MutableString.CreateMutable();
                str.Append("#<");
                str.Append(context.GetClassOf(obj).Name);

                // Ruby prints 2*object_id for objects
                str.Append(':');
                AppendFormatHexObjectId(str, GetObjectId(context, obj));

                // display instance variables
                RubyInstanceData data = context.TryGetInstanceData(obj);
                if (data != null) {
                    var vars = data.GetInstanceVariablePairs();
                    bool first = true;
                    foreach (KeyValuePair<string, object> var in vars) {
                        if (first) {
                            str.Append(" ");
                            first = false;
                        } else {
                            str.Append(", ");
                        }
                        str.Append(var.Key);
                        str.Append("=");
                        str.Append(RubySites.Inspect(context, var.Value));
                    }
                }
                str.Append(">");

                return str;
            }
        }
Ejemplo n.º 47
0
        //  declared private instance methods:
        //    initialize
        //  declared protected instance methods:
        //  declared public instance methods:

        private static Exception MakeKeyTypeException(RubyContext/*!*/ context, object key) {
            if (key == null) {
                return RubyExceptions.CreateTypeError("nil is not a symbol");
            } else {
                // MRI calls RubyUtils.InspectObject, but this should be good enought as an error message:
                return RubyExceptions.CreateArgumentError(String.Format("{0} is not a symbol", context.GetClassOf(key).Name));
            }
        }
Ejemplo n.º 48
0
        internal static IList ImplicitTrySplat(RubyContext /*!*/ context, object splattee)
        {
            var site = context.GetClassOf(splattee).ToImplicitTrySplatSite;

            return(site.Target(site, splattee) as IList);
        }
Ejemplo n.º 49
0
 public static Exception/*!*/ ReinitializeException(RubyContext/*!*/ context, Exception/*!*/ self, [DefaultParameterValue(null)]object message) {
     var instance = RubyExceptionData.GetInstance(self);
     instance.Backtrace = null;
     instance.Message = message ?? MutableString.Create(context.GetClassOf(self).Name);
     return self;
 }
Ejemplo n.º 50
0
 public static Exception/*!*/ MakeCoercionError(RubyContext/*!*/ context, object self, object other) {
     string selfClass = context.GetClassOf(self).Name;
     string otherClass = context.GetClassOf(other).Name;
     return CreateTypeError("{0} can't be coerced into {1}", selfClass, otherClass);
 }
Ejemplo n.º 51
0
 public static RubyArray Coerce(RubyContext/*!*/ context, object self, object other) {
     if (context.GetClassOf(self) == context.GetClassOf(other)) {
         return RubyOps.MakeArray2(other, self);
     }
     return RubyOps.MakeArray2(Protocols.ConvertToFloat(context, other), Protocols.ConvertToFloat(context, self));
 }
Ejemplo n.º 52
0
 public static string /*!*/ GetClassName(RubyContext /*!*/ context, object self)
 {
     return(context.GetClassOf(self).Name);
 }
Ejemplo n.º 53
0
 public static Exception/*!*/ MakeComparisonError(RubyContext/*!*/ context, object self, object other) {
     string selfClass = context.GetClassOf(self).Name;
     string otherClass = context.GetClassOf(other).Name;
     return CreateArgumentError("comparison of {0} with {1} failed", selfClass, otherClass);
 }
Ejemplo n.º 54
0
        private static RubyClass /*!*/ ToSuperClass(RubyContext /*!*/ ec, object superClassObject)
        {
            if (superClassObject != null)
            {
                RubyClass superClass = superClassObject as RubyClass;
                if (superClass == null)
                {
                    throw RubyExceptions.CreateTypeError(String.Format("superclass must be a Class ({0} given)", ec.GetClassOf(superClassObject).Name));
                }

                if (superClass.IsSingletonClass)
                {
                    throw RubyExceptions.CreateTypeError("can't make subclass of virtual class");
                }

                return(superClass);
            }
            else
            {
                return(ec.ObjectClass);
            }
        }
Ejemplo n.º 55
0
 internal static IList ConvertToArraySplat(RubyContext/*!*/ context, object splattee) {
     var site = context.GetClassOf(splattee).ToArraySplatSite;
     return site.Target(site, splattee) as IList;
 }
Ejemplo n.º 56
0
 private static RubyTime MakeTime(RubyContext/*!*/ context, object obj) {
     if (obj == null) {
         return new RubyTime(DateTime.Now);
     } else if (obj is RubyTime) {
         return (RubyTime)obj;
     } else if (obj is int) {
         return new RubyTime(RubyTime.Epoch.AddSeconds((int)obj));
     } else if (obj is double) {
         return new RubyTime(RubyTime.Epoch.AddSeconds((double)obj));
     } else {
         string name = context.GetClassOf(obj).Name;
         throw RubyExceptions.CreateTypeConversionError(name, "time");
     }
 }
Ejemplo n.º 57
0
                public static MutableString ToString(RubyContext/*!*/ context, Certificate/*!*/ self) {
                    using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) {
                        // #<OpenSSL::X509::Certificate subject=, issuer=, serial=0, not_before=nil, not_after=nil>
                        var result = MutableString.CreateEmpty();
                        result.Append("#<");
                        result.Append(context.Inspect(context.GetClassOf(self)));

                        if (handle == null) {
                            return result.Append(":...>");
                        }
                        bool empty = self._certificate.Handle == IntPtr.Zero;
                        result.AppendFormat(" subject={0}, issuer={1}, serial={2}, not_before=nil, not_after=nil>", 
                            empty ? "" : OpenSSLFormat(self._certificate.Subject),
                            empty ? "" : OpenSSLFormat(self._certificate.Issuer),
                            empty ? 0 : self.SerailNumber
                        );
                        return result;
                    }
                }
Ejemplo n.º 58
0
 private static DateTime MakeTime(RubyContext/*!*/ context, object obj) {
     if (obj == null) {
         return DateTime.Now;
     } else if (obj is DateTime) {
         return (DateTime)obj;
     } else if (obj is int) {
         return TimeOps.Create(typeof(RubyFileOps), (int)obj);
     } else {
         string name = context.GetClassOf(obj).Name;
         throw RubyExceptions.CreateTypeConversionError(name, "time");
     }
 }