Example #1
0
        private string JSValueToString(JavaScriptValue val)
        {
            switch (val.ValueType)
            {
            case JavaScriptValueType.Boolean:
                return(val.ToBoolean() ? "true" : "false");

            case JavaScriptValueType.Number:
                try
                {
                    return(val.ToInt32().ToString());
                }
                catch (JavaScriptException)
                {
                    return(val.ToDouble().ToString(CultureInfo.InvariantCulture));
                }

            case JavaScriptValueType.Error:
                var keys = val.GetOwnPropertyNames();
                throw new Exception("Error in js");

            case JavaScriptValueType.String:
                return(val.ToString());

            case JavaScriptValueType.Array:
//                    int length = val.GetProperty(JavaScriptPropertyId.FromString("length")).ToInt32();
//                    return "Array";
            case JavaScriptValueType.Object:
                return(val.ToJsonString());

            default:
                return(val.ValueType.ToString());
            }
        }
Example #2
0
        private IEnumerable <Span> GetValueSpans(JavaScriptValue value, int level = 0, bool inArray = false)
        {
            switch (value.ValueType)
            {
            case JavaScriptValueType.Null:
                return(new Span[] { new Span("null") });

            case JavaScriptValueType.Undefined:
                return(new Span[] { new Span("undefined") });

            case JavaScriptValueType.String:
                return(new Span[] { $"\"{value.ToString().Replace("\"", "\\\"")}\"".DarkGreen() });

            case JavaScriptValueType.Boolean:
                return(new Span[] { new Span(value.ToBoolean() ? "true" : "false") });

            case JavaScriptValueType.Number:
                try
                {
                    return(new Span[] { value.ToInt32().ToString().Red() });
                }
                catch (JavaScriptException)
                {
                    return(new Span[] { value.ToDouble().ToString(CultureInfo.InvariantCulture).Red() });
                }

            case JavaScriptValueType.Function:
                return(new Span[] { GetFuncRep(value).Magenta() });

            case JavaScriptValueType.Error:
                return(new Span[] { value.ToJsonString().DarkRed() });

            case JavaScriptValueType.Object:
                return(GetObjectSpans(value, level, inArray));

            case JavaScriptValueType.Array:
                return(GetArraySpans(value, level));

            default:
                return(new Span[] { new Span(value.ValueType.ToString()), });
            }
        }
Example #3
0
        /// <summary>
        /// Converts a <see cref="JavaScriptValue"/> with a number type to host value.
        /// </summary>
        public object ToHostNumber(JavaScriptValue arg, Type toType)
        {
            // 32-bit Conversions
            if (toType == typeof(byte))
            {
                return((byte)arg.ToInt32());
            }
            if (toType == typeof(sbyte))
            {
                return((sbyte)arg.ToInt32());
            }
            if (toType == typeof(short))
            {
                return((short)arg.ToInt32());
            }
            if (toType == typeof(ushort))
            {
                return((ushort)arg.ToInt32());
            }
            if (toType == typeof(int))
            {
                return(arg.ToInt32());
            }
            if (toType == typeof(uint))
            {
                return((uint)arg.ToInt32());
            }

            // 64-bit Conversions
            if (toType == typeof(long))
            {
                return((long)arg.ToDouble());
            }
            if (toType == typeof(ulong))
            {
                return((ulong)arg.ToDouble());
            }
            if (toType == typeof(float))
            {
                return((float)arg.ToDouble());
            }
            if (toType == typeof(double))
            {
                return(arg.ToDouble());
            }
            if (toType == typeof(decimal))
            {
                return((decimal)arg.ToDouble());
            }

            // Other Conversions
            if (toType == typeof(string))
            {
                return(arg.ConvertToString().ToString());
            }
            if (toType == typeof(bool))
            {
                return(arg.ConvertToBoolean().ToBoolean());
            }

            // Last Attempt
            if (toType.IsAssignableFrom(typeof(double)))
            {
                return(arg.ToDouble());
            }

            throw new Exception($"Cannot convert javascript number to type: {toType}");
        }