IsPrimitiveNumericTypeCode() static private method

static private IsPrimitiveNumericTypeCode ( TypeCode tc ) : bool
tc TypeCode
return bool
コード例 #1
0
        internal override IReflect InferType(JSField inference_target)
        {
            Debug.Assert(Globals.TypeRefs.InReferenceContext(this.type1));
            Debug.Assert(Globals.TypeRefs.InReferenceContext(this.type2));
            MethodInfo oper;

            if (this.type1 == null || inference_target != null)
            {
                oper = this.GetOperator(this.operand1.InferType(inference_target), this.operand2.InferType(inference_target));
            }
            else
            {
                oper = this.GetOperator(this.type1, this.type2);
            }
            if (oper != null)
            {
                this.metaData = oper;
                return(oper.ReturnType);
            }
            if (this.type1 == Typeob.Char && this.operatorTok == JSToken.Minus)
            {
                TypeCode t2 = Type.GetTypeCode(this.type2);
                if (Convert.IsPrimitiveNumericTypeCode(t2) || t2 == TypeCode.Boolean)
                {
                    return(Typeob.Char);
                }
                else if (t2 == TypeCode.Char)
                {
                    return(Typeob.Int32);
                }
            }
            if (Convert.IsPrimitiveNumericType(this.type1))
            {
                if (Convert.IsPromotableTo(this.type2, this.type1) || ((this.operand2 is ConstantWrapper) && ((ConstantWrapper)this.operand2).IsAssignableTo(this.type1)))
                {
                    return(this.type1);
                }
                else if (Convert.IsPrimitiveNumericType(this.type1) && Convert.IsPrimitiveNumericTypeFitForDouble(this.type2))
                {
                    return(Typeob.Double);
                }
            }
            return(Typeob.Object);
        }
コード例 #2
0
        public static Object DoOp(Object v1, Object v2)
        {
            IConvertible ic1 = Convert.GetIConvertible(v1);
            IConvertible ic2 = Convert.GetIConvertible(v2);

            v1 = Convert.ToPrimitive(v1, PreferredType.Either, ref ic1);
            v2 = Convert.ToPrimitive(v2, PreferredType.Either, ref ic2);
            TypeCode t1 = Convert.GetTypeCode(v1, ic1);
            TypeCode t2 = Convert.GetTypeCode(v2, ic2);

            if (t1 == TypeCode.String)
            {
                if (v1 is ConcatString)
                {
                    return(new ConcatString((ConcatString)v1, Convert.ToString(v2, ic2)));
                }
                else
                {
                    return(new ConcatString(ic1.ToString(null), Convert.ToString(v2, ic2)));
                }
            }
            else if (t2 == TypeCode.String)
            {
                return(Convert.ToString(v1, ic1) + ic2.ToString(null));
            }
            else if (t1 == TypeCode.Char && t2 == TypeCode.Char)
            {
                return(ic1.ToString(null) + ic2.ToString(null));
            }
            else if ((t1 == TypeCode.Char && (Convert.IsPrimitiveNumericTypeCode(t2) || t2 == TypeCode.Boolean)) ||
                     (t2 == TypeCode.Char && (Convert.IsPrimitiveNumericTypeCode(t1) || t1 == TypeCode.Boolean)))
            {
                return((char)(int)Runtime.DoubleToInt64(Convert.ToNumber(v1, ic1) + Convert.ToNumber(v2, ic2)));
            }
            else
            {
                return(Convert.ToNumber(v1, ic1) + Convert.ToNumber(v2, ic2));
            }
        }
コード例 #3
0
ファイル: numericbinary.cs プロジェクト: SSCLI/sscli_20021101
        internal override IReflect InferType(JSField inference_target)
        {
            MethodInfo oper;

            if (this.type1 == null || inference_target != null)
            {
                oper = this.GetOperator(this.operand1.InferType(inference_target), this.operand2.InferType(inference_target));
            }
            else
            {
                oper = this.GetOperator(this.type1, this.type2);
            }
            if (oper != null)
            {
                this.metaData = oper;
                return(oper.ReturnType);
            }
            if (this.type1 == Typeob.Char && this.operatorTok == JSToken.Minus)
            {
                TypeCode t2 = Type.GetTypeCode(this.type2);
                if (Convert.IsPrimitiveNumericTypeCode(t2) || t2 == TypeCode.Boolean)
                {
                    return(Typeob.Char);
                }
                else if (t2 == TypeCode.Char)
                {
                    return(Typeob.Int32);
                }
            }
            if ((Convert.IsPrimitiveNumericTypeFitForDouble(this.type1) || Typeob.JSObject.IsAssignableFrom(this.type1)) &&
                (Convert.IsPrimitiveNumericTypeFitForDouble(this.type2) || Typeob.JSObject.IsAssignableFrom(this.type2)))
            {
                return(Typeob.Double);
            }
            else
            {
                return(Typeob.Object);
            }
        }
コード例 #4
0
ファイル: scriptobject.cs プロジェクト: SSCLI/sscli_20021101
 public Object this[params Object[] pars] {
     get{
         int n = pars.Length;
         if (n == 0)
         {
             if (this is ScriptFunction || this == null)
             {
                 throw new JScriptException(JSError.FunctionExpected);
             }
             else
             {
                 throw new JScriptException(JSError.TooFewParameters);
             }
         }
         if (this == null)
         {
             throw new JScriptException(JSError.ObjectExpected);
         }
         Object val = pars[n - 1];
         if (val is Int32)
         {
             return(this[(int)val]);
         }
         IConvertible ic = Convert.GetIConvertible(val);
         if (ic != null && Convert.IsPrimitiveNumericTypeCode(ic.GetTypeCode()))
         {
             double d = ic.ToDouble(null);
             if (d >= 0 && d <= Int32.MaxValue && d == System.Math.Round(d))
             {
                 return(this[(int)d]);
             }
         }
         return(this[Convert.ToString(val)]);
     }
     set{
         int n = pars.Length;
         if (n == 0)
         {
             if (this == null)
             {
                 throw new JScriptException(JSError.FunctionExpected);
             }
             else if (this is ScriptFunction)
             {
                 throw new JScriptException(JSError.CannotAssignToFunctionResult);
             }
             else
             {
                 throw new JScriptException(JSError.TooFewParameters);
             }
         }
         if (this == null)
         {
             throw new JScriptException(JSError.ObjectExpected);
         }
         Object val = pars[n - 1];
         if (val is Int32)
         {
             this[(int)val] = value;
             return;
         }
         IConvertible ic = Convert.GetIConvertible(val);
         if (ic != null && Convert.IsPrimitiveNumericTypeCode(ic.GetTypeCode()))
         {
             double d = ic.ToDouble(null);
             if (d >= 0 && d <= Int32.MaxValue && d == System.Math.Round(d))
             {
                 this[(int)d] = value;
                 return;
             }
         }
         this[Convert.ToString(val)] = value;
     }
 }
コード例 #5
0
ファイル: scriptobject.cs プロジェクト: SSCLI/sscli_20021101
        public virtual Object InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target,
                                           Object[] args, ParameterModifier[] modifiers, CultureInfo locale, String[] namedParameters)
        {
            if (target != this)
            {
                throw new TargetException();
            }

            bool preferredTypeSpecified = name.StartsWith("< JScript-");
            bool dispid0 = (name == null || name == String.Empty || name.Equals("[DISPID=0]") || preferredTypeSpecified);

            if ((invokeAttr & BindingFlags.CreateInstance) != 0)
            {
                if ((invokeAttr & (BindingFlags.InvokeMethod | BindingFlags.GetField | BindingFlags.GetProperty |
                                   BindingFlags.SetField | BindingFlags.SetProperty | BindingFlags.PutDispProperty)) != 0)
                {
                    throw new ArgumentException();
                }
                //js: x = new foo() --> dispid0, create
                if (dispid0)
                {
                    throw new MissingMethodException();
                }

                //js: x = new foo.name() --> dispid0, create
                LateBinding lb = new LateBinding(name, this);
                return(lb.Call(binder, args, modifiers, locale, namedParameters, true, false, this.engine));
            }

            //According to docs, name == null is only valid for CreateInstance
            if (name == null)
            {
                throw new ArgumentException();
            }

            if ((invokeAttr & (BindingFlags.InvokeMethod | BindingFlags.GetField | BindingFlags.GetProperty)) != 0)
            {
                if ((invokeAttr & (BindingFlags.SetField | BindingFlags.SetProperty | BindingFlags.PutDispProperty)) != 0)
                {
                    throw new ArgumentException();
                }

                if (dispid0)
                {
                    //All callable functions inherit from ScriptFunction which overrides this method to handle
                    //the InvokeMethod case.

                    //js,vbs: x = foo() --> dispid0, invoke
                    if ((invokeAttr & (BindingFlags.GetField | BindingFlags.GetProperty)) == 0)
                    {
                        throw new MissingMethodException();
                    }

                    //js: x = foo --> dispid0, propget; vbs: x = foo --> dispid0, invoke|propget
                    if (args == null || args.Length == 0)
                    {
                        if (this is JSObject || this is GlobalScope || this is ClassScope)
                        {
                            PreferredType preferredType = PreferredType.Either;
                            if (preferredTypeSpecified)
                            {
                                if (name.StartsWith("< JScript-Number"))
                                {
                                    preferredType = PreferredType.Number;
                                }
                                else if (name.StartsWith("< JScript-String"))
                                {
                                    preferredType = PreferredType.String;
                                }
                                else if (name.StartsWith("< JScript-LocaleString"))
                                {
                                    preferredType = PreferredType.LocaleString;
                                }
                            }
                            return(this.GetDefaultValue(preferredType));
                        }
                        throw new MissingFieldException();
                    }

                    //We support indexed properties with exactly one index on all script objects.

                    //js,vbs: x = foo(1,2) --> dispid0, invoke|propget
                    if (args.Length > 1)
                    {
                        throw new ArgumentException();
                    }

                    //js,vbs: x = foo(1) --> dispid0, invoke|propget
                    Object val = args[0];
                    if (val is Int32)
                    {
                        return(this[(int)val]);
                    }
                    IConvertible ic = Convert.GetIConvertible(val);
                    if (ic != null && Convert.IsPrimitiveNumericTypeCode(ic.GetTypeCode()))
                    {
                        double d = ic.ToDouble(null);
                        if (d >= 0 && d <= Int32.MaxValue && d == System.Math.Round(d))
                        {
                            return(this[(int)d]);
                        }
                    }
                    return(this[Convert.ToString(val)]);
                }

                //If no arguments are supplied, prefer GetXXXX rather than Invoke.
                //js: x = foo.bar --> name="bar", propget; vbs: x = foo.bar --> name="bar", propget|invoke
                if ((args == null || args.Length == 0) && (invokeAttr & (BindingFlags.GetField | BindingFlags.GetProperty)) != 0)
                {
                    Object member = this.GetMemberValue(name);
                    if (member != Missing.Value)
                    {
                        return(member);
                    }

                    //js: x = foo.bar --> name="bar", propget
                    if ((invokeAttr & BindingFlags.InvokeMethod) == 0)
                    {
                        throw new MissingFieldException();
                    }
                }

                //Use LateBinding to call because arguments have been supplied.
                //vbs: x = foo.bar --> name="bar", propget|invoke
                //js,vbs: x = foo.bar() --> name="bar", invoke
                //js,vbs: x = foo.bar(1) --> name="bar", invoke|propget
                LateBinding lb = new LateBinding(name, this);
                return(lb.Call(binder, args, modifiers, locale, namedParameters, false, false, this.engine));
            }

            if ((invokeAttr & (BindingFlags.SetField | BindingFlags.SetProperty | BindingFlags.PutDispProperty)) != 0)
            {
                if (dispid0)
                {
                    if (args == null || args.Length != 2)
                    {
                        throw new ArgumentException();
                    }
                    Object val = args[0];
                    if (val is Int32)
                    {
                        this[(int)val] = args[1];
                        return(null);
                    }
                    IConvertible ic = Convert.GetIConvertible(val);
                    if (ic != null && Convert.IsPrimitiveNumericTypeCode(ic.GetTypeCode()))
                    {
                        double d = ic.ToDouble(null);
                        if (d >= 0 && d <= Int32.MaxValue && d == System.Math.Round(d))
                        {
                            this[(int)d] = args[1];
                            return(null);
                        }
                    }
                    this[Convert.ToString(val)] = args[1];
                    return(null);
                }
                if (args == null || args.Length > 1)
                {
                    throw new ArgumentException();
                }
                this.SetMemberValue(name, args[0]);
                return(null);
            }

            throw new ArgumentException();
        }