protected override JsTypeDefinition OnBuildRequest(Type t)
        {
            JsTypeDefinition typedefinition = new JsTypeDefinition(t.Name);

            //only instance /public method /prop***
            //MethodInfo[] methods = t.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            var methods = t.GetRuntimeMethods();
            foreach (var met in methods)
            {
                if(!met.IsStatic && met.IsPublic)
                {
                    typedefinition.AddMember(new JsMethodDefinition(met.Name, met));
                }
            }

            //var properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            var properties = t.GetRuntimeProperties();
            //TODO finding GetProperties with BindingFlags
            foreach (var property in properties)
            {
                typedefinition.AddMember(new JsPropertyDefinition(property.Name, property)); 
            }

            return typedefinition;
        }
        protected override JsTypeDefinition OnBuildRequest(Type t)
        {
            //find member that has JsPropertyAttribute or JsMethodAttribute
            JsTypeDefinition typedefinition = new JsTypeDefinition(t.Name);

            //only instance /public method /prop***
            var methods = t.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

            foreach (var met in methods)
            {
                var customAttrs = met.GetCustomAttributes(typeOfJsMethodAttr, false);
                if (customAttrs != null && customAttrs.Length > 0)
                {
                    var attr = customAttrs[0] as JsMethodAttribute;
                    typedefinition.AddMember(new JsMethodDefinition(attr.Name ?? met.Name, met));
                }
            }

            var properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

            foreach (var property in properties)
            {
                var customAttrs = property.GetCustomAttributes(typeOfJsPropertyAttr, false);
                if (customAttrs != null && customAttrs.Length > 0)
                {
                    var attr = customAttrs[0] as JsPropertyAttribute;
                    typedefinition.AddMember(new JsPropertyDefinition(attr.Name ?? property.Name, property));
                }
            }

            return(typedefinition);
        }
        protected override JsTypeDefinition OnBuildRequest(Type t)
        {
            //find member that has JsPropertyAttribute or JsMethodAttribute
            JsTypeDefinition typedefinition = new JsTypeDefinition(t.Name);

            //only instance /public method /prop***
            var methods = t.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            foreach (var met in methods)
            {
                var customAttrs = met.GetCustomAttributes(typeOfJsMethodAttr, false);
                if (customAttrs != null && customAttrs.Length > 0)
                {
                    var attr = customAttrs[0] as JsMethodAttribute;
                    typedefinition.AddMember(new JsMethodDefinition(attr.Name ?? met.Name, met));
                }
            }

            var properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            foreach (var property in properties)
            {
                var customAttrs = property.GetCustomAttributes(typeOfJsPropertyAttr, false);
                if (customAttrs != null && customAttrs.Length > 0)
                {
                    var attr = customAttrs[0] as JsPropertyAttribute;
                    typedefinition.AddMember(new JsPropertyDefinition(attr.Name ?? property.Name, property));
                }
            }

            return typedefinition;
        }
Example #4
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));
                }
        }
Example #5
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));
            }
        }
Example #6
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));
                }
        }
Example #7
0
        protected override JsTypeDefinition OnBuildRequest(Type actualType)
        {
#if DEBUG
            if (actualType.IsInterface)
            {
                //should not occurs since we send this as actual type ***
                throw new NotSupportedException();
            }
#endif
            //********************
            //this is sample only
            //you can rewrite this yourself
            //********************
            //TODO: review, typename collision ,
            //use fullname instead ?
            JsTypeDefinition typedefinition = new JsTypeDefinition(actualType.Name);
            AddTypeDefinitionDetail(actualType, typedefinition);
            return(typedefinition);
        }
Example #8
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();
                    //wrap t1 with custom js type definition
                    INativeScriptable proxy = ctx.CreateWrapper(t1, jstypedef);
                    ctx.SetVariableFromAny("x", proxy);
                    for (int i = 2000; i >= 0; --i)
                    {
                        object result = ctx.Execute("(function(){if(x.C()){return x.B();}else{return 0;}})()");
                    }

                    stwatch.Stop();

                    Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
                }
        }
        protected override JsTypeDefinition OnBuildRequest(Type t)
        {
            JsTypeDefinition typedefinition = new JsTypeDefinition(t.Name);

            //only instance /public method /prop***
            var methods = t.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            foreach (var met in methods)
            {
                typedefinition.AddMember(new JsMethodDefinition(met.Name, met));
            }

            var properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            foreach (var property in properties)
            {
                typedefinition.AddMember(new JsPropertyDefinition(property.Name, property));
            }

            return typedefinition;
        }
Example #10
0
        private void ScriptThread(object obj)
        {
            workQueue.Enqueue(InitializeJsGlobals);
            NodeJsEngine.Run(Debugger.IsAttached ? new string[] { "--inspect", "hello.espr" } : new string[] { "hello.espr" },
            (eng, ctx) =>
            {
                _engine = eng;
                _context = ctx;

                JsTypeDefinition jstypedef = new JsTypeDefinition("LibEspressoClass");
                jstypedef.AddMember(new JsMethodDefinition("LoadMainSrcFile", args =>
                {
                    args.SetResult(@"
function MainLoop() {
    LibEspresso.Next();
    setImmediate(MainLoop);
}
MainLoop();");
                }));

                jstypedef.AddMember(new JsMethodDefinition("Log", args =>
                {
                    Console.WriteLine(args.GetArgAsObject(0));
                }));

                jstypedef.AddMember(new JsMethodDefinition("Next", args =>
                {
                    //call from js server
                    System.Action work;
                    if (workQueue.TryDequeue(out work))
                    {
                        work();
                    }
                }));
                _context.RegisterTypeDefinition(jstypedef);
                _context.SetVariableFromAny("LibEspresso", _context.CreateWrapper(new object(), jstypedef));
            });
        }
Example #11
0
        public NativeJsInstanceProxy CreateProxyForObject(object o, JsTypeDefinition jsTypeDefinition)
        {
            NativeJsInstanceProxy found;
            if (this.createdWrappers.TryGetValue(o, out found))
            {
                return found;
            }

            var proxyObject = new NativeJsInstanceProxy(
                nativeRefList.Count,
                o,
                jsTypeDefinition);

            nativeRefList.Add(proxyObject);
            this.createdWrappers.Add(o, proxyObject);

            //register
            NativeV8JsInterOp.CreateNativePart(ownerContext, proxyObject);
            return proxyObject;
        }
Example #12
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));
            });
        }
Example #13
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 currentdir = System.IO.Directory.GetCurrentDirectory();
            string libEspr    = @"../../../node-v8.4.0/Release/libespr.dll";

            if (File.Exists(libEspr))
            {
                //delete the old one
                File.Delete(libEspr);
            }
            File.Copy(
                @"../../../node-v8.4.0/Release/node.dll", //from
                libEspr);
            //-----------------------------------
            //2. load libespr.dll (node.dll)

            IntPtr intptr     = LoadLibrary(libEspr);
            int    errCode    = GetLastError();
            int    libesprVer = JsBridge.LibVersion;

#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 Espresso-ND');
	                                                var server = http.createServer(function(req, res) {
                                                    res.writeHead(200);
                                                    res.end('Hello! from Espresso-ND');
                                                    });
                                                    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));
            });

            string userInput = Console.ReadLine();
        }
Example #14
0
        private void button4_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(100);
            }));
            jstypedef.AddMember(new JsMethodDefinition("C", args =>
            {
                args.SetResult(true);
            }));

            //-----------------------------------------------------
            jstypedef.AddMember(new JsPropertyDefinition("D",
                args =>
                {
                    //getter
                    TestMe1 t2 = new TestMe1();
                    args.SetResultObj(t2, jstypedef);

                },
                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 = "(function(){if(x.C()){return  x.B();}else{return 0;}})()";
                //string testsrc = "(function(){if(x.D != null){ x.E=300; return  x.B();}else{return 0;}})()";
                string testsrc = "x.B(x.D,15);";

                object result = ctx.Execute(testsrc);
                stwatch.Stop();

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

            }
        }
Example #15
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 =>
            {
                int 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);";
                    string testsrc = "x.B(x.D.GetOK());";
                    object result  = ctx.Execute(testsrc);
                    stwatch.Stop();

                    Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
                }
        }
Example #16
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));
                }
        }
Example #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 currentdir = System.IO.Directory.GetCurrentDirectory();
            string libEspr    = @"../../../node-v8.1.4/Release/libespr.dll";

            if (File.Exists(libEspr))
            {
                //delete the old one
                File.Delete(libEspr);
            }
            File.Copy(
                @"../../../node-v8.1.4/Release/node.dll", //from
                libEspr);
            //-----------------------------------
            //2. load libespr.dll (node.dll)

            IntPtr intptr     = LoadLibrary(libEspr);
            int    errCode    = GetLastError();
            int    libesprVer = JsBridge.LibVersion;

            //change working dir to target app and run
            //test with socket.io's chat sample
            System.IO.Directory.SetCurrentDirectory(@"../../../socket.io/examples/chat");

#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 =>
                {
                    //since this is sample socket io app
                    string filedata = File.ReadAllText("index.js");
                    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));
            });

            string userInput = Console.ReadLine();
        }
Example #18
0
File: Form1.cs Project: killf/V8Net
        private void button5_Click(object sender, EventArgs e)
        {
            JsBridge.dbugTestCallbacks();
            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());

            }
        }
Example #19
0
        /// <summary>
        /// add detail member to js type definition
        /// </summary>
        /// <param name="source"></param>
        /// <param name="targetTypeDef"></param>
        /// <returns></returns>
        void AddTypeDefinitionDetail(Type source, JsTypeDefinition targetTypeDef)
        {
            //------------------------------
            //this is sample only ***
            //------------------------------

            JsTypeAttribute foundJsTypeAttr = FindSingleCustomAttr <JsTypeAttribute>(source);

            Type[] interfaces = source.GetInterfaces();
            Dictionary <string, JsPropertyDefinition> declarProps = new Dictionary <string, JsPropertyDefinition>();

            for (int i = interfaces.Length - 1; i >= 0; --i)
            {
                Type i_interface = interfaces[i];
                //check if an interface has Js...attribute or not
                JsTypeAttribute foundJsTypeAttr2 = FindSingleCustomAttr <JsTypeAttribute>(i_interface);
                if (foundJsTypeAttr2 != null)
                {
                    InterfaceMapping interfaceMapping = source.GetInterfaceMap(i_interface);
                    //all members is
                    int m = interfaceMapping.InterfaceMethods.Length;
                    for (int n = 0; n < m; ++n)
                    {
                        MethodInfo interface_met = interfaceMapping.InterfaceMethods[n];
                        MethodInfo target_met    = interfaceMapping.TargetMethods[n];
                        if (!target_met.IsPublic)
                        {
                            //if this is public we will add it later***
                            if (target_met.IsSpecialName)
                            {
                                //eg. property ?
                                string metName = GetProperMemberName(target_met);
                                if (metName.StartsWith("get_"))
                                {
                                    string onlyPropName = metName.Substring(4);
                                    //property get
                                    JsPropertyDefinition existingProp;
                                    if (!declarProps.TryGetValue(onlyPropName, out existingProp))
                                    {
                                        existingProp = new JsPropertyDefinition(onlyPropName);
                                        declarProps.Add(onlyPropName, existingProp);
                                    }
                                    existingProp.GetterMethod = new JsPropertyGetDefinition(metName, target_met);
                                }
                                else if (metName.StartsWith("set_"))
                                {
                                    //property set
                                    string onlyPropName = metName.Substring(4);
                                    //property get
                                    JsPropertyDefinition existingProp;
                                    if (!declarProps.TryGetValue(onlyPropName, out existingProp))
                                    {
                                        existingProp = new JsPropertyDefinition(onlyPropName);
                                        declarProps.Add(onlyPropName, existingProp);
                                    }
                                    existingProp.SetterMethod = new JsPropertySetDefinition(metName, target_met);
                                }
                            }
                            else
                            {
                                JsMethodAttribute foundJsMetAttr = FindSingleCustomAttr <JsMethodAttribute>(target_met);
                                string            metName;
                                if (foundJsMetAttr != null)
                                {
                                    metName = foundJsMetAttr.Name ?? GetProperMemberName(target_met);
                                }
                                else
                                {
                                    metName = GetProperMemberName(target_met);
                                }
                                targetTypeDef.AddMember(new JsMethodDefinition(metName, target_met));
                            }
                        }
                    }
                }
            }
            //------------------------------
            int propCount = declarProps.Count;

            if (declarProps.Count > 0)
            {
                foreach (JsPropertyDefinition prop in declarProps.Values)
                {
                    targetTypeDef.AddMember(prop);
                }
            }
            //------------------------------
            MethodInfo[] methods = source.GetMethods(
                System.Reflection.BindingFlags.Instance |
                System.Reflection.BindingFlags.Public);

            foreach (var met in methods)
            {
                if (met.IsSpecialName)
                {
                    continue;
                }
                JsMethodAttribute foundJsMetAttr = FindSingleCustomAttr <JsMethodAttribute>(met);
                if (foundJsMetAttr != null)
                {
                    targetTypeDef.AddMember(new JsMethodDefinition(foundJsMetAttr.Name ?? GetProperMemberName(met), met));
                }
                //in this version,
                //if not found js attr, -> not expose it
            }
            var properties = source.GetProperties(System.Reflection.BindingFlags.Instance
                                                  | System.Reflection.BindingFlags.Public);

            foreach (var property in properties)
            {
                JsPropertyAttribute foundJsPropAttr = FindSingleCustomAttr <JsPropertyAttribute>(property);
                if (foundJsPropAttr != null)
                {
                    targetTypeDef.AddMember(new JsPropertyDefinition(foundJsPropAttr.Name ?? GetProperMemberName(property), property));
                }
                //in this version,
                //if not found js attr, -> not expose it
            }
        }
Example #20
0
 public NativeJsInstanceProxy(int mIndex, object wrapObject, JsTypeDefinition jsTypeDef)
     : base(mIndex, wrapObject)
 {
     this.jsTypeDef = jsTypeDef;
 }
Example #21
0
        public static unsafe void RegisterTypeDef(JsContext context, JsTypeDefinition jsTypeDefinition)
        {
            INativeRef proxObject = jsTypeDefinition.nativeProxy;
            byte[] finalBuffer = null;
            using (MemoryStream ms = new MemoryStream())
            {
                //serialize with our custom protocol
                //plan change to json ?

                //utf16
                BinaryWriter binWriter = new BinaryWriter(ms, System.Text.Encoding.Unicode);
                //binay format
                //1. typename
                //2. fields
                //3. method
                //4. indexer get/set
                binWriter.Write((short)1);//start marker

                context.CollectionTypeMembers(jsTypeDefinition);
                //------------------------------------------------

                jsTypeDefinition.WriteDefinitionToStream(binWriter);
                //------------------------------------------------
                finalBuffer = ms.ToArray();

                fixed (byte* tt = &finalBuffer[0])
                {
                    proxObject.SetUnmanagedPtr(
                        ContextRegisterTypeDefinition(
                        context.Handle.Handle,
                        0, tt, finalBuffer.Length));
                }

                ms.Close();
            }
        }
Example #22
0
 public INativeRef CreateProxyForTypeDefinition(JsTypeDefinition jsTypeDefinition)
 {
     var proxyObject = new NativeRef(nativeRefList.Count, jsTypeDefinition);
     //store data this side too
     jsTypeDefinition.nativeProxy = proxyObject;
     //store in exported list
     nativeRefList.Add(proxyObject);
     //register type definition
     NativeV8JsInterOp.RegisterTypeDef(ownerContext, jsTypeDefinition);
     return proxyObject;
 }
Example #23
0
File: Form1.cs Project: killf/V8Net
        private void button1_Click(object sender, EventArgs e)
        {
            JsBridge.dbugTestCallbacks();

            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;}})()");
                }
                stwatch.Stop();

                Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString());
                //Assert.That(result, Is.EqualTo(100));
            }
        }