Calling C# void method causes JintException

Aug 15, 2011 at 11:15 PM

Hi, It appears that if I call a void C# method from the interpreter, I get the following exception:

JintException: Object reference not set to an instance of an object
Line: 1 Char: 0
UnityEngine.Debug.Log("FROM JAVASCRIPT"

------ Stack Trace:
Jint.JintEngine.Run (Jint.Expressions.Program program, Boolean unwrap) (at Y:/Desktop/Jint-0.9.0-sources/Jint/JintEngine.cs:274)
Jint.JintEngine.Run (System.String script, Boolean unwrap) (at Y:/Desktop/Jint-0.9.0-sources/Jint/JintEngine.cs:200)
UnityHello.Start () (at Assets/UnityHello.cs:39)

Btw, this exception is thrown *after* the C# method successfully operates (because I see "FROM JAVASCRIPT" in the log window).

Thanks, -Chuck

Aug 29, 2011 at 5:20 AM

Hi, This error also occurs in the following simple setting running in Mono 2.6.7 outside of Unity:

      jint.Run("function println(obj) { System.Console.WriteLine(obj); }");


Same behavior: the printout appears, but then the JintException happens.  The error does not happen in .NET.

Any ideas?


Aug 31, 2011 at 7:41 AM

Probably this is a bug in Mono

Aug 27, 2012 at 8:01 PM

I was able to solve this problem with 2 changes

In NativeMethodOverload.c, in public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters, Type[] genericArguments), on line 83 wrap "visitor.Return(impl(visitor.Global, that, parameters));" in a try catch block. The catch can be empty. It's just complaining there is no return;

In Marshal.cs, in public T MarshalJsValue<T>(JsInstance value) add "if(value == null) return default(T);" to the top of the method; This catches the void return, and returns a null instead;


These changes plus the changes mentioned at the bottom of allows Jint to run just fine in Unity3D.