Beispiel #1
0
        protected virtual void EmitCtorForStaticClass()
        {
            var ctor = this.TypeInfo.StaticCtor;

            if (ctor != null && ctor.Body.HasChildren)
            {
                this.ResetLocals();
                var prevNamesMap = this.BuildLocalsNamesMap();
                this.Write("constructor");
                this.WriteColon();
                this.WriteFunction();
                this.WriteOpenCloseParentheses(true);

                this.BeginBlock();

                ctor.Body.AcceptChildren(this.Emitter);

                this.EndBlock();
                this.ClearLocalsNamesMap(prevNamesMap);
                this.Emitter.Comma = true;
            }

            var injectors = this.GetInjectors();
            IEnumerable<string> fieldsInjectors = null;

            var fieldBlock = new FieldBlock(this.Emitter, this.TypeInfo, true, true);
            fieldBlock.Emit();

            fieldsInjectors = fieldBlock.Injectors;

            if (fieldBlock.WasEmitted)
            {
                this.Emitter.Comma = true;
            }

            if (this.TypeInfo.StaticConfig.HasConfigMembers || injectors.Any() || fieldsInjectors.Any())
            {
                this.EnsureComma();

                if (this.TypeInfo.StaticMethods.Any(m => m.Value.Any(subm => this.Emitter.GetEntityName(subm) == "config")) ||
                    this.TypeInfo.StaticConfig.Fields.Any(m => m.GetName(this.Emitter) == "config"))
                {
                    this.Write("$");
                }

                this.Write("config");

                this.WriteColon();
                this.BeginBlock();

                if (this.TypeInfo.StaticConfig.HasConfigMembers)
                {
                    var configBlock = new FieldBlock(this.Emitter, this.TypeInfo, true, false);
                    configBlock.Emit();

                    if (configBlock.Injectors.Count > 0)
                    {
                        injectors = configBlock.Injectors.Concat(injectors);
                    }

                    if (configBlock.WasEmitted)
                    {
                        this.Emitter.Comma = true;
                    }
                }

                if (fieldsInjectors.Any())
                {
                    injectors = fieldsInjectors.Concat(injectors);
                }

                if (injectors.Count() > 0)
                {
                    this.EnsureComma();

                    this.Write("init");
                    this.WriteColon();
                    this.WriteFunction();
                    this.WriteOpenParentheses();
                    this.WriteCloseParentheses();
                    this.WriteSpace();
                    this.BeginBlock();
                    foreach (var fn in injectors)
                    {
                        this.Write(fn);
                        this.WriteNewLine();
                    }
                    this.EndBlock();
                    this.Emitter.Comma = true;
                }
                this.WriteNewLine();
                this.EndBlock();
            }
        }
Beispiel #2
0
        protected virtual void EmitInitMembers()
        {
            var injectors = this.GetInjectors();
            IEnumerable<string> fieldsInjectors = null;

            var fieldBlock = new FieldBlock(this.Emitter, this.TypeInfo, false, true);
            fieldBlock.Emit();

            fieldsInjectors = fieldBlock.Injectors;

            if (fieldBlock.WasEmitted)
            {
                this.Emitter.Comma = true;
            }

            if (!this.TypeInfo.InstanceConfig.HasConfigMembers && !injectors.Any() && !fieldsInjectors.Any())
            {
                return;
            }

            if (this.TypeInfo.InstanceMethods.Any(m => m.Value.Any(subm => this.Emitter.GetEntityName(subm) == "config")) ||
                this.TypeInfo.InstanceConfig.Fields.Any(m => m.GetName(this.Emitter) == "config"))
            {
                this.Write("$");
            }

            this.EnsureComma();
            this.Write("config");

            this.WriteColon();
            this.BeginBlock();

            var changeCase = this.Emitter.ChangeCase;
            var baseType = this.Emitter.GetBaseTypeDefinition();

            if (this.TypeInfo.InstanceConfig.HasConfigMembers)
            {
                var configBlock = new FieldBlock(this.Emitter, this.TypeInfo, false, false);
                configBlock.Emit();

                if (configBlock.Injectors.Count > 0)
                {
                    injectors = configBlock.Injectors.Concat(injectors);
                }

                if (configBlock.WasEmitted)
                {
                    this.Emitter.Comma = true;
                }
            }

            if (fieldsInjectors.Any())
            {
                injectors = fieldsInjectors.Concat(injectors);
            }

            if (injectors.Count() > 0)
            {
                this.EnsureComma();
                this.Write("init");
                this.WriteColon();
                this.WriteFunction();
                this.WriteOpenParentheses();
                this.WriteCloseParentheses();
                this.WriteSpace();
                this.BeginBlock();

                foreach (var fn in injectors)
                {
                    this.Write(fn);
                    this.WriteNewLine();
                }

                this.EndBlock();

                this.Emitter.Comma = true;
            }

            this.WriteNewLine();
            this.EndBlock();
        }
Beispiel #3
0
        protected virtual void EmitCtorForInstantiableClass()
        {
            var baseType        = this.Emitter.GetBaseTypeDefinition();
            var typeDef         = this.Emitter.GetTypeDefinition();
            var isObjectLiteral = this.Emitter.Validator.IsObjectLiteral(typeDef);
            var isPlainMode     = this.Emitter.Validator.GetObjectCreateMode(typeDef) == 0;

            var ctorWrappers = isObjectLiteral ? new string[0] : this.EmitInitMembers().ToArray();

            if (!this.TypeInfo.HasRealInstantiable(this.Emitter) && ctorWrappers.Length == 0 || isObjectLiteral && isPlainMode)
            {
                if (this.ctorHeader)
                {
                    this.WriteNewLine();
                    this.EndBlock();
                }
                return;
            }

            bool forceDefCtor = isObjectLiteral && this.Emitter.Validator.GetObjectCreateMode(typeDef) == 1 && this.TypeInfo.Ctors.Count == 0;

            if (typeDef.IsValueType || forceDefCtor || (this.TypeInfo.Ctors.Count == 0 && ctorWrappers.Length > 0))
            {
                this.TypeInfo.Ctors.Add(new ConstructorDeclaration
                {
                    Modifiers = Modifiers.Public,
                    Body      = new BlockStatement()
                });
            }

            if (!this.ctorHeader && this.TypeInfo.Ctors.Count > 0)
            {
                this.EnsureComma();
                this.ctorHeader = true;
                this.Write(JS.Fields.CTORS);
                this.WriteColon();
                this.BeginBlock();
            }

            this.Emitter.InConstructor = true;
            foreach (var ctor in this.TypeInfo.Ctors)
            {
                var oldRules = this.Emitter.Rules;

                if (ctor.Body.HasChildren)
                {
                    var rr = this.Emitter.Resolver.ResolveNode(ctor, this.Emitter) as MemberResolveResult;
                    if (rr != null)
                    {
                        this.Emitter.Rules = Rules.Get(this.Emitter, rr.Member);
                    }
                }

                this.EnsureComma();
                this.ResetLocals();
                var prevMap      = this.BuildLocalsMap();
                var prevNamesMap = this.BuildLocalsNamesMap();
                this.AddLocals(ctor.Parameters, ctor.Body);

                var ctorName = JS.Funcs.CONSTRUCTOR;

                if (this.TypeInfo.Ctors.Count > 1 && ctor.Parameters.Count > 0)
                {
                    var overloads = OverloadsCollection.Create(this.Emitter, ctor);
                    ctorName = overloads.GetOverloadName();
                }

                XmlToJsDoc.EmitComment(this, ctor);
                this.Write(ctorName);

                this.WriteColon();
                this.WriteFunction();

                int pos = this.Emitter.Output.Length;
                this.EmitMethodParameters(ctor.Parameters, null, ctor);
                var ctorParams = this.Emitter.Output.ToString().Substring(pos);

                this.WriteSpace();
                this.BeginBlock();
                var len            = this.Emitter.Output.Length;
                var requireNewLine = false;

                var         noThisInvocation = ctor.Initializer == null || ctor.Initializer.IsNull || ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base;
                IWriterInfo oldWriter        = null;
                if (ctorWrappers.Length > 0 && noThisInvocation)
                {
                    oldWriter = this.SaveWriter();
                    this.NewWriter();
                }

                this.ConvertParamsToReferences(ctor.Parameters);

                if (len != this.Emitter.Output.Length)
                {
                    requireNewLine = true;
                }

                if (isObjectLiteral)
                {
                    if (requireNewLine)
                    {
                        this.WriteNewLine();
                    }

                    this.Write("var " + JS.Vars.D_THIS + " = ");

                    var isBaseObjectLiteral = baseType != null && this.Emitter.Validator.IsObjectLiteral(baseType);
                    if (isBaseObjectLiteral && baseType != null && (!this.Emitter.Validator.IsExternalType(baseType) || this.Emitter.Validator.IsBridgeClass(baseType)) ||
                        (ctor.Initializer != null && ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.This))
                    {
                        this.EmitBaseConstructor(ctor, ctorName, true);
                    }
                    else if (isBaseObjectLiteral && baseType != null && ctor.Initializer != null &&
                             ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base)
                    {
                        this.EmitExternalBaseCtor(ctor, ref requireNewLine);
                    }
                    else
                    {
                        this.Write("{ };");
                    }

                    this.WriteNewLine();

                    string name = this.Emitter.Validator.GetCustomTypeName(typeDef, this.Emitter, false);
                    if (name.IsEmpty())
                    {
                        name = BridgeTypes.DefinitionToJsName(this.TypeInfo.Type, this.Emitter);
                    }

                    this.Write(JS.Vars.D_THIS + "." + JS.Funcs.GET_TYPE + " = function () { return " + name + "; };");

                    this.WriteNewLine();
                    this.Write("(function ()");
                    this.BeginBlock();
                    requireNewLine = false;
                }

                var beginPosition = this.Emitter.Output.Length;

                if (noThisInvocation)
                {
                    if (requireNewLine)
                    {
                        this.WriteNewLine();
                    }

                    if (isObjectLiteral)
                    {
                        var fieldBlock = new FieldBlock(this.Emitter, this.TypeInfo, false, false, true);
                        fieldBlock.Emit();

                        var properties = this.TypeInfo.InstanceProperties;

                        var names = new List <string>(properties.Keys);

                        foreach (var name in names)
                        {
                            var props = properties[name];

                            foreach (var prop in props)
                            {
                                var p = prop as PropertyDeclaration;
                                if (p != null)
                                {
                                    if (p.Getter.Body.IsNull && p.Setter.Body.IsNull)
                                    {
                                        continue;
                                    }

                                    this.Write(JS.Types.Object.DEFINEPROPERTY);
                                    this.WriteOpenParentheses();
                                    this.Write("this, ");
                                    this.WriteScript(OverloadsCollection.Create(this.Emitter, p).GetOverloadName());
                                    this.WriteComma();
                                    this.Emitter.Comma = false;
                                    this.BeginBlock();
                                    var memberResult = this.Emitter.Resolver.ResolveNode(p, this.Emitter) as MemberResolveResult;
                                    var block        = new VisitorPropertyBlock(this.Emitter, p);
                                    block.EmitPropertyMethod(p, p.Getter, ((IProperty)memberResult.Member).Getter, false, true);
                                    block.EmitPropertyMethod(p, p.Setter, ((IProperty)memberResult.Member).Setter, true, true);
                                    this.EnsureComma(true);
                                    this.Write(JS.Fields.ENUMERABLE + ": true");
                                    this.WriteNewLine();
                                    this.EndBlock();
                                    this.WriteCloseParentheses();
                                    this.Write(";");
                                    this.WriteNewLine();
                                }
                            }
                        }
                    }
                    else
                    {
                        this.Write("this." + JS.Funcs.INITIALIZE + "();");
                        requireNewLine = true;
                    }
                }

                if (!isObjectLiteral)
                {
                    if (baseType != null && (!this.Emitter.Validator.IsExternalType(baseType) || this.Emitter.Validator.IsBridgeClass(baseType)) ||
                        (ctor.Initializer != null && ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.This))
                    {
                        if (requireNewLine)
                        {
                            this.WriteNewLine();
                            requireNewLine = false;
                        }
                        this.EmitBaseConstructor(ctor, ctorName, false);
                    }
                    else if (baseType != null && ctor.Initializer != null &&
                             ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base)
                    {
                        this.EmitExternalBaseCtor(ctor, ref requireNewLine);
                    }
                }

                var script = this.Emitter.GetScript(ctor);
                var hasAdditionalIndent = false;

                if (script == null)
                {
                    if (ctor.Body.HasChildren)
                    {
                        if (requireNewLine)
                        {
                            this.WriteNewLine();
                        }

                        ctor.Body.AcceptChildren(this.Emitter);

                        if (!this.Emitter.IsAsync)
                        {
                            hasAdditionalIndent = this.Emitter.TempVariables.Count > 0;
                            this.EmitTempVars(beginPosition, true);
                        }
                    }
                    else if (requireNewLine)
                    {
                        this.WriteNewLine();
                    }
                }
                else
                {
                    if (requireNewLine)
                    {
                        this.WriteNewLine();
                    }

                    this.WriteLines(script);
                }

                if (oldWriter != null)
                {
                    this.WrapBody(oldWriter, ctorWrappers, ctorParams);
                }

                if (isObjectLiteral)
                {
                    if (requireNewLine)
                    {
                        this.WriteNewLine();
                    }
                    this.EndBlock();
                    this.Write(")." + JS.Funcs.CALL + "(" + JS.Vars.D_THIS + ");");
                    this.WriteNewLine();
                    this.Write("return " + JS.Vars.D_THIS + ";");
                    this.WriteNewLine();
                }

                if (hasAdditionalIndent)
                {
                    this.Indent();
                }

                this.EndBlock();
                this.Emitter.Comma = true;
                this.ClearLocalsMap(prevMap);
                this.ClearLocalsNamesMap(prevNamesMap);

                this.Emitter.Rules = oldRules;
            }

            this.Emitter.InConstructor = false;

            if (this.ctorHeader)
            {
                this.WriteNewLine();
                this.EndBlock();
            }
        }
Beispiel #4
0
        protected virtual void EmitCtorForStaticClass()
        {
            var injectors = this.GetInjectors();
            IEnumerable <string> fieldsInjectors = null;

            var fieldBlock = new FieldBlock(this.Emitter, this.TypeInfo, true, true);

            fieldBlock.Emit();

            fieldsInjectors = fieldBlock.Injectors;

            if (fieldBlock.WasEmitted)
            {
                this.Emitter.Comma = true;
            }

            bool ctorHeader = false;

            if (this.TypeInfo.StaticConfig.HasConfigMembers || injectors.Any() || fieldsInjectors.Any())
            {
                this.EnsureComma();

                if (this.TypeInfo.StaticConfig.HasConfigMembers)
                {
                    var configBlock = new FieldBlock(this.Emitter, this.TypeInfo, true, false);
                    configBlock.ClearTempVariables = false;
                    configBlock.Emit();

                    if (configBlock.Injectors.Count > 0)
                    {
                        injectors = configBlock.Injectors.Concat(injectors);
                    }

                    if (configBlock.WasEmitted)
                    {
                        this.Emitter.Comma = true;
                    }
                }

                if (fieldsInjectors.Any())
                {
                    injectors = fieldsInjectors.Concat(injectors);
                }

                if (injectors.Count() > 0)
                {
                    this.EnsureComma();

                    ctorHeader = true;
                    this.Write(JS.Fields.CTORS);
                    this.WriteColon();
                    this.BeginBlock();

                    this.Write(JS.Funcs.INIT);
                    this.WriteColon();
                    this.WriteFunction();
                    this.WriteOpenParentheses();
                    this.WriteCloseParentheses();
                    this.WriteSpace();
                    this.BeginBlock();

                    if (this.Emitter.TempVariables != null)
                    {
                        this.SimpleEmitTempVars();
                        this.Emitter.TempVariables = new Dictionary <string, bool>();
                    }

                    foreach (var fn in injectors)
                    {
                        this.Write(WriteIndentToString(fn, this.Level - 1));
                        this.WriteNewLine();
                    }
                    this.EndBlock();
                    this.Emitter.Comma = true;
                }
            }

            var ctor = this.TypeInfo.StaticCtor;

            if (ctor != null && ctor.Body.HasChildren)
            {
                this.EnsureComma();

                if (!ctorHeader)
                {
                    ctorHeader = true;
                    this.Write(JS.Fields.CTORS);
                    this.WriteColon();
                    this.BeginBlock();
                }

                this.ResetLocals();
                var prevNamesMap = this.BuildLocalsNamesMap();
                this.Write(JS.Funcs.CONSTRUCTOR);
                this.WriteColon();
                this.WriteFunction();
                this.WriteOpenCloseParentheses(true);

                this.BeginBlock();
                var beginPosition = this.Emitter.Output.Length;

                var oldRules = this.Emitter.Rules;
                var rr       = this.Emitter.Resolver.ResolveNode(ctor, this.Emitter) as MemberResolveResult;
                if (rr != null)
                {
                    this.Emitter.Rules = Rules.Get(this.Emitter, rr.Member);
                }

                ctor.Body.AcceptChildren(this.Emitter);

                if (!this.Emitter.IsAsync)
                {
                    var indent = this.Emitter.TempVariables.Count > 0;
                    this.EmitTempVars(beginPosition, true);

                    if (indent)
                    {
                        this.Indent();
                    }
                }

                this.Emitter.Rules = oldRules;
                this.EndBlock();
                this.ClearLocalsNamesMap(prevNamesMap);
                this.Emitter.Comma = true;
            }

            if (ctorHeader)
            {
                this.WriteNewLine();
                this.EndBlock();
            }
        }
Beispiel #5
0
        protected virtual IEnumerable <string> EmitInitMembers()
        {
            var injectors = this.GetInjectors();

            var constructorWrapperString = CS.Wrappers.CONSTRUCTORWRAPPER + ":";

            IEnumerable <string> ctorWrappers = injectors.Where(i => i.StartsWith(constructorWrapperString)).Select(i => i.Substring(constructorWrapperString.Length));

            injectors = injectors.Where(i => !i.StartsWith(constructorWrapperString));

            IEnumerable <string> fieldsInjectors = null;

            var fieldBlock = new FieldBlock(this.Emitter, this.TypeInfo, false, true);

            fieldBlock.Emit();

            fieldsInjectors = fieldBlock.Injectors;

            if (fieldBlock.WasEmitted)
            {
                this.Emitter.Comma = true;
            }

            if (!this.TypeInfo.InstanceConfig.HasConfigMembers && !injectors.Any() && !fieldsInjectors.Any())
            {
                return(ctorWrappers);
            }

            if (this.TypeInfo.InstanceConfig.HasConfigMembers)
            {
                var configBlock = new FieldBlock(this.Emitter, this.TypeInfo, false, false);
                configBlock.ClearTempVariables = false;
                configBlock.Emit();

                if (configBlock.Injectors.Count > 0)
                {
                    injectors = configBlock.Injectors.Concat(injectors);
                }

                if (configBlock.WasEmitted)
                {
                    this.Emitter.Comma = true;
                }
            }

            if (fieldsInjectors.Any())
            {
                injectors = fieldsInjectors.Concat(injectors);
            }

            if (injectors.Count() > 0)
            {
                this.EnsureComma();
                this.ctorHeader = true;
                this.Write(JS.Fields.CTORS);
                this.WriteColon();
                this.BeginBlock();

                this.Write(JS.Funcs.INIT);
                this.WriteColon();
                this.WriteFunction();
                this.WriteOpenParentheses();
                this.WriteCloseParentheses();
                this.WriteSpace();
                this.BeginBlock();

                if (this.Emitter.TempVariables != null)
                {
                    this.SimpleEmitTempVars();
                    this.Emitter.TempVariables = new Dictionary <string, bool>();
                }

                foreach (var fn in injectors)
                {
                    this.Write(WriteIndentToString(fn, this.Level - 1));
                    this.WriteNewLine();
                }

                this.EndBlock();

                this.Emitter.Comma = true;
            }

            return(ctorWrappers);
        }
Beispiel #6
0
        protected virtual void EmitCtorForStaticClass()
        {
            var ctor = this.TypeInfo.StaticCtor;

            if (ctor != null && ctor.Body.HasChildren)
            {
                this.ResetLocals();
                var prevNamesMap = this.BuildLocalsNamesMap();
                this.Write(JS.Funcs.CONSTRUCTOR);
                this.WriteColon();
                this.WriteFunction();
                this.WriteOpenCloseParentheses(true);

                this.BeginBlock();
                var beginPosition = this.Emitter.Output.Length;

                ctor.Body.AcceptChildren(this.Emitter);

                if (!this.Emitter.IsAsync)
                {
                    this.EmitTempVars(beginPosition, true);
                }

                this.EndBlock();
                this.ClearLocalsNamesMap(prevNamesMap);
                this.Emitter.Comma = true;
            }

            var injectors = this.GetInjectors();
            IEnumerable <string> fieldsInjectors = null;

            var fieldBlock = new FieldBlock(this.Emitter, this.TypeInfo, true, true);

            fieldBlock.Emit();

            fieldsInjectors = fieldBlock.Injectors;

            if (fieldBlock.WasEmitted)
            {
                this.Emitter.Comma = true;
            }

            if (this.TypeInfo.StaticConfig.HasConfigMembers || injectors.Any() || fieldsInjectors.Any())
            {
                this.EnsureComma();

                if (this.TypeInfo.StaticMethods.Any(m => m.Value.Any(subm => this.Emitter.GetEntityName(subm) == JS.Fields.CONFIG)) ||
                    this.TypeInfo.StaticConfig.Fields.Any(m => m.GetName(this.Emitter) == JS.Fields.CONFIG))
                {
                    this.Write(JS.Vars.D);
                }

                this.Write(JS.Fields.CONFIG);

                this.WriteColon();
                this.BeginBlock();

                if (this.TypeInfo.StaticConfig.HasConfigMembers)
                {
                    var configBlock = new FieldBlock(this.Emitter, this.TypeInfo, true, false);
                    configBlock.Emit();

                    if (configBlock.Injectors.Count > 0)
                    {
                        injectors = configBlock.Injectors.Concat(injectors);
                    }

                    if (configBlock.WasEmitted)
                    {
                        this.Emitter.Comma = true;
                    }
                }

                if (fieldsInjectors.Any())
                {
                    injectors = fieldsInjectors.Concat(injectors);
                }

                if (injectors.Count() > 0)
                {
                    this.EnsureComma();

                    this.Write(JS.Funcs.INIT);
                    this.WriteColon();
                    this.WriteFunction();
                    this.WriteOpenParentheses();
                    this.WriteCloseParentheses();
                    this.WriteSpace();
                    this.BeginBlock();

                    if (this.Emitter.TempVariables != null)
                    {
                        this.SimpleEmitTempVars();
                        this.Emitter.TempVariables = new Dictionary <string, bool>();
                    }

                    foreach (var fn in injectors)
                    {
                        this.Write(fn);
                        this.WriteNewLine();
                    }
                    this.EndBlock();
                    this.Emitter.Comma = true;
                }
                this.WriteNewLine();
                this.EndBlock();
            }
        }
Beispiel #7
0
        protected virtual IEnumerable <string> EmitInitMembers()
        {
            var injectors = this.GetInjectors();

            var constructorWrapperString = CS.Wrappers.CONSTRUCTORWRAPPER + ":";

            IEnumerable <string> ctorWrappers = injectors.Where(i => i.StartsWith(constructorWrapperString)).Select(i => i.Substring(constructorWrapperString.Length));

            injectors = injectors.Where(i => !i.StartsWith(constructorWrapperString));

            IEnumerable <string> fieldsInjectors = null;

            var fieldBlock = new FieldBlock(this.Emitter, this.TypeInfo, false, true);

            fieldBlock.Emit();

            fieldsInjectors = fieldBlock.Injectors;

            if (fieldBlock.WasEmitted)
            {
                this.Emitter.Comma = true;
            }

            if (!this.TypeInfo.InstanceConfig.HasConfigMembers && !injectors.Any() && !fieldsInjectors.Any())
            {
                return(ctorWrappers);
            }

            int  pos        = this.Emitter.Output.Length;
            bool oldComma   = this.Emitter.Comma;
            bool oldNewLine = this.Emitter.IsNewLine;

            if (this.TypeInfo.InstanceMethods.Any(m => m.Value.Any(subm => this.Emitter.GetEntityName(subm) == JS.Fields.CONFIG)) ||
                this.TypeInfo.InstanceConfig.Fields.Any(m => m.GetName(this.Emitter) == JS.Fields.CONFIG))
            {
                this.Write(JS.Vars.D);
            }

            this.EnsureComma();
            this.Write(JS.Fields.CONFIG);

            this.WriteColon();
            this.BeginBlock();

            if (this.TypeInfo.InstanceConfig.HasConfigMembers)
            {
                var configBlock = new FieldBlock(this.Emitter, this.TypeInfo, false, false);
                configBlock.Emit();

                if (configBlock.Injectors.Count > 0)
                {
                    injectors = configBlock.Injectors.Concat(injectors);
                }

                if (configBlock.WasEmitted)
                {
                    this.Emitter.Comma = true;
                }
            }

            if (fieldsInjectors.Any())
            {
                injectors = fieldsInjectors.Concat(injectors);
            }

            if (injectors.Count() > 0)
            {
                this.EnsureComma();
                this.Write(JS.Funcs.INIT);
                this.WriteColon();
                this.WriteFunction();
                this.WriteOpenParentheses();
                this.WriteCloseParentheses();
                this.WriteSpace();
                this.BeginBlock();

                if (this.Emitter.TempVariables != null)
                {
                    this.SimpleEmitTempVars();
                    this.Emitter.TempVariables = new Dictionary <string, bool>();
                }

                foreach (var fn in injectors)
                {
                    this.Write(fn);
                    this.WriteNewLine();
                }

                this.EndBlock();

                this.Emitter.Comma = true;
            }

            this.WriteNewLine();
            this.EndBlock();

            var str = this.Emitter.Output.ToString().Substring(pos);

            if (Regex.IsMatch(str, "^\\s*\\$?" + JS.Fields.CONFIG + "\\s*:\\s*\\{\\s*\\}\\s*$", RegexOptions.Multiline))
            {
                this.Emitter.Output.Length = pos;
                this.Emitter.Comma         = oldComma;
                this.Emitter.IsNewLine     = oldNewLine;
            }

            return(ctorWrappers);
        }
Beispiel #8
0
        protected virtual void EmitCtorForStaticClass()
        {
            var ctor = this.TypeInfo.StaticCtor;

            if (ctor != null && ctor.Body.HasChildren)
            {
                this.ResetLocals();
                var prevNamesMap = this.BuildLocalsNamesMap();
                this.Write("constructor");
                this.WriteColon();
                this.WriteFunction();
                this.WriteOpenCloseParentheses(true);

                this.BeginBlock();

                ctor.Body.AcceptChildren(this.Emitter);

                this.EndBlock();
                this.ClearLocalsNamesMap(prevNamesMap);
                this.Emitter.Comma = true;
            }

            var injectors = this.GetInjectors();
            IEnumerable <string> fieldsInjectors = null;

            var fieldBlock = new FieldBlock(this.Emitter, this.TypeInfo, true, true);

            fieldBlock.Emit();

            fieldsInjectors = fieldBlock.Injectors;

            if (fieldBlock.WasEmitted)
            {
                this.Emitter.Comma = true;
            }

            if (this.TypeInfo.StaticConfig.HasConfigMembers || injectors.Any() || fieldsInjectors.Any())
            {
                this.EnsureComma();

                if (this.TypeInfo.StaticMethods.Any(m => m.Value.Any(subm => this.Emitter.GetEntityName(subm) == "config")) ||
                    this.TypeInfo.StaticConfig.Fields.Any(m => m.GetName(this.Emitter) == "config"))
                {
                    this.Write("$");
                }

                this.Write("config");

                this.WriteColon();
                this.BeginBlock();

                if (this.TypeInfo.StaticConfig.HasConfigMembers)
                {
                    var configBlock = new FieldBlock(this.Emitter, this.TypeInfo, true, false);
                    configBlock.Emit();

                    if (configBlock.Injectors.Count > 0)
                    {
                        injectors = configBlock.Injectors.Concat(injectors);
                    }

                    if (configBlock.WasEmitted)
                    {
                        this.Emitter.Comma = true;
                    }
                }

                if (fieldsInjectors.Any())
                {
                    injectors = fieldsInjectors.Concat(injectors);
                }

                if (injectors.Count() > 0)
                {
                    this.EnsureComma();

                    this.Write("init");
                    this.WriteColon();
                    this.WriteFunction();
                    this.WriteOpenParentheses();
                    this.WriteCloseParentheses();
                    this.WriteSpace();
                    this.BeginBlock();
                    foreach (var fn in injectors)
                    {
                        this.Write(fn);
                        this.WriteNewLine();
                    }
                    this.EndBlock();
                    this.Emitter.Comma = true;
                }
                this.WriteNewLine();
                this.EndBlock();
            }
        }
Beispiel #9
0
        protected virtual void EmitInitMembers()
        {
            var injectors = this.GetInjectors();
            IEnumerable <string> fieldsInjectors = null;

            var fieldBlock = new FieldBlock(this.Emitter, this.TypeInfo, false, true);

            fieldBlock.Emit();

            fieldsInjectors = fieldBlock.Injectors;

            if (fieldBlock.WasEmitted)
            {
                this.Emitter.Comma = true;
            }

            if (!this.TypeInfo.InstanceConfig.HasConfigMembers && !injectors.Any() && !fieldsInjectors.Any())
            {
                return;
            }

            if (this.TypeInfo.InstanceMethods.Any(m => m.Value.Any(subm => this.Emitter.GetEntityName(subm) == "config")) ||
                this.TypeInfo.InstanceConfig.Fields.Any(m => m.GetName(this.Emitter) == "config"))
            {
                this.Write("$");
            }

            this.EnsureComma();
            this.Write("config");

            this.WriteColon();
            this.BeginBlock();

            var changeCase = this.Emitter.ChangeCase;
            var baseType   = this.Emitter.GetBaseTypeDefinition();

            if (this.TypeInfo.InstanceConfig.HasConfigMembers)
            {
                var configBlock = new FieldBlock(this.Emitter, this.TypeInfo, false, false);
                configBlock.Emit();

                if (configBlock.Injectors.Count > 0)
                {
                    injectors = configBlock.Injectors.Concat(injectors);
                }

                if (configBlock.WasEmitted)
                {
                    this.Emitter.Comma = true;
                }
            }

            if (fieldsInjectors.Any())
            {
                injectors = fieldsInjectors.Concat(injectors);
            }

            if (injectors.Count() > 0)
            {
                this.EnsureComma();
                this.Write("init");
                this.WriteColon();
                this.WriteFunction();
                this.WriteOpenParentheses();
                this.WriteCloseParentheses();
                this.WriteSpace();
                this.BeginBlock();

                foreach (var fn in injectors)
                {
                    this.Write(fn);
                    this.WriteNewLine();
                }

                this.EndBlock();

                this.Emitter.Comma = true;
            }

            this.WriteNewLine();
            this.EndBlock();
        }