Exemple #1
0
        private static object CreateDefaultStream(UnaryOpStorage /*!*/ newStorage, RubyScope /*!*/ scope, RubyModule /*!*/ yamlModule)
        {
            object streamClass = RubyUtils.GetConstant(scope.GlobalScope, yamlModule, "Stream", false);
            var    newSite     = newStorage.GetCallSite("new");

            return(newSite.Target(newSite, streamClass));
        }
Exemple #2
0
        public static object Each(
            ConversionStorage <MutableString> /*!*/ stringCast,
            RespondToStorage /*!*/ respondToStorage,
            BinaryOpStorage /*!*/ comparisonStorage,
            BinaryOpStorage /*!*/ lessThanStorage,
            BinaryOpStorage /*!*/ greaterThanStorage,
            BinaryOpStorage /*!*/ equalsStorage,
            UnaryOpStorage /*!*/ succStorage,
            BlockParam block, Range /*!*/ self)
        {
            // MRI: Checks that self.begin responds to "succ" even though it might not be used.
            CheckBegin(respondToStorage, self.Begin);

            if (self.Begin is int && self.End is int)
            {
                return(StepFixnum(block, self, (int)self.Begin, (int)self.End, 1));
            }
            else if (self.Begin is MutableString)
            {
                return(StepString(stringCast, comparisonStorage, lessThanStorage, greaterThanStorage, succStorage,
                                  block, self, (MutableString)self.Begin, (MutableString)self.End, 1
                                  ));
            }
            else
            {
                return(StepObject(comparisonStorage, lessThanStorage, greaterThanStorage, equalsStorage, succStorage,
                                  block, self, self.Begin, self.End, 1
                                  ));
            }
        }
Exemple #3
0
            public static object Wrap(BinaryOpStorage /*!*/ newStorage, UnaryOpStorage /*!*/ closeStorage,
                                      BlockParam block, RubyClass /*!*/ self, object io)
            {
                var      newSite  = newStorage.GetCallSite("new");
                GZipFile gzipFile = (GZipFile)newSite.Target(newSite, self, io);

                return(gzipFile.Do(block));
            }
Exemple #4
0
        public override string ToString() {
#if DEBUG // This can be made un-conditional after RubyTypeBuilder is also updated to override ToString
            UnaryOpStorage unaryOpStorage = new UnaryOpStorage(_class.Context);
            return RubyUtils.ObjectToMutableString(unaryOpStorage, this).ConvertToString();
#else
            return base.ToString();
#endif
        }
Exemple #5
0
 public static object Abs(BinaryOpStorage/*!*/ lessThanStorage, UnaryOpStorage/*!*/ minusStorage, object self)
 {
     var lessThan = lessThanStorage.GetCallSite("<");
     if (RubyOps.IsTrue(lessThan.Target(lessThan, self, 0))) {
         var minus = minusStorage.GetCallSite("-@");
         return minus.Target(minus, self);
     }
     return self;
 }
Exemple #6
0
        public static object Abs(BinaryOpStorage /*!*/ lessThanStorage, UnaryOpStorage /*!*/ minusStorage, object self)
        {
            var lessThan = lessThanStorage.GetCallSite("<");

            if (RubyOps.IsTrue(lessThan.Target(lessThan, self, 0)))
            {
                var minus = minusStorage.GetCallSite("-@");
                return(minus.Target(minus, self));
            }
            return(self);
        }
Exemple #7
0
        public static object DumpStream(UnaryOpStorage /*!*/ newStorage, BinaryOpStorage /*!*/ addStorage, UnaryOpStorage /*!*/ emitStorage,
                                        RubyScope /*!*/ scope, RubyModule /*!*/ self, params object[] /*!*/ args)
        {
            object io = CreateDefaultStream(newStorage, scope, self);

            AddDocumentsToStream(addStorage, args, io);

            var emitSite = emitStorage.GetCallSite("emit");

            return(emitSite.Target(emitSite, io));
        }
Exemple #8
0
        public static object LoadStream(ConversionStorage <MutableString> /*!*/ toStr, RespondToStorage /*!*/ respondTo,
                                        UnaryOpStorage /*!*/ newStorage, BinaryOpStorage /*!*/ addStorage, RubyScope /*!*/ scope,
                                        RubyModule /*!*/ self, object io)
        {
            RubyConstructor rc = MakeConstructor(scope.GlobalScope, GetStream(toStr, respondTo, io));

            // TODO: only if io was converted to a string:
            io = CreateDefaultStream(newStorage, scope, self);

            AddDocumentsToStream(addStorage, rc, io);
            return(io);
        }
Exemple #9
0
        public static int GetHashCode(UnaryOpStorage /*!*/ hashStorage, Range /*!*/ self)
        {
            // MRI: Ruby treatment of hash return value is inconsistent.
            // No conversions happen here (unlike e.g. Array.hash).
            var hashSite = hashStorage.GetCallSite("hash");

            return(unchecked (
                       Protocols.ToHashCode(hashSite.Target(hashSite, self.Begin)) ^
                       Protocols.ToHashCode(hashSite.Target(hashSite, self.End)) ^
                       (self.ExcludeEnd ? 179425693 : 1794210891)
                       ));
        }
Exemple #10
0
        /// <summary>
        /// Step through a Range of Strings.
        /// </summary>
        /// <remarks>
        /// This method requires step to be a Fixnum.
        /// It uses a hybrid string comparison to prevent infinite loops and calls String#succ to get each item in the range.
        /// </remarks>
        private static object StepString(
            ConversionStorage <MutableString> /*!*/ stringCast,
            BinaryOpStorage /*!*/ comparisonStorage,
            BinaryOpStorage /*!*/ lessThanStorage,
            BinaryOpStorage /*!*/ greaterThanStorage,
            UnaryOpStorage /*!*/ succStorage,
            BlockParam block, Range /*!*/ self, MutableString begin, MutableString end, int step)
        {
            CheckStep(step);
            object        result;
            MutableString item = begin;
            int           comp;

            var succSite = succStorage.GetCallSite("succ");

            while ((comp = Protocols.Compare(comparisonStorage, lessThanStorage, greaterThanStorage, item, end)) < 0)
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result))
                {
                    return(result);
                }

                for (int i = 0; i < step; i++)
                {
                    item = Protocols.CastToString(stringCast, succSite.Target(succSite, item));
                }

                if (item.Length > end.Length)
                {
                    return(self);
                }
            }

            if (comp == 0 && !self.ExcludeEnd)
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result))
                {
                    return(result);
                }
            }
            return(self);
        }
Exemple #11
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
                                  ));
            }
        }
Exemple #12
0
        public static IList Compact(UnaryOpStorage/*!*/ allocateStorage, IList/*!*/ self)
        {
            IList result = CreateResultArray(allocateStorage, self);

            foreach (object item in self) {
                if (item != null) {
                    result.Add(item);
                }
            }

            allocateStorage.Context.TaintObjectBy(result, self);

            return result;
        }
        public static Node ToYamlNode(UnaryOpStorage /*!*/ messageStorage, Exception /*!*/ self, [NotNull] RubyRepresenter /*!*/ rep)
        {
            var site = messageStorage.GetCallSite("message", 0);
            var map  = new Dictionary <object, object>();

            rep.AddYamlProperties(map, self, false);
            return(rep.Map(
                       new Dictionary <Node, Node> {
                { rep.Scalar(null, "message", ScalarQuotingStyle.None), rep.RepresentItem(site.Target(site, self)) }
            },
                       rep.GetTagUri(self),
                       map,
                       FlowStyle.Block
                       ));
        }
Exemple #14
0
        /// <summary>
        /// Step through a Range of objects that are not Numeric or String.
        /// </summary>
        private static object StepObject(
            BinaryOpStorage /*!*/ comparisonStorage,
            BinaryOpStorage /*!*/ lessThanStorage,
            BinaryOpStorage /*!*/ greaterThanStorage,
            BinaryOpStorage /*!*/ equalsStorage,
            UnaryOpStorage /*!*/ succStorage,
            BlockParam block, Range /*!*/ self, object begin, object end, int step)
        {
            CheckStep(lessThanStorage.GetCallSite("<"), equalsStorage.GetCallSite("=="), step);

            object item = begin, result;
            int    comp;

            var succSite = succStorage.GetCallSite("succ");

            while ((comp = Protocols.Compare(comparisonStorage, lessThanStorage, greaterThanStorage, item, end)) < 0)
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result))
                {
                    return(result);
                }

                for (int i = 0; i < step; ++i)
                {
                    item = succSite.Target(succSite, item);
                }
            }

            if (comp == 0 && !self.ExcludeEnd)
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result))
                {
                    return(result);
                }
            }
            return(self);
        }
Exemple #15
0
        public static void RaiseException(RespondToStorage /*!*/ respondToStorage, UnaryOpStorage /*!*/ storage0, BinaryOpStorage /*!*/ storage1,
                                          CallSiteStorage <Action <CallSite, Exception, RubyArray> > /*!*/ setBackTraceStorage,
                                          Thread /*!*/ self, object /*!*/ obj, [Optional] object arg, [Optional] RubyArray backtrace)
        {
            if (self == Thread.CurrentThread)
            {
                KernelOps.RaiseException(respondToStorage, storage0, storage1, setBackTraceStorage, self, obj, arg, backtrace);
                return;
            }

#if SILVERLIGHT
            throw new NotImplementedError("Thread#raise is not implemented on Silverlight");
#else
            Exception e = KernelOps.CreateExceptionToRaise(respondToStorage, storage0, storage1, setBackTraceStorage, obj, arg, backtrace);
            RaiseAsyncException(self, e);
#endif
        }
Exemple #16
0
        public static void RaiseException(RespondToStorage /*!*/ respondToStorage, UnaryOpStorage /*!*/ storage0, BinaryOpStorage /*!*/ storage1,
                                          CallSiteStorage <Action <CallSite, Exception, RubyArray> > /*!*/ setBackTraceStorage,
                                          Thread /*!*/ self, object /*!*/ obj, [Optional] object arg, [Optional] RubyArray backtrace)
        {
            if (self == Thread.CurrentThread)
            {
                KernelOps.RaiseException(respondToStorage, storage0, storage1, setBackTraceStorage, self, obj, arg, backtrace);
                return;
            }

#if FEATURE_EXCEPTION_STATE
            Exception e = KernelOps.CreateExceptionToRaise(respondToStorage, storage0, storage1, setBackTraceStorage, obj, arg, backtrace);
            RaiseAsyncException(self, e);
#else
            throw new NotImplementedError("Thread#raise not supported on this platform");
#endif
        }
Exemple #17
0
        public static StringIO /*!*/ Reopen(RespondToStorage /*!*/ respondToStorage, UnaryOpStorage /*!*/ toStringIoStorage,
                                            StringIO /*!*/ self, [NotNull] object /*!*/ other)
        {
            if (!Protocols.RespondTo(respondToStorage, other, "to_strio"))
            {
                throw RubyExceptions.CreateTypeConversionError(respondToStorage.Context.GetClassName(other), "StringIO");
            }

            var site  = toStringIoStorage.GetCallSite("to_strio", 0);
            var strio = site.Target(site, other) as StringIO;

            if (strio == null)
            {
                throw RubyExceptions.CreateTypeError("C#to_strio should return StringIO");
            }

            return(Reopen(respondToStorage.Context, self, strio));
        }
Exemple #18
0
        public static object InvokeOpenBlock(UnaryOpStorage /*!*/ closeStorage, BlockParam block, object obj)
        {
            object result = obj;

            if (!RubyOps.IsRetrySingleton(obj) && block != null)
            {
                try {
                    block.Yield(obj, out result);
                } finally {
                    try {
                        var site = closeStorage.GetCallSite("close");
                        site.Target(site, obj);
                    } catch (SystemException) {
                        // MRI: nop
                    }
                }
            }
            return(result);
        }
        public static Node /*!*/ ToYaml(UnaryOpStorage /*!*/ beginStorage, UnaryOpStorage /*!*/ endStorage, UnaryOpStorage /*!*/ exclStorage,
                                        Range /*!*/ self, [NotNull] RubyRepresenter /*!*/ rep)
        {
            var begin = beginStorage.GetCallSite("begin");
            var end   = endStorage.GetCallSite("end");

            var map = new Dictionary <object, object>();

            rep.AddYamlProperties(map, self, false);
            return(rep.Map(
                       new Dictionary <Node, Node> {
                { rep.Scalar(null, "begin", ScalarQuotingStyle.None), rep.RepresentItem(begin.Target(begin, self)) },
                { rep.Scalar(null, "end", ScalarQuotingStyle.None), rep.RepresentItem(end.Target(end, self)) },
                { rep.Scalar(null, "excl", ScalarQuotingStyle.None), rep.Scalar(self.ExcludeEnd) },
            },
                       rep.GetTagUri(self),
                       map,
                       FlowStyle.Block
                       ));
        }
Exemple #20
0
        public static int GetHashCode(UnaryOpStorage /*!*/ hashStorage, ConversionStorage <int> /*!*/ fixnumCast, IList /*!*/ self)
        {
            int hash = self.Count;

            using (IDisposable handle = _HashTracker.TrackObject(self)) {
                if (handle == null)
                {
                    // hashing of recursive array
                    return(0);
                }

                var hashSite  = hashStorage.GetCallSite("hash");
                var toIntSite = fixnumCast.GetSite(ConvertToFixnumAction.Make(fixnumCast.Context));
                foreach (object item in self)
                {
                    hash = (hash << 1) ^ toIntSite.Target(toIntSite, hashSite.Target(hashSite, item));
                }
            }
            return(hash);
        }
Exemple #21
0
        public static MutableString /*!*/ Inspect(UnaryOpStorage /*!*/ inspectStorage, ConversionStorage <MutableString> /*!*/ tosConversion, Exception /*!*/ self)
        {
            object message   = RubyExceptionData.GetInstance(self).Message;
            string className = inspectStorage.Context.GetClassDisplayName(self);

            MutableString result = MutableString.CreateMutable(inspectStorage.Context.GetIdentifierEncoding());

            result.Append("#<");
            result.Append(className);
            result.Append(": ");
            if (message != null)
            {
                result.Append(KernelOps.Inspect(inspectStorage, tosConversion, message));
            }
            else
            {
                result.Append(className);
            }
            result.Append('>');
            return(result);
        }
Exemple #22
0
        public static bool Equal(
            UnaryOpStorage /*!*/ messageStorage, UnaryOpStorage /*!*/ backtraceStorage, BinaryOpStorage /*!*/ stringEqlStorage,
            BinaryOpStorage /*!*/ arrayEqlStorage, RespondToStorage /*!*/ respondTo, Exception /*!*/ self, object /*!*/ other)
        {
            if (!Protocols.RespondTo(respondTo, other, "message") || !Protocols.RespondTo(respondTo, other, "backtrace"))
            {
                return(false);
            }

            var messageSite  = messageStorage.GetCallSite("message");
            var selfMessage  = messageSite.Target(messageSite, self);
            var otherMessage = messageSite.Target(messageSite, other);

            var backtraceSite  = backtraceStorage.GetCallSite("backtrace");
            var selfBacktrace  = backtraceSite.Target(backtraceSite, self) as System.Collections.IList;
            var otherBacktrace = backtraceSite.Target(backtraceSite, other);

            var stringEqlSite = stringEqlStorage.GetCallSite("==");

            return(true.Equals(stringEqlSite.Target(stringEqlSite, selfMessage, otherMessage)) &&
                   RubyArray.Equals(arrayEqlStorage, selfBacktrace, otherBacktrace));
        }
Exemple #23
0
        public static void PrintInspect(BinaryOpStorage/*!*/ writeStorage, UnaryOpStorage/*!*/ inspectStorage, ConversionStorage<MutableString>/*!*/ tosConversion, 
            object self, [NotNull]params object[]/*!*/ args) {

            var inspect = inspectStorage.GetCallSite("inspect");
            var inspectedArgs = new object[args.Length];
            for (int i = 0; i < args.Length; i++) {
                inspectedArgs[i] = inspect.Target(inspect, args[i]);
            }
            
            // no dynamic dispatch to "puts":
            RubyIOOps.Puts(writeStorage, tosConversion, writeStorage.Context.StandardOutput, inspectedArgs);
        }
Exemple #24
0
        public static object InducedFrom(UnaryOpStorage /*!*/ tofStorage, RubyModule /*!*/ self, [NotNull] BigInteger /*!*/ value)
        {
            var site = tofStorage.GetCallSite("to_f");

            return(site.Target(site, value));
        }
Exemple #25
0
        public static Node/*!*/ ToYamlNode(UnaryOpStorage/*!*/ isBinaryDataStorage, MutableString/*!*/ self, [NotNull]RubyRepresenter/*!*/ rep) {

            var site = isBinaryDataStorage.GetCallSite("is_binary_data?");
            if (RubyOps.IsTrue(site.Target(site, rep.Context, self))) {
                return rep.BaseCreateNode(self.ConvertToBytes());
            }

            string str = self.ConvertToString();
            RubyArray props = rep.ToYamlProperties(self);
            if (props.Count == 0) {
                MutableString taguri = rep.GetTagUri(self);

                char style = '\0';
                if (str.StartsWith(":")) {
                    style = '"';
                } else {
                    MutableString styleStr = rep.ToYamlStyle(self);
                    if (styleStr != null && styleStr.Length > 0) {
                        style = styleStr.GetChar(0);
                    }
                }

                return rep.Scalar(taguri != null ? taguri.ConvertToString() : "", str, style);
            }

            var map = new Dictionary<MutableString, object>() {
                { MutableString.Create("str"), str }
            };
            rep.AddYamlProperties(self, map, props);
            return rep.Map(self, map);
        }
Exemple #26
0
        public static object GetMessage(UnaryOpStorage /*!*/ stringReprStorage, Exception /*!*/ self)
        {
            var site = stringReprStorage.GetCallSite("to_s");

            return(site.Target(site, self));
        }
Exemple #27
0
        internal static Exception/*!*/ CreateExceptionToRaise(RespondToStorage/*!*/ respondToStorage, UnaryOpStorage/*!*/ storage0, BinaryOpStorage/*!*/ storage1,
            CallSiteStorage<Action<CallSite, Exception, RubyArray>>/*!*/ setBackTraceStorage, 
            object/*!*/ obj, object arg, RubyArray backtrace) {

            if (Protocols.RespondTo(respondToStorage, obj, "exception")) {
                Exception e = null;
                if (arg != Missing.Value) {
                    var site = storage1.GetCallSite("exception");
                    e = site.Target(site, obj, arg) as Exception;
                } else {
                    var site = storage0.GetCallSite("exception");
                    e = site.Target(site, obj) as Exception;
                }

                if (e != null) {
                    if (backtrace != null) {
                        var site = setBackTraceStorage.GetCallSite("set_backtrace", 1);
                        site.Target(site, e, backtrace);
                    }
                    return e;
                }
            }

            throw RubyExceptions.CreateTypeError("exception class/object expected");
        }
Exemple #28
0
        public static void RaiseException(RespondToStorage/*!*/ respondToStorage, UnaryOpStorage/*!*/ storage0, BinaryOpStorage/*!*/ storage1, 
            CallSiteStorage<Action<CallSite, Exception, RubyArray>>/*!*/ setBackTraceStorage, 
            Thread/*!*/ self, object/*!*/ obj, [Optional]object arg, [Optional]RubyArray backtrace) {

            if (self == Thread.CurrentThread) {
                KernelOps.RaiseException(respondToStorage, storage0, storage1, setBackTraceStorage, self, obj, arg, backtrace);
                return;
            }

#if SILVERLIGHT
            throw new NotImplementedError("Thread#raise is not implemented on Silverlight");
#else
            Exception e = KernelOps.CreateExceptionToRaise(respondToStorage, storage0, storage1, setBackTraceStorage, obj, arg, backtrace);
            RaiseAsyncException(self, e);
#endif
        }
Exemple #29
0
        public static object IsNonZero(UnaryOpStorage /*!*/ isZeroStorage, object self)
        {
            var isZero = isZeroStorage.GetCallSite("zero?");

            return(Protocols.IsTrue(isZero.Target(isZero, self)) ? null : self);
        }
Exemple #30
0
        public static object Each(
            ConversionStorage<MutableString>/*!*/ stringCast, 
            RespondToStorage/*!*/ respondToStorage,
            BinaryOpStorage/*!*/ comparisonStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            BinaryOpStorage/*!*/ equalsStorage,
            UnaryOpStorage/*!*/ succStorage,
            BlockParam block, Range/*!*/ self) {

            // We check that self.begin responds to "succ" even though some of the implementations don't use it.
            CheckBegin(respondToStorage, self.Begin);

            if (self.Begin is int && self.End is int) {
                return StepFixnum(block, self, (int)self.Begin, (int)self.End, 1);
            } else if (self.Begin is MutableString) {
                return StepString(stringCast, comparisonStorage, lessThanStorage, greaterThanStorage, succStorage,  
                    block, self, (MutableString)self.Begin, (MutableString)self.End, 1
                );
            } else {
                return StepObject(comparisonStorage, lessThanStorage, greaterThanStorage, equalsStorage, succStorage,  
                    block, self, self.Begin, self.End, 1
                );
            }
        }
Exemple #31
0
        public static RubyArray/*!*/ Intersection(UnaryOpStorage/*!*/ hashStorage, BinaryOpStorage/*!*/ eqlStorage, 
            IList/*!*/ self, [DefaultProtocol]IList/*!*/ other) {
            Dictionary<object, bool> items = new Dictionary<object, bool>(new EqualityComparer(hashStorage, eqlStorage));
            RubyArray result = new RubyArray();

            // first get the items in the RHS
            foreach (object item in other) {
                items[item] = true;
            }

            // now, go through the items in the LHS, adding ones that were also in the RHS
            // this ensures that we return the items in the correct order
            foreach (object item in self) {
                if (items.Remove(item)) {
                    result.Add(item);
                    if (items.Count == 0) {
                        break; // all done
                    }
                }
            }

            return result;
        }
Exemple #32
0
        public static object InducedFrom(UnaryOpStorage /*!*/ toiStorage, RubyClass /*!*/ self, double obj)
        {
            var site = toiStorage.GetCallSite("to_i");

            return(site.Target(site, obj));
        }
Exemple #33
0
        public static RubyArray/*!*/ Difference(UnaryOpStorage/*!*/ hashStorage, BinaryOpStorage/*!*/ eqlStorage, 
            IList/*!*/ self, [DefaultProtocol, NotNull]IList/*!*/ other) {

            RubyArray result = new RubyArray();
            
            // cost: (|self| + |other|) * (hash + eql) + dict
            var remove = new Dictionary<object, bool>(new EqualityComparer(hashStorage, eqlStorage));
            bool removeNull = false;
            foreach (var item in other) {
                if (item != null) {
                    remove[item] = true;
                } else {
                    removeNull = true;
                }
            }

            foreach (var item in self) {
                if (!(item != null ? remove.ContainsKey(item) : removeNull)) {
                    result.Add(item);
                }
            }

            return result;
        }
Exemple #34
0
        public static object ToInt(UnaryOpStorage /*!*/ toiStorage, object self)
        {
            var site = toiStorage.GetCallSite("to_i");

            return(site.Target(site, self));
        }
Exemple #35
0
 public static int Hash(UnaryOpStorage /*!*/ hashStorage, ConversionStorage <int> /*!*/ fixnumCast, RubyStruct /*!*/ self)
 {
     return(self.GetHashCode(hashStorage, fixnumCast));
 }
Exemple #36
0
 public static object ReadInputCharacter(UnaryOpStorage/*!*/ getcStorage, object self) {
     getcStorage.Context.ReportWarning("getc is obsolete; use STDIN.getc instead");
     var site = getcStorage.GetCallSite("getc", 0);
     return site.Target(site, getcStorage.Context.StandardInput);
 }
Exemple #37
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
                );
            }
        }
Exemple #38
0
        public static void RaiseException(RespondToStorage/*!*/ respondToStorage, UnaryOpStorage/*!*/ storage0, BinaryOpStorage/*!*/ storage1,
            CallSiteStorage<Action<CallSite, Exception, RubyArray>>/*!*/ setBackTraceStorage, 
            object self, object/*!*/ obj, [Optional]object arg, [Optional]RubyArray backtrace) {

            Exception exception = CreateExceptionToRaise(respondToStorage, storage0, storage1, setBackTraceStorage, obj, arg, backtrace);
#if DEBUG && !SILVERLIGHT
            if (RubyOptions.UseThreadAbortForSyncRaise) {
                RubyUtils.RaiseAsyncException(Thread.CurrentThread, exception);
            }
#endif
            // rethrow semantics, preserves the backtrace associated with the exception:
            throw exception;
        }
Exemple #39
0
        /// <summary>
        /// Step through a Range of Strings.
        /// </summary>
        /// <remarks>
        /// This method requires step to be a Fixnum.
        /// It uses a hybrid string comparison to prevent infinite loops and calls String#succ to get each item in the range.
        /// </remarks>
        private static object StepString(
            ConversionStorage<MutableString>/*!*/ stringCast, 
            BinaryOpStorage/*!*/ comparisonStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            UnaryOpStorage/*!*/ succStorage, 
            BlockParam block, Range/*!*/ self, MutableString begin, MutableString end, int step) {

            CheckStep(step);
            object result;
            MutableString item = begin;
            int comp;

            var succSite = succStorage.GetCallSite("succ");
            while ((comp = Protocols.Compare(comparisonStorage, lessThanStorage, greaterThanStorage, item, end)) < 0) {
                if (block == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result)) {
                    return result;
                }

                for (int i = 0; i < step; i++) {
                    item = Protocols.CastToString(stringCast, succSite.Target(succSite, item));
                }

                if (item.Length > end.Length) {
                    return self;
                }
            }

            if (comp == 0 && !self.ExcludeEnd) {
                if (block == null) {
                    throw RubyExceptions.NoBlockGiven();
                } 
                
                if (block.Yield(item, out result)) {
                    return result;
                }
            }
            return self;
        }
Exemple #40
0
 public static object InducedFrom(UnaryOpStorage/*!*/ toiStorage, RubyClass/*!*/ self, double obj) {
     var site = toiStorage.GetCallSite("to_i");
     return site.Target(site, obj);
 }
Exemple #41
0
        /// <summary>
        /// Step through a Range of objects that are not Numeric or String.
        /// </summary>
        private static object StepObject(
            BinaryOpStorage/*!*/ comparisonStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            BinaryOpStorage/*!*/ equalsStorage,
            UnaryOpStorage/*!*/ succStorage,
            BlockParam block, Range/*!*/ self, object begin, object end, int step) {

            CheckStep(lessThanStorage.GetCallSite("<"), equalsStorage.GetCallSite("=="), step);

            object item = begin, result;
            int comp;

            var succSite = succStorage.GetCallSite("succ");
            while ((comp = Protocols.Compare(comparisonStorage, lessThanStorage, greaterThanStorage, item, end)) < 0) {
                if (block == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result)) {
                    return result;
                }

                for (int i = 0; i < step; ++i) {
                    item = succSite.Target(succSite, item);
                }
            }

            if (comp == 0 && !self.ExcludeEnd) {
                if (block == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(item, out result)) {
                    return result;
                }
            }
            return self;
        }
Exemple #42
0
        public static object IsBinaryData(UnaryOpStorage/*!*/ isEmptyStorage, RubyContext/*!*/ context, MutableString/*!*/ self) {

            var site = isEmptyStorage.GetCallSite("empty?");
            if (RubyOps.IsTrue(site.Target(site, context, self))) {
                return null;
            }

            return ScriptingRuntimeHelpers.BooleanToObject((self.IsBinary ? self.IndexOf(0) : self.IndexOf('\0')) >= 0);
        }
        public static object UpTo(
            ConversionStorage<MutableString>/*!*/ stringCast, 
            RespondToStorage/*!*/ respondToStorage,
            BinaryOpStorage/*!*/ comparisonStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            BinaryOpStorage/*!*/ equalsStorage,
            UnaryOpStorage/*!*/ succStorage,
            BlockParam block, MutableString/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ endString) {

            RangeOps.Each(stringCast, respondToStorage, comparisonStorage, lessThanStorage, greaterThanStorage, equalsStorage, succStorage,  
                block, new Range(self, endString, false)
            );

            return self;
        }
Exemple #44
0
        public static Node/*!*/ ToYaml(UnaryOpStorage/*!*/ beginStorage, UnaryOpStorage/*!*/ endStorage, UnaryOpStorage/*!*/ exclStorage, 
            Range/*!*/ self, [NotNull]RubyRepresenter/*!*/ rep) {

            var begin = beginStorage.GetCallSite("begin");
            var end = endStorage.GetCallSite("end");

            var map = new Dictionary<MutableString, object>() {
                { MutableString.Create("begin"), begin.Target(begin, rep.Context, self) },
                { MutableString.Create("end"), end.Target(end, rep.Context, self) },
                { MutableString.Create("excl"), self.ExcludeEnd },
            };

            rep.AddYamlProperties(self, map);
            return rep.Map(self, map);
        }
Exemple #45
0
 public static object ToInt(UnaryOpStorage/*!*/ toiStorage, RubyContext/*!*/ context, object self) {
     var site = toiStorage.GetCallSite("to_i");
     return site.Target(site, context, self);
 }
Exemple #46
0
 public static object Wrap(BinaryOpStorage/*!*/ newStorage, UnaryOpStorage/*!*/ closeStorage,
     BlockParam block, RubyClass/*!*/ self, object io)
 {
     var newSite = newStorage.GetCallSite("new");
     GZipFile gzipFile = (GZipFile)newSite.Target(newSite, self, io);
     return gzipFile.Do(block);
 }
Exemple #47
0
 public int GetHashCode(UnaryOpStorage /*!*/ hashStorage, ConversionStorage <int> /*!*/ fixnumCast)
 {
     // hash is: struct's hash, plus data hashes
     return(StructInfo.GetHashCode() ^ RubyArray.GetHashCode(hashStorage, fixnumCast, _data));
 }
Exemple #48
0
 public static object IsNonZero(UnaryOpStorage/*!*/ isZeroStorage, RubyContext/*!*/ context, object self) {
     var isZero = isZeroStorage.GetCallSite("zero?");
     return Protocols.IsTrue(isZero.Target(isZero, context, self)) ? null : self;
 }
Exemple #49
0
        public static MutableString/*!*/ Inspect(UnaryOpStorage/*!*/ inspectStorage, ConversionStorage<MutableString>/*!*/ tosConversion,
            object self)
        {

            var context = tosConversion.Context;
            if (context.HasInstanceVariables(self))
            {
                return RubyUtils.InspectObject(inspectStorage, tosConversion, self);
            }
            else
            {
                var site = tosConversion.GetSite(ConvertToSAction.Make(context));
                return site.Target(site, self);
            }
        }
Exemple #50
0
        public static IList UniqueSelf(UnaryOpStorage/*!*/ hashStorage, BinaryOpStorage/*!*/ eqlStorage, IList/*!*/ self) {
            var seen = new Dictionary<object, bool>(new EqualityComparer(hashStorage, eqlStorage));
            bool nilSeen = false;
            bool modified = false;
            int i = 0;
            while (i < self.Count) {
                object key = self[i];
                if (key != null && !seen.ContainsKey(key)) {
                    seen.Add(key, true);
                    i++;
                } else if (key == null && !nilSeen) {
                    nilSeen = true;
                    i++;
                } else {
                    self.RemoveAt(i);
                    modified = true;
                }
            }

            return modified ? self : null;
        }
Exemple #51
0
        public static object InducedFrom(UnaryOpStorage /*!*/ tofStorage, RubyModule /*!*/ self, int value)
        {
            var site = tofStorage.GetCallSite("to_f");

            return(site.Target(site, ScriptingRuntimeHelpers.Int32ToObject(value)));
        }
Exemple #52
0
 public static int GetHashCode(UnaryOpStorage/*!*/ hashStorage, ConversionStorage<int>/*!*/ fixnumCast, IList/*!*/ self) {
     return RubyArray.GetHashCode(hashStorage, fixnumCast, self);
 }
Exemple #53
0
 public static object Reject(CallSiteStorage<EachSite>/*!*/ each, UnaryOpStorage/*!*/ allocate,
     BlockParam predicate, IList/*!*/ self) {
     return (predicate != null) ? RejectImpl(each, allocate, predicate, self) : new Enumerator((_, block) => RejectImpl(each, allocate, block, self));
 }
Exemple #54
0
        public static RubyArray/*!*/ Union(UnaryOpStorage/*!*/ hashStorage, BinaryOpStorage/*!*/ eqlStorage, 
            IList/*!*/ self, [DefaultProtocol]IList other) {
            var seen = new Dictionary<object, bool>(new EqualityComparer(hashStorage, eqlStorage));
            bool nilSeen = false;
            var result = new RubyArray();

            // Union merges the two arrays, removing duplicates
            AddUniqueItems(self, result, seen, ref nilSeen);

            AddUniqueItems(other, result, seen, ref nilSeen);

            return result;
        }
Exemple #55
0
        private static object RejectImpl(CallSiteStorage<EachSite>/*!*/ each, UnaryOpStorage/*!*/ allocate, 
            BlockParam/*!*/ predicate, IList/*!*/ self) {

            IList result = CreateResultArray(allocate, self);

            for (int i = 0; i < self.Count; i++) {
                object item = self[i];
                object blockResult;
                if (predicate.Yield(item, out blockResult)) {
                    return blockResult;
                }

                if (RubyOps.IsFalse(blockResult)) {
                    result.Add(item);
                }
            }

            return result;
        }
Exemple #56
0
 public static object InvokeOpenBlock(UnaryOpStorage/*!*/ closeStorage, BlockParam block, object obj) {
     object result = obj;
     if (!RubyOps.IsRetrySingleton(obj) && block != null) {
         try {
             block.Yield(obj, out result);
         } finally {
             try {
                 var site = closeStorage.GetCallSite("close");
                 site.Target(site, obj);                        
             } catch (SystemException) {
                 // MRI: nop
             }
         }
     }
     return result;
 }
Exemple #57
0
 public static int GetHashCode(UnaryOpStorage/*!*/ hashStorage, Range/*!*/ self) {
     // MRI: Ruby treatment of hash return value is inconsistent. 
     // No conversions happen here (unlike e.g. Array.hash).
     var hashSite = hashStorage.GetCallSite("hash");
     return unchecked(
         Protocols.ToHashCode(hashSite.Target(hashSite, self.Begin)) ^
         Protocols.ToHashCode(hashSite.Target(hashSite, self.End)) ^ 
         (self.ExcludeEnd ? 179425693 : 1794210891)
     );
 }