public override JsNode _VisitDelegate(ITypeDefinition ce) { var CurrentType = new JsClrType { Kind = JsClrTypeKind.Delegate }; CurrentType.fullname = GetJsTypeName(ce); //Generate constructor var genericParams = new List <ITypeParameter>(ce.GetGenericArguments()); var func = new JsFunction(); func.Parameters = genericParams.Select(t => t.Name).ToList(); func.Parameters.Add("obj"); func.Parameters.Add("func"); func.Block = Js.Block(); foreach (var ga in genericParams) { func.Block.Add(Js.This().Member(ga.Name).Assign(Js.Member(ga.Name)).Statement()); } func.Block.Add(Js.Members("System.MulticastDelegate.ctor.call").Invoke(Js.This(), Js.Member("obj"), Js.Member("func")).Statement()); CurrentType.GetDefinition(false)["ctor"] = func; return(OnAfterExportType(ce, CurrentType)); //return func; //FullName',{ ShortName:function(T1,T2,T3,...,obj,func){this.T1=T1;....;this.construct(obj,func);}, })"); }
private void ImportToType(JsClrType ce, List <EntityToJsNode> mappings) { foreach (var map in mappings) { if (map.JsNode == null) { continue; } var isStatic = map.Entity.IsStatic(); var def = ce.GetDefinition(isStatic); List <JsJsonNameValue> pairs; if (map.JsNode is JsNodeList) { pairs = ((JsNodeList)map.JsNode).Nodes.Cast <JsJsonNameValue>().ToList(); } else { pairs = new List <JsJsonNameValue> { (JsJsonNameValue)map.JsNode } }; foreach (var pair in pairs) { if (pair == null) { throw new CompilerException(map.Entity, "Export of entity is null"); } var name = pair.Name.Name; //if (def[name] != pair.Value) // throw new Exception(); def[name] = pair.Value; } } }
protected virtual JsUnit OnAfterExportType(ITypeDefinition ce, JsClrType jsType) { //HACK: backward if (jsType.Kind == JsClrTypeKind.Interface && jsType.baseTypeName.IsNullOrEmpty()) { jsType.baseTypeName = "System.Object"; } var unit = new JsUnit { Statements = new List <JsStatement>() }; unit.Statements.Add(VerifyJsTypesArrayStatement.Clone()); var json = (JsJsonObjectExpression)Serialize(jsType); var moveToEnd = json.NamesValues.Where(t => t.Name.Name.Contains("definition")).ToList(); moveToEnd.ForEach(t => json.NamesValues.Remove(t)); json.NamesValues.AddRange(moveToEnd); var ceVarName = GetJsTypeName(ce).Replace(".", "$"); unit.Statements.Add(Js.Var(ceVarName, json).Statement()); unit.Statements.Add(Js.Member("JsTypes").Member("push").Invoke(Js.Member(ceVarName)).Statement()); return(unit); }
protected override JsUnit OnAfterExportType(ITypeDefinition ce, JsClrType jsType) { var extJsType = new ExtJsType { extend = jsType.baseTypeName, statics = jsType.staticDefinition, }; //if (extJsType.extend == "System.Object") // extJsType.extend = null; var json = (JsJsonObjectExpression)Serialize(extJsType); //var ctor = json.NamesValues.Where(t => t.Name.Name == "ctor").FirstOrDefault(); if (jsType.definition == null) { jsType.definition = new JsObject(); } var ctor = jsType.definition.TryGetValue("ctor"); if (ctor != null) { jsType.definition.Remove("ctor"); jsType.definition.Add("constructor", ctor); //ctor.Name.Name = "constructor"; //var func = ctor.Value as JsFunction; //if (func != null) //{ // func.Block.Statements.Insert(0, Js.This().Member("callSuper").Invoke(Js.Member("arguments")).Statement()); //} } foreach (var me in jsType.definition) { var name = me.Key; var value = (JsExpression)me.Value; if (json.NamesValues == null) { json.NamesValues = new List <JsJsonNameValue>(); } json.NamesValues.Add(new JsJsonNameValue { Name = new JsJsonMember { Name = name }, Value = value }); } var define = Js.Members("Ext.define").Invoke(Js.String(jsType.fullname), json).Statement(); var unit = new JsUnit { Statements = new List <JsStatement> { define } }; return(unit);// base.OnAfterExportType(ce, jsType); }
private void ExportImplementedInterfaces(ITypeDefinition ce, JsClrType jsType) { var list = ce.DirectBaseTypes.Where(t => t.Kind == TypeKind.Interface).ToList(); if (list.Count == 0) { return; } if (jsType.interfaceNames == null) { jsType.interfaceNames = new JsArray <string>(); } jsType.interfaceNames.AddRange(list.Select(GetJsTypeName)); }
public override JsNode _VisitEnum(ITypeDefinition ce) { var CurrentType = new JsClrType { Kind = JsClrTypeKind.Enum }; CurrentType.fullname = GetJsTypeName(ce); var json = VisitEnumToJson(ce); foreach (var item in json.NamesValues) { ////TODO: baseType (long, byte, etc...) //foreach (var pe in ce.GetConstants()) //{ //var name = pe.Name; CurrentType.GetDefinition(true)[item.Name.Name] = item.Value; // } } return(OnAfterExportType(ce, CurrentType)); }
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); }