package cn.fateverse.query;

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;

public class Main {
    public static void main(String[] args) {
        // 创建 GraalVM 上下文
        Context context = Context.create();

        // 定义一个 JavaScript 函数
        String script = "function execute(data) {\n" +
                "    console.log(data)\n" +
                "    console.log(\"test console!\")\n" +
                "    let add = (a, b) => a + b; // 使用箭头函数\n" +
                "    console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
                "    for (let i = 0; i < 1000; i++) {\n" +
                "        console.log(i)\n" +
                "    }" +
                "    return \"success\";\n" +
                "}";

        // 编译 JavaScript 函数
        context.eval("js", script);

        // 获取函数对象
        Value addFunction = context.getBindings("js").getMember("execute");

        // 执行函数并传入参数
        Value result = addFunction.execute("测试");
        Value result2 = addFunction.execute("测试");
        // 获取返回值
//        int sum = result.asInt();

        // 输出结果
        System.out.println("Sum: "); // 输出：Sum: 5
    }
}
