Example #1
0
        protected void VisitArrayCreateExpression()
        {
            ArrayCreateExpression arrayCreateExpression = ArrayCreateExpression;
            var rr   = ArrayCreateResolveResult ?? (Emitter.Resolver.ResolveNode(arrayCreateExpression) as ArrayCreateResolveResult);
            var at   = (ArrayType)rr.Type;
            var rank = arrayCreateExpression.Arguments.Count;

            if (arrayCreateExpression.Initializer.IsNull && rank == 1)
            {
                string typedArrayName = null;
                if (Emitter.AssemblyInfo.UseTypedArrays && (typedArrayName = Helpers.GetTypedArrayName(at.ElementType)) != null)
                {
                    Write(JS.Types.System.Array.INIT);
                    WriteOpenParentheses();

                    Write("new ", typedArrayName, "(");
                    if (ArrayCreateResolveResult != null)
                    {
                        AttributeCreateBlock.WriteResolveResult(ArrayCreateResolveResult.SizeArguments.First(), this);
                    }
                    else
                    {
                        arrayCreateExpression.Arguments.First().AcceptVisitor(Emitter);
                    }
                    Write(")");
                    Write(", ");
                    Write(H5Types.ToJsName(at.ElementType, Emitter));
                    Write(")");
                }
                else
                {
                    Write(JS.Types.System.Array.INIT);
                    WriteOpenParentheses();

                    if (ArrayCreateResolveResult != null)
                    {
                        AttributeCreateBlock.WriteResolveResult(ArrayCreateResolveResult.SizeArguments.First(), this);
                    }
                    else
                    {
                        arrayCreateExpression.Arguments.First().AcceptVisitor(Emitter);
                    }
                    WriteComma();

                    var def = Inspector.GetDefaultFieldValue(at.ElementType, arrayCreateExpression.Type);
                    if (def == at.ElementType || def is RawValue)
                    {
                        WriteFunction();
                        WriteOpenCloseParentheses();
                        BeginBlock();
                        WriteReturn(true);
                        if (def is RawValue)
                        {
                            Write(def.ToString());
                        }
                        else
                        {
                            Write(Inspector.GetStructDefaultValue(at.ElementType, Emitter));
                        }

                        WriteSemiColon();
                        WriteNewLine();
                        EndBlock();
                    }
                    else
                    {
                        WriteScript(def);
                    }

                    Write(", ");
                    Write(H5Types.ToJsName(at.ElementType, Emitter));

                    Write(")");
                }
                return;
            }

            if (at.Dimensions > 1)
            {
                Write(JS.Types.System.Array.CREATE);
                WriteOpenParentheses();

                var def = Inspector.GetDefaultFieldValue(at.ElementType, arrayCreateExpression.Type);
                var defaultInitializer = new PrimitiveExpression(def, "?");

                if (def == at.ElementType || def is RawValue)
                {
                    WriteFunction();
                    WriteOpenCloseParentheses();
                    BeginBlock();
                    WriteReturn(true);
                    if (def is RawValue)
                    {
                        Write(def.ToString());
                    }
                    else
                    {
                        Write(Inspector.GetStructDefaultValue(at.ElementType, Emitter));
                    }

                    WriteSemiColon();
                    WriteNewLine();
                    EndBlock();
                }
                else if (defaultInitializer.Value is IType)
                {
                    Write(Inspector.GetStructDefaultValue((IType)defaultInitializer.Value, Emitter));
                }
                else if (defaultInitializer.Value is RawValue)
                {
                    Write(defaultInitializer.Value.ToString());
                }
                else
                {
                    defaultInitializer.AcceptVisitor(Emitter);
                }

                WriteComma();
            }
            else
            {
                Write(JS.Types.System.Array.INIT);
                WriteOpenParentheses();
            }

            if (rr.InitializerElements != null && rr.InitializerElements.Count > 0)
            {
                string typedArrayName = null;
                bool   isTyped        = Emitter.AssemblyInfo.UseTypedArrays && (typedArrayName = Helpers.GetTypedArrayName(at.ElementType)) != null;
                if (isTyped)
                {
                    Write("new ", typedArrayName, "(");
                }

                WriteOpenBracket();

                if (ArrayCreateResolveResult != null)
                {
                    bool needComma = false;
                    foreach (ResolveResult item in ArrayCreateResolveResult.InitializerElements)
                    {
                        if (needComma)
                        {
                            WriteComma();
                        }

                        needComma = true;

                        AttributeCreateBlock.WriteResolveResult(item, this);
                    }
                }
                else
                {
                    var elements = arrayCreateExpression.Initializer.Elements;
                    new ExpressionListBlock(Emitter, elements, null, null, 0).Emit();
                }

                WriteCloseBracket();

                if (isTyped)
                {
                    Write(")");
                }
            }
            else if (at.Dimensions > 1)
            {
                Write("null");
            }
            else
            {
                Write("[]");
            }

            Write(", ");
            Write(H5Types.ToJsName(at.ElementType, Emitter));

            if (at.Dimensions > 1)
            {
                Emitter.Comma = true;

                for (int i = 0; i < rr.SizeArguments.Count; i++)
                {
                    var a = rr.SizeArguments[i];
                    EnsureComma(false);

                    if (a.IsCompileTimeConstant)
                    {
                        this.Write(a.ConstantValue);
                    }
                    else if (ArrayCreateResolveResult != null)
                    {
                        AttributeCreateBlock.WriteResolveResult(ArrayCreateResolveResult.SizeArguments[i], this);
                    }
                    else if (arrayCreateExpression.Arguments.Count > i)
                    {
                        var arg = arrayCreateExpression.Arguments.ElementAt(i);

                        if (arg != null)
                        {
                            arg.AcceptVisitor(Emitter);
                        }
                    }
                    Emitter.Comma = true;
                }
            }

            Write(")");
            Emitter.Comma = false;
        }
Example #2
0
        protected override void DoEmit()
        {
            var elements = ArrayInitializerExpression.Elements;
            var first    = elements.Count > 0 ? elements.First() : null;

            var isObjectInitializer = first is NamedExpression || first is NamedArgumentExpression;
            var rr     = Emitter.Resolver.ResolveNode(ArrayInitializerExpression) as ArrayCreateResolveResult;
            var at     = rr != null ? (ArrayType)rr.Type : null;
            var create = at != null && at.Dimensions > 1;

            if (rr != null)
            {
            }
            if (!isObjectInitializer || ArrayInitializerExpression.IsSingleElement)
            {
                if (at != null)
                {
                    Write(create ? JS.Types.System.Array.CREATE : JS.Types.System.Array.INIT);
                    WriteOpenParentheses();
                }

                if (create)
                {
                    var defaultInitializer = new PrimitiveExpression(Inspector.GetDefaultFieldValue(at.ElementType, AstType.Null), "?");

                    if (defaultInitializer.Value is IType)
                    {
                        Write(Inspector.GetStructDefaultValue((IType)defaultInitializer.Value, Emitter));
                    }
                    else if (defaultInitializer.Value is RawValue)
                    {
                        Write(defaultInitializer.Value.ToString());
                    }
                    else
                    {
                        defaultInitializer.AcceptVisitor(Emitter);
                    }

                    WriteComma();
                }

                Write("[");
            }
            else
            {
                BeginBlock();
            }

            new ExpressionListBlock(Emitter, elements, null, null, 0, elements.Count > 2).Emit();

            if (!isObjectInitializer || ArrayInitializerExpression.IsSingleElement)
            {
                Write("]");
                if (at != null)
                {
                    Write(", ");
                    Write(H5Types.ToJsName(at.ElementType, Emitter));

                    if (create)
                    {
                        Emitter.Comma = true;

                        for (int i = 0; i < rr.SizeArguments.Count; i++)
                        {
                            var a = rr.SizeArguments[i];
                            EnsureComma(false);

                            if (a.IsCompileTimeConstant)
                            {
                                Write(a.ConstantValue);
                            }
                            else
                            {
                                AttributeCreateBlock.WriteResolveResult(rr.SizeArguments[i], this);
                            }
                            Emitter.Comma = true;
                        }
                    }

                    Write(")");
                }
            }
            else
            {
                WriteNewLine();
                EndBlock();
            }
        }
Example #3
0
        protected virtual void WriteObjectInitializer(IEnumerable <Expression> expressions, TypeDefinition type, InvocationResolveResult rr, bool withCtor)
        {
            bool          needComma       = false;
            List <string> names           = new List <string>();
            var           isObjectLiteral = Emitter.Validator.IsObjectLiteral(type);

            if (!withCtor && rr != null && ObjectCreateExpression.Arguments.Count > 0)
            {
                var args      = ObjectCreateExpression.Arguments.ToList();
                var arrIsOpen = false;
                for (int i = 0; i < args.Count; i++)
                {
                    Expression expression = args[i];
                    var        p          = rr.Member.Parameters[i < rr.Member.Parameters.Count ? i : (rr.Member.Parameters.Count - 1)];
                    var        name       = p.Name;

                    if (p.Type.FullName == "H5.ObjectInitializationMode" ||
                        p.Type.FullName == "H5.ObjectCreateMode")
                    {
                        continue;
                    }

                    if (needComma)
                    {
                        WriteComma();
                    }

                    needComma = true;

                    if (p.IsParams && !arrIsOpen)
                    {
                        arrIsOpen = true;
                        Write("[");
                    }

                    Write(name, ": ");
                    expression.AcceptVisitor(Emitter);

                    names.Add(name);
                }

                if (arrIsOpen)
                {
                    Write("]");
                }
            }

            if (expressions != null)
            {
                foreach (Expression item in expressions)
                {
                    NamedExpression         namedExression          = item as NamedExpression;
                    NamedArgumentExpression namedArgumentExpression = item as NamedArgumentExpression;
                    string name = namedExression != null ? namedExression.Name : namedArgumentExpression.Name;

                    if (Emitter.Resolver.ResolveNode(item) is MemberResolveResult itemrr)
                    {
                        var  oc = OverloadsCollection.Create(Emitter, itemrr.Member);
                        bool forceObjectLiteral = itemrr.Member is IProperty && !itemrr.Member.Attributes.Any(attr => attr.AttributeType.FullName == "H5.NameAttribute") && !Emitter.Validator.IsObjectLiteral(itemrr.Member.DeclaringTypeDefinition);

                        name = oc.GetOverloadName(isObjectLiteral: forceObjectLiteral);
                    }

                    if (needComma)
                    {
                        WriteComma();
                    }

                    needComma = true;

                    Expression expression = namedExression != null ? namedExression.Expression : namedArgumentExpression.Expression;

                    WriteIdentifier(name, true, true);
                    Write(": ");
                    expression.AcceptVisitor(Emitter);

                    names.Add(name);
                }
            }

            if (isObjectLiteral)
            {
                var key   = H5Types.GetTypeDefinitionKey(type);
                var tinfo = Emitter.Types.FirstOrDefault(t => t.Key == key);

                var mode = 0;
                if (rr != null)
                {
                    if (rr.Member.Parameters.Count > 0)
                    {
                        var prm = rr.Member.Parameters.FirstOrDefault(p => p.Type.FullName == "H5.ObjectInitializationMode");

                        if (prm != null)
                        {
                            var prmIndex = rr.Member.Parameters.IndexOf(prm);
                            var arg      = rr.Arguments.FirstOrDefault(a =>
                            {
                                if (a is NamedArgumentResolveResult)
                                {
                                    return(((NamedArgumentResolveResult)a).ParameterName == prm.Name);
                                }

                                return(prmIndex == rr.Arguments.IndexOf(a));
                            });

                            if (arg != null && arg.ConstantValue != null && arg.ConstantValue is int)
                            {
                                mode = (int)arg.ConstantValue;
                            }
                        }
                    }
                    else if (type != null)
                    {
                        mode = Emitter.Validator.GetObjectInitializationMode(type);
                    }
                }

                if (tinfo == null)
                {
                    if (mode == 2)
                    {
                        var properties = rr.Member.DeclaringTypeDefinition.GetProperties(null, GetMemberOptions.IgnoreInheritedMembers);
                        foreach (var prop in properties)
                        {
                            var name = OverloadsCollection.Create(Emitter, prop).GetOverloadName();

                            if (names.Contains(name))
                            {
                                continue;
                            }

                            if (needComma)
                            {
                                WriteComma();
                            }

                            needComma = true;

                            WriteIdentifier(name, true, true);
                            Write(": ");

                            var argType  = prop.ReturnType;
                            var defValue = Inspector.GetDefaultFieldValue(argType, null);
                            if (defValue == argType)
                            {
                                Write(Inspector.GetStructDefaultValue(argType, Emitter));
                            }
                            else
                            {
                                Write(defValue);
                            }
                        }
                    }

                    return;
                }

                if (mode != 0)
                {
                    var members = tinfo.InstanceConfig.Fields.Concat(tinfo.InstanceConfig.Properties);

                    if (members.Any())
                    {
                        foreach (var member in members)
                        {
                            if (mode == 1 && (member.VarInitializer == null || member.VarInitializer.Initializer.IsNull) && !member.IsPropertyInitializer)
                            {
                                continue;
                            }

                            var name = member.GetName(Emitter);

                            if (names.Contains(name))
                            {
                                continue;
                            }

                            if (needComma)
                            {
                                WriteComma();
                            }

                            needComma = true;

                            WriteIdentifier(name, true, true);
                            Write(": ");

                            if (mode == 2 && (member.Initializer == null || member.Initializer.IsNull) && !(member.VarInitializer == null || member.VarInitializer.Initializer.IsNull))
                            {
                                var argType  = Emitter.Resolver.ResolveNode(member.VarInitializer).Type;
                                var defValue = Inspector.GetDefaultFieldValue(argType, null);
                                if (defValue == argType)
                                {
                                    Write(Inspector.GetStructDefaultValue(argType, Emitter));
                                }
                                else
                                {
                                    Write(defValue);
                                }
                            }
                            else
                            {
                                if (member.Initializer is PrimitiveExpression primitiveExpr && primitiveExpr.Value is AstType)
                                {
                                    Write(Inspector.GetStructDefaultValue((AstType)primitiveExpr.Value, Emitter));
                                }
                                else if (member.Initializer != null)
                                {
                                    member.Initializer.AcceptVisitor(Emitter);
                                }
                                else
                                {
                                    Write("null");
                                }
                            }
                        }
                    }