Example #1
0
 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);
     }
 }