示例#1
0
        protected internal override void SetInstanceIdValue(int id, System.Object value_Renamed)
        {
            switch (id - base.MaxInstanceId)
            {
            case Id_ignoreComments:
                lib.ignoreComments = ScriptConvert.ToBoolean(value_Renamed);
                return;

            case Id_ignoreProcessingInstructions:
                lib.ignoreProcessingInstructions = ScriptConvert.ToBoolean(value_Renamed);
                return;

            case Id_ignoreWhitespace:
                lib.ignoreWhitespace = ScriptConvert.ToBoolean(value_Renamed);
                return;

            case Id_prettyIndent:
                lib.prettyIndent = ScriptConvert.ToInt32(value_Renamed);
                return;

            case Id_prettyPrinting:
                lib.prettyPrinting = ScriptConvert.ToBoolean(value_Renamed);
                return;
            }
            base.SetInstanceIdValue(id, value_Renamed);
        }
示例#2
0
        internal static int GetPrecision(object arg)
        {
            int precision = ScriptConvert.ToInt32(arg);

            AssertValidPrecision(precision);
            return(precision);
        }
示例#3
0
        private static string js_toSource(Context cx, IScriptable scope, IScriptable thisObj)
        {
            // Emulation of SpiderMonkey behavior
            object name       = ScriptableObject.GetProperty(thisObj, "name");
            object message    = ScriptableObject.GetProperty(thisObj, "message");
            object fileName   = ScriptableObject.GetProperty(thisObj, "fileName");
            object lineNumber = ScriptableObject.GetProperty(thisObj, "lineNumber");

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("(new ");
            if (name == UniqueTag.NotFound)
            {
                name = Undefined.Value;
            }
            sb.Append(ScriptConvert.ToString(name));
            sb.Append("(");
            if (message != UniqueTag.NotFound || fileName != UniqueTag.NotFound || lineNumber != UniqueTag.NotFound)
            {
                if (message == UniqueTag.NotFound)
                {
                    message = "";
                }
                sb.Append(ScriptRuntime.uneval(cx, scope, message));
                if (fileName != UniqueTag.NotFound || lineNumber != UniqueTag.NotFound)
                {
                    sb.Append(", ");
                    if (fileName == UniqueTag.NotFound)
                    {
                        fileName = "";
                    }
                    sb.Append(ScriptRuntime.uneval(cx, scope, fileName));
                    if (lineNumber != UniqueTag.NotFound)
                    {
                        int line = ScriptConvert.ToInt32(lineNumber);
                        if (line != 0)
                        {
                            sb.Append(", ");
                            sb.Append(ScriptConvert.ToString(line));
                        }
                    }
                }
            }
            sb.Append("))");
            return(sb.ToString());
        }
示例#4
0
        private static string num_to(double val, object [] args, int zeroArgMode, int oneArgMode, int precisionMin, int precisionOffset)
        {
            int precision;

            if (args.Length == 0)
            {
                precision  = 0;
                oneArgMode = zeroArgMode;
            }
            else
            {
                /* We allow a larger range of precision than
                *  ECMA requires; this is permitted by ECMA. */
                precision = ScriptConvert.ToInt32(args [0]);
                if (precision < precisionMin || precision > MAX_PRECISION)
                {
                    string msg = ScriptRuntime.GetMessage("msg.bad.precision", ScriptConvert.ToString(args [0]));
                    throw ScriptRuntime.ConstructError("RangeError", msg);
                }
            }


            switch (zeroArgMode)
            {
            case DTOSTR_FIXED:
                return(val.ToString("F" + (precision + precisionOffset), NumberFormatter));

            case DTOSTR_STANDARD_EXPONENTIAL:
                return(val.ToString("e" + (precision + precisionOffset), NumberFormatter));

            case DTOSTR_STANDARD:
                if (oneArgMode == DTOSTR_PRECISION)
                {
                    return(val.ToString(precision.ToString(), NumberFormatter));
                }
                else
                {
                    return(val.ToString(NumberFormatter));
                }
            }

            Context.CodeBug();
            return(string.Empty); // Not reached
        }
示例#5
0
        internal static BuiltinError make(Context cx, IScriptable scope, IdFunctionObject ctorObj, object [] args)
        {
            IScriptable proto = (IScriptable)(ctorObj.Get("prototype", ctorObj));

            BuiltinError obj = new BuiltinError();

            obj.SetPrototype(proto);
            obj.ParentScope = scope;

            if (args.Length >= 1)
            {
                ScriptableObject.PutProperty(obj, "message", ScriptConvert.ToString(args [0]));
                if (args.Length >= 2)
                {
                    ScriptableObject.PutProperty(obj, "fileName", args [1]);
                    if (args.Length >= 3)
                    {
                        int line = ScriptConvert.ToInt32(args [2]);
                        ScriptableObject.PutProperty(obj, "lineNumber", (object)line);
                    }
                }
            }
            return(obj);
        }
示例#6
0
        /// <summary> The global method parseInt, as per ECMA-262 15.1.2.2.</summary>
        private object js_parseInt(object [] args)
        {
            string s     = ScriptConvert.ToString(args, 0);
            int    radix = ScriptConvert.ToInt32(args, 1);

            int len = s.Length;

            if (len == 0)
            {
                return(double.NaN);
            }

            bool negative = false;
            int  start    = 0;
            char c;

            do
            {
                c = s [start];
                if (!char.IsWhiteSpace(c))
                {
                    break;
                }
                start++;
            }while (start < len);

            if (c == '+' || (negative = (c == '-')))
            {
                start++;
            }

            const int NO_RADIX = -1;

            if (radix == 0)
            {
                radix = NO_RADIX;
            }
            else if (radix < 2 || radix > 36)
            {
                return(double.NaN);
            }
            else if (radix == 16 && len - start > 1 && s [start] == '0')
            {
                c = s [start + 1];
                if (c == 'x' || c == 'X')
                {
                    start += 2;
                }
            }

            if (radix == NO_RADIX)
            {
                radix = 10;
                if (len - start > 1 && s [start] == '0')
                {
                    c = s [start + 1];
                    if (c == 'x' || c == 'X')
                    {
                        radix  = 16;
                        start += 2;
                    }
                    else if ('0' <= c && c <= '9')
                    {
                        radix = 8;
                        start++;
                    }
                }
            }

            double d = ScriptConvert.ToNumber(s, start, radix);

            return(negative ? -d : d);
        }
示例#7
0
        internal static string ImplToString(double value, object [] args)
        {
            int radix = (args == null || args.Length == 0) ? 10 : ScriptConvert.ToInt32(args [0]);

            return(ImplToString(value, radix));
        }
示例#8
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;

            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));
            }
        }