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(); } }
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); }
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(); } }