I am currently working on a project where the purpose is to teach programming by playing a game. Therefore, I want to include a Javascript interpreter.
I started working with Jurassic (Unity Asset Store - The Best Assets for Game Making) : it perfectly works when I run the game inside the editor or in Standalone builds. However, my target platform is WebGL and I don’t find a way to make it work, I receive this error : “Attempting to call method ‘(null)’ for which no ahead of time (AOT) code was generated.”
My code :
engine.SetGlobalFunction(“Tirer”, new System.Action(_Tirer));
engine.Execute(“Tirer(1);”);
I understand WebGL uses IL2CPP and that can mess up some things. But anyone have an idea to make it work?
If not, anyone has another solution to integrate a javascript interpreter inside a WebGL build?
I have no direct experience with the WebGL target platform, but here is a possible explanation:
The code stripping settings in Unity may be on. This means that at build time it might be deciding that you didn’t use certain classes/methods, so it strips them out. To disable this function, under Player Settings, under Other Settings, disable “Strip Engine Code”
An alternate explanation might have something to do with certain namespaces not being accessible for security reasons on the WebGL build. It might not let you random add/remove methods, or they might need to be declared in advance, etc. I know little of JS’s details but this could be something to look at.
If your actions changed the error, you have either introduced a new error that happens earlier, or you have found the problem and moved it or changed it in some way.
Anyone else here have any AOT/JIT experience on this web platform??
Hi, I’m working on a similar project with the same jurassic engine and webgl platform, and getting the same Reflection.Emit error on Evaluate and Execute, did you happen to solve the problem? does anyone have an idea of how to solve this? or have an alternative runtime interpreter? even a pseudo will do for me, no need to actually access unity core classes, just interpret and compile code and get a result (in javascript).
If anyone finds this and is still looking for an answer, I found another engine, it’s called Jint by Sebastien Ros, here is the link to the git repository: GitHub - djkrose/jint-unity: Javascript Interpreter for .NET [forked for .Net 3.5, Unity, and ScriptingMod]
All you have to do is compile the project, get the Jint.dll and Microsoft.Scripting.Core.dll into your assets and you are good to go, it’s similar to jurassic and this works fine with Unity WebGL.
using Jint;
//...
Engine _engine = new Engine();
Debug.Log(_engine.SetValue("MyGlobalVariable",0)
.Execute("MyGlobalVariable++;")
.GetValue("MyGlobalVariable").AsNumber()); //1.0
I suppose you can’t emit & execute arbitrary code from unity app. I don’t think this is bug, I think they did that intentionally because of security means. But if your target platform is web, then you may execute your code within the browser itself.