コード例 #1
0
        public void LargeGraph()
        {
            var    list = new LinkedList <object>();
            object obj  = list;

            for (int i = 0; i < 10000; i++)
            {
                var node    = list.AddFirst(i);
                var newList = new LinkedList <object>();
                list.AddAfter(node, newList);
                list = newList;
            }

            string output = "LinkedList<object>(2) { 0, LinkedList<object>(2) { 1, LinkedList<object>(2) { 2, LinkedList<object>(2) {";

            for (int i = 100; i > 4; i--)
            {
                var options = new ObjectFormattingOptions(maxOutputLength: i, memberFormat: MemberDisplayFormat.Inline);
                var str     = CSharpObjectFormatter.Instance.FormatObject(obj, options);

                var expected = output.Substring(0, i - " ...".Length);
                if (!expected.EndsWith(" ", StringComparison.Ordinal))
                {
                    expected += " ";
                }

                expected += "...";

                Assert.Equal(expected, str);
            }
        }
コード例 #2
0
            public Service()
            {
                // TODO (tomat): we should share the copied files with the host
                _metadataFileProvider = new MetadataShadowCopyProvider(
                    Path.Combine(Path.GetTempPath(), "InteractiveHostShadow"),
                    noShadowCopyDirectories: s_systemNoShadowCopyDirectories);

                _assemblyLoader = new InteractiveAssemblyLoader(_metadataFileProvider);

                _formattingOptions = new ObjectFormattingOptions(
                    memberFormat: MemberDisplayFormat.Inline,
                    quoteStrings: true,
                    useHexadecimalNumbers: false,
                    maxOutputLength: 200,
                    memberIndentation: "  ");

                _hostObject = new InteractiveHostObject();

                var initialState = new EvaluationState(
                    scriptState: null,
                    scriptOptions: ScriptOptions.Default,
                    sourceSearchPaths: ImmutableArray <string> .Empty,
                    referenceSearchPaths: ImmutableArray <string> .Empty,
                    workingDirectory: Directory.GetCurrentDirectory());

                _lastTask = Task.FromResult(initialState);

                Console.OutputEncoding = Encoding.UTF8;

                // We want to be sure to delete the shadow-copied files when the process goes away. Frankly
                // there's nothing we can do if the process is forcefully quit or goes down in a completely
                // uncontrolled manner (like a stack overflow). When the process goes down in a controlled
                // manned, we should generally expect this event to be called.
                AppDomain.CurrentDomain.ProcessExit += HandleProcessExit;
            }
コード例 #3
0
ファイル: Sandbox.cs プロジェクト: nickdevore/Compilify
        private ExecutionResult Execute(object assemblyInput)
        {
            var assembly          = (ICodeAssembly)assemblyInput;
            var result            = new ExecutionResult();
            var type              = typeof(ByteCodeLoader);
            var formatter         = ObjectFormatter.Instance;
            var formattingOptions = new ObjectFormattingOptions(maxOutputLength: 5120);

            var className      = assembly.EntryPointClassName;
            var methodName     = assembly.EntryPointMethodName;
            var resultProperty = "Result";
            var assemblyBytes  = assembly.CompiledAssembly;

            try
            {
                var handle            = Activator.CreateInstanceFrom(domain, type.Assembly.ManifestModule.FullyQualifiedName, type.FullName);
                var loader            = (ByteCodeLoader)handle.Unwrap();
                var unformattedResult = loader.Run(className, methodName, resultProperty, assemblyBytes);

                result.Result               = (unformattedResult != null) ? formatter.FormatObject(unformattedResult.ReturnValue, formattingOptions) : "null";
                result.ConsoleOutput        = (unformattedResult != null) ? unformattedResult.ConsoleOutput : string.Empty;
                result.ProcessorTime        = domain.MonitoringTotalProcessorTime;
                result.TotalMemoryAllocated = domain.MonitoringTotalAllocatedMemorySize;
            }
            catch (SerializationException ex)
            {
                result.Result = ex.Message;
            }
            catch (TargetInvocationException ex)
            {
                result.Result = ex.InnerException.ToString();
            }

            return(result);
        }
コード例 #4
0
 // TODO (tomat): testing only
 public void SetTestObjectFormattingOptions()
 {
     _formattingOptions = new ObjectFormattingOptions(
         memberFormat: MemberDisplayFormat.Inline,
         quoteStrings: true,
         useHexadecimalNumbers: false,
         maxOutputLength: int.MaxValue,
         memberIndentation: "  ");
 }
コード例 #5
0
 public Service()
 {
     // TODO (tomat): we should share the copied files with the host
     _metadataFileProvider = new MetadataShadowCopyProvider();
     _assemblyLoader       = new InteractiveAssemblyLoader(_metadataFileProvider);
     _sourceSearchPaths    = DefaultSourceSearchPaths;
     _formattingOptions    = new ObjectFormattingOptions(
         memberFormat: MemberDisplayFormat.Inline,
         quoteStrings: true,
         useHexadecimalNumbers: false,
         maxOutputLength: 200,
         memberIndentation: "  ");
 }
コード例 #6
0
        public void LongMembers()
        {
            object obj = new LongMembers();

            var options = new ObjectFormattingOptions(maxLineLength: 20, memberFormat: MemberDisplayFormat.Inline);

            //str = ObjectFormatter.Instance.FormatObject(obj, options);
            //Assert.Equal("LongMembers { Lo ...", str);

            options = new ObjectFormattingOptions(maxLineLength: 20, memberFormat: MemberDisplayFormat.List);
            var str = CSharpObjectFormatter.Instance.FormatObject(obj, options);

            Assert.Equal("LongMembers {\r\n  LongName012345 ...\r\n  LongValue: \"01 ...\r\n}\r\n", str);
        }
コード例 #7
0
            public Service()
            {
                // TODO (tomat): we should share the copied files with the host
                _metadataFileProvider = new MetadataShadowCopyProvider(
                    Path.Combine(Path.GetTempPath(), "InteractiveHostShadow"),
                    noShadowCopyDirectories: s_systemNoShadowCopyDirectories);
                _assemblyLoader    = new InteractiveAssemblyLoader(_metadataFileProvider);
                _formattingOptions = new ObjectFormattingOptions(
                    memberFormat: MemberDisplayFormat.Inline,
                    quoteStrings: true,
                    useHexadecimalNumbers: false,
                    maxOutputLength: 200,
                    memberIndentation: "  ");

                // We want to be sure to delete the shadow-copied files when the process goes away. Frankly
                // there's nothing we can do if the process is forcefully quit or goes down in a completely
                // uncontrolled manner (like a stack overflow). When the process goes down in a controlled
                // manned, we should generally expect this event to be called.
                AppDomain.CurrentDomain.ProcessExit += HandleProcessExit;
            }
コード例 #8
0
        private StringBuilder AppendTypeInstantiation(StringBuilder builder, Type type, Type[] genericArguments, ref int genericArgIndex,
                                                      ObjectFormattingOptions options)
        {
            // generic arguments of all the outer types and the current type;
            Type[] currentGenericArgs = type.GetGenericArguments();
            int    currentArgCount    = currentGenericArgs.Length - genericArgIndex;

            if (currentArgCount > 0)
            {
                int backtick = type.Name.IndexOf('`');
                if (backtick > 0)
                {
                    builder.Append(type.Name.Substring(0, backtick));
                }
                else
                {
                    builder.Append(type.Name);
                }

                builder.Append('<');

                for (int i = 0; i < currentArgCount; i++)
                {
                    if (i > 0)
                    {
                        builder.Append(", ");
                    }
                    builder.Append(FormatTypeName(genericArguments[genericArgIndex++], options));
                }

                builder.Append('>');
            }
            else
            {
                builder.Append(type.Name);
            }

            return(builder);
        }
コード例 #9
0
 public override string FormatArrayTypeName(Array array, ObjectFormattingOptions options)
 {
     return FormatArrayTypeName(array.GetType(), array, options);
 }
コード例 #10
0
 public override string FormatTypeName(Type type, ObjectFormattingOptions options)
 {
     return(GetPrimitiveTypeName(type) ?? AppendComplexTypeName(new StringBuilder(), type, options).ToString());
 }
コード例 #11
0
 // TODO (tomat): testing only
 public void SetTestObjectFormattingOptions()
 {
     _formattingOptions = new ObjectFormattingOptions(
         memberFormat: MemberDisplayFormat.Inline,
         quoteStrings: true,
         useHexadecimalNumbers: false,
         maxOutputLength: int.MaxValue,
         memberIndentation: "  ");
 }
コード例 #12
0
            public Service()
            {
                // TODO (tomat): we should share the copied files with the host
                _metadataFileProvider = new MetadataShadowCopyProvider(
                    Path.Combine(Path.GetTempPath(), "InteractiveHostShadow"),
                    noShadowCopyDirectories: s_systemNoShadowCopyDirectories);

                _assemblyLoader = new InteractiveAssemblyLoader(_metadataFileProvider);

                _formattingOptions = new ObjectFormattingOptions(
                    memberFormat: MemberDisplayFormat.Inline,
                    quoteStrings: true,
                    useHexadecimalNumbers: false,
                    maxOutputLength: 200,
                    memberIndentation: "  ");

                _hostObject = new InteractiveHostObject();

                var initialState = new EvaluationState(
                    scriptState: null,
                    scriptOptions: ScriptOptions.Default,
                    sourceSearchPaths: ImmutableArray<string>.Empty,
                    referenceSearchPaths: ImmutableArray<string>.Empty,
                    workingDirectory: Directory.GetCurrentDirectory());

                _lastTask = Task.FromResult(initialState);

                Console.OutputEncoding = Encoding.UTF8;
              
                // We want to be sure to delete the shadow-copied files when the process goes away. Frankly
                // there's nothing we can do if the process is forcefully quit or goes down in a completely
                // uncontrolled manner (like a stack overflow). When the process goes down in a controlled
                // manned, we should generally expect this event to be called.
                AppDomain.CurrentDomain.ProcessExit += HandleProcessExit;
            }
コード例 #13
0
        public override string FormatArrayTypeName(Type arrayType, Array arrayOpt, ObjectFormattingOptions options)
        {
            StringBuilder sb = new StringBuilder();

            // print the inner-most element type first:
            Type elementType = arrayType.GetElementType();
            while (elementType.IsArray)
            {
                elementType = elementType.GetElementType();
            }

            sb.Append(FormatTypeName(elementType, options));

            // print all components of a jagged array:
            Type type = arrayType;
            do
            {
                if (arrayOpt != null)
                {
                    sb.Append('[');

                    int rank = type.GetArrayRank();

                    bool anyNonzeroLowerBound = false;
                    for (int i = 0; i < rank; i++)
                    {
                        if (arrayOpt.GetLowerBound(i) > 0)
                        {
                            anyNonzeroLowerBound = true;
                            break;
                        }
                    }

                    for (int i = 0; i < rank; i++)
                    {
                        int lowerBound = arrayOpt.GetLowerBound(i);
                        int length = arrayOpt.GetLength(i);

                        if (i > 0)
                        {
                            sb.Append(", ");
                        }

                        if (anyNonzeroLowerBound)
                        {
                            AppendArrayBound(sb, lowerBound, options.UseHexadecimalNumbers);
                            sb.Append("..");
                            AppendArrayBound(sb, length + lowerBound, options.UseHexadecimalNumbers);
                        }
                        else
                        {
                            AppendArrayBound(sb, length, options.UseHexadecimalNumbers);
                        }
                    }

                    sb.Append(']');
                    arrayOpt = null;
                }
                else
                {
                    AppendArrayRank(sb, type);
                }

                type = type.GetElementType();
            }
            while (type.IsArray);

            return sb.ToString();
        }
コード例 #14
0
        private StringBuilder AppendComplexTypeName(StringBuilder builder, Type type, ObjectFormattingOptions options)
        {
            if (type.IsArray)
            {
                builder.Append(FormatArrayTypeName(type, arrayOpt: null, options: options));
                return(builder);
            }

            // compiler generated (e.g. iterator/async)
            string stateMachineName;

            if (GeneratedNames.TryParseSourceMethodNameFromGeneratedName(type.Name, GeneratedNameKind.StateMachineType, out stateMachineName))
            {
                builder.Append(stateMachineName);
                return(builder);
            }

            if (type.IsGenericType)
            {
                // consolidated generic arguments (includes arguments of all declaring types):
                Type[] genericArguments = type.GetGenericArguments();

                if (type.DeclaringType != null)
                {
                    List <Type> nestedTypes = new List <Type>();
                    do
                    {
                        nestedTypes.Add(type);
                        type = type.DeclaringType;
                    }while (type != null);

                    int typeArgumentIndex = 0;
                    for (int i = nestedTypes.Count - 1; i >= 0; i--)
                    {
                        AppendTypeInstantiation(builder, nestedTypes[i], genericArguments, ref typeArgumentIndex, options);
                        if (i > 0)
                        {
                            builder.Append('.');
                        }
                    }
                }
                else
                {
                    int typeArgumentIndex = 0;
                    return(AppendTypeInstantiation(builder, type, genericArguments, ref typeArgumentIndex, options));
                }
            }
            else if (type.DeclaringType != null)
            {
                builder.Append(type.Name.Replace('+', '.'));
            }
            else
            {
                builder.Append(type.Name);
            }

            return(builder);
        }
コード例 #15
0
        public void LongMembers()
        {
            object obj = new LongMembers();

            var options = new ObjectFormattingOptions(maxLineLength: 20, memberFormat: MemberDisplayFormat.Inline);
            //str = ObjectFormatter.Instance.FormatObject(obj, options);
            //Assert.Equal("LongMembers { Lo ...", str);

            options = new ObjectFormattingOptions(maxLineLength: 20, memberFormat: MemberDisplayFormat.List);
            var str = CSharpObjectFormatter.Instance.FormatObject(obj, options);
            Assert.Equal("LongMembers {\r\n  LongName012345 ...\r\n  LongValue: \"01 ...\r\n}\r\n", str);
        }
コード例 #16
0
            public Service()
            {
                // TODO (tomat): we should share the copied files with the host
                _metadataFileProvider = new MetadataShadowCopyProvider(
                    Path.Combine(Path.GetTempPath(), "InteractiveHostShadow"),
                    noShadowCopyDirectories: s_systemNoShadowCopyDirectories);
                _assemblyLoader = new InteractiveAssemblyLoader(_metadataFileProvider);
                _formattingOptions = new ObjectFormattingOptions(
                    memberFormat: MemberDisplayFormat.Inline,
                    quoteStrings: true,
                    useHexadecimalNumbers: false,
                    maxOutputLength: 200,
                    memberIndentation: "  ");

                // We want to be sure to delete the shadow-copied files when the process goes away. Frankly
                // there's nothing we can do if the process is forcefully quit or goes down in a completely
                // uncontrolled manner (like a stack overflow). When the process goes down in a controlled
                // manned, we should generally expect this event to be called.
                AppDomain.CurrentDomain.ProcessExit += HandleProcessExit;
            }
コード例 #17
0
        public void LargeGraph()
        {
            var list = new LinkedList<object>();
            object obj = list;
            for (int i = 0; i < 10000; i++)
            {
                var node = list.AddFirst(i);
                var newList = new LinkedList<object>();
                list.AddAfter(node, newList);
                list = newList;
            }

            string output = "LinkedList<object>(2) { 0, LinkedList<object>(2) { 1, LinkedList<object>(2) { 2, LinkedList<object>(2) {";

            for (int i = 100; i > 4; i--)
            {
                var options = new ObjectFormattingOptions(maxOutputLength: i, memberFormat: MemberDisplayFormat.Inline);
                var str = CSharpObjectFormatter.Instance.FormatObject(obj, options);

                var expected = output.Substring(0, i - " ...".Length);
                if (!expected.EndsWith(" ", StringComparison.Ordinal))
                {
                    expected += " ";
                }

                expected += "...";

                Assert.Equal(expected, str);
            }
        }
コード例 #18
0
        private StringBuilder AppendTypeInstantiation(StringBuilder builder, Type type, Type[] genericArguments, ref int genericArgIndex, ObjectFormattingOptions options)
        {
            // generic arguments of all the outer types and the current type;
            Type[] currentGenericArgs = type.GetGenericArguments();
            int currentArgCount = currentGenericArgs.Length - genericArgIndex;

            if (currentArgCount > 0)
            {
                int backtick = type.Name.IndexOf('`');
                if (backtick > 0)
                {
                    builder.Append(type.Name.Substring(0, backtick));
                }
                else
                {
                    builder.Append(type.Name);
                }

                builder.Append('<');

                for (int i = 0; i < currentArgCount; i++)
                {
                    if (i > 0)
                    {
                        builder.Append(", ");
                    }
                    builder.Append(FormatTypeName(genericArguments[genericArgIndex++], options));
                }

                builder.Append('>');
            }
            else
            {
                builder.Append(type.Name);
            }

            return builder;
        }
コード例 #19
0
        private StringBuilder AppendComplexTypeName(StringBuilder builder, Type type, ObjectFormattingOptions options)
        {
            if (type.IsArray)
            {
                builder.Append(FormatArrayTypeName(type, arrayOpt: null, options: options));
                return builder;
            }

            // compiler generated (e.g. iterator/async)
            string stateMachineName;
            if (GeneratedNames.TryParseSourceMethodNameFromGeneratedName(type.Name, GeneratedNameKind.StateMachineType, out stateMachineName))
            {
                builder.Append(stateMachineName);
                return builder;
            }

            if (type.IsGenericType)
            {
                // consolidated generic arguments (includes arguments of all declaring types):
                Type[] genericArguments = type.GetGenericArguments();

                if (type.DeclaringType != null)
                {
                    List<Type> nestedTypes = new List<Type>();
                    do
                    {
                        nestedTypes.Add(type);
                        type = type.DeclaringType;
                    }
                    while (type != null);

                    int typeArgumentIndex = 0;
                    for (int i = nestedTypes.Count - 1; i >= 0; i--)
                    {
                        AppendTypeInstantiation(builder, nestedTypes[i], genericArguments, ref typeArgumentIndex, options);
                        if (i > 0)
                        {
                            builder.Append('.');
                        }
                    }
                }
                else
                {
                    int typeArgumentIndex = 0;
                    return AppendTypeInstantiation(builder, type, genericArguments, ref typeArgumentIndex, options);
                }
            }
            else if (type.DeclaringType != null)
            {
                builder.Append(type.Name.Replace('+', '.'));
            }
            else
            {
                builder.Append(type.Name);
            }

            return builder;
        }
コード例 #20
0
 public override string FormatTypeName(Type type, ObjectFormattingOptions options)
 {
     return GetPrimitiveTypeName(GetPrimitiveSpecialType(type)) ?? AppendComplexTypeName(new StringBuilder(), type, options).ToString();
 }
コード例 #21
0
        private string FormatArrayTypeName(Type arrayType, Array arrayOpt, ObjectFormattingOptions options)
        {
            StringBuilder sb = new StringBuilder();

            // print the inner-most element type first:
            Type elementType = arrayType.GetElementType();

            while (elementType.IsArray)
            {
                elementType = elementType.GetElementType();
            }

            sb.Append(FormatTypeName(elementType, options));

            // print all components of a jagged array:
            Type type = arrayType;

            do
            {
                if (arrayOpt != null)
                {
                    sb.Append('[');

                    int rank = type.GetArrayRank();

                    bool anyNonzeroLowerBound = false;
                    for (int i = 0; i < rank; i++)
                    {
                        if (arrayOpt.GetLowerBound(i) > 0)
                        {
                            anyNonzeroLowerBound = true;
                            break;
                        }
                    }

                    for (int i = 0; i < rank; i++)
                    {
                        int  lowerBound = arrayOpt.GetLowerBound(i);
                        long length     = arrayOpt.GetLongLength(i);

                        if (i > 0)
                        {
                            sb.Append(", ");
                        }

                        if (anyNonzeroLowerBound)
                        {
                            AppendArrayBound(sb, lowerBound, options.UseHexadecimalNumbers);
                            sb.Append("..");
                            AppendArrayBound(sb, length + lowerBound, options.UseHexadecimalNumbers);
                        }
                        else
                        {
                            AppendArrayBound(sb, length, options.UseHexadecimalNumbers);
                        }
                    }

                    sb.Append(']');
                    arrayOpt = null;
                }
                else
                {
                    AppendArrayRank(sb, type);
                }

                type = type.GetElementType();
            }while (type.IsArray);

            return(sb.ToString());
        }
コード例 #22
0
 public override string FormatArrayTypeName(Array array, ObjectFormattingOptions options)
 {
     return(FormatArrayTypeName(array.GetType(), array, options));
 }