Example #1
0
        private static void WriteAllBytes(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            NSJSString     url       = arguments.Length > 0 ? arguments[0] as NSJSString : null;
            NSJSUInt8Array buffer    = arguments.Length > 1 ? arguments[1] as NSJSUInt8Array : null;
            bool           success   = false;
            Exception      exception = null;

            if (url != null)
            {
                string path  = url.Value;
                byte[] bytes = (buffer == null ? BufferExtension.EmptryBuffer : buffer.Buffer);
                try
                {
                    FILE.WriteAllBytes(path, bytes);
                    success = true;
                }
                catch (Exception e)
                {
                    exception = e;
                    success   = false;
                }
            }
            if (exception == null)
            {
                arguments.SetReturnValue(success);
            }
            else
            {
                Throwable.Exception(arguments.VirtualMachine, exception);
            }
        }
Example #2
0
        private static void ToUInt32(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            uint result = 0;

            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)
                    {
                        result = BITCONVERTER.ToUInt32(buffer, offset);
                    }
                }
            }
            arguments.SetReturnValue(result);
        }
Example #3
0
        private static void GetString(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            ENCODING encoding = NSJSKeyValueCollection.Get <ENCODING>(arguments.This);

            if (encoding == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                string s = null;
                if (arguments.Length > 0)
                {
                    NSJSUInt8Array chars = arguments[0] as NSJSUInt8Array;
                    if (chars != null)
                    {
                        byte[] buffer = chars.Buffer;
                        if (buffer != null)
                        {
                            NSJSInt32 index = null;
                            NSJSInt32 len   = null;
                            switch (arguments.Length)
                            {
                            case 2:
                                len = arguments[1] as NSJSInt32;
                                break;

                            case 3:
                                index = arguments[1] as NSJSInt32;
                                len   = arguments[2] as NSJSInt32;
                                break;
                            }
                            int ofs   = index != null ? index.Value : 0;
                            int count = len != null ? len.Value : buffer.Length;
                            if (count < 0)
                            {
                                count = 0;
                            }
                            if (ofs < 0)
                            {
                                ofs = 0;
                            }
                            s = encoding.GetString(buffer, ofs, count);
                        }
                    }
                }
                if (s != null)
                {
                    arguments.SetReturnValue(s);
                }
                else
                {
                    arguments.SetReturnValue(NSJSValue.Undefined(arguments.VirtualMachine));
                }
            }
        }
Example #4
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);
        }
Example #5
0
 public static void Copy(IList <byte> sourceArray, int sourceIndex, NSJSUInt8Array destinationArray, int destinationIndex, int length)
 {
     if (sourceArray == null)
     {
         throw new ArgumentNullException("sourceArray");
     }
     if (destinationArray == null)
     {
         throw new ArgumentNullException("destinationArray");
     }
     for (int i = 0; sourceIndex < length; sourceIndex++, i++)
     {
         destinationArray[destinationIndex + i] = sourceArray[sourceIndex];
     }
 }
Example #6
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));
                }
            }
        }
Example #7
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 #8
0
        private static void Write(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                Exception exception = null;
                bool      success   = false;
                if (arguments.Length > 0)
                {
                    NSJSUInt8Array buffer = arguments[0] as NSJSUInt8Array;
                    if (buffer != null)
                    {
                        int?count  = null;
                        int offset = 0;
                        switch (arguments.Length)
                        {
                        case 2:
                            count = (arguments[1] as NSJSInt32)?.Value;
                            break;

                        case 3:
                            offset = ((arguments[1] as NSJSInt32)?.Value).GetValueOrDefault();
                            count  = (arguments[2] as NSJSInt32)?.Value;
                            break;
                        }
                        if (offset < 0)
                        {
                            offset = 0;
                        }
                        if (count == null)
                        {
                            count = buffer.Length;
                        }
                        if ((offset + count) > buffer.Length)
                        {
                            count = (buffer.Length - offset);
                        }
                        if (!(offset < 0 || offset >= count))
                        {
                            try
                            {
                                stream.Write(buffer.Buffer, offset, count.Value);
                            }
                            catch (Exception e)
                            {
                                exception = e;
                            }
                        }
                    }
                }
                if (exception != null)
                {
                    Throwable.Exception(arguments.VirtualMachine, exception);
                }
                else
                {
                    arguments.SetReturnValue(success);
                }
            }
        }
Example #9
0
        private static void Read(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                Exception exception = null;
                int?      len       = null;
                if (arguments.Length > 0)
                {
                    NSJSUInt8Array buffer = arguments[0] as NSJSUInt8Array;
                    if (buffer != null)
                    {
                        int?count  = null;
                        int offset = 0;
                        switch (arguments.Length)
                        {
                        case 2:
                            count = ((arguments[1] as NSJSInt32)?.Value).GetValueOrDefault();
                            break;

                        case 3:
                            offset = ((arguments[1] as NSJSInt32)?.Value).GetValueOrDefault();
                            count  = (arguments[2] as NSJSInt32)?.Value;
                            break;
                        }
                        if (offset < 0)
                        {
                            offset = 0;
                        }
                        if (count == null)
                        {
                            count = buffer.Length;
                        }
                        if ((offset + count) > buffer.Length)
                        {
                            count = (buffer.Length - offset);
                        }
                        if (offset < 0 || offset >= count)
                        {
                            len = 0;
                        }
                        else
                        {
                            try
                            {
                                byte[] cch = new byte[count.Value];
                                len = stream.Read(cch, offset, cch.Length);
                                if (len > 0)
                                {
                                    for (int i = offset; i < len; i++)
                                    {
                                        buffer[i] = cch[i];
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                exception = e;
                            }
                        }
                    }
                }
                if (exception != null)
                {
                    Throwable.Exception(arguments.VirtualMachine, exception);
                }
                else
                {
                    arguments.SetReturnValue(len.GetValueOrDefault());
                }
            }
        }
Example #10
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));
        }
Example #11
0
 public static void Copy(IList <byte> sourceArray, NSJSUInt8Array destinationArray, int length)
 {
     ArrayAuxiliary.Copy(sourceArray, 0, destinationArray, 0, length);
 }
Example #12
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);
        }