コード例 #1
0
        /// <summary>
        /// Added by chuan.yin in 2017/4/27
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private object[] ToArray(JsValue value)
        {
            var length = value.ArrayLength;

            List <object> rtn = new List <object>();

            for (var i = 0; i < length; i++)
            {
                var jsv = value.GetIndexedProperty(JsValue.FromInt32(i));
                rtn.Add(MapToHostType(jsv));
            }
            return(rtn.ToArray());
        }
コード例 #2
0
 public void TMArray()
 {
     using (new JsContext.Scope(fixture.context))
     {
         var gob   = JsValue.GlobalObject;
         var array = JsValue.CreateArray(3);
         array.SetIndexedProperty(0, JsValue.FromInt32(1));
         array.SetIndexedProperty(1, JsValue.FromInt32(2));
         array.SetIndexedProperty(2, JsValue.FromInt32(7));
         gob.SetProperty("x", array, true);
         var result      = JsContext.RunScript("x[0]");
         var finalResult = fixture.typeMapper.MapToHostType(result);
         Assert.Equal(1, finalResult);
     }
 }
コード例 #3
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));
                }
            }
        }
コード例 #4
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));
            }
        }