Search code examples
c#.netciljint

Ldfld problems under Mac OS X / Mono, probably Mono bug


I'm using 3rd party library JInt (the JavaScript interpriter) which was working fine until I've switched to Mac OS X, after that I keep on getting ArgumentNullExceptions, after some investigation I've found out that JInt uses dynamic code generation for making some sort of Js-Clr bridge. This method has the following instructions in the end:

code.Emit(OpCodes.Ldnull);
FieldInfo fieldInfo = typeof(JsUndefined).GetField("Instance");
code.Emit(OpCodes.Ldfld, fieldInfo);

Here's how these lines are executed (Full size screenshot here)

enter image description here

It is clearly seen that fieldInfo argument is not null, though when it comes to executing these lines, notice that LDFLD has no argument! (Full size screenshot here):

enter image description here

My current statement that will get executed is Ldnull, I performing "Step In"(Over Ldnull) and BANG exception occurs over Ldfld (Full size screenshot here)::

enter image description here

Any suggestions?


Solution

  • This issue you describe is a combination of different issues:

    • The IL disassembly doesn't show the fields operands. It's not only for your field, you can see it in the disassembly before for a ldsfld. I'd not worry about that, I encourage you to report it has a MonoDevelop bug though.

    • JInt is using a weird pattern to load a static field on the stack. I can reproduce the issue and the NRE on Mono 2.10.6 by emitting this pattern. Good news is that it is fixed in master. As you have access to JInt's code, to prevent the NRE, you can modify the else branch to simply read:

      code.Emit(OpCodes.Ldsfld, typeof(JsUndefined).GetField("Instance"));

    This will get you going.