Пример #1
0
            public async Task <SyntaxTree> ReplaceVars(SyntaxTree syntaxTree, MonoProxy proxy, MessageId msg_id, int scope_id, CancellationToken token)
            {
                CompilationUnitSyntax root = syntaxTree.GetCompilationUnitRoot();

                foreach (var var in variables)
                {
                    ClassDeclarationSyntax  classDeclaration = root.Members.ElementAt(0) as ClassDeclarationSyntax;
                    MethodDeclarationSyntax method           = classDeclaration.Members.ElementAt(0) as MethodDeclarationSyntax;

                    JToken value = await proxy.TryGetVariableValue(msg_id, scope_id, var.Identifier.Text, false, token);

                    if (value == null)
                    {
                        throw new Exception($"The name {var.Identifier.Text} does not exist in the current context");
                    }

                    values.Add(ConvertJSToCSharpType(value ["value"]));

                    var updatedMethod = method.AddParameterListParameters(
                        SyntaxFactory.Parameter(
                            SyntaxFactory.Identifier(var.Identifier.Text))
                        .WithType(SyntaxFactory.ParseTypeName(GetTypeFullName(value["value"]))));
                    root = root.ReplaceNode(method, updatedMethod);
                }
                syntaxTree = syntaxTree.WithRootAndOptions(root, syntaxTree.Options);
                return(syntaxTree);
            }
        internal static async Task <string> CompileAndRunTheExpression(MonoProxy proxy, MessageId msg_id, int scope_id, string expression, CancellationToken token)
        {
            FindVariableNMethodCall findVarNMethodCall = new FindVariableNMethodCall();
            string     retString;
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(@"
				using System;
				public class CompileAndRunTheExpression
				{
					public string Evaluate()
					{
						return ("                         + expression + @").ToString(); 
					}
				}"                );

            FindThisExpression findThisExpression = new FindThisExpression(syntaxTree);
            var expressionTree = GetExpressionFromSyntaxTree(syntaxTree);

            findThisExpression.Visit(expressionTree);
            await findThisExpression.CheckIfIsProperty(proxy, msg_id, scope_id, token);

            syntaxTree = findThisExpression.syntaxTree;

            expressionTree = GetExpressionFromSyntaxTree(syntaxTree);
            findVarNMethodCall.Visit(expressionTree);

            syntaxTree = await findVarNMethodCall.ReplaceVars(syntaxTree, proxy, msg_id, scope_id, token);

            MetadataReference [] references = new MetadataReference []
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location)
            };

            CSharpCompilation compilation = CSharpCompilation.Create(
                "compileAndRunTheExpression",
                syntaxTrees: new [] { syntaxTree },
                references: references,
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var ms = new MemoryStream()) {
                EmitResult result = compilation.Emit(ms);
                ms.Seek(0, SeekOrigin.Begin);
                Assembly assembly = Assembly.Load(ms.ToArray());
                Type     type     = assembly.GetType("CompileAndRunTheExpression");
                object   obj      = Activator.CreateInstance(type);
                var      ret      = type.InvokeMember("Evaluate",
                                                      BindingFlags.Default | BindingFlags.InvokeMethod,
                                                      null,
                                                      obj,
                                                      //new object [] { 10 }
                                                      findVarNMethodCall.values.ToArray());
                retString = ret.ToString();
            }
            return(retString);
        }
Пример #3
0
            public async Task CheckIfIsProperty(MonoProxy proxy, MessageId msg_id, int scope_id, CancellationToken token)
            {
                foreach (var var in thisExpressions)
                {
                    JToken value = await proxy.TryGetVariableValue(msg_id, scope_id, var, true, token);

                    if (value == null)
                    {
                        throw new Exception($"The property {var} does not exist in the current context");
                    }
                }
            }
Пример #4
0
        public async Task LaunchAndServe(ProcessStartInfo psi, HttpContext context, Func <string, Task <string> > extract_conn_url)
        {
            if (!context.WebSockets.IsWebSocketRequest)
            {
                context.Response.StatusCode = 400;
                return;
            }

            var tcs = new TaskCompletionSource <string> ();

            var proc = Process.Start(psi);

            try {
                proc.ErrorDataReceived += (sender, e) => {
                    var str = e.Data;
                    Console.WriteLine($"stderr: {str}");

                    if (str.Contains("listening on", StringComparison.Ordinal))
                    {
                        var res = str.Substring(str.IndexOf("ws://", StringComparison.Ordinal));
                        if (res != null)
                        {
                            tcs.TrySetResult(res);
                        }
                    }
                };

                proc.OutputDataReceived += (sender, e) => {
                    Console.WriteLine($"stdout: {e.Data}");
                };

                proc.BeginErrorReadLine();
                proc.BeginOutputReadLine();

                if (await Task.WhenAny(tcs.Task, Task.Delay(2000)) != tcs.Task)
                {
                    Console.WriteLine("Didnt get the con string after 2s.");
                    throw new Exception("node.js timedout");
                }
                var line    = await tcs.Task;
                var con_str = extract_conn_url != null ? await extract_conn_url(line) : line;

                Console.WriteLine($"lauching proxy for {con_str}");

                var proxy      = new MonoProxy();
                var browserUri = new Uri(con_str);
                var ideSocket  = await context.WebSockets.AcceptWebSocketAsync();

                await proxy.Run(browserUri, ideSocket);

                Console.WriteLine("Proxy done");
            } catch (Exception e) {
                Console.WriteLine("got exception {0}", e);
            } finally {
                proc.CancelErrorRead();
                proc.CancelOutputRead();
                proc.Kill();
                proc.WaitForExit();
                proc.Close();
            }
        }
Пример #5
0
 public DebuggerProxy(ILoggerFactory loggerFactory)
 {
     proxy = new MonoProxy(loggerFactory);
 }