Exemple #1
0
        internal static void Init(IScriptable scope, bool zealed)
        {
            BuiltinNumber obj = new BuiltinNumber(0.0);

            obj.ExportAsJSClass(MAX_PROTOTYPE_ID, scope, zealed
                                , ScriptableObject.DONTENUM | ScriptableObject.READONLY | ScriptableObject.PERMANENT);
        }
Exemple #2
0
        public override object ExecIdCall(IdFunctionObject f, Context cx, IScriptable scope, IScriptable thisObj, object [] args)
        {
            if (!f.HasTag(NUMBER_TAG))
            {
                return(base.ExecIdCall(f, cx, scope, thisObj, args));
            }
            int id = f.MethodId;

            if (id == Id_constructor)
            {
                double val = (args.Length >= 1) ? ScriptConvert.ToNumber(args [0]) : 0.0;
                if (thisObj == null)
                {
                    // new Number(val) creates a new Number object.
                    return(new BuiltinNumber(val));
                }
                // Number(val) converts val to a number value.
                return(val);
            }

            // The rest of Number.prototype methods require thisObj to be Number
            BuiltinNumber nativeNumber = (thisObj as BuiltinNumber);

            if (nativeNumber == null)
            {
                throw IncompatibleCallError(f);
            }
            double value = nativeNumber.doubleValue;

            switch (id)
            {
            case Id_toLocaleString:
            case Id_toString:
                return(ImplToString(value, args));

            case Id_toSource:
                return("(new Number(" + ScriptConvert.ToString(value) + "))");


            case Id_valueOf:
                return(value);


            case Id_toFixed:
                return(ImplToFixed(value, args));


            case Id_toExponential:
                return(ImplToExponential(value, args));


            case Id_toPrecision:
                return(ImplToPrecision(value, args));

            default:
                throw new ArgumentException(Convert.ToString(id));
            }
        }
 internal static void Init(IScriptable scope, bool zealed)
 {
     BuiltinNumber obj = new BuiltinNumber (0.0);
     obj.ExportAsJSClass (MAX_PROTOTYPE_ID, scope, zealed
         , ScriptableObject.DONTENUM | ScriptableObject.READONLY | ScriptableObject.PERMANENT);
 }
        public override object ExecIdCall(IdFunctionObject f, Context cx, IScriptable scope, IScriptable thisObj, object [] args)
        {
            if (!f.HasTag(NUMBER_TAG))
            {
                return(base.ExecIdCall(f, cx, scope, thisObj, args));
            }
            int id = f.MethodId;

            if (id == Id_constructor)
            {
                double val = (args.Length >= 1) ? ScriptConvert.ToNumber(args [0]) : 0.0;
                if (thisObj == null)
                {
                    // new Number(val) creates a new Number object.
                    return(new BuiltinNumber(val));
                }
                // Number(val) converts val to a number value.
                return(val);
            }

            // The rest of Number.prototype methods require thisObj to be Number
            BuiltinNumber nativeNumber = (thisObj as BuiltinNumber);

            if (nativeNumber == null)
            {
                throw IncompatibleCallError(f);
            }
            double value = nativeNumber.doubleValue;

            int toBase = 0;

            switch (id)
            {
            case Id_toString:
                toBase = (args.Length == 0) ? 10 : ScriptConvert.ToInt32(args [0]);
                return(ScriptConvert.ToString(value, toBase));

            case Id_toLocaleString: {
                // toLocaleString is just an alias for toString for now
                toBase = (args.Length == 0) ? 10 : ScriptConvert.ToInt32(args [0]);
                return(ScriptConvert.ToString(value, toBase));
            }


            case Id_toSource:
                return("(new Number(" + ScriptConvert.ToString(value) + "))");


            case Id_valueOf:
                return(value);


            case Id_toFixed:
                return(num_to(value, args, DTOSTR_FIXED, DTOSTR_FIXED, -20, 0));


            case Id_toExponential:
                return(num_to(value, args, DTOSTR_STANDARD_EXPONENTIAL, DTOSTR_EXPONENTIAL, 0, 1));


            case Id_toPrecision: {
                if (args.Length < 0 || args [0] == Undefined.Value)
                {
                    return(ScriptConvert.ToString(value));
                }
                int precision = ScriptConvert.ToInt32(args [0]);
                if (precision < 0 || precision > MAX_PRECISION)
                {
                    throw ScriptRuntime.ConstructError("RangeError",
                                                       ScriptRuntime.GetMessage("msg.bad.precision", ScriptConvert.ToString(args [0])));
                }
                return(value.ToString(GetFormatString(precision)));
            }


            default:
                throw new ArgumentException(Convert.ToString(id));
            }
        }