コード例 #1
0
        protected void AssertCorrect(string csharp, string expected, INamingConventionResolver namingConvention = null, IRuntimeLibrary runtimeLibrary = null, bool addSkeleton = true, bool referenceSystemCore = false)
        {
            CompileMethod(csharp, namingConvention: namingConvention ?? new MockNamingConventionResolver {
                GetPropertySemantics = p => {
                    if (p.DeclaringType.Kind == TypeKind.Anonymous || new Regex("^F[0-9]*$").IsMatch(p.Name) || (p.DeclaringType.FullName == "System.Array" && p.Name == "Length"))
                        return PropertyScriptSemantics.Field("$" + p.Name);
                    else
                        return PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_$" + p.Name), MethodScriptSemantics.NormalMethod("set_$" + p.Name));
                },
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name),
                GetEventSemantics  = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_$" + e.Name), MethodScriptSemantics.NormalMethod("remove_$" + e.Name)),
            }, runtimeLibrary: runtimeLibrary, addSkeleton: addSkeleton, referenceSystemCore: referenceSystemCore);
            string actual = OutputFormatter.Format(CompiledMethod, true);

            int begin = actual.IndexOf("// BEGIN");
            if (begin > -1) {
                while (begin < (actual.Length - 1) && actual[begin - 1] != '\n')
                    begin++;
                actual = actual.Substring(begin);
            }

            int end = actual.IndexOf("// END");
            if (end >= 0) {
                while (end >= 0 && actual[end] != '\n')
                    end--;
                actual = actual.Substring(0, end + 1);
            }
            Assert.That(actual.Replace("\r\n", "\n"), Is.EqualTo(expected.Replace("\r\n", "\n")));
        }
コード例 #2
0
        protected void CompileMethod(string source, INamingConventionResolver namingConvention = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, string methodName = "M", bool addSkeleton = true, bool referenceSystemCore = false)
        {
            Compile(new[] { addSkeleton ? "using System; class C { " + source + "}" : source }, namingConvention, runtimeLibrary, errorReporter, (m, res, mc) => {
                if (m.Name == methodName) {
                    Method = m;
                    MethodCompiler = mc;
                    CompiledMethod = res;
                }
            }, referenceSystemCore: referenceSystemCore);

            Assert.That(Method, Is.Not.Null, "Method " + methodName + " was not compiled");
        }
コード例 #3
0
        protected void Compile(IEnumerable<string> sources, INamingConventionResolver namingConvention = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, Action<IMethod, JsFunctionDefinitionExpression, MethodCompiler> methodCompiled = null, IList<string> defineConstants = null, bool allowUnsupportedConstructs = true, bool referenceSystemCore = false)
        {
            var sourceFiles = sources.Select((s, i) => new MockSourceFile("File" + i + ".cs", s)).ToList();
            bool defaultErrorHandling = false;
            if (errorReporter == null) {
                defaultErrorHandling = true;
                errorReporter = new MockErrorReporter(true);
            }

            var compiler = new Saltarelle.Compiler.Compiler.Compiler(namingConvention ?? new MockNamingConventionResolver(), runtimeLibrary ?? new MockRuntimeLibrary(), errorReporter) { AllowUnsupportedConstructs = allowUnsupportedConstructs };
            if (methodCompiled != null)
                compiler.MethodCompiled += methodCompiled;

            var references = referenceSystemCore ? new[] { Common.Mscorlib, Common.Linq } : new[] { Common.Mscorlib };
            var c = compiler.CreateCompilation(sourceFiles, references, defineConstants);
            CompiledTypes = compiler.Compile(c).AsReadOnly();
            if (defaultErrorHandling) {
                ((MockErrorReporter)errorReporter).AllMessagesText.Should().BeEmpty("Compile should not generate errors");
            }
        }
コード例 #4
0
        internal StatementCompiler(INamingConventionResolver namingConvention, IErrorReporter errorReporter, ICompilation compilation, CSharpAstResolver resolver, IDictionary<IVariable, VariableData> variables, IDictionary<LambdaResolveResult, NestedFunctionData> nestedFunctions, IRuntimeLibrary runtimeLibrary, string thisAlias, ISet<string> usedVariableNames, NestedFunctionContext nestedFunctionContext, IMethod methodBeingCompiled, ISet<string> definedSymbols, ExpressionCompiler expressionCompiler, SharedValue<int> nextLabelIndex, IVariable currentVariableForRethrow, IDictionary<object, string> currentGotoCaseMap)
        {
            _namingConvention           = namingConvention;
            _errorReporter              = errorReporter;
            _compilation                = compilation;
            _resolver                   = resolver;
            _variables                  = variables;
            _nestedFunctions            = nestedFunctions;
            _runtimeLibrary             = runtimeLibrary;
            _thisAlias                  = thisAlias;
            _usedVariableNames          = usedVariableNames;
            _nestedFunctionContext      = nestedFunctionContext;
            _methodBeingCompiled        = methodBeingCompiled;
            _definedSymbols             = definedSymbols;
            _currentVariableForRethrow  = currentVariableForRethrow;
            _currentGotoCaseMap         = currentGotoCaseMap;

            _nextLabelIndex             = nextLabelIndex ?? new SharedValue<int>(1);

            _expressionCompiler         = expressionCompiler ?? new ExpressionCompiler(compilation, namingConvention, runtimeLibrary, errorReporter, variables, nestedFunctions, v => CreateTemporaryVariable(v, new DomRegion(_filename, _location, _location)), c => new StatementCompiler(_namingConvention, _errorReporter, _compilation, _resolver, _variables, _nestedFunctions, _runtimeLibrary, thisAlias, _usedVariableNames, c, _methodBeingCompiled, _definedSymbols), thisAlias, nestedFunctionContext, null, _methodBeingCompiled);
            _result                     = new List<JsStatement>();
        }
        protected void AssertCorrectForBulkOperators(string csharp, string expected, bool includeEqualsAndNotEquals, INamingConventionResolver namingConvention = null)
        {
            // Bulk operators are all except for division, shift right, coalesce and the logical operators.
            foreach (var op in new[] { "*", "%", "+", "-", "<<", "<", ">", "<=", ">=", "&", "^", "|" }) {
                var jsOp = (op == "==" || op == "!=" ? op + "=" : op);	// Script should use strict equals (===) rather than normal equals (==)
                AssertCorrect(csharp.Replace("+", op), expected.Replace("+", jsOp), namingConvention);
            }

            if (includeEqualsAndNotEquals) {
                AssertCorrect(csharp.Replace("+", "=="), expected.Replace("+", "==="), namingConvention);
                AssertCorrect(csharp.Replace("+", "!="), expected.Replace("+", "!=="), namingConvention);
            }
        }
コード例 #6
0
 public StatementCompiler(INamingConventionResolver namingConvention, IErrorReporter errorReporter, ICompilation compilation, CSharpAstResolver resolver, IDictionary<IVariable, VariableData> variables, IDictionary<LambdaResolveResult, NestedFunctionData> nestedFunctions, IRuntimeLibrary runtimeLibrary, string thisAlias, ISet<string> usedVariableNames, NestedFunctionContext nestedFunctionContext, IMethod methodBeingCompiled, ISet<string> definedSymbols)
     : this(namingConvention, errorReporter, compilation, resolver, variables, nestedFunctions, runtimeLibrary, thisAlias, usedVariableNames, nestedFunctionContext, methodBeingCompiled, definedSymbols, null, null, null, null)
 {
 }
コード例 #7
0
        protected void Compile(string source, INamingConventionResolver namingConvention = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, bool useFirstConstructor = false)
        {
            Compile(new[] { source }, namingConvention, runtimeLibrary, errorReporter, (m, res, mc) => {
                if (m.IsConstructor && (m.Attributes.Any() || useFirstConstructor)) {
                    Constructor = m;
                    MethodCompiler = mc;
                    CompiledConstructor = res;
                }
            });

            Assert.That(Constructor, Is.Not.Null, "No constructors with attributes were compiled.");
        }
コード例 #8
0
 protected void AssertCorrectForBulkOperators(string csharp, string expected, INamingConventionResolver namingConvention = null, bool addSkeleton = true)
 {
     // Bulk operators are all except for division and shift right.
     foreach (var op in new[] { "+", "*", "%", "-", "<<", "&", "|", "^" }) {
         AssertCorrect(csharp.Replace("+", op), expected.Replace("+", op), namingConvention, addSkeleton: addSkeleton);
     }
 }
コード例 #9
0
 protected void AssertCorrect(string csharp, string expected, INamingConventionResolver namingConvention = null, bool useFirstConstructor = false)
 {
     Compile(csharp, namingConvention, useFirstConstructor: useFirstConstructor);
     string actual = OutputFormatter.Format(CompiledConstructor, allowIntermediates: true);
     Assert.That(actual.Replace("\r\n", "\n"), Is.EqualTo(expected.Replace("\r\n", "\n")));
 }
コード例 #10
0
 public VariableGatherer(CSharpAstResolver resolver, INamingConventionResolver namingConvention, IErrorReporter errorReporter)
 {
     _resolver = resolver;
     _namingConvention = namingConvention;
     _errorReporter = errorReporter;
 }
コード例 #11
0
 protected void AssertCorrectForBoth(string csharp, string expected, INamingConventionResolver namingConvention = null)
 {
     AssertCorrect(csharp, expected, namingConvention);
     AssertCorrect(csharp.Replace("+", "-"), expected.Replace("+", "-"), namingConvention);
 }
コード例 #12
0
 private static void FindUsedUnusableTypes(IEnumerable<IType> types, INamingConventionResolver namingConvention, HashSet<ITypeDefinition> result)
 {
     foreach (var t in types) {
         if (t is ITypeDefinition) {
             if (namingConvention.GetTypeSemantics((ITypeDefinition)t).Type == TypeScriptSemantics.ImplType.NotUsableFromScript)
                 result.Add((ITypeDefinition)t);
         }
         else if (t is ParameterizedType) {
             var pt = (ParameterizedType)t;
             FindUsedUnusableTypes(new[] { pt.GetDefinition() }.Concat(pt.TypeArguments), namingConvention, result);
         }
     }
 }
コード例 #13
0
 public static IEnumerable<ITypeDefinition> FindUsedUnusableTypes(IEnumerable<IType> types, INamingConventionResolver namingConvention)
 {
     var s = new HashSet<ITypeDefinition>();
     FindUsedUnusableTypes(types, namingConvention, s);
     return s;
 }
コード例 #14
0
 public Compiler(INamingConventionResolver namingConvention, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter)
 {
     _namingConvention = namingConvention;
     _errorReporter    = errorReporter;
     _runtimeLibrary   = runtimeLibrary;
 }