Пример #1
0
 /// <summary>
 /// Check that the object responds to "succ".
 /// </summary>
 private static void CheckBegin(RespondToStorage /*!*/ respondToStorage, object begin)
 {
     if (!Protocols.RespondTo(respondToStorage, begin, "succ"))
     {
         throw RubyExceptions.CreateTypeError("can't iterate from {0}", respondToStorage.Context.GetClassDisplayName(begin));
     }
 }
Пример #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
                                  ));
            }
        }
Пример #3
0
        // TODO: 1.9 only
        // bytes -> Enumerable::Enumerator
        // lines -> Enumerable::Enumerator

        public static IOWrapper /*!*/ CreateIOWrapper(RespondToStorage /*!*/ respondToStorage, object io, FileAccess access)
        {
            bool canRead, canWrite, canSeek, canFlush, canBeClosed;

            if (access == FileAccess.Read || access == FileAccess.ReadWrite)
            {
                canRead = Protocols.RespondTo(respondToStorage, io, "read");
            }
            else
            {
                canRead = false;
            }

            if (access == FileAccess.Write || access == FileAccess.ReadWrite)
            {
                canWrite = Protocols.RespondTo(respondToStorage, io, "write");
            }
            else
            {
                canWrite = false;
            }

            canSeek     = Protocols.RespondTo(respondToStorage, io, "seek") && Protocols.RespondTo(respondToStorage, io, "tell");
            canFlush    = Protocols.RespondTo(respondToStorage, io, "flush");
            canBeClosed = Protocols.RespondTo(respondToStorage, io, "close");

            return(new IOWrapper(respondToStorage.Context, io, canRead, canWrite, canSeek, canFlush, canBeClosed));
        }
Пример #4
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);
 }
Пример #5
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;
        }
Пример #6
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
                                  ));
            }
        }
Пример #7
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);
        }
Пример #8
0
        public static object Parse(ConversionStorage <MutableString> /*!*/ toStr, RespondToStorage /*!*/ respondTo, RubyModule /*!*/ self, object io)
        {
            using (Stream stream = GetStream(toStr, respondTo, io)) {
                foreach (Node obj in MakeComposer(self.Context, stream))
                {
                    // TODO: the enumerator shouldn't return null:
                    if (obj == null)
                    {
                        break;
                    }

                    return(obj);
                }
            }
            return(ScriptingRuntimeHelpers.False);
        }
Пример #9
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
        }
Пример #10
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
        }
Пример #11
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));
        }
Пример #12
0
        public static object Load(ReaderSites /*!*/ sites, RespondToStorage /*!*/ respondToStorage,
                                  RubyScope /*!*/ scope, RubyModule /*!*/ self, object source, [Optional] Proc proc)
        {
            Stream stream = null;

            if (source != null)
            {
                stream = RubyIOOps.CreateIOWrapper(respondToStorage, source, FileAccess.Read);
            }
            if (stream == null || !stream.CanRead)
            {
                throw RubyExceptions.CreateTypeError("instance of IO needed");
            }
            BinaryReader  reader = new BinaryReader(stream);
            MarshalReader loader = new MarshalReader(sites, reader, scope.GlobalScope, proc);

            return(loader.Load());
        }
Пример #13
0
        public IOWrapper(RubyContext/*!*/ context, object io, bool canRead, bool canWrite, bool canSeek, bool canFlush, bool canClose, int bufferSize)
        {
            Assert.NotNull(context);

            _context = context;
            _respondToStorage = new RespondToStorage(_context);

            _io = io;
            _canRead = canRead;
            _canWrite = canWrite;
            _canSeek = canSeek;
            _canFlush = canFlush;
            _canClose = canClose;
            _buffer = new byte[bufferSize];
            _writePos = 0;
            _readPos = 0;
            _readLen = 0;
        }
Пример #14
0
 public static object Load(ConversionStorage <MutableString> /*!*/ toStr, RespondToStorage /*!*/ respondTo,
                           RubyScope /*!*/ scope, RubyModule /*!*/ self, object io)
 {
     try {
         foreach (object obj in MakeConstructor(scope.GlobalScope, GetStream(toStr, respondTo, io)))
         {
             return(obj);
         }
         return(null);
     } catch (Exception e) {
         throw RubyExceptions.CreateArgumentError(e, e.Message);
     } finally {
         RubyIO rio = io as RubyIO;
         if (rio != null)
         {
             rio.Close();
         }
     }
 }
Пример #15
0
        public static object Dump(WriterSites /*!*/ sites, RespondToStorage /*!*/ respondToStorage,
                                  RubyModule /*!*/ self, object obj, object io, [Optional] int?limit)
        {
            Stream stream = null;

            if (io != null)
            {
                stream = RubyIOOps.CreateIOWrapper(respondToStorage, io, FileAccess.Write);
            }
            if (stream == null || !stream.CanWrite)
            {
                throw RubyExceptions.CreateTypeError("instance of IO needed");
            }

            BinaryWriter  writer = new BinaryWriter(stream);
            MarshalWriter dumper = new MarshalWriter(sites, writer, self.Context, limit);

            dumper.Dump(obj);
            return(io);
        }
Пример #16
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));
        }
Пример #17
0
        public static object Load(ReaderSites/*!*/ sites, RespondToStorage/*!*/ respondToStorage, 
            RubyScope/*!*/ scope, RubyModule/*!*/ self, object source, [Optional]Proc proc) {

            Stream stream = null;
            if (source != null) {
                stream = RubyIOOps.CreateIOWrapper(respondToStorage, source, FileAccess.Read);
            }
            if (stream == null || !stream.CanRead) {
                throw RubyExceptions.CreateTypeError("instance of IO needed");
            }
            BinaryReader reader = new BinaryReader(stream);
            MarshalReader loader = new MarshalReader(sites, reader, scope.GlobalScope, proc);
            return loader.Load();
        }
Пример #18
0
        public static object Dump(WriterSites/*!*/ sites, RespondToStorage/*!*/ respondToStorage, 
            RubyModule/*!*/ self, object obj, object io, [Optional]int? limit) {
            Stream stream = null;
            if (io != null) {
                stream = RubyIOOps.CreateIOWrapper(respondToStorage, io, FileAccess.Write);
            }
            if (stream == null || !stream.CanWrite) {
                throw RubyExceptions.CreateTypeError("instance of IO needed");
            }

            BinaryWriter writer = new BinaryWriter(stream);
            MarshalWriter dumper = new MarshalWriter(sites, writer, self.Context, limit);
            dumper.Dump(obj);
            return io;
        }
Пример #19
0
 public static bool RespondTo(RespondToStorage/*!*/ respondToStorage, object target, string/*!*/ methodName) {
     var site = respondToStorage.GetCallSite();
     return IsTrue(site.Target(site, target, SymbolTable.StringToId(methodName)));
 }
Пример #20
0
        public static object ParseDocuments(ConversionStorage <MutableString> /*!*/ toStr, RespondToStorage /*!*/ respondTo,
                                            BlockParam block, RubyModule /*!*/ self, object io)
        {
            foreach (Node obj in MakeComposer(self.Context, GetStream(toStr, respondTo, io)))
            {
                // TODO: the enumerator shouldn't return null:
                if (obj == null)
                {
                    return(null);
                }

                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

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

            return(null);
        }
Пример #21
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");
        }
Пример #22
0
 public static object Compare(BinaryOpStorage /*!*/ comparisonStorage, RespondToStorage /*!*/ respondToStorage, ClrName /*!*/ self, object other)
 {
     return(MutableStringOps.Compare(comparisonStorage, respondToStorage, self.MangledName, other));
 }
Пример #23
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
                );
            }
        }
Пример #24
0
 public static bool Equals(RespondToStorage/*!*/ respondToStorage, BinaryOpStorage/*!*/ equalsStorage, string/*!*/ self, object other) {
     return MutableStringOps.Equals(respondToStorage, equalsStorage, self, other);
 }
Пример #25
0
 public static bool RespondTo(RespondToStorage/*!*/ respondToStorage, object target, string/*!*/ methodName) {
     var site = respondToStorage.GetCallSite();
     return IsTrue(site.Target(site, target, respondToStorage.Context.EncodeIdentifier(methodName)));
 }
Пример #26
0
        public static bool Equals(RespondToStorage/*!*/ respondToStorage, BinaryOpStorage/*!*/ equalsStorage,
            object/*!*/ self, object other) {
            // Self is object so that we can reuse this method.

            if (!Protocols.RespondTo(respondToStorage, other, "to_str")) {
                return false;
            }

            var equals = equalsStorage.GetCallSite("==");
            return Protocols.IsTrue(equals.Target(equals, other, self));
        }
Пример #27
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;
        }
Пример #28
0
        private static Stream /*!*/ GetStream(ConversionStorage <MutableString> /*!*/ toStr, RespondToStorage /*!*/ respondTo, object port)
        {
            var           toStrSite = toStr.GetSite(TryConvertToStrAction.Make(toStr.Context));
            MutableString str       = toStrSite.Target(toStrSite, port);

            if (str != null)
            {
                return(new MutableStringStream(str));
            }

            IOWrapper wrapper = RubyIOOps.CreateIOWrapper(respondTo, port, FileAccess.Read);

            if (!wrapper.CanRead)
            {
                throw RubyExceptions.CreateTypeError("instance of IO needed");
            }

            return(wrapper);
        }
Пример #29
0
        public static object EachDocument(ConversionStorage <MutableString> /*!*/ toStr, RespondToStorage /*!*/ respondTo,
                                          RubyScope /*!*/ scope, BlockParam block, RubyModule /*!*/ self, object io)
        {
            RubyConstructor rc = MakeConstructor(scope.GlobalScope, GetStream(toStr, respondTo, io));

            if (block == null && rc.CheckData())
            {
                throw RubyExceptions.NoBlockGiven();
            }

            foreach (object obj in rc)
            {
                object result;
                if (block.Yield(obj, out result))
                {
                    return(result);
                }
            }
            return(null);
        }
Пример #30
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);
        }
Пример #31
0
 public static bool Equals(RespondToStorage/*!*/ respondTo, BinaryOpStorage/*!*/ equals, IList/*!*/ self, object other) {
     return Protocols.RespondTo(respondTo, other, "to_ary") && Protocols.IsEqual(equals, other, self);
 }
Пример #32
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
                );
            }
        }
Пример #33
0
        // TODO: 1.9 only
        // bytes -> Enumerable::Enumerator
        // lines -> Enumerable::Enumerator

        public static IOWrapper/*!*/ CreateIOWrapper(RespondToStorage/*!*/ respondToStorage, object io, FileAccess access) {
            return CreateIOWrapper(respondToStorage, io, access, 0x1000);
        }
Пример #34
0
 /// <summary>
 /// Check that the object responds to "succ".
 /// </summary>
 private static void CheckBegin(RespondToStorage/*!*/ respondToStorage, object begin) {
     if (!Protocols.RespondTo(respondToStorage, begin, "succ")) {
         throw RubyExceptions.CreateTypeError(String.Format("can't iterate from {0}", respondToStorage.Context.GetClassName(begin)));
     }
 }
Пример #35
0
 public static bool Equals(RespondToStorage /*!*/ respondTo, BinaryOpStorage /*!*/ equals, IDictionary <object, object> /*!*/ self, object other)
 {
     return(Protocols.RespondTo(respondTo, other, "to_hash") && Protocols.IsEqual(equals, other, self));
 }
Пример #36
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;
        }
Пример #37
0
        private static Stream/*!*/ GetStream(ConversionStorage<MutableString>/*!*/ toStr, RespondToStorage/*!*/ respondTo, object port) {
            var toStrSite = toStr.GetSite(TryConvertToStrAction.Make(toStr.Context));
            MutableString str = toStrSite.Target(toStrSite, port);
            if (str != null) {
                return new MutableStringStream(str);
            }

            IOWrapper wrapper = RubyIOOps.CreateIOWrapper(respondTo, port, FileAccess.Read);
            if (!wrapper.CanRead) {
                throw RubyExceptions.CreateTypeError("instance of IO needed");
            }

            return wrapper;
        }
Пример #38
0
        public static object ParseDocuments(ConversionStorage<MutableString>/*!*/ toStr, RespondToStorage/*!*/ respondTo,
            BlockParam block, RubyModule/*!*/ self, object io) {

            foreach (Node obj in MakeComposer(self.Context, GetStream(toStr, respondTo, io))) {
                // TODO: the enumerator shouldn't return null:
                if (obj == null) {
                    return null;
                }

                if (block == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

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

            return null;
        }
Пример #39
0
        //sysseek
        //syswrite

        #endregion

        internal static IOWrapper/*!*/ CreateIOWrapper(RespondToStorage/*!*/ respondToStorage, 
            RubyContext/*!*/ context, object io, FileAccess access) {

            bool canRead, canWrite, canSeek;

            if (access == FileAccess.Read || access == FileAccess.ReadWrite) {
                canRead = Protocols.RespondTo(respondToStorage, context, io, "read");
            } else {
                canRead = false;
            }

            if (access == FileAccess.Write || access == FileAccess.ReadWrite) {
                canWrite = Protocols.RespondTo(respondToStorage, context, io, "write");
            } else {
                canWrite = false;
            }

            canSeek = Protocols.RespondTo(respondToStorage, context, io, "seek") && Protocols.RespondTo(respondToStorage, context, io, "tell");

            return new IOWrapper(context, io, canRead, canWrite, canSeek);
        }
Пример #40
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
        }
Пример #41
0
 public static bool Equals(RespondToStorage /*!*/ respondToStorage, BinaryOpStorage /*!*/ equalsStorage, string /*!*/ self, object other)
 {
     return(MutableStringOps.Equals(respondToStorage, equalsStorage, self, other));
 }
Пример #42
0
        public static object Compare(BinaryOpStorage/*!*/ comparisonStorage, RespondToStorage/*!*/ respondToStorage, object/*!*/ self, object other) {
            // Self is object so that we can reuse this method.

            // We test to see if other responds to to_str AND <=>
            // Ruby never attempts to convert other to a string via to_str and call Compare ... which is strange -- feels like a BUG in Ruby

            if (Protocols.RespondTo(respondToStorage, other, "to_str") && Protocols.RespondTo(respondToStorage, other, "<=>")) {
                var site = comparisonStorage.GetCallSite("<=>");
                object result = Integer.TryUnaryMinus(site.Target(site, other, self));
                if (result == null) {
                    throw RubyExceptions.CreateTypeError(String.Format("{0} can't be coerced into Fixnum",
                        comparisonStorage.Context.GetClassDisplayName(result)));
                }

                return result;
            }

            return null;
        }
Пример #43
0
        public static bool Equals(RespondToStorage/*!*/ respondToStorage, BinaryOpStorage/*!*/ equalsStorage,
            RubyContext/*!*/ context, MutableString/*!*/ self, object other) {

            CallSite<Func<CallSite, RubyContext, object, object, object>> equals;
            return Protocols.RespondTo(respondToStorage, context, other, "to_str") 
                && Protocols.IsTrue((equals = equalsStorage.GetCallSite("==")).Target(equals, context, other, self));
        }
Пример #44
0
        public static object Load(ConversionStorage<MutableString>/*!*/ toStr, RespondToStorage/*!*/ respondTo,
            RubyScope/*!*/ scope, RubyModule/*!*/ self, object io) {

            try {
                foreach (object obj in MakeConstructor(scope.GlobalScope, GetStream(toStr, respondTo, io))) {
                    return obj;
                }
                return null;
            } catch (Exception e) {
                throw RubyExceptions.CreateArgumentError(e, e.Message);
            } finally {
                RubyIO rio = io as RubyIO;
                if (rio != null) {
                    rio.Close();
                }
            }
        }
Пример #45
0
        public static object EachDocument(ConversionStorage<MutableString>/*!*/ toStr, RespondToStorage/*!*/ respondTo, 
            RubyScope/*!*/ scope, BlockParam block, RubyModule/*!*/ self, object io) {
            RubyConstructor rc = MakeConstructor(scope.GlobalScope, GetStream(toStr, respondTo, io));
            if (block == null && rc.CheckData()) {
                throw RubyExceptions.NoBlockGiven();
            }

            foreach (object obj in rc) {
                object result;
                if (block.Yield(obj, out result)) {
                    return result;
                }
            }
            return null;
        }
Пример #46
0
 public static object Compare(BinaryOpStorage/*!*/ comparisonStorage, RespondToStorage/*!*/ respondToStorage, string/*!*/ self, object other) {
     return MutableStringOps.Compare(comparisonStorage, respondToStorage, self, other);
 }
Пример #47
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;
        }
Пример #48
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();
                }
            }
        }
Пример #49
0
 public static object Parse(ConversionStorage<MutableString>/*!*/ toStr, RespondToStorage/*!*/ respondTo, RubyModule/*!*/ self, object io) {
     using (Stream stream = GetStream(toStr, respondTo, io)) {
         foreach (Node obj in MakeComposer(self.Context, stream)) {
             // TODO: the enumerator shouldn't return null:
             if (obj == null) {
                 break;
             } 
             
             return obj;
         }
     }
     return ScriptingRuntimeHelpers.False;
 }
Пример #50
0
        public static IOWrapper/*!*/ CreateIOWrapper(RespondToStorage/*!*/ respondToStorage, object io, FileAccess access, int bufferSize) {
            bool canRead, canWrite, canSeek, canFlush, canBeClosed;

            if (access == FileAccess.Read || access == FileAccess.ReadWrite) {
                canRead = Protocols.RespondTo(respondToStorage, io, "read");
            } else {
                canRead = false;
            }

            if (access == FileAccess.Write || access == FileAccess.ReadWrite) {
                canWrite = Protocols.RespondTo(respondToStorage, io, "write");
            } else {
                canWrite = false;
            }

            canSeek = Protocols.RespondTo(respondToStorage, io, "seek") && Protocols.RespondTo(respondToStorage, io, "tell");
            canFlush = Protocols.RespondTo(respondToStorage, io, "flush");
            canBeClosed = Protocols.RespondTo(respondToStorage, io, "close");

            return new IOWrapper(respondToStorage.Context, io, canRead, canWrite, canSeek, canFlush, canBeClosed, bufferSize);
        }
Пример #51
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));
        }