protected virtual JsNode ExportPropertyInfo(IProperty pe) { var name = SkJs.GetEntityJsName(pe) + "$$"; var value = Js.String(pe.GetPropertyTypeRef().GetFullyQualifiedCLRName()); //CurrentType.GetDefinition(pe.IsStatic)[name] = value; return(Js.JsonNameValue(name, value)); }
public override JsNode ExportConstructor(IMethod ctor) { var ctorName = SkJs.GetEntityJsName(ctor); var func = base.ExportConstructor(ctor); //CurrentType.definition[ctorName] = func; return(Js.JsonNameValue(ctorName, (JsExpression)func)); //return func; }
JsJsonNameValue VisitEnumField(IField pe, bool valuesAsNames) { if (valuesAsNames) { return(Js.JsonNameValue(pe.Name, Js.String(pe.Name))); } else { return(Js.JsonNameValue(pe.Name, Js.Value(pe.ConstantValue))); } }
public override JsNode _VisitField(IField fld) { var initializer = GetCreateFieldInitializer(fld); if (initializer == null) { initializer = Cs.Null(); } var node = AstNodeConverter.Visit(initializer); return(Js.JsonNameValue(fld.Name, (JsExpression)node)); }
public override JsNode _Visit(IEvent ee) { if (ee.DeclaringType.Kind == TypeKind.Interface) { return(null); } var list2 = new JsNodeList { Nodes = new List <JsNode>() }; if (ee.IsAutomatic(Compiler.Project)) { var adder = GenerateAutomaticEventAccessor(ee, false); var remover = GenerateAutomaticEventAccessor(ee, true); //TODO: //var adder = Js.Code(String.Format("function(value){{this.{0}=CombineDelegates(this.{0}, value);}}", ee.Name)); //var remover = Js.Code(String.Format("function(value){{this.{0}=RemoveDelegate(this.{0}, value);}}", ee.Name)); list2.Nodes.Add(Js.JsonNameValue("add_" + ee.Name, adder)); list2.Nodes.Add(Js.JsonNameValue("remove_" + ee.Name, remover)); } else { var list = new List <IMethod>(); if (ee.AddAccessor != null) { if (ee.AddAccessor.Name == null) { throw new NotImplementedException(); //ee.AddAccessor.Name = "add_" + ee.Name; } list.Add(ee.AddAccessor); } if (ee.RemoveAccessor != null) { if (ee.RemoveAccessor.Name == null) { throw new NotImplementedException(); } //ee.RemoveAccessor.name = "remove_" + ee.name; list.Add(ee.RemoveAccessor); } foreach (var node in list) { var node2 = Visit(node); //if (node2 == null) // throw new CompilerException(ee, "Export of event accessor returned null"); list2.Nodes.Add(node2); } } return(list2); }
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); } }
public override JsNode ExportMethod(IMethod me) { var node = base.ExportMethod(me); if (node == null) { return(node); } if (!node.Is(JsNodeType.Function)) { return(node); } var func = (JsFunction)node; var funcName = func.Name; func.Name = null; if (LongFunctionNames) { func.Name = SkJs.GetLongFunctionName(me); } return(Js.JsonNameValue(funcName, func)); }
internal JsExpression InitializersToJson(IList <ResolveResult> initializerStatements, IType type) { //if (Sk.IsNativeArrayEnumerator(type.GetDefinition())) //FIX for issue 325: if (Sk.IsNativeArrayEnumerator(type.GetDefinition()) && (initializerStatements.IsEmpty() || initializerStatements[0] is CSharpInvocationResolveResult)) { var items = initializerStatements.Cast <CSharpInvocationResolveResult>().Select(t => t.Arguments[0]).ToList(); var items2 = VisitExpressions(items); var arr = Js.NewJsonArray(items2.ToArray()); return(arr); } else { var json = Js.Json(); foreach (var st in initializerStatements) { if (st is OperatorResolveResult) { var op = (OperatorResolveResult)st; var mrr = (MemberResolveResult)op.Operands[0]; var name = SkJs.GetEntityJsName(mrr.Member); var value = VisitExpression(op.Operands[1]); var pair = Js.JsonNameValue(name, value); if (mrr.TargetResult is MemberResolveResult) //happens when using object initializers to set inner properties, e.g. new Parent { Child = { Name="ggg" } } { var targetMrr = (MemberResolveResult)mrr.TargetResult; var name2 = SkJs.GetEntityJsName(targetMrr.Member); var innerJson = Js.Json(); innerJson.Add(pair); pair = Js.JsonNameValue(name2, innerJson); } json.Add(pair); } else if (st is InvocationResolveResult) { var irr = (InvocationResolveResult)st; var targetMrr = irr.TargetResult as MemberResolveResult; if (targetMrr == null) { throw new CompilerException(st.GetFirstNode(), "Expected MemberResolveResult"); } var name = SkJs.GetEntityJsName(targetMrr.Member); if (irr.Arguments.Count != 1) { throw new CompilerException(st.GetFirstNode(), "Expected one argument, not " + name + " " + irr.Arguments.Count); } var value = VisitExpression(irr.Arguments[0]); var jsonMember = json.NamesValues.NotNull().FirstOrDefault(t => t.Name.Name == name); if (jsonMember == null) { json.Add(name, Js.NewJsonArray(value)); } else { var array = jsonMember.Value as JsJsonArrayExpression; if (array == null) { throw new CompilerException(st.GetFirstNode(), "json member value array not found " + name); } array.Items.Add(value); } } else { throw new NotImplementedException(); } } //var inits2 = initializerStatements.Select(t => Visit(t)).ToList(); //var namesValues = inits2.Cast<JsBinaryExpression>().Select(t => Js.JsonNameValue(((JsMemberExpression)t.Left).Name, t.Right)).ToList(); //var json = Js.Json(); //json.NamesValues = namesValues; return(json); } }