1

按照文档,我可以运行 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;
}

先感谢您。

4

1 回答 1

0

正如Schatz 在 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);
于 2021-07-30T00:24:30.350 回答