2

I am trying to access public methods from a C# class library (dll) from javascript, specifically a Rhino package for javascript.

Here is what I'm trying to do:

namespace ReturnINT
{
    public class ReturnINT
    {

        public static int RetornaInteiro ()
        {
            try
            {
                int number = 2;

                return number;
            }
            catch (Exception)
            {
                return 1;
            }
        }
    }
}

I created a class library from above code or to be precise a dll file. Now I want to access this dll in my javascript, but I don't want to create any wrappers, which is mentioned in some other threads.

Can someone please guide me here and let me know if I need to perform some setting while creating the dll file?

  • Possible duplicate of [Call DLL methods from Javascript](http://stackoverflow.com/questions/11517319/call-dll-methods-from-javascript) – Matias Cicero Dec 04 '15 at 18:34
  • @Matias Cicero:I am not using the javascript in any of the web browser ,Rhino is a package with JavaScript which allow us for debug server scripting refer [link](http://processors.wiki.ti.com/index.php/Debug_Server_Scripting ) ,is there any other apart from accessing via pluging. – Manzoor Elahi Dec 04 '15 at 20:53

1 Answers1

0

Here is my sample .net C# app using Rhino over ikvm.

My sample will execute sunspider benchmark script.

class Program
    {
        public static org.mozilla.javascript.Context con = null;
        public static org.mozilla.javascript.Scriptable scope = null;
        public static string FILE_PATH = @"C:\sunspider-0.9.1";


        public static string JS_Script = "load('run.js');";
        static void Main(string[] args)
        {
            Console.WriteLine("Using .NET Framework " + System.Environment.Version.ToString());
            string ___strScriptPath = System.Configuration.ConfigurationManager.AppSettings["script_path"];
            if (string.IsNullOrEmpty(___strScriptPath) == false && System.IO.Directory.Exists(___strScriptPath))
            {
                System.Environment.CurrentDirectory = ___strScriptPath;
                FILE_PATH = ___strScriptPath;
            }
            else
            {
                System.Environment.CurrentDirectory = FILE_PATH;
            }
            string ___strOptLevel = System.Configuration.ConfigurationManager.AppSettings["opt"];
            int ___optLevel = -1;
            if (string.IsNullOrEmpty(___strOptLevel) == false)
            {
                if (int.TryParse(___strOptLevel, out ___optLevel))
                {
                    if (___optLevel  9)
                    {
                        ___optLevel = 9;
                    }
                }
            }
            string ___strScript = System.Configuration.ConfigurationManager.AppSettings["script"];

            if (string.IsNullOrEmpty(___strScript) == false)
            {
                JS_Script = ___strScript;
            }
            con = org.mozilla.javascript.Context.enter();
            con.setLanguageVersion(org.mozilla.javascript.Context.VERSION_ES6);
            //con.setGeneratingDebug(false);

            con.setOptimizationLevel(___optLevel);
            if (___optLevel == -1)
            {
                con.setGeneratingDebug(true);
                con.setGeneratingSource(true);
                con.setDebugger(null, null);
            }
            else
            {
                con.setGeneratingDebug(false);
                con.setGeneratingSource(false);
                con.setDebugger(null, null);
            }
            Console.WriteLine("Starting Rhino Processing OptLevel : {0} Path : {1}", ___optLevel, System.Environment.CurrentDirectory);
            scope = con.initSafeStandardObjects();
            org.mozilla.javascript.BaseFunction fuc = new org.mozilla.javascript.BaseFunction();
            java.lang.Class css = typeof(Program);
            java.lang.reflect.Member   loadMethod = css.getMethod("load", new java.lang.Class[]{typeof(string)});
            org.mozilla.javascript.FunctionObject func = new org.mozilla.javascript.FunctionObject("load", loadMethod, scope);
            scope.put("load", scope,func);

            java.lang.reflect.Member printMethod = css.getMethod("print", new java.lang.Class[] { typeof(object) });
            org.mozilla.javascript.FunctionObject funcPrint = new org.mozilla.javascript.FunctionObject("print", printMethod, scope);
            scope.put("print", scope, funcPrint);

            java.lang.reflect.Member hogehogeMethod = css.getMethod("hogehoge", new java.lang.Class[] { typeof(object), typeof(object), typeof(object), typeof(object) });
            DelegatableFunctionObject.deleHogehoge dele = new DelegatableFunctionObject.deleHogehoge(hogehoge);
            //DelegatableFunctionObject funcHogehoge = new DelegatableFunctionObject("hogehoge", hogehogeMethod, scope,dele);
            org.mozilla.javascript.FunctionObject funcHogehoge = new org.mozilla.javascript.FunctionObject("hogehoge", hogehogeMethod, scope);
            scope.put("hogehoge", scope, funcHogehoge);
            try
            {
                con.evaluateString(scope, JS_Script, "", 1, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            DateTime dtStart = DateTime.Now;
            /*
            org.mozilla.javascript.Script script = con.compileString("", "", 1, null);
            for (int i = 0; i  cacheFile = new Dictionary();
        public static void load(string s)
        {
            string trace = System.Environment.StackTrace;
            string strScript = null;

                strScript = System.IO.File.ReadAllText(FILE_PATH + "\\" + s);
             //   cacheFile[s] = strScript;

           con.evaluateString(scope, strScript, "", 1, null);
        }
        public static void print(object o)
        {
            Console.WriteLine(string.Format("[Rhino] {0}", o));
        }
        public static void hogehoge(object o, object o2, object o3, object o4)
        {

        }
        public class DelegatableFunctionObject : org.mozilla.javascript.FunctionObject
        {
            public delegate void deleHogehoge(object o1, object o2, object o3, object o4);
            private deleHogehoge __dele = null;
            public DelegatableFunctionObject(string name, java.lang.reflect.Member methodOrContructior, org.mozilla.javascript.Scriptable scope,deleHogehoge dele)
                : base(name, methodOrContructior, scope)
            {
                this.__dele = dele;
            }
            public override object call(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, org.mozilla.javascript.Scriptable thisObj, object[] args)
            {
                if (__dele != null)
                {
                    switch (args.Length)
                    {
                        case 1:
                            __dele.Invoke(args[0], null, null, null);
                            break;
                    }
                }
                return null;

            }
        }
    }

I hope this will help you.