This project is read-only.

c# using v8.net : i need to call c# member function from javascript : please correct the example

May 7, 2015 at 10:42 AM
c# using v8.net : i need to call c# member function from javascript : please help me to correct the example.

There is javascript : string sampleJavaScript, it has two parts mentioned in it. part-1 is working and showing alert. but part-2 is not giving alert message.

(Please Note: This is my requirement that i need to use "myFunc2" member function which is under class "myClass". both class and member function name i do not want to change. )

Please help me correct this code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

using V8.Net;

namespace Project
{
    class Program
    {
        static void Main(string[] args)
        {
            string sampleJavaScript = @"
                    // [part-1]
                    var obj1 = new showalert('hi');   // c# class constructor in javascript showing alert
                    
                    // [part-2] 
                    var obj2 = new myClass();   // c# class 
                    var y1 = obj2.myFunc2('hihi'); // !!! C# member function is not working !!!
                    ";
            
            V8Engine v8Engine = new V8Engine();

            v8Engine.RegisterType<showalert>(null, true, ScriptMemberSecurity.Locked);
            v8Engine.GlobalObject.SetProperty(typeof(showalert));

            v8Engine.RegisterType<myClass>(null, true, ScriptMemberSecurity.Locked);
            v8Engine.GlobalObject.SetProperty(typeof(myClass));

            var result = v8Engine.Execute(
                    sampleJavaScript
              );
        }
    }

    public class showalert
    {  public showalert(string msg)
        { MessageBox.Show(msg);
        }
    }

    public class myClass 
    {  public int myFunc1()
        { return 10;
        }
        public void myFunc2(string msg) // i want to use this member function to use in javascript
        {
            MessageBox.Show(msg);
        }
    }
}
May 7, 2015 at 2:34 PM
Odd, at first glance I don't see any coding issues. I'll try to compile and test it; might be a bug somewhere.
May 7, 2015 at 3:35 PM
Edited May 7, 2015 at 3:36 PM
This is working for me. Your issue must be elsewhere. The only difference is I used a console app for testing, and called Console.WriteLine(). Try using the latest development branch binaries and see if it makes any difference.

Try putting a breakpoint in myFunc2 to see if it is being called.
May 8, 2015 at 5:30 AM
There is javascript : string sampleJavaScript, it has two parts mentioned in it. part-1 is working and showing alert. but part-2 is not giving alert message, this part-2 has some issue.

_--- in part-2 javascript, below given line of code is not working, please suggest where i am wrong.

var y1 = obj2.myFunc2('Hi from myFunc2');

_
(Please Note: This is my requirement that i need to use "myFunc2" member function which is under class "myClass". both class and member function name i do not want to change. )

Please help me correct this code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.Diagnostics;
    using System.IO;

    using V8.Net;

namespace Project
{
    class Program
    {
        static void Main(string[] args)
        {
            string sampleJavaScript = @"
                    // [part-1]
                    var obj1 = new showalert('Hi from showalert');   // c# class constructor in javascript showing alert
                    
                    // [part-2] 
                    var obj2 = new myClass();   // c# class 
                    var y1 = obj2.myFunc2('Hi from myFunc2'); // !!! C# member function is not working !!!
                    ";
            
            V8Engine v8Engine = new V8Engine();

            v8Engine.RegisterType<showalert>(null, true, ScriptMemberSecurity.Locked);
            v8Engine.GlobalObject.SetProperty(typeof(showalert));

            v8Engine.RegisterType<myClass>(null, true, ScriptMemberSecurity.Locked);
            v8Engine.GlobalObject.SetProperty(typeof(myClass));

            var result = v8Engine.Execute(

                    sampleJavaScript
              );

            Console.WriteLine("Thank you.");

            Console.ReadKey();
        }
    }

    public class showalert
    {  public showalert(string msg)
        {
            Console.WriteLine(msg);
        
        }
    }

    public class myClass 
    {  public int myFunc1()
        { return 10;
        }
        public void myFunc2(string msg) // i want to use this member function to use in javascript
        {
            Console.WriteLine(msg);
         
        }
    }
}
}
May 8, 2015 at 6:04 AM
Edited May 8, 2015 at 6:05 AM
Yes, I read that the first time. ;) Both parts work for me. I just tried your second code and this is my output:
Hi from showalert
Hi from myFunc2
Thank you.
As I said, there's something else, somewhere else, that is going wrong in your setup. Explain how your project is setup. What OS are you using? Is it 32-bit or 64-bit? etc.
May 11, 2015 at 5:07 AM
I have checked that the "result i am getting fine when opting x64 to build" (Application Build Properties).
And It is "not giving proper result when opting x86 to build" (Application Build Properties).
my machine is 64bit but i kept all downloaded libraries in place which is having x86 and x64 folders as well.
I was trying to build with the option "Any CPU" in application build settings but is fails to give proper result. (Part-2 result is not appearing).
where i could be wrong.
May 11, 2015 at 6:27 AM
Edited May 11, 2015 at 6:29 AM
I can confirm this is a bug when using the release x86 libraries. For some reason, my debug mode in x86 works perfectly. Looking into it, thanks.
May 11, 2015 at 7:18 AM
OK, I found the issue. It's a marshalling issue with the bool type. I added an attribute to help this convert better, and it's working again. Not sure why this came about all of a sudden, but glad it is worked out now. ;) The new binaries are in the development branch.