CoerceT() static private method

static private CoerceT ( Object value, Type type ) : Object
value Object
type System.Type
return Object
        public static bool JScriptInstanceof(Object v1, Object v2)
        {
            if (v2 is ClassScope)
            {
                return(((ClassScope)v2).HasInstance(v1));
            }
            if (v2 is ScriptFunction)
            {
                return(((ScriptFunction)v2).HasInstance(v1));
            }
            if (v1 == null)
            {
                return(false);
            }
            if (v2 is Type)
            {
                Type t1 = v1.GetType();
                if (v1 is IConvertible)
                {
                    try{
                        Convert.CoerceT(v1, (Type)v2);
                        return(true);
                    }catch (JScriptException) {
                        return(false);
                    }
                }
                else
                {
                    return(((Type)v2).IsAssignableFrom(t1));
                }
            }


            throw new JScriptException(JSError.NeedType);
        }
Exemplo n.º 2
0
        internal CustomAttributeBuilder GetCustomAttribute()
        {
            ConstructorInfo c = (ConstructorInfo)((Binding)this.ctor).member;

            ParameterInfo[] pars = c.GetParameters();
            int             pn   = pars.Length;

            if (c is JSConstructor)
            {
                c = ((JSConstructor)c).GetConstructorInfo(compilerGlobals);
            }
            this.ConvertClassScopesAndEnumWrappers(this.positionalArgValues);
            this.ConvertClassScopesAndEnumWrappers(this.namedArgPropertyValues);
            this.ConvertClassScopesAndEnumWrappers(this.namedArgFieldValues);
            this.ConvertFieldAndPropertyInfos(this.namedArgProperties);
            this.ConvertFieldAndPropertyInfos(this.namedArgFields);
            for (int i = positionalArgValues.Count; i < pn; i++)
            {
                positionalArgValues.Add(Convert.CoerceT(null, pars[i].ParameterType));
            }
            Object[]       pArgVals   = new Object[pn]; positionalArgValues.CopyTo(0, pArgVals, 0, pn);
            PropertyInfo[] nArgProps  = new PropertyInfo[namedArgProperties.Count]; namedArgProperties.CopyTo(nArgProps);
            Object[]       nArgPVals  = new Object[namedArgPropertyValues.Count]; namedArgPropertyValues.CopyTo(nArgPVals);
            FieldInfo[]    nArgFields = new FieldInfo[namedArgFields.Count]; namedArgFields.CopyTo(nArgFields);
            Object[]       nArgFVals  = new Object[namedArgFieldValues.Count]; namedArgFieldValues.CopyTo(nArgFVals);
            return(new CustomAttributeBuilder(c, pArgVals, nArgProps, nArgPVals, nArgFields, nArgFVals));
        }
Exemplo n.º 3
0
        internal override void TranslateToIL(ILGenerator il, Type rtype)
        {
            if (rtype == Typeob.Void)
            {
                return;
            }
            Object val = this.value;

            if (val is EnumWrapper && rtype != Typeob.Object && rtype != Typeob.String)
            {
                val = ((EnumWrapper)val).value;
            }
            if (this.isNumericLiteral && (rtype == Typeob.Decimal || rtype == Typeob.Int64 || rtype == Typeob.UInt64 || rtype == Typeob.Single))
            {
                val = this.context.GetCode();
            }
            if (!(rtype is TypeBuilder))
            {
                try{
                    val = Convert.CoerceT(val, rtype);
                }catch (Exception) {
                }
            }
            this.TranslateToIL(il, val, rtype);
        }
        internal virtual Array ToNativeArray(Type elementType)
        {
            uint n = this.len;

            if (n > Int32.MaxValue)
            {
                throw new JScriptException(JSError.OutOfMemory);
            }
            if (elementType == null)
            {
                elementType = typeof(Object);
            }
            uint m = this.denseArrayLength;

            if (m > n)
            {
                m = n;
            }
            Array result = Array.CreateInstance(elementType, (int)n);

            for (int i = 0; i < m; i++)
            {
                result.SetValue(Convert.CoerceT(this.denseArray[i], elementType), i);
            }
            for (int i = (int)m; i < n; i++)
            {
                result.SetValue(Convert.CoerceT(this.GetValueAtIndex((uint)i), elementType), i);
            }
            return(result);
        }
Exemplo n.º 5
0
 internal bool IsAssignableTo(Type rtype)
 {
     try{
         Convert.CoerceT(this.value, rtype, false);
         return(true);
     }catch {
         return(false);
     }
 }
Exemplo n.º 6
0
        internal void CoerceToBaseType(Type bt, Context errCtx)
        {
            Object val  = 0;
            AST    pval = ((AST)this.value).PartiallyEvaluate();

            if (pval is ConstantWrapper)
            {
                val = ((ConstantWrapper)pval).Evaluate();
            }
            else
            {
                pval.context.HandleError(JSError.NotConst);
            }
            try{
                this.value = Convert.CoerceT(val, bt);
            }catch (Exception) {
                errCtx.HandleError(JSError.TypeMismatch);
                this.value = Convert.CoerceT(0, bt);
            }
        }
Exemplo n.º 7
0
        internal override Object Evaluate()
        {
            ConstructorInfo c = (ConstructorInfo)((Binding)this.ctor).member;

            ParameterInfo[] pars = c.GetParameters();
            int             pn   = pars.Length;

            for (int i = positionalArgValues.Count; i < pn; i++)
            {
                positionalArgValues.Add(Convert.CoerceT(null, pars[i].ParameterType));
            }
            Object[] pArgVals = new Object[pn]; positionalArgValues.CopyTo(0, pArgVals, 0, pn);
            Object   ca       = c.Invoke(BindingFlags.ExactBinding, null, pArgVals, null);

            for (int i = 0, n = this.namedArgProperties.Count; i < n; i++)
            {
                JSProperty prop = this.namedArgProperties[i] as JSProperty;
                if (prop != null)
                {
                    prop.SetValue(ca, Convert.Coerce(this.namedArgPropertyValues[i], prop.PropertyIR()), null);
                }
                else
                {
                    ((PropertyInfo)this.namedArgProperties[i]).SetValue(ca, this.namedArgPropertyValues[i], null);
                }
            }
            for (int i = 0, n = this.namedArgFields.Count; i < n; i++)
            {
                JSVariableField field = this.namedArgFields[i] as JSVariableField;
                if (field != null)
                {
                    field.SetValue(ca, Convert.Coerce(this.namedArgFieldValues[i], field.GetInferredType(null)));
                }
                else
                {
                    ((FieldInfo)this.namedArgFields[i]).SetValue(ca, this.namedArgFieldValues[i]);
                }
            }
            return(ca);
        }
Exemplo n.º 8
0
 // Determine if an object is an instance of a class.
 public static bool JScriptInstanceof(Object v1, Object v2)
 {
     if (v2 is ScriptFunction)
     {
         // Check for function instances.
         return(((ScriptFunction)v2).HasInstance(v1));
     }
     else if (v1 == null)
     {
         // Null is never an instance of any type.
         return(false);
     }
     else if (v2 is Type)
     {
         // Try to coerce to the destination type.
         Type type = (Type)v2;
                         #if !ECMA_COMPAT
         if (v1 is IConvertible)
         {
             try
             {
                 Convert.CoerceT(v1, type, false);
             }
             catch (JScriptException)
             {
                 return(false);
             }
         }
                         #endif
         return(type.IsAssignableFrom(v1.GetType()));
     }
     else
     {
         throw new JScriptException(JSError.NeedType);
     }
 }
Exemplo n.º 9
0
        internal override void SetValueAtIndex(uint index, Object val)
        {
            Type arrayType = this.value.GetType();

            checked { this.value.SetValue(Convert.CoerceT(val, arrayType.GetElementType()), (int)index); }
        }
Exemplo n.º 10
0
 internal override AST PartiallyEvaluate()
 {
     if (this.alreadyPartiallyEvaluated)
     {
         return(this);
     }
     this.alreadyPartiallyEvaluated = true;
     if (this.inBrackets && this.AllParamsAreMissing())
     {
         if (this.isConstructor)
         {
             this.args.context.HandleError(JSError.TypeMismatch);
         }
         IReflect ir = ((TypeExpression)(new TypeExpression(this.func)).PartiallyEvaluate()).ToIReflect();
         return(new ConstantWrapper(new TypedArray(ir, this.args.count + 1), this.context));
     }
     this.func = this.func.PartiallyEvaluateAsCallable();
     this.args = (ASTList)this.args.PartiallyEvaluate();
     IReflect[] argIRs = this.ArgIRs();
     this.func.ResolveCall(this.args, argIRs, this.isConstructor, this.inBrackets);
     if (!this.isConstructor && !this.inBrackets && this.func is Binding && this.args.count == 1)
     {
         Binding b = (Binding)this.func;
         if (b.member is Type)
         {
             Type            t    = (Type)b.member;
             ConstantWrapper arg0 = this.args[0] as ConstantWrapper;
             if (arg0 != null)
             {
                 try{
                     if (arg0.value == null || arg0.value is DBNull)
                     {
                         return(this);
                     }
                     else if (arg0.isNumericLiteral && (t == Typeob.Decimal || t == Typeob.Int64 || t == Typeob.UInt64 || t == Typeob.Single))
                     {
                         return(new ConstantWrapper(Convert.CoerceT(arg0.context.GetCode(), t, true), this.context));
                     }
                     else
                     {
                         return(new ConstantWrapper(Convert.CoerceT(arg0.Evaluate(), t, true), this.context));
                     }
                 }catch {
                     arg0.context.HandleError(JSError.TypeMismatch);
                 }
             }
             else
             {
                 if (!Binding.AssignmentCompatible(t, this.args[0], argIRs[0], false))
                 {
                     this.args[0].context.HandleError(JSError.ImpossibleConversion);
                 }
             }
         }
         else if (b.member is JSVariableField)
         {
             JSVariableField field = (JSVariableField)b.member;
             if (field.IsLiteral)
             {
                 if (field.value is ClassScope)
                 {
                     ClassScope csc = (ClassScope)field.value;
                     IReflect   ut  = csc.GetUnderlyingTypeIfEnum();
                     if (ut != null)
                     {
                         if (!Convert.IsPromotableTo(argIRs[0], ut) && !Convert.IsPromotableTo(ut, argIRs[0]) && (argIRs[0] != Typeob.String || ut == csc))
                         {
                             this.args[0].context.HandleError(JSError.ImpossibleConversion);
                         }
                     }
                     else
                     {
                         if (!Convert.IsPromotableTo(argIRs[0], csc) && !Convert.IsPromotableTo(csc, argIRs[0]))
                         {
                             this.args[0].context.HandleError(JSError.ImpossibleConversion);
                         }
                     }
                 }
                 else if (field.value is TypedArray)
                 {
                     TypedArray ta = (TypedArray)field.value;
                     if (!Convert.IsPromotableTo(argIRs[0], ta) && !Convert.IsPromotableTo(ta, argIRs[0]))
                     {
                         this.args[0].context.HandleError(JSError.ImpossibleConversion);
                     }
                 }
             }
         }
     }
     return(this);
 }
Exemplo n.º 11
0
        internal override AST PartiallyEvaluate()
        {
            this.ctor = this.ctor.PartiallyEvaluateAsCallable();

            //first weed out assignment expressions and use them as property initializers
            ASTList positionalArgs = new ASTList(this.args.context);
            ASTList namedArgs      = new ASTList(this.args.context);

            for (int i = 0, m = this.args.count; i < m; i++)
            {
                AST    arg    = this.args[i];
                Assign assign = arg as Assign;
                if (assign != null)
                {
                    assign.rhside = assign.rhside.PartiallyEvaluate();
                    namedArgs.Append(assign);
                }
                else
                {
                    positionalArgs.Append(arg.PartiallyEvaluate());
                }
            }

            int n = positionalArgs.count;

            IReflect[] argIRs = new IReflect[n];
            for (int i = 0; i < n; i++)
            {
                AST arg = positionalArgs[i];
                // only accept ConstantWrappers
                if (arg is ConstantWrapper)
                {
                    Object argument = arg.Evaluate();
                    if ((argIRs[i] = CustomAttribute.TypeOfArgument(argument)) != null)
                    {
                        this.positionalArgValues.Add(argument);
                        continue;
                    }
                }
                else if (arg is ArrayLiteral && ((ArrayLiteral)arg).IsOkToUseInCustomAttribute())
                {
                    argIRs[i] = Typeob.ArrayObject;
                    this.positionalArgValues.Add(arg.Evaluate());
                    continue;
                }
                arg.context.HandleError(JSError.InvalidCustomAttributeArgument);
                return(null); // the custom attribute is not good and it will be ignored
            }

            //Get the custom attribute and the appropriate constructor (under the covers)
            this.type = this.ctor.ResolveCustomAttribute(positionalArgs, argIRs, this.target);
            if (this.type == null)
            {
                return(null);
            }
            if (Convert.IsPromotableTo((IReflect)this.type, typeof(CodeAccessSecurityAttribute)))
            {
                this.context.HandleError(JSError.CannotUseStaticSecurityAttribute);
                return(null);
            }


            //Coerce the positional arguments to the right type and supply default values for optional parameters
            ConstructorInfo c = (ConstructorInfo)((Binding)this.ctor).member;

            ParameterInfo[] parameters = c.GetParameters();
            int             j          = 0;
            int             len        = this.positionalArgValues.Count;

            foreach (ParameterInfo p in parameters)
            {
                IReflect ir = p is ParameterDeclaration ? ((ParameterDeclaration)p).ParameterIReflect : p.ParameterType;
                if (j < len)
                {
                    Object value = this.positionalArgValues[j];
                    this.positionalArgValues[j] = Convert.Coerce(value, ir, value is ArrayObject);
                    j++;
                }
                else
                {
                    Object value;
                    if (p.DefaultValue == System.Convert.DBNull)
                    {
                        value = Convert.Coerce(null, ir);
                    }
                    else
                    {
                        value = p.DefaultValue;
                    }
                    this.positionalArgValues.Add(value);
                }
            }

            //Check validity of property/field initializers
            for (int i = 0, m = namedArgs.count; i < m; i++)
            {
                Assign assign = (Assign)namedArgs[i];
                if (assign.lhside is Lookup &&
                    (assign.rhside is ConstantWrapper ||
                     (assign.rhside is ArrayLiteral && ((ArrayLiteral)assign.rhside).IsOkToUseInCustomAttribute())))
                {
                    Object   value   = assign.rhside.Evaluate();
                    IReflect argType = null;
                    if (value is ArrayObject || ((argType = CustomAttribute.TypeOfArgument(value)) != null && argType != Typeob.Object))
                    {
                        String        name    = ((Lookup)assign.lhside).Name;
                        MemberInfo [] members = ((IReflect)this.type).GetMember(name, BindingFlags.Public | BindingFlags.Instance);
                        if (members == null || members.Length == 0)
                        {
                            assign.context.HandleError(JSError.NoSuchMember);
                            return(null);
                        }
                        if (members.Length == 1)
                        {
                            MemberInfo member = members[0];
                            if (member is FieldInfo)
                            {
                                FieldInfo fieldInfo = (FieldInfo)member;
                                if (!fieldInfo.IsLiteral && !fieldInfo.IsInitOnly)
                                {
                                    try{
                                        IReflect ir = fieldInfo is JSVariableField ? ((JSVariableField)fieldInfo).GetInferredType(null) : fieldInfo.FieldType;
                                        value = Convert.Coerce(value, ir, value is ArrayObject);
                                        this.namedArgFields.Add(member);
                                        this.namedArgFieldValues.Add(value);
                                        continue;
                                    }catch (JScriptException) {
                                        assign.rhside.context.HandleError(JSError.TypeMismatch);
                                        return(null); // the custom attribute is not good and it will be ignored
                                    }
                                }
                            }
                            else if (member is PropertyInfo)
                            {
                                PropertyInfo propertyInfo  = (PropertyInfo)member;
                                MethodInfo   setMethodInfo = JSProperty.GetSetMethod(propertyInfo, false);
                                if (setMethodInfo != null)
                                {
                                    ParameterInfo [] paramInfo = setMethodInfo.GetParameters();
                                    if (paramInfo != null && paramInfo.Length == 1)
                                    {
                                        try{
                                            IReflect ir = paramInfo[0] is ParameterDeclaration ? ((ParameterDeclaration)paramInfo[0]).ParameterIReflect : paramInfo[0].ParameterType;
                                            value = Convert.Coerce(value, ir, value is ArrayObject);
                                            this.namedArgProperties.Add(member);
                                            this.namedArgPropertyValues.Add(value);
                                        }catch (JScriptException) {
                                            assign.rhside.context.HandleError(JSError.TypeMismatch);
                                            return(null); // the custom attribute is not good and it will be ignored
                                        }
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
                assign.context.HandleError(JSError.InvalidCustomAttributeArgument);
                return(null);
            }

            if (!this.CheckIfTargetOK(this.type))
            {
                return(null); //Ignore attribute
            }
            //Consume and discard assembly name attributes
            try{
                Type ty = this.type as Type;
                if (ty != null && this.target is AssemblyCustomAttributeList)
                {
                    if (ty.FullName == "System.Reflection.AssemblyAlgorithmIdAttribute")
                    {
                        if (this.positionalArgValues.Count > 0)
                        {
                            this.Engine.Globals.assemblyHashAlgorithm = (AssemblyHashAlgorithm)Convert.CoerceT(this.positionalArgValues[0], typeof(AssemblyHashAlgorithm));
                        }
                        return(null);
                    }
                    if (ty.FullName == "System.Reflection.AssemblyCultureAttribute")
                    {
                        if (this.positionalArgValues.Count > 0)
                        {
                            String cultureId = Convert.ToString(this.positionalArgValues[0]);
                            if (this.Engine.PEFileKind != PEFileKinds.Dll && cultureId.Length > 0)
                            {
                                this.context.HandleError(JSError.ExecutablesCannotBeLocalized);
                                return(null);
                            }
                            this.Engine.Globals.assemblyCulture = new CultureInfo(cultureId);
                        }
                        return(null);
                    }
                    if (ty.FullName == "System.Reflection.AssemblyDelaySignAttribute")
                    {
                        if (this.positionalArgValues.Count > 0)
                        {
                            this.Engine.Globals.assemblyDelaySign = Convert.ToBoolean(this.positionalArgValues[0], false);
                        }
                        return(null);
                    }
                    if (ty.FullName == "System.Reflection.AssemblyFlagsAttribute")
                    {
                        if (this.positionalArgValues.Count > 0)
                        {
                            this.Engine.Globals.assemblyFlags = (AssemblyFlags)(uint)Convert.CoerceT(this.positionalArgValues[0], typeof(uint));
                        }
                        return(null);
                    }
                    if (ty.FullName == "System.Reflection.AssemblyKeyFileAttribute")
                    {
                        if (this.positionalArgValues.Count > 0)
                        {
                            this.Engine.Globals.assemblyKeyFileName = Convert.ToString(this.positionalArgValues[0]);
                            if (this.Engine.Globals.assemblyKeyFileName != null && this.Engine.Globals.assemblyKeyFileName.Length == 0)
                            {
                                this.Engine.Globals.assemblyKeyFileName = null;
                            }
                        }
                        return(null);
                    }
                    if (ty.FullName == "System.Reflection.AssemblyKeyNameAttribute")
                    {
                        if (this.positionalArgValues.Count > 0)
                        {
                            this.Engine.Globals.assemblyKeyName = Convert.ToString(this.positionalArgValues[0]);
                            if (this.Engine.Globals.assemblyKeyName != null && this.Engine.Globals.assemblyKeyName.Length == 0)
                            {
                                this.Engine.Globals.assemblyKeyName = null;
                            }
                        }
                        return(null);
                    }
                    if (ty.FullName == "System.Reflection.AssemblyVersionAttribute")
                    {
                        if (this.positionalArgValues.Count > 0)
                        {
                            this.Engine.Globals.assemblyVersion = this.ParseVersion(Convert.ToString(this.positionalArgValues[0]));
                        }
                        return(null);
                    }
                    if (ty.FullName == "System.CLSCompliantAttribute")
                    {
                        this.Engine.isCLSCompliant = this.args == null || this.args.count == 0 || Convert.ToBoolean(this.positionalArgValues[0], false);
                        return(this);
                    }
                }
            }catch (ArgumentException) {
                this.context.HandleError(JSError.InvalidCall);
            }

            return(this);
        }