Example #1
0
        private static NSJSObject New(NSJSVirtualMachine machine, WebSocketListener server)
        {
            if (machine == null || server == null)
            {
                return(null);
            }
            NSJSObject objective = NSJSObject.New(machine);

            objective.Set("Port", server.Port);
            objective.Set("GetBindPort", g_GetBindPortProc);
            objective.Set("Stop", g_StopProc);
            objective.Set("Start", g_StartProc);
            objective.Set("Close", g_CloseProc);
            objective.Set("Dispose", g_CloseProc);

            server.UserToken         = objective;
            objective.CrossThreading = true;

            server.OnMessage += (websocket, e) => ProcessEvent(objective, websocket, "OnMessage", e);
            server.OnOpen    += (websocket, e) => ProcessEvent(objective, websocket, "OnOpen", e, true);
            server.OnError   += (websocket, e) => ProcessEvent(objective, websocket, "OnError", e);
            server.OnClose   += (websocket, e) => ProcessEvent(objective, websocket, "OnClose", e);

            NSJSKeyValueCollection.Set(objective, server);
            return(objective);
        }
Example #2
0
        private static void Require(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            NSJSString rawUri = arguments.Length > 0 ? arguments[0] as NSJSString : null;

            if (rawUri == null || rawUri.DateType != NSJSDataType.kString)
            {
                arguments.SetReturnValue(false);
            }
            else
            {
                NSJSVirtualMachine machine = arguments.VirtualMachine;
                NSJSObject         global  = machine.Global;
                string             path    = rawUri.Value;
                string             source;
                if (string.IsNullOrEmpty(path))
                {
                    arguments.SetReturnValue(false);
                }
                else
                {
                    int index = path.IndexOf('#');
                    if (index > -1)
                    {
                        path = path.Substring(0, index);
                    }
                    index = path.IndexOf('?');
                    if (index > -1)
                    {
                        path = path.Substring(0, index);
                    }
                    do
                    {
                        bool success = false;
                        if (!FileAuxiliary.TryReadAllText(path, out source))
                        {
                            if (!FileAuxiliary.TryReadAllText(Application.StartupPath + "/" + path, out source))
                            {
                                arguments.SetReturnValue(false);
                                break;
                            }
                            success = true;
                        }
                        if (!success)
                        {
                            if (!HttpAuxiliary.TryReadAllText(path, out source))
                            {
                                arguments.SetReturnValue(false);
                                break;
                            }
                        }
                        if (File.Exists(path))
                        {
                            path = Path.GetFullPath(path);
                        }
                        arguments.SetReturnValue(machine.Run(source, path));
                    } while (false);
                }
            }
        }
Example #3
0
        private static void ContentEncoding(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            HTTPResponse             response  = GetResponse(arguments.This);

            if (response == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                NSJSVirtualMachine machine = arguments.VirtualMachine;
                if (arguments.Length <= 0)
                {
                    arguments.SetReturnValue(NSJSEncoding.New(machine, response.ContentEncoding));
                }
                else
                {
                    var encoding = NSJSEncoding.GetEncoding(arguments[0] as NSJSObject);
                    if (encoding == null)
                    {
                        encoding = NSJSEncoding.DefaultEncoding;
                    }
                    response.ContentEncoding = encoding;
                    arguments.SetReturnValue(true);
                }
            }
        }
Example #4
0
        public static NSJSValue ToObject(NSJSVirtualMachine machine, IDictionary <string, string> s)
        {
            if (machine == null)
            {
                return(null);
            }
            NSJSObject obj = NSJSObject.New(machine);

            if (s != null)
            {
                foreach (KeyValuePair <string, string> kv in s)
                {
                    if (string.IsNullOrEmpty(kv.Key))
                    {
                        continue;
                    }
                    if (kv.Value == null)
                    {
                        obj.Set(kv.Key, NSJSValue.Null(machine));
                    }
                    else
                    {
                        obj.Set(kv.Key, kv.Value);
                    }
                }
            }
            return(obj);
        }
Example #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);
        }
Example #6
0
        public static NSJSValue ToObject(NSJSVirtualMachine machine, NameValueCollection s)
        {
            if (machine == null)
            {
                return(null);
            }
            NSJSObject obj = NSJSObject.New(machine);

            if (s != null)
            {
                foreach (string key in s.AllKeys)
                {
                    if (string.IsNullOrEmpty(key))
                    {
                        continue;
                    }
                    string value = s.Get(key);
                    string name  = key.ToLower();
                    if (value != null)
                    {
                        obj.Set(name, value);
                    }
                    else
                    {
                        obj.Set(name, NSJSValue.Null(machine));
                    }
                }
            }
            return(obj);
        }
Example #7
0
        public static NSJSValue ToObject(NSJSVirtualMachine machine, Cookie cookie)
        {
            if (machine == null)
            {
                return(null);
            }
            if (cookie == null)
            {
                return(NSJSValue.Null(machine));
            }
            NSJSObject objective = NSJSObject.New(machine);

            objective.Set("Comment", cookie.Comment);
            objective.Set("CommentUri", cookie.CommentUri?.ToString());
            objective.Set("Discard", cookie.Discard);
            objective.Set("Domain", cookie.Domain);
            objective.Set("Expired", cookie.Expired);
            objective.Set("Expires", cookie.Expires < NSJSDateTime.Min ? NSJSDateTime.Min : cookie.Expires);
            objective.Set("HttpOnly", cookie.HttpOnly);
            objective.Set("Name", cookie.Name);
            objective.Set("Path", cookie.Path);
            objective.Set("Port", cookie.Port);
            objective.Set("Secure", cookie.Secure);
            objective.Set("TimeStamp", cookie.TimeStamp);
            objective.Set("Value", cookie.Value);
            objective.Set("Version", cookie.Version);
            return(objective);
        }
Example #8
0
        public static NSJSValue ToObject(NSJSVirtualMachine machine, HttpFileCollection files)
        {
            if (machine == null)
            {
                return(null);
            }
            if (files == null)
            {
                return(NSJSValue.Null(machine));
            }
            NSJSObject owner = NSJSObject.New(machine);

            foreach (string key in files.AllKeys)
            {
                if (string.IsNullOrEmpty(key))
                {
                    continue;
                }
                HttpPostedFile fileinfo = files[key];
                if (fileinfo == null)
                {
                    continue;
                }
                owner.Set(key, ToObject(machine, fileinfo));
            }
            return(owner);
        }
Example #9
0
        public static NSJSObject New(NSJSVirtualMachine machine, NSJSObject context, HTTPResponse response)
        {
            if (machine == null || context == null || response == null)
            {
                return(null);
            }
            NSJSObject objective = NSJSObject.New(machine);

            objective.Set("CurrentContext", context);
            objective.DefineProperty("ContentEncoding", g_ContentEncodingProc, g_ContentEncodingProc);
            objective.DefineProperty("ContentType", g_ContentTypeProc, g_ContentTypeProc);
            objective.DefineProperty("StatusDescription", g_StatusDescriptionProc, g_StatusDescriptionProc);
            objective.DefineProperty("StatusCode", g_StatusCodeProc, g_StatusCodeProc);
            objective.DefineProperty("KeepAlive", g_KeepAliveProc, g_KeepAliveProc);
            objective.DefineProperty("ProtocolVersion", g_ProtocolVersionProc, g_ProtocolVersionProc);
            objective.DefineProperty("RedirectLocation", g_RedirectLocationProc, g_RedirectLocationProc);
            objective.DefineProperty("SendChunked", g_SendChunkedProc, g_SendChunkedProc);
            objective.DefineProperty("Headers", g_HeadersProc, g_HeadersProc);
            objective.DefineProperty("Cookies", g_CookiesProc, g_CookiesProc);

            objective.Set("Redirect", g_RedirectProc);
            objective.Set("End", g_EndProc);
            objective.Set("Abort", g_AbortProc);

            objective.Set("SetCookie", g_SetCookieProc);
            objective.Set("AppendCookie", g_SetCookieProc);
            objective.Set("AddHeader", g_AddHeaderProc);
            objective.Set("AppendHeader", g_AddHeaderProc);

            objective.Set("Write", g_WriteProc);
            objective.Set("WriteFile", g_WriteFileProc);
            objective.Set("BinaryWrite", g_BinaryWriteProc);
            NSJSKeyValueCollection.Set(objective, response);
            return(objective);
        }
Example #10
0
 public static NSJSObject New(NSJSVirtualMachine machine, ENCODING encoding)
 {
     lock (g_Locker)
     {
         if (machine == null || encoding == null)
         {
             return(null);
         }
         IDictionary <IntPtr, NSJSObject> dVirtualTables;
         if (!g_EncodingInstanceTable.TryGetValue(encoding.CodePage, out dVirtualTables))
         {
             dVirtualTables = new Dictionary <IntPtr, NSJSObject>();
             g_EncodingInstanceTable.Add(encoding.CodePage, dVirtualTables);
         }
         NSJSObject o;
         if (dVirtualTables.TryGetValue(machine.Isolate, out o))
         {
             return(o);
         }
         if (!g_UninitializedEncoding)
         {
             g_UninitializedEncoding = true;
             g_GetBytesProc          = NSJSPinnedCollection.Pinned <NSJSFunctionCallback>(GetBytes);
             g_GetStringProc         = NSJSPinnedCollection.Pinned <NSJSFunctionCallback>(GetString);
         }
         o = NSJSObject.New(machine);
         o.CrossThreading = true;
         o.Set("GetBytes", g_GetBytesProc);
         o.Set("GetString", g_GetStringProc);
         dVirtualTables.Add(machine.Isolate, o);
         NSJSKeyValueCollection.Set(o, encoding);
         return(o);
     }
 }
Example #11
0
        private static bool fill2object(NSJSObject o, HttpClientResponse responset)
        {
            if (o == null || responset == null)
            {
                return(false);
            }
            NSJSVirtualMachine machine = o.VirtualMachine;

            machine.Join((sender, state) =>
            {
                o.Set("AsynchronousMode", responset.AsynchronousMode);
                o.Set("CharacterSet", responset.CharacterSet ?? string.Empty);
                o.Set("ContentLength", responset.ContentLength);
                o.Set("ContentType", responset.ContentType ?? string.Empty);
                o.Set("LastModified", NSJSDateTime.Invalid(responset.LastModified) ? NSJSDateTime.Min :
                      responset.LastModified);
                o.Set("ManualWriteToStream", responset.ManualWriteToStream);
                o.Set("ResponseUri", responset.ResponseUri == null ? string.Empty :
                      responset.ResponseUri.ToString());
                o.Set("Server", responset.Server ?? string.Empty);
                o.Set("StatusCode", unchecked ((int)responset.StatusCode));
                o.Set("ContentEncoding", responset.ContentEncoding ?? string.Empty);
                o.Set("StatusDescription", responset.StatusDescription ?? string.Empty);
            });
            return(true);
        }
Example #12
0
        public static NSJSObject GetMessageEventData(NSJSVirtualMachine machine, EventArgs e)
        {
            if (machine == null || e == null)
            {
                return(null);
            }
            MessageEventArgs message = e as MessageEventArgs;

            if (message == null)
            {
                return(null);
            }
            NSJSObject data = NSJSObject.New(machine);

            data.Set("IsText", message.IsText);
            if (message.IsText)
            {
                data.Set("RawData", message.Message);
            }
            else // BLOB
            {
                data.Set("RawData", message.RawData);
            }
            return(data);
        }
Example #13
0
        public static NSJSObject New(NSJSVirtualMachine machine, BaseStream stream)
        {
            if (machine == null || stream == null)
            {
                return(null);
            }
            NSJSObject o = NSJSObject.New(machine);

            o.Set("CanWrite", stream.CanWrite);
            o.Set("CanSeek", stream.CanSeek);
            o.Set("CanRead", stream.CanRead);

            o.DefineProperty("Length", m_LengthProc, (NSJSFunctionCallback)null);
            o.DefineProperty("Position", m_PositionProc, m_PositionProc);
            o.Set("Seek", m_SeekProc);
            o.Set("CopyTo", m_CopyToProc);

            o.Set("Read", m_ReadProc);
            o.Set("Write", m_WriteProc);
            o.Set("Flush", m_FlushProc);
            o.Set("ReadBytes", m_ReadBytesProc);

            o.Set("Close", m_DisposeProc);
            o.Set("Dispose", m_DisposeProc);

            NSJSKeyValueCollection.Set(o, stream);
            return(o);
        }
Example #14
0
            public void ProcessRequest(HTTPContext context)
            {
                if (context == null)
                {
                    return /*undefined*/;
                }
                NSJSVirtualMachine machine = this.GetVirtualMachine();

                machine.Join((sender, state) =>
                {
                    NSJSFunction function = this.GetProcessRequestCallback();
                    if (function != null)
                    {
                        NSJSObject context_object = null;
                        try
                        {
                            context_object = this.NewContextObject(context);
                        }
                        catch (Exception) { /*-----*/ }
                        if (context_object != null)
                        {
                            function.Call(context_object);
                        }
                    }
                });
            }
Example #15
0
        private static void New(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            string             url             = arguments.Length > 0 ? (arguments[0] as NSJSString)?.Value : null;
            NSJSVirtualMachine machine         = arguments.VirtualMachine;

            if (url == null)
            {
                Throwable.ArgumentNullException(machine);
            }
            else if ((url = url.Trim()).Length <= 0)
            {
                Throwable.ArgumentException(machine);
            }
            else
            {
                try
                {
                    arguments.SetReturnValue(New(machine, new WebSocket(url)));
                }
                catch (Exception exception)
                {
                    Throwable.Exception(machine, exception);
                }
            }
        }
Example #16
0
 private static void ReceiveAsync(IntPtr info)
 {
     InternalReceiveAsync(info, false, (socket, socketobject, data, buffer, ofs, count, flags, remoteep, callback) =>
     {
         bool success = false;
         if ((success = SocketExtension.BeginReceive(socket, buffer, ofs, count, flags, (result) =>
         {
             int len = SocketExtension.EndReceive(socket, result, out SocketError error);
             NSJSVirtualMachine machine = socketobject.VirtualMachine;
             machine.Join((sender, state) =>
             {
                 if (len > 0)
                 {
                     for (int i = ofs; i < len; i++)
                     {
                         data[i] = buffer[i];
                     }
                 }
                 if (callback != null)
                 {
                     callback.Call(socketobject, NSJSInt32.New(machine, unchecked ((int)error)), NSJSInt32.New(machine, len));
                 }
             });
         })))
         {
             if (callback != null)
             {
                 callback.CrossThreading = true;
             }
             data.CrossThreading = true;
         }
         return(success);
     });
Example #17
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);
        }
Example #18
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);
        }
Example #19
0
        public static NSJSObject New(NSJSVirtualMachine machine, HTTPApplication application)
        {
            if (machine == null || application == null)
            {
                return(null);
            }
            NSJSObject objective = NSJSObject.New(machine);

            objective.DefineProperty("Name", g_NameProc, g_NameProc);
            objective.DefineProperty("Root", g_RootProc, g_RootProc);
            objective.Set("Start", g_StartProc);
            objective.Set("Stop", g_StopProc);
            objective.Set("Close", g_CloseProc);
            objective.Set("Dispose", g_CloseProc);

            application.Tag          = objective;
            objective.CrossThreading = true;

            application.Handler              = new HttpHandler(objective, application);
            application.EndProcessRequest   += g_EndProcessRequestProc;
            application.BeginProcessRequest += g_BeginProcessRequestProc;

            NSJSKeyValueCollection.Set(objective, application);
            return(objective);
        }
Example #20
0
            public static NSJSObject NewContextObject(NSJSVirtualMachine machine,
                                                      NSJSObject application,
                                                      HTTPContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }
                if (application == null)
                {
                    throw new ArgumentNullException("application");
                }
                if (machine == null)
                {
                    throw new ArgumentNullException("machine");
                }
                NSJSObject objective = NSJSObject.New(machine); // ctx

                objective.Set("Application", application);
                objective.Set("Close", m_CloseProc);
                objective.Set("Dispose", m_CloseProc);
                objective.Set("Request", HttpRequest.New(machine, objective, context.Request));
                objective.Set("Response", HttpResponse.New(machine, objective, context.Response));
                objective.DefineProperty("Asynchronous", m_AsynchronousProc, m_AsynchronousProc);
                NSJSKeyValueCollection.Set(objective, context);
                return(objective);
            }
Example #21
0
 private static void ProcessConnected(object sender, SocketAsyncEventArgs e)
 {
     using (e)
     {
         try
         {
             SocketContext context = e.UserToken as SocketContext;
             do
             {
                 if (context == null)
                 {
                     break;
                 }
                 NSJSFunction function = context.ConnectedAsyncCallback;
                 NSJSObject   socket   = context.This;
                 context.ConnectedAsync         = null;
                 context.ConnectedAsyncCallback = null;
                 if (function == null)
                 {
                     break;
                 }
                 NSJSVirtualMachine machine = function.VirtualMachine;
                 if (machine == null)
                 {
                     break;
                 }
                 machine.Join((sendert, statet) => function.Call(socket, NSJSInt32.New(machine, unchecked ((int)e.SocketError))));
             } while (false);
         }
         catch (Exception) { }
     }
 }
Example #22
0
        private static bool ProcessEvent(object sender, string evt, EventArgs e)
        {
            if (sender == null || string.IsNullOrEmpty(evt))
            {
                return(false);
            }
            WebSocket socket = sender as WebSocket;

            if (socket == null)
            {
                return(false);
            }
            NSJSObject websocket = Get(socket);

            if (websocket == null)
            {
                return(false);
            }
            NSJSVirtualMachine machine = websocket.VirtualMachine;

            machine.Join(delegate
            {
                NSJSFunction callback = websocket.Get(evt) as NSJSFunction;
                if (callback != null)
                {
                    NSJSObject data = WebSocketClient.GetMessageEventData(machine, e);
                    callback.Call(new NSJSValue[] { data });
                }
            });
            return(true);
        }
Example #23
0
        private NSJSObject New(NSJSVirtualMachine machine, RijndaelCryptoServiceProvider provider)
        {
            NSJSObject o = NSJSObject.New(machine);

            o.Set("Encrypt", this.m_EncryptProc);
            o.Set("Decrypt", this.m_DecryptProc);
            o.Set("Dispose", this.m_DisposeProc);
            NSJSKeyValueCollection.Set(o, provider);
            return(o);
        }
Example #24
0
        private static NSJSConsoleHandler GetConsoleHandler(NSJSFunctionCallbackInfo arguments)
        {
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }
            NSJSVirtualMachine machine = arguments.VirtualMachine;

            return(machine.ConsoleHandler);
        }
Example #25
0
        private static NSJSObject New(NSJSVirtualMachine machine, RC4CSP provider)
        {
            NSJSObject o = NSJSObject.New(machine);

            o.Set("Encrypt", m_EncryptProc);
            o.Set("Decrypt", m_DecryptProc);
            o.Set("Dispose", m_DisposeProc);
            NSJSKeyValueCollection.Set(o, provider);
            return(o);
        }
Example #26
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 #27
0
 public static void Exception(this NSJSVirtualMachine machine, Exception exception)
 {
     do
     {
         if (machine == null || exception == null)
         {
             break;
         }
         NSJSException.Throw(machine, exception);
     } while (false);
 }
Example #28
0
        private static void ____nsjsdotnet_framework_object_isdefined(NSJSVirtualMachine machine)
        {
            machine.Run(@"
                        function ____nsjsdotnet_framework_object_isdefined(obj, key) {
                            'use strict'

                            if(obj === null || obj === undefined) {
                                return 0;
                            }
                            return (obj.hasOwnProperty(key) ? 1 : 0);
                        }");
        }
Example #29
0
        private static void ____nsjsdotnet_framework_object_getpropertynames(NSJSVirtualMachine machine)
        {
            machine.Run(@"
                        function ____nsjsdotnet_framework_object_getpropertynames(obj) {
                            'use strict'

                            if(obj === null || obj === undefined) {
                                return new Array();
                            }
                            return Object.getOwnPropertyNames(obj);
                        }");
        }
Example #30
0
 public static void Initialization(NSJSVirtualMachine machine)
 {
     if (machine == null)
     {
         throw new ArgumentNullException("machine");
     }
     ____nsjsdotnet_framework_instanceof(machine);
     ____nsjsdotnet_framework_object_getpropertynames(machine);
     ____nsjsdotnet_framework_object_defineproperty(machine);
     ____nsjsdotnet_framework_object_isdefined(machine);
     ____nsjsdotnet_framework_object_getpropertydescriptor(machine);
 }