Пример #1
0
        private int ReadNumberOrStar()
        {
            int res = 0; // default value

            if (_curCh == '*')
            {
                int?argindex = TryReadArgumentIndex();
                _curCh = _format[_index++];

                res = Protocols.CastToFixnum(_context, GetData(argindex));
            }
            else
            {
                if (Char.IsDigit(_curCh))
                {
                    res = 0;
                    while (Char.IsDigit(_curCh) && _index < this._format.Length)
                    {
                        res    = res * 10 + ((int)(_curCh - '0'));
                        _curCh = _format[_index++];
                    }
                }
            }
            return(res);
        }
Пример #2
0
        internal static AddressFamily ConvertToAddressFamily(ConversionStorage <MutableString> /*!*/ stringCast, ConversionStorage <int> /*!*/ fixnumCast,
                                                             object family)
        {
            // Default is AF_INET
            if (family == null)
            {
                return(AddressFamily.InterNetwork);
            }
            // If it is a Fixnum then assume it is just the constant value
            if (family is int)
            {
                return((AddressFamily)(int)family);
            }
            // Convert to a string (using to_str) and then look up the value
            MutableString strFamily = Protocols.CastToString(stringCast, family);

            foreach (AddressFamilyName name in FamilyNames)
            {
                if (name.Name.Equals(strFamily))
                {
                    return(name.Family);
                }
            }
            // Convert to a Fixnum (using to_i) and hope it is a valid AddressFamily constant
            return((AddressFamily)Protocols.CastToFixnum(fixnumCast, strFamily));
        }
Пример #3
0
        public static object Step(RubyContext /*!*/ context, BlockParam block, Range /*!*/ self, object step)
        {
            // We attempt to cast step to Fixnum here even though if we were iterating over Floats, for instance, we use step as is.
            // This prevents cases such as (1.0..2.0).step(0x800000000000000) {|x| x } from working but that is what MRI does.
            int intStep = Protocols.CastToFixnum(context, step);

            if (self.Begin is int && self.End is int)
            {
                // self.begin is Fixnum; directly call item = item + 1 instead of succ
                return(StepFixnum(context, block, self, (int)self.Begin, (int)self.End, intStep));
            }
            else if (self.Begin is MutableString)
            {
                // self.begin is String; use item.succ and item <=> self.end but make sure you check the length of the strings
                return(StepString(context, block, self, (MutableString)self.Begin, (MutableString)self.End, intStep));
            }
            else if (context.IsInstanceOf(self.Begin, context.GetClass(typeof(Numeric))))
            {
                // self.begin is Numeric; invoke item = item + 1 instead of succ and invoke < or <= for compare
                return(StepNumeric(context, block, self, self.Begin, self.End, step));
            }
            else
            {
                // self.begin is not Numeric or String; just invoke item.succ and item <=> self.end
                CheckBegin(context, self.Begin);
                return(StepObject(context, block, self, self.Begin, self.End, intStep));
            }
        }
Пример #4
0
        private static int GetComponent(ConversionStorage <int> /*!*/ conversionStorage, object[] components, int index, int defValue, bool zeroOk)
        {
            if (index >= components.Length || components[index] == null)
            {
                return(defValue);
            }

            object component = components[index];

            int result;

            try {
                result = Protocols.CastToFixnum(conversionStorage, component);
            } catch (InvalidOperationException) {
                MutableString str = component as MutableString;
                if (str == null)
                {
                    throw;
                }
                result = checked ((int)MutableStringOps.ToInteger(str, 10));
            }

            if (result == 0 && !zeroOk)
            {
                return(defValue);
            }
            else
            {
                return(result);
            }
        }
Пример #5
0
 private static int GetComponent(RubyContext /*!*/ context, object[] components, int index, int defValue)
 {
     if (index >= components.Length || components[index] == null)
     {
         return(defValue);
     }
     return(Protocols.CastToFixnum(context, components[index]));
 }
Пример #6
0
 internal static SocketFlags ConvertToSocketFlag(ConversionStorage <int> /*!*/ conversionStorage, object flags)
 {
     if (flags == null)
     {
         return(SocketFlags.None);
     }
     return((SocketFlags)Protocols.CastToFixnum(conversionStorage, flags));
 }
Пример #7
0
 internal static SocketFlags ConvertToSocketFlag(RubyContext /*!*/ context, object flags)
 {
     if (flags == null)
     {
         return(SocketFlags.None);
     }
     return((SocketFlags)Protocols.CastToFixnum(context, flags));
 }
Пример #8
0
        public static void SetSocketOption(RubyContext /*!*/ context, RubyBasicSocket /*!*/ self, object /*Numeric*/ level, object /*Numeric*/ optname, bool value)
        {
            Protocols.CheckSafeLevel(context, 2, "setsockopt");
            int iLevel   = Protocols.CastToFixnum(context, level);
            int iOptname = Protocols.CastToFixnum(context, optname);

            self.Socket.SetSocketOption((SocketOptionLevel)iLevel, (SocketOptionName)iOptname, value);
        }
Пример #9
0
        public static MutableString GetSocketOption(RubyContext /*!*/ context, RubyBasicSocket /*!*/ self, object /*Numeric*/ level, object /*Numeric*/ optname)
        {
            Protocols.CheckSafeLevel(context, 2, "getsockopt");
            int iLevel   = Protocols.CastToFixnum(context, Protocols.ConvertToInteger(context, level));
            int iOptname = Protocols.CastToFixnum(context, Protocols.ConvertToInteger(context, optname));

            byte[] value = self.Socket.GetSocketOption((SocketOptionLevel)iLevel, (SocketOptionName)iOptname, 4);
            return(MutableString.CreateBinary(value));
        }
Пример #10
0
        public static void SetSocketOption(RubyContext /*!*/ context, RubyBasicSocket /*!*/ self, object /*Numeric*/ level, object /*Numeric*/ optname, object value)
        {
            Protocols.CheckSafeLevel(context, 2, "setsockopt");
            MutableString strValue = Protocols.CastToString(context, value);
            int           iLevel   = Protocols.CastToFixnum(context, Protocols.ConvertToInteger(context, level));
            int           iOptname = Protocols.CastToFixnum(context, Protocols.ConvertToInteger(context, optname));

            self.Socket.SetSocketOption((SocketOptionLevel)iLevel, (SocketOptionName)iOptname, strValue.ConvertToBytes());
        }
Пример #11
0
        public static int Shutdown(RubyContext /*!*/ context, RubyBasicSocket /*!*/ self, object /*Numeric*/ how)
        {
            int iHow = 2;

            if (how != null)
            {
                iHow = Protocols.CastToFixnum(context, how);
            }
            return(Shutdown(context, self, iHow));
        }
Пример #12
0
 public static DateTime CreateGmtTime(RubyContext /*!*/ context, object /*!*/ self, [NotNull] params object[] /*!*/ components)
 {
     if (components.Length > 7 || components.Length == 0)
     {
         throw RubyExceptions.CreateArgumentError(String.Format("wrong number of arguments ({0} for 7)", components.Length));
     }
     return(new DateTime(Protocols.CastToFixnum(context, components[0]), GetComponent(context, components, 1, 1),
                         GetComponent(context, components, 2, 1), GetComponent(context, components, 3, 0), GetComponent(context, components, 4, 0),
                         GetComponent(context, components, 5, 0), DateTimeKind.Utc).AddTicks(GetComponent(context, components, 6, 0) * 10));
 }
Пример #13
0
        public static MutableString /*!*/ ToChr(RubyContext /*!*/ context, object self)
        {
            int intSelf = Protocols.CastToFixnum(context, self);

            if (intSelf < 0 || intSelf > 255)
            {
                throw RubyExceptions.CreateRangeError(String.Format("{0} out of char range", intSelf));
            }
            return(MutableString.CreateBinary(new byte[] { (byte)intSelf }));
        }
Пример #14
0
        public static RubyArray /*!*/ ValuesAt(RubyContext /*!*/ context, MatchData /*!*/ self, [NotNull] params object[] /*!*/ indices)
        {
            RubyArray result = new RubyArray();

            for (int i = 0; i < indices.Length; i++)
            {
                result.Add(GetGroup(context, self, Protocols.CastToFixnum(context, indices[i])));
            }
            return(result);
        }
Пример #15
0
        public static object SetLength(ConversionStorage <int> /*!*/ fixnumCast, StringIO /*!*/ self, object lengthObj)
        {
            int length = Protocols.CastToFixnum(fixnumCast, lengthObj);

            if (length < 0)
            {
                throw RubyExceptions.CreateEINVAL("negative length");
            }
            self.GetWritableContent().SetByteCount(length);
            return(lengthObj);
        }
Пример #16
0
        public static object Step(
            ConversionStorage <MutableString> /*!*/ stringCast,
            ConversionStorage <int> /*!*/ fixnumCast,
            RespondToStorage /*!*/ respondToStorage,
            BinaryOpStorage /*!*/ comparisonStorage,
            BinaryOpStorage /*!*/ lessThanStorage,
            BinaryOpStorage /*!*/ lessThanEqualsStorage,
            BinaryOpStorage /*!*/ greaterThanStorage,
            BinaryOpStorage /*!*/ equalsStorage,
            BinaryOpStorage /*!*/ addStorage,
            UnaryOpStorage /*!*/ succStorage,
            BlockParam block, Range /*!*/ self, [Optional] object step)
        {
            if (step == Missing.Value)
            {
                step = ClrInteger.One;
            }

            // We attempt to cast step to Fixnum here even though if we were iterating over Floats, for instance, we use step as is.
            // This prevents cases such as (1.0..2.0).step(0x800000000000000) {|x| x } from working but that is what MRI does.
            if (self.Begin is int && self.End is int)
            {
                // self.begin is Fixnum; directly call item = item + 1 instead of succ
                int intStep = Protocols.CastToFixnum(fixnumCast, step);
                return(StepFixnum(block, self, (int)self.Begin, (int)self.End, intStep));
            }
            else if (self.Begin is MutableString)
            {
                // self.begin is String; use item.succ and item <=> self.end but make sure you check the length of the strings
                int intStep = Protocols.CastToFixnum(fixnumCast, step);
                return(StepString(stringCast, comparisonStorage, lessThanStorage, greaterThanStorage, succStorage,
                                  block, self, (MutableString)self.Begin, (MutableString)self.End, intStep
                                  ));
            }
            else if (succStorage.Context.IsInstanceOf(self.Begin, succStorage.Context.GetClass(typeof(Numeric))))
            {
                // self.begin is Numeric; invoke item = item + 1 instead of succ and invoke < or <= for compare
                return(StepNumeric(lessThanStorage, lessThanEqualsStorage, equalsStorage, addStorage,
                                   block, self, self.Begin, self.End, step
                                   ));
            }
            else
            {
                // self.begin is not Numeric or String; just invoke item.succ and item <=> self.end
                CheckBegin(respondToStorage, self.Begin);
                int intStep = Protocols.CastToFixnum(fixnumCast, step);
                return(StepObject(comparisonStorage, lessThanStorage, greaterThanStorage, equalsStorage, succStorage,
                                  block, self, self.Begin, self.End, intStep
                                  ));
            }
        }
Пример #17
0
        public static RubyArray GetAddressInfo(RubyContext /*!*/ context, RubyClass /*!*/ self, object hostname, object port,
                                               [DefaultParameterValue(null)] object family,
                                               [DefaultParameterValue(0)] object socktype,
                                               [DefaultParameterValue(0)] object protocol,
                                               [DefaultParameterValue(null)] object flags)
        {
            MutableString strHostname = ConvertToHostString(context, hostname);
            int           iPort       = ConvertToPortNum(context, port);

            // Sadly the family, socktype, protocol and flags get passed through at best and ignored at worst,
            // since System.Net does not provide for them
            AddressFamily addrFamily   = ConvertToAddressFamily(context, family);
            int           socketType   = Protocols.CastToFixnum(context, socktype);
            int           protocolType = Protocols.CastToFixnum(context, protocol);

            RubyArray results = new RubyArray();

            IPHostEntry entry = (strHostname != null) ? GetHostEntry(strHostname.ConvertToString()) : MakeEntry(IPAddress.Any);

            for (int i = 0; i < entry.AddressList.Length; ++i)
            {
                IPAddress address = entry.AddressList[i];
                RubyArray result  = new RubyArray();
                result.Add(ToAddressFamilyString(address.AddressFamily));
                result.Add(iPort);
                if (DoNotReverseLookup(context).Value)
                {
                    result.Add(MutableString.Create(address.ToString()));
                }
                else
                {
                    IPHostEntry alias = GetHostEntry(address);
                    result.Add(MutableString.Create(alias.HostName));
                }
                result.Add(MutableString.Create(address.ToString()));
                result.Add((int)address.AddressFamily);
                result.Add(socketType);

                // TODO: protocol type:
                result.Add(protocolType);

                results.Add(result);
            }
            return(results);
        }
Пример #18
0
        public static RubyArray /*!*/ ValuesAt(RubyStruct /*!*/ self, [NotNull] params object[] values)
        {
            RubyArray result = new RubyArray();

            object[] data = self.Values;

            for (int i = 0; i < values.Length; ++i)
            {
                Range range = values[i] as Range;
                if (range != null)
                {
                    bool excludeEnd;
                    int  begin, end;
                    Protocols.ConvertToIntegerRange(self.Class.Context, range, out begin, out end, out excludeEnd);

                    if (excludeEnd)
                    {
                        end -= 1;
                    }

                    begin = NormalizeIndex(data.Length, begin);
                    end   = NormalizeIndex(data.Length, end);
                    Debug.Assert(end - begin <= data.Length); // because we normalized the indicies

                    if (end - begin > 0)
                    {
                        result.Capacity += (end - begin);
                        for (int j = begin; j <= end; j++)
                        {
                            result.Add(data[j]);
                        }
                    }
                }
                else
                {
                    int index = NormalizeIndex(data.Length, Protocols.CastToFixnum(self.Class.Context, values[i]));
                    result.Add(data[index]);
                }
            }

            return(result);
        }
Пример #19
0
        public static RubyArray GetAddressInfo(
            ConversionStorage <MutableString> /*!*/ stringCast, ConversionStorage <int> /*!*/ fixnumCast,
            RubyClass /*!*/ self, object hostNameOrAddress, object port,
            [DefaultParameterValue(null)] object family,
            [DefaultParameterValue(0)] object socktype,
            [DefaultParameterValue(0)] object protocol,
            [DefaultParameterValue(null)] object flags)
        {
            RubyContext context = self.Context;

            IPHostEntry entry = (hostNameOrAddress != null) ?
                                GetHostEntry(ConvertToHostString(stringCast, hostNameOrAddress), DoNotReverseLookup(context).Value) :
                                MakeEntry(IPAddress.Any, DoNotReverseLookup(context).Value);

            int iPort = ConvertToPortNum(stringCast, fixnumCast, port);

            // TODO: ignore family, the only supported families are InterNetwork and InterNetworkV6
            ConvertToAddressFamily(stringCast, fixnumCast, family);
            int socketType   = Protocols.CastToFixnum(fixnumCast, socktype);
            int protocolType = Protocols.CastToFixnum(fixnumCast, protocol);

            RubyArray results = new RubyArray(entry.AddressList.Length);

            for (int i = 0; i < entry.AddressList.Length; ++i)
            {
                IPAddress address = entry.AddressList[i];

                RubyArray result = new RubyArray(9);
                result.Add(ToAddressFamilyString(address.AddressFamily));
                result.Add(iPort);
                result.Add(HostNameToMutableString(context, IPAddressToHostName(address, DoNotReverseLookup(context).Value)));
                result.Add(MutableString.CreateAscii(address.ToString()));
                result.Add((int)address.AddressFamily);
                result.Add(socketType);

                // TODO: protocol type:
                result.Add(protocolType);

                results.Add(result);
            }
            return(results);
        }
Пример #20
0
        public static RubyArray /*!*/ ValuesAt(ConversionStorage <int> /*!*/ fixnumCast, RubyStruct /*!*/ self, params object[] /*!*/ values)
        {
            RubyArray result = new RubyArray();

            object[] data = self.Values;

            for (int i = 0; i < values.Length; ++i)
            {
                Range range = values[i] as Range;
                if (range != null)
                {
                    int begin = Protocols.CastToFixnum(fixnumCast, range.Begin);
                    int end   = Protocols.CastToFixnum(fixnumCast, range.End);

                    if (range.ExcludeEnd)
                    {
                        end -= 1;
                    }

                    begin = NormalizeIndex(data.Length, begin);
                    end   = NormalizeIndex(data.Length, end);
                    Debug.Assert(end - begin <= data.Length); // because we normalized the indicies

                    if (end - begin > 0)
                    {
                        result.AddCapacity(end - begin);
                        for (int j = begin; j <= end; j++)
                        {
                            result.Add(data[j]);
                        }
                    }
                }
                else
                {
                    int index = NormalizeIndex(data.Length, Protocols.CastToFixnum(fixnumCast, values[i]));
                    result.Add(data[index]);
                }
            }

            return(result);
        }
Пример #21
0
        public static object CreateArray(BlockParam block, RubyClass /*!*/ self,
                                         [NotNull] object /*!*/ arrayOrSize)
        {
            IList array = Protocols.AsArray(self.Context, arrayOrSize);

            if (array != null)
            {
                // block ignored
                return(CreateArray(array));
            }

            int size = Protocols.CastToFixnum(self.Context, arrayOrSize);

            if (block != null)
            {
                return(CreateArray(block, size));
            }
            else
            {
                return(CreateArray(self, size, null));
            }
        }
Пример #22
0
        private void AppendChar()
        {
            int fixChar = Protocols.CastToFixnum(_context, _opts.Value);

            fixChar = fixChar > 0 ? fixChar % 256 : ((-fixChar / 256) + 1) * 256 + fixChar;
            char val = (char)fixChar;

            if (_opts.FieldWidth > 1)
            {
                if (!_opts.LeftAdj)
                {
                    _buf.Append(' ', _opts.FieldWidth - 1);
                }
                _buf.Append(val);
                if (_opts.LeftAdj)
                {
                    _buf.Append(' ', _opts.FieldWidth - 1);
                }
            }
            else
            {
                _buf.Append(val);
            }
        }
Пример #23
0
        public static object Reinitialize(RubyContext /*!*/ context, BlockParam block, RubyArray /*!*/ self,
                                          [NotNull] object /*!*/ arrayOrSize)
        {
            RubyUtils.RequiresNotFrozen(context, self);

            IList array = Protocols.AsArray(context, arrayOrSize);

            if (array != null)
            {
                // block ignored
                return(Reinitialize(self, array));
            }

            int size = Protocols.CastToFixnum(context, arrayOrSize);

            if (block != null)
            {
                return(Reinitialize(block, self, size));
            }
            else
            {
                return(ReinitializeByRepeatedValue(context, self, size, null));
            }
        }
Пример #24
0
 public static object EditDocument(RubyContext /*!*/ context, YamlStream /*!*/ self, object index, object document)
 {
     return(IListOps.SetElement(context, self.Documents, Protocols.CastToFixnum(context, index), document));
 }
Пример #25
0
 public static object SetValue(ConversionStorage <int> /*!*/ conversionStorage, RubyStruct /*!*/ self, object index, object value)
 {
     return(self[NormalizeIndex(self.ItemCount, Protocols.CastToFixnum(conversionStorage, index))] = value);
 }
Пример #26
0
 public static int InducedFrom(RubyClass /*!*/ self, object obj)
 {
     return(Protocols.CastToFixnum(self.Context, Protocols.ConvertToInteger(self.Context, obj)));
 }
Пример #27
0
 public static object SetValue(RubyStruct /*!*/ self, object index, object value)
 {
     return(self[NormalizeIndex(self.ItemCount, Protocols.CastToFixnum(self.Class.Context, index))] = value);
 }
Пример #28
0
 public static object GetDocument(RubyContext /*!*/ context, YamlStream /*!*/ self, object index)
 {
     return(IListOps.GetElement(self.Documents, Protocols.CastToFixnum(context, index)));
 }