问题描述
在 the docs 之后,我可以运行 polyglot 应用程序,其中起始语言是 Java,目标语言是 C++,它位于单独的源文件中。
我想知道如何将一些参数从 Java 传递给 C++。
代码示例
开始语言(Java)
import org.graalvm.polyglot.*;
import java.io.File;
import java.io.IOException;
public class Hellopolyglot {
public static void main(String[] args) throws IOException {
File file = new File("polyglot"); // the path to the file
Source source = Source.newBuilder("llvm",file).build();
Context polyglot = Context.newBuilder().allowAllAccess(true).build();
Value cpart = polyglot.eval(source);
cpart.executeVoid();
}
}
目标语言(C++)
#include <iostream>
using namespace std;
int main(int argc,char** argv){
cout << "You have entered " << argc
<< " arguments:" << "\n";
for (int i = 0; i < argc; ++i)
cout << argv[i] << "\n";
return 0;
}
提前致谢。
解决方法
正如 Schatz on GraalVM Slack Server 所回答的,您可以通过以下方式将参数传递给 LLVM:
String[] arguments = {"Hello","World"};
Context polyglot = Context.newBuilder().arguments("llvm",arguments).allowAllAccess(true).build();
Value cpart = polyglot.eval(source);
cpart.executeVoid();
或者,您可以直接调用函数,使用
cpart.readMember("functionName").execute(arguments);