Esempio n. 1
0
        static void TestCase2()
        {
            #if DEBUG
            JsBridge.dbugTestCallbacks();
            #endif
            //create js engine and context

            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext())
            {
                GC.Collect();
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Start();

                TestMe1 t1 = new TestMe1();

                for (int i = 2000; i >= 0; --i)
                {
                    ctx.SetVariableFromAny("x", t1);
                    object result = ctx.Execute("(function(){if(x.C()){return  x.B();}else{return 0;}})()");
                }
                stwatch.Stop();
                Console.WriteLine("met2 managed reflection:" + stwatch.ElapsedMilliseconds.ToString());
                //Assert.That(result, Is.EqualTo(100));
            }
        }
Esempio n. 2
0
 internal void InitJsEngine()
 {
     if (myengine == null)
     {
         var jstypeBuilder = new LayoutFarm.Scripting.MyJsTypeDefinitionBuilder();
         myengine = new JsEngine();
         myCtx = myengine.CreateContext(jstypeBuilder);
     }
     myCtx.SetVariableAutoWrap("document", HtmlDoc);
     myCtx.SetVariableAutoWrap("console", Console);
 }
Esempio n. 3
0
        static void TestCase1()
        {
            #if DEBUG
            JsBridge.dbugTestCallbacks();
            #endif

            JsTypeDefinition jstypedef = new JsTypeDefinition("AA");
            jstypedef.AddMember(new JsMethodDefinition("B", args =>
            {
                args.SetResult(100);
            }));
            jstypedef.AddMember(new JsMethodDefinition("C", args =>
            {
                args.SetResult(true);
            }));
            //===============================================================
            //create js engine and context
            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext())
            {

                if (!jstypedef.IsRegisterd)
                {
                    ctx.RegisterTypeDefinition(jstypedef);
                }
                GC.Collect();
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Start();

                TestMe1 t1 = new TestMe1();
                var proxy = ctx.CreateWrapper(t1, jstypedef);

                for (int i = 2000; i >= 0; --i)
                {
                    ctx.SetVariableFromAny("x", proxy);
                    object result = ctx.Execute("(function(){if(x.C()){return  x.B();}else{return 0;}})()");
                }
                //test value of
                object re = ctx.Execute("(function(){function myNumberType(n) {    this.number = n;}" +
                        "myNumberType.prototype.valueOf = function() {    return this.number;};" +
                        "myObj = new myNumberType(4);" +
                        "return myObj + 3;" +
                        "})()");
                stwatch.Stop();

                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
                //Assert.That(result, Is.EqualTo(100));
            }
        }
Esempio n. 4
0
        internal JsScript(int id, JsEngine engine, HandleRef engineHandle, JsConvert convert, string code, string name, Action<int> notifyDispose)
        {
            _id = id;
            _engine = engine;
            _notifyDispose = notifyDispose;

            _script = new HandleRef(this, jsscript_new(engineHandle));

            IntPtr v2 = jsscript_compile(_script, code, name);
            //JsValue v = jsscript_compile(_script, code, name);

            //object res = convert.FromJsValue(v);
            //Exception e = res as JsException;
            //if (e != null)
            //{
            //    throw e;
            //}
        }
Esempio n. 5
0
        public static void Main(string[] args)
        {
            // string lodash = File.ReadAllText(@"c:\lodash.js");
            using (JsEngine engine = new JsEngine())
            {
                //Stopwatch watch = new Stopwatch();
                //	watch.Start();
                JsScript script = engine.CompileScript("3+3");
                using (JsContext ctx = engine.CreateContext())
                {
                    ctx.Execute(script);
                }
            }

            debugtest dbg = new debugtest();

            //	Delegate.CreateDelegate()
            //Dictionary<string, object> values = new Dictionary<string, object>();

            //values["test"] = 333;
            while (true)
            {
                using (JsEngine js = new JsEngine(4, 32))
                {
                    using (JsContext context = js.CreateContext())
                    {
                        //context.SetVariable("dbg", dbg);
                        //object result = context.Execute("dbg.Write(dbg.valueOf());");
                        context.SetVariableFromAny("Debug", typeof(debugtest));

                        object result = context.Execute("Debug.BoolTest(3,4);");

                    }
                    GC.Collect();
                    js.DumpHeapStats();
                }
            }

            //context.SetVariable("values", values);
            //object result = context.Execute("dbg.runFunc(); values.test ? true : false;");

            //	object result =
            //	context.Execute("var obj = { test: 0 }; obj.ft = function (v) { dbg.Write(v); return 'from JS'; }; dbg.runFunc(obj.ft); dbg.write(obj.test);");

            //int a = 1;

            //	context.SetFunction("runfunc", new Func<int, bool>(Activate));
            //object result = context.Execute("runfunc(2);");
            //  }

            //		}
            //}
            /*using (JsEngine js = new JsEngine()) {
                using (JsContext context = js.CreateContext()) {
                    using (JsScript script = js.CompileScript("3 * 4")) {
                        object result = context.Execute(script, TimeSpan.FromHours(200));
                        Console.WriteLine(result);
                    }
                }
            }*/

            //return;
            /*
            using (JsEngine js = new JsEngine()) {
                using (JsContext context = js.CreateContext()) {
                    for (int i = 0; i < 10; i++) {
                        context.SetVariable("a", new Simple { N = i, S = (i * 10).ToString() });
                        Console.WriteLine(context.Execute("a.N+' '+a.S"));
                    }
                    Console.WriteLine(context.Execute("a.N+' '+a.X"));
                }
            }*/
        }
Esempio n. 6
0
        private void button10_Click(object sender, EventArgs e)
        {
            //very basic ***
            //-----------------
            //test tsc.js
            //this needs EspressoHostForTsc
            //-----------------
            string esprima_code = File.ReadAllText("d:\\projects\\Espresso\\js_tools\\tsc\\tsc_espr.js");
            StringBuilder stbuilder = new StringBuilder();
            stbuilder.Append(esprima_code);
            //-----------------

            int version = JsBridge.LibVersion;
            JsBridge.dbugTestCallbacks();
            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext(new MyJsTypeDefinitionBuilder()))
            {

                GC.Collect();
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Reset();
                stwatch.Start();

                var my_expr_ext = new EspressoHostForTsc();
                ctx.SetVariableAutoWrap("my_expr_ext", my_expr_ext);

                string testsrc = @"(function(){

                        // test1: general  compile through commamd line
                        // ts.executeCommandLine(['greeter.ts']);
                        //-------------------------------------------------
                        // test 2: generate ast
                        var filename=""greeter.ts"";  //example only
                        //parse
                        const sourceFile = ts.createSourceFile(filename,
                        my_expr_ext.ReadFile(filename),2, false);
                        //send output as json to managed host
                        my_expr_ext.ConsoleLog(JSON.stringify( sourceFile));
                    })()";
                stbuilder.Append(testsrc);
                ctx.Execute(stbuilder.ToString());

                stwatch.Stop();
                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
            }
        }
Esempio n. 7
0
        private void button9_Click(object sender, EventArgs e)
        {
            //------------------------
            //test esprima package
            //------------------------

            string esprima_code = File.ReadAllText("d:\\projects\\Espresso\\js_tools\\esprima\\esprima.js");
            StringBuilder stbuilder = new StringBuilder();
            stbuilder.Append(esprima_code);

            int version = JsBridge.LibVersion;
            JsBridge.dbugTestCallbacks();
            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext(new MyJsTypeDefinitionBuilder()))
            {

                GC.Collect();
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Reset();
                stwatch.Start();

                var ab = new AboutMe();
                ctx.SetVariableAutoWrap("aboutme1", ab);

                string testsrc = @"(function(){
                            var syntax= esprima.parse('var answer = 42');
                            //convert to json format and send to managed side
                            aboutme1.SetResult(JSON.stringify(syntax, null, 4));
                    })()";
                stbuilder.Append(testsrc);
                ctx.Execute(stbuilder.ToString());

                stwatch.Stop();
                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
            }
        }
Esempio n. 8
0
 internal NodeJsExecSession(JsEngine engine, JsContext ctx)
 {
     Engine  = engine;
     Context = ctx;
 }
Esempio n. 9
0
        private void button7_Click(object sender, EventArgs e)
        {
            #if DEBUG
            JsBridge.dbugTestCallbacks();
            #endif
            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext(new MyJsTypeDefinitionBuilder()))
            {

                GC.Collect();
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Reset();
                stwatch.Start();

                var ab = new AboutMe();
                ctx.SetVariableAutoWrap("x", ab);

                //string testsrc = "x.IsOK;";
                string testsrc = "x.NewAboutMe();";
                object result = ctx.Execute(testsrc);
                stwatch.Stop();

                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());

            }
        }
Esempio n. 10
0
        private void button5_Click(object sender, EventArgs e)
        {
            #if DEBUG
            JsBridge.dbugTestCallbacks();
            #endif
            JsTypeDefinition jstypedef = new JsTypeDefinition("AA");
            jstypedef.AddMember(new JsMethodDefinition("B", args =>
            {
                var argCount = args.ArgCount;
                var thisArg = args.GetThisArg();
                var arg0 = args.GetArgAsObject(0);
                args.SetResult((bool)arg0);

            }));
            jstypedef.AddMember(new JsMethodDefinition("C", args =>
            {
                args.SetResult(true);
            }));

            //-----------------------------------------------------
            jstypedef.AddMember(new JsPropertyDefinition("D",
                args =>
                {
                    var ab = new AboutMe();
                    args.SetResultAutoWrap(ab);
                },
                args =>
                {
                    //setter
                }));
            jstypedef.AddMember(new JsPropertyDefinition("E",
                args =>
                {   //getter
                    args.SetResult(250);
                },
                args =>
                {
                    //setter
                }));
            //===============================================================
            //create js engine and context
            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext())
            {

                ctx.RegisterTypeDefinition(jstypedef);

                GC.Collect();
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Reset();
                stwatch.Start();

                TestMe1 t1 = new TestMe1();

                INativeScriptable proxy = ctx.CreateWrapper(t1, jstypedef);
                ctx.SetVariable("x", proxy);

                string testsrc = "x.B(x.D.IsOK);";
                object result = ctx.Execute(testsrc);
                stwatch.Stop();

                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());

            }
        }
Esempio n. 11
0
        private void button3_Click(object sender, EventArgs e)
        {
            #if DEBUG
            JsBridge.dbugTestCallbacks();
            #endif
            JsTypeDefinition jstypedef = new JsTypeDefinition("AA");
            jstypedef.AddMember(new JsMethodDefinition("B", args =>
            {
                args.SetResult(100);
            }));
            jstypedef.AddMember(new JsMethodDefinition("C", args =>
            {
                args.SetResult(true);
            }));

            jstypedef.AddMember(new JsPropertyDefinition("D",
                args =>
                {   //getter
                    args.SetResult(true);
                },
                args =>
                {
                    //setter
                }));
            jstypedef.AddMember(new JsPropertyDefinition("E",
                args =>
                {   //getter
                    args.SetResult(250);
                },
                args =>
                {
                    //setter
                }));

            //===============================================================
            //create js engine and context
            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext())
            {

                ctx.RegisterTypeDefinition(jstypedef);

                GC.Collect();
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Reset();
                stwatch.Start();

                TestMe1 t1 = new TestMe1();
                var proxy = ctx.CreateWrapper(t1, jstypedef);

                //for (int i = 2000; i >= 0; --i)
                //{
                ctx.SetVariableFromAny("x", proxy);
                //object result = ctx.Execute("(function(){if(x.C()){return  x.B();}else{return 0;}})()");
                object result = ctx.Execute("(function(){if(x.D){ x.E=300; return  x.B();}else{return 0;}})()");

                //}
                stwatch.Stop();

                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
                //Assert.That(result, Is.EqualTo(100));
            }
        }
Esempio n. 12
0
        private void button1_Click(object sender, EventArgs e)
        {
            #if DEBUG
            JsBridge.dbugTestCallbacks();
            #endif

            JsTypeDefinition jstypedef = new JsTypeDefinition("AA");
            jstypedef.AddMember(new JsMethodDefinition("B", args =>
            {
                args.SetResult(100);
            }));
            jstypedef.AddMember(new JsMethodDefinition("C", args =>
            {
                args.SetResult(true);
            }));
            //===============================================================
            //create js engine and context
            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext())
            {

                if (!jstypedef.IsRegisterd)
                {
                    ctx.RegisterTypeDefinition(jstypedef);
                }
                GC.Collect();
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Start();

                TestMe1 t1 = new TestMe1();
                var proxy = ctx.CreateWrapper(t1, jstypedef);

                for (int i = 2000; i >= 0; --i)
                {
                    ctx.SetVariableFromAny("x", proxy);
                    object result = ctx.Execute("(function(){if(x.C()){return x.B();}else{return 0;}})()");
                }
                //for (int i = 1; i >= 0; --i)
                //{
                //    ctx.SetVariableFromAny("x", proxy);
                //    object result = ctx.Execute(@"
                //    var http = require('http');
                //    var server = http.createServer(function(req, res) {
                //    res.writeHead(200);
                //    res.end('Hello Http');
                //    });
                //    server.listen(8080);
                //    ");
                //}
                stwatch.Stop();

                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
                //Assert.That(result, Is.EqualTo(100));
            }
        }
Esempio n. 13
0
        private void button11_Click(object sender, EventArgs e)
        {
            //very basic ***
            //-----------------
            //test loki.js
            //this needs EspressoHostForTsc
            //-----------------
            string esprima_code = File.ReadAllText("d:\\projects\\Espresso\\js_tools\\lokijs\\lokijs.js");
            StringBuilder stbuilder = new StringBuilder();
            stbuilder.Append(esprima_code);
            //-----------------

            int version = JsBridge.LibVersion;
            JsBridge.dbugTestCallbacks();
            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext(new MyJsTypeDefinitionBuilder()))
            {

                GC.Collect();
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Reset();
                stwatch.Start();

                var my_expr_ext = new EspressoHostForLoki();
                ctx.SetVariableAutoWrap("my_expr_ext", my_expr_ext);
                string testsrc = @"
                    function require(file){
                        my_expr_ext.Require(file);
                    }
                    (function(){
                       var db = new loki('loki.json');
                       //test log
                       my_expr_ext.ConsoleLog(db);
                       db.save();
                    })()";
                stbuilder.Append(testsrc);
                ctx.Execute(stbuilder.ToString());

                stwatch.Stop();
                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
            }
        }
Esempio n. 14
0
        private void button5_Click(object sender, EventArgs e)
        {

            //1. init html
            var fileContent = "<html><body><div id=\"a\">A</div><div id=\"b\" style=\"background-color:blue\">B</div></body></html>";
            easeViewport.LoadHtmlString("", fileContent);
            //----------------------------------------------------------------
            //after load html page 

            //test javascript ... 

#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //===============================================================

            //2. access dom  

            var webdoc = easeViewport.GetHtmlDom() as IHtmlDocument;
            //create js engine and context
            var jstypeBuilder = new LayoutFarm.Scripting.MyJsTypeDefinitionBuilder();
            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext(jstypeBuilder))
            {
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Reset();
                stwatch.Start();
                ctx.SetVariableAutoWrap("document", webdoc);
                string testsrc1 = "document.getElementById('a');";
                object domNodeA = ctx.Execute(testsrc1);
                string testsrc2 = "document.getElementById('b');";
                object domNodeB = ctx.Execute(testsrc2);
                stwatch.Stop();
                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
            }

            ////3. get element by id 
            //var domNodeA = webdoc.GetElementById("a");
            //var domNodeB = webdoc.GetElementById("b");

            //domNodeA.AddTextContent("Hello from A");
            //domNodeB.AddChild("div", div =>
            //{
            //    div.SetAttribute("style", "background-color:yellow");
            //    div.AddTextContent("Hello from B");
            //});

            //domNodeB.AttachMouseDownEvent(ev =>
            //{
            //    var domB = new EaseDomElement(domNodeB);
            //    domB.SetBackgroundColor(Color.Red);
            //    ev.StopPropagation();
            //    //domNodeB.SetAttribute("style", "background-color:red");
            //});
            //domNodeB.AttachMouseUpEvent(ev =>
            //{
            //    var domB = new EaseDomElement(domNodeB);
            //    domB.SetBackgroundColor(Color.Yellow);
            //    ev.StopPropagation();
            //    //domNodeB.SetAttribute("style", "background-color:red");
            //}); 
        }
Esempio n. 15
0
        private void button8_Click(object sender, EventArgs e)
        {
            int version = JsBridge.LibVersion;
            JsBridge.dbugTestCallbacks();
            using (JsEngine engine = new JsEngine())
            using (JsContext ctx = engine.CreateContext(new MyJsTypeDefinitionBuilder()))
            {

                GC.Collect();
                System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
                stwatch.Reset();
                stwatch.Start();

                var ab = new AboutMe();
                ctx.SetVariableAutoWrap("x", ab);

                string testsrc = @"(function(){
                    x.AttachEvent('mousedown',function(evArgs){});
                    x.FireEventMouseDown({});
                })()";
                object result = ctx.Execute(testsrc);
                stwatch.Stop();

                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
            }
        }
Esempio n. 16
0
        private void button7_Click(object sender, EventArgs e)
        {

            //1. init html
            var initHtml = "<html><script>function doc_ready(){console.log('doc_ready');}</script><body onload=\"doc_ready()\"><div id=\"a\">A</div><div id=\"b\" style=\"background-color:blue\">B</div><div id=\"c\">c_node</div></body></html>";
            easeViewport.LoadHtmlString("", initHtml);
            //----------------------------------------------------------------
            //after load html page 

            //load v8 if ready

#if DEBUG
            JsBridge.dbugTestCallbacks();
#endif
            //===============================================================

            //2. access dom  

            var webdoc = easeViewport.GetHtmlDom() as LayoutFarm.WebDom.IHtmlDocument;
            //create js engine and context
            if (myengine == null)
            {
                var jstypeBuilder = new LayoutFarm.Scripting.MyJsTypeDefinitionBuilder();
                myengine = new JsEngine();
                myCtx = myengine.CreateContext(jstypeBuilder);
            }

            System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch();
            stwatch.Reset();
            stwatch.Start();
            myCtx.SetVariableAutoWrap("document", webdoc);
            myCtx.SetVariableAutoWrap("console", myWbConsole);
            string simplejs = @"
                    (function(){
                        console.log('hello world!');
                        var domNodeA = document.getElementById('a');
                        var domNodeB = document.getElementById('b');
                        var domNodeC = document.getElementById('c');

                        var newText1 = document.createTextNode('... says hello world!');
                        domNodeA.appendChild(newText1);

                        for(var i=0;i<10;++i){
                            var newText2= document.createTextNode(i.toString());
                            domNodeA.appendChild(newText2);       
                        } 

                        var newDivNode= document.createElement('div');
                        newDivNode.appendChild(document.createTextNode('new div'));
                        newDivNode.attachEventListener('mousedown',function(){console.log('new div');});
                        domNodeB.appendChild(newDivNode);    
                        
                        domNodeC.innerHTML='<div> from inner html <span> from span</span> </div>';
                        console.log(domNodeC.innerHTML);
                    })();
                ";
            object testResult = myCtx.Execute(simplejs);
            stwatch.Stop();
            Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
        }
Esempio n. 17
0
        static void Main()
        {
            //-----------------------------------
            //1.
            //after we build nodejs in dll version
            //we will get node.dll
            //then just copy it to another name 'libespr'
            string libEspr = @"C:\projects\node-v6.7.0\Release\libespr.dll";
            if (File.Exists(libEspr))
            {
                //delete the old one
                File.Delete(libEspr);
            }
            File.Copy(
               @"C:\projects\node-v6.7.0\Release\node.dll", //from
               libEspr);
            //-----------------------------------
            //2. load libespr.dll (node.dll)
            IntPtr intptr = LoadLibrary(libEspr);
            int errCode = GetLastError();
            #if DEBUG
            JsBridge.dbugTestCallbacks();
            #endif
            //------------
            JsEngine.RunJsEngine((IntPtr nativeEngine, IntPtr nativeContext) =>
            {

                JsEngine eng = new JsEngine(nativeEngine);
                JsContext ctx = eng.CreateContext(nativeContext);
                //-------------
                //this LibEspressoClass object is need,
                //so node can talk with us,
                //-------------
                JsTypeDefinition jstypedef = new JsTypeDefinition("LibEspressoClass");
                jstypedef.AddMember(new JsMethodDefinition("LoadMainSrcFile", args =>
                {
                    string filedata = @"var http = require('http');
                                                (function t(){
                                                    console.log('hello from EspressoCup');
                                                    var server = http.createServer(function(req, res) {
                                                    res.writeHead(200);
                                                    res.end('Hello! from EspressoCup');
                                                    });
                                                    server.listen(8080,'localhost');
                                                })();";
                    args.SetResult(filedata);
                }));
                jstypedef.AddMember(new JsMethodDefinition("C", args =>
                {

                    args.SetResult(true);
                }));
                jstypedef.AddMember(new JsMethodDefinition("E", args =>
                {
                    args.SetResult(true);
                }));
                if (!jstypedef.IsRegisterd)
                {
                    ctx.RegisterTypeDefinition(jstypedef);
                }
                //----------
                //then register this as x***
                //this object is just an instance for reference
                ctx.SetVariableFromAny("LibEspresso",
                      ctx.CreateWrapper(new object(), jstypedef));
            });
        }