Exemple #1
0
        static string GetEntityJsName(this ITypeDefinition ce, bool shortName)
        {
            var jta = ce.GetJsTypeAttribute();

            if (shortName)
            {
                if (jta != null && jta.Name != null)
                {
                    if (jta.Name.Contains('.'))
                    {
                        return(jta.Name.Substring(jta.Name.LastIndexOf('.') + 1));
                    }
                    else
                    {
                        return(jta.Name);
                    }
                }
                var name = ce.Name;
                if (ce.TypeParameters.IsNotNullOrEmpty() && !Sk.IgnoreTypeArguments(ce))
                {
                    name += "$" + ce.TypeParameterCount;
                }
                return(name);
            }
            else
            {
                if (jta != null && jta.Name != null)
                {
                    return(jta.Name);
                }
                var name = ce.FullName;
                if (ce.ParentAssembly != null)
                {
                    var atts = ce.ParentAssembly.GetMetadatas <JsNamespaceAttribute>().Where(t => t.Namespace.IsNotNullOrEmpty() && t.JsNamespace != null).OrderByDescending(t => t.Namespace.Length).ToList();
                    if (atts.Count > 0)
                    {
                        foreach (var att in atts)
                        {
                            if (name.StartsWith(att.Namespace))
                            {
                                if (att.JsNamespace.IsNullOrEmpty())
                                {
                                    name = name.ReplaceFirst(att.Namespace + ".", "");
                                }
                                else
                                {
                                    name = name.ReplaceFirst(att.Namespace, att.JsNamespace);
                                }
                                break;
                            }
                        }
                    }
                }
                if (ce.TypeParameters.IsNotNullOrEmpty() && !Sk.IgnoreTypeArguments(ce))
                {
                    name += "$" + ce.TypeParameterCount;
                }
                return(name);
            }
        }
Exemple #2
0
        public static JsMemberExpression EntityMethodToJsFunctionRef(IMethod me)
        {
            var ownerType = me.GetDeclaringTypeDefinition();

            if (Sk.IsGlobalMethod(me))
            {
                var member = Js.Member(SkJs.GetEntityJsName(me));
                return(member);
            }
            else
            {
                var member = SkJs.EntityToMember(ownerType);
                if (!me.IsStatic)
                {
                    if (Sk.IsNativeType(ownerType))
                    {
                        member = member.Member("prototype");
                    }
                    else
                    {
                        member = member.Member("commonPrototype");
                    }
                }
                member = member.Member(SkJs.GetEntityJsName(me));
                return(member);
            }
        }
Exemple #3
0
        public static void CheckAddInvocation(ICSharpCode.NRefactory.Semantics.MemberResolveResult res, string methodName)
        {
            var    member       = res.Member;
            string typeFullName = SkJs.GetEntityJsName(member.DeclaringType);
            bool   exported     = Sk.IsJsExported(member);

            if (!exported || typeDefaultIsExported.Contains(typeFullName))
            {
                InvocationLocation Loc = null;
                var firstNode          = res.GetFirstNode();
                if (firstNode != null)
                {
                    Loc = new InvocationLocation {
                        FileName = firstNode.GetFileName(), Line = firstNode.StartLocation.Line
                    }
                }
                ;

                if (Loc != null)
                {
                    if (member.IsStatic)
                    {
                        methodName = "Static_" + methodName;
                    }
                    AddInvocation(typeFullName, methodName, Loc);
                }
            }
        }
Exemple #4
0
        public static string GetEntityJsName(IType ceref)
        {
            if (ceref is ITypeDefinition)
            {
                return(GetEntityJsName((ITypeDefinition)ceref));
            }
            var includeGenericArgs = !Sk.IgnoreTypeArguments(ceref.GetEntityType());

            return(ceref.GetEntityJsName(false, includeGenericArgs));
        }
Exemple #5
0
        static bool NeedsJsOverloadSuffix(IMethod me)
        {
            if (Sk.ForceMethodSuffix(me))
            {
                return(true);
            }

            if (me.IsIndexerAccessor()) //TODO: HACK: bug with metaspec - base_type is null in overridden indexer methods
            {
                //var x = me.HasFlag(entity_flags.f_override);
                //var y = me.HasFlag(entity_flags.f_method_Virtual);
                return(true);
            }
            if (me.Parameters.IsNullOrEmpty())
            {
                return(false);
            }
            var ce = me.GetDeclaringTypeDefinition();

            if (me.IsConstructor)
            {
                //var ctors = ce.GetConstructors().Where(t => !t.IsGenerated()).ToList(); //isGenerated - metaspec bug? parser generates empty ctor when a ctor with parameters is written
                var ctors = ce.GetConstructors().ToList(); //removed "IsGenerated" removed, because of default constructor of structs
                if (ctors.Count > 1)
                {
                    if (ctors.Any((m) => m.IsInternal))
                    {
                        if (!me.IsInternal && ctors.Count((m) => !m.IsInternal) == 1)
                        {
                            return(false);
                        }
                    }
                    return(true);
                }
                return(false);
            }
            var bm = me.GetBaseMethod();

            if (bm != null)
            {
                return(NeedsJsOverloadSuffix(bm));
            }
            if (ce != null)
            {
                var methods = ce.GetAllMethods(me.Name).Where(t => !t.IsExplicitInterfaceImplementation).ToList();
                if (methods.Count == 1)
                {
                    return(false);
                }
            }
            return(true);
        }
Exemple #6
0
        public static JsMemberExpression EntityToMember(IEntity me)
        {
            if (me == null)
            {
                return(null);
            }
            if (me.DeclaringType != null && (me.DeclaringType.IsGenericMethodArgument() || me.DeclaringType.IsGenericTypeParameter()))
            {
                var x = (JsMemberExpression)EntityTypeRefToMember(me.DeclaringType);
                var s = x.ToJs();
                return(x);
            }
            var name = GetEntityJsName(me);

            if (me is ITypeDefinition)
            {
                return(Js.Member(name));
            }
            else if (me.IsStatic())
            {
                var member = Js.Member(name);
                if (Sk.IsGlobalMember(me))
                {
                    return(member);
                }
                if (me is IMethod && Sk.ExtensionImplementedInInstance((IMethod)me))
                {
                    return(member);
                }
                member.PreviousMember = EntityToMember(me.GetDeclaringTypeDefinition());
                return(member);
            }
            else if (me.SymbolKind == SymbolKind.Constructor)
            {
                var att = Sk.GetJsMethodAttribute((IMethod)me);
                if (att != null && att.Name != null) //TODO: hack
                {
                    return(Js.Member(att.Name));
                }
                var ce     = me.GetDeclaringTypeDefinition();
                var member = EntityToMember(ce);
                var att2   = Sk.GetJsTypeAttribute(ce);
                if (att2 != null && att2.NativeConstructors)
                {
                    return(member);
                }
                member = member.Member(name);
                return(member);
            }
            return(Js.Member(name));
        }
Exemple #7
0
        static string GetEntityJsName(this IType ceref, bool shortName)
        {
            if (ceref is ITypeDefinition)
            {
                return(GetEntityJsName((ITypeDefinition)ceref, shortName));
            }

            var ce = ceref.GetEntityType();
            var includeGenericArgs = false;

            if (ce != null)
            {
                includeGenericArgs = !Sk.IgnoreTypeArguments(ceref.GetEntityType());
            }
            return(ceref.GetEntityJsName(shortName, includeGenericArgs));
        }
Exemple #8
0
        public static JsExpression EntityTypeRefToMember(IType typeRef, bool isGenericArgument)
        {
            if (typeRef == null)
            {
                return(null);
            }
            if (typeRef.Kind == TypeKind.Anonymous)
            {
                return(Js.Null());
            }
            if (isGenericArgument)
            {
                var code = Sk.GetGenericArugmentJsCode(typeRef.GetEntityType());
                if (code != null)
                {
                    return(Js.CodeExpression(code));
                }
            }
            var name = GetEntityJsName(typeRef);

            if (Sk.IsJsonMode(typeRef.GetEntityType()))
            {
                return(Js.String(name));
            }
            var member = Js.Members(name);

            if (typeRef.IsGenericTypeParameter())
            {
                if (member.PreviousMember == null)
                {
                    member.PreviousMember = Js.This();
                }
                else
                {
                    throw new Exception();
                }
            }
            var def = typeRef.GetDefinitionOrArrayType();

            if (def != null && Sk.IsClrType(def))
            {
                member = member.Member("ctor");
            }
            return(member);
        }
Exemple #9
0
        /// <summary>
        /// Indicates that object is IProperty that uses getter setter functions, and not native fields
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        internal static bool IsEntityFunctionProperty(IEntity entity, ICSharpCode.NRefactory.Semantics.ResolveResult scope)
        {
            var pe = entity as IProperty;

            if (pe != null)
            {
                var ce = pe.DeclaringType;
                if (ce != null && ce.Kind == TypeKind.Anonymous)
                {
                    var ce2 = scope.GetParentType();
                    if (ce2 != null && Sk.UseNativeJsons(ce2))
                    {
                        return(false);
                    }
                }
                return(!Sk.IsNativeField(pe) && !Sk.UseNativeIndexer(pe)); // && !Sk.IsNativeProperty(pe);
            }
            return(false);
        }
Exemple #10
0
        static string GetEntityJsNameV2(this IType type, bool shortName)
        {
            //if (type.Kind == TypeKind.Anonymous)
            //    return "Object";
            var ce = type.GetDefinition();

            var r  = type.ToTypeReference();
            var pr = r as ParameterizedTypeReference;

            var jta = ce.GetJsTypeAttribute();

            if (shortName)
            {
                if (jta != null && jta.Name != null)
                {
                    if (jta.Name.Contains('.'))
                    {
                        return(jta.Name.Substring(jta.Name.LastIndexOf('.') + 1));
                    }
                    else
                    {
                        return(jta.Name);
                    }
                }
                var name = ce.Name;
                if (ce.TypeParameters.IsNotNullOrEmpty() && !Sk.IgnoreTypeArguments(ce))
                {
                    if (pr == null || !UseFullParameterizedName(pr, type))
                    {
                        name += "$" + ce.TypeParameterCount;
                    }
                    else
                    {
                        name += pr.TypeArguments.StringConcat((p) =>
                        {
                            var p2 = p as ICSharpCode.NRefactory.TypeSystem.Implementation.GetClassTypeReference;
                            return(p2.FullTypeName.Name);
                        }, "$" + ce.TypeParameterCount + "$", "$", "");
                    }
                }
                return(name);
            }
            else
            {
                if (jta != null && jta.Name != null)
                {
                    return(jta.Name);
                }
                var name = ce.FullName;
                if (ce.ParentAssembly != null)
                {
                    var atts = ce.ParentAssembly.GetMetadatas <JsNamespaceAttribute>().Where(t => t.Namespace.IsNotNullOrEmpty() && t.JsNamespace != null).OrderByDescending(t => t.Namespace.Length).ToList();
                    if (atts.Count > 0)
                    {
                        foreach (var att in atts)
                        {
                            if (name.StartsWith(att.Namespace))
                            {
                                if (att.JsNamespace.IsNullOrEmpty())
                                {
                                    name = name.ReplaceFirst(att.Namespace + ".", "");
                                }
                                else
                                {
                                    name = name.ReplaceFirst(att.Namespace, att.JsNamespace);
                                }
                                break;
                            }
                        }
                    }
                }
                if (ce.TypeParameters.IsNotNullOrEmpty() && !Sk.IgnoreTypeArguments(ce))
                {
                    name += "$" + ce.TypeParameterCount;
                }
                return(name);
            }
        }
Exemple #11
0
        public static string GetEntityJsName(IMethod me2)
        {
            IMethod me;

            if (me2.IsConstructor && me2.DeclaringType.Kind == TypeKind.TypeParameter)//happens when invoking new T();
            {
                me = me2;
            }
            else
            {
                me = (IMethod)me2.MemberDefinition;
            }
            var name = me.Name;

            if (name != null)
            {
                if (name == ".ctor")
                {
                    name = "ctor";
                }
                else if (name == ".cctor")
                {
                    name = "cctor";
                }
            }

            var att = me.GetMetadata <JsMethodAttribute>(true);

            if (att != null && att.Name != null)
            {
                name = att.Name;
            }
            else if (me.DeclaringType.Kind == TypeKind.Delegate && me.Name == "Invoke")
            {
                return("");
            }
            else if (me.DeclaringType.IsGenericMethodArgument()) //happens when invoking new T() in method MyMethod<T>();
            {
                name = GetEntityJsName(me.DeclaringType);
            }
            //else if (me.DeclaringType.IsGenericTypeParameter()) //happens when invoking new T() in class List<T>;
            //{
            //    name = EntityTypeRefToMember(me.DeclaringType);
            //}
            else
            {
                var owner = me.GetOwner();
                if (owner != null && owner is IProperty)
                {
                    var pe = (IProperty)owner;
                    if (pe.SymbolKind == SymbolKind.Indexer && Sk.UseNativeIndexer(pe))
                    {
                        return("");
                    }
                    name = GetEntityJsName(pe);
                    if (me.IsGetter())
                    {
                        name = "get_" + name;
                    }
                    else
                    {
                        name = "set_" + name;
                    }
                }
            }

            // 前面有地方被统一修改
            if (me.DeclaringType.FullName == "System.Enum")
            {
                if (name == "ToString" || name == "toString")
                {
                    name = "System.Enum.GetName";
                }
            }

            if (Sk.NewInterfaceImplementation)
            {
                name = SkJs.GetMethodPrefix(me) + name;
            }

            if (!Sk.UseNativeOverloads(me))
            {
                if (me.TypeParameters.IsNotNullOrEmpty())
                {
                    name += "$" + me.TypeParameters.Count;
                }
                name += SkJs.GetOverloadedMethodSuffix(me);
            }
            return(name);
        }