(chakra) recursive evaluation
viceice opened this issue · 12 comments
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?
Hello, Michael!
You could give an example of JS code that causes this error.
Simple test case:
_engine.EmbedHostObject("_test", (Action<string>)(c => _engine.Evaluate(c));
_engine.Evaluate("_test('var test;')");
Why are you doing this? You can use a eval
keyword.
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.
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");
That's my workaround 😄
Only here, instead of the eval(…)
expression used the new Function(…)()
, which is more safe.
When there is free time, I will deal with this problem.
Solution
- Inline the execution if we don't specify a max stack size.
- Check, if we are the dispatcher thread and inline the execution.
Which solution do you prefer, or another solution? I can send a PR.
Which solution do you prefer, or another solution? I can send a PR.
I'll deal with the problem myself, but later.
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.
Hello, Michael!
This error is fixed in version 3.0.8.