コード例 #1
0
ファイル: CodeGenCapturing.cs プロジェクト: XieShuquan/roslyn
 public CaptureContext Clone()
 {
     var fields = VariablesByScope[0];
     var newCtx = new CaptureContext();
     newCtx.VariablesByScope.Add(fields);
     newCtx.VariablesByScope.AddRange(
         this.VariablesByScope
         .Skip(1)
         .Select(list => list == null ? null : new List<string>(list)));
     newCtx.CaptureNameIndex = this.CaptureNameIndex;
     return newCtx;
 }
コード例 #2
0
ファイル: CodeGenCapturing.cs プロジェクト: zygygy/roslyn
            public CaptureContext Clone()
            {
                var fields = VariablesByScope[0];
                var newCtx = new CaptureContext();

                newCtx.VariablesByScope.Add(fields);
                newCtx.VariablesByScope.AddRange(
                    this.VariablesByScope
                    .Skip(1)
                    .Select(list => list == null ? null : new List <string>(list)));
                newCtx.CaptureNameIndex = this.CaptureNameIndex;
                return(newCtx);
            }
コード例 #3
0
    static void Main(string[] args)
    {
        var writers = new List <Writer>();
        var ctx     = new CaptureContext();

        for (ctx.i = 0; ctx.i < 10; ctx.i++)
        {
            writers.Add(ctx.DoStuff);
        }

        foreach (Writer writer in writers)
        {
            writer();
        }
    }
コード例 #4
0
        public void ExpressionGeneratorTest01()
        {
            var ctx = new CaptureContext(1);

            int[] captures = { 1 }; // Capture 1 var at the 0 depth
            var   expr     = MakeCaptureExpression(captures, ctx);

            Assert.Equal("field_0", expr);
            VerifyContext(new[] { new[] { "field_0" } }, ctx.VariablesByScope);

            ctx      = new CaptureContext(3);
            captures = new[] { 3 }; // Capture 3 vars at 0 depth
            expr     = MakeCaptureExpression(captures, ctx);
            Assert.Equal("field_0 + field_1 + field_2", expr);
            VerifyContext(
                new[] { new[] { "field_0", "field_1", "field_2" } },
                ctx.VariablesByScope
                );

            ctx      = new CaptureContext(3);
            captures = new[] { 1, 1, 1 }; // Capture 1 var at each of 3 depths
            expr     = MakeCaptureExpression(captures, ctx);
            Assert.Equal("field_2 + captureVar_0 + captureVar_1", expr);
            VerifyContext(
                new[]
            {
                new[] { "field_0", "field_1", "field_2" },
                new[] { "captureVar_0" },
                new[] { "captureVar_1" }
            },
                ctx.VariablesByScope
                );

            void VerifyContext(
                IList <IEnumerable <string> > expectedCtx,
                List <IList <string> > actualCtx
                )
            {
                Assert.Equal(expectedCtx.Count, ctx.VariablesByScope.Count);
                for (int depth = 0; depth < expectedCtx.Count; depth++)
                {
                    AssertEx.Equal(expectedCtx[depth], ctx.VariablesByScope[depth]);
                }
            }
        }
コード例 #5
0
ファイル: CodeGenCapturing.cs プロジェクト: zygygy/roslyn
        private static string MakeCaptureExpression(IList <int> varsToCapture, CaptureContext ctx)
        {
            var varNames = new List <string>();

            for (int varDepth = 0; varDepth < varsToCapture.Count; varDepth++)
            {
                var variablesByScope = ctx.VariablesByScope;
                // Do we have any variables in this scope depth?
                // If not, initialize an empty list
                if (variablesByScope.Count <= varDepth)
                {
                    variablesByScope.Add(new List <string>());
                }

                var varsAtCurrentDepth = variablesByScope[varDepth];

                int numToCapture     = varsToCapture[varDepth];
                int numVarsAvailable = variablesByScope[varDepth].Count;
                // If we have enough variables to capture in the context
                // just add them
                if (numVarsAvailable >= numToCapture)
                {
                    // Capture the last variables added since if there are more
                    // vars in the context than the max vars to capture we'll never
                    // have code coverage of the newest vars added to the context.
                    varNames.AddRange(varsAtCurrentDepth
                                      .Skip(numVarsAvailable - numToCapture)
                                      .Take(numToCapture));
                }
                else
                {
                    // Not enough variables in the context -- add more
                    for (int i = 0; i < numToCapture - numVarsAvailable; i++)
                    {
                        varsAtCurrentDepth.Add($"captureVar_{ctx.CaptureNameIndex++}");
                    }

                    varNames.AddRange(varsAtCurrentDepth);
                }
            }

            return(varNames.Count == 0 ? "0" : string.Join(" + ", varNames));
        }
コード例 #6
0
ファイル: CodeGenCapturing.cs プロジェクト: XieShuquan/roslyn
        private static string MakeCaptureExpression(IList<int> varsToCapture, CaptureContext ctx)
        {
            var varNames = new List<string>();
            for (int varDepth = 0; varDepth < varsToCapture.Count; varDepth++)
            {
                var variablesByScope = ctx.VariablesByScope;
                // Do we have any variables in this scope depth?
                // If not, initialize an empty list
                if (variablesByScope.Count <= varDepth)
                {
                    variablesByScope.Add(new List<string>());
                }

                var varsAtCurrentDepth = variablesByScope[varDepth];

                int numToCapture = varsToCapture[varDepth];
                int numVarsAvailable = variablesByScope[varDepth].Count;
                // If we have enough variables to capture in the context
                // just add them
                if (numVarsAvailable >= numToCapture)
                {
                    // Capture the last variables added since if there are more
                    // vars in the context than the max vars to capture we'll never
                    // have code coverage of the newest vars added to the context.
                    varNames.AddRange(varsAtCurrentDepth
                        .Skip(numVarsAvailable - numToCapture)
                        .Take(numToCapture));
                }
                else
                {
                    // Not enough variables in the context -- add more
                    for (int i = 0; i < numToCapture - numVarsAvailable; i++)
                    {
                        varsAtCurrentDepth.Add($"captureVar_{ctx.CaptureNameIndex++}");
                    }

                    varNames.AddRange(varsAtCurrentDepth);
                }
            }

            return varNames.Count == 0 ? "0" : string.Join(" + ", varNames);
        }
コード例 #7
0
ファイル: CodeGenCapturing.cs プロジェクト: XieShuquan/roslyn
 public MethodInfo(int MaxCaptures)
 {
     LocalFuncs = new List<IList<string>>();
     CaptureContext = new CaptureContext(MaxCaptures);
 }
コード例 #8
0
ファイル: CodeGenCapturing.cs プロジェクト: XieShuquan/roslyn
        public void ExpressionGeneratorTest01()
        {
            var ctx = new CaptureContext(1);
            int[] captures = { 1 }; // Capture 1 var at the 0 depth
            var expr = MakeCaptureExpression(captures, ctx);
            Assert.Equal("field_0", expr);
            VerifyContext(new[]
            {
                new[] { "field_0"}
            }, ctx.VariablesByScope);

            ctx = new CaptureContext(3);
            captures = new[] { 3 }; // Capture 3 vars at 0 depth
            expr = MakeCaptureExpression(captures, ctx);
            Assert.Equal("field_0 + field_1 + field_2", expr);
            VerifyContext(new[]
            {
                new[] { "field_0", "field_1", "field_2" }
            }, ctx.VariablesByScope);

            ctx = new CaptureContext(3);
            captures = new[] { 1, 1, 1 }; // Capture 1 var at each of 3 depths
            expr = MakeCaptureExpression(captures, ctx);
            Assert.Equal("field_2 + captureVar_0 + captureVar_1", expr);
            VerifyContext(new[]
            {
                new[] { "field_0", "field_1", "field_2"},
                new[] { "captureVar_0"},
                new[] { "captureVar_1"}
            }, ctx.VariablesByScope);

            void VerifyContext(IList<IEnumerable<string>> expectedCtx, List<IList<string>> actualCtx)
            {
                Assert.Equal(expectedCtx.Count, ctx.VariablesByScope.Count);
                for (int depth = 0; depth < expectedCtx.Count; depth++)
                {
                    AssertEx.Equal(expectedCtx[depth], ctx.VariablesByScope[depth]);
                }
            }
        }