Exemple #1
0
        public void Serialization1()
        {
            if (_driver.PartialTrust)
            {
                return;
            }

            var encodings = new object[] {
                RubyEncoding.KCodeEUC,
                RubyEncoding.KCodeSJIS,
                RubyEncoding.UTF8,
                RubyEncoding.KCodeSJIS.RealEncoding,
            };

            Assert(ArrayUtils.ValueEquals(encodings, Roundtrip(encodings)));

            var s  = MutableString.Create("foo", RubyEncoding.UTF8);
            var rs = Roundtrip(s);

            Assert(s.Equals(rs));

            var e  = new Exception("msg");
            var ed = RubyExceptionData.GetInstance(e);

            ed.Backtrace = new RubyArray(new[] { 1, 2, 3 });

            var re  = Roundtrip(e);
            var rde = RubyExceptionData.TryGetInstance(re);

            Assert(rde != null);
            Assert(ArrayUtils.ValueEquals(rde.Backtrace.ToArray(), new object[] { 1, 2, 3 }));
        }
        private void _ParseException(Exception ex)
        {
            var red   = RubyExceptionData.GetInstance(ex);
            var trace = red.Backtrace;

            ParseFromBacktrace((MutableString)red.Message, trace);
        }
Exemple #3
0
        public static Exception /*!*/ ReinitializeException(RubyContext /*!*/ context, Exception /*!*/ self, [DefaultParameterValue(null)] object message)
        {
            var instance = RubyExceptionData.GetInstance(self);

            instance.Backtrace = null;
            instance.Message   = message ?? MutableString.Create(context.GetClassOf(self).Name, context.GetIdentifierEncoding());
            return(self);
        }
Exemple #4
0
        public static Exception /*!*/ ReinitializeException(Exception /*!*/ self, object /*!*/ message)
        {
            var instance = RubyExceptionData.GetInstance(self);

            instance.Backtrace = null;
            instance.Message   = message;
            return(self);
        }
Exemple #5
0
        public static RubyArray SetBacktrace(Exception /*!*/ self, RubyArray backtrace)
        {
            if (backtrace != null && !CollectionUtils.TrueForAll(backtrace, (item) => item is MutableString))
            {
                throw RubyExceptions.CreateTypeError("backtrace must be Array of String");
            }

            return(RubyExceptionData.GetInstance(self).Backtrace = backtrace);
        }
Exemple #6
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 #7
0
        public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, Exception /*!*/ self)
        {
            object message   = RubyExceptionData.GetInstance(self).Message;
            string className = RubyUtils.GetClassName(context, self);

            MutableString result = MutableString.CreateMutable();

            result.Append("#<");
            result.Append(className);
            result.Append(": ");
            if (message != null)
            {
                result.Append(KernelOps.Inspect(context, message));
            }
            else
            {
                result.Append(className);
            }
            result.Append('>');
            return(result);
        }
Exemple #8
0
        public static Thread /*!*/ CreateThread(RubyContext /*!*/ context, BlockParam startRoutine, object self, [NotNull] params object[] /*!*/ args)
        {
            if (startRoutine == null)
            {
                throw new ThreadError("must be called with a block");
            }
            ThreadGroup group  = Group(Thread.CurrentThread);
            Thread      result = new Thread(new ThreadStart(delegate() {
                RubyThreadInfo info = RubyThreadInfo.FromThread(Thread.CurrentThread);
                info.Group          = group;

                try {
                    object threadResult;
                    // TODO: break?
                    startRoutine.Yield(args, out threadResult);
                    info.Result = threadResult;
#if !SILVERLIGHT
                } catch (ThreadInterruptedException) {
                    // Do nothing with this for now
#endif
                } catch (Exception e) {
                    info.Exception = e;

                    Utils.Log(
                        e.Message + "\r\n\r\n" +
                        e.StackTrace + "\r\n\r\n" +
                        IListOps.Join(context, RubyExceptionData.GetInstance(e).Backtrace).ToString(),
                        "THREAD"
                        );

                    if (_globalAbortOnException || info.AbortOnException)
                    {
                        throw;
                    }
                }
            }));

            result.Start();
            return(result);
        }
Exemple #9
0
 public static object StringRepresentation(Exception /*!*/ self)
 {
     return(RubyExceptionData.GetInstance(self).Message);
 }
Exemple #10
0
 public static RubyArray /*!*/ SetBacktrace(Exception /*!*/ self, [NotNull] MutableString /*!*/ backtrace)
 {
     return(RubyExceptionData.GetInstance(self).Backtrace = RubyArray.Create(backtrace));
 }
Exemple #11
0
 public static RubyArray GetBacktrace(Exception /*!*/ self)
 {
     return(RubyExceptionData.GetInstance(self).Backtrace);
 }
Exemple #12
0
        private static void RubyThreadStart(RubyContext /*!*/ context, BlockParam /*!*/ startRoutine, object[] /*!*/ args, ThreadGroup group)
        {
            RubyThreadInfo info = RubyThreadInfo.FromThread(Thread.CurrentThread);

            info.CreatedFromRuby = true;

            info.Group = group;

            try {
                object threadResult;
                // TODO: break/returns might throw LocalJumpError if the RFC that was created for startRoutine is not active anymore:
                if (startRoutine.Yield(args, out threadResult) && startRoutine.Returning(threadResult, out threadResult))
                {
                    info.Exception = new ThreadError("return can't jump across threads");
                }
                info.Result = threadResult;
            } catch (MethodUnwinder) {
                info.Exception = new ThreadError("return can't jump across threads");
            } catch (Exception e) {
                if (info.ExitRequested)
                {
                    // Note that "e" may not be ThreadAbortException at this point If an exception was raised from a finally block,
                    // we will get that here instead
                    Utils.Log(String.Format("Thread {0} exited.", info.Thread.ManagedThreadId), "THREAD");
                    info.Result = false;
#if !SILVERLIGHT
                    Thread.ResetAbort();
#endif
                }
                else
                {
                    e = RubyUtils.GetVisibleException(e);
                    RubyExceptionData.ActiveExceptionHandled(e);
                    info.Exception = e;

                    StringBuilder trace = new StringBuilder();
                    trace.Append(e.Message);
                    trace.AppendLine();
                    trace.AppendLine();
                    trace.Append(e.StackTrace);
                    trace.AppendLine();
                    trace.AppendLine();
                    RubyExceptionData data = RubyExceptionData.GetInstance(e);
                    if (data.Backtrace != null)
                    {
                        foreach (var frame in data.Backtrace)
                        {
                            trace.Append(frame.ToString());
                        }
                    }

                    Utils.Log(trace.ToString(), "THREAD");

                    if (_globalAbortOnException || info.AbortOnException)
                    {
                        throw;
                    }
                }
            } finally {
                // Its not a good idea to terminate a thread which has set Thread.critical=true, but its hard to predict
                // which thread will be scheduled next, even with green threads. However, ConditionVariable.create_timer
                // in monitor.rb explicitly does "Thread.critical=true; other_thread.raise" before exiting, and expects
                // other_thread to be scheduled immediately.
                // To deal with such code, we release the critical monitor here if the current thread is holding it
                if (context.RubyOptions.Compatibility < RubyCompatibility.Ruby19 && context.CriticalThread == Thread.CurrentThread)
                {
                    SetCritical(context, false);
                }
            }
        }
Exemple #13
0
 public static object GetMessage(Exception /*!*/ self)
 {
     return(RubyExceptionData.GetInstance(self).Message);
 }