jint: Executing any script using Jint 3 with Unity 2021.3 results in TypeLoadException

Version used

I’m using Unity version 2021.3.26f1, Jint version 3.0.0-beta-2049 (targeting netstandard-2.1) and Esprima 3.0.0-rc-02, SystemMemory version 4.5.5.

Describe the bug

I was successfully able to use Jira 2 from the tutorial . Any script using the Jira engine results in the error:

TypeLoadException: Failure has occurred while loading a type.
Jint.Runtime.Interpreter.Statements.JintExpressionStatement.Initialize (Jint.Runtime.Interpreter.EvaluationContext context) (at <0636d30103674fca8d470ee2d5418bc9>:0)
Jint.Runtime.Interpreter.Statements.JintStatement.Execute (Jint.Runtime.Interpreter.EvaluationContext context) (at <0636d30103674fca8d470ee2d5418bc9>:0)
Jint.Runtime.Interpreter.JintStatementList.Execute (Jint.Runtime.Interpreter.EvaluationContext context) (at <0636d30103674fca8d470ee2d5418bc9>:0)
Jint.Engine.ScriptEvaluation (Jint.Runtime.ScriptRecord scriptRecord) (at <0636d30103674fca8d470ee2d5418bc9>:0)
Jint.Engine+<>c__DisplayClass78_0.<Execute>b__0 () (at <0636d30103674fca8d470ee2d5418bc9>:0)
Jint.Engine.ExecuteWithConstraints[T] (System.Boolean strict, System.Func`1[TResult] callback) (at <0636d30103674fca8d470ee2d5418bc9>:0)
Jint.Engine.Execute (Esprima.Ast.Script script) (at <0636d30103674fca8d470ee2d5418bc9>:0)
Jint.Engine.Execute (System.String code, System.String source, Esprima.ParserOptions parserOptions) (at <0636d30103674fca8d470ee2d5418bc9>:0)
Jint.Engine.Execute (System.String code, System.String source) (at <0636d30103674fca8d470ee2d5418bc9>:0)
JavascriptRunner.Start () (at Assets/Scripts/JavascriptRunner.cs:23)

To Reproduce

Follow steps from tutorial Using real JavaScript with Unity, using the above versions of Jira.

using UnityEngine;
using Jint;
using System;
using Esprima;
using Jint.Runtime;
using Jint.Native;

public class JavascriptRunner : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
      try{

        var engine = new Engine();

        engine.SetValue("log", new Action<object>(msg => Debug.Log(msg)));

        JsValue v = engine.Evaluate(@"log('Hello World')", "test");
      } catch (TypeLoadException err)
      {
            Debug.Log(err.ToString());
      }
        // Debug.Log($"{worldModel.PlayerName} has {worldModel.NumberOfDonuts} donuts");
    }

}

Expected behavior

Should display “Hello World” to debug log.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 19 (7 by maintainers)

Most upvoted comments

Good news and thank you @CreepGin ! I was able to run the code after compiling the latest version in the repository. It seems like the problem was with 2049. I did copy all dll files from the build folder, which may have also helped.

OK cool, it seems that all open issues and problems have been addressed I guess - except for the original issue 😆

That seems still to be quite impossible to replicate - let’s see if the build + installation instructions help.

Before beta-2048, globalThis was needed as the 1st arg in the func.Call. But after beta-2048, globalThis seems to be no longer required and would actually be passed as the 1st arg to the target js function if left there.

Just appears to be a breaking change, not a bug or anything.

Thanks for the clarification, IIRC, I changed the signatures you could earlier get different behavior based on parameter count so it was a bit ambiguous- beta label allows to do such surprising changes 😉

  • engine.Json and engine.Object are gone.

Yes we’ve hidden everything that hasn’t been requested to be visible. We try to open things via public API tests cases to ensure proper API is exposed and we don’t accidentally hide it later. So if there’s something needed missing - open a PR or an issue and we’ll figure it out.

  • GetCompletionValue() is gone. It seems engine.Evaluate() is the preferred way now.

Yes. Execute just set the value inside the engine and you had to retrieve it manually. CompletionValue is more JS spec thing and Evaluate gives you the result you want. Execute is for running batch of commands which allows chaining.

@lahma Sure ofc I’ll take a stab at it =)

@lucidbard Has this been happening just for the recent Jint version (3.0.0-beta-2049)? The Jint version I’m using with Unity (without any problem) is a few months old.

I’m building the new Jint dll right now; will report back with any findings. (Apparently my current IDE needs updating; the Jint codebase now requires VS 2022 17.3+ to build)