Exemple #1
0
        public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, Hash /*!*/ self)
        {
            using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) {
                if (handle == null)
                {
                    return(MutableString.CreateAscii("{...}"));
                }

                MutableString str = MutableString.CreateMutable(RubyEncoding.Binary);
                str.Append('{');
                bool first = true;
                foreach (var entry in self)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        str.Append(", ");
                    }
                    str.Append(context.Inspect(BaseSymbolDictionary.ObjToNull(entry.Key)));
                    str.Append("=>");
                    str.Append(context.Inspect(entry.Value));
                }
                str.Append('}');
                return(str);
            }
        }
Exemple #2
0
        public static RubyArray /*!*/ GetAllNames(RubyContext /*!*/ context, RubyEncoding /*!*/ self)
        {
            var result = new RubyArray();

            string name = self.Name;

            result.Add(MutableString.Create(name));

            foreach (var alias in RubyEncoding.Aliases)
            {
                if (StringComparer.OrdinalIgnoreCase.Equals(alias.Value, name))
                {
                    result.Add(MutableString.CreateAscii(alias.Key));
                }
            }

            if (self == context.RubyOptions.LocaleEncoding)
            {
                result.Add(MutableString.CreateAscii("locale"));
            }

            if (self == context.DefaultExternalEncoding)
            {
                result.Add(MutableString.CreateAscii("external"));
            }

            if (self == context.GetPathEncoding())
            {
                result.Add(MutableString.CreateAscii("filesystem"));
            }

            return(result);
        }
        public static object Status(Thread /*!*/ self)
        {
            RubyThreadInfo.RegisterThread(Thread.CurrentThread);
            switch (GetStatus(self))
            {
            case RubyThreadStatus.Unstarted:
                return(MutableString.CreateAscii("unstarted"));

            case RubyThreadStatus.Running:
                return(MutableString.CreateAscii("run"));

            case RubyThreadStatus.Sleeping:
                return(MutableString.CreateAscii("sleep"));

            case RubyThreadStatus.Aborting:
                return(MutableString.CreateAscii("aborting"));

            case RubyThreadStatus.Completed:
                return(false);

            case RubyThreadStatus.Aborted:
                return(null);

            default:
                throw new ArgumentException("unknown thread status");
            }
        }
Exemple #4
0
 public static MutableString /*!*/ Dump(char self)
 {
     return(MutableString.CreateAscii(
                MutableString.AppendUnicodeRepresentation(
                    new StringBuilder().Append('\''), self.ToString(), MutableString.Escape.Special | MutableString.Escape.NonAscii, '\'', -1
                    ).Append("' (Char)").ToString()
                ));
 }
 public static MutableString /*!*/ Inspect(Status /*!*/ self)
 {
     return(MutableString.CreateAscii(String.Format(CultureInfo.InvariantCulture, "#<Process::Status: pid={0},{1}({2})>",
                                                    Pid(self),
                                                    Exited(self) ? "exited" : "running",
                                                    ExitStatus(self)
                                                    )));
 }
Exemple #6
0
 public static MutableString /*!*/ CTime(RubyTime /*!*/ self)
 {
     return(MutableString.CreateAscii(String.Format(CultureInfo.InvariantCulture,
                                                    "{0:ddd MMM} {1,2} {0:HH:mm:ss yyyy}",
                                                    self.DateTime,
                                                    self.DateTime.Day
                                                    )));
 }
Exemple #7
0
 public static MutableString /*!*/ ToString(RubyContext /*!*/ context, RubyTime /*!*/ self)
 {
     return(MutableString.CreateAscii(String.Format(CultureInfo.InvariantCulture,
                                                    (context.RubyOptions.Compatibility < RubyCompatibility.Ruby19) ?
                                                    "{0:ddd MMM dd HH:mm:ss} {1} {0:yyyy}" :
                                                    "{0:yyyy-dd-MM HH:mm:ss} {1}",
                                                    self.DateTime,
                                                    (self.Kind == DateTimeKind.Utc) ? "UTC" : self.FormatUtcOffset()
                                                    )));
 }
Exemple #8
0
        public static MutableString /*!*/ ToString(BigInteger /*!*/ self, int radix)
        {
            if (radix < 2 || radix > 36)
            {
                throw RubyExceptions.CreateArgumentError("illegal radix {0}", radix);
            }

            // TODO: Can we do the ToLower in BigInteger?
            return(MutableString.CreateAscii(self.ToString(radix).ToLowerInvariant()));
        }
Exemple #9
0
 public static object ToString([NotNull] BigInteger /*!*/ self, int radix)
 {
     if (radix < 2 || radix > 36)
     {
         throw RubyExceptions.CreateArgumentError("illegal radix {0}", radix);
     }
     // TODO: Should we try to use a Fixnum specific ToString?
     // TODO: Can we do the ToLower in BigInteger?
     return(MutableString.CreateAscii(self.ToString(radix).ToLowerInvariant()));
 }
 public static MutableString /*!*/ ToPrintedString(ConversionStorage <MutableString> /*!*/ tosConversion, object obj)
 {
     if (obj == null)
     {
         return(MutableString.CreateAscii("nil"));
     }
     else
     {
         return(Protocols.ConvertToString(tosConversion, obj));
     }
 }
        public static void Print(BinaryOpStorage /*!*/ writeStorage, object self, object value)
        {
            Protocols.Write(writeStorage, self, value ?? MutableString.CreateAscii("nil"));

            MutableString delimiter = writeStorage.Context.OutputSeparator;

            if (delimiter != null)
            {
                Protocols.Write(writeStorage, self, delimiter);
            }
        }
Exemple #12
0
 public static MutableString /*!*/ ToMutableString(ConversionStorage <MutableString> /*!*/ tosConversion,
                                                   IDictionary <object, object> /*!*/ self)
 {
     using (IDisposable handle = RubyUtils.InfiniteToSTracker.TrackObject(self)) {
         if (handle == null)
         {
             return(MutableString.CreateAscii("{...}"));
         }
         else
         {
             return(IListOps.Join(tosConversion, ToArray(self)));
         }
     }
 }
Exemple #13
0
        public static Hash /*!*/ List(RubyContext /*!*/ context, RubyModule /*!*/ self)
        {
            Hash result = new Hash(context);

            result.Add(MutableString.CreateAscii("TERM"), ScriptingRuntimeHelpers.Int32ToObject(15));
            result.Add(MutableString.CreateAscii("SEGV"), ScriptingRuntimeHelpers.Int32ToObject(11));
            result.Add(MutableString.CreateAscii("KILL"), ScriptingRuntimeHelpers.Int32ToObject(9));
            result.Add(MutableString.CreateAscii("EXIT"), ScriptingRuntimeHelpers.Int32ToObject(0));
            result.Add(MutableString.CreateAscii("INT"), ScriptingRuntimeHelpers.Int32ToObject(2));
            result.Add(MutableString.CreateAscii("FPE"), ScriptingRuntimeHelpers.Int32ToObject(8));
            result.Add(MutableString.CreateAscii("ABRT"), ScriptingRuntimeHelpers.Int32ToObject(22));
            result.Add(MutableString.CreateAscii("ILL"), ScriptingRuntimeHelpers.Int32ToObject(4));
            return(result);
        }
Exemple #14
0
        public static int SysWrite(BinaryOpStorage /*!*/ writeStorage, ConversionStorage <MutableString> /*!*/ tosConversion,
                                   RubyContext /*!*/ context, RubyIO /*!*/ self, [NotNull] MutableString /*!*/ val)
        {
            RubyBufferedStream stream = self.GetWritableStream();

            if (stream.DataBuffered)
            {
                PrintOps.ReportWarning(writeStorage, tosConversion, MutableString.CreateAscii("syswrite for buffered IO"));
            }
            int bytes = Write(self, val);

            self.Flush();
            return(bytes);
        }
Exemple #15
0
        public static Hash /*!*/ GetAliases(RubyClass /*!*/ self)
        {
            var context = self.Context;
            var result  = new Hash(context.EqualityComparer, RubyEncoding.Aliases.Count + 3);

            foreach (var alias in RubyEncoding.Aliases)
            {
                result.Add(MutableString.CreateAscii(alias.Key).Freeze(), MutableString.CreateAscii(alias.Value).Freeze());
            }

            result.Add(MutableString.CreateAscii("locale").Freeze(), MutableString.Create(context.RubyOptions.LocaleEncoding.Name).Freeze());
            result.Add(MutableString.CreateAscii("external").Freeze(), MutableString.Create(context.DefaultExternalEncoding.Name).Freeze());
            result.Add(MutableString.CreateAscii("filesystem").Freeze(), MutableString.Create(context.GetPathEncoding().Name).Freeze());
            return(result);
        }
Exemple #16
0
        public static MutableString GetKCode(RubyRegex /*!*/ self)
        {
            switch (self.Options & RubyRegexOptions.EncodingMask)
            {
            case RubyRegexOptions.NONE: return(null);

            case RubyRegexOptions.EUC: return(MutableString.CreateAscii("euc"));

            case RubyRegexOptions.FIXED: return(MutableString.CreateAscii("none"));

            case RubyRegexOptions.UTF8: return(MutableString.CreateAscii("utf8"));

            case RubyRegexOptions.SJIS: return(MutableString.CreateAscii("sjis"));

            default: throw Assert.Unreachable;
            }
        }
Exemple #17
0
        public MutableString ReadParagraph(RubyEncoding /*!*/ encoding, bool preserveEndOfLines)
        {
            var result = ReadLine(MutableString.CreateAscii("\n\n"), encoding, preserveEndOfLines);

            int c;

            while ((c = PeekByteNormalizeEoln(preserveEndOfLines)) != -1)
            {
                if (c != '\n')
                {
                    break;
                }
                ReadByteNormalizeEoln(preserveEndOfLines);
            }

            return(result);
        }
Exemple #18
0
 public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, FileSystemInfo /*!*/ self)
 {
     return(MutableString.CreateAscii(String.Format(CultureInfo.InvariantCulture,
                                                    "#<File::Stat dev={0}, ino={1}, mode={2}, nlink={3}, uid={4}, gid={5}, rdev={6}, size={7}, blksize={8}, blocks={9}, atime={10}, mtime={11}, ctime={12}",
                                                    context.Inspect(DeviceId(self)),
                                                    context.Inspect(Inode(self)),
                                                    context.Inspect(Mode(self)),
                                                    context.Inspect(NumberOfLinks(self)),
                                                    context.Inspect(UserId(self)),
                                                    context.Inspect(GroupId(self)),
                                                    context.Inspect(DeviceId(self)),
                                                    context.Inspect(Size(self)),
                                                    context.Inspect(BlockSize(self)),
                                                    context.Inspect(Blocks(self)),
                                                    context.Inspect(AccessTime(self)),
                                                    context.Inspect(ModifiedTime(self)),
                                                    context.Inspect(CreateTime(self))
                                                    )));
 }
Exemple #19
0
 public static MutableString /*!*/ GetZone(RubyContext /*!*/ context, RubyTime /*!*/ self)
 {
     if (self.Kind == DateTimeKind.Utc)
     {
         return(MutableString.CreateAscii("UTC"));
     }
     else
     {
         var name = RubyTime.GetCurrentZoneName();
         if (name.IsAscii())
         {
             return(MutableString.CreateAscii(name));
         }
         else
         {
             // TODO: what encoding should we use?
             return(MutableString.Create(name, context.GetPathEncoding()));
         }
     }
 }
Exemple #20
0
        private static RubyRegex /*!*/ Union(ConversionStorage <MutableString> /*!*/ stringCast, ICollection /*!*/ objs)
        {
            if (objs.Count == 0)
            {
                return(new RubyRegex(MutableString.CreateAscii("(?!)"), RubyRegexOptions.NONE));
            }

            MutableString result = MutableString.CreateMutable(RubyEncoding.Binary);
            int           i      = 0;

            foreach (var obj in objs)
            {
                if (i > 0)
                {
                    result.Append('|');
                }

                // TODO: to_regexp
                RubyRegex regex = obj as RubyRegex;
                if (regex != null)
                {
                    if (objs.Count == 1)
                    {
                        return(regex);
                    }

                    regex.AppendTo(result);
                }
                else
                {
                    result.Append(RubyRegex.Escape(Protocols.CastToString(stringCast, obj)));
                }

                i++;
            }

            return(new RubyRegex(result, RubyRegexOptions.NONE));
        }
Exemple #21
0
 public static MutableString Inspect(RubyContext /*!*/ context, IDictionary <object, object> /*!*/ self)
 {
     using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) {
         if (handle == null)
         {
             return(MutableString.CreateAscii("{...}"));
         }
         MutableString str = MutableString.CreateMutable(RubyEncoding.Binary);
         str.Append('{');
         foreach (KeyValuePair <object, object> pair in self)
         {
             if (str.Length != 1)
             {
                 str.Append(", ");
             }
             str.Append(context.Inspect(CustomStringDictionary.ObjToNull(pair.Key)));
             str.Append("=>");
             str.Append(context.Inspect(pair.Value));
         }
         str.Append('}');
         return(str);
     }
 }
Exemple #22
0
        public static RubyRegex /*!*/ Union(ConversionStorage <MutableString> /*!*/ stringCast, RubyClass /*!*/ self, params object[] /*!*/ strings)
        {
            if (strings.Length == 0)
            {
                return(new RubyRegex(MutableString.CreateAscii("(?!)"), RubyRegexOptions.NONE));
            }

            MutableString result = MutableString.CreateMutable(RubyEncoding.Binary);

            for (int i = 0; i < strings.Length; i++)
            {
                if (i > 0)
                {
                    result.Append('|');
                }

                RubyRegex regex = strings[i] as RubyRegex;
                if (regex != null)
                {
                    if (strings.Length == 1)
                    {
                        return(regex);
                    }

                    regex.AppendTo(result);
                }
                else
                {
                    result.Append(RubyRegex.Escape(Protocols.CastToString(stringCast, strings[i])));
                }
            }

            // TODO:
            //RubyClass regexClass = RubyUtils.GetExecutionContext(context).GetClass(typeof(RubyRegex));
            //return NewCallSite3.Invoke(context, regexClass, result, null, null);
            return(new RubyRegex(result, RubyRegexOptions.NONE));
        }
Exemple #23
0
        public static RubyArray /*!*/ GetNameList(RubyClass /*!*/ self)
        {
            var infos  = Encoding.GetEncodings();
            var result = new RubyArray(1 + infos.Length);

            // Ruby specific:
            result.Add(MutableString.CreateAscii(RubyEncoding.Binary.Name));

            foreach (var info in infos)
            {
                result.Add(MutableString.Create(RubyEncoding.GetRubySpecificName(info.CodePage) ?? info.Name));
            }

            foreach (var alias in RubyEncoding.Aliases.Keys)
            {
                result.Add(MutableString.CreateAscii(alias));
            }

            result.Add(MutableString.CreateAscii("locale"));
            result.Add(MutableString.CreateAscii("external"));
            result.Add(MutableString.CreateAscii("filesystem"));

            return(result);
        }
        public static void Puts(BinaryOpStorage /*!*/ writeStorage, ConversionStorage <MutableString> /*!*/ tosConversion,
                                ConversionStorage <IList> /*!*/ tryToAry, object self, [NotNull] object /*!*/ val)
        {
            IList list = Protocols.TryCastToArray(tryToAry, val);

            if (list != null)
            {
                IEnumerable recEnum = IListOps.EnumerateRecursively(tryToAry, list, (_) => MutableString.CreateAscii("[...]"));
                foreach (object item in recEnum ?? list)
                {
                    Puts(writeStorage, self, ToPrintedString(tosConversion, item));
                }
            }
            else
            {
                Puts(writeStorage, self, ToPrintedString(tosConversion, val));
            }
        }
 public static void PutsEmptyLine(BinaryOpStorage /*!*/ writeStorage, object self)
 {
     Protocols.Write(writeStorage, self, MutableString.CreateAscii("\n"));
 }
Exemple #26
0
 public static MutableString /*!*/ ToString(BigInteger /*!*/ self)
 {
     return(MutableString.CreateAscii(self.ToString()));
 }
 public static MutableString Inspect(object self)
 {
     return(MutableString.CreateAscii("nil"));
 }
Exemple #28
0
 public static MutableString /*!*/ ToString(RubyContext /*!*/ context, RubyTime /*!*/ self)
 {
     return(MutableString.CreateAscii(String.Format(CultureInfo.InvariantCulture,
                                                    "{0:yyyy-MM-dd HH:mm:ss} {1}", self.DateTime, (self.Kind == DateTimeKind.Utc) ? "UTC" : self.FormatUtcOffset()
                                                    )));
 }
Exemple #29
0
 public static MutableString ToS(RubyContext /*!*/ context, object self)
 {
     return(MutableString.CreateAscii("ARGF"));
 }
Exemple #30
0
 public static object ToString(object /*!*/ self)
 {
     return(MutableString.CreateAscii(self.ToString()));
 }