我使用Jint解析JS代码并调用其中的函数。当我使用多线程环境时,我使用程序解析方法,如对此问题的响应:https://github.com/sebastienros/jint/issues/384。
所以我有一个Jint.Parser.Ast.Program实例。我可以在其中迭代IFunctionDeclaration并找到我的函数。但我不知道该怎么称呼这些函数..。
Dim parser As New Jint.Parser.JavaScriptParser
Dim program As Jint.Parser.Ast.Program = parser.Parse(code)
For Each func As Jint.Parser.IFunctionDeclaration In program.FunctionDeclarations
If func.Id.Name = myFunctionName Then
' How to call the function?
End If
Next我只找到了一种执行整个Program的方法。我假设我必须这样做,这样才能在引擎中实际定义函数。但是,我如何在脚本中调用某个函数呢?
发布于 2017-09-08 09:52:45
一旦您的程序被执行,只需使用相同的方法执行您的函数。例子是c#
var parser = new Jint.Parser.JavaScriptParser();
// _parserCache is a static ConcurrentDictionary<string, Jint.Parser.Ast.Program>
var program = _parserCache.GetOrAdd(scriptName, key => parser.Parse(code));
foreach (var func in program.FunctionDeclarations)
{
if (func.Id.Name == myFunctionName)
{
var exec = new Engine();
// The entire program is executed, to define the function
exec.Execute(program);
// now you can call your function
exec.Execute($"{myFunctionName}()");
}
}https://stackoverflow.com/questions/45412668
复制相似问题