Пример #1
0
        /// <summary>
        /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.3
        /// </summary>
        public static double ToNumber(JsValue o)
        {
            if (o._type == Types.Object && o is IPrimitiveInstance)
            {
                var p = o as IPrimitiveInstance;
                return(ToNumber(ToPrimitive(p.PrimitiveValue, Types.Number)));
            }

            switch (o._type)
            {
            // check number first as this is what is usually expected
            case Types.Number:
                return(((JsNumber)o)._value);

            case Types.Undefined:
                return(double.NaN);

            case Types.Null:
                return(0);

            case Types.Boolean:
                return(((JsBoolean)o)._value ? 1 : 0);

            case Types.String:
                return(ToNumber(o.AsStringWithoutTypeCheck()));

            default:
                return(ToNumber(ToPrimitive(o, Types.Number)));
            }
        }
Пример #2
0
        /// <summary>
        /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.8
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ToString(JsValue o)
        {
            if (o._type == Types.Object && o is IPrimitiveInstance)
            {
                var p = o as IPrimitiveInstance;
                return(ToString(ToPrimitive(p.PrimitiveValue, Types.String)));
            }

            switch (o._type)
            {
            case Types.String:
                return(o.AsStringWithoutTypeCheck());

            case Types.Boolean:
                return(((JsBoolean)o)._value ? "true" : "false");

            case Types.Number:
                return(ToString(((JsNumber)o)._value));

            case Types.Symbol:
                return(o.AsSymbol());

            case Types.Undefined:
                return(Undefined.Text);

            case Types.Null:
                return(Null.Text);

            default:
                return(ToString(ToPrimitive(o, Types.String)));
            }
        }
Пример #3
0
        private static string GetErrorMessage(JsValue error)
        {
            if (error.IsObject())
            {
                var oi      = error.AsObject();
                var message = oi.Get("message").ToString();
                return(message);
            }
            if (error.IsString())
            {
                return(error.AsStringWithoutTypeCheck());
            }

            return(error.ToString());
        }
Пример #4
0
        public static ObjectInstance ToObject(Engine engine, JsValue value)
        {
            switch (value._type)
            {
            case Types.Object:
                return((ObjectInstance)value);

            case Types.Boolean:
                return(engine.Boolean.Construct(((JsBoolean)value)._value));

            case Types.Number:
                return(engine.Number.Construct(((JsNumber)value)._value));

            case Types.String:
                return(engine.String.Construct(value.AsStringWithoutTypeCheck()));

            case Types.Symbol:
                return(engine.Symbol.Construct(((JsSymbol)value)._value));

            default:
                ExceptionHelper.ThrowTypeError(engine);
                return(null);
            }
        }
Пример #5
0
        public JsValue Serialize(JsValue value, JsValue replacer, JsValue space)
        {
            _stack = new Stack <object>();

            // for JSON.stringify(), any function passed as the first argument will return undefined
            // if the replacer is not defined. The function is not called either.
            if (value.Is <ICallable>() && ReferenceEquals(replacer, Undefined.Instance))
            {
                return(Undefined.Instance);
            }

            if (replacer.IsObject())
            {
                if (replacer.Is <ICallable>())
                {
                    _replacerFunction = replacer;
                }
                else
                {
                    var replacerObj = replacer.AsObject();
                    if (replacerObj.Class == "Array")
                    {
                        _propertyList = new List <string>();
                    }

                    foreach (var property in replacerObj.GetOwnProperties().Select(x => x.Value))
                    {
                        JsValue v    = _engine.GetValue(property, false);
                        string  item = null;
                        if (v.IsString())
                        {
                            item = v.AsStringWithoutTypeCheck();
                        }
                        else if (v.IsNumber())
                        {
                            item = TypeConverter.ToString(v);
                        }
                        else if (v.IsObject())
                        {
                            var propertyObj = v.AsObject();
                            if (propertyObj.Class == "String" || propertyObj.Class == "Number")
                            {
                                item = TypeConverter.ToString(v);
                            }
                        }

                        if (item != null && !_propertyList.Contains(item))
                        {
                            _propertyList.Add(item);
                        }
                    }
                }
            }

            if (space.IsObject())
            {
                var spaceObj = space.AsObject();
                if (spaceObj.Class == "Number")
                {
                    space = TypeConverter.ToNumber(spaceObj);
                }
                else if (spaceObj.Class == "String")
                {
                    space = TypeConverter.ToString(spaceObj);
                }
            }

            // defining the gap
            if (space.IsNumber())
            {
                var number = ((JsNumber)space)._value;
                if (number > 0)
                {
                    _gap = new string(' ', (int)System.Math.Min(10, number));
                }
                else
                {
                    _gap = string.Empty;
                }
            }
            else if (space.IsString())
            {
                var stringSpace = space.AsStringWithoutTypeCheck();
                _gap = stringSpace.Length <= 10 ? stringSpace : stringSpace.Substring(0, 10);
            }
            else
            {
                _gap = string.Empty;
            }

            var wrapper = _engine.Object.Construct(Arguments.Empty);

            wrapper.DefineOwnProperty("", new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable), false);

            return(Str("", wrapper));
        }