Skip to content

(chakra) recursive evaluation #72

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

Closed
viceice opened this issue Apr 4, 2019 · 12 comments
Closed

(chakra) recursive evaluation #72

viceice opened this issue Apr 4, 2019 · 12 comments

Comments

@viceice
Copy link

viceice commented Apr 4, 2019

I'm trying to a simple module loading feature which requires recursive script execution:

net -> js -> net -> js

This is currently not possible, because the scriptdispatcher is blocked by the first eval.

My current workaround is to use eval, which i don't like. Is there any way to make recursive calls work?

@Taritsyn
Copy link
Owner

Taritsyn commented Apr 4, 2019

Hello, Michael!

You could give an example of JS code that causes this error.

@viceice
Copy link
Author

viceice commented Apr 4, 2019

Simple test case:

_engine.EmbedHostObject("_test", (Action<string>)(c => _engine.Evaluate(c));
_engine.Evaluate("_test('var test;')");

@Taritsyn
Copy link
Owner

Taritsyn commented Apr 4, 2019

Why are you doing this? You can use a eval keyword.

@viceice
Copy link
Author

viceice commented Apr 4, 2019

What about this:

_engine.EmbedHostObject("_test", (Action<string>)(c => _engine.ExecuteFile($"dist/{c}.js"));
_engine.Evaluate("_test('test')");

I'm trying to implement a simple module system.

@Taritsyn
Copy link
Owner

Taritsyn commented Apr 4, 2019

Try this variant:

Func<string, string> readFileContent = File.ReadAllText;

_engine.EmbedHostObject("readFileContent", readFileContent);
_engine.Execute(@"function loadModule(moduleName) {
    var modulePath = 'dist/' + moduleName + '.js';
    var moduleContent = readFileContent(modulePath);

    return new Function(moduleContent)();
}");
_engine.CallFunction("loadModule", "test");

@viceice
Copy link
Author

viceice commented Apr 4, 2019

That's my workaround 😄

@Taritsyn
Copy link
Owner

Taritsyn commented Apr 4, 2019

Only here, instead of the eval(…) expression used the new Function(…)(), which is more safe.

@Taritsyn
Copy link
Owner

Taritsyn commented Apr 4, 2019

When there is free time, I will deal with this problem.

@viceice
Copy link
Author

viceice commented Apr 5, 2019

Solution

using (var waitHandle = new ManualResetEvent(false))
{
task = new ScriptTask(del, waitHandle);
EnqueueTask(task);
waitHandle.WaitOne();
}

  1. Inline the execution if we don't specify a max stack size.
  2. Check, if we are the dispatcher thread and inline the execution.

Which solution do you prefer, or another solution? I can send a PR.

@Taritsyn
Copy link
Owner

Taritsyn commented Apr 5, 2019

Which solution do you prefer, or another solution? I can send a PR.

I'll deal with the problem myself, but later.

@viceice
Copy link
Author

viceice commented Apr 5, 2019

OK, the new Function solution is a little bit annoying, because i don't have a filename in the stack traces.

So it is very difficult to find the right source file.

@Taritsyn
Copy link
Owner

Taritsyn commented Apr 9, 2019

Hello, Michael!

This error is fixed in version 3.0.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants