Add Scripting interpreter to teach code

Hi everyone,

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?

Thanks in advance.

Big kiss from Paris under the rain :slight_smile:

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.

Big kisses back from Costa Mesa in the sunshine! :slight_smile:

Unfortunately, it does not work.

Instead of the previous error, I get this one: “Unsupported subtype of MethodBase.”

Any other idea.

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??

It seems this error come from the plugin core (search “Unsupported subtype of MethodBase.” here : https://github.com/Alxandr/Jurassic/blob/master/Jurassic/Compiler/Emit/ILGenerator/ReflectionEmitILGenerator.cs).

Maybe it has anything to do with reflection but I have no idea about it actually is.

Maybe I should change the entire system…

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.