System.MethodAccessException

Sep 15, 2011 at 12:30 AM

I am still struggling with getting Jint to call my code from inside JavaScript.  The following simple test case illustrates my current problem:

using System;
using Jint;

namespace TestJint {
   class MainClass {
      public static void Main (string[] args) {
         JintEngine engine = new JintEngine();
         engine.DisableSecurity();
         Test("Test WriteLine");
         Console.WriteLine(engine.Run("TestJint.MainClass.Test(\"INSIDE\")"));
      }
     
      public static void Test (String line) {
         Console.WriteLine(line);
      }
   }
}

Running Jint 0.9.2 in .NET on Windows, throws System.MethodAccessError (see details below), which "typically occurs when the caller does not have access permission to the member".  Is the call to DisableSecurity above not effective?

>>>>> OUTPUT

Test WriteLine

Unhandled Exception: Jint.JintException: TestJint.MainClass.Test(System.String)
Line: 1 Char: 0
TestJint.MainClass.Test("INSIDE")

------ Stack Trace:
Test('INSIDE')
 ---> System.MethodAccessException: TestJint.MainClass.Test(System.String)
   at jsWrapper(IGlobal , JsInstance , JsInstance[] )
   at Jint.Native.NativeMethod.Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
   at Jint.ExecutionVisitor.ExecuteFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters, Type[] genericParameters)
   at Jint.ExecutionVisitor.Visit(MethodCall methodCall)
   at Jint.Expressions.MethodCall.Accept(IStatementVisitor visitor)
   at Jint.ExecutionVisitor.Visit(MemberExpression expression)
   at Jint.Expressions.MemberExpression.Accept(IStatementVisitor visitor)
   at Jint.ExecutionVisitor.Visit(ExpressionStatement statement)
   at Jint.Expressions.ExpressionStatement.Accept(IStatementVisitor visitor)
   at Jint.ExecutionVisitor.Visit(Program program)
   at Jint.JintEngine.Run(Program program, Boolean unwrap)
   --- End of inner exception stack trace ---
   at Jint.JintEngine.Run(Program program, Boolean unwrap)
   at Jint.JintEngine.Run(String script, Boolean unwrap)
   at Jint.JintEngine.Run(String script)
   at TestJint.MainClass.Main(String[] args) in y:\disco\mono\TestJint\Main.cs:line 31

Coordinator
Sep 15, 2011 at 12:31 AM

Did you try to also call AllowClr() ?

Sep 15, 2011 at 12:44 AM

Yup, just added

  engine.AllowClr = true;

before or after DisableSecurity() above.  (Documentation suggested that only DisableSecurity was required).

But behavior unchanged.  Can you reproduce this?

-CR

Coordinator
Sep 15, 2011 at 12:46 AM

Oh, seems your class is private … can’t be called like that. Make it public.

Sep 15, 2011 at 12:53 AM

My bad.  I was led astray by the New Project template. Of course the main class should be public.  Thanks for the quick reply.