Jint: Works in Unity but Not with IL2CPP Enabled

Created on 15 Oct 2019  路  3Comments  路  Source: sebastienros/jint

I've successfully integrated Jint into a Unity (v2019.1.11f1) project, and everything works great when running in the Unity IDE. However, when I enable IL2CPP to produce a release build, a JavaScript call to subscribe to a C# event doesn't seem to resolve. I receive the following error: JavaScriptException: No public methods with the specified arguments were found.

Has anyone else been able to get Jint working in a Unity project using IL2CPP? While IL2CPP is optional for most of the platforms we support, we need it for iOS.

I'm wondering if I need a link.xml file in Unity. I've tried many variations, but I can't seem to get it to work.

Here's an example project that illustrates the issue. If you run this in the Unity editor and wait about two seconds, you'll see output in the console that the C# event was successfully captured in JavaScript.

If I simply turn on IL2CPP in the player project settings, the subscription itself fails with the aforementioned error. Here's the JavaScript code that fails: objectWithEvent.add_TestEvent(testEvent);

There are fewer than 10 lines of code in this example (JintTest.cs), and I'd love any insights.

Most helpful comment

Okay...after a bunch of sleuthing, I've figured this one out. My original project will work as long as this link.xml file is in the Unity Assets folder. This basically tells Unity not to remove the types under System.Linq.Expressions during its IL2CPP stripping process.

<linker>
  <assembly fullname="System.Core">
    <type fullname="System.Linq.Expressions.*" preserve="all"/>
  </assembly>
</linker>

Hopefully this can help someone else too.

All 3 comments

Hey !
Just tested your code, not sure how the javascript add_TestEvent function event worked in editor since it does not exist in the ObjectWithEvent class.
Anyway I made a work around that works in IL2CPP: I made an actual function in ObjectWithEvent to subscribe to the EventHandler. This way the function definition is known in advance by the program and can be found at run time.

Code as attachment: JintTest.cs.txt

Thanks for taking a moment to look into this issue. I appreciate it.

The add_TestEvent method is created based on the add/remove event accessors in C#. I couldn't see any other way of subscribing/unsubscribing to C# events in Jint.

Here's a screenshot that shows the ILDASM for those generated event accessors.

Ideally, I'd like to avoid having to introduce special syntax for adding and removing events, since we're trying to script/expose an existing object model with a lot of C# events that we'd like to subscribe to. Is there an existing technique in Jint to do this that I've missed?

Okay...after a bunch of sleuthing, I've figured this one out. My original project will work as long as this link.xml file is in the Unity Assets folder. This basically tells Unity not to remove the types under System.Linq.Expressions during its IL2CPP stripping process.

<linker>
  <assembly fullname="System.Core">
    <type fullname="System.Linq.Expressions.*" preserve="all"/>
  </assembly>
</linker>

Hopefully this can help someone else too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ciel picture ciel  路  13Comments

appel1 picture appel1  路  25Comments

asdfgasdfsafgsdfa picture asdfgasdfsafgsdfa  路  17Comments

shestakov picture shestakov  路  10Comments

smadurange picture smadurange  路  3Comments