Ejemplo n.º 1
0
 internal static extern JavaScriptErrorCode JsRunScript(string script, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Ejemplo n.º 2
0
 internal static extern JavaScriptErrorCode JsRunSerializedScript(string script, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Ejemplo n.º 3
0
 /// <summary>
 ///     Runs a serialized script.
 /// </summary>
 /// <remarks>
 ///     Requires an active script context.
 /// </remarks>
 /// <param name="script">The source code of the serialized script.</param>
 /// <param name="buffer">The serialized script.</param>
 /// <param name="sourceContext">
 ///     A cookie identifying the script that can be used by script contexts that have debugging enabled.
 /// </param>
 /// <param name="sourceName">The location the script came from.</param>
 /// <returns>The result of the script, if any.</returns>
 public static JavaScriptValue RunScript(string script, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceName)
 {
     JavaScriptValue result;
     Native.ThrowIfError(Native.JsRunSerializedScript(script, buffer, sourceContext, sourceName, out result));
     return result;
 }
Ejemplo n.º 4
0
 internal static extern JavaScriptErrorCode JsRunSerializedScriptWithCallback(JavaScriptSerializedScriptLoadSourceCallback scriptLoadCallback,
     JavaScriptSerializedScriptUnloadCallback scriptUnloadCallback, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
 /// <summary>
 /// Instantiates the <see cref="ChakraJavaScriptExecutor"/>.
 /// </summary>
 public ChakraJavaScriptExecutor()
 {
     _runtime = JavaScriptRuntime.Create();
     _context = JavaScriptSourceContext.FromIntPtr(IntPtr.Zero);
     InitializeChakra();
 }
Ejemplo n.º 6
0
 /// <summary>
 ///     Parses a script and returns a <c>Function</c> representing the script.
 /// </summary>
 /// <remarks>
 ///     Requires an active script context.
 /// </remarks>
 /// <param name="script">The script to parse.</param>
 /// <param name="sourceContext">
 ///     A cookie identifying the script that can be used by script contexts that have debugging enabled.
 /// </param>
 /// <param name="sourceName">The location the script came from.</param>
 /// <returns>A <c>Function</c> representing the script code.</returns>
 public static JavaScriptValue ParseScript(string script, JavaScriptSourceContext sourceContext, string sourceName)
 {
     JavaScriptValue result;
     Native.ThrowIfError(Native.JsParseScript(script, sourceContext, sourceName, out result));
     return result;
 }
Ejemplo n.º 7
0
        public static extern JavaScriptErrorCode JsRunSerializedScriptWithCallback(JavaScriptSerializedScriptLoadSourceCallback scriptLoadCallback,

                                                                                   JavaScriptSerializedScriptUnloadCallback scriptUnloadCallback, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Ejemplo n.º 8
0
 public static extern JavaScriptErrorCode JsParseModuleSource(JavaScriptModuleRecord moduel, JavaScriptSourceContext sourceContext, byte[] script, uint scriptLength, JavaScriptParseModuleSourceFlags flags, out JavaScriptValue parseException);
Ejemplo n.º 9
0
 public static extern JavaScriptErrorCode JsRunScript(string script, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Ejemplo n.º 10
0
 public static extern JavaScriptErrorCode JsRunSerializedScript(string script, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
        private void button2_Click(object sender, EventArgs e)
        {
            JavaScriptRuntime       runtime;
            JavaScriptContext       context;
            JavaScriptSourceContext currentSourceContext = JavaScriptSourceContext.FromIntPtr(IntPtr.Zero);
            JavaScriptValue         jsValue;
            string szWelcomMessage;
            string szWhoamI;
            double nVersion;
            bool   bSuccess = false;

            try
            {
                string script = System.IO.File.ReadAllText("C:/Temp/Script/Sample02.js");


                runtime = JavaScriptRuntime.Create();
                context = runtime.CreateContext();
                JavaScriptContext.Current = context;
                JavaScriptContext.RunScript(script, currentSourceContext++, "");

                jsValue         = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWelcomMessage"));
                szWelcomMessage = jsValue.ToString();

                jsValue  = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWhoamI"));
                szWhoamI = jsValue.ToString();

                jsValue  = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("nVersion"));
                nVersion = jsValue.ToDouble();

                System.Diagnostics.Trace.WriteLine("szWelcomMessage = " + szWelcomMessage);
                System.Diagnostics.Trace.WriteLine("szWhoamI = " + szWhoamI);
                System.Diagnostics.Trace.WriteLine("nVersion = " + nVersion.ToString());



                jsValue = JavaScriptValue.FromString("New_Sample02.js");
                JavaScriptValue.GlobalObject.SetProperty(JavaScriptPropertyId.FromString("szWhoamI"), jsValue, true);


                jsValue         = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWelcomMessage"));
                szWelcomMessage = jsValue.ToString();

                jsValue  = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWhoamI"));
                szWhoamI = jsValue.ToString();

                jsValue  = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("nVersion"));
                nVersion = jsValue.ToDouble();

                System.Diagnostics.Trace.WriteLine("szWelcomMessage = " + szWelcomMessage);
                System.Diagnostics.Trace.WriteLine("szWhoamI = " + szWhoamI);
                System.Diagnostics.Trace.WriteLine("nVersion = " + nVersion.ToString());

                bSuccess = true;
            }
            finally
            {
                if (true == bSuccess)
                {
                    System.Diagnostics.Trace.WriteLine("Success");
                }
                else
                {
                    System.Diagnostics.Trace.WriteLine("Error");
                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            JavaScriptRuntime       runtime;
            JavaScriptContext       context;
            JavaScriptSourceContext currentSourceContext = JavaScriptSourceContext.FromIntPtr(IntPtr.Zero);
            JavaScriptValue         jsValue;
            string szWelcomMessage;
            string szWhoamI;


            JavaScriptValue returnValue;
            JavaScriptValue myfunc_1;
            JavaScriptValue myfunc_2;
            JavaScriptValue myfunc_3;
            JavaScriptValue myfunc_4;

            bool bSuccess = false;

            try
            {
                string script = System.IO.File.ReadAllText("C:/Temp/Script/Sample03.js");


                runtime = JavaScriptRuntime.Create();
                context = runtime.CreateContext();
                JavaScriptContext.Current = context;
                JavaScriptContext.RunScript(script, currentSourceContext++, "");

                jsValue         = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWelcomMessage"));
                szWelcomMessage = jsValue.ToString();

                jsValue  = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWhoamI"));
                szWhoamI = jsValue.ToString();

                System.Diagnostics.Trace.WriteLine("szWelcomMessage = " + szWelcomMessage);
                System.Diagnostics.Trace.WriteLine("szWhoamI = " + szWhoamI);



                // myfunc_1

                JavaScriptValue[] args1 = new JavaScriptValue[1] {
                    JavaScriptValue.FromString("self")
                };
                myfunc_1 = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("myfunc_1"));
                myfunc_1.CallFunction(args1);


                jsValue         = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWelcomMessage"));
                szWelcomMessage = jsValue.ToString();

                jsValue  = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWhoamI"));
                szWhoamI = jsValue.ToString();


                System.Diagnostics.Trace.WriteLine("szWelcomMessage = " + szWelcomMessage);
                System.Diagnostics.Trace.WriteLine("szWhoamI = " + szWhoamI);



                // myfunc_2
                JavaScriptValue[] args2 = new JavaScriptValue[1] {
                    JavaScriptValue.GlobalObject
                };
                myfunc_2    = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("myfunc_2"));
                returnValue = myfunc_2.CallFunction(args2);


                jsValue         = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWelcomMessage"));
                szWelcomMessage = jsValue.ToString();

                jsValue  = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWhoamI"));
                szWhoamI = jsValue.ToString();

                System.Diagnostics.Trace.WriteLine("Return Value = " + returnValue.ToString());
                System.Diagnostics.Trace.WriteLine("szWelcomMessage = " + szWelcomMessage);
                System.Diagnostics.Trace.WriteLine("szWhoamI = " + szWhoamI);


                // myfunc_3
                JavaScriptValue[] args3 = new JavaScriptValue[2] {
                    JavaScriptValue.GlobalObject, JavaScriptValue.FromString("myfunc_3")
                };
                myfunc_3    = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("myfunc_3"));
                returnValue = myfunc_3.CallFunction(args3);


                jsValue         = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWelcomMessage"));
                szWelcomMessage = jsValue.ToString();

                jsValue  = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWhoamI"));
                szWhoamI = jsValue.ToString();

                System.Diagnostics.Trace.WriteLine("Return Value[0] = " + (returnValue.GetIndexedProperty(JavaScriptValue.FromInt32(0))).ToString());
                System.Diagnostics.Trace.WriteLine("Return Value[1] = " + (returnValue.GetIndexedProperty(JavaScriptValue.FromInt32(1))).ToBoolean());
                System.Diagnostics.Trace.WriteLine("szWelcomMessage = " + szWelcomMessage);
                System.Diagnostics.Trace.WriteLine("szWhoamI = " + szWhoamI);


                // myfunc_4
                JavaScriptValue[] args4 = new JavaScriptValue[3] {
                    JavaScriptValue.GlobalObject, JavaScriptValue.FromString("myfunc_4"), JavaScriptValue.FromBoolean(false)
                };
                myfunc_4    = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("myfunc_4"));
                returnValue = myfunc_4.CallFunction(args4);


                jsValue         = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWelcomMessage"));
                szWelcomMessage = jsValue.ToString();

                jsValue  = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("szWhoamI"));
                szWhoamI = jsValue.ToString();

                System.Diagnostics.Trace.WriteLine("Return Value[0] = " + (returnValue.GetIndexedProperty(JavaScriptValue.FromInt32(0))).ToString());
                System.Diagnostics.Trace.WriteLine("Return Value[1] = " + (returnValue.GetIndexedProperty(JavaScriptValue.FromInt32(1))).ToBoolean());
                System.Diagnostics.Trace.WriteLine("szWelcomMessage = " + szWelcomMessage);
                System.Diagnostics.Trace.WriteLine("szWhoamI = " + szWhoamI);

                bSuccess = true;
            }
            finally
            {
                if (true == bSuccess)
                {
                    System.Diagnostics.Trace.WriteLine("Success");
                }
                else
                {
                    System.Diagnostics.Trace.WriteLine("Error");
                }
            }
        }
Ejemplo n.º 13
0
        public async Task Run(string[] arguments)
        {
            try
            {
                using (JavaScriptRuntime runtime = JavaScriptRuntime.Create())
                {
                    // Create a context. Note that if we had wanted to start debugging from the very
                    // beginning, we would have called JsStartDebugging right after context is create
                    JavaScriptContext context = runtime.CreateContext();

                    // Now set the execution context as being the current one on this thread.
                    using (new JavaScriptContext.Scope(context))
                    {
                        var hostObject = JavaScriptValue.CreateObject();

                        // Create an object called 'host' and set it on the global object.
                        var hostPropertyId = JavaScriptPropertyId.FromString("host");
                        JavaScriptValue.GlobalObject.SetProperty(hostPropertyId, hostObject, true);

                        // Register a bunch of callbacks on the 'host' object so that the JS code can call into C# code.
                        this.DefineHostCallback(hostObject, "echo", this.hostFunctions.EchoDelegate, IntPtr.Zero);
                        //this.DefineHostCallback(hostObject, "runScript", this.hostFunctions.RunScriptDelegate, IntPtr.Zero);

                        // Now register some async callbacks.
                        this.DefineHostCallback(hostObject, "doSuccessfulWork", this.hostFunctions.DoSuccessfulWorkDelegate, IntPtr.Zero);
                        this.DefineHostCallback(
                            hostObject,
                            "doUnsuccessfulWork",
                            this.hostFunctions.DoUnsuccessfulWorkDelegate,
                            IntPtr.Zero);
                        this.DefineHostCallback(hostObject, "getUrl", this.hostFunctions.GetUrlDelegate, IntPtr.Zero);

                        // Tell Chakra how to handle promises.
                        JavaScriptRuntime.SetPromiseContinuationCallback(this.jsTaskScheduler.PromiseContinuationCallback, IntPtr.Zero);

                        // Everything is setup, so we can go and use the engine now.
                        try
                        {
                            var javaScriptSourceContext = JavaScriptSourceContext.FromIntPtr(IntPtr.Zero);

                            // Load and execute the JavaScript file.
                            var script = File.ReadAllText(arguments[0]);
                            var result = JavaScriptContext.RunScript(
                                script,
                                javaScriptSourceContext + 0,
                                arguments[0]);

                            // Start pumping the task queue so that promise continuations will be processed.
                            // Note that this must be done after the task queue has been initially filled.
                            var completion = this.jsTaskScheduler.PumpMessages();

                            // If the result was a promise, convert it into a C# Task and await its result.
                            // Note that this could be simplified so that the
                            if (IsPromise(result))
                            {
                                Console.WriteLine("Script returned a promise, awaiting it.");
                                result = await this.ConvertPromiseToTask(result);
                            }

                            Console.WriteLine($"Script result: {result.ConvertToString().ToString()}");

                            // Call the 'sayHello' method on the object which was returned from the script.
                            await this.ConvertPromiseToTask(
                                result.GetProperty(JavaScriptPropertyId.FromString("sayHello"))
                                .CallFunction(JavaScriptValue.GlobalObject, JavaScriptValue.FromString("do a barrel roll!")));

                            // Call the 'add' method, which is not an async method
                            var addResult = result.GetProperty(JavaScriptPropertyId.FromString("add"))
                                            .CallFunction(
                                JavaScriptValue.GlobalObject,
                                JavaScriptValue.FromInt32(78),
                                JavaScriptValue.FromInt32(22));

                            Console.WriteLine($"In C# land: 78 + 22 = {addResult.ConvertToNumber().ToDouble()}");

                            // Wait for the task pump to complete.
                            await completion;
                        }
                        catch (JavaScriptScriptException exception)
                        {
                            var messageValue = exception.Error.GetProperty(JavaScriptPropertyId.FromString("message"));
                            Console.Error.WriteLine("exception: {0}", messageValue.ToString());
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine("failed to run script: {0}", e.Message);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("fatal error: internal error: {0}.", e.Message);
            }

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Ejemplo n.º 14
0
 private JavaScriptErrorCode FetchImportedModuleFromScript(JavaScriptSourceContext sourceContext, JavaScriptValue source, out JavaScriptModuleRecord result)
 {
     System.Diagnostics.Debug.WriteLine("FetchImportedModuleFromScriptDelegate start");
     result = new JavaScriptModuleRecord();
     return(JavaScriptErrorCode.NoError);
 }
Ejemplo n.º 15
0
 private JavaScriptErrorCode FetchImportedModuleFromScript(JavaScriptSourceContext sourceContext, JavaScriptValue source, out JavaScriptModuleRecord result)
 {
     // Debug.Log("FetchImportedModuleFromScriptDelegate start");
     result = new JavaScriptModuleRecord();
     return(JavaScriptErrorCode.NoError);
 }