Exemple #1
0
        public override void Setup()
        {
            var settings = new JavaScriptRuntimeSettings();

            runtime_ = new JavaScriptRuntime(settings);
            engine_  = runtime_.CreateEngine();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            using (var runtime = new JavaScriptRuntime())
                using (var engine = runtime.CreateEngine())
                {
                    engine.SetGlobalFunction("echo", Echo);
                    var fn = engine.EvaluateScriptText(@"(function() {
    echo('{0}, {1}!', 'Hello', 'world');
})();");
                    fn.Invoke(Enumerable.Empty <JavaScriptValue>());

                    dynamic fnAsDynamic = fn;
                    fnAsDynamic.foo = 24;
                    dynamic global = engine.GlobalObject;
                    global.echo("{0}, {1}, via dynamic!", "Hey there", "world");

                    dynamic echo = global.echo;
                    echo("Whoa, {0}, that {1} {2}???", "world", "really", "worked");

                    foreach (dynamic name in global.Object.getOwnPropertyNames(global))
                    {
                        echo(name);
                    }
                }
            Console.ReadLine();
        }
Exemple #3
0
        public void Initialize()
        {
            var settings = new JavaScriptRuntimeSettings();

            runtime_ = new JavaScriptRuntime(settings);
            engine_  = runtime_.CreateEngine();
        }
 public async Task Setup()
 {
     await DispatchContainer.GlobalDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
     {
         runtime_ = new JavaScriptRuntime();
         engine_  = runtime_.CreateEngine();
     });
 }
        public override void Setup()
        {
            var settings = new JavaScriptRuntimeSettings()
            {
                AllowScriptInterrupt = true,
            };

            runtime_ = new JavaScriptRuntime(settings);
            engine_  = runtime_.CreateEngine();
        }
Exemple #6
0
        private void Eval_Click(object sender, RoutedEventArgs e)
        {
            var settings = new JavaScriptRuntimeSettings();
            var runtime  = new JavaScriptRuntime(settings);
            var engine   = runtime.CreateEngine();

            var result = engine.Execute(new Microsoft.Scripting.ScriptSource("[eval code]", input.Text));

            output.Text = result.ToString();
        }
        public void Setup()
        {
            var settings = new JavaScriptRuntimeSettings()
            {
                EnableIdleProcessing = true,
            };

            runtime_ = new JavaScriptRuntime(settings);
            engine_  = runtime_.CreateEngine();
        }
Exemple #8
0
        internal static void RunEval(string code, PipeStream pipeStream, StreamWriter writer)
        {
            using (JavaScriptRuntime runtime = new JavaScriptRuntime())
                using (JavaScriptEngine engine = runtime.CreateEngine())
                {
                    try
                    {
                        using (engine.AcquireContext())
                        {
                            engine.AddTypeToGlobal <JSConsole>();
                            engine.AddTypeToGlobal <XMLHttpRequest>();
                            engine.SetGlobalVariable("tools", engine.Converter.FromObject(new Tools()));
                            engine.SetGlobalVariable("console", engine.Converter.FromObject(new JSConsole()));

                            engine.SetGlobalFunction("get", JsGet);
                            engine.SetGlobalFunction("post", JsPost);
                            engine.SetGlobalFunction("atob", JsAtob);
                            engine.SetGlobalFunction("btoa", JsBtoa);

                            try
                            {
                                var fn = engine.EvaluateScriptText($@"(function() {{ {code} }})();");
                                var v  = fn.Invoke(Enumerable.Empty <JavaScriptValue>());

                                if (engine.HasException)
                                {
                                    var e = engine.GetAndClearException();
                                    writer.WriteLine(JsonConvert.SerializeObject(engine.Converter.ToObject(e)));
                                }
                                else
                                {
                                    string o = engine.Converter.ToString(v);
                                    writer.WriteLine(JsonConvert.SerializeObject(o));
                                }
                            }
                            catch (Exception ex)
                            {
                                if (engine.HasException)
                                {
                                    var e = engine.GetAndClearException();
                                    writer.WriteLine(JsonConvert.SerializeObject(engine.Converter.ToObject(e)));
                                }
                                else
                                {
                                    writer.WriteLine(JsonConvert.SerializeObject(ex));
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        writer.WriteLine(JsonConvert.SerializeObject(ex));
                    }
                }
        }
        public override void Setup()
        {
            runtime_ = new JavaScriptRuntime(new JavaScriptRuntimeSettings());
            engine_  = runtime_.CreateEngine();

            var baseline = new ScriptSource("test://init.js", @"(function(global) {
    global.buffer = new ArrayBuffer(1024);
    global.typedArray = new Uint8ClampedArray(buffer);
    global.dataView = new DataView(buffer, 1);
})(this);");

            engine_.Execute(baseline);
            buffer_     = (JavaScriptArrayBuffer)engine_.GetGlobalVariable("buffer");
            typedArray_ = (JavaScriptTypedArray)engine_.GetGlobalVariable("typedArray");
            dataView_   = (JavaScriptDataView)engine_.GetGlobalVariable("dataView");
        }
Exemple #10
0
        public void TestMethod1()
        {
            bool ok = false;
            JavaScriptCallableFunction callback = (eng, construct, thisObj, args) =>
            {
                ok = true;
                return(eng.UndefinedValue);
            };

            using (var rt = new JavaScriptRuntime())
            {
                rt.MemoryChanging += Rt_MemoryChanging;
                using (var eng = rt.CreateEngine())
                {
                    eng.SetGlobalFunction("hitIt", callback);

                    eng.Execute(new ScriptSource("[eval code]", "hitIt();"));
                }
            }
            Assert.IsTrue(ok);
        }
Exemple #11
0
        static void Main(string[] args)
        {
            using (var runtime = new JavaScriptRuntime())
            {
                runtime.MemoryChanging += Runtime_MemoryChanging;

                using (var engine = runtime.CreateEngine())
                {
                    using (var context = engine.AcquireContext())
                    {
                        engine.SetGlobalFunction("echo", Echo);
                        engine.AddTypeToGlobal <Point3D>();
                        engine.AddTypeToGlobal <Point>();
                        engine.AddTypeToGlobal <Toaster>();
                        engine.AddTypeToGlobal <ToasterOven>();
                        var pt = new Point3D {
                            X = 18, Y = 27, Z = -1
                        };
                        //engine.SetGlobalVariable("pt", engine.Converter.FromObject(pt));
                        engine.RuntimeExceptionRaised += (sender, e) =>
                        {
                            dynamic error = engine.GetAndClearException();
                            dynamic glob  = engine.GlobalObject;
                            var     color = Console.ForegroundColor;
                            Console.ForegroundColor = ConsoleColor.Red;
                            var err = glob.JSON.stringify(error);
                            if ((string)err == "{}")
                            {
                                err = engine.Converter.ToString(error);
                            }
                            Console.WriteLine("Script error occurred: {0}", (string)err);
                            Console.ForegroundColor = color;
                        };

                        var fn = engine.EvaluateScriptText(@"(function() {
    var t = new ToasterOven();
    t.addEventListener('toastcompleted', function(e) {
        echo('Toast is done!');
        echo('{0}', JSON.stringify(e));
    });
    t.addEventListener('loaftoasted', function(e) {
        echo('Loaf is done!');
        echo('{0}', JSON.stringify(e.e));
        echo('Cooked {0} pieces', e.e.PiecesCookied);
    });
    t.StartToasting();

    var o = new Point3D(1, 2, 3);
    echo(o.toString());
    o.X = 254;
    echo('{0}', o.X);
    o.Y = 189;
    o.Z = -254.341;
    echo('o after mutation? {0}', o.ToString());
    echo('{0}, {1}!', 'Hello', 'world');
    //echo('{0}', pt.X);
    //echo('{0}', pt.Y);
    //echo('{0}', pt.ToString());
    //pt.Y = 207;
    //echo('{0}', pt.ToString());
})();");
                        fn.Invoke(Enumerable.Empty <JavaScriptValue>());

                        dynamic fnAsDynamic = fn;
                        fnAsDynamic.foo = 24;
                        dynamic global = engine.GlobalObject;
                        global.echo("{0}, {1}, via dynamic!", "Hey there", "world");

                        dynamic echo = global.echo;
                        echo("Whoa, {0}, that {1} {2}???", "world", "really", "worked");

                        foreach (dynamic name in global.Object.getOwnPropertyNames(global))
                        {
                            echo(name);
                        }
                    } // release context

                    Console.ReadLine();
                }
            }
        }
 public override void Setup()
 {
     runtime_ = new JavaScriptRuntime();
     engine_  = runtime_.CreateEngine();
 }