24 April 2012 C#, Compiler, Compiler-as-a-Service, Roslyn Robert Muehsig

Heute bin ich durch Philip Proplesch auf Compilify aufmerksam geworden:

image

Compilify funktioniert so ähnlich wie jsFiddler – man kann on the fly Code eingeben und es kommt ein Ergebnis. Da Compilify Open Source ist kann man natürlich auch ein Blick hinter die Kulissen werfen und siehe da: Das “Roslyn” ist im Einsatz!

Was ist Roslyn?

Ganz stark verkürzt ist Roslyn ein Compiler-as-a-Service, geschrieben in .NET. Das heisst man kann den Compiler auch aus dem Code heraus aufrufen und es gibt eine API um den Syntax zu erkennen und den Code zu kompilieren. Genau das wird auch bei Compilify genutzt.

Schauen wir in den Compilify Source Code

Leider bekam ich das Projekt auf die schnelle nicht zum Laufen, da man wohl noch eine Redis Instanz braucht und ansonsten irgendwelche Dinge passieren. Naja – interessanter waren für mich zwei Code Teile, welche ich aus dem GitHub Repository hier mal übernehme:

Wie kommen die Syntax Errors auf die Seite?

Wenn ich was völlig falsches eingebe gibt es schicke “Fehlermeldungen”, die auch aus dem Visual Studio hätten kommen können. Auf den ersten Blick scheint dieser Code dieses Verhalten zu erzeugen:

public class CSharpCompiler
    {
        public IEnumerable<IDiagnostic> GetCompilationErrors(string command, string classes)
        {
            var builder = new StringBuilder();

            builder.AppendLine("public static object Eval() {");
            builder.AppendLine("#line 1");
            builder.Append(command);
            builder.AppendLine();
            builder.AppendLine("}");

            var script = builder.ToString();

            var mscorlib = Assembly.Load("mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089");
            var system = Assembly.Load("System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089");
            var core = Assembly.Load("System.Core,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089");

            var namespaces = ReadOnlyArray<string>.CreateFrom(new[]
                             {
                                 "System", 
                                 "System.IO", 
                                 "System.Net", 
                                 "System.Linq", 
                                 "System.Text", 
                                 "System.Text.RegularExpressions", 
                                 "System.Collections.Generic"
                             });

            var compilation = Compilation.Create("foo",
                new CompilationOptions(assemblyKind: AssemblyKind.DynamicallyLinkedLibrary, usings: namespaces),
                new[]
                {
                    SyntaxTree.ParseCompilationUnit(CodeExecuter.EntryPoint), 
                    SyntaxTree.ParseCompilationUnit(script, fileName: "Prompt", options: new ParseOptions(kind: SourceCodeKind.Interactive)),
                    SyntaxTree.ParseCompilationUnit(classes ?? string.Empty, fileName: "Editor", options: new ParseOptions(kind: SourceCodeKind.Script))
                },
                new MetadataReference[]
                { 
                    new AssemblyFileReference(mscorlib.Location),
                    new AssemblyFileReference(core.Location), 
                    new AssemblyFileReference(system.Location)
                });

            return compilation.GetDiagnostics();
        } 
    }

 

Sieht eigentlich “relativ” einfach aus – es werden auch nur bestimmte .NET Assemblies geladen und dann wird der Syntax Parser angeworfen. Als Ergebnis kommen Diagnostic Informationen, welche am Ende als Fehler angezeigt werden.

Der CodeExecuter

Nach der Validierung (und einigen Zwischenschritten) wird der Code am Ende dem CodeExecuter übergeben:

public class CodeExecuter
    {
        private static readonly string[] Namespaces =
            new[]
            {
                "System", 
                "System.IO", 
                "System.Net", 
                "System.Linq", 
                "System.Text", 
                "System.Text.RegularExpressions", 
                "System.Collections.Generic"
            };

        public const string EntryPoint = @"public class EntryPoint 
                                           {
                                               public static object Result { get; set; }
                      
                                               public static void Main()
                                               {
                                                   Result = Script.Eval();
                                               }
                                           }";

        public object Execute(string command, string classes)
        {
            if (!Validator.Validate(command) || !Validator.Validate(classes))
            {
                return "Not supported";
            } 

            var sandbox = SecureAppDomainFactory.Create();

            // Load basic .NET assemblies into our sandbox
            var mscorlib = sandbox.Load("mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089");
            var system = sandbox.Load("System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089");
            var core = sandbox.Load("System.Core,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089");

            var script = "public static object Eval() {" + command + "}";
            
            var options = new CompilationOptions(assemblyKind: AssemblyKind.ConsoleApplication, usings: ReadOnlyArray<string>.CreateFrom(Namespaces));

            var compilation = Compilation.Create(Guid.NewGuid().ToString("N"), options,
                new[]
                {
                    SyntaxTree.ParseCompilationUnit(EntryPoint),
                    // This is the syntax tree represented in the `Script` variable.
                    SyntaxTree.ParseCompilationUnit(script, options: new ParseOptions(kind: SourceCodeKind.Interactive)),
                    SyntaxTree.ParseCompilationUnit(classes ?? string.Empty, options: new ParseOptions(kind: SourceCodeKind.Script))
                },
                new MetadataReference[] { 
                    new AssemblyFileReference(core.Location), 
                    new AssemblyFileReference(system.Location),
                    new AssemblyFileReference(mscorlib.Location)
                });

            byte[] compiledAssembly;
            using (var output = new MemoryStream())
            {
                var emitResult = compilation.Emit(output);

                if (!emitResult.Success)
                {
                    var errors = emitResult.Diagnostics.Select(x => x.Info.GetMessage().Replace("Eval()", "<Factory>()")).ToArray();
                    return string.Join(", ", errors);
                }

                compiledAssembly = output.ToArray();
            }

            if (compiledAssembly.Length == 0)
            {
                // Not sure how this would happen?
                return "Incorrect data";
            }

            var loader = (ByteCodeLoader)Activator.CreateInstance(sandbox, typeof(ByteCodeLoader).Assembly.FullName, typeof(ByteCodeLoader).FullName).Unwrap();

            bool unloaded = false;
            object result = null;
            var timeout = TimeSpan.FromSeconds(5);
            try
            {
                var task = Task.Factory
                               .StartNew(() =>
                                         {
                                             try
                                             {
                                                 result = loader.Run("EntryPoint", "Result", compiledAssembly);
                                             }
                                             catch (Exception ex)
                                             {
                                                 result = ex.Message;
                                             }
                                         });

                if (!task.Wait(timeout))
                {
                    AppDomain.Unload(sandbox);
                    unloaded = true;
                    result = "[Execution timed out after 5 seconds]";
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            
            if (!unloaded)
            {
                AppDomain.Unload(sandbox);
            }
            
            if (result == null || string.IsNullOrEmpty(result.ToString()))
            {
                result = "null";
            }

            return result;
        }
    }

 

Hier passiert etwas mehr Magic. Es wird ein Kompilat erzeugt und am Ende wird der Bytecode geladen und ausgeführt… noch kann ich nicht ganz folgen was hier im Detail passiert.

Auf alle Fälle interessant ;)

Weitere Hintergrund Infos findet ihr hier oder im GitHub Repository sowie auf der MSDN Roslyn Seite.


Written by Robert Muehsig

Software Developer - from Saxony, Germany - working on primedocs.io. Microsoft MVP & Web Geek.
Other Projects: KnowYourStack.com | ExpensiveMeeting | EinKofferVollerReisen.de