Esempio n. 1
0
        public void Initialize(BinaryOpStorage/*!*/ comparisonStorage,
            RubyContext/*!*/ context, object begin, object end, bool excludeEnd) {

            if (_initialized) {
                throw RubyExceptions.CreateNameError("`initialize' called twice");
            }

            // Range tests whether the items can be compared, and uses that to determine if the range is valid
            // Only a non-existent <=> method or a result of nil seems to trigger the exception.
            object compareResult;

            var site = comparisonStorage.GetCallSite("<=>");
            try {
                compareResult = site.Target(site, begin, end);
            } catch (Exception) {
                compareResult = null;
            }

            if (compareResult == null) {
                throw RubyExceptions.CreateArgumentError("bad value for range");
            }

            _begin = begin;
            _end = end;
            _excludeEnd = excludeEnd;
            _initialized = true;
        }
Esempio n. 2
0
        public static void Abort(BinaryOpStorage/*!*/ writeStorage, object/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ message)
        {
            var site = writeStorage.GetCallSite("write", 1);
            site.Target(site, writeStorage.Context.StandardErrorOutput, message);

            Exit(self, 1);
        }
Esempio n. 3
0
        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);
            }
        }
Esempio n. 4
0
 public static void PrintFormatted(
     StringFormatterSiteStorage/*!*/ storage,
     ConversionStorage<MutableString>/*!*/ stringCast,
     BinaryOpStorage/*!*/ writeStorage,
     object/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ format, params object[]/*!*/ args)
 {
     KernelOps.PrintFormatted(storage, stringCast, writeStorage, null, self, format, args);
 }
Esempio n. 5
0
        public static bool Greater(
            BinaryOpStorage/*!*/ compareStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            object self, object other) {

            return Compare(compareStorage, lessThanStorage, greaterThanStorage, self, other).GetValueOrDefault(0) > 0;
        }
Esempio n. 6
0
        public static bool Equal(BinaryOpStorage/*!*/ equals, decimal self, object other) {
            if (other is decimal) {
                return self == (decimal)other;
            }

            // Call == on the right operand like Float#== does
            return Protocols.IsEqual(equals, other, self);
        }
Esempio n. 7
0
        public static bool Between(
            BinaryOpStorage/*!*/ compareStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            object self, object min, object max) {

            return !Less(compareStorage, lessThanStorage, greaterThanStorage, self, min)
                && !Greater(compareStorage, lessThanStorage, greaterThanStorage, self, max);
        }
Esempio n. 8
0
 public static RubyArray/*!*/ ToYamlProperties(
     BinaryOpStorage/*!*/ comparisonStorage,
     BinaryOpStorage/*!*/ lessThanStorage,
     BinaryOpStorage/*!*/ greaterThanStorage,
     RubyContext/*!*/ context, object self) {
     return ArrayOps.SortInPlace(comparisonStorage, lessThanStorage, greaterThanStorage, context, 
         null, KernelOps.InstanceVariables(context, self)
     );
 }
Esempio n. 9
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;
 }
Esempio n. 10
0
 public static Object Scan(ConversionStorage<MutableString>/*!*/ toMutableStringStorage, RespondToStorage/*!*/ respondsTo, 
     BinaryOpStorage/*!*/ readIOStorage, BlockParam block, RubyModule/*!*/ self, Object/*!*/ source, Hash/*!*/ options)
 {
     Object elementContent;
     if (!self.TryGetConstant(null, "ElementContent", out elementContent) && !(elementContent is Hash)) {
         throw new Exception("Hpricot::ElementContent is missing or it is not an Hash");
     }
     var scanner = new HpricotScanner(toMutableStringStorage, readIOStorage, block);
     return scanner.Scan(source, options, elementContent as Hash);
 }
Esempio n. 11
0
        public static MutableString/*!*/ Putc(BinaryOpStorage/*!*/ writeStorage, object self, [NotNull]MutableString/*!*/ val) {
            if (val.IsEmpty) {
                throw RubyExceptions.CreateTypeError("can't convert String into Integer");
            }

            // writes a single byte into the output stream:
            var c = MutableString.CreateBinary(val.GetBinarySlice(0, 1));
            Protocols.Write(writeStorage, self, c);
            return val;
        }
Esempio n. 12
0
        public static object DefineFinalizer(RespondToStorage/*!*/ respondTo, BinaryOpStorage/*!*/ call, RubyModule/*!*/ self, object obj, object finalizer)
        {
            if (!Protocols.RespondTo(respondTo, finalizer, "call")) {
                throw RubyExceptions.CreateArgumentError("finalizer should be callable (respond to :call)");
            }

            respondTo.Context.SetInstanceVariable(obj, FinalizerInvoker.InstanceVariableName, new FinalizerInvoker(call.GetCallSite("call"), finalizer));
            RubyArray result = new RubyArray(2);
            result.Add(0);
            result.Add(finalizer);
            return result;
        }
Esempio n. 13
0
        /// <summary>
        /// Try to compare the lhs and rhs. Throws and exception if comparison returns null. Returns null on failure, -1/0/+1 otherwise.
        /// </summary>
        private static int? Compare(BinaryOpStorage/*!*/ compareStorage, BinaryOpStorage/*!*/ lessThanStorage, BinaryOpStorage/*!*/ greaterThanStorage,
            object lhs, object rhs) {

            // calls method_missing, doesn't catch any exception:
            var compare = compareStorage.GetCallSite("<=>");
            object compareResult = compare.Target(compare, lhs, rhs);
            
            if (compareResult != null) {
                return Protocols.ConvertCompareResult(lessThanStorage, greaterThanStorage, compareResult);
            } else {
                throw RubyExceptions.MakeComparisonError(lessThanStorage.Context, lhs, rhs);
            }
        }
Esempio n. 14
0
        public static object Contains(CallSiteStorage<EachSite>/*!*/ each, BinaryOpStorage/*!*/ equals, object self, object value)
        {
            object result = ScriptingRuntimeHelpers.BooleanToObject(false);

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (Protocols.IsEqual(equals, item, value)) {
                    result = ScriptingRuntimeHelpers.BooleanToObject(true);
                    return selfBlock.Break(result);
                }
                return null;
            }));

            return result;
        }
Esempio n. 15
0
        public static int Count(CallSiteStorage<EachSite>/*!*/ each, BinaryOpStorage/*!*/ equals, BlockParam comparer, object self, object value)
        {
            if (comparer != null) {
                each.Context.ReportWarning("given block not used");
            }

            int result = 0;
            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (Protocols.IsEqual(equals, item, value)) {
                    result++;
                }
                return null;
            }));
            return result;
        }
Esempio n. 16
0
        public static bool Equal(BinaryOpStorage/*!*/ compareStorage, object self, object other)
        {
            if (self == other) {
                return true;
            }

            // calls method_missing:
            var compare = compareStorage.GetCallSite("<=>");

            object compareResult;
            try {
                compareResult = compare.Target(compare, self, other);
            } catch (SystemException) {
                // catches StandardError (like rescue)
                return false;
            }

            return compareResult is int && (int)compareResult == 0;
        }
Esempio n. 17
0
 public static object GreaterThanOrEqual(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ comparisonStorage,
                                         BigDecimal /*!*/ self, object other)
 {
     return(Protocols.TryCoerceAndApply(coercionStorage, comparisonStorage, ">=", self, other));
 }
Esempio n. 18
0
 public static object /*!*/ Output(BinaryOpStorage /*!*/ writeStorage, object self, object value)
 {
     Protocols.Write(writeStorage, self, value);
     return(self);
 }
Esempio n. 19
0
 public static object Quotient(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ binaryOpSite,
                               BigDecimal /*!*/ self, object other)
 {
     return(Protocols.CoerceAndApply(coercionStorage, binaryOpSite, "quo", self, other));
 }
Esempio n. 20
0
 public static bool LessThan(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ comparisonStorage, double self, object other)
 {
     return(Protocols.CoerceAndRelate(coercionStorage, comparisonStorage, "<", self, other));
 }
Esempio n. 21
0
 // Convience function for constructing from C#, calls initialize
 public Range(BinaryOpStorage /*!*/ comparisonStorage,
              RubyContext /*!*/ context, object begin, object end, bool excludeEnd)
 {
     Initialize(comparisonStorage, context, begin, end, excludeEnd);
 }
Esempio n. 22
0
 public static object Remainder(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ binaryOpSite, object /*!*/ self, object other)
 {
     return(Protocols.CoerceAndApply(coercionStorage, binaryOpSite, "remainder", self, other));
 }
Esempio n. 23
0
        public static RubyArray Grep(CallSiteStorage<EachSite>/*!*/ each, BinaryOpStorage/*!*/ caseEquals, 
            BlockParam action, object self, object pattern) {

            RubyArray result = new RubyArray();
            var site = caseEquals.GetCallSite("===");

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (RubyOps.IsTrue(site.Target(site, pattern, item))) {
                    if (action != null) {
                        if (action.Yield(item, out item)) {
                            return item;
                        }
                    }
                    result.Add(item);
                }
                return null;
            }));

            return result;
        }
Esempio n. 24
0
 public static object Power(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ binaryOpSite, object /*!*/ self, object exponent)
 {
     return(Protocols.CoerceAndApply(coercionStorage, binaryOpSite, "**", self, exponent));
 }
Esempio n. 25
0
 public static object Modulo(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ binaryOpSite, object /*!*/ self, object other)
 {
     return(Protocols.CoerceAndApply(coercionStorage, binaryOpSite, "modulo", self, other));
 }
Esempio n. 26
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));
        }
Esempio n. 27
0
 public static object Add(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ opStorage,
                          BigDecimal /*!*/ self, object other, [DefaultProtocol] int n)
 {
     return(Protocols.CoerceAndApply(coercionStorage, opStorage, "+", self, other));
 }
Esempio n. 28
0
 public static object LessThan(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ comparisonStorage,
                               BigDecimal /*!*/ self, object other)
 {
     return(Protocols.TryCoerceAndApply(coercionStorage, comparisonStorage, "<", self, other));
 }
Esempio n. 29
0
 public static bool Equals(RespondToStorage/*!*/ respondToStorage, BinaryOpStorage/*!*/ equalsStorage, string/*!*/ self, object other) {
     return MutableStringOps.Equals(respondToStorage, equalsStorage, self, other);
 }
Esempio n. 30
0
 public static bool Equal(BinaryOpStorage /*!*/ equals, BigInteger /*!*/ self, object other)
 {
     // If we can't convert then swap self and other and try again.
     return(Protocols.IsEqual(equals, other, self));
 }
Esempio n. 31
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;
        }
Esempio n. 32
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
        }
Esempio n. 33
0
 public static object Multiply(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ binaryOpSite,
                               BigDecimal /*!*/ self, object other, [DefaultProtocol] int n)
 {
     // TODO: converts result to BigDecimal
     return(Protocols.CoerceAndApply(coercionStorage, binaryOpSite, "mult", self, other));
 }
Esempio n. 34
0
 public static Range /*!*/ CreateRange(BinaryOpStorage /*!*/ comparisonStorage,
                                       RubyClass /*!*/ self, object begin, object end, [Optional] bool excludeEnd)
 {
     return(new Range(comparisonStorage, self.Context, begin, end, excludeEnd));
 }
Esempio n. 35
0
 public static bool GreaterThanOrEqual(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ comparisonStorage, double self, object other)
 {
     return(Protocols.CoerceAndRelate(coercionStorage, comparisonStorage, ">=", self, other));
 }
Esempio n. 36
0
 public static Range /*!*/ Reinitialize(BinaryOpStorage /*!*/ comparisonStorage,
                                        RubyContext /*!*/ context, Range /*!*/ self, object begin, object end, [Optional] bool excludeEnd)
 {
     self.Initialize(comparisonStorage, context, begin, end, excludeEnd);
     return(self);
 }
Esempio n. 37
0
 public static void PutsEmptyLine(BinaryOpStorage /*!*/ writeStorage, object self)
 {
     Protocols.Write(writeStorage, self, MutableString.CreateAscii("\n"));
 }
Esempio n. 38
0
        public static object Modulo(BinaryOpStorage /*!*/ moduloStorage, RubyContext /*!*/ context, BigDecimal /*!*/ self, double other)
        {
            var modulo = moduloStorage.GetCallSite("modulo");

            return(modulo.Target(modulo, BigDecimal.ToFloat(GetConfig(context), self), other));
        }
Esempio n. 39
0
 public static object Times(BinaryOpStorage /*!*/ lessThanStorage, BinaryOpStorage /*!*/ addStorage, BlockParam block, object /*!*/ self)
 {
     return((block != null) ? TimesImpl(lessThanStorage, addStorage, block, self) :
            new Enumerator((_, innerBlock) => TimesImpl(lessThanStorage, addStorage, innerBlock, self)));
 }
Esempio n. 40
0
        /// <summary>
        /// Applies given operator on coerced values and returns the result.
        /// </summary>
        /// <exception cref="TypeError">
        /// "coerce" method is not defined, throws a subclass of SystemException, or returns something other than a pair of objects.
        /// </exception>
        public static object CoerceAndApply(BinaryOpStorage/*!*/ coercionStorage, BinaryOpStorage/*!*/ binaryOpStorage, 
            string/*!*/ binaryOp, object self, object other) {

            object result;
            if (TryCoerceAndApply(coercionStorage, binaryOpStorage, binaryOp, self, other, out result)) {
                return result;
            }

            throw RubyExceptions.MakeCoercionError(coercionStorage.Context, other, self);
        }
Esempio n. 41
0
        public static object Pred(BinaryOpStorage /*!*/ subStorage, object /*!*/ self)
        {
            var site = subStorage.GetCallSite("-");

            return(site.Target(site, self, ClrInteger.One));
        }
Esempio n. 42
0
        private static bool TryCoerceAndApply(
            BinaryOpStorage/*!*/ coercionStorage,
            BinaryOpStorage/*!*/ binaryOpStorage, string/*!*/ binaryOp,
            object self, object other, out object result) {

            var coerce = coercionStorage.GetCallSite("coerce", new RubyCallSignature(1, RubyCallFlags.HasImplicitSelf));

            IList coercedValues;

            try {
                // Swap self and other around to do the coercion.
                coercedValues = coerce.Target(coerce, other, self) as IList;
            } catch (SystemException) { 
                // catches StandardError (like rescue)
                result = null;
                return false;
            }

            if (coercedValues != null && coercedValues.Count == 2) {
                var opSite = binaryOpStorage.GetCallSite(binaryOp);
                result = opSite.Target(opSite, coercedValues[0], coercedValues[1]);
                return true;
            }

            result = null;
            return false;
        }
Esempio n. 43
0
 public static object DownTo(BinaryOpStorage /*!*/ lessThanStorage, BinaryOpStorage /*!*/ subtractStorage,
                             BlockParam block, object /*!*/ self, object other)
 {
     return((block != null) ? DownToImpl(lessThanStorage, subtractStorage, block, self, other) :
            new Enumerator((_, innerBlock) => DownToImpl(lessThanStorage, subtractStorage, innerBlock, self, other)));
 }
Esempio n. 44
0
 public static object DivMod(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ binaryOpSite,
                             RubyContext /*!*/ context, BigDecimal /*!*/ self, object other)
 {
     return(Protocols.CoerceAndApply(coercionStorage, binaryOpSite, "divmod", self, other));
 }
Esempio n. 45
0
 public static object GetMinimum(
     CallSiteStorage<EachSite>/*!*/ each, 
     BinaryOpStorage/*!*/ compareStorage,
     BinaryOpStorage/*!*/ lessThanStorage,
     BinaryOpStorage/*!*/ greaterThanStorage,
     BlockParam comparer, object self) {
     return GetExtreme(each, compareStorage, lessThanStorage, greaterThanStorage, comparer, self, 1/*look for min*/);
 }
Esempio n. 46
0
        public static object Sort(
            CallSiteStorage<EachSite>/*!*/ each, 
            BinaryOpStorage/*!*/ comparisonStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            BlockParam keySelector, object self) {

            return ArrayOps.SortInPlace(comparisonStorage, lessThanStorage, greaterThanStorage, keySelector, ToArray(each, self));
        }
Esempio n. 47
0
        /// <summary>
        /// Applies given operator on coerced values and returns the result.
        /// </summary>
        /// <exception cref="TypeError">
        /// "coerce" method is not defined, throws a subclass of SystemException, or returns something other than a pair of objects.
        /// </exception>
        public static object TryCoerceAndApply(
            BinaryOpStorage/*!*/ coercionStorage,
            BinaryOpStorage/*!*/ binaryOpStorage, string/*!*/ binaryOp,
            object self, object other) {

            if (other == null) {
                return null;
            }

            object result;
            if (TryCoerceAndApply(coercionStorage, binaryOpStorage, binaryOp, self, other, out result)) {
                if (result != null) {
                    return RubyOps.IsTrue(result);
                }
            }
            return null;
        }
Esempio n. 48
0
 public static object UpTo(BinaryOpStorage /*!*/ greaterThanStorage, BinaryOpStorage /*!*/ addStorage,
                           BlockParam block, object /*!*/ self, object other)
 {
     return((block != null) ? UpToImpl(greaterThanStorage, addStorage, block, self, other) :
            new Enumerator((_, innerBlock) => UpToImpl(greaterThanStorage, addStorage, innerBlock, self, other)));
 }
Esempio n. 49
0
 public static object Compare(BinaryOpStorage/*!*/ comparisonStorage, RespondToStorage/*!*/ respondToStorage, string/*!*/ self, object other) {
     return MutableStringOps.Compare(comparisonStorage, respondToStorage, self, other);
 }
Esempio n. 50
0
 public static object DivMod(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ binaryOpSite, double self, object other)
 {
     return(Protocols.CoerceAndApply(coercionStorage, binaryOpSite, "divmod", self, other));
 }
Esempio n. 51
0
        public static object CopyStream(
            ConversionStorage<MutableString>/*!*/ toPath, ConversionStorage<int>/*!*/ toInt, RespondToStorage/*!*/ respondTo,
            BinaryOpStorage/*!*/ writeStorage, CallSiteStorage<Func<CallSite, object, object, object, object>>/*!*/ readStorage,
            RubyClass/*!*/ self, object src, object dst, [DefaultParameterValue(-1)]int count, [DefaultParameterValue(-1)]int src_offset) {

            if (count < -1) {
                throw RubyExceptions.CreateArgumentError("count should be >= -1");
            }

            if (src_offset < -1) {
                throw RubyExceptions.CreateArgumentError("src_offset should be >= -1");
            }

            RubyIO srcIO = src as RubyIO;
            RubyIO dstIO = dst as RubyIO;
            Stream srcStream = null, dstStream = null;
            var context = toPath.Context;
            CallSite<Func<CallSite, object, object, object>> writeSite = null;
            CallSite<Func<CallSite, object, object, object, object>> readSite = null;

            try {
                if (srcIO == null || dstIO == null) {
                    var toPathSite = toPath.GetSite(TryConvertToPathAction.Make(toPath.Context));
                    var srcPath = toPathSite.Target(toPathSite, src);
                    if (srcPath != null) {
                        srcStream = new FileStream(context.DecodePath(srcPath), FileMode.Open, FileAccess.Read);
                    } else {
                        readSite = readStorage.GetCallSite("read", 2);
                    }

                    var dstPath = toPathSite.Target(toPathSite, dst);
                    if (dstPath != null) {
                        dstStream = new FileStream(context.DecodePath(dstPath), FileMode.Truncate);
                    } else {
                        writeSite = writeStorage.GetCallSite("write", 1);
                    }
                } else {
                    srcStream = srcIO.GetReadableStream();
                    dstStream = dstIO.GetWritableStream();
                }

                if (src_offset != -1) {
                    if (srcStream == null) {
                        throw RubyExceptions.CreateArgumentError("cannot specify src_offset for non-IO");
                    }
                    srcStream.Seek(src_offset, SeekOrigin.Current);
                }

                MutableString userBuffer = null;
                byte[] buffer = null;

                long bytesCopied = 0;
                long remaining = (count < 0) ? Int64.MaxValue : count;
                int minBufferSize = 16 * 1024;
                
                if (srcStream != null) {
                    buffer = new byte[Math.Min(minBufferSize, remaining)];
                }

                while (remaining > 0) {
                    int bytesRead;
                    int chunkSize = (int)Math.Min(minBufferSize, remaining);
                    if (srcStream != null) {
                        userBuffer = null;
                        bytesRead = srcStream.Read(buffer, 0, chunkSize);
                    } else {
                        userBuffer = MutableString.CreateBinary();
                        bytesRead = Protocols.CastToFixnum(toInt, readSite.Target(readSite, src, chunkSize, userBuffer));
                    }
                    
                    if (bytesRead <= 0) {
                        break;
                    }

                    if (dstStream != null) {
                        if (userBuffer != null) {
                            dstStream.Write(userBuffer, 0, bytesRead);
                        } else {
                            dstStream.Write(buffer, 0, bytesRead);
                        }
                    } else {
                        if (userBuffer == null) {
                            userBuffer = MutableString.CreateBinary(bytesRead).Append(buffer, 0, bytesRead);
                        } else {
                            userBuffer.SetByteCount(bytesRead);
                        }
                        writeSite.Target(writeSite, dst, userBuffer);
                    }
                    bytesCopied += bytesRead;
                    remaining -= bytesRead;
                }
                return Protocols.Normalize(bytesCopied);

            } finally {
                if (srcStream != null) {
                    srcStream.Close();
                }
                if (dstStream != null) {
                    dstStream.Close();
                }
            }
        }
Esempio n. 52
0
 public static bool Equal(BinaryOpStorage /*!*/ equals, double self, object other)
 {
     // Call == on the right operand like Float#== does
     return(Protocols.IsEqual(equals, other, self));
 }
Esempio n. 53
0
 public static int SysWrite(BinaryOpStorage/*!*/ writeStorage, ConversionStorage<MutableString>/*!*/ tosConversion,
     RubyContext/*!*/ context, RubyIO/*!*/ self, object obj) {
     return SysWrite(writeStorage, tosConversion, context, self, Protocols.ConvertToString(tosConversion, obj));
 }
Esempio n. 54
0
 public static object Compare(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ comparisonStorage, double self, object other)
 {
     return(Protocols.CoerceAndCompare(coercionStorage, comparisonStorage, self, other));
 }
Esempio n. 55
0
        public static bool Contains(CallSiteStorage<EachSite>/*!*/ each, BinaryOpStorage/*!*/ equals, object self, object value) {
            bool result = false;

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (Protocols.IsEqual(equals, item, value)) {
                    result = true;
                    return selfBlock.Break(result);
                }
                return null;
            }));

            return result;
        }
Esempio n. 56
0
 public static object Subtract(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ binaryOpSite,
                               BigDecimal /*!*/ self, object other, [DefaultProtocol] int n)
 {
     return(Protocols.CoerceAndApply(coercionStorage, binaryOpSite, "sub", self, other));
 }
Esempio n. 57
0
        private static object GetExtreme(
            CallSiteStorage<EachSite>/*!*/ each, 
            BinaryOpStorage/*!*/ compareStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            BlockParam comparer, object self, int comparisonValue) {

            bool firstItem = true;
            object result = null;

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                // Check for first element
                if (firstItem) {
                    result = item;
                    firstItem = false;
                    return null;
                }

                int compareResult;
                if (comparer != null) {
                    object blockResult;
                    if (comparer.Yield(result, item, out blockResult)) {
                        return blockResult;
                    }

                    if (blockResult == null) {
                        throw RubyExceptions.MakeComparisonError(selfBlock.RubyContext, result, item);
                    }

                    compareResult = Protocols.ConvertCompareResult(lessThanStorage, greaterThanStorage, blockResult);
                } else {
                    compareResult = Protocols.Compare(compareStorage, lessThanStorage, greaterThanStorage, result, item);
                }

                // Check if we have found the new minimum or maximum (-1 to select max, 1 to select min)
                if (compareResult == comparisonValue) {
                    result = item;
                }

                return null;
            }));
            return result;
        }
Esempio n. 58
0
        public static object Next(BinaryOpStorage /*!*/ addStorage, object /*!*/ self)
        {
            var site = addStorage.GetCallSite("+");

            return(site.Target(site, self, ClrInteger.One));
        }
Esempio n. 59
0
        public static RubyArray/*!*/ SortBy(
            CallSiteStorage<EachSite>/*!*/ each, 
            BinaryOpStorage/*!*/ comparisonStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            BlockParam keySelector, object self) {

            // collect key, value pairs
            List<KeyValuePair<object, object>> keyValuePairs = new List<KeyValuePair<object, object>>();

            // Collect the key, value pairs
            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (keySelector == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                object key;
                if (keySelector.Yield(item, out key)) {
                    return key;
                }

                keyValuePairs.Add(new KeyValuePair<object, object>(key, item));
                return null;
            }));

            // sort by keys
            keyValuePairs.Sort(delegate(KeyValuePair<object, object> x, KeyValuePair<object, object> y) {
                return Protocols.Compare(comparisonStorage, lessThanStorage, greaterThanStorage, x.Key, y.Key);
            });

            // return values
            RubyArray result = new RubyArray(keyValuePairs.Count);
            foreach (KeyValuePair<object, object> pair in keyValuePairs) {
                result.Add(pair.Value);
            }

            return result;
        }
Esempio n. 60
0
 public static object Subtract(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ binaryOpSite, double self, object other)
 {
     return(Protocols.CoerceAndApply(coercionStorage, binaryOpSite, "-", self, other));
 }