Beispiel #1
0
        /// <summary>
        /// Makes a mapping of value from the host type to a script type
        /// </summary>
        /// <param name="value">The source value</param>
        /// <returns>The mapped value</returns>
        public JsValue MapToScriptType(object value)
        {
            if (value == null)
            {
                return(JsValue.Null);
            }

            TypeCode typeCode = Type.GetTypeCode(value.GetType());

            switch (typeCode)
            {
            case TypeCode.Boolean:
                return((bool)value ? JsValue.True : JsValue.False);

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
                return(JsValue.FromInt32(Convert.ToInt32(value)));

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                return(JsValue.FromDouble(Convert.ToDouble(value)));

            case TypeCode.Char:
            case TypeCode.String:
                return(JsValue.FromString((string)value));

            default:
                if (value is JsValue jsValue)
                {
                    return(jsValue);
                }
                else
                {
                    return(GetOrCreateScriptObject(value));
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Makes a mapping of value from the host type to a script type
        /// </summary>
        /// <param name="value">The source value</param>
        /// <returns>The mapped value</returns>
        private JsValue MapToScriptType(object value)
        {
            if (value == null)
            {
                return(JsValue.Null);
            }

            if (value is Undefined)
            {
                return(JsValue.Undefined);
            }

            TypeCode typeCode = value.GetType().GetTypeCode();

            switch (typeCode)
            {
            case TypeCode.Boolean:
                return((bool)value ? JsValue.True : JsValue.False);

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
                return(JsValue.FromInt32(Convert.ToInt32(value)));

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                return(JsValue.FromDouble(Convert.ToDouble(value)));

            case TypeCode.Char:
            case TypeCode.String:
                return(JsValue.FromString((string)value));

            default:
                return(FromObject(value));
            }
        }
 private JsValue VisitInteger(JValue token) => AddRef(JsValue.FromDouble(token.Value <double>()));
 private JsValue VisitFloat(JValue token) => AddRef(JsValue.FromDouble(token.Value <double>()));