Exemplo n.º 1
0
        public static NSJSValue ToArray(NSJSVirtualMachine machine, Type element, IList s)
        {
            if (machine == null)
            {
                return(null);
            }
            int       count = s == null ? 0 : s.Count;
            NSJSArray array = null;

            if (element == typeof(byte))
            {
                array = NSJSUInt8Array.New(machine, ToArray <byte>(s));
            }
            else if (element == typeof(sbyte))
            {
                array = NSJSInt8Array.New(machine, ToArray <sbyte>(s));
            }
            else if (element == typeof(short))
            {
                array = NSJSInt16Array.New(machine, ToArray <short>(s));
            }
            else if (element == typeof(ushort))
            {
                array = NSJSUInt16Array.New(machine, ToArray <ushort>(s));
            }
            else if (element == typeof(int))
            {
                array = NSJSInt32Array.New(machine, ToArray <int>(s));
            }
            else if (element == typeof(uint))
            {
                array = NSJSUInt32Array.New(machine, ToArray <uint>(s));
            }
            else if (element == typeof(float))
            {
                array = NSJSFloat32Array.New(machine, ToArray <float>(s));
            }
            else if (element == typeof(double))
            {
                array = NSJSFloat64Array.New(machine, ToArray <double>(s));
            }
            else
            {
                array = NSJSArray.New(machine, count);
                for (int i = 0; i < count; i++)
                {
                    array[i] = ObjectAuxiliary.ToObject(machine, s[i]);
                }
            }
            if (array == null)
            {
                return(NSJSValue.Null(machine));
            }
            return(array);
        }
Exemplo n.º 2
0
        private static void ReadBytes(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                NSJSValue result    = null;
                Exception exception = null;
                if (arguments.Length > 0)
                {
                    int max = ((arguments[0] as NSJSInt32)?.Value).GetValueOrDefault();
                    if (max >= 0)
                    {
                        try
                        {
                            byte[] ch  = new byte[max];
                            int    len = stream.Read(ch, 0, max);
                            result = NSJSUInt8Array.New(arguments.VirtualMachine, ch, len);
                        }
                        catch (Exception e)
                        {
                            exception = e;
                        }
                    }
                }
                if (exception != null)
                {
                    Throwable.Exception(arguments.VirtualMachine, exception);
                }
                else if (result != null)
                {
                    arguments.SetReturnValue(result);
                }
                else
                {
                    arguments.SetReturnValue(NSJSValue.Undefined(arguments.VirtualMachine));
                }
            }
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        // [native] bool HttpClient.TryUploadAsync(string url, HttpPostValue[] blobs, HttpClientOptions options, HttpClientResponse response, HttpClientAsyncCallback callback)
        private static void TryUploadAsync(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            bool success = false;

            if (arguments.Length > 4)
            {
                string                      url      = (arguments[0] as NSJSString)?.Value;
                HttpClientOptions           options  = HttpClient.object2options(arguments[2] as NSJSObject);
                NSJSObject                  response = arguments[3] as NSJSObject;
                IEnumerable <HttpPostValue> blobs    = HttpClient.array2blobs(arguments[1] as NSJSArray);
                if (options != null && response != null)
                {
                    NSJSFunction callback = arguments[4] as NSJSFunction;
                    if (callback != null)
                    {
                        callback.CrossThreading = true;
                    }
                    response.CrossThreading = true;
                    bool fillToObject            = false;
                    HttpClientResponse responset = HttpClient.object2response(response);
                    success = RESTClient.TryUploadAsync(url, blobs, options, responset, (error, buffer, count) =>
                    {
                        NSJSVirtualMachine machine = arguments.VirtualMachine;
                        if (error == HttpClientError.Success && !fillToObject)
                        {
                            fillToObject = true;
                            fill2object(response, responset);
                        }
                        if (callback != null)
                        {
                            bool breakto = false;
                            machine.Join((sender, state) => breakto = ((callback.Call
                                                                        (
                                                                            NSJSInt32.New(machine, (int)error),
                                                                            NSJSValue.NullMerge(machine, buffer != null && count >= 0 ? NSJSUInt8Array.New(machine, buffer, count) : null)
                                                                        ) as NSJSBoolean)?.Value) == false);
                            if (breakto)
                            {
                                return(false);
                            }
                        }
                        return(count > 0);
                    });
                }
            }
            arguments.SetReturnValue(success);
        }