public List <IMember> GetMembersToExport(ITypeDefinition ce) { var members = ce.Members.Where(t => ShouldExportMember(t)).ToList(); var fields = GeneratePropertyFields(ce, true).Concat(GeneratePropertyFields(ce, false)); members = members.Concat(fields).ToList(); var ctor = ce.Members.Where(t => t.SymbolKind == SymbolKind.Constructor && !t.IsStatic).FirstOrDefault(); if (ctor == null && !Sk.OmitDefaultConstructor(ce)) { ctor = GenerateDefaultConstructor(ce); if (ctor != null) { members.Add(ctor); } } if (ctor != null && members.Contains(ctor)) { var ctorIndex = 0; if (members.IndexOf(ctor) != ctorIndex) { members.Remove(ctor); members.Insert(ctorIndex, ctor); } } var inlineFields = Sk.InlineFields(ce); if (!inlineFields) { var vars = members.Where(t => t.SymbolKind == SymbolKind.Field).Cast <IField>(); if (vars.Where(t => t.IsStatic()).FirstOrDefault() != null) { var cctor = ce.GetConstructors(false, true).FirstOrDefault(); if (cctor == null) { cctor = CreateStaticCtor(ce); members.Insert(1, cctor); } } members.RemoveAll(t => t.SymbolKind == SymbolKind.Field); } return(members); }
public override JsNode _Visit(IProperty pe) { if (Sk.IsNativeField(pe)) { if (Sk.InlineFields(pe.GetDeclaringTypeDefinition())) { var fe = GenerateFakeField(pe); var value = AstNodeConverter.Visit(GetCreateInitializer(fe)); return(Js.JsonNameValue(pe.Name, (JsExpression)value)); } throw new Exception(); } else { var list2 = new JsNodeList { Nodes = new List <JsNode>() }; var node2 = ExportPropertyInfo(pe); if (node2 != null) { list2.Nodes.Add(node2); } var list = GetAccessorsToExport(pe); if (list.Count > 0) { foreach (var accessor in list) { var pair = (JsJsonNameValue)ExportMethod(accessor); list2.Nodes.Add(pair); } } //else if (pe.IsAutomaticProperty()) //{ // throw new NotImplementedException(); // //var def = CurrentType.GetDefinition(pe.IsStatic); // var getter = Js.Code(String.Format("function(){{return this._{0};}}", pe.Name)); ; // var setter = Js.Code(String.Format("function(value){{this._{0} = value;}}", pe.Name)); // list2.Nodes.Add(new JsJsonNameValue { Name = new JsJsonMember { Name = SkJs.GetEntityJsName(pe.Getter) }, Value = getter }); // list2.Nodes.Add(new JsJsonNameValue { Name = new JsJsonMember { Name = SkJs.GetEntityJsName(pe.Setter) }, Value = setter }); //} return(list2); } }
protected JsBlock ExportConstructorBody(IMethod ctor) { var ctorNode = (ConstructorDeclaration)ctor.GetDeclaration(); BlockStatement ccc = null; if (ctorNode != null) { ccc = ctorNode.Body; } //var ccc = ctor.GetDefinition();//.decl as CsConstructor; //var ccc = ctor.GetDefinition(); var block2 = (JsBlock)AstNodeConverter.Visit(ccc); if (block2 == null) { block2 = new JsBlock { Statements = new List <JsStatement>() } } ; var ce = ctor.GetDeclaringTypeDefinition(); var isClr = Sk.IsClrType(ce); var isPrototype = Sk.IsNativeType(ce); var statements = new List <JsStatement>(); //instance fields initializations if (!Sk.InlineFields(ce)) { var isGlobal = Sk.IsGlobalType(ce); var fields = GetExportedDeclaredAndGeneratedFields(ce, ctor.IsStatic); //var fields = ctor.GetDeclaringTypeDefinition().GetFields(null, GetMemberOptions.IgnoreInheritedMembers).Where(t => t.IsStatic() == ctor.IsStatic).ToList(); //fields = fields.Where(ShouldExportField).ToList(); //fields.AddRange(GeneratePropertyFields(ctor.DeclaringTypeDefinition, ctor.IsStatic)); //var props = ctor.GetDeclaringTypeDefinition().GetProperties(null, GetMemberOptions.IgnoreInheritedMembers).Where(t => t.IsStatic() == ctor.IsStatic).ToList(); //props = props.Where(t=>Sk.IsNativeField(t)).ToList(); //props = props.Where(t => Sk.IsJsExported(t)).ToList(); //var fieldsAndProperties = fields.Cast<IEntity>().Concat(props.Cast<IEntity>()); var initializers = fields.Select(fe => ExportInitializer(fe, null, isGlobal, false)).Cast <JsStatement>().ToList(); if (initializers.Contains(null)) { Log.Warn("Some field initializers were not exported"); } statements.AddRange(initializers.Where(t => t != null)); } if (!ctor.IsStatic) { //base/this ctor invocation var invocation = GetConstructorBaseOrThisInvocation2(ctor); if (invocation != null) { var baseThisCe = invocation.Member.DeclaringType; var isBaseClr = Sk.IsClrType(baseThisCe.GetDefinition()); var isBasePrototype = Sk.IsNativeType(baseThisCe.GetDefinition()) && !Sk.IsJsonMode(baseThisCe.GetDefinition()) && !Sk.IsGlobalType(baseThisCe.GetDefinition()); //happens when prototype inherits from json if (isBaseClr == isClr && isBasePrototype == isPrototype) //base and derived are both prototype, or both are clr { var newObjExp2 = AstNodeConverter.VisitExpression(invocation); JsInvocationExpression invocation2; if (newObjExp2 is JsNewObjectExpression) { var newObjExp = (JsNewObjectExpression)newObjExp2; invocation2 = newObjExp.Invocation; } else if (newObjExp2 is JsInvocationExpression) { invocation2 = (JsInvocationExpression)newObjExp2; } else { throw new Exception("Unexpected node: " + newObjExp2); } if (Sk.IsExtJsType(ce)) { var invocation3 = Js.This().Member("callParent").Invoke(); if (invocation2.Arguments.IsNotNullOrEmpty()) { invocation3.Arguments = new List <JsExpression> { Js.NewJsonArray(invocation2.Arguments.NotNull().ToArray()) } } ; statements.Add(invocation3.Statement()); } else { JsRefactorer.ToCallWithContext(invocation2, new JsThis()); statements.Add(invocation2.Statement()); } } } } if (block2.Statements == null) { block2.Statements = new List <JsStatement>(); } block2.Statements.InsertRange(0, statements); return(block2); } void ExportConstructorParameters(IMethod ctor, JsFunction func) { var ce = ctor.GetDeclaringTypeDefinition(); var list = new List <string>(); if (!Sk.IgnoreTypeArguments(ce)) { //danel var gprms = ce.TypeParameters.ToList();//.GetGenericArguments().Where(ga => ga.isGenericParam()).ToList(); if (gprms.IsNotNullOrEmpty()) { var i = 0; foreach (var gprm in gprms) { func.Parameters.Add(gprm.Name); if (!ctor.IsStatic && func.Block != null) { func.Block.Statements.Insert(i, Js.This().Member(gprm.Name).Assign(Js.Member(gprm.Name)).Statement()); i++; } } } } var prms = ctor.Parameters; if (prms != null) { func.Parameters.AddRange(prms.Select(t => t.Name)); } }
private JsNode ExportClassStructInterface(ITypeDefinition ce) { var jsType = new JsClrType(); jsType.Kind = GetJsTypeKind(ce); jsType.fullname = GetJsTypeName(ce); var baseClass = GetBaseClassIfValid(ce, true); if (baseClass != null) { var baseClassName = GetJsTypeName(baseClass); if (baseClassName != jsType.fullname) //avoid object:object inheritance recursion { jsType.baseTypeName = baseClassName; } } var globalCode = new List <JsUnit>(); if (!ce.IsInterface() || Sk.NewInterfaceImplementation) { var members = GetMembersToExport(ce); var inlineFields = Sk.InlineFields(ce); var cctor = ce.GetConstructors(false, true).FirstOrDefault(); if (cctor == null && !inlineFields && ce.GetFields().Where(t => t.IsStatic).FirstOrDefault() != null) { cctor = CreateStaticCtor(ce); if (ShouldExportConstructor(cctor)) { members.Insert(0, cctor); } } var mappings = members.Select(t => new EntityToJsNode { JsNode = Visit(t), Entity = t }).ToList(); //Remove GlobalCode=True members from mappings for (var i = mappings.Count - 1; i >= 0; i--) { if (mappings[i].JsNode is JsUnit) { //Maybe more clean: Another way could be to check the IEntity for GlobalCode=True globalCode.Add((JsUnit)mappings[i].JsNode); mappings.RemoveAt(i); } } ImportToType(jsType, mappings); } ExportImplementedInterfaces(ce, jsType); jsType.assemblyName = AssemblyName; var atts = ExportCustomAttributes(ce); if (atts.IsNotNullOrEmpty()) { if (jsType.customAttributes == null) { jsType.customAttributes = new JsArray <JsClrAttribute>(); } jsType.customAttributes.AddRange(atts); } var unit = OnAfterExportType(ce, jsType); globalCode.ForEach(t => unit.Statements.AddRange(t.Statements)); return(unit); }