Exemplo n.º 1
0
        private static void GenerateInterfaceRegistrationScript(ScriptGenerator generator, InterfaceSymbol interfaceSymbol)
        {
            ScriptTextWriter writer = generator.Writer;

            writer.Write("ss.defineInterface(");
            writer.Write(interfaceSymbol.FullGeneratedName);

            if (interfaceSymbol.Interfaces != null)
            {
                writer.Write(", [");
                bool first = true;

                foreach (InterfaceSymbol inheritedInterface in interfaceSymbol.Interfaces)
                {
                    if (!first)
                    {
                        writer.Write(", ");
                    }
                    writer.Write(inheritedInterface.FullGeneratedName);
                    first = false;
                }

                writer.Write("]");
            }
            writer.Write(")");
        }
Exemplo n.º 2
0
        private static void GenerateResources(ScriptGenerator generator, ResourcesSymbol resourcesSymbol)
        {
            ScriptTextWriter writer        = generator.Writer;
            string           resourcesName = resourcesSymbol.FullGeneratedName;

            writer.Write("var ");
            writer.Write(resourcesName);
            writer.WriteLine(" = {");
            writer.Indent++;

            bool firstValue = true;

            foreach (FieldSymbol member in resourcesSymbol.Members)
            {
                Debug.Assert(member.Value is string);

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

                writer.Write(member.GeneratedName);
                writer.Write(": ");
                writer.Write(Utility.QuoteString((string)member.Value));

                firstValue = false;
            }

            writer.Indent--;
            writer.WriteLine();
            writer.Write("};");
            writer.WriteLine();
        }
Exemplo n.º 3
0
        private static void GenerateLiteralExpression(ScriptGenerator generator, MemberSymbol symbol, LiteralExpression expression)
        {
            ScriptTextWriter writer = generator.Writer;

            object value     = expression.Value;
            string textValue = null;

            if (value == null)
            {
                textValue = "null";
            }
            else
            {
                if (value is bool)
                {
                    if ((bool)value)
                    {
                        textValue = "true";
                    }
                    else
                    {
                        textValue = "false";
                    }
                }
                else if ((value is char) || (value is string))
                {
                    textValue = Utility.QuoteString(value.ToString());
                }
                else if (value is TypeSymbol)
                {
                    textValue = ((TypeSymbol)value).FullGeneratedName;
                }
                else if (value is Expression[])
                {
                    Expression[] values = (Expression[])value;
                    if (values.Length == 0)
                    {
                        textValue = "[]";
                    }
                    else
                    {
                        writer.WriteTrimmed("[ ");
                        GenerateExpressionList(generator, symbol, (Expression[])value);
                        writer.WriteTrimmed(" ]");
                    }
                }
                else
                {
                    textValue = Convert.ToString(value, CultureInfo.InvariantCulture);
                }
            }

            if (textValue != null)
            {
                writer.Write(textValue);
            }
        }
Exemplo n.º 4
0
        private static void WriteParameters(MethodSymbol methodSymbol, ScriptTextWriter writer)
        {
            if (methodSymbol.Parameters != null)
            {
                int paramIndex = 0;

                foreach (ParameterSymbol parameterSymbol in methodSymbol.Parameters)
                {
                    if (paramIndex > 0)
                    {
                        writer.Write(", ");
                    }
                    writer.Write(parameterSymbol.GeneratedName);

                    paramIndex++;
                }
            }
        }
Exemplo n.º 5
0
        private static void GenerateConditionalExpression(ScriptGenerator generator, MemberSymbol symbol, ConditionalExpression expression)
        {
            ScriptTextWriter writer = generator.Writer;

            if (expression.Condition.Parenthesized == false)
            {
                writer.Write("(");
            }
            ExpressionGenerator.GenerateExpression(generator, symbol, expression.Condition);
            if (expression.Condition.Parenthesized == false)
            {
                writer.Write(")");
            }
            writer.Write(" ? ");
            ExpressionGenerator.GenerateExpression(generator, symbol, expression.TrueValue);
            writer.Write(" : ");
            ExpressionGenerator.GenerateExpression(generator, symbol, expression.FalseValue);
        }
Exemplo n.º 6
0
        private static void GenerateEventExpression(ScriptGenerator generator, MemberSymbol symbol, EventExpression expression)
        {
            ScriptTextWriter writer = generator.Writer;

            ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference);
            if (expression.Type == ExpressionType.EventAdd)
            {
                writer.Write(".add_");
            }
            else
            {
                writer.Write(".remove_");
            }
            writer.Write(expression.Event.GeneratedName);
            writer.Write("(");
            ExpressionGenerator.GenerateExpression(generator, symbol, expression.Handler);
            writer.Write(")");
        }
Exemplo n.º 7
0
        public static void GenerateClassRegistrationScript(ScriptGenerator generator, ClassSymbol classSymbol)
        {
            // NOTE: This is emitted towards the end of the script file as opposed to immediately after the
            //       class definition, because it allows us to reference other class (base class, interfaces)
            //       without having to do a manual topological sort to get the ordering of class definitions
            //       that would be needed otherwise.

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

            writer.Write(name);
            writer.Write(".registerClass('");
            writer.Write(name);
            writer.Write("'");

            // TODO: We need to introduce the notion of a base class that only exists in the metadata
            //       and not at runtime. At that point this check of IsTestClass can be generalized.
            if (classSymbol.IsTestClass)
            {
                writer.Write(");");
                writer.WriteNewLine();

                return;
            }

            if (classSymbol.BaseClass != null)
            {
                writer.WriteTrimmed(", ");
                writer.Write(classSymbol.BaseClass.FullGeneratedName);
            }

            if (classSymbol.Interfaces != null)
            {
                if (classSymbol.BaseClass == null)
                {
                    writer.WriteTrimmed(", ");
                    writer.Write("null");
                }
                foreach (InterfaceSymbol interfaceSymbol in classSymbol.Interfaces)
                {
                    writer.WriteTrimmed(", ");
                    writer.Write(interfaceSymbol.FullGeneratedName);
                }
            }

            writer.Write(");");
            writer.WriteNewLine();
        }
        private static void GeneratePropertyComment(ScriptTextWriter writer, PropertySymbol propertySymbol)
        {
            GenerateSummaryComment(writer, propertySymbol);

            writer.Write("/// <value");

            GenerateTypeAttributes(writer, propertySymbol.AssociatedType);

            writer.WriteLine("></value>");
        }
Exemplo n.º 9
0
        private static void GenerateBaseInitializerExpression(ScriptGenerator generator, MemberSymbol symbol, BaseInitializerExpression expression)
        {
            ScriptTextWriter writer = generator.Writer;

            Debug.Assert(symbol.Parent is ClassSymbol);

            ClassSymbol baseClass = ((ClassSymbol)symbol.Parent).BaseClass;

            Debug.Assert(baseClass != null);

            writer.Write(baseClass.FullGeneratedName);
            writer.Write(".call(this");
            if (expression.Parameters != null)
            {
                writer.Write(", ");
                GenerateExpressionList(generator, symbol, expression.Parameters);
            }
            writer.Write(")");
        }
Exemplo n.º 10
0
        private static void GenerateIfElseStatement(ScriptGenerator generator, MemberSymbol symbol, IfElseStatement statement)
        {
            if ((statement.IfStatement == null) && (statement.ElseStatement == null))
            {
                return;
            }

            ScriptTextWriter writer = generator.Writer;

            writer.Write("if (");
            ExpressionGenerator.GenerateExpression(generator, symbol, statement.Condition);
            writer.WriteLine(") {");
            if (statement.IfStatement != null)
            {
                writer.Indent++;
                GenerateStatement(generator, symbol, statement.IfStatement);
                writer.Indent--;
            }
            writer.Write("}");
            writer.WriteLine();
            if (statement.ElseStatement != null)
            {
                bool writeEndBlock = false;

                if (statement.ElseStatement is IfElseStatement)
                {
                    writer.Write("else ");
                }
                else
                {
                    writer.WriteLine("else {");
                    writeEndBlock = true;
                    writer.Indent++;
                }
                GenerateStatement(generator, symbol, statement.ElseStatement);
                if (writeEndBlock)
                {
                    writer.Indent--;
                    writer.WriteLine("}");
                }
            }
        }
Exemplo n.º 11
0
        private static void GenerateParameterComment(ScriptTextWriter writer, ParameterSymbol parameterSymbol) {
            writer.Write("/// <param name=\"{0}\"", parameterSymbol.GeneratedName);

            GenerateTypeAttributes(writer, parameterSymbol.ValueType);

            writer.WriteLine(">");

            GenerateFormattedComment(writer, parameterSymbol.Documentation);

            writer.WriteLine("/// </param>");
        }
Exemplo n.º 12
0
        private static void GenerateExpressionStatement(ScriptGenerator generator, MemberSymbol symbol, ExpressionStatement statement)
        {
            ScriptTextWriter writer = generator.Writer;

            ExpressionGenerator.GenerateExpression(generator, symbol, statement.Expression);
            if (statement.IsFragment == false)
            {
                writer.Write(";");
                writer.WriteNewLine();
            }
        }
        private static void GenerateParameterComment(ScriptTextWriter writer, ParameterSymbol parameterSymbol)
        {
            writer.Write("/// <param name=\"{0}\"", parameterSymbol.GeneratedName);

            GenerateTypeAttributes(writer, parameterSymbol.ValueType);

            writer.WriteLine(">");

            GenerateFormattedComment(writer, parameterSymbol.Documentation);

            writer.WriteLine("/// </param>");
        }
Exemplo n.º 14
0
        private static void GenerateRecord(ScriptGenerator generator, RecordSymbol recordSymbol)
        {
            ScriptTextWriter writer     = generator.Writer;
            string           recordName = recordSymbol.FullGeneratedName;

            writer.Write("function ");
            writer.Write(recordName);
            writer.Write("(");
            if (recordSymbol.Constructor != null)
            {
                ConstructorSymbol ctorSymbol = recordSymbol.Constructor;

                if (ctorSymbol.Parameters != null)
                {
                    bool firstParameter = true;
                    foreach (ParameterSymbol parameterSymbol in ctorSymbol.Parameters)
                    {
                        if (firstParameter == false)
                        {
                            writer.Write(", ");
                        }
                        writer.Write(parameterSymbol.GeneratedName);
                        firstParameter = false;
                    }
                }
            }
            writer.Write(") {");
            if (recordSymbol.Constructor != null)
            {
                writer.Indent++;
                writer.WriteLine();
                writer.WriteLine("var $o = {};");
                CodeGenerator.GenerateScript(generator, recordSymbol.Constructor);
                writer.Write("return $o;");
                writer.WriteLine();
                writer.Indent--;
            }
            else
            {
                writer.Write(" return {}; ");
            }
            writer.Write("}");
            writer.WriteLine();
        }
Exemplo n.º 15
0
        private static void GenerateEnumeration(ScriptGenerator generator, EnumerationSymbol enumSymbol)
        {
            ScriptTextWriter writer   = generator.Writer;
            string           enumName = enumSymbol.FullGeneratedName;

            writer.Write("var ");
            writer.Write(enumSymbol.FullGeneratedName);
            writer.Write(" = {");
            writer.Indent++;

            bool firstValue = true;

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

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

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

            writer.Indent--;
            writer.WriteLine();
            writer.Write("};");
            writer.WriteLine();
        }
        private static void GenerateReturnsComment(ScriptTextWriter writer, TypeSymbol typeSymbol)
        {
            if (IsVoid(typeSymbol))
            {
                return;
            }

            writer.Write("/// <returns");

            GenerateTypeAttributes(writer, typeSymbol);

            writer.WriteLine("></returns>");
        }
Exemplo n.º 17
0
        private static void GenerateForStatement(ScriptGenerator generator, MemberSymbol symbol, ForStatement statement)
        {
            if (statement.Body == null)
            {
                return;
            }

            ScriptTextWriter writer = generator.Writer;

            writer.WriteTrimmed("for ");
            writer.Write("(");
            if (statement.Initializers != null)
            {
                ExpressionGenerator.GenerateExpressionList(generator, symbol, statement.Initializers);
            }
            else if (statement.Variables != null)
            {
                GenerateVariableDeclarations(generator, symbol, statement.Variables);
            }
            writer.WriteTrimmed("; ");
            if (statement.Condition != null)
            {
                ExpressionGenerator.GenerateExpression(generator, symbol, statement.Condition);
            }
            writer.WriteTrimmed("; ");
            if (statement.Increments != null)
            {
                ExpressionGenerator.GenerateExpressionList(generator, symbol, statement.Increments);
            }
            writer.WriteTrimmed(") ");
            writer.Write("{");
            writer.WriteNewLine();
            writer.Indent++;
            GenerateStatement(generator, symbol, statement.Body);
            writer.Indent--;
            writer.Write("}");
            writer.WriteNewLine();
        }
Exemplo n.º 18
0
        private static void GenerateReturnStatement(ScriptGenerator generator, MemberSymbol symbol, ReturnStatement statement)
        {
            ScriptTextWriter writer = generator.Writer;

            if (statement.Value != null)
            {
                writer.Write("return ");
                ExpressionGenerator.GenerateExpression(generator, symbol, statement.Value);
                writer.WriteLine(";");
            }
            else
            {
                writer.WriteLine("return;");
            }
        }
Exemplo n.º 19
0
        public static void GenerateClassConstructorScript(ScriptGenerator generator, ClassSymbol classSymbol)
        {
            // NOTE: This is emitted last in the script file, and separate from the initial class definition
            //       because it needs to be emitted after the class registration.

            foreach (MemberSymbol memberSymbol in classSymbol.Members)
            {
                if ((memberSymbol.Type == SymbolType.Field) &&
                    ((memberSymbol.Visibility & MemberVisibility.Static) != 0))
                {
                    if (((FieldSymbol)memberSymbol).IsConstant &&
                        ((memberSymbol.Visibility & (MemberVisibility.Public | MemberVisibility.Protected)) == 0))
                    {
                        // PrivateInstance/Internal constant fields are omitted since they have been inlined
                        continue;
                    }

                    MemberGenerator.GenerateScript(generator, memberSymbol);
                }
            }

            if (classSymbol.StaticConstructor != null)
            {
                ScriptTextWriter writer = generator.Writer;

                writer.Write("(function");
                writer.WriteTrimmed(" () ");
                writer.Write("{");
                writer.WriteNewLine();
                writer.Indent++;
                CodeGenerator.GenerateScript(generator, classSymbol.StaticConstructor);
                writer.Indent--;
                writer.Write("})();");
                writer.WriteSignificantNewLine();
            }
        }
Exemplo n.º 20
0
        private static void GenerateFieldComment(ScriptTextWriter writer, FieldSymbol fieldSymbol)
        {
            writer.Write("/// <field name=\"{0}\"", fieldSymbol.GeneratedName);

            GenerateTypeAttributes(writer, fieldSymbol.AssociatedType);

            if ((fieldSymbol.Visibility & MemberVisibility.Static) != 0) {
                writer.Write(" static=\"true\"");
            }

            writer.WriteLine(">");

            GenerateFormattedComment(writer, fieldSymbol.Documentation);

            writer.WriteLine("/// </field>");
        }
Exemplo n.º 21
0
        public static void GenerateExpressionList(ScriptGenerator generator, MemberSymbol symbol, ICollection <Expression> expressions)
        {
            ScriptTextWriter writer = generator.Writer;

            bool firstExpression = true;

            foreach (Expression expression in expressions)
            {
                if (firstExpression == false)
                {
                    writer.Write(", ");
                }
                GenerateExpression(generator, symbol, expression);
                firstExpression = false;
            }
        }
Exemplo n.º 22
0
        public static void GenerateClassConstructorScript(ScriptGenerator generator, ClassSymbol classSymbol)
        {
            // NOTE: This is emitted last in the script file, and separate from the initial class definition
            //       because it needs to be emitted after the class registration.

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

                    if (fieldSymbol.IsConstant &&
                        ((memberSymbol.Visibility & (MemberVisibility.Public | MemberVisibility.Protected)) == 0))
                    {
                        // PrivateInstance/Internal constant fields are omitted since they have been inlined
                        continue;
                    }

                    if (fieldSymbol.HasInitializer)
                    {
                        MemberGenerator.GenerateScript(generator, memberSymbol);
                    }
                }
            }

            if (classSymbol.StaticConstructor != null)
            {
                ScriptTextWriter writer = generator.Writer;

                SymbolImplementation implementation = classSymbol.StaticConstructor.Implementation;
                bool requiresFunctionScope          = implementation.DeclaresVariables;

                if (requiresFunctionScope)
                {
                    writer.WriteLine("(function() {");
                    writer.Indent++;
                }
                CodeGenerator.GenerateScript(generator, classSymbol.StaticConstructor);
                if (requiresFunctionScope)
                {
                    writer.Indent--;
                    writer.Write("})();");
                    writer.WriteLine();
                }
            }
        }
Exemplo n.º 23
0
        private static void GenerateTryCatchFinallyStatement(ScriptGenerator generator, MemberSymbol symbol, TryCatchFinallyStatement statement)
        {
            ScriptTextWriter writer = generator.Writer;

            writer.WriteTrimmed("try ");
            writer.Write("{");
            writer.WriteNewLine();
            writer.Indent++;
            GenerateStatement(generator, symbol, statement.Body);
            writer.Indent--;
            writer.Write("}");
            writer.WriteNewLine();
            if (statement.Catch != null)
            {
                writer.WriteTrimmed("catch ");
                writer.Write("(");
                writer.Write(statement.ExceptionVariable.GeneratedName);
                writer.WriteTrimmed(") ");
                writer.Write("{");
                writer.WriteNewLine();
                writer.Indent++;
                GenerateStatement(generator, symbol, statement.Catch);
                writer.Indent--;
                writer.Write("}");
                writer.WriteNewLine();
            }
            if (statement.Finally != null)
            {
                writer.WriteTrimmed("finally ");
                writer.Write("{");
                writer.WriteNewLine();
                writer.Indent++;
                GenerateStatement(generator, symbol, statement.Finally);
                writer.Indent--;
                writer.Write("}");
                writer.WriteNewLine();
            }
        }
Exemplo n.º 24
0
        private static void GenerateWhileStatement(ScriptGenerator generator, MemberSymbol symbol, WhileStatement statement)
        {
            if (statement.Body == null)
            {
                return;
            }

            ScriptTextWriter writer = generator.Writer;

            if (statement.PreCondition)
            {
                writer.WriteTrimmed("while ");
                writer.Write("(");
                ExpressionGenerator.GenerateExpression(generator, symbol, statement.Condition);
                writer.WriteTrimmed(") ");
                writer.Write("{");
                writer.WriteNewLine();
            }
            else
            {
                writer.WriteTrimmed("do ");
                writer.Write("{");
                writer.WriteNewLine();
            }

            writer.Indent++;
            GenerateStatement(generator, symbol, statement.Body);
            writer.Indent--;

            if (statement.PreCondition)
            {
                writer.Write("}");
                writer.WriteNewLine();
            }
            else
            {
                writer.WriteTrimmed("} ");
                writer.WriteTrimmed("while ");
                writer.Write("(");
                ExpressionGenerator.GenerateExpression(generator, symbol, statement.Condition);
                writer.Write(");");
                writer.WriteNewLine();
            }
        }
Exemplo n.º 25
0
        private static void GeneratePropertyExpression(ScriptGenerator generator, MemberSymbol symbol, PropertyExpression expression)
        {
            Debug.Assert(expression.Type == ExpressionType.PropertyGet);
            ScriptTextWriter writer = generator.Writer;

            if (expression.ObjectReference is BaseExpression)
            {
                Debug.Assert(symbol.Parent is ClassSymbol);

                writer.Write(((ClassSymbol)symbol.Parent).FullGeneratedName);
                writer.Write(".callBaseMethod(this, 'get_");
                writer.Write(expression.Property.GeneratedName);
                writer.Write("')");
            }
            else
            {
                ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference);
                writer.Write(".get_");
                writer.Write(expression.Property.GeneratedName);
                writer.Write("()");
            }
        }
Exemplo n.º 26
0
        private static void GenerateSwitchStatement(ScriptGenerator generator, MemberSymbol symbol, SwitchStatement statement)
        {
            ScriptTextWriter writer = generator.Writer;

            writer.WriteTrimmed("switch ");
            writer.Write("(");
            ExpressionGenerator.GenerateExpression(generator, symbol, statement.Condition);
            writer.WriteTrimmed(") ");
            writer.Write("{");
            writer.WriteNewLine();
            writer.Indent++;
            foreach (SwitchGroup group in statement.Groups)
            {
                if (group.Cases != null)
                {
                    foreach (Expression caseExpression in group.Cases)
                    {
                        writer.Write("case ");
                        ExpressionGenerator.GenerateExpression(generator, symbol, caseExpression);
                        writer.Write(":");
                        writer.WriteNewLine();
                    }
                }

                if (group.IncludeDefault)
                {
                    writer.Write("default:");
                    writer.WriteNewLine();
                }

                writer.Indent++;
                foreach (Statement childStatement in group.Statements)
                {
                    GenerateStatement(generator, symbol, childStatement);
                }
                writer.Indent--;
            }
            writer.Indent--;
            writer.Write("}");
            writer.WriteNewLine();
        }
        private static void GenerateTypeAttributes(ScriptTextWriter writer, TypeSymbol typeSymbol)
        {
            if (IsDomElement(typeSymbol))
            {
                writer.Write(" type=\"Object\" domElement=\"true\"");
            }
            else
            {
                writer.Write(" type=\"{0}\"", typeSymbol.FullGeneratedName);
            }

            if (IsInteger(typeSymbol))
            {
                writer.Write(" integer=\"true\"");
            }

            if (typeSymbol.IsArray)
            {
                ClassSymbol classSymbol = (ClassSymbol)typeSymbol;

                TypeSymbol elementTypeSymbol = classSymbol.Indexer.AssociatedType;

                if (IsDomElement(elementTypeSymbol))
                {
                    writer.Write(" elementType=\"Object\" elementDomElement=\"true\"");
                }
                else
                {
                    writer.Write(" elementType=\"{0}\"", elementTypeSymbol.GeneratedName);
                }

                if (IsInteger(elementTypeSymbol))
                {
                    writer.Write(" elementInteger=\"true\"");
                }
            }
        }
Exemplo n.º 28
0
        public static void GenerateScript(ScriptGenerator generator, ClassSymbol testClassSymbol)
        {
            Debug.Assert(generator != null);

            List <MethodSymbol> testMethods = new List <MethodSymbol>();
            bool hasSetup   = false;
            bool hasCleanup = false;

            foreach (MemberSymbol member in testClassSymbol.Members)
            {
                if (member.Type == SymbolType.Method)
                {
                    if (String.CompareOrdinal(member.Name, "Setup") == 0)
                    {
                        hasSetup = true;
                    }
                    else if (String.CompareOrdinal(member.Name, "Cleanup") == 0)
                    {
                        hasCleanup = true;
                    }
                    else if (member.Name.StartsWith("Test", StringComparison.Ordinal))
                    {
                        testMethods.Add((MethodSymbol)member);
                    }
                }
            }

            ScriptTextWriter writer = generator.Writer;

            writer.WriteLine();
            writer.Write("module('");
            writer.Write(testClassSymbol.GeneratedName);
            writer.WriteLine("', {");
            writer.Indent++;
            writer.WriteLine("setup: function() {");
            writer.Indent++;
            writer.Write("this.instance = new ");
            writer.Write(testClassSymbol.FullGeneratedName);
            writer.WriteLine("();");
            if (hasSetup)
            {
                writer.WriteLine("this.instance.setup();");
            }
            writer.Indent--;
            writer.WriteLine("},");
            writer.WriteLine("teardown: function() {");
            writer.Indent++;
            if (hasCleanup)
            {
                writer.WriteLine("this.instance.cleanup();");
            }
            writer.WriteLine("delete this.instance;");
            writer.Indent--;
            writer.WriteLine("}");
            writer.Indent--;
            writer.WriteLine("});");
            writer.WriteLine();

            foreach (MethodSymbol testMethod in testMethods)
            {
                writer.Write("test('");
                writer.Write(testMethod.GeneratedName);
                writer.WriteLine("', function() {");
                writer.Indent++;
                writer.Write("this.instance.");
                writer.Write(testMethod.GeneratedName);
                writer.WriteLine("();");
                writer.Indent--;
                writer.WriteLine("});");
            }
        }
Exemplo n.º 29
0
        private static void GenerateRecord(ScriptGenerator generator, RecordSymbol recordSymbol)
        {
            ScriptTextWriter writer     = generator.Writer;
            string           recordName = recordSymbol.GeneratedName;

            if ((recordSymbol.Namespace.Length == 0) || recordSymbol.IgnoreNamespace)
            {
                writer.Write("window.");
            }
            else
            {
                writer.Write(recordSymbol.GeneratedNamespace);
                writer.Write(".");
            }
            writer.Write("$create_");
            writer.Write(recordName);
            writer.WriteTrimmed(" = ");
            writer.Write("function");
            if (generator.Options.DebugFlavor)
            {
                writer.Write(" ");
                writer.Write(recordSymbol.FullGeneratedName.Replace(".", "_"));
            }
            writer.Write("(");
            if (recordSymbol.Constructor != null)
            {
                ConstructorSymbol ctorSymbol = recordSymbol.Constructor;

                if (ctorSymbol.Parameters != null)
                {
                    bool firstParameter = true;
                    foreach (ParameterSymbol parameterSymbol in ctorSymbol.Parameters)
                    {
                        if (firstParameter == false)
                        {
                            writer.WriteTrimmed(", ");
                        }
                        writer.Write(parameterSymbol.GeneratedName);
                        firstParameter = false;
                    }
                }
            }
            writer.WriteTrimmed(")");
            writer.WriteTrimmed(" {");
            if (recordSymbol.Constructor != null)
            {
                writer.Indent++;
                writer.WriteNewLine();
                writer.Write("var $o");
                writer.WriteTrimmed(" = ");
                writer.WriteTrimmed("{ ");
                writer.Write("};");
                writer.WriteNewLine();
                CodeGenerator.GenerateScript(generator, recordSymbol.Constructor);
                writer.Write("return $o;");
                writer.WriteNewLine();
                writer.Indent--;
            }
            else
            {
                writer.WriteTrimmed(" return {}; ");
            }
            writer.Write("}");
            writer.WriteSignificantNewLine();
        }
Exemplo n.º 30
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();
        }
Exemplo n.º 31
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();
        }
Exemplo n.º 32
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);
        }
Exemplo n.º 33
0
        private static void GeneratePropertyComment(ScriptTextWriter writer, PropertySymbol propertySymbol) {
            GenerateSummaryComment(writer, propertySymbol);

            writer.Write("/// <value");

            GenerateTypeAttributes(writer, propertySymbol.AssociatedType);

            writer.WriteLine("></value>");
        }
Exemplo n.º 34
0
        private static void GenerateReturnsComment(ScriptTextWriter writer, TypeSymbol typeSymbol) {
            if (IsVoid(typeSymbol)) {
                return;
            }

            writer.Write("/// <returns");

            GenerateTypeAttributes(writer, typeSymbol);

            writer.WriteLine("></returns>");
        }
Exemplo n.º 35
0
        private static void GenerateTypeAttributes(ScriptTextWriter writer, TypeSymbol typeSymbol) {
            if (IsDomElement(typeSymbol)) {
                writer.Write(" type=\"Object\" domElement=\"true\"");
            }
            else {
                writer.Write(" type=\"{0}\"", typeSymbol.FullGeneratedName);
            }

            if (IsInteger(typeSymbol)) {
                writer.Write(" integer=\"true\"");
            }

            if (typeSymbol.IsArray) {
                ClassSymbol classSymbol = (ClassSymbol)typeSymbol;

                TypeSymbol elementTypeSymbol = classSymbol.Indexer.AssociatedType;

                if (IsDomElement(elementTypeSymbol)) {
                    writer.Write(" elementType=\"Object\" elementDomElement=\"true\"");
                }
                else {
                    writer.Write(" elementType=\"{0}\"", elementTypeSymbol.GeneratedName);
                }

                if (IsInteger(elementTypeSymbol)) {
                    writer.Write(" elementInteger=\"true\"");
                }
            }
        }