예제 #1
0
        protected override string GetTypeDeclaration(TypeDefinition type)
        {
            var buf = new StringBuilder();

            if (type.IsEnum)
            {
                // var value = [fully qualified type].[camel-cased name of first enum value];
                var firstElement = type.GetMembers().Skip(1).First();//skip "__value element"

                buf.Append("var value = ");
                buf.Append(ProcessFullName(type.FullName));
                buf.Append(".");
                buf.Append(CamelCase(firstElement.Name));
                return(buf.ToString());
            }
            if (type.IsValueType)
            {
                //Structures: Windows Runtime structures are objects in JavaScript.
                // If you want to pass a Windows Runtime structure to a Windows Runtime method,
                // don't instantiate the structure with the new keyword. Instead, create an object
                // and add the relevant members and their values. The names of the members should be in camel case:
                // SomeStruct.firstMember.

                // var [struct name, camel cased] = {
                //	[fieldname, came cased] : /* Your value */
                buf.Append("var ");
                buf.Append(CamelCase(GetName(type)));
                buf.Append(" = {");
                buf.Append(GetLineEnding());
                buf.Append(string.Join("," + GetLineEnding(),
                                       type.Fields.Select(i => CamelCase(i.Name) + " : /* Your value */")));
                buf.Append(GetLineEnding());
                buf.Append("}");
                return(buf.ToString());
            }
            if (DocUtils.IsDelegate(type))
            {
                //  var [delegateName, camel-cased]Handler = function([parameter name list, camel cased]){
                //  /* Your code */
                //            }
                MethodDefinition invoke = type.GetMethod("Invoke");
                buf.Append("var ");
                buf.Append(CamelCase(GetName(type)));
                buf.Append("Handler = function(");
                AppendParameters(buf, invoke, invoke.Parameters);
                buf.Append("){");
                buf.Append(GetLineEnding());
                buf.Append("/* Your code */");
                buf.Append(GetLineEnding());
                buf.Append("}");
                return(buf.ToString());
            }

            var publicConstructor = GetConstructor(type);

            return(GetDeclaration(publicConstructor));
        }
예제 #2
0
        private static bool IsAttachedEventHandler(TypeReference typeReference)
        {
            var type = typeReference.Resolve();

            if (!DocUtils.IsDelegate(type))
            {
                return(false);
            }
            MethodDefinition invoke = type.GetMethod("Invoke");

            return(invoke.Parameters.Count == 2);
        }
예제 #3
0
        public override bool IsSupported(TypeReference tref)
        {
            var type = tref.Resolve();

            if (type == null)
            {
                return(false);
            }

            if (type.IsEnum ||
                type.IsValueType ||
                DocUtils.IsDelegate(type))
            {
                return(false);
            }

            return(base.IsSupported(tref));
        }
예제 #4
0
        // For the V1 Pri1 implementation, we will not implement custom “retrievers”.
        // If a non-static class doesn’t have a public constructor
        // (in other words, it is not possible to automatically determine the call to instantiate an instance of the class),
        // the Javascript syntax should either:
        //
        // show a standard disclaimer such as:
        // “This class does not provide a public constructor” or
        // “See the remarks section for information on obtaining an instance of this class”
        // Give a degenerate declarative syntax, such as simply: “Windows.System.FolderLauncherOptions” for the FolderLauncherOptions class.
        // The specific solution to use here is still TBD. If you’re blocked, pick A1 for now.
        // We will investigate whether a Pri 2 feature to modify the syntax block with custom syntax is necessary.
        public override bool IsSupported(TypeReference tref)
        {
            var type = tref.Resolve();

            if (type == null ||
                type.IsAbstract ||
                type.IsInterface ||// Interfaces: You cannot implement a Windows Runtime interface in JavaScript.
                type.HasGenericParameters ||
                !IsSupported(type.CustomAttributes) ||
                type.DeclaringType != null)   //WinRT type can not be nested
            {
                return(false);
            }

            if (type.IsEnum ||
                type.IsValueType ||
                DocUtils.IsDelegate(type))
            {
                if (type.IsEnum && !IsEnumSupported(type))
                {
                    return(false);
                }

                return(true);
            }

            // Windows Runtime types cannot have multiple constructors with same number of arguments
            var publicConstructors = type.GetConstructors().Where(i => i.IsPublic).ToList();

            if (!publicConstructors.Any())
            {
                return(false);
            }

            var constructorsWithEqualNumberOfArguments = publicConstructors.GroupBy(x => x.Parameters.Count)
                                                         .Where(g => g.Count() > 1)
                                                         .Select(y => y.Key)
                                                         .ToList();

            return(constructorsWithEqualNumberOfArguments.Count == 0);
        }
        public override bool IsSupported(TypeReference tref)
        {
            if (HasNestedClassesDuplicateNames(tref))
            {
                return(false);
            }

            var ns           = DocUtils.GetNamespace(tref);
            var allowedTypes = GetAllowedTypes();

            if (allowedTypes.Contains(tref.FullName.Split(' ')[0])
                //winRt specific namespaces so no need for further check
                || ns.StartsWith("Windows.")
                )
            {
                return(true);
            }

            TypeDefinition typedef = null;

            try
            {
                typedef = tref.Resolve();
            }
            catch
            {
                //for winRt Resolve() can fail as Cecil understands winRt types as .net (eg, "System.Object" cannot be resolved)
            }

            if (typedef != null)
            {
                if (allowedTypes.Contains(typedef.FullName))
                {
                    //to check type of array
                    return(true);
                }

                if (DocUtils.IsDelegate(typedef))
                {
                    //delegates can be used only in managed context
                    return(false);
                }

                if (typedef.HasGenericParameters &&
                    typedef.GenericParameters.Any(x => x.HasConstraints ||
                                                  x.HasReferenceTypeConstraint ||
                                                  x.HasDefaultConstructorConstraint ||
                                                  x.HasNotNullableValueTypeConstraint)
                    )
                {
                    //Type parameters cannot be constrained
                    return(false);
                }

                if (HasUnsupportedParent(typedef))
                {
                    return(false);
                }
            }

            return(IsSupportedGenericParameter(tref) &&
                   !ns.StartsWith("System.") && !ns.Equals("System"));
        }
        protected override string GetTypeDeclaration(TypeDefinition type)
        {
            StringBuilder buf = new StringBuilder();

            var genericParamList = GetTypeSpecifiGenericParameters(type);

            AppendGenericItem(buf, genericParamList);
            AppendGenericTypeConstraints(buf, type);

            AppendWebHostHiddenAttribute(buf, type);

            buf.Append(GetTypeKind(type));
            buf.Append(" ");
            var cppType = GetCppType(type.FullName);

            buf.Append(cppType == null
                    ? GetNameWithOptions(type, false, false)
                    : cppType);

            if (type.IsSealed && !DocUtils.IsDelegate(type) && !type.IsValueType)
            {
                buf.Append(" final");
            }

            CppWinRtFullMemberFormatter full = new CppWinRtFullMemberFormatter(this.TypeMap);

            if (!type.IsEnum)
            {
                TypeReference basetype = type.BaseType;
                if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType)   // FIXME
                {
                    basetype = null;
                }

                List <string> interfaceNames;
                try
                {
                    //for winRt Resolve() can fail as Cecil understands winRt types as .net (eg, "System.Object" cannot be resolved)
                    interfaceNames = DocUtils.GetUserImplementedInterfaces(type)
                                     .Select(iface => full.GetNameWithOptions(iface, true, false))
                                     .OrderBy(s => s)
                                     .ToList();
                }
                catch
                {
                    interfaceNames = null;
                }

                if (basetype != null || interfaceNames?.Count > 0)
                {
                    buf.Append(" : ");
                }

                if (basetype != null)
                {
                    buf.Append(full.GetNameWithOptions(basetype, true, false));
                    if (interfaceNames?.Count > 0)
                    {
                        buf.Append(", ");
                    }
                }

                for (int i = 0; i < interfaceNames?.Count; i++)
                {
                    if (i != 0)
                    {
                        buf.Append(", ");
                    }
                    buf.Append(interfaceNames?[i]);
                }
            }

            return(buf.ToString());
        }
        public override bool IsSupported(TypeReference tref)
        {
            if (HasNestedClassesDuplicateNames(tref))
            {
                return(false);
            }

            var allowedTypes = GetAllowedTypes();

            //no support of jagged arrays
            if (tref is ArrayType)
            {
                var typeOfArray = tref is TypeSpecification spec ? spec.ElementType : tref.GetElementType();
                if (typeOfArray is ArrayType)
                {
                    return(false);
                }

                if (allowedTypes.Contains(typeOfArray.FullName) ||
                    CppCxSpecificNamespases.Contains(typeOfArray.Namespace))
                {
                    return(true);
                }
            }

            if (allowedTypes.Contains(tref.FullName.Split(' ')[0]) || CppCxSpecificNamespases.Contains(tref.Namespace))
            {
                return(true);
            }

            var ns = DocUtils.GetNamespace(tref);

            var typedef = tref.Resolve();

            if (typedef != null)
            {
                if (DocUtils.IsDelegate(typedef))
                {
                    if (typedef.HasGenericParameters && typedef.IsPublic)
                    {
                        //generic delegates cannot be declared as public
                        return(false);
                    }

                    return
                        //check types of delegate signature
                        (IsSupported(typedef.GetMethod("Invoke"))
                         //check type
                         && base.IsSupported(tref) && !(ns.StartsWith("System.") || ns.StartsWith("System")));
                }

                if (typedef.IsInterface && typedef.HasGenericParameters &&
                    typedef.GenericParameters.Any(x => x.HasConstraints ||
                                                  x.HasReferenceTypeConstraint ||
                                                  x.HasDefaultConstructorConstraint ||
                                                  x.HasNotNullableValueTypeConstraint)
                    )
                {
                    //generic interface - Type parameters cannot be constrained
                    return(false);
                }

                if (HasUnsupportedParent(typedef))
                {
                    return(false);
                }

                var typeVisibility = typedef.Attributes & TypeAttributes.VisibilityMask;

                if (typeVisibility == TypeAttributes.Public
                    //all public types, including those in your own code, must be declared in a namespace
                    && (string.IsNullOrEmpty(ns)
                        //public standard C++ types are not allowed
                        || ns.StartsWith("std") || ns.StartsWith("cli"))
                    )
                {
                    return(false);
                }

                if (typeVisibility == TypeAttributes.NestedPublic &&
                    (typedef.IsEnum || typedef.IsClass)
                    )
                {
                    //no support of nested public classes/enums
                    return(false);
                }

                if (typedef.IsClass && typeVisibility == TypeAttributes.Public && typedef.HasGenericParameters)
                {
                    //Public ref classes that have type parameters (generics) are not permitted.
                    return(false);
                }

                if (typedef.IsClass && typeVisibility == TypeAttributes.Public)
                {
                    //A public ref class that has a public constructor must also be declared as sealed
                    //to prevent further derivation through the application binary interface (ABI).
                    if (typedef.Methods.Any(x => x.IsConstructor && x.IsPublic) && !typedef.IsSealed)
                    {
                        return(false);
                    }
                }

                if (typedef.IsValueType && !typedef.IsEnum && typedef.Fields.Any(x =>
                                                                                 !ValueClassPropertyTypeAllowed.Contains(x.FieldType.FullName) && !(x.FieldType.Resolve() != null && x.FieldType.Resolve().IsEnum)))
                {
                    //A value struct or value class can contain as fields only
                    //fundamental numeric types, enum classes, Platform::String^, or Platform::IBox <T>^
                    return(false);
                }


                bool condition;
                try
                {
                    //custom attribute can contain only public fields and only with allowed types or enums
                    condition = IsCustomAttribute(typedef) &&
                                (typedef.Fields.Any(z =>
                                                    !CustomAttributesFieldTypesAllowed.Contains(z.FieldType.FullName) &&
                                                    !(z.FieldType.Resolve() != null && z.FieldType.Resolve().IsEnum)
                                                    ) ||
                                 typedef.Properties.Count != 0 ||
                                 typedef.Methods.Count(x => !x.IsConstructor) != 0 ||
                                 typedef.Events.Count != 0
                                );
                }
                catch
                {
                    condition = false;
                }

                if (condition)
                {
                    //custom attribute can contain only public fields and only with allowed types or enums
                    return(false);
                }
            }

            //cannot support .Net types
            return(!ns.StartsWith("System.") && !ns.Equals("System") && base.IsSupported(tref));
        }
        protected override string GetTypeDeclaration(TypeDefinition type)
        {
            string visibility = GetTypeVisibility(type.Attributes);

            if (visibility == null)
            {
                return(null);
            }

            StringBuilder buf = new StringBuilder();

            if (!visibility.Contains(":"))
            {
                AppendWebHostHiddenAttribute(buf, type);
            }

            buf.Append(visibility);
            buf.Append(" ");

            if (visibility.Contains(":"))
            {
                AppendWebHostHiddenAttribute(buf, type);
            }

            CppFullMemberFormatter full = new CppCxFullMemberFormatter(this.TypeMap);

            if (DocUtils.IsDelegate(type))
            {
                buf.Append("delegate ");
                MethodDefinition invoke = type.GetMethod("Invoke");
                buf.Append(full.GetNameWithOptions(invoke.ReturnType)).Append(" ");
                buf.Append(GetNameWithOptions(type, false, false));
                AppendParameters(buf, invoke, invoke.Parameters);
                buf.Append(";");

                return(buf.ToString());
            }

            buf.Append(GetTypeKind(type));
            buf.Append(" ");
            buf.Append(GetCppType(type.FullName) == null
                    ? GetNameWithOptions(type, false, false)
                    : type.Name);

            if (type.IsAbstract && !type.IsInterface)
            {
                buf.Append(" abstract");
            }
            if (type.IsSealed && !DocUtils.IsDelegate(type) && !type.IsValueType)
            {
                buf.Append(" sealed");
            }

            if (!type.IsEnum)
            {
                TypeReference basetype = type.BaseType;
                if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType)   // FIXME
                {
                    basetype = null;
                }

                List <string> interfaceNames;
                try
                {
                    //for c++/cx Resolve() can fail as Cecil understands CX types as .net (eg, "System.Attribute" instead of "Platform::Metadata::Attribute")
                    interfaceNames = DocUtils.GetUserImplementedInterfaces(type)
                                     .Select(iface => full.GetNameWithOptions(iface, true, false))
                                     .OrderBy(s => s)
                                     .ToList();
                }
                catch
                {
                    interfaceNames = null;
                }

                if (basetype != null || interfaceNames?.Count > 0)
                {
                    buf.Append(" : ");
                }

                if (basetype != null)
                {
                    var appendValue = GetCppType(basetype.FullName);
                    buf.Append(appendValue ?? full.GetNameWithOptions(basetype, true, false));
                    if (interfaceNames?.Count > 0)
                    {
                        buf.Append(", ");
                    }
                }

                for (int i = 0; i < interfaceNames?.Count; i++)
                {
                    if (i != 0)
                    {
                        buf.Append(", ");
                    }
                    buf.Append(interfaceNames?[i]);
                }
            }

            return(buf.ToString());
        }
        protected override string GetTypeDeclaration(TypeDefinition type)
        {
            string visibility = GetTypeVisibility(type.Attributes);

            if (visibility == null)
            {
                return(null);
            }

            StringBuilder buf = new StringBuilder();

            var genericParamList = GetTypeSpecifiGenericParameters(type);

            if (!visibility.Contains(":"))
            {
                AppendGenericItem(buf, genericParamList);
                AppendGenericTypeConstraints(buf, type);
            }

            buf.Append(visibility);
            buf.Append(" ");

            if (visibility.Contains(":"))
            {
                AppendGenericItem(buf, genericParamList);
                AppendGenericTypeConstraints(buf, type);
            }

            CppFullMemberFormatter full = new CppFullMemberFormatter(this.TypeMap);

            if (DocUtils.IsDelegate(type))
            {
                buf.Append("delegate ");
                MethodDefinition invoke = type.GetMethod("Invoke");
                buf.Append(full.GetNameWithOptions(invoke.ReturnType));
                buf.Append(" ");
                buf.Append(GetNameWithOptions(type, false, false));
                AppendParameters(buf, invoke, invoke.Parameters);
                buf.Append(";");

                return(buf.ToString());
            }

            buf.Append(GetTypeKind(type));
            buf.Append(" ");
            buf.Append(GetCppType(type.FullName) == null
                    ? GetNameWithOptions(type, false, false)
                    : type.Name);

            if (type.IsAbstract && !type.IsInterface)
            {
                buf.Append(" abstract");
            }
            if (type.IsSealed && !DocUtils.IsDelegate(type) && !type.IsValueType)
            {
                buf.Append(" sealed");
            }

            if (!type.IsEnum)
            {
                TypeReference basetype = type.BaseType;
                if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType)   // FIXME
                {
                    basetype = null;
                }

                List <string> interfaceNames = DocUtils.GetUserImplementedInterfaces(type)
                                               .Select(iface => full.GetNameWithOptions(iface, true, false))
                                               .OrderBy(s => s)
                                               .ToList();

                if (basetype != null || interfaceNames.Count > 0)
                {
                    buf.Append(" : ");
                }

                if (basetype != null)
                {
                    buf.Append(full.GetNameWithOptions(basetype, true, false));
                    if (interfaceNames.Count > 0)
                    {
                        buf.Append(", ");
                    }
                }

                for (int i = 0; i < interfaceNames.Count; i++)
                {
                    if (i != 0)
                    {
                        buf.Append(", ");
                    }
                    buf.Append(interfaceNames[i]);
                }
            }

            return(buf.ToString());
        }
예제 #10
0
        protected override string GetTypeDeclaration(TypeDefinition type)
        {
            string visibility = GetTypeVisibility(type.Attributes);

            if (visibility == null)
            {
                return(null);
            }

            StringBuilder buf = new StringBuilder();

            buf.Append(visibility);
            buf.Append(" ");

            MemberFormatter full = new CSharpFullMemberFormatter(this.TypeMap);

            if (DocUtils.IsDelegate(type))
            {
                buf.Append("delegate ");
                MethodDefinition invoke = type.GetMethod("Invoke");
                var context             = AttributeParserContext.Create(invoke.MethodReturnType);
                var isNullableType      = context.IsNullable();
                buf.Append(full.GetName(invoke.ReturnType, context));
                buf.Append(GetTypeNullableSymbol(invoke.ReturnType, isNullableType));
                buf.Append(" ");
                buf.Append(GetName(type));
                AppendParameters(buf, invoke, invoke.Parameters);
                AppendGenericTypeConstraints(buf, type);
                buf.Append(";");

                return(buf.ToString());
            }

            if (type.IsAbstract && !type.IsInterface)
            {
                buf.Append("abstract ");
            }
            if (type.IsSealed && !DocUtils.IsDelegate(type) && !type.IsValueType)
            {
                buf.Append("sealed ");
            }
            buf.Replace("abstract sealed", "static");

            buf.Append(GetTypeKind(type));
            buf.Append(" ");
            buf.Append(GetCSharpType(type.FullName) == null
                    ? GetName(type)
                    : type.Name);

            if (!type.IsEnum)
            {
                TypeReference basetype = type.BaseType;
                if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType)   // FIXME
                {
                    basetype = null;
                }

                List <string> interface_names = DocUtils.GetUserImplementedInterfaces(type)
                                                .Select(iface => full.GetName(iface))
                                                .OrderBy(s => s)
                                                .ToList();

                if (basetype != null || interface_names.Count > 0)
                {
                    buf.Append(" : ");
                }

                if (basetype != null)
                {
                    buf.Append(full.GetName(basetype));
                    if (interface_names.Count > 0)
                    {
                        buf.Append(", ");
                    }
                }

                for (int i = 0; i < interface_names.Count; i++)
                {
                    if (i != 0)
                    {
                        buf.Append(", ");
                    }
                    buf.Append(interface_names[i]);
                }
                AppendGenericTypeConstraints(buf, type);
            }

            return(buf.ToString());
        }