GraalVM 多语言:将参数从 Java 传递到 LLVM (C++)

问题描述

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);

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...