Example #1
0
        private static void ToDateTime(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            NSJSValue result = null;

            if (arguments.Length > 0)
            {
                NSJSUInt8Array value      = arguments[0] as NSJSUInt8Array;
                NSJSInt32      startIndex = null;
                if (arguments.Length > 1)
                {
                    startIndex = arguments[1] as NSJSInt32;
                }
                if (value != null)
                {
                    int offset = 0;
                    if (startIndex != null)
                    {
                        offset = startIndex.Value;
                    }
                    if (offset < 0)
                    {
                        offset = 0;
                    }
                    byte[] buffer = value.Buffer;
                    if (buffer != null)
                    {
                        long ticks = BITCONVERTER.ToInt64(buffer, offset);
                        result = NSJSDateTime.New(arguments.VirtualMachine, ticks);
                    }
                }
            }
            if (result == null)
            {
                result = NSJSValue.Undefined(arguments.VirtualMachine);
            }
            arguments.SetReturnValue(result);
        }
Example #2
0
        public static NSJSValue As(this object value, NSJSVirtualMachine machine)
        {
            if (machine == null)
            {
                return(null);
            }
            if (value == null || value == DBNull.Value)
            {
                return(NSJSValue.Null(machine));
            }
            if (value is NSJSValue)
            {
                return(value as NSJSValue);
            }
            Type typeid = value.GetType();

            if (typeid == typeof(int) ||
                typeid == typeof(short) ||
                typeid == typeof(sbyte) ||
                typeid == typeof(char))
            {
                return(NSJSInt32.New(machine, Convert.ToInt32(value)));
            }
            else if (typeid == typeof(uint) ||
                     typeid == typeof(ushort) ||
                     typeid == typeof(byte))
            {
                return(NSJSUInt32.New(machine, Convert.ToUInt32(value)));
            }
            else if (typeid == typeof(string))
            {
                return(NSJSString.New(machine, value.ToString()));
            }
            else if (typeid == typeof(bool))
            {
                return(NSJSBoolean.New(machine, Convert.ToBoolean(value)));
            }
            else if (typeid == typeof(DateTime))
            {
                DateTime datetime = Convert.ToDateTime(value);
                if (NSJSDateTime.Invalid(datetime))
                {
                    datetime = NSJSDateTime.Min;
                }
                return(NSJSDateTime.New(machine, datetime));
            }
            else if (typeid == typeof(float) || typeid == typeof(double))
            {
                return(NSJSDouble.New(machine, Convert.ToDouble(value)));
            }
            else if (typeid == typeof(byte[]))
            {
                byte[] buffer = (byte[])(object)value;
                if (buffer == null)
                {
                    return(NSJSValue.Null(machine));
                }
                return(NSJSUInt8Array.New(machine, buffer));
            }
            else if (typeid == typeof(sbyte[]))
            {
                sbyte[] buffer = (sbyte[])(object)value;
                if (buffer == null)
                {
                    return(NSJSValue.Null(machine));
                }
                return(NSJSInt8Array.New(machine, buffer));
            }
            else if (typeid == typeof(short[]))
            {
                short[] buffer = (short[])(object)value;
                if (buffer == null)
                {
                    return(NSJSValue.Null(machine));
                }
                return(NSJSInt16Array.New(machine, buffer));
            }
            else if (typeid == typeof(ushort[]))
            {
                ushort[] buffer = (ushort[])(object)value;
                if (buffer == null)
                {
                    return(NSJSValue.Null(machine));
                }
                return(NSJSUInt16Array.New(machine, buffer));
            }
            else if (typeid == typeof(int[]))
            {
                int[] buffer = (int[])(object)value;
                if (buffer == null)
                {
                    return(NSJSValue.Null(machine));
                }
                return(NSJSInt32Array.New(machine, buffer));
            }
            else if (typeid == typeof(uint[]))
            {
                uint[] buffer = (uint[])(object)value;
                if (buffer == null)
                {
                    return(NSJSValue.Null(machine));
                }
                return(NSJSUInt32Array.New(machine, buffer));
            }
            else if (typeid == typeof(float[]))
            {
                float[] buffer = (float[])(object)value;
                if (buffer == null)
                {
                    return(NSJSValue.Null(machine));
                }
                return(NSJSFloat32Array.New(machine, buffer));
            }
            else if (typeid == typeof(double[]))
            {
                double[] buffer = (double[])(object)value;
                if (buffer == null)
                {
                    return(NSJSValue.Null(machine));
                }
                return(NSJSFloat64Array.New(machine, buffer));
            }
            return(NSJSValue.Null(machine));
        }