Exemplo n.º 1
0
        void AssignMethodProc(JavaScriptValue setTo, MethodInfo[] methods)
        {
            var methodDic = new Dictionary <string, List <MethodInfo> >();

            foreach (var m in methods)
            {
                if (m.IsSpecialName)
                {
                    continue;
                }
                if (m.IsGenericMethodDefinition)
                {
                    continue;
                }
                if (m.IsGenericMethod)
                {
                    continue;
                }
                if (!methodDic.ContainsKey(m.Name))
                {
                    methodDic[m.Name] = new List <MethodInfo>();
                }
                methodDic[m.Name].Add(m);
            }
            foreach (var methodName in methodDic.Keys)
            {
                var ms = methodDic[methodName];
                if (ms.Count == 1)
                {
                    var m   = ms[0];
                    var smw = (m.IsStatic) ? (FunctionWrapper)StaticMethodWrapper.Wrap(m) : (FunctionWrapper)InstanceMethodWrapper.Wrap(m);
                    setTo.SetIndexedProperty(JavaScriptValue.FromString(m.Name), smw.GetJavaScriptValue());
                    methodWrappers.Add(smw);
                }
                else
                {
                    var os = new OverloadSelector();
                    foreach (var m in ms)
                    {
                        os.AppendMethod(m);
                    }
                    setTo.SetIndexedProperty(JavaScriptValue.FromString(os.GetName()), os.GetJavaScriptValue());
                    methodWrappers.Add(os);
                }
            }
        }
        private JavaScriptValue JSGetPushClients(JavaScriptValue callee, bool isConstructCall, JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData)
        {
            List <string>   clientIds = new List <string>(this.WebSocketClients.GetAll().Keys);
            JavaScriptValue outValue  = JavaScriptValue.CreateArray((uint)clientIds.Count);

            for (int i = 0; i < clientIds.Count; i++)
            {
                outValue.SetIndexedProperty(JavaScriptValue.FromInt32(i), JavaScriptValue.FromString(clientIds[i]));
            }

            return(outValue);
        }
Exemplo n.º 3
0
 public JavaScriptValue GetJavaScriptValue()
 {
     if (!value.IsValid)
     {
         if (isType)
         {
             typeWrapper = TypeWrapper.Wrap(type);
             value       = typeWrapper.GetJavaScriptValue();
         }
         else
         {
             value = JavaScriptValue.CreateObject();
             value.SetIndexedProperty(JavaScriptValue.FromString("toString"),
                                      JavaScriptValue.CreateFunction(InternalUtil.GetSavedStringDg, GCHandle.ToIntPtr(GCHandle.Alloc(path))));
         }
         value.AddRef();
         AssignToObject(value);
     }
     return(value);
 }
Exemplo n.º 4
0
        private static JavaScriptContext CreateHostContext(JavaScriptRuntime runtime, string[] arguments, int argumentsStart)
        {
            //
            // Create the context. Note that if we had wanted to start debugging from the very
            // beginning, we would have called JsStartDebugging right after context is created.
            //

            JavaScriptContext context = runtime.CreateContext();

            //
            // Now set the execution context as being the current one on this thread.
            //

            using (new JavaScriptContext.Scope(context))
            {
                //
                // Create the host object the script will use.
                //

                JavaScriptValue hostObject = JavaScriptValue.CreateObject();

                //
                // Get the global object
                //

                JavaScriptValue globalObject = JavaScriptValue.GlobalObject;

                //
                // Get the name of the property ("host") that we're going to set on the global object.
                //

                JavaScriptPropertyId hostPropertyId = JavaScriptPropertyId.FromString("host");

                //
                // Set the property.
                //

                globalObject.SetProperty(hostPropertyId, hostObject, true);

                //
                // Now create the host callbacks that we're going to expose to the script.
                //

                DefineHostCallback(hostObject, "echo", echoDelegate, IntPtr.Zero);
                DefineHostCallback(hostObject, "runScript", runScriptDelegate, IntPtr.Zero);

                //
                // Create an array for arguments.
                //

                JavaScriptValue hostArguments = JavaScriptValue.CreateArray((uint)(arguments.Length - argumentsStart));

                for (int index = argumentsStart; index < arguments.Length; index++)
                {
                    //
                    // Create the argument value.
                    //

                    JavaScriptValue argument = JavaScriptValue.FromString(arguments[index]);

                    //
                    // Create the index.
                    //

                    JavaScriptValue indexValue = JavaScriptValue.FromInt32(index - argumentsStart);

                    //
                    // Set the value.
                    //

                    hostArguments.SetIndexedProperty(indexValue, argument);
                }

                //
                // Get the name of the property that we're going to set on the host object.
                //

                JavaScriptPropertyId argumentsPropertyId = JavaScriptPropertyId.FromString("arguments");

                //
                // Set the arguments property.
                //

                hostObject.SetProperty(argumentsPropertyId, hostArguments, true);
            }

            return(context);
        }
Exemplo n.º 5
0
        public static void Register(JavaScriptContext context)
        {
            JavaScriptValue GameObjectPrototype;
            JavaScriptValue GameObjectConstructor = Bridge.CreateConstructor(
                typeof(UnityEngine.GameObject),
                (args) => {
                GameObject obj;

                if (args.Length > 1 && args[1].ValueType == JavaScriptValueType.String)
                {
                    obj = new GameObject(args[1].ToString());
                }
                else
                {
                    obj = new GameObject();
                }

                return(Bridge.CreateExternalWithPrototype(obj));
            },
                out GameObjectPrototype
                );

            JavaScriptValue
            .GlobalObject
            .GetProperty("UnityEngine")
            .SetProperty("GameObject", GameObjectConstructor);


            // Static Fields


            // Static Property Accessors


            // Static Methods

            GameObjectConstructor.SetProperty(
                "CreatePrimitive",
                Bridge.CreateFunction(
                    "CreatePrimitive",
                    (args) => {
                PrimitiveType type;
                switch (args[1].ToString().ToLower()) // TODO lowercase? can these collide?
                {
                case "capsule":
                    type = PrimitiveType.Capsule;
                    break;

                case "cube":
                    type = PrimitiveType.Cube;
                    break;

                case "cylinder":
                    type = PrimitiveType.Cylinder;
                    break;

                case "plane":
                    type = PrimitiveType.Plane;
                    break;

                case "quad":
                    type = PrimitiveType.Quad;
                    break;

                case "sphere":
                    type = PrimitiveType.Sphere;
                    break;

                default:
                    throw new System.Exception("invalid type");
                }
                return(Bridge.CreateExternalWithPrototype(GameObject.CreatePrimitive(type), GameObjectPrototype));
            }
                    )
                );


            GameObjectConstructor.SetProperty(
                "FindWithTag",
                Bridge.CreateFunction(
                    "FindWithTag",
                    (args) => Bridge.CreateExternalWithPrototype(UnityEngine.GameObject.FindWithTag(args[1].ToString()))
                    )
                );


            GameObjectConstructor.SetProperty(
                "FindGameObjectWithTag",
                Bridge.CreateFunction(
                    "FindGameObjectWithTag",
                    (args) => Bridge.CreateExternalWithPrototype(UnityEngine.GameObject.FindGameObjectWithTag(args[1].ToString()))
                    )
                );


            GameObjectConstructor.SetProperty(
                "FindGameObjectsWithTag",
                Bridge.CreateFunction(
                    "FindGameObjectsWithTag",
                    (args) => { // TODO make an array creator helper
                GameObject[] foundObjects = UnityEngine.GameObject.FindGameObjectsWithTag(args[1].ToString());
                JavaScriptValue jsArray   = JavaScriptValue.CreateArray(0);

                for (int i = 0; i < foundObjects.Length; i++)
                {
                    jsArray.SetIndexedProperty(i, Bridge.CreateExternalWithPrototype(foundObjects[i]));
                }

                return(jsArray);
            }
                    )
                );


            GameObjectConstructor.SetProperty(
                "Find",
                Bridge.CreateFunction(
                    "Find",
                    (args) => Bridge.CreateExternalWithPrototype(UnityEngine.GameObject.Find(args[1].ToString()))
                    )
                );


            // Instance Fields


            // Instance Property Accessors

            Bridge.DefineGetter(
                GameObjectPrototype,
                "transform",
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => Bridge.CreateExternalWithPrototype(o.transform))
                );


            Bridge.DefineGetterSetter(
                GameObjectPrototype,
                "layer",
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromInt32(o.layer)),
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { o.layer = args[1].ToInt32(); })
                );


            Bridge.DefineGetter(
                GameObjectPrototype,
                "activeSelf",
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromBoolean(o.activeSelf))
                );


            Bridge.DefineGetter(
                GameObjectPrototype,
                "activeInHierarchy",
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromBoolean(o.activeInHierarchy))
                );


            Bridge.DefineGetterSetter(
                GameObjectPrototype,
                "isStatic",
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromBoolean(o.isStatic)),
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { o.isStatic = args[1].ToBoolean(); })
                );


            Bridge.DefineGetterSetter(
                GameObjectPrototype,
                "tag",
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromString(o.tag)),
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { o.tag = args[1].ToString(); })
                );


            Bridge.DefineGetter(
                GameObjectPrototype,
                "scene",
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => Bridge.CreateExternalWithPrototype(o.scene))
                );


            Bridge.DefineGetter(
                GameObjectPrototype,
                "gameObject",
                Bridge.WithExternal <UnityEngine.GameObject>((o, args) => Bridge.CreateExternalWithPrototype(o.gameObject))
                );


            // Instance Methods

            GameObjectPrototype.SetProperty("GetComponent", Bridge.CreateFunction(Bridge.WithExternal <UnityEngine.GameObject>((o, args) => {
                string typeName;
                if (args[1].ValueType == JavaScriptValueType.Function)
                {
                    typeName = args[1].GetProperty("name").ToString();
                }
                else if (args[1].ValueType == JavaScriptValueType.String)
                {
                    typeName = args[1].ToString();
                }
                else
                {
                    throw new System.Exception("Argument passed to GetComponent must be a class or string");
                }

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);

                return(Bridge.CreateExternalWithPrototype(
                           o.GetComponent(type),
                           prototype
                           ));
            })));


            GameObjectPrototype.SetProperty(
                "GetComponentInChildren",
                Bridge.CreateFunction(
                    "GetComponentInChildren",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);

                if (args.Length == 2)
                {
                    return(Bridge.CreateExternalWithPrototype(
                               o.GetComponentInChildren(type),
                               prototype
                               ));
                }
                else
                {
                    return(Bridge.CreateExternalWithPrototype(
                               o.GetComponentInChildren(type, args[2].ToBoolean()),
                               prototype
                               ));
                }
            })
                    )
                );

            GameObjectPrototype.SetProperty(
                "GetComponentInParent",
                Bridge.CreateFunction(
                    "GetComponentInParent",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);

                return(Bridge.CreateExternalWithPrototype(
                           o.GetComponentInParent(type),
                           prototype
                           ));
            })
                    )
                );


            GameObjectPrototype.SetProperty(
                "GetComponents",
                Bridge.CreateFunction(
                    "GetComponents",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);


                JavaScriptValue array = JavaScriptValue.CreateArray(0);

                foreach (var component in o.GetComponents(type))
                {
                    array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype(
                                                               component,
                                                               prototype
                                                               ));
                }

                return(array);
            })
                    )
                );


            GameObjectPrototype.SetProperty(
                "GetComponentsInChildren",
                Bridge.CreateFunction(
                    "GetComponentsInChildren",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);
                bool includeInactive      = args.Length == 3 ? args[2].ToBoolean() : false;

                JavaScriptValue array = JavaScriptValue.CreateArray(0);

                foreach (var component in o.GetComponentsInChildren(type, includeInactive))
                {
                    array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype(
                                                               component,
                                                               prototype
                                                               ));
                }

                return(array);
            })
                    )
                );


            GameObjectPrototype.SetProperty(
                "GetComponentsInParent",
                Bridge.CreateFunction(
                    "GetComponentsInParent",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);
                bool includeInactive      = args.Length == 3 ? args[2].ToBoolean() : false;

                JavaScriptValue array = JavaScriptValue.CreateArray(0);

                foreach (var component in o.GetComponentsInParent(type, includeInactive))
                {
                    array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype(
                                                               component,
                                                               prototype
                                                               ));
                }

                return(array);
            })
                    )
                );


            GameObjectPrototype.SetProperty(
                "SendMessageUpwards",
                Bridge.CreateFunction(
                    "SendMessageUpwards",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2])))
                    )
                );


            GameObjectPrototype.SetProperty(
                "SendMessage",
                Bridge.CreateFunction(
                    "SendMessage",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2])))
                    )
                );


            GameObjectPrototype.SetProperty(
                "BroadcastMessage",
                Bridge.CreateFunction(
                    "BroadcastMessage",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2])))
                    )
                );


            GameObjectPrototype.SetProperty(
                "AddComponent",
                Bridge.CreateFunction(
                    "AddComponent",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);

                return(Bridge.CreateExternalWithPrototype(o.AddComponent(type), prototype));
            })
                    )
                );


            /*
             * GameObject AddComponent
             * method has generics
             */


            GameObjectPrototype.SetProperty(
                "SetActive",
                Bridge.CreateFunction(
                    "SetActive",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SetActive(args[1].ToBoolean()))
                    )
                );


            GameObjectPrototype.SetProperty(
                "CompareTag",
                Bridge.CreateFunction(
                    "CompareTag",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromBoolean(o.CompareTag(args[1].ToString())))
                    )
                );


            GameObjectPrototype.SetProperty(
                "SendMessageUpwards",
                Bridge.CreateFunction(
                    "SendMessageUpwards",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3])))
                    )
                );


            GameObjectPrototype.SetProperty(
                "SendMessageUpwards",
                Bridge.CreateFunction(
                    "SendMessageUpwards",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2])))
                    )
                );


            GameObjectPrototype.SetProperty(
                "SendMessageUpwards",
                Bridge.CreateFunction(
                    "SendMessageUpwards",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessageUpwards(args[1].ToString()))
                    )
                );


            GameObjectPrototype.SetProperty(
                "SendMessage",
                Bridge.CreateFunction(
                    "SendMessage",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3])))
                    )
                );


            GameObjectPrototype.SetProperty(
                "SendMessage",
                Bridge.CreateFunction(
                    "SendMessage",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2])))
                    )
                );


            GameObjectPrototype.SetProperty(
                "SendMessage",
                Bridge.CreateFunction(
                    "SendMessage",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessage(args[1].ToString()))
                    )
                );


            GameObjectPrototype.SetProperty(
                "BroadcastMessage",
                Bridge.CreateFunction(
                    "BroadcastMessage",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3])))
                    )
                );


            GameObjectPrototype.SetProperty(
                "BroadcastMessage",
                Bridge.CreateFunction(
                    "BroadcastMessage",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2])))
                    )
                );


            GameObjectPrototype.SetProperty(
                "BroadcastMessage",
                Bridge.CreateFunction(
                    "BroadcastMessage",
                    Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.BroadcastMessage(args[1].ToString()))
                    )
                );
        }
Exemplo n.º 6
0
        TypeWrapper(Type type)
        {
            TypeWrapper baseTypeWrapper = null;

            if (type.BaseType != null)
            {
                baseTypeWrapper = TypeWrapper.Wrap(type.BaseType);
            }

            this.type = type;
            var ctors = type.GetConstructors();

            thisPtr = GCHandle.Alloc(this);
            if (ctors.Length == 0)
            {
                constructorValue = JavaScriptValue.CreateFunction(NoConstructorDg, GCHandle.ToIntPtr(thisPtr));
            }
            else if (ctors.Length == 1)
            {
                var ctorw = ConstructorWrapper.Wrap(ctors[0]);
                constructorValue = ctorw.GetJavaScriptValue();
                ctorWrapper      = ctorw;
            }
            else
            {
                var os = new OverloadSelector();
                foreach (var m in ctors)
                {
                    os.AppendMethod(m);
                }
                constructorValue = os.GetJavaScriptValue();
                ctorWrapper      = os;
            }
            constructorValue.AddRef();
            prototypeValue = JavaScriptValue.CreateObject();
            prototypeValue.AddRef();
            typePtr = GCHandle.Alloc(type);
            constructorValue.SetIndexedProperty(JavaScriptValue.FromString("_clrtypevalue"),
                                                JavaScriptValue.CreateExternalObject(GCHandle.ToIntPtr(typePtr), FreeDg));
            // statics
            ctorStr = type.FullName;
            constructorValue.SetIndexedProperty(JavaScriptValue.FromString("toString"),
                                                JavaScriptValue.CreateFunction(InternalUtil.GetSavedStringDg, GCHandle.ToIntPtr(GCHandle.Alloc(ctorStr))));
            AssignMethodProc(constructorValue,
                             type.GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static));
            AssignFieldProc(constructorValue,
                            type.GetFields(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static));
            AssignPropertyProc(constructorValue,
                               type.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static));
            // instances
            prototypeStr = type.FullName + " Instance";
            prototypeValue.SetIndexedProperty(JavaScriptValue.FromString("toString"),
                                              JavaScriptValue.CreateFunction(InternalUtil.GetSavedStringDg, GCHandle.ToIntPtr(GCHandle.Alloc(prototypeStr))));
            AssignMethodProc(prototypeValue,
                             type.GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance));
            AssignFieldProc(prototypeValue,
                            type.GetFields(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance));
            AssignPropertyProc(prototypeValue,
                               type.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance));

            constructorValue.SetIndexedProperty(JavaScriptValue.FromString("prototype"), prototypeValue);

            if (baseTypeWrapper != null)
            {
                constructorValue.Prototype = baseTypeWrapper.constructorValue;
                prototypeValue.Prototype   = baseTypeWrapper.prototypeValue;
            }
        }
        private JavaScriptContext CreateHostContext(JavaScriptRuntime runtime, HttpListenerRequest request)
        {
            // Create the context. Note that if we had wanted to start debugging from the very
            // beginning, we would have called JsStartDebugging right after context is created.
            JavaScriptContext context = runtime.CreateContext();

            // Now set the execution context as being the current one on this thread.
            using (new JavaScriptContext.Scope(context))
            {
                // Create the host object the script will use.
                JavaScriptValue hostObject = JavaScriptValue.CreateObject();

                // Get the global object
                JavaScriptValue globalObject = JavaScriptValue.GlobalObject;

                // Get the name of the property ("host") that we're going to set on the global object.
                JavaScriptPropertyId hostPropertyId = JavaScriptPropertyId.FromString("host");

                // Set the property.
                globalObject.SetProperty(hostPropertyId, hostObject, true);

                // Now create the host callbacks that we're going to expose to the script.
                DefineHostCallback(hostObject, "echo", echoDelegate, IntPtr.Zero);
                DefineHostCallback(hostObject, "runScript", runScriptDelegate, IntPtr.Zero);
                DefineHostCallback(hostObject, "readFile", readFileDelegate, IntPtr.Zero);
                DefineHostCallback(hostObject, "writeFile", writeFileDelegate, IntPtr.Zero);

                // Create an object for request.
                JavaScriptValue requestParams = JavaScriptValue.CreateObject();

                if (request.RawUrl != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("rawUrl"), JavaScriptValue.FromString(request.RawUrl), true);
                }
                if (request.UserAgent != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("userAgent"), JavaScriptValue.FromString(request.UserAgent), true);
                }
                if (request.UserHostName != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("userHostName"), JavaScriptValue.FromString(request.UserHostName), true);
                }
                if (request.UserHostAddress != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("userHostAddress"), JavaScriptValue.FromString(request.UserHostAddress), true);
                }
                if (request.ServiceName != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("serviceName"), JavaScriptValue.FromString(request.ServiceName), true);
                }
                if (request.HttpMethod != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("httpMethod"), JavaScriptValue.FromString(request.HttpMethod), true);
                }
                if (request.ContentType != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("contentType"), JavaScriptValue.FromString(request.ContentType), true);
                }
                if (request.ContentEncoding != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("contentEncoding"), JavaScriptValue.FromString(request.ContentEncoding.WebName), true);
                }

                requestParams.SetProperty(JavaScriptPropertyId.FromString("keepAlive"), JavaScriptValue.FromBoolean(request.KeepAlive), true);
                requestParams.SetProperty(JavaScriptPropertyId.FromString("isWebSocketRequest"), JavaScriptValue.FromBoolean(request.IsWebSocketRequest), true);
                requestParams.SetProperty(JavaScriptPropertyId.FromString("isSecureConnection"), JavaScriptValue.FromBoolean(request.IsSecureConnection), true);
                requestParams.SetProperty(JavaScriptPropertyId.FromString("isLocal"), JavaScriptValue.FromBoolean(request.IsLocal), true);
                requestParams.SetProperty(JavaScriptPropertyId.FromString("isAuthenticated"), JavaScriptValue.FromBoolean(request.IsAuthenticated), true);
                requestParams.SetProperty(JavaScriptPropertyId.FromString("hasEntityBody"), JavaScriptValue.FromBoolean(request.HasEntityBody), true);
                requestParams.SetProperty(JavaScriptPropertyId.FromString("contentLength64"), JavaScriptValue.FromDouble(request.ContentLength64), true);

                // need to call begingetclientcertificate
                //requestParams.SetProperty(JavaScriptPropertyId.FromString("clientCertificateError"), JavaScriptValue.FromInt32(request.ClientCertificateError), true);

                if (request.UrlReferrer != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("urlReferrer"), JavaScriptValue.FromString(request.UrlReferrer.ToString()), true);
                }
                if (request.RequestTraceIdentifier != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("requestTraceIdentifier"), JavaScriptValue.FromString(request.RequestTraceIdentifier.ToString()), true);
                }
                if (request.RemoteEndPoint != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("remoteEndPoint"), JavaScriptValue.FromString(request.RemoteEndPoint.ToString()), true);
                }
                if (request.ProtocolVersion != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("protocolVersion"), JavaScriptValue.FromString(request.ProtocolVersion.ToString()), true);
                }
                if (request.LocalEndPoint != null)
                {
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("localEndPoint"), JavaScriptValue.FromString(request.LocalEndPoint.ToString()), true);
                }

                if (request.UserLanguages != null)
                {
                    JavaScriptValue userLanguages = JavaScriptValue.CreateArray((uint)request.UserLanguages.Length);
                    for (int i = 0; i < request.UserLanguages.Length; i++)
                    {
                        userLanguages.SetIndexedProperty(JavaScriptValue.FromInt32(i), JavaScriptValue.FromString(request.UserLanguages[i]));
                    }
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("userLanguages"), userLanguages, true);
                }

                if (request.AcceptTypes != null)
                {
                    JavaScriptValue acceptTypes = JavaScriptValue.CreateArray((uint)request.AcceptTypes.Length);
                    for (int i = 0; i < request.AcceptTypes.Length; i++)
                    {
                        acceptTypes.SetIndexedProperty(JavaScriptValue.FromInt32(i), JavaScriptValue.FromString(request.AcceptTypes[i]));
                    }
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("acceptTypes"), acceptTypes, true);
                }

                if (request.QueryString != null)
                {
                    JavaScriptValue queryString = JavaScriptValue.CreateArray((uint)request.QueryString.Count);
                    for (int i = 0; i < request.QueryString.Count; i++)
                    {
                        JavaScriptValue qsItem = JavaScriptValue.CreateObject();

                        qsItem.SetProperty(JavaScriptPropertyId.FromString("name"), JavaScriptValue.FromString(request.QueryString.GetKey(i) ?? string.Empty), false);
                        qsItem.SetProperty(JavaScriptPropertyId.FromString("value"), JavaScriptValue.FromString(request.QueryString[i] ?? string.Empty), false);

                        queryString.SetIndexedProperty(JavaScriptValue.FromInt32(i), qsItem);
                    }
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("queryString"), queryString, true);
                }

                if (request.Headers != null)
                {
                    JavaScriptValue headers = JavaScriptValue.CreateArray((uint)request.Headers.Count);
                    for (int i = 0; i < request.Headers.Count; i++)
                    {
                        JavaScriptValue headerItem = JavaScriptValue.CreateObject();
                        headerItem.SetProperty(JavaScriptPropertyId.FromString("name"), JavaScriptValue.FromString(request.Headers.GetKey(i) ?? string.Empty), false);
                        headerItem.SetProperty(JavaScriptPropertyId.FromString("value"), JavaScriptValue.FromString(request.Headers[i] ?? string.Empty), false);

                        headers.SetIndexedProperty(JavaScriptValue.FromInt32(i), headerItem);
                    }
                    requestParams.SetProperty(JavaScriptPropertyId.FromString("headers"), headers, true);
                }

                // #todo
                // Stream InputStream
                // CookieCollection Cookies

                hostObject.SetProperty(JavaScriptPropertyId.FromString("request"), requestParams, true);
            }

            return(context);
        }