Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reset the state of the engine between calls? #805

Closed
smadurange opened this issue Nov 20, 2020 · 3 comments
Closed

How to reset the state of the engine between calls? #805

smadurange opened this issue Nov 20, 2020 · 3 comments

Comments

@smadurange
Copy link

smadurange commented Nov 20, 2020

I'm using Jint 2.11.58v. I'm trying to understand how to use the engine's EnterExecutionContext/LeaveExecutionContext methods. I would like to reset the engine after a certain operation. For example, in the following code, I don't expect to see name printed as foo but it does.

static void Main(string[] args)
{
    var engine = new Engine();

    for (int i = 0; i < 2; i++)
    {
        if (i == 0)
        {
            var ctx = engine.ExecutionContext;
            var lexEnv = LexicalEnvironment.NewDeclarativeEnvironment(engine, ctx.LexicalEnvironment);
            var varEnv = LexicalEnvironment.NewDeclarativeEnvironment(engine, ctx.VariableEnvironment);
            engine.EnterExecutionContext(lexEnv, varEnv, ctx.ThisBinding);
            var x = engine.SetValue("name", "foo");
            Console.WriteLine("First " + engine.GetValue("name"));
            engine.LeaveExecutionContext();
        }
        else
        {
            Console.WriteLine("Second " + engine.GetValue("name"));
        }
    }
}

Is there any other way I can reset the state of the engine between calls?

@lahma
Copy link
Collaborator

lahma commented Dec 5, 2020

#789 tries to solve this by allowing you to enter temporary context which is tried to be cleaned after usage.

@lahma
Copy link
Collaborator

lahma commented May 22, 2022

ShadowRealm was merged to main and it might contain the functionality requested here, see Jint test case how it isolates a playground from the actual engine.

@scarletquasar
Copy link

Currently, in MelonJS I am using a quick "dirty" hack: recreating the engine as new everytime I want to restart it and injecting what I want. It is not the best solution here, but for a PoC is working fine.

engine.SetValue("__reset_current_execution__", new Action(() => {
  EngineManager.ResetEngine();
  engine.SetupFor(BuiltInJsModule.LibrariesAndPolyfills, currentApp, container);
  engine.SetupFor(BuiltInJsModule.Engine, currentApp, container);
  engine.SetupFor(BuiltInJsModule.Application, currentApp, container);
  engine.SetupFor(BuiltInJsModule.Environment, currentApp, container);
  engine.SetupFor(BuiltInJsModule.InputOutput, currentApp, container);
  engine.SetupFor(BuiltInJsModule.UnsafeScripting, currentApp, container);
  engine.SetupFor(BuiltInJsModule.DataManagement, currentApp, container);
  engine.SetupFor(BuiltInJsModule.HttpOperations, currentApp, container);
  engine.SetupFor(BuiltInJsModule.Tools, currentApp, container);
  engine.SetupFor(BuiltInJsModule.Debug, currentApp, container);
  engine.SetupFor(BuiltInJsModule.Database, currentApp, container);
}));
public static void ResetEngine()
{
    JintStatic.CurrentJintEngine = new();
}

Repository owner locked and limited conversation to collaborators Jul 2, 2022
@lahma lahma converted this issue into discussion #1214 Jul 2, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants