protected void AssertCorrect(string csharp, string expected, IMetadataImporter metadataImporter = null, IRuntimeLibrary runtimeLibrary = null, bool addSkeleton = true, bool referenceSystemCore = false, string methodName = "M")
        {
            CompileMethod(csharp, metadataImporter: metadataImporter ?? new MockMetadataImporter {
                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, methodName: methodName, 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")));
        }
		private string Process(IList<JsStatement> stmts, IAssembly mainAssembly, IMetadataImporter metadata = null, INamer namer = null) {
			var compilation = new Mock<ICompilation>();
			compilation.SetupGet(_ => _.MainAssembly).Returns(mainAssembly);
			var obj = new Linker(metadata ?? new MockMetadataImporter(), namer ?? new MockNamer(), compilation.Object);
			var processed = obj.Process(stmts);
			return string.Join("", processed.Select(s => OutputFormatter.Format(s, allowIntermediates: false)));
		}
Beispiel #3
0
 private static void FindTypeUsageErrors(IEnumerable <IType> types, IMetadataImporter metadataImporter, HashSet <ITypeDefinition> usedUnusableTypes, HashSet <ITypeDefinition> mutableValueTypesBoundToTypeArguments)
 {
     foreach (var t in types)
     {
         if (t is ITypeDefinition)
         {
             if (metadataImporter.GetTypeSemantics((ITypeDefinition)t).Type == TypeScriptSemantics.ImplType.NotUsableFromScript)
             {
                 usedUnusableTypes.Add((ITypeDefinition)t);
             }
         }
         else if (t is ParameterizedType)
         {
             var pt = (ParameterizedType)t;
             foreach (var ta in pt.TypeArguments)
             {
                 if (ta.Kind == TypeKind.Struct && metadataImporter.GetTypeSemantics(ta.GetDefinition()).Type == TypeScriptSemantics.ImplType.MutableValueType)
                 {
                     mutableValueTypesBoundToTypeArguments.Add(ta.GetDefinition());
                 }
             }
             FindTypeUsageErrors(new[] { pt.GetDefinition() }.Concat(pt.TypeArguments), metadataImporter, usedUnusableTypes, mutableValueTypesBoundToTypeArguments);
         }
     }
 }
        private void Prepare(string source, Action preparer)
        {
            IProjectContent project = new CSharpProjectContent();
            var             parser  = new CSharpParser();

            using (var rdr = new StringReader(source)) {
                var pf = new CSharpUnresolvedFile {
                    FileName = "File.cs"
                };
                var syntaxTree = parser.Parse(rdr, pf.FileName);
                syntaxTree.AcceptVisitor(new TypeSystemConvertVisitor(pf));
                project = project.AddOrUpdateFiles(pf);
            }
            project = project.AddAssemblyReferences(new[] { Files.Mscorlib });

            var compilation = project.CreateCompilation();

            AllTypes = compilation.MainAssembly.TopLevelTypeDefinitions.SelectMany(SelfAndNested).ToDictionary(t => t.ReflectionName);

            var er = new MockErrorReporter(true);
            var s  = new AttributeStore(compilation, er);

            Metadata = new MetadataImporter(er, compilation, s, new CompilerOptions());
            preparer();
            Metadata.Prepare(compilation.GetAllTypeDefinitions());
            Assert.That(er.AllMessages, Is.Empty, "Should not generate errrors");
        }
Beispiel #5
0
 public Compiler(IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter)
 {
     _metadataImporter = metadataImporter;
     _namer            = namer;
     _errorReporter    = errorReporter;
     _runtimeLibrary   = runtimeLibrary;
 }
Beispiel #6
0
        protected void AssertCorrect(string csharp, string expected, IMetadataImporter metadataImporter = null, bool useFirstConstructor = false)
        {
            Compile(csharp, metadataImporter, useFirstConstructor: useFirstConstructor);
            string actual = OutputFormatter.Format(CompiledConstructor, allowIntermediates: true);

            Assert.That(actual.Replace("\r\n", "\n"), Is.EqualTo(expected.Replace("\r\n", "\n")));
        }
        public static bool IsReflectable(IMember member, IMetadataImporter metadataImporter)
        {
            var ra = AttributeReader.ReadAttribute <ReflectableAttribute>(member);

            if (ra != null)
            {
                return(ra.Reflectable);
            }
            if (member.Attributes.Any(a => metadataImporter.GetTypeSemantics(a.AttributeType.GetDefinition()).Type == TypeScriptSemantics.ImplType.NormalType))
            {
                return(true);                   // Any scriptable attribute will cause reflectability (unless [Reflectable(false)] was specified.
            }
            if (member.DeclaringType.Kind != TypeKind.Anonymous)
            {
                var tdr = AttributeReader.ReadAttribute <DefaultMemberReflectabilityAttribute>(member.DeclaringTypeDefinition);
                if (tdr != null)
                {
                    return(IsMemberReflectable(member, tdr.DefaultReflectability));
                }

                var adr = AttributeReader.ReadAttribute <DefaultMemberReflectabilityAttribute>(member.ParentAssembly.AssemblyAttributes);
                if (adr != null)
                {
                    return(IsMemberReflectable(member, adr.DefaultReflectability));
                }
            }

            return(false);
        }
Beispiel #8
0
 public Compiler(IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter)
 {
     _metadataImporter = metadataImporter;
     _namer            = namer;
     _errorReporter    = errorReporter;
     _runtimeLibrary   = runtimeLibrary;
 }
Beispiel #9
0
        protected void Compile(IEnumerable <string> sources, IMetadataImporter metadataImporter = null, INamer namer = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, Action <IMethod, JsFunctionDefinitionExpression, MethodCompiler> methodCompiled = null, IList <string> defineConstants = null, bool allowUserDefinedStructs = true, IEnumerable <IAssemblyReference> references = null)
        {
            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(metadataImporter ?? new MockMetadataImporter(), namer ?? new MockNamer(), runtimeLibrary ?? new MockRuntimeLibrary(), errorReporter);

            compiler.AllowUserDefinedStructs = allowUserDefinedStructs;
            if (methodCompiled != null)
            {
                compiler.MethodCompiled += methodCompiled;
            }

            var c = PreparedCompilation.CreateCompilation(sourceFiles, references ?? new[] { Common.Mscorlib }, defineConstants);

            CompiledTypes = compiler.Compile(c).AsReadOnly();
            if (defaultErrorHandling)
            {
                ((MockErrorReporter)errorReporter).AllMessages.Should().BeEmpty("Compile should not generate errors");
            }
        }
Beispiel #10
0
 private IntroducedNamesGatherer(IMetadataImporter metadataImporter, IAssembly mainAssembly, IAttributeStore attributeStore)
 {
     _metadataImporter = metadataImporter;
     _mainAssembly     = mainAssembly;
     _attributeStore   = attributeStore;
     _mainModuleName   = MetadataUtils.GetModuleName(_mainAssembly, attributeStore);
 }
Beispiel #11
0
 public Linker(IMetadataImporter metadataImporter, INamer namer, IAttributeStore attributeStore, ICompilation compilation)
 {
     _metadataImporter = metadataImporter;
     _namer            = namer;
     _attributeStore   = attributeStore;
     _compilation      = compilation;
 }
Beispiel #12
0
 public DefaultRuntimeContext(ITypeDefinition currentType, IMetadataImporter metadataImporter, IErrorReporter errorReporter, INamer namer)
 {
     _currentType      = currentType;
     _metadataImporter = metadataImporter;
     _errorReporter    = errorReporter;
     _namer            = namer;
 }
 public Compiler(IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter, bool allowUserDefinedStructs)
 {
     _metadataImporter        = metadataImporter;
     _namer                   = namer;
     _errorReporter           = errorReporter;
     _runtimeLibrary          = runtimeLibrary;
     _allowUserDefinedStructs = allowUserDefinedStructs;
 }
		public RuntimeLibrary(IMetadataImporter metadataImporter, IErrorReporter errorReporter, ICompilation compilation, INamer namer) {
			_metadataImporter = metadataImporter;
			_errorReporter = errorReporter;
			_compilation = compilation;
			_namer = namer;
			_omitDowncasts = MetadataUtils.OmitDowncasts(compilation);
			_omitNullableChecks = MetadataUtils.OmitNullableChecks(compilation);
		}
Beispiel #15
0
 protected void AssertCorrectForBulkOperators(string csharp, string expected, IMetadataImporter metadataImporter = 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), metadataImporter, addSkeleton: addSkeleton);
     }
 }
Beispiel #16
0
 internal void SetMetadataImporter(IMetadataImporter importer)
 {
     if (this.metadata_importer != null)
     {
         throw new InvalidOperationException();
     }
     this.metadata_importer = importer;
 }
 public Compiler(IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter, bool allowUserDefinedStructs)
 {
     _metadataImporter        = metadataImporter;
     _namer                   = namer;
     _errorReporter           = errorReporter;
     _runtimeLibrary          = runtimeLibrary;
     _allowUserDefinedStructs = allowUserDefinedStructs;
 }
		private string Process(IList<JsStatement> stmts, IAssembly[] assemblies, IMetadataImporter metadata = null, INamer namer = null) {
			var compilation = new Mock<ICompilation>();
			compilation.SetupGet(_ => _.MainAssembly).Returns(assemblies[0]);
			compilation.SetupGet(_ => _.Assemblies).Returns(assemblies);
			var s = new AttributeStore(compilation.Object, new MockErrorReporter());
			var obj = new Linker(metadata ?? new MockMetadataImporter(), namer ?? new MockNamer(), s, compilation.Object);
			var processed = obj.Process(stmts);
			return OutputFormatter.Format(processed, allowIntermediates: false);
		}
 public MethodCompiler(IMetadataImporter metadataImporter, INamer namer, IErrorReporter errorReporter, ICompilation compilation, CSharpAstResolver resolver, IRuntimeLibrary runtimeLibrary)
 {
     _metadataImporter = metadataImporter;
     _namer            = namer;
     _errorReporter    = errorReporter;
     _compilation      = compilation;
     _resolver         = resolver;
     _runtimeLibrary   = runtimeLibrary;
 }
 public RuntimeLibrary(IMetadataImporter metadataImporter, IErrorReporter errorReporter, ICompilation compilation, INamer namer)
 {
     _metadataImporter   = metadataImporter;
     _errorReporter      = errorReporter;
     _compilation        = compilation;
     _namer              = namer;
     _omitDowncasts      = MetadataUtils.OmitDowncasts(compilation);
     _omitNullableChecks = MetadataUtils.OmitNullableChecks(compilation);
 }
Beispiel #21
0
        public static void Prepare(this IMetadataImporter md, IEnumerable <ITypeDefinition> types)
        {
            var l = types.ToList();

            foreach (var t in TopologicalSorter.TopologicalSort(l, l.SelectMany(GetBaseAndOuterTypeDefinitions, Edge.Create)))
            {
                md.Prepare(t);
            }
        }
Beispiel #22
0
 private ImportVisitor(IMetadataImporter metadataImporter, INamer namer, IAssembly mainAssembly, HashSet <string> usedSymbols)
 {
     _metadataImporter = metadataImporter;
     _namer            = namer;
     _mainModuleName   = MetadataUtils.GetModuleName(mainAssembly);
     _mainAssembly     = mainAssembly;
     _usedSymbols      = usedSymbols;
     _moduleAliases    = new Dictionary <string, string>();
 }
 public MethodCompiler(IMetadataImporter metadataImporter, INamer namer, IErrorReporter errorReporter, ICompilation compilation, CSharpAstResolver resolver, IRuntimeLibrary runtimeLibrary, ISet <string> definedSymbols)
 {
     _metadataImporter = metadataImporter;
     _namer            = namer;
     _errorReporter    = errorReporter;
     _compilation      = compilation;
     _resolver         = resolver;
     _runtimeLibrary   = runtimeLibrary;
     _definedSymbols   = definedSymbols;
 }
        private string Process(IList <JsStatement> stmts, IAssembly mainAssembly, IMetadataImporter metadata = null, INamer namer = null)
        {
            var compilation = new Mock <ICompilation>();

            compilation.SetupGet(_ => _.MainAssembly).Returns(mainAssembly);
            var obj       = new Linker(metadata ?? new MockMetadataImporter(), namer ?? new MockNamer(), compilation.Object);
            var processed = obj.Process(stmts);

            return(string.Join("", processed.Select(s => OutputFormatter.Format(s, allowIntermediates: false))));
        }
		protected void CompileMethod(string source, IMetadataImporter metadataImporter = null, INamer namer = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, string methodName = "M", bool addSkeleton = true, IEnumerable<IAssemblyReference> references = null, bool allowUserDefinedStructs = false) {
			Compile(new[] { addSkeleton ? "using System; class C { " + source + "}" : source }, metadataImporter: metadataImporter, namer: namer, runtimeLibrary: runtimeLibrary, errorReporter: errorReporter, methodCompiled: (m, res, mc) => {
				if (m.Name == methodName) {
					Method = m;
					MethodCompiler = mc;
					CompiledMethod = res;
				}
			}, references: references, allowUserDefinedStructs: allowUserDefinedStructs);

			Assert.That(Method, Is.Not.Null, "Method " + methodName + " was not compiled");
		}
Beispiel #26
0
 private ImportVisitor(IMetadataImporter metadataImporter, INamer namer, IAttributeStore attributeStore, IAssembly mainAssembly, HashSet <string> usedSymbols, JsExpression currentAssembly)
 {
     _metadataImporter = metadataImporter;
     _namer            = namer;
     _attributeStore   = attributeStore;
     _mainModuleName   = MetadataUtils.GetModuleName(mainAssembly, attributeStore);
     _mainAssembly     = mainAssembly;
     _usedSymbols      = usedSymbols;
     _currentAssembly  = currentAssembly;
     _moduleAliases    = new Dictionary <string, string>();
 }
		protected void Compile(string source, IMetadataImporter metadataImporter = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, bool useFirstConstructor = false) {
			Compile(new[] { source }, metadataImporter: metadataImporter, runtimeLibrary: runtimeLibrary, errorReporter: errorReporter, methodCompiled: (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.");
		}
Beispiel #28
0
		private static void FindUsedUnusableTypes(IEnumerable<IType> types, IMetadataImporter metadataImporter, HashSet<ITypeDefinition> result) {
			foreach (var t in types) {
				if (t is ITypeDefinition) {
					if (metadataImporter.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), metadataImporter, result);
				}
			}
		}
        private string Process(IList <JsStatement> stmts, IAssembly[] assemblies, IMetadataImporter metadata = null, INamer namer = null)
        {
            var compilation = new Mock <ICompilation>();

            compilation.SetupGet(_ => _.MainAssembly).Returns(assemblies[0]);
            compilation.SetupGet(_ => _.Assemblies).Returns(assemblies);
            var s         = new AttributeStore(compilation.Object, new MockErrorReporter());
            var obj       = new Linker(metadata ?? new MockMetadataImporter(), namer ?? new MockNamer(), s, compilation.Object);
            var processed = obj.Process(stmts);

            return(OutputFormatter.Format(processed, allowIntermediates: false));
        }
		protected void AssertCorrectForBulkOperators(string csharp, string expected, bool includeEqualsAndNotEquals, IMetadataImporter metadataImporter = 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), metadataImporter);
			}

			if (includeEqualsAndNotEquals) {
				AssertCorrect(csharp.Replace("+", "=="), expected.Replace("+", "==="), metadataImporter);
				AssertCorrect(csharp.Replace("+", "!="), expected.Replace("+", "!=="), metadataImporter);
			}
		}
Beispiel #31
0
        protected void CompileMethod(string source, IMetadataImporter metadataImporter = null, INamer namer = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, string methodName = "M", bool addSkeleton = true, bool referenceSystemCore = false, bool allowUserDefinedStructs = false)
        {
            Compile(new[] { addSkeleton ? "using System; class C { " + source + "}" : source }, metadataImporter: metadataImporter, namer: namer, runtimeLibrary: runtimeLibrary, errorReporter: errorReporter, methodCompiled: (m, res, mc) => {
                if (m.Name == methodName)
                {
                    Method         = m;
                    MethodCompiler = mc;
                    CompiledMethod = res;
                }
            }, referenceSystemCore: referenceSystemCore, allowUserDefinedStructs: allowUserDefinedStructs);

            Assert.That(Method, Is.Not.Null, "Method " + methodName + " was not compiled");
        }
        protected void CompileMethod(string source, IMetadataImporter metadataImporter = null, INamer namer = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, string methodName = "M", bool addSkeleton = true, IEnumerable <IAssemblyReference> references = null)
        {
            Compile(new[] { addSkeleton ? "using System; class C { " + source + "}" : source }, metadataImporter: metadataImporter, namer: namer, runtimeLibrary: runtimeLibrary, errorReporter: errorReporter, methodCompiled: (m, res, mc) => {
                if (m.Name == methodName)
                {
                    Method         = m;
                    MethodCompiler = mc;
                    CompiledMethod = res;
                }
            }, references: references);

            Assert.That(Method, Is.Not.Null, "Method " + methodName + " was not compiled");
        }
		protected void AssertCorrectConstructor(string source, string expected, string className, IMetadataImporter metadataImporter = null) {
			JsExpression compiledConstructor = null;
			Compile(new[] { source }, methodCompiled: (m, res, mc) => {
				if (m.IsConstructor && m.DeclaringType.FullName == className) {
					compiledConstructor = res;
				}
			}, metadataImporter: metadataImporter);

			Assert.That(compiledConstructor, Is.Not.Null, "No constructor was compiled.");

			string actual = OutputFormatter.Format(compiledConstructor, allowIntermediates: true);
			Assert.That(actual.Replace("\r\n", "\n"), Is.EqualTo(expected.Replace("\r\n", "\n")));
		}
Beispiel #34
0
        protected void Compile(string source, IMetadataImporter metadataImporter = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, bool useFirstConstructor = false)
        {
            Compile(new[] { source }, metadataImporter: metadataImporter, runtimeLibrary: runtimeLibrary, errorReporter: errorReporter, methodCompiled: (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.");
        }
Beispiel #35
0
        public OOPEmulator(ICompilation compilation, IMetadataImporter metadataImporter, IRuntimeLibrary runtimeLibrary, INamer namer, IErrorReporter errorReporter)
        {
            _compilation  = compilation;
            _systemScript = new JsTypeReferenceExpression(compilation.FindType(new FullTypeName("System.Script")).GetDefinition());
            _systemObject = new JsTypeReferenceExpression(compilation.FindType(KnownTypeCode.Object).GetDefinition());

            _metadataImporter = metadataImporter;
            _runtimeLibrary   = runtimeLibrary;
            _namer            = namer;
            _errorReporter    = errorReporter;
            _defaultReflectionRuntimeContext = new ReflectionRuntimeContext(false, _systemObject, _namer);
            _genericSpecializationReflectionRuntimeContext = new ReflectionRuntimeContext(true, _systemObject, _namer);
        }
		public ExpressionTreeBuilder(ICompilation compilation, IMetadataImporter metadataImporter, Func<IType, string> createTemporaryVariable, Func<IMethod, JsExpression, JsExpression[], ExpressionCompileResult> compileMethodCall, Func<IType, JsExpression> instantiateType, Func<IType, JsExpression> getDefaultValue, Func<IMember, JsExpression> getMember, Func<IVariable, JsExpression> createLocalReferenceExpression, JsExpression @this) {
			_compilation = compilation;
			_metadataImporter = metadataImporter;
			_expression = (ITypeDefinition)ReflectionHelper.ParseReflectionName(typeof(System.Linq.Expressions.Expression).FullName).Resolve(compilation);
			_createTemporaryVariable = createTemporaryVariable;
			_compileMethodCall = compileMethodCall;
			_instantiateType = instantiateType;
			_getDefaultValue = getDefaultValue;
			_getMember = getMember;
			_createLocalReferenceExpression = createLocalReferenceExpression;
			_this = @this;
			_allParameters = new Dictionary<IVariable, string>();
			_additionalStatements = new List<JsStatement>();
		}
Beispiel #37
0
            public static IList <JsStatement> Process(IMetadataImporter metadataImporter, INamer namer, ICompilation compilation, IList <JsStatement> statements)
            {
                var usedSymbols        = UsedSymbolsGatherer.Analyze(statements);
                var importer           = new ImportVisitor(metadataImporter, namer, compilation.MainAssembly, usedSymbols);
                var body               = statements.Select(s => importer.VisitStatement(s, null)).ToList();
                var moduleDependencies = importer._moduleAliases.Concat(MetadataUtils.GetAdditionalDependencies(compilation.MainAssembly));

                if (MetadataUtils.IsAsyncModule(compilation.MainAssembly))
                {
                    body.InsertRange(0, new[] { JsStatement.UseStrict, JsStatement.Var("exports", JsExpression.ObjectLiteral()) });
                    body.Add(JsStatement.Return(JsExpression.Identifier("exports")));

                    var pairs = new[] { new KeyValuePair <string, string>("mscorlib", namer.GetVariableName("_", usedSymbols)) }
                    .Concat(moduleDependencies.OrderBy(x => x.Key))
                    .ToList();

                    body = new List <JsStatement> {
                        JsExpression.Invocation(
                            JsExpression.Identifier("define"),
                            JsExpression.ArrayLiteral(pairs.Select(p => JsExpression.String(p.Key))),
                            JsExpression.FunctionDefinition(
                                pairs.Select(p => p.Value),
                                JsStatement.Block(body)
                                )
                            )
                    };
                }
                else if (moduleDependencies.Any())
                {
                    // If we require any module, we require mscorlib. This should work even if we are a leaf module that doesn't include any other module because our parent script will do the mscorlib require for us.
                    body.InsertRange(0, new[] { JsStatement.UseStrict, JsExpression.Invocation(JsExpression.Identifier("require"), JsExpression.String("mscorlib")) }
                                     .Concat(moduleDependencies
                                             .OrderBy(x => x.Key).OrderBy(x => x.Key)
                                             .Select(x => JsStatement.Var(
                                                         x.Value,
                                                         JsExpression.Invocation(
                                                             JsExpression.Identifier("require"),
                                                             JsExpression.String(x.Key))))
                                             .ToList()));
                }
                else
                {
                    body.Insert(0, JsStatement.UseStrict);
                    body = new List <JsStatement> {
                        JsExpression.Invocation(JsExpression.FunctionDefinition(new string[0], JsStatement.Block(body)))
                    };
                }

                return(body);
            }
Beispiel #38
0
		public static UnusableTypesResult FindGenericInstantiationErrors(IEnumerable<IType> typeArguments, IMetadataImporter metadataImporter) {
			if (!(typeArguments is ICollection<IType>))
				typeArguments = typeArguments.ToList();

			var usedUnusableTypes = new HashSet<ITypeDefinition>();
			var mutableValueTypesBoundToTypeArguments = new HashSet<ITypeDefinition>();

			foreach (var ta in typeArguments) {
				if (ta.Kind == TypeKind.Struct && metadataImporter.GetTypeSemantics(ta.GetDefinition()).Type == TypeScriptSemantics.ImplType.MutableValueType)
					mutableValueTypesBoundToTypeArguments.Add(ta.GetDefinition());
			}

			FindTypeUsageErrors(typeArguments, metadataImporter, usedUnusableTypes, mutableValueTypesBoundToTypeArguments);
			return new UnusableTypesResult(usedUnusableTypes.Count > 0 ? usedUnusableTypes.ToList<IType>() : (IList<IType>)EmptyList<IType>.Instance, mutableValueTypesBoundToTypeArguments.Count > 0 ? mutableValueTypesBoundToTypeArguments.ToList<IType>() : (IList<IType>)EmptyList<IType>.Instance);
		}
Beispiel #39
0
		private static void FindTypeUsageErrors(IEnumerable<IType> types, IMetadataImporter metadataImporter, HashSet<ITypeDefinition> usedUnusableTypes, HashSet<ITypeDefinition> mutableValueTypesBoundToTypeArguments) {
			foreach (var t in types) {
				if (t is ITypeDefinition) {
					if (metadataImporter.GetTypeSemantics((ITypeDefinition)t).Type == TypeScriptSemantics.ImplType.NotUsableFromScript)
						usedUnusableTypes.Add((ITypeDefinition)t);
				}
				else if (t is ParameterizedType) {
					var pt = (ParameterizedType)t;
					foreach (var ta in pt.TypeArguments) {
						if (ta.Kind == TypeKind.Struct && metadataImporter.GetTypeSemantics(ta.GetDefinition()).Type == TypeScriptSemantics.ImplType.MutableValueType)
							mutableValueTypesBoundToTypeArguments.Add(ta.GetDefinition());
					}
					FindTypeUsageErrors(new[] { pt.GetDefinition() }.Concat(pt.TypeArguments), metadataImporter, usedUnusableTypes, mutableValueTypesBoundToTypeArguments);
				}
			}
		}
		protected void Compile(IEnumerable<string> sources, IMetadataImporter metadataImporter = null, INamer namer = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, Action<IMethod, JsFunctionDefinitionExpression, MethodCompiler> methodCompiled = null, IList<string> defineConstants = null, IEnumerable<IAssemblyReference> references = null) {
			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(metadataImporter ?? new MockMetadataImporter(), namer ?? new MockNamer(), runtimeLibrary ?? new MockRuntimeLibrary(), errorReporter);
			if (methodCompiled != null)
				compiler.MethodCompiled += methodCompiled;

			var c = PreparedCompilation.CreateCompilation("x", sourceFiles, references ?? new[] { Common.Mscorlib }, defineConstants);
			CompiledTypes = compiler.Compile(c).AsReadOnly();
			if (defaultErrorHandling) {
				((MockErrorReporter)errorReporter).AllMessages.Should().BeEmpty("Compile should not generate errors");
			}
		}
 private static void FindUsedUnusableTypes(IEnumerable <IType> types, IMetadataImporter metadataImporter, HashSet <ITypeDefinition> result)
 {
     foreach (var t in types)
     {
         if (t is ITypeDefinition)
         {
             if (metadataImporter.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), metadataImporter, result);
         }
     }
 }
        protected void AssertCorrect(string csharp, string expected, IMetadataImporter metadataImporter = null, IRuntimeLibrary runtimeLibrary = null, bool addSkeleton = true, IEnumerable <IAssemblyReference> references = null, string methodName = "M", bool mutableValueTypes = false)
        {
            CompileMethod(csharp, metadataImporter: metadataImporter ?? new MockMetadataImporter {
                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)),
                GetTypeSemantics   = t => {
                    return(mutableValueTypes && t.Kind == TypeKind.Struct ? TypeScriptSemantics.MutableValueType(t.FullName) : TypeScriptSemantics.NormalType(t.FullName));
                }
            }, runtimeLibrary: runtimeLibrary, methodName: methodName, addSkeleton: addSkeleton, references: references);
            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")), "Expected:\n" + expected + "\n\nActual:\n" + actual);
        }
        private void Prepare(string source, IRuntimeLibrary runtimeLibrary = null, bool expectErrors = false, bool MinimizeNames = false)
        {
            IProjectContent project = new CSharpProjectContent();
            var             parser  = new CSharpParser();

            using (var rdr = new StringReader(source)) {
                var pf = new CSharpUnresolvedFile()
                {
                    FileName = "File.cs"
                };
                var syntaxTree = parser.Parse(rdr, pf.FileName);
                syntaxTree.AcceptVisitor(new TypeSystemConvertVisitor(pf));
                project = project.AddOrUpdateFiles(pf);
            }
            project = project.AddAssemblyReferences(new[] { Files.Mscorlib, Files.Web, Files.Knockout });

            _compilation = project.CreateCompilation();

            var options = new CompilerOptions {
                MinimizeScript = MinimizeNames
            };

            _errorReporter = new MockErrorReporter(!expectErrors);
            var s = new AttributeStore(_compilation, _errorReporter);

            s.RunAttributeCode();
            _metadata = new MetadataImporter(_errorReporter, _compilation, s, options);

            _metadata.Prepare(_compilation.GetAllTypeDefinitions());

            _allErrors = _errorReporter.AllMessages.ToList().AsReadOnly();
            if (expectErrors)
            {
                Assert.That(_allErrors, Is.Not.Empty, "Compile should have generated errors");
            }
            else
            {
                Assert.That(_allErrors, Is.Empty, "Compile should not generate errors");
            }

            var rtl = new RuntimeLibrary(_metadata, _errorReporter, _compilation, new Namer(), s);
        }
		private void Prepare(string source, Action preparer) {
			IProjectContent project = new CSharpProjectContent();
			var parser = new CSharpParser();

			using (var rdr = new StringReader(source)) {
				var pf = new CSharpUnresolvedFile("File.cs");
				var syntaxTree = parser.Parse(rdr, pf.FileName);
				syntaxTree.AcceptVisitor(new TypeSystemConvertVisitor(pf));
				project = project.AddOrUpdateFiles(pf);
			}
			project = project.AddAssemblyReferences(new[] { Files.Mscorlib });

			var compilation = project.CreateCompilation();
			AllTypes = compilation.MainAssembly.TopLevelTypeDefinitions.SelectMany(SelfAndNested).ToDictionary(t => t.ReflectionName);

			var er = new MockErrorReporter(true);
			Metadata = new MetadataImporter(er, compilation, new CompilerOptions());
			preparer();
			Metadata.Prepare(compilation.GetAllTypeDefinitions());
			Assert.That(er.AllMessages, Is.Empty, "Should not generate errrors");
		}
Beispiel #45
0
		public static JsExpression ResolveTypeParameter(ITypeParameter tp, ITypeDefinition currentType, IMethod currentMethod, IMetadataImporter metadataImporter, IErrorReporter errorReporter, INamer namer) {
			bool unusable = false;
			switch (tp.OwnerType) {
				case EntityType.TypeDefinition:
					unusable = metadataImporter.GetTypeSemantics(currentType).IgnoreGenericArguments;
					break;
				case EntityType.Method: {
					var sem = metadataImporter.GetMethodSemantics(currentMethod);
					unusable = sem.Type != MethodScriptSemantics.ImplType.InlineCode && metadataImporter.GetMethodSemantics(currentMethod).IgnoreGenericArguments;
					break;
				}
				default:
					errorReporter.InternalError("Invalid owner " + tp.OwnerType + " for type parameter " + tp);
					return JsExpression.Null;
			}
			if (unusable) {
				errorReporter.Message(Messages._7536, tp.Name, tp.OwnerType == EntityType.TypeDefinition ? "type" : "method", tp.OwnerType == EntityType.TypeDefinition ? currentType.FullName : currentMethod.FullName);
				return JsExpression.Null;
			}
			return JsExpression.Identifier(namer.GetTypeParameterName(tp));
		}
		internal StatementCompiler(IMetadataImporter metadataImporter, INamer namer, 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, ITypeDefinition typeBeingCompiled, ExpressionCompiler expressionCompiler, SharedValue<int> nextLabelIndex, IVariable currentVariableForRethrow, IDictionary<object, string> currentGotoCaseMap) {
			_metadataImporter           = metadataImporter;
			_namer                      = namer;
			_errorReporter              = errorReporter;
			_compilation                = compilation;
			_resolver                   = resolver;
			_variables                  = variables;
			_nestedFunctions            = nestedFunctions;
			_runtimeLibrary             = runtimeLibrary;
			_thisAlias                  = thisAlias;
			_usedVariableNames          = usedVariableNames;
			_nestedFunctionContext      = nestedFunctionContext;
			_methodBeingCompiled        = methodBeingCompiled;
			_typeBeingCompiled          = typeBeingCompiled;
			_currentVariableForRethrow  = currentVariableForRethrow;
			_currentGotoCaseMap         = currentGotoCaseMap;

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

			_expressionCompiler         = expressionCompiler ?? new ExpressionCompiler(compilation, metadataImporter, namer, runtimeLibrary, errorReporter, variables, nestedFunctions, v => CreateTemporaryVariable(v, _region), c => new StatementCompiler(_metadataImporter, _namer, _errorReporter, _compilation, _resolver, _variables, _nestedFunctions, _runtimeLibrary, thisAlias, _usedVariableNames, c, _methodBeingCompiled, _typeBeingCompiled), thisAlias, nestedFunctionContext, null, _methodBeingCompiled, _typeBeingCompiled);
			_result                     = new List<JsStatement>();
		}
		private static ExpressionCompileResult Compile(ResolveResult rr, ITypeDefinition currentType, IMethod currentMethod, ICompilation compilation, IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter, bool returnValueIsImportant, Dictionary<IVariable, VariableData> variables, ISet<string> usedVariableNames) {
			variables = variables ?? new Dictionary<IVariable, VariableData>();
			usedVariableNames = usedVariableNames ?? new HashSet<string>();
			return new ExpressionCompiler(compilation,
			                              metadataImporter,
			                              namer,
			                              runtimeLibrary,
			                              errorReporter,
			                              variables,
			                              new Dictionary<LambdaResolveResult, NestedFunctionData>(),
			                              t => {
			                                  string name = namer.GetVariableName(null, usedVariableNames);
			                                  IVariable variable = new SimpleVariable(t, "temporary", DomRegion.Empty);
			                                  variables[variable] = new VariableData(name, null, false);
			                                  usedVariableNames.Add(name);
			                                  return variable;
			                              },
			                              _ => { throw new Exception("Cannot compile nested functions here"); },
			                              null,
			                              new NestedFunctionContext(EmptyList<IVariable>.Instance),
			                              null,
			                              currentMethod,
			                              currentType
			                             ).Compile(rr, returnValueIsImportant);
		}
		public static JsExpression ConstructAttribute(IAttribute attr, ITypeDefinition currentType, ICompilation compilation, IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter) {
			errorReporter.Region = attr.Region;
			var initializerStatements = attr.NamedArguments.Select(a => new OperatorResolveResult(a.Key.ReturnType, ExpressionType.Assign, new MemberResolveResult(new InitializedObjectResolveResult(attr.AttributeType), a.Key), a.Value)).ToList<ResolveResult>();
			var constructorResult = CompileConstructorInvocation(attr.Constructor, initializerStatements, currentType, null, attr.PositionalArguments, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, null, null);
			if (constructorResult.AdditionalStatements.Count > 0) {
				return JsExpression.Invocation(JsExpression.FunctionDefinition(new string[0], JsStatement.Block(constructorResult.AdditionalStatements.Concat(new[] { JsStatement.Return(constructorResult.Expression) }))));
			}
			else {
				return constructorResult.Expression;
			}
		}
        public static JsExpression ConstructMemberInfo(IMember m, ICompilation compilation, IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter, Func <IType, JsExpression> instantiateType, bool includeDeclaringType)
        {
            MethodScriptSemantics semanticsIfAccessor = null;

            if (m is IMethod && ((IMethod)m).IsAccessor)
            {
                var owner = ((IMethod)m).AccessorOwner;
                if (owner is IProperty)
                {
                    var sem = metadataImporter.GetPropertySemantics((IProperty)owner);
                    if (sem.Type == PropertyScriptSemantics.ImplType.GetAndSetMethods)
                    {
                        if (ReferenceEquals(m, ((IProperty)owner).Getter))
                        {
                            semanticsIfAccessor = sem.GetMethod;
                        }
                        else if (ReferenceEquals(m, ((IProperty)owner).Setter))
                        {
                            semanticsIfAccessor = sem.SetMethod;
                        }
                        else
                        {
                            throw new ArgumentException("Invalid member " + m);
                        }
                    }
                }
                else if (owner is IEvent)
                {
                    var sem = metadataImporter.GetEventSemantics((IEvent)owner);
                    if (sem.Type == EventScriptSemantics.ImplType.AddAndRemoveMethods)
                    {
                        if (ReferenceEquals(m, ((IEvent)owner).AddAccessor))
                        {
                            semanticsIfAccessor = sem.AddMethod;
                        }
                        else if (ReferenceEquals(m, ((IEvent)owner).RemoveAccessor))
                        {
                            semanticsIfAccessor = sem.RemoveMethod;
                        }
                        else
                        {
                            throw new ArgumentException("Invalid member " + m);
                        }
                    }
                }
                else
                {
                    throw new ArgumentException("Invalid owner " + owner);
                }
            }

            return(ConstructMemberInfo(m, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, instantiateType, includeDeclaringType, semanticsIfAccessor));
        }
		public StatementCompiler(IMetadataImporter metadataImporter, INamer namer, 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, ITypeDefinition typeBeingCompiled)
			: this(metadataImporter, namer, errorReporter, compilation, resolver, variables, nestedFunctions, runtimeLibrary, thisAlias, usedVariableNames, nestedFunctionContext, methodBeingCompiled, typeBeingCompiled, null, null, null, null)
		{
		}
		public static JsExpression ConstructFieldPropertyAccessor(IMethod m, ICompilation compilation, IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter, string fieldName, Func<IType, JsExpression> instantiateType, bool isGetter, bool includeDeclaringType) {
			var properties = GetCommonMemberInfoProperties(m, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, instantiateType, includeDeclaringType);
			properties.Add(new JsObjectLiteralProperty("type", JsExpression.Number((int)MemberTypes.Method)));
			properties.Add(new JsObjectLiteralProperty("params", JsExpression.ArrayLiteral(m.Parameters.Select(p => instantiateType(p.Type)))));
			properties.Add(new JsObjectLiteralProperty("returnType", instantiateType(m.ReturnType)));
			properties.Add(new JsObjectLiteralProperty(isGetter ? "fget" : "fset", JsExpression.String(fieldName)));
			if (m.IsStatic)
				properties.Add(new JsObjectLiteralProperty("isStatic", JsExpression.True));
			return JsExpression.ObjectLiteral(properties);
		}
		public static IEnumerable<IAttribute> GetScriptableAttributes(IEnumerable<IAttribute> attributes, IMetadataImporter metadataImporter) {
			return attributes.Where(a => !a.IsConditionallyRemoved && metadataImporter.GetTypeSemantics(a.AttributeType.GetDefinition()).Type != TypeScriptSemantics.ImplType.NotUsableFromScript);
		}
		private static List<JsObjectLiteralProperty> GetCommonMemberInfoProperties(IMember m, ICompilation compilation, IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter, Func<IType, JsExpression> instantiateType, bool includeDeclaringType) {
			var result = new List<JsObjectLiteralProperty>();
			var attr = GetScriptableAttributes(m.Attributes, metadataImporter).ToList();
			if (attr.Count > 0)
				result.Add(new JsObjectLiteralProperty("attr", JsExpression.ArrayLiteral(attr.Select(a => ConstructAttribute(a, m.DeclaringTypeDefinition, compilation, metadataImporter, namer, runtimeLibrary, errorReporter)))));
			if (includeDeclaringType)
				result.Add(new JsObjectLiteralProperty("typeDef", instantiateType(m.DeclaringType)));

			result.Add(new JsObjectLiteralProperty("name", JsExpression.String(m.Name)));
			return result;
		}
		private static JsExpression ConstructConstructorInfo(IMethod constructor, ICompilation compilation, IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter, Func<IType, JsExpression> instantiateType, bool includeDeclaringType) {
			var properties = GetCommonMemberInfoProperties(constructor, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, instantiateType, includeDeclaringType);

			var sem = metadataImporter.GetConstructorSemantics(constructor);
			if (sem.Type == ConstructorScriptSemantics.ImplType.NotUsableFromScript) {
				errorReporter.Message(Messages._7200, constructor.FullName);
				return JsExpression.Null;
			}
			properties.Add(new JsObjectLiteralProperty("type", JsExpression.Number((int)MemberTypes.Constructor)));
			properties.Add(new JsObjectLiteralProperty("params", JsExpression.ArrayLiteral(constructor.Parameters.Select(p => instantiateType(p.Type)))));
			if (sem.Type == ConstructorScriptSemantics.ImplType.NamedConstructor || sem.Type == ConstructorScriptSemantics.ImplType.StaticMethod)
				properties.Add(new JsObjectLiteralProperty("sname", JsExpression.String(sem.Name)));
			if (sem.Type == ConstructorScriptSemantics.ImplType.StaticMethod)
				properties.Add(new JsObjectLiteralProperty("sm", JsExpression.True));
			if ((sem.Type == ConstructorScriptSemantics.ImplType.UnnamedConstructor || sem.Type == ConstructorScriptSemantics.ImplType.NamedConstructor || sem.Type == ConstructorScriptSemantics.ImplType.StaticMethod) && sem.ExpandParams)
				properties.Add(new JsObjectLiteralProperty("exp", JsExpression.True));
			if (sem.Type == ConstructorScriptSemantics.ImplType.Json || sem.Type == ConstructorScriptSemantics.ImplType.InlineCode) {
				var usedNames = new HashSet<string>();
				var parameters = new List<IVariable>();
				var variables = new Dictionary<IVariable, VariableData>();
				IList<ResolveResult> constructorParameters = null;
				IList<ResolveResult> initializerStatements = null;
				if (sem.Type == ConstructorScriptSemantics.ImplType.Json && constructor.DeclaringType.Kind == TypeKind.Anonymous) {
					initializerStatements = new List<ResolveResult>();
					foreach (var p in constructor.DeclaringType.GetProperties()) {
						string paramName = MakeCamelCase(p.Name);
						string name = namer.GetVariableName(paramName, usedNames);
						usedNames.Add(name);
						var variable = new SimpleVariable(p.ReturnType, paramName, DomRegion.Empty);
						parameters.Add(variable);
						variables.Add(variable, new VariableData(name, null, false));
						initializerStatements.Add(new OperatorResolveResult(p.ReturnType, ExpressionType.Assign, new MemberResolveResult(new InitializedObjectResolveResult(constructor.DeclaringType), p), new LocalResolveResult(variable)));
					}
				}
				else {
					constructorParameters = new List<ResolveResult>();
					foreach (var p in constructor.Parameters) {
						string name = namer.GetVariableName(p.Name, usedNames);
						usedNames.Add(name);
						var variable = new SimpleVariable(p.Type, p.Name, DomRegion.Empty);
						parameters.Add(variable);
						variables.Add(variable, new VariableData(name, null, false));
						constructorParameters.Add(new LocalResolveResult(variable));
					}
				}
				var compileResult = CompileConstructorInvocation(constructor, initializerStatements, constructor.DeclaringTypeDefinition, constructor, constructorParameters, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, variables, usedNames);
				var definition = JsExpression.FunctionDefinition(parameters.Select(p => variables[p].Name), JsStatement.Block(compileResult.AdditionalStatements.Concat(new[] { JsStatement.Return(compileResult.Expression) })));
				properties.Add(new JsObjectLiteralProperty("def", definition));
			}
			return JsExpression.ObjectLiteral(properties);
		}
		private static JsExpression ConstructMemberInfo(IMember m, ICompilation compilation, IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter, Func<IType, JsExpression> instantiateType, bool includeDeclaringType, MethodScriptSemantics semanticsIfAccessor) {
			if (m is IMethod && ((IMethod)m).IsConstructor)
				return ConstructConstructorInfo((IMethod)m, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, instantiateType, includeDeclaringType);

			var properties = GetCommonMemberInfoProperties(m, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, instantiateType, includeDeclaringType);
			if (m.IsStatic)
				properties.Add(new JsObjectLiteralProperty("isStatic", JsExpression.True));

			if (m is IMethod) {
				var method = (IMethod)m;
				var sem = semanticsIfAccessor ?? metadataImporter.GetMethodSemantics(method);
				if (sem.Type != MethodScriptSemantics.ImplType.NormalMethod && sem.Type != MethodScriptSemantics.ImplType.StaticMethodWithThisAsFirstArgument && sem.Type != MethodScriptSemantics.ImplType.InlineCode) {
					errorReporter.Message(Messages._7201, m.FullName, "method");
					return JsExpression.Null;
				}
				if ((sem.Type == MethodScriptSemantics.ImplType.NormalMethod || sem.Type == MethodScriptSemantics.ImplType.StaticMethodWithThisAsFirstArgument) && sem.ExpandParams)
					properties.Add(new JsObjectLiteralProperty("exp", JsExpression.True));

				properties.Add(new JsObjectLiteralProperty("type", JsExpression.Number((int)MemberTypes.Method)));
				if (sem.Type == MethodScriptSemantics.ImplType.InlineCode) {
					var usedNames = new HashSet<string>();
					var parameters = new List<IVariable>();
					var variables = new Dictionary<IVariable, VariableData>();
					var arguments = new List<ResolveResult>();
					foreach (var p in method.Parameters) {
						string name = namer.GetVariableName(p.Name, usedNames);
						usedNames.Add(name);
						var variable = new SimpleVariable(p.Type, p.Name, DomRegion.Empty);
						parameters.Add(variable);
						variables.Add(variable, new VariableData(name, null, false));
						arguments.Add(new LocalResolveResult(variable));
					}
					var tokens = InlineCodeMethodCompiler.Tokenize(method, sem.LiteralCode, _ => {});

					var compileResult = Compile(CreateMethodInvocationResolveResult(method, method.IsStatic ? (ResolveResult)new TypeResolveResult(method.DeclaringType) : new ThisResolveResult(method.DeclaringType), arguments), method.DeclaringTypeDefinition, method, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, true, variables, usedNames);
					var definition = JsExpression.FunctionDefinition(parameters.Select(p => variables[p].Name), JsStatement.Block(compileResult.AdditionalStatements.Concat(new[] { JsStatement.Return(compileResult.Expression) })));

					if (tokens.Any(t => t.Type == InlineCodeToken.TokenType.TypeParameter && t.OwnerType == SymbolKind.Method)) {
						definition = JsExpression.FunctionDefinition(method.TypeParameters.Select(namer.GetTypeParameterName), JsStatement.Return(definition));
						properties.Add(new JsObjectLiteralProperty("tpcount", JsExpression.Number(method.TypeParameters.Count)));
					}
					properties.Add(new JsObjectLiteralProperty("def", definition));
				}
				else {
					if (IsJsGeneric(method, metadataImporter)) {
						properties.Add(new JsObjectLiteralProperty("tpcount", JsExpression.Number(method.TypeParameters.Count)));
					}
					if (sem.Type == MethodScriptSemantics.ImplType.StaticMethodWithThisAsFirstArgument) {
						properties.Add(new JsObjectLiteralProperty("sm", JsExpression.True));
					}
					properties.Add(new JsObjectLiteralProperty("sname", JsExpression.String(sem.Name)));
				}
				properties.Add(new JsObjectLiteralProperty("returnType", instantiateType(method.ReturnType)));
				properties.Add(new JsObjectLiteralProperty("params", JsExpression.ArrayLiteral(method.Parameters.Select(p => instantiateType(p.Type)))));
			}
			else if (m is IField) {
				var field = (IField)m;
				var sem = metadataImporter.GetFieldSemantics(field);
				if (sem.Type != FieldScriptSemantics.ImplType.Field) {
					errorReporter.Message(Messages._7201, m.FullName, "field");
					return JsExpression.Null;
				}
				properties.Add(new JsObjectLiteralProperty("type", JsExpression.Number((int)MemberTypes.Field)));
				properties.Add(new JsObjectLiteralProperty("returnType", instantiateType(field.ReturnType)));
				properties.Add(new JsObjectLiteralProperty("sname", JsExpression.String(sem.Name)));
			}
			else if (m is IProperty) {
				var prop = (IProperty)m;
				var sem = metadataImporter.GetPropertySemantics(prop);
				properties.Add(new JsObjectLiteralProperty("type", JsExpression.Number((int)MemberTypes.Property)));
				properties.Add(new JsObjectLiteralProperty("returnType", instantiateType(prop.ReturnType)));
				if (prop.Parameters.Count > 0)
					properties.Add(new JsObjectLiteralProperty("params", JsExpression.ArrayLiteral(prop.Parameters.Select(p => instantiateType(p.Type)))));

				switch (sem.Type) {
					case PropertyScriptSemantics.ImplType.GetAndSetMethods:
						if (sem.GetMethod != null && sem.GetMethod.Type != MethodScriptSemantics.ImplType.NormalMethod && sem.GetMethod.Type != MethodScriptSemantics.ImplType.StaticMethodWithThisAsFirstArgument && sem.GetMethod.Type != MethodScriptSemantics.ImplType.InlineCode) {
							errorReporter.Message(Messages._7202, m.FullName, "property", "getter");
							return JsExpression.Null;
						}
						if (sem.SetMethod != null && sem.SetMethod.Type != MethodScriptSemantics.ImplType.NormalMethod && sem.SetMethod.Type != MethodScriptSemantics.ImplType.StaticMethodWithThisAsFirstArgument && sem.SetMethod.Type != MethodScriptSemantics.ImplType.InlineCode) {
							errorReporter.Message(Messages._7202, m.FullName, "property", "setter");
							return JsExpression.Null;
						}
						if (sem.GetMethod != null)
							properties.Add(new JsObjectLiteralProperty("getter", ConstructMemberInfo(prop.Getter, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, instantiateType, includeDeclaringType, sem.GetMethod)));
						if (sem.SetMethod != null)
							properties.Add(new JsObjectLiteralProperty("setter", ConstructMemberInfo(prop.Setter, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, instantiateType, includeDeclaringType, sem.SetMethod)));
						break;
					case PropertyScriptSemantics.ImplType.Field:
						if (prop.CanGet)
							properties.Add(new JsObjectLiteralProperty("getter", ConstructFieldPropertyAccessor(prop.Getter, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, sem.FieldName, instantiateType, isGetter: true, includeDeclaringType: includeDeclaringType)));
						if (prop.CanSet)
							properties.Add(new JsObjectLiteralProperty("setter", ConstructFieldPropertyAccessor(prop.Setter, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, sem.FieldName, instantiateType, isGetter: false, includeDeclaringType: includeDeclaringType)));
						properties.Add(new JsObjectLiteralProperty("fname", JsExpression.String(sem.FieldName)));
						break;
					default:
						errorReporter.Message(Messages._7201, m.FullName, "property");
						return JsExpression.Null;
				}
			}
			else if (m is IEvent) {
				var evt = (IEvent)m;
				var sem = metadataImporter.GetEventSemantics(evt);
				if (sem.Type != EventScriptSemantics.ImplType.AddAndRemoveMethods) {
					errorReporter.Message(Messages._7201, m.FullName, "event");
					return JsExpression.Null;
				}
				if (sem.AddMethod.Type != MethodScriptSemantics.ImplType.NormalMethod && sem.AddMethod.Type != MethodScriptSemantics.ImplType.StaticMethodWithThisAsFirstArgument && sem.AddMethod.Type != MethodScriptSemantics.ImplType.InlineCode) {
					errorReporter.Message(Messages._7202, m.FullName, "event", "add accessor");
					return JsExpression.Null;
				}
				if (sem.RemoveMethod.Type != MethodScriptSemantics.ImplType.NormalMethod && sem.RemoveMethod.Type != MethodScriptSemantics.ImplType.StaticMethodWithThisAsFirstArgument && sem.RemoveMethod.Type != MethodScriptSemantics.ImplType.InlineCode) {
					errorReporter.Message(Messages._7202, m.FullName, "event", "remove accessor");
					return JsExpression.Null;
				}

				properties.Add(new JsObjectLiteralProperty("type", JsExpression.Number((int)MemberTypes.Event)));
				properties.Add(new JsObjectLiteralProperty("adder", ConstructMemberInfo(evt.AddAccessor, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, instantiateType, includeDeclaringType, sem.AddMethod)));
				properties.Add(new JsObjectLiteralProperty("remover", ConstructMemberInfo(evt.RemoveAccessor, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, instantiateType, includeDeclaringType, sem.RemoveMethod)));
			}
			else {
				throw new ArgumentException("Invalid member " + m);
			}

			return JsExpression.ObjectLiteral(properties);
		}
		protected MetadataImporterDecoratorBase(IMetadataImporter prev) {
			this._prev = prev;
		}
		public static JsExpression ConstructMemberInfo(IMember m, ICompilation compilation, IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter, Func<IType, JsExpression> instantiateType, bool includeDeclaringType) {
			MethodScriptSemantics semanticsIfAccessor = null;
			if (m is IMethod && ((IMethod)m).IsAccessor) {
				var owner = ((IMethod)m).AccessorOwner;
				if (owner is IProperty) {
					var sem = metadataImporter.GetPropertySemantics((IProperty)owner);
					if (sem.Type == PropertyScriptSemantics.ImplType.GetAndSetMethods) {
						if (ReferenceEquals(m, ((IProperty)owner).Getter))
							semanticsIfAccessor = sem.GetMethod;
						else if (ReferenceEquals(m, ((IProperty)owner).Setter))
							semanticsIfAccessor = sem.SetMethod;
						else
							throw new ArgumentException("Invalid member " + m);
					}
				}
				else if (owner is IEvent) {
					var sem = metadataImporter.GetEventSemantics((IEvent)owner);
					if (sem.Type == EventScriptSemantics.ImplType.AddAndRemoveMethods) {
						if (ReferenceEquals(m, ((IEvent)owner).AddAccessor))
							semanticsIfAccessor = sem.AddMethod;
						else if (ReferenceEquals(m, ((IEvent)owner).RemoveAccessor))
							semanticsIfAccessor = sem.RemoveMethod;
						else
							throw new ArgumentException("Invalid member " + m);
					}
				}
				else
					throw new ArgumentException("Invalid owner " + owner);
			}

			return ConstructMemberInfo(m, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, instantiateType, includeDeclaringType, semanticsIfAccessor);
		}
		public static ExpressionCompileResult CompileConstructorInvocation(IMethod constructor, IList<ResolveResult> initializerStatements, ITypeDefinition currentType, IMethod currentMethod, IList<ResolveResult> arguments, ICompilation compilation, IMetadataImporter metadataImporter, INamer namer, IRuntimeLibrary runtimeLibrary, IErrorReporter errorReporter, Dictionary<IVariable, VariableData> variables, ISet<string> usedVariableNames) {
			return Compile(new CSharpInvocationResolveResult(new TypeResolveResult(constructor.DeclaringType), constructor, arguments, initializerStatements: initializerStatements), currentType, currentMethod, compilation, metadataImporter, namer, runtimeLibrary, errorReporter, true, variables, usedVariableNames);
		}
		public static bool IsReflectable(IMember member, IMetadataImporter metadataImporter) {
			var ra = AttributeReader.ReadAttribute<ReflectableAttribute>(member);
			if (ra != null)
				return ra.Reflectable;
			if (member.Attributes.Any(a => metadataImporter.GetTypeSemantics(a.AttributeType.GetDefinition()).Type != TypeScriptSemantics.ImplType.NotUsableFromScript))
				return true;	// Any scriptable attribute will cause reflectability (unless [Reflectable(false)] was specified.

			if (member.DeclaringType.Kind != TypeKind.Anonymous) {
				var tdr = AttributeReader.ReadAttribute<DefaultMemberReflectabilityAttribute>(member.DeclaringTypeDefinition);
				if (tdr != null) {
					return IsMemberReflectable(member, tdr.DefaultReflectability);
				}

				var adr = AttributeReader.ReadAttribute<DefaultMemberReflectabilityAttribute>(member.ParentAssembly.AssemblyAttributes);
				if (adr != null) {
					return IsMemberReflectable(member, adr.DefaultReflectability);
				}
			}

			return false;
		}
		public static bool IsJsGeneric(ITypeDefinition type, IMetadataImporter metadataImporter) {
			return type.TypeParameterCount > 0 && !metadataImporter.GetTypeSemantics(type).IgnoreGenericArguments;
		}