Пример #1
0
        internal JsNode ForceInteger(JsNode node2)
        {
            var exp = node2 as JsExpression;

            if (exp == null)
            {
                return(node2); //problem
            }
            if (!exp.Is(JsNodeType.MemberExpression))
            {
                exp = Js.Parentheses(exp);
            }
            return(Js.BitwiseOr(exp, Js.Value(0)));
        }
Пример #2
0
 public JsNode VisitTypeIsResolveResult(TypeIsResolveResult res)
 {
     if (Sk.OmitCasts(res.TargetType))
     {
         return(Js.True());
         //var node2 = Visit(res.Input);
         //return node2;
     }
     else if (Sk.NativeCasts(res.TargetType))
     {
         var typeFieldName = Sk.GetJsonTypeFieldName(res.TargetType.GetDefinitionOrArrayType());
         if (typeFieldName != null && Sk.IsJsonMode(res.TargetType.GetDefinitionOrArrayType()))
         {
             return(Js.Parentheses(VisitExpression(res.Input).Or(Js.Json())).Member(typeFieldName).Equal(SkJs.EntityTypeRefToMember(res.TargetType)));
         }
         var node2 = VisitExpression(res.Input).InstanceOf(SkJs.EntityTypeRefToMember(res.TargetType));
         return(node2);
     }
     else
     {
         var node2 = Js.Member("Is").Invoke(VisitExpression(res.Input), SkJs.EntityTypeRefToMember(res.TargetType));
         return(node2);
     }
 }
Пример #3
0
        private JsNode VisitConversion(ResolveResult input, Conversion conversion, IType conversionType)
        {
            ////TODO: HACK: https://github.com/icsharpcode/NRefactory/issues/183
            //var isImplicit = res.Conversion.IsImplicit;
            //if (!isImplicit && res.Conversion.IsExplicit && res.Conversion.Method != null && res.Conversion.Method.Name != null && res.Conversion.Method.Name.Contains("Implicit"))
            //    isImplicit = true;
            if (conversion.IsMethodGroupConversion)
            {
            }
            else if (conversion.IsUserDefined)
            {
                ITypeDefinition typeDef;
                if (conversion.Method != null && conversion.Method.DeclaringType != null)
                {
                    typeDef = conversion.Method.DeclaringType.GetDefinitionOrArrayType();
                }
                else
                {
                    typeDef = conversionType.GetDefinitionOrArrayType();
                }
                var nativeOverloads = Sk.UseNativeOperatorOverloads(typeDef);
                if (nativeOverloads)
                {
                    return(Visit(input));
                }
                ////TODO: Check if OmitCalls is found on conversion method, if so - return Visit(input);
                //if (Sk.IsOmitCalls(conversion.Method))
                //{
                //}
                var fake  = conversion.Method.InvokeMethod(null, input);
                var node2 = Visit(fake);
                return(node2);
            }
            else if (conversion.IsTryCast || conversion.IsExplicit)
            {
                if (ForceIntegers && conversion.IsNumericConversion && IsInteger(conversionType))
                {
                    return(ForceInteger(Visit(input)));
                }

                //Skip enum casts
                if ((conversionType.Kind == TypeKind.Enum && IsInteger(input.Type)) || (input.Type.Kind == TypeKind.Enum && IsInteger(conversionType)))
                {
                    return(Visit(input));
                }

                var omitCasts = Sk.OmitCasts(conversionType);
                if (omitCasts)
                {
                    return(Visit(input));
                }
                if (Sk.NativeCasts(conversionType))
                {
                    var exp2  = VisitExpression(input);
                    var type2 = SkJs.EntityTypeRefToMember(conversionType);
                    if (conversion.IsTryCast)
                    {
                        var node2 = exp2.InstanceOf(type2).Conditional(exp2, Js.Null());
                        return(node2);
                    }
                    else
                    {
                        var node2 = Js.Conditional(exp2.InstanceOf(type2).Or(exp2.Equal(Js.Null())), exp2,
                                                   Js.Parentheses(Js.Function().Add(Js.ThrowNewError("InvalidCastException")).Invoke()));
                        //var node2 = Js.Parentheses(Js.Function().AddStatements(
                        //Js.If(exp2.InstanceOf(type2).Or(exp2.Equal(Js.Null())), Js.Return(exp2)),
                        //Js.ThrowNewError("InvalidCastException"))).Member("call").Invoke(Js.This());
                        return(node2);
                    }
                }
                else
                {
                    var cast  = conversion.IsTryCast ? "As" : "Cast";
                    var node2 = Js.Member(cast).Invoke(VisitExpression(input), SkJs.EntityTypeRefToMember(conversionType));
                    return(node2);
                }
            }
            return(Visit(input));
        }