Example #1
0
        private static void GeneratePropertyGetter(ScriptGenerator generator, string typeName, PropertySymbol propertySymbol, ScriptTextWriter writer, bool instanceMember)
        {
            if (instanceMember)
            {
                writer.Write("$get_");
                writer.Write(propertySymbol.GeneratedName);
                writer.Write(": ");
            }
            else
            {
                writer.Write("ss.createPropertyGet(");
                writer.Write(typeName);
                writer.Write(", '");
                writer.Write(propertySymbol.GeneratedName);
                writer.Write("', ");
            }

            writer.WriteLine("function() {");
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, propertySymbol);
            }

            CodeGenerator.GenerateScript(generator, propertySymbol, /* getter */ true);
            writer.Indent--;
            writer.Write("}");

            if (instanceMember == false)
            {
                writer.WriteLine(");");
            }
        }
Example #2
0
        private static void GenerateInterface(ScriptGenerator generator, InterfaceSymbol interfaceSymbol)
        {
            ScriptTextWriter writer        = generator.Writer;
            string           interfaceName = interfaceSymbol.FullGeneratedName;

            if (interfaceSymbol.Namespace.Length == 0)
            {
                writer.Write("window.");
            }

            writer.Write(interfaceName);
            writer.WriteTrimmed(" = ");
            writer.Write("function()");
            writer.WriteTrimmed(" { ");

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, interfaceSymbol);
            }

            writer.Write("};");
            writer.WriteNewLine();

            if (generator.Options.DebugFlavor)
            {
                writer.Write(interfaceName);
                writer.Write(".prototype = {");
                writer.WriteLine();
                writer.Indent++;
                bool firstMember = true;
                foreach (MemberSymbol member in interfaceSymbol.Members)
                {
                    if (firstMember == false)
                    {
                        writer.Write(",");
                        writer.WriteLine();
                    }

                    if (member.Type == SymbolType.Property)
                    {
                        writer.Write("get_");
                        writer.Write(member.GeneratedName);
                        writer.WriteTrimmed(" : ");
                        writer.Write("null");

                        if (((PropertySymbol)member).IsReadOnly == false)
                        {
                            writer.Write(",");
                            writer.WriteLine();

                            writer.Write("set_");
                            writer.Write(member.GeneratedName);
                            writer.WriteTrimmed(" : ");
                            writer.Write("null");
                        }
                    }
                    else if (member.Type == SymbolType.Event)
                    {
                        writer.Write("add_");
                        writer.Write(member.GeneratedName);
                        writer.WriteTrimmed(" : ");
                        writer.Write("null,");
                        writer.WriteLine();

                        writer.Write("remove_");
                        writer.Write(member.GeneratedName);
                        writer.WriteTrimmed(" : ");
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write(member.GeneratedName);
                        writer.WriteTrimmed(" : ");
                        writer.Write("null");
                    }
                    firstMember = false;
                }
                writer.Indent--;
                writer.WriteLine();
                writer.Write("}");
                writer.WriteSignificantNewLine();
            }

            writer.Write(interfaceName);
            writer.Write(".registerInterface('");
            writer.Write(interfaceName);
            writer.Write("');");
            writer.WriteNewLine();
        }
Example #3
0
        private static void GenerateEnumeration(ScriptGenerator generator, EnumerationSymbol enumSymbol)
        {
            ScriptTextWriter writer = generator.Writer;

            if (enumSymbol.Namespace.Length == 0)
            {
                writer.Write("window.");
            }

            writer.Write(enumSymbol.FullGeneratedName);
            writer.WriteTrimmed(" = ");
            writer.Write("function()");
            writer.WriteTrimmed(" { ");

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, enumSymbol);
            }

            writer.Write("};");
            writer.WriteNewLine();
            writer.Write(enumSymbol.FullGeneratedName);
            writer.Write(".prototype = {");
            writer.Indent++;

            bool firstValue = true;

            foreach (MemberSymbol memberSymbol in enumSymbol.Members)
            {
                EnumerationFieldSymbol fieldSymbol = memberSymbol as EnumerationFieldSymbol;
                if (fieldSymbol == null)
                {
                    continue;
                }

                if (firstValue == false)
                {
                    writer.WriteTrimmed(", ");
                }

                writer.WriteNewLine();
                writer.Write(fieldSymbol.GeneratedName);
                writer.WriteTrimmed(": ");
                if (enumSymbol.UseNamedValues)
                {
                    writer.Write(Utility.QuoteString(enumSymbol.CreateNamedValue(fieldSymbol)));
                }
                else
                {
                    writer.Write(fieldSymbol.Value);
                }
                firstValue = false;
            }

            writer.Indent--;
            writer.WriteNewLine();
            writer.WriteTrimmed("}");
            writer.WriteSignificantNewLine();

            writer.Write(enumSymbol.FullGeneratedName);
            writer.Write(".registerEnum('");
            writer.Write(enumSymbol.FullGeneratedName);
            writer.WriteTrimmed("', ");
            writer.Write(enumSymbol.Flags ? "true" : "false");
            writer.Write(");");
            writer.WriteNewLine();
        }
Example #4
0
        private static void GenerateClass(ScriptGenerator generator, ClassSymbol classSymbol)
        {
            if (classSymbol.HasGlobalMethods)
            {
                GenerateGlobalMethods(generator, classSymbol);
                generator.AddGeneratedClass(classSymbol);
                return;
            }

            ScriptTextWriter writer = generator.Writer;
            string           name   = classSymbol.FullGeneratedName;

            if (classSymbol.Namespace.Length == 0)
            {
                writer.Write("window.");
            }

            writer.Write(name);
            writer.WriteTrimmed(" = ");
            writer.Write("function");
            if (generator.Options.DebugFlavor)
            {
                writer.Write(" ");
                writer.Write(name.Replace(".", "_"));
            }
            writer.Write("(");
            if ((classSymbol.Constructor != null) && (classSymbol.Constructor.Parameters != null))
            {
                bool firstParameter = true;
                foreach (ParameterSymbol parameterSymbol in classSymbol.Constructor.Parameters)
                {
                    if (firstParameter == false)
                    {
                        writer.WriteTrimmed(", ");
                    }
                    writer.Write(parameterSymbol.GeneratedName);
                    firstParameter = false;
                }
            }
            writer.WriteTrimmed(") ");
            writer.Write("{");
            writer.WriteNewLine();
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, classSymbol);
            }

            foreach (MemberSymbol memberSymbol in classSymbol.Members)
            {
                if ((memberSymbol.Type == SymbolType.Field) &&
                    (memberSymbol.Visibility & MemberVisibility.Static) == 0)
                {
                    FieldSymbol fieldSymbol = (FieldSymbol)memberSymbol;
                    if (fieldSymbol.HasInitializer)
                    {
                        writer.Write("this.");
                        writer.Write(fieldSymbol.GeneratedName);
                        writer.WriteTrimmed(" = ");
                        CodeGenerator.GenerateScript(generator, fieldSymbol);
                        writer.Write(";");
                        writer.WriteNewLine();
                    }
                }
            }
            if (classSymbol.Constructor != null)
            {
                CodeGenerator.GenerateScript(generator, classSymbol.Constructor);
            }
            else if (classSymbol.BaseClass != null)
            {
                writer.Write(classSymbol.FullGeneratedName);
                writer.Write(".initializeBase(this);");
                writer.WriteNewLine();
            }
            writer.Indent--;
            writer.Write("}");
            writer.WriteSignificantNewLine();

            foreach (MemberSymbol memberSymbol in classSymbol.Members)
            {
                if ((memberSymbol.Type != SymbolType.Field) &&
                    (memberSymbol.Visibility & MemberVisibility.Static) != 0)
                {
                    MemberGenerator.GenerateScript(generator, memberSymbol);
                }
            }

            bool hasPrototypeMembers = false;
            bool firstMember         = true;
            bool lastMemberWasField  = true;

            foreach (MemberSymbol memberSymbol in classSymbol.Members)
            {
                if ((memberSymbol.Visibility & MemberVisibility.Static) == 0)
                {
                    if ((memberSymbol.Type == SymbolType.Field) &&
                        ((FieldSymbol)memberSymbol).HasInitializer)
                    {
                        continue;
                    }

                    if ((memberSymbol is CodeMemberSymbol) &&
                        ((CodeMemberSymbol)memberSymbol).IsAbstract)
                    {
                        continue;
                    }

                    if (hasPrototypeMembers == false)
                    {
                        hasPrototypeMembers = true;

                        writer.Write(name);
                        writer.Write(".prototype");
                        writer.WriteTrimmed(" = ");
                        writer.Write("{");
                        writer.WriteNewLine();
                        writer.Indent++;
                    }

                    if (firstMember == false)
                    {
                        writer.Write(",");
                        writer.WriteNewLine();
                    }
                    if ((lastMemberWasField == false) || !(memberSymbol is FieldSymbol))
                    {
                        writer.WriteNewLine();
                    }

                    MemberGenerator.GenerateScript(generator, memberSymbol);
                    lastMemberWasField = (memberSymbol is FieldSymbol);
                    firstMember        = false;
                }
            }

            if (classSymbol.Indexer != null)
            {
                if (hasPrototypeMembers == false)
                {
                    hasPrototypeMembers = true;

                    writer.Write(name);
                    writer.Write(".prototype");
                    writer.WriteTrimmed(" = ");
                    writer.Write("{");
                    writer.WriteNewLine();
                    writer.Indent++;
                }
                if (firstMember == false)
                {
                    writer.Write(",");
                    writer.WriteNewLine();
                }

                MemberGenerator.GenerateScript(generator, classSymbol.Indexer);
            }

            if (hasPrototypeMembers)
            {
                writer.Indent--;
                writer.WriteNewLine();
                writer.Write("}");
                writer.WriteSignificantNewLine();
            }

            generator.AddGeneratedClass(classSymbol);
        }
Example #5
0
        private static void GenerateProperty(ScriptGenerator generator, string typeName, PropertySymbol propertySymbol)
        {
            if (propertySymbol.IsAbstract)
            {
                return;
            }

            ScriptTextWriter writer = generator.Writer;

            bool instanceMember = true;

            if ((propertySymbol.Visibility & MemberVisibility.Static) != 0)
            {
                instanceMember = false;
                writer.Write(typeName);
                writer.Write(".");
            }

            writer.Write("get_");
            writer.Write(propertySymbol.GeneratedName);
            if (instanceMember)
            {
                writer.WriteTrimmed(": ");
            }
            else
            {
                writer.WriteTrimmed(" = ");
            }
            writer.Write("function");
            if (generator.Options.DebugFlavor)
            {
                writer.Write(" ");
                writer.Write(typeName.Replace(".", "_"));
                writer.Write("$get_");
                writer.Write(propertySymbol.GeneratedName);
            }
            writer.Write("()");
            writer.WriteTrimmed(" {");
            writer.WriteNewLine();
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, propertySymbol);
            }

            CodeGenerator.GenerateScript(generator, propertySymbol, /* getter */ true);
            writer.Indent--;
            writer.Write("}");

            if (instanceMember == false)
            {
                writer.WriteSignificantNewLine();
            }

            if (propertySymbol.IsReadOnly == false)
            {
                ParameterSymbol valueParameter = propertySymbol.Parameters[0];
                if (generator.Options.Minimize)
                {
                    bool obfuscateParams = !propertySymbol.IsPublic;
                    if (obfuscateParams)
                    {
                        valueParameter.SetTransformedName("$p0");
                    }
                }

                if (instanceMember)
                {
                    writer.Write(",");
                    writer.WriteNewLine();
                }
                else
                {
                    writer.Write(typeName);
                    writer.Write(".");
                }

                writer.Write("set_");
                writer.Write(propertySymbol.GeneratedName);
                if (instanceMember)
                {
                    writer.WriteTrimmed(": ");
                }
                else
                {
                    writer.WriteTrimmed(" = ");
                }
                writer.Write("function");
                if (generator.Options.DebugFlavor)
                {
                    writer.Write(" ");
                    writer.Write(typeName.Replace(".", "_"));
                    writer.Write("$set_");
                    writer.Write(propertySymbol.GeneratedName);
                }
                writer.Write("(");
                writer.Write(valueParameter.GeneratedName);
                writer.Write(")");
                writer.WriteTrimmed(" {");
                writer.WriteNewLine();
                writer.Indent++;

                if (generator.Options.EnableDocComments)
                {
                    DocCommentGenerator.GenerateComment(generator, propertySymbol);
                }

                CodeGenerator.GenerateScript(generator, propertySymbol, /* getter */ false);
                writer.Write("return ");
                writer.Write(valueParameter.GeneratedName);
                writer.Write(";");
                writer.WriteNewLine();
                writer.Indent--;
                writer.Write("}");

                if (instanceMember == false)
                {
                    writer.WriteSignificantNewLine();
                }
            }
        }
Example #6
0
        private static void GenerateMethod(ScriptGenerator generator, string typeName, MethodSymbol methodSymbol)
        {
            if (methodSymbol.IsAbstract)
            {
                return;
            }

            ScriptTextWriter writer = generator.Writer;

            bool instanceMember = ((methodSymbol.Visibility & MemberVisibility.Static) == 0);

            if (instanceMember == false)
            {
                if (methodSymbol.IsGlobalMethod)
                {
                    string mixinRoot = null;
                    if (methodSymbol.Parent.Type == SymbolType.Class)
                    {
                        mixinRoot = ((ClassSymbol)methodSymbol.Parent).MixinRoot;
                    }
                    if (String.IsNullOrEmpty(mixinRoot))
                    {
                        mixinRoot = "window";
                    }

                    writer.Write(mixinRoot);
                }
                else
                {
                    writer.Write(typeName);
                }

                writer.Write(".");
            }

            writer.Write(methodSymbol.GeneratedName);
            if (instanceMember)
            {
                writer.WriteTrimmed(": ");
            }
            else
            {
                writer.WriteTrimmed(" = ");
            }

            writer.Write("function");
            if (generator.Options.DebugFlavor)
            {
                writer.Write(" ");
                writer.Write(typeName.Replace(".", "_"));
                writer.Write("$");
                writer.Write(methodSymbol.GeneratedName);
            }
            writer.Write("(");
            if (methodSymbol.Parameters != null)
            {
                bool obfuscateParams = false;
                if (generator.Options.Minimize)
                {
                    obfuscateParams = !methodSymbol.IsPublic;
                }

                int paramIndex = 0;
                foreach (ParameterSymbol parameterSymbol in methodSymbol.Parameters)
                {
                    if (paramIndex > 0)
                    {
                        writer.WriteTrimmed(", ");
                    }
                    if (obfuscateParams)
                    {
                        parameterSymbol.SetTransformedName("$p" + paramIndex);
                    }
                    writer.Write(parameterSymbol.GeneratedName);

                    paramIndex++;
                }
            }
            writer.WriteTrimmed(") ");
            writer.Write("{");
            writer.WriteNewLine();
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, methodSymbol);
            }

            CodeGenerator.GenerateScript(generator, methodSymbol);
            writer.Indent--;
            writer.Write("}");

            if (instanceMember == false)
            {
                writer.WriteSignificantNewLine();
            }
        }
Example #7
0
        private static void GenerateIndexer(ScriptGenerator generator, string typeName, PropertySymbol indexerSymbol)
        {
            if (indexerSymbol.IsAbstract)
            {
                return;
            }

            ScriptTextWriter writer = generator.Writer;

            bool instanceMember = true;

            if ((indexerSymbol.Visibility & MemberVisibility.Static) != 0)
            {
                instanceMember = false;
                writer.Write(typeName);
                writer.Write(".");
            }

            bool obfuscateParams = !indexerSymbol.IsPublic;

            if (obfuscateParams && generator.Options.Minimize)
            {
                for (int i = 0; i < indexerSymbol.Parameters.Count; i++)
                {
                    ParameterSymbol parameterSymbol = indexerSymbol.Parameters[i];
                    parameterSymbol.SetTransformedName("$p" + i);
                }
            }

            writer.Write("get_");
            writer.Write(indexerSymbol.GeneratedName);
            if (instanceMember)
            {
                writer.WriteTrimmed(": ");
            }
            else
            {
                writer.WriteTrimmed(" = ");
            }

            writer.Write("function");
            if (generator.Options.DebugFlavor)
            {
                writer.Write(" ");
                writer.Write(typeName.Replace(".", "_"));
                writer.Write("$get_");
                writer.Write(indexerSymbol.GeneratedName);
            }

            writer.Write("(");

            for (int i = 0; i < indexerSymbol.Parameters.Count - 1; i++)
            {
                ParameterSymbol parameterSymbol = indexerSymbol.Parameters[i];
                if (i > 0)
                {
                    writer.WriteTrimmed(", ");
                }
                writer.Write(parameterSymbol.GeneratedName);
            }

            writer.Write(")");
            writer.WriteTrimmed(" {");
            writer.WriteNewLine();
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, indexerSymbol);
            }

            CodeGenerator.GenerateScript(generator, (IndexerSymbol)indexerSymbol, /* getter */ true);
            writer.Indent--;
            writer.Write("}");

            if (instanceMember == false)
            {
                writer.WriteSignificantNewLine();
            }

            if (indexerSymbol.IsReadOnly == false)
            {
                if (instanceMember)
                {
                    writer.Write(",");
                    writer.WriteNewLine();
                }
                else
                {
                    writer.Write(typeName);
                    writer.Write(".");
                }

                writer.Write("set_");
                writer.Write(indexerSymbol.GeneratedName);
                if (instanceMember)
                {
                    writer.WriteTrimmed(": ");
                }
                else
                {
                    writer.WriteTrimmed(" = ");
                }
                writer.Write("function");
                if (generator.Options.DebugFlavor)
                {
                    writer.Write(" ");
                    writer.Write(typeName.Replace(".", "_"));
                    writer.Write("$set_");
                    writer.Write(indexerSymbol.GeneratedName);
                }
                writer.Write("(");
                for (int i = 0; i < indexerSymbol.Parameters.Count; i++)
                {
                    ParameterSymbol parameterSymbol = indexerSymbol.Parameters[i];
                    if (i > 0)
                    {
                        writer.WriteTrimmed(", ");
                    }
                    writer.Write(parameterSymbol.GeneratedName);
                }
                writer.Write(")");
                writer.WriteTrimmed(" {");
                writer.WriteNewLine();
                writer.Indent++;

                if (generator.Options.EnableDocComments)
                {
                    DocCommentGenerator.GenerateComment(generator, indexerSymbol);
                }

                CodeGenerator.GenerateScript(generator, (IndexerSymbol)indexerSymbol, /* getter */ false);
                writer.Write("return ");
                writer.Write(indexerSymbol.Parameters[indexerSymbol.Parameters.Count - 1].GeneratedName);
                writer.Write(";");
                writer.WriteNewLine();
                writer.Indent--;
                writer.Write("}");

                if (instanceMember == false)
                {
                    writer.WriteSignificantNewLine();
                }
            }
        }
Example #8
0
        private static void GenerateEvent(ScriptGenerator generator, string typeName, EventSymbol eventSymbol)
        {
            ScriptTextWriter writer = generator.Writer;

            ParameterSymbol valueParameter = eventSymbol.Parameters[0];

            if (generator.Options.Minimize)
            {
                bool obfuscateParams = !eventSymbol.IsPublic;
                if (obfuscateParams)
                {
                    valueParameter.SetTransformedName("$p0");
                }
            }

            string eventName = eventSymbol.GeneratedName;
            string fieldName = eventName;

            if (eventSymbol.DefaultImplementation)
            {
                fieldName = "__" + Utility.CreateCamelCaseName(eventSymbol.Name);

                Debug.Assert(eventSymbol.Parent.Type == SymbolType.Class);

                Symbol fieldSymbol = ((ClassSymbol)eventSymbol.Parent).GetMember(fieldName);
                Debug.Assert((fieldSymbol != null) && (fieldSymbol.Type == SymbolType.Field));

                fieldName = fieldSymbol.GeneratedName;
            }

            string fieldReference;

            if ((eventSymbol.Visibility & MemberVisibility.Static) == 0)
            {
                fieldReference = "this.";
            }
            else
            {
                fieldReference = typeName + ".";
            }
            fieldReference += fieldName;

            bool instanceMember = true;

            if ((eventSymbol.Visibility & MemberVisibility.Static) != 0)
            {
                instanceMember = false;
                writer.Write(typeName);
                writer.Write(".");
            }

            writer.Write("add_");
            writer.Write(eventName);
            if (instanceMember)
            {
                writer.WriteTrimmed(": ");
            }
            else
            {
                writer.WriteTrimmed(" = ");
            }
            writer.Write("function");
            if (generator.Options.DebugFlavor)
            {
                writer.Write(" ");
                writer.Write(typeName.Replace(".", "_"));
                writer.Write("$add_");
                writer.Write(eventName);
            }
            writer.Write("(");
            writer.Write(valueParameter.GeneratedName);
            writer.Write(")");
            writer.WriteTrimmed(" {");
            writer.WriteNewLine();
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, eventSymbol);
            }

            if (eventSymbol.DefaultImplementation)
            {
                writer.Write(fieldReference);
                writer.WriteTrimmed(" = ");
                writer.Write("ss.Delegate.combine(");
                writer.Write(fieldReference);
                writer.WriteTrimmed(", ");
                writer.Write(valueParameter.GeneratedName);
                writer.Write(");");
                writer.WriteNewLine();
            }
            else
            {
                CodeGenerator.GenerateScript(generator, eventSymbol, /* add */ true);
            }
            writer.Indent--;
            writer.Write("}");

            if (instanceMember == false)
            {
                writer.WriteSignificantNewLine();
            }

            if (instanceMember)
            {
                writer.Write(",");
                writer.WriteNewLine();
            }
            else
            {
                writer.Write(typeName);
                writer.Write(".");
            }
            writer.Write("remove_");
            writer.Write(eventName);
            if (instanceMember)
            {
                writer.WriteTrimmed(": ");
            }
            else
            {
                writer.WriteTrimmed(" = ");
            }
            writer.Write("function");
            if (generator.Options.DebugFlavor)
            {
                writer.Write(" ");
                writer.Write(typeName.Replace(".", "_"));
                writer.Write("$remove_");
                writer.Write(eventName);
            }
            writer.Write("(");
            writer.Write(valueParameter.GeneratedName);
            writer.Write(")");
            writer.WriteTrimmed(" {");
            writer.WriteNewLine();
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, eventSymbol);
            }

            if (eventSymbol.DefaultImplementation)
            {
                writer.Write(fieldReference);
                writer.WriteTrimmed(" = ");
                writer.Write("ss.Delegate.remove(");
                writer.Write(fieldReference);
                writer.WriteTrimmed(", ");
                writer.Write(valueParameter.GeneratedName);
                writer.Write(");");
                writer.WriteNewLine();
            }
            else
            {
                CodeGenerator.GenerateScript(generator, eventSymbol, /* add */ false);
            }
            writer.Indent--;
            writer.Write("}");

            if (instanceMember == false)
            {
                writer.WriteSignificantNewLine();
            }
        }
Example #9
0
        private static void GenerateMethod(ScriptGenerator generator, string typeName, MethodSymbol methodSymbol)
        {
            if (methodSymbol.IsAbstract)
            {
                return;
            }

            ScriptTextWriter writer = generator.Writer;

            bool instanceMember = ((methodSymbol.Visibility & MemberVisibility.Static) == 0);

            if (instanceMember == false)
            {
                if (methodSymbol.IsExtension)
                {
                    string extendee = null;
                    if (methodSymbol.Parent.Type == SymbolType.Class)
                    {
                        extendee = ((ClassSymbol)methodSymbol.Parent).Extendee;
                    }
                    if (String.IsNullOrEmpty(extendee))
                    {
                        extendee = "this";
                    }

                    writer.Write(extendee);
                }
                else
                {
                    writer.Write(typeName);
                }

                writer.Write(".");
            }

            writer.Write(methodSymbol.GeneratedName);
            if (instanceMember)
            {
                writer.Write(": ");
            }
            else
            {
                writer.Write(" = ");
            }

            bool hasParams = HasParamsModifier(methodSymbol);

            if (hasParams)
            {
                writer.Write("ss.paramsGenerator(");
                writer.Write("{0}, ", methodSymbol.Parameters.Count - 1);
            }

            writer.Write("function(");
            WriteParameters(methodSymbol, writer);

            writer.WriteLine(") {");
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, methodSymbol);
            }

            CodeGenerator.GenerateScript(generator, methodSymbol);
            writer.Indent--;
            writer.Write("}");

            if (hasParams)
            {
                writer.Write(")");
            }

            if (instanceMember == false)
            {
                writer.WriteLine(";");
            }
        }
Example #10
0
        private static void GenerateIndexer(ScriptGenerator generator, string typeName, PropertySymbol indexerSymbol)
        {
            if (indexerSymbol.IsAbstract)
            {
                return;
            }

            Debug.Assert((indexerSymbol.Visibility & MemberVisibility.Static) == 0);

            ScriptTextWriter writer = generator.Writer;

            writer.Write("get_");
            writer.Write(indexerSymbol.GeneratedName);
            writer.Write(": function(");

            for (int i = 0; i < indexerSymbol.Parameters.Count - 1; i++)
            {
                ParameterSymbol parameterSymbol = indexerSymbol.Parameters[i];
                if (i > 0)
                {
                    writer.Write(", ");
                }
                writer.Write(parameterSymbol.GeneratedName);
            }

            writer.WriteLine(") {");
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, indexerSymbol);
            }

            CodeGenerator.GenerateScript(generator, (IndexerSymbol)indexerSymbol, /* getter */ true);
            writer.Indent--;
            writer.Write("}");

            if (indexerSymbol.IsReadOnly == false)
            {
                writer.WriteLine(",");

                writer.Write("set_");
                writer.Write(indexerSymbol.GeneratedName);
                writer.Write(": function(");
                for (int i = 0; i < indexerSymbol.Parameters.Count; i++)
                {
                    ParameterSymbol parameterSymbol = indexerSymbol.Parameters[i];
                    if (i > 0)
                    {
                        writer.Write(", ");
                    }
                    writer.Write(parameterSymbol.GeneratedName);
                }
                writer.WriteLine(") {");
                writer.Indent++;

                if (generator.Options.EnableDocComments)
                {
                    DocCommentGenerator.GenerateComment(generator, indexerSymbol);
                }

                CodeGenerator.GenerateScript(generator, (IndexerSymbol)indexerSymbol, /* getter */ false);
                writer.Write("return ");
                writer.Write(indexerSymbol.Parameters[indexerSymbol.Parameters.Count - 1].GeneratedName);
                writer.WriteLine(";");
                writer.Indent--;
                writer.Write("}");
            }
        }
Example #11
0
        private static void GenerateEvent(ScriptGenerator generator, string typeName, EventSymbol eventSymbol)
        {
            ScriptTextWriter writer = generator.Writer;

            ParameterSymbol valueParameter = eventSymbol.Parameters[0];

            string eventName = eventSymbol.GeneratedName;
            string fieldName = eventName;

            if (eventSymbol.DefaultImplementation)
            {
                fieldName = "__" + Utility.CreateCamelCaseName(eventSymbol.Name);

                Debug.Assert(eventSymbol.Parent.Type == SymbolType.Class);

                Symbol fieldSymbol = ((ClassSymbol)eventSymbol.Parent).GetMember(fieldName);
                Debug.Assert((fieldSymbol != null) && (fieldSymbol.Type == SymbolType.Field));

                fieldName = fieldSymbol.GeneratedName;
            }

            string fieldReference;

            if ((eventSymbol.Visibility & MemberVisibility.Static) == 0)
            {
                fieldReference = "this.";
            }
            else
            {
                fieldReference = typeName + ".";
            }
            fieldReference += fieldName;

            bool instanceMember = true;

            if ((eventSymbol.Visibility & MemberVisibility.Static) != 0)
            {
                instanceMember = false;
                writer.Write(typeName);
                writer.Write(".");
            }

            writer.Write("add_");
            writer.Write(eventName);
            if (instanceMember)
            {
                writer.Write(": ");
            }
            else
            {
                writer.Write(" = ");
            }

            writer.Write("function(");
            writer.Write(valueParameter.GeneratedName);
            writer.WriteLine(") {");
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, eventSymbol);
            }

            if (eventSymbol.DefaultImplementation)
            {
                writer.Write(fieldReference);
                writer.Write(" = ss.bindAdd(");
                writer.Write(fieldReference);
                writer.Write(", ");
                writer.Write(valueParameter.GeneratedName);
                writer.WriteLine(");");
            }
            else
            {
                CodeGenerator.GenerateScript(generator, eventSymbol, /* add */ true);
            }
            writer.Indent--;
            writer.Write("}");

            if (instanceMember == false)
            {
                writer.WriteLine(";");
            }

            if (instanceMember)
            {
                writer.WriteLine(",");
            }
            else
            {
                writer.Write(typeName);
                writer.Write(".");
            }
            writer.Write("remove_");
            writer.Write(eventName);
            if (instanceMember)
            {
                writer.Write(": ");
            }
            else
            {
                writer.Write(" = ");
            }
            writer.Write("function(");
            writer.Write(valueParameter.GeneratedName);
            writer.WriteLine(") {");
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, eventSymbol);
            }

            if (eventSymbol.DefaultImplementation)
            {
                writer.Write(fieldReference);
                writer.Write(" = ss.bindSub(");
                writer.Write(fieldReference);
                writer.Write(", ");
                writer.Write(valueParameter.GeneratedName);
                writer.WriteLine(");");
            }
            else
            {
                CodeGenerator.GenerateScript(generator, eventSymbol, /* add */ false);
            }
            writer.Indent--;
            writer.Write("}");

            if (instanceMember == false)
            {
                writer.WriteLine(";");
            }
        }
Example #12
0
        private static void GenerateClass(ScriptGenerator generator, ClassSymbol classSymbol)
        {
            ScriptTextWriter writer = generator.Writer;
            string           name   = classSymbol.FullGeneratedName;

            writer.Write("function ");
            writer.Write(name);
            writer.Write("(");
            if ((classSymbol.Constructor != null) && (classSymbol.Constructor.Parameters != null))
            {
                bool firstParameter = true;
                foreach (ParameterSymbol parameterSymbol in classSymbol.Constructor.Parameters)
                {
                    if (firstParameter == false)
                    {
                        writer.Write(", ");
                    }
                    writer.Write(parameterSymbol.GeneratedName);
                    firstParameter = false;
                }
            }
            writer.WriteLine(") {");
            writer.Indent++;

            if (generator.Options.EnableDocComments)
            {
                DocCommentGenerator.GenerateComment(generator, classSymbol);
            }

            foreach (MemberSymbol memberSymbol in classSymbol.Members)
            {
                if ((memberSymbol.Type == SymbolType.Field) &&
                    (memberSymbol.Visibility & MemberVisibility.Static) == 0)
                {
                    FieldSymbol fieldSymbol = (FieldSymbol)memberSymbol;
                    if (fieldSymbol.HasInitializer)
                    {
                        writer.Write("this.");
                        writer.Write(fieldSymbol.GeneratedName);
                        writer.Write(" = ");
                        CodeGenerator.GenerateScript(generator, fieldSymbol);
                        writer.Write(";");
                        writer.WriteLine();
                    }
                }
            }
            if (classSymbol.Constructor != null)
            {
                CodeGenerator.GenerateScript(generator, classSymbol.Constructor);
            }
            else if ((classSymbol.BaseClass != null) && (classSymbol.IsTestClass == false))
            {
                writer.Write(classSymbol.BaseClass.FullGeneratedName);
                writer.Write(".call(this);");
                writer.WriteLine();
            }
            writer.Indent--;
            writer.WriteLine("}");

            foreach (MemberSymbol memberSymbol in classSymbol.Members)
            {
                if ((memberSymbol.Type != SymbolType.Field) &&
                    (memberSymbol.Visibility & MemberVisibility.Static) != 0)
                {
                    MemberGenerator.GenerateScript(generator, memberSymbol);
                }
            }

            if (classSymbol.IsStaticClass == false)
            {
                writer.Write("var ");
                writer.Write(name);
                writer.WriteLine("$ = {");
                writer.Indent++;

                bool firstMember = true;
                foreach (MemberSymbol memberSymbol in classSymbol.Members)
                {
                    if ((memberSymbol.Visibility & MemberVisibility.Static) == 0)
                    {
                        if (memberSymbol.Type == SymbolType.Field)
                        {
                            continue;
                        }

                        if ((memberSymbol is CodeMemberSymbol) &&
                            ((CodeMemberSymbol)memberSymbol).IsAbstract)
                        {
                            continue;
                        }

                        if (firstMember == false)
                        {
                            writer.WriteLine(",");
                        }

                        MemberGenerator.GenerateScript(generator, memberSymbol);
                        firstMember = false;
                    }
                }

                if (classSymbol.Indexer != null)
                {
                    if (firstMember == false)
                    {
                        writer.WriteLine(",");
                    }

                    MemberGenerator.GenerateScript(generator, classSymbol.Indexer);
                }

                writer.Indent--;
                writer.WriteLine();
                writer.Write("};");
                writer.WriteLine();
            }
        }