Ejemplo n.º 1
0
        public static string ToJsName(IType type, IEmitter emitter, bool asDefinition = false, bool excludens = false, bool isAlias = false, bool skipMethodTypeParam = false, bool removeScope = true, bool nomodule = false, bool ignoreLiteralName = true, bool ignoreVirtual = false, bool excludeTypeOnly = false)
        {
            var    itypeDef = type.GetDefinition();
            H5Type h5Type   = emitter.H5Types.Get(type, true);

            if (itypeDef != null)
            {
                string globalTarget = H5Types.GetGlobalTarget(itypeDef, null, removeScope);

                if (globalTarget != null)
                {
                    if (h5Type != null && !nomodule)
                    {
                        bool customName;
                        globalTarget = H5Types.AddModule(globalTarget, h5Type, excludens, false, out customName);
                    }
                    return(globalTarget);
                }
            }

            if (itypeDef != null && itypeDef.Attributes.Any(a => a.AttributeType.FullName == "H5.NonScriptableAttribute"))
            {
                throw new EmitterException(emitter.Translator.EmitNode, "Type " + type.FullName + " is marked as not usable from script");
            }

            if (type.Kind == TypeKind.Array)
            {
                if (type is ArrayType arrayType && arrayType.ElementType != null)
                {
                    string typedArrayName;
                    if (emitter.AssemblyInfo.UseTypedArrays && (typedArrayName = Helpers.GetTypedArrayName(arrayType.ElementType)) != null)
                    {
                        return(typedArrayName);
                    }

                    var elementAlias = H5Types.ToJsName(arrayType.ElementType, emitter, asDefinition, excludens, isAlias, skipMethodTypeParam, excludeTypeOnly: excludeTypeOnly);

                    if (isAlias)
                    {
                        return($"{elementAlias}$Array{(arrayType.Dimensions > 1 ? "$" + arrayType.Dimensions : "")}");
                    }

                    if (arrayType.Dimensions > 1)
                    {
                        return(string.Format(JS.Types.System.Array.TYPE + "({0}, {1})", elementAlias, arrayType.Dimensions));
                    }
                    return(string.Format(JS.Types.System.Array.TYPE + "({0})", elementAlias));
                }

                return(JS.Types.ARRAY);
            }

            if (type.Kind == TypeKind.Delegate)
            {
                return(JS.Types.FUNCTION);
            }

            if (type.Kind == TypeKind.Dynamic)
            {
                return(JS.Types.System.Object.NAME);
            }

            if (type is ByReferenceType)
            {
                return(H5Types.ToJsName(((ByReferenceType)type).ElementType, emitter, asDefinition, excludens, isAlias, skipMethodTypeParam, excludeTypeOnly: excludeTypeOnly));
            }

            if (ignoreLiteralName)
            {
                var isObjectLiteral = itypeDef != null && emitter.Validator.IsObjectLiteral(itypeDef);
                var isPlainMode     = isObjectLiteral && emitter.Validator.GetObjectCreateMode(emitter.GetTypeDefinition(type)) == 0;

                if (isPlainMode)
                {
                    return("System.Object");
                }
            }

            if (type.Kind == TypeKind.Anonymous)
            {
                if (type is AnonymousType at && emitter.AnonymousTypes.ContainsKey(at))
                {
                    return(emitter.AnonymousTypes[at].Name);
                }
                else
                {
                    return(JS.Types.System.Object.NAME);
                }
            }