Beispiel #1
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 passed in an IDebugApplication pointer here.
            //

            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);
        }
        public JavaScriptContext CreateContext(JavaScriptRuntime engine)
        {
            var context = engine.CreateContext();

            return(context);
        }
Beispiel #3
0
 void CreateContext()
 {
     _context = _runtime.CreateContext();
     _scope   = new JavaScriptContext.Scope(_context);
 }
 public void Reset()
 {
     Dispose();
     runtime = JavaScriptRuntime.Create();
     context = runtime.CreateContext();
 }
        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);
        }