Beispiel #1
0
        public static bool Close(NSJSValue request)
        {
            NSJSObject o = request as NSJSObject;

            if (o == null)
            {
                return(false);
            }
            ObjectAuxiliary.RemoveInKeyValueCollection(o.Get("InputStream") as NSJSObject);
            NSJSArray files = o.Get("Files") as NSJSArray;

            if (files != null)
            {
                foreach (NSJSValue value in files)
                {
                    if (value == null)
                    {
                        continue;
                    }
                    NSJSObject posedfile = value as NSJSObject;
                    if (posedfile == null)
                    {
                        continue;
                    }
                    ObjectAuxiliary.RemoveInKeyValueCollection(o.Get("InputStream") as NSJSObject);
                }
            }
            return(ObjectAuxiliary.RemoveInKeyValueCollection(request as NSJSObject));
        }
Beispiel #2
0
        private static IEnumerable <HttpPostValue> array2blobs(NSJSArray array)
        {
            if (array == null)
            {
                return(null);
            }
            IList <HttpPostValue> blobs = new List <HttpPostValue>();
            int count = array.Length;

            for (int i = 0; i < count; i++)
            {
                NSJSObject o = array[i] as NSJSObject;
                if (o == null)
                {
                    continue;
                }
                HttpPostValue blob = new HttpPostValue();
                blob.FileName = (o.Get("FileName") as NSJSString)?.Value;
                NSJSBoolean boolean = o.Get("IsText") as NSJSBoolean;
                if (boolean != null)
                {
                    blob.IsText = boolean.Value;
                }
                blob.Key   = (o.Get("Key") as NSJSString)?.Value;
                blob.Value = (o.Get("Value") as NSJSUInt8Array)?.Buffer;
            }
            return(blobs);
        }
Beispiel #3
0
        public static int Fill(NSJSValue source, MailAddressCollection destination)
        {
            int count = 0;

            if (destination == null)
            {
                return(count);
            }
            NSJSString addresss = source as NSJSString;

            if (addresss != null)
            {
                destination.Add(addresss.Value);
                return(count);
            }
            NSJSArray s = source as NSJSArray;

            if (s == null)
            {
                return(count);
            }
            int len = s.Length;

            for (int i = 0; i < len; i++)
            {
                MailAddress address = ObjectAuxiliary.ToMailAddress(s[i]);
                if (address == null)
                {
                    continue;
                }
                destination.Add(address);
                count++;
            }
            return(count);
        }
Beispiel #4
0
        public static NSJSValue ToArray(NSJSVirtualMachine machine, IEnumerable <string> s)
        {
            if (machine == null)
            {
                return(null);
            }
            int       count = Enumerable.Count(s);
            NSJSArray array = NSJSArray.New(machine, count);
            int       index = 0;

            s.FirstOrDefault(item =>
            {
                NSJSValue value = null;
                if (item == null)
                {
                    value = NSJSValue.Null(machine);
                }
                else
                {
                    value = NSJSString.New(machine, item);
                }
                array[index++] = value;
                return(false);
            });
            return(array);
        }
Beispiel #5
0
        public static NSJSValue ToObject(NSJSVirtualMachine machine, IPHostEntry host)
        {
            if (machine == null)
            {
                return(null);
            }
            if (host == null)
            {
                return(NSJSValue.Null(machine));
            }
            NSJSObject entry = NSJSObject.New(machine);

            entry.Set("HostName", host.HostName);
            entry.Set("AddressList", ArrayAuxiliary.ToArray(machine, host.AddressList));

            string[]  aliases = host.Aliases;
            NSJSArray array   = NSJSArray.New(machine, aliases.Length);

            for (int i = 0; i < aliases.Length; i++)
            {
                array[i] = NSJSString.New(machine, aliases[i]);
            }
            entry.Set("Aliases", array);

            return(entry);
        }
Beispiel #6
0
        public static int Fill <T>(NSJSValue source, ICollection <T> destination, Func <NSJSArray, int, T> converter)
        {
            NSJSArray s     = source as NSJSArray;
            int       count = 0;

            if (destination == null)
            {
                return(count);
            }
            if (s == null)
            {
                return(count);
            }
            if (converter == null)
            {
                return(count);
            }
            int len = s.Length;

            for (int i = 0; i < len; i++)
            {
                T item = converter(s, count);
                if (item == null)
                {
                    continue;
                }
                destination.Add(item);
                count++;
            }
            return(count);
        }
Beispiel #7
0
        public static IList <string> ToStringList(NSJSValue value)
        {
            if (value == null)
            {
                return(null);
            }
            IList <string> s = new List <string>();

            try
            {
                NSJSArray arrays = value as NSJSArray;
                if (arrays != null)
                {
                    foreach (NSJSValue i in arrays)
                    {
                        if (i == null)
                        {
                            continue;
                        }
                        s.Add(ValueAuxiliary.ToString(i));
                    }
                }
            }
            catch (Exception) { }
            return(s);
        }
Beispiel #8
0
        public static NSJSValue ToArray(NSJSVirtualMachine machine, DataTable dataTable)
        {
            if (machine == null)
            {
                return(null);
            }
            DataRowCollection rows = null;
            int       count        = dataTable == null ? 0 : (rows = dataTable.Rows).Count;
            NSJSArray results      = NSJSArray.New(machine, count);

            if (count <= 0)
            {
                return(results);
            }
            IDictionary <string, int> columns = new Dictionary <string, int>();

            foreach (DataColumn column in dataTable.Columns)
            {
                columns.Add(column.ColumnName, column.Ordinal);
            }
            for (int i = 0; i < count; i++)
            {
                NSJSObject item = NSJSObject.New(machine);
                DataRow    row  = rows[i];
                results[i] = item;
                foreach (KeyValuePair <string, int> column in columns)
                {
                    object value = row[column.Value];
                    item.Set(column.Key, value.As(machine));
                }
            }
            return(results);
        }
Beispiel #9
0
 public static void Sort(NSJSArray s, int low, int high, Func <NSJSValue, NSJSValue, bool> max)
 {
     if (!(s.Length <= 1 || low >= high))
     {
         int middle = QuickSorting.Middle(s, low, high, max);
         QuickSorting.Sort(s, low, middle - 1, max);  // 向左向中心扩散
         QuickSorting.Sort(s, middle + 1, high, max); // 从中心向右扩散
     }
 }
Beispiel #10
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);
        }
Beispiel #11
0
        public static NSJSValue ToArray(NSJSVirtualMachine machine, IEnumerable <KeyValuePair <string, string> > s)
        {
            if (machine == null)
            {
                return(null);
            }
            int       count = Enumerable.Count(s);
            NSJSArray array = NSJSArray.New(machine, count);
            int       index = 0;

            s.FirstOrDefault(kv =>
            {
                array[index++] = ObjectAuxiliary.ToObject(machine, kv);
                return(false);
            });
            return(array);
        }
Beispiel #12
0
        public static NSJSValue ToArray(NSJSVirtualMachine machine, IEnumerable <IPEndPoint> endpoints)
        {
            if (machine == null)
            {
                return(null);
            }
            int       count = Enumerable.Count(endpoints);
            NSJSArray array = NSJSArray.New(machine, count);
            int       index = 0;

            endpoints.FirstOrDefault(address =>
            {
                array[index++] = ObjectAuxiliary.ToObject(machine, address);
                return(false);
            });
            return(array);
        }
Beispiel #13
0
        public static NSJSValue ToArray(NSJSVirtualMachine machine, CookieCollection cookies)
        {
            if (machine == null)
            {
                return(null);
            }
            int       count = cookies == null ? 0 : cookies.Count;
            NSJSArray s     = NSJSArray.New(machine, count);

            if (cookies != null)
            {
                int index = 0;
                foreach (Cookie cookie in cookies)
                {
                    s[index++] = ObjectAuxiliary.ToObject(machine, cookie);
                }
            }
            return(s);
        }
Beispiel #14
0
            private static int Middle(NSJSArray s, int low, int high, Func <NSJSValue, NSJSValue, bool> max)
            {
                NSJSValue key = s[low];

                while (low < high)
                {
                    while (max(s[high], key) && high > low) // R轴
                    {
                        high--;
                    }
                    s[low] = s[high];
                    while (!max(s[low], key) && high > low) // L轴
                    {
                        low--;
                    }
                    s[high] = s[low];
                }
                s[low] = key;
                return(high);
            }
Beispiel #15
0
        public static NSJSValue ToArray(NSJSVirtualMachine machine, IEnumerable <IDbDataParameter> parameters)
        {
            if (machine == null)
            {
                return(null);
            }
            int       count   = parameters.Count();
            NSJSArray results = NSJSArray.New(machine, count);
            int       index   = 0;

            parameters.FirstOrDefault(i =>
            {
                if (i == null)
                {
                    return(false);
                }
                results[index++] = ObjectAuxiliary.ToObject(machine, i);
                return(false);
            });
            return(results);
        }
Beispiel #16
0
        public static int Fill(NSJSValue source, CookieCollection destination)
        {
            NSJSArray s     = source as NSJSArray;
            int       count = 0;

            if (s == null || destination == null)
            {
                return(count);
            }
            int len = s.Length;

            for (int i = 0; i < len; i++)
            {
                Cookie cookie = ObjectAuxiliary.ToCookie(s[i]);
                if (cookie == null)
                {
                    continue;
                }
                destination.Add(cookie);
                count++;
            }
            return(count);
        }
Beispiel #17
0
        private static void Sort(IntPtr info, Action <NSJSArray, int, int, Func <NSJSValue, NSJSValue, bool> > sorting)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            bool success = false;

            if (arguments.Length > 1)
            {
                NSJSArray    s    = arguments[0] as NSJSArray;
                NSJSFunction max  = arguments[1] as NSJSFunction;
                int          low  = 0;
                int          high = -1;
                if (arguments.Length > 3 && max == null)
                {
                    max = arguments[3] as NSJSFunction;
                    NSJSInt32 i32 = arguments[1] as NSJSInt32;
                    if (i32 != null)
                    {
                        low = i32.Value;
                    }
                    i32 = arguments[2] as NSJSInt32;
                    if (i32 != null)
                    {
                        high = i32.Value;
                    }
                }
                if (max != null && s != null)
                {
                    if (high < 0)
                    {
                        high = s.Length - 1;
                    }
                    sorting(s, low, high, (x, y) => ((max.Call(x, y) as NSJSBoolean)?.Value).GetValueOrDefault());
                }
            }
            arguments.SetReturnValue(success);
        }
Beispiel #18
0
        private static void GetFilesOrDirectories(IntPtr info, bool fileMode)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);

            string[]  buffer    = null;
            Exception exception = null;

            try
            {
                if (arguments.Length > 0)
                {
                    string path = (arguments[0] as NSJSString)?.Value;
                    if (DIRECTORY.Exists(path))
                    {
                        if (arguments.Length > 1)
                        {
                            string searchPattern = (arguments[1] as NSJSString)?.Value;
                            if (string.IsNullOrEmpty(searchPattern))
                            {
                                searchPattern = "*";
                            }
                            if (arguments.Length > 2)
                            {
                                int?searchOption = (arguments[2] as NSJSInt32)?.Value;
                                buffer = fileMode ?
                                         DIRECTORY.GetFiles(path, searchPattern, (SearchOption)searchOption.GetValueOrDefault()) :
                                         DIRECTORY.GetDirectories(path, searchPattern, (SearchOption)searchOption.GetValueOrDefault());
                            }
                            else
                            {
                                buffer = fileMode ?
                                         DIRECTORY.GetFiles(path, searchPattern) :
                                         DIRECTORY.GetDirectories(path, searchPattern);
                            }
                        }
                        else
                        {
                            buffer = fileMode ?
                                     DIRECTORY.GetFiles(path) :
                                     DIRECTORY.GetDirectories(path);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                exception = e;
            }
            if (exception != null)
            {
                Throwable.Exception(arguments.VirtualMachine, exception);
            }
            else if (buffer == null)
            {
                Throwable.DirectoryNotFoundException(arguments.VirtualMachine);
            }
            else
            {
                NSJSArray s = NSJSArray.New(arguments.VirtualMachine, buffer.Length);
                for (int i = 0; i < buffer.Length; i++)
                {
                    s[i] = NSJSString.New(arguments.VirtualMachine, buffer[i]);
                }
                arguments.SetReturnValue(s);
            }
        }
Beispiel #19
0
        private static void InternalExecute(NSJSFunctionCallbackInfo arguments, bool nonquery)
        {
            InternalExecute(arguments, (gateway, adapter, text) =>
            {
                DataTable dataTable = null;
                IDbCommand command  = null;
                try
                {
                    IDbTransaction transaction = null;
                    NSJSArray parameters       = null;
                    NSJSInt32 cmdtype          = null;
                    for (int solt = 1, count = arguments.Length; solt < count && (transaction == null ||
                                                                                  cmdtype == null || parameters == null); solt++)
                    {
                        NSJSValue current = arguments[solt];
                        if (transaction == null)
                        {
                            transaction = DatabaseTransaction.GetTransaction(current as NSJSObject);
                        }
                        if (cmdtype == null)
                        {
                            cmdtype = current as NSJSInt32;
                        }
                        if (parameters == null)
                        {
                            parameters = current as NSJSArray;
                        }
                    }
                    command         = ObjectAuxiliary.ToDbCommand(adapter, text, parameters);
                    int commandType = ValueAuxiliary.ToInt32(cmdtype);
                    switch (commandType)
                    {
                    case 1:
                        command.CommandType = CommandType.StoredProcedure;
                        break;

                    case 2:
                        command.CommandType = CommandType.TableDirect;
                        break;

                    default:
                        command.CommandType = CommandType.Text;
                        break;
                    }
                    command.Transaction = transaction;
                    if (nonquery)
                    {
                        arguments.SetReturnValue(gateway.ExecuteNonQuery(command));
                    }
                    else
                    {
                        dataTable = gateway.Select(command);
                        arguments.SetReturnValue(ArrayAuxiliary.ToArray(arguments.VirtualMachine, dataTable));
                    }
                }
                catch (Exception e)
                {
                    Throwable.Exception(arguments.VirtualMachine, e);
                }
                if (dataTable != null)
                {
                    dataTable.Dispose();
                }
                if (command != null)
                {
                    command.Dispose();
                }
            });
        }
Beispiel #20
0
        private static HttpClientOptions object2options(NSJSObject options)
        {
            if (options == null)
            {
                return(null);
            }
            HttpClientOptions o       = new HttpClientOptions();
            NSJSBoolean       boolean = options.Get("AllowAutoRedirect") as NSJSBoolean;

            if (boolean != null)
            {
                o.AllowAutoRedirect = boolean.Value;
            }
            NSJSInt32 int32 = options.Get("AutomaticDecompression") as NSJSInt32;

            if (int32 != null)
            {
                o.AutomaticDecompression = (DecompressionMethods)int32.Value;
            }
            int32 = options.Get("CachePolicy") as NSJSInt32;
            if (int32 != null)
            {
                o.CachePolicy = new HttpRequestCachePolicy((HttpRequestCacheLevel)int32.Value);
            }
            int32 = options.Get("MaximumAutomaticRedirections") as NSJSInt32;
            if (int32 != null)
            {
                o.MaximumAutomaticRedirections = int32.Value;
            }
            int32 = options.Get("Timeout") as NSJSInt32;
            if (int32 != null)
            {
                o.Timeout = int32.Value;
            }
            NSJSString stringt = options.Get("Proxy") as NSJSString;

            if (stringt != null)
            {
                o.Proxy = new WebProxy(stringt.Value);
            }
            stringt = options.Get("Referer") as NSJSString;
            if (stringt != null)
            {
                o.Referer = stringt.Value;
            }
            NSJSInt32Array int32array = options.Get("Range") as NSJSInt32Array;

            if (int32array != null)
            {
                o.Range = int32array.Buffer;
            }
            NSJSArray array = options.Get("Headers") as NSJSArray;

            if (array != null)
            {
                NameValueCollection headers = o.Headers;
                int count = array.Length;
                for (int i = 0; i < count; i++)
                {
                    stringt = array[i] as NSJSString;
                    if (stringt == null)
                    {
                        continue;
                    }
                    headers.Add(headers);
                }
            }
            return(o);
        }