wixette / isb

Interactive Small Basic (ISB) - Simple scripting language to be embedded in Unity games or shell environments.
https://www.nuget.org/packages/ISB/
Apache License 2.0
24 stars 3 forks source link

Support Unity coroutine or asynchronization during execution #8

Closed wixette closed 3 years ago

wixette commented 3 years ago

@ratkingsminion added stepping to the execution method to prevent long-time running in every frame:

Basically I added a parameter "stepping" to Run() and a method Continue(). ExecuteAssembly() now returns true if execution is done. This way I can call Continue() for only a certain amount per frame, or when the script calls a xyz.Break() function, etc

wixette commented 3 years ago

With https://github.com/wixette/isb/commit/ec9b7744e4ddf24a80204c8452930d03b776c5b3 , running program as a Unity coroutine is supported.

Canceling an execution when there are time-consuming operations, such as an infinite loop, is also supported.

See https://github.com/wixette/isb/blob/main/unity_integration_demo/Assets/Scripts/Program.cs for an example:

        // Runs the program in a Unity coroutine.
        Action<bool> doneCallback = (value) =>
        {
            if (!value)
            {
                ReportErrors(engine);
            }
            else if (engine.StackCount > 0)
            {
                string ret = engine.StackTop.ToDisplayString();
                PrintDebugInfo(ret);
            }
        };
        // Prevents the scripting engine to be stuck in an infinite loop.
        int maxInstructionsToExecute = 1000000;
        Func<int, bool> canContinueCallback =
            (counter) => counter >= maxInstructionsToExecute ? false : true;
        StartCoroutine(engine.RunAsCoroutine(doneCallback, canContinueCallback));

With the demo env set up by the above code, the following long (infinite yet) loop will be canceled during execution:

x = 0
for i = 1 to 100000
  x = x + 1
endfor
x