java 对象的 Nashorn 调用方法(通过绑定传递)通过 JS 中的函数引用

Nashorn call method of java object (passed through bindings) via function reference in JS

我想将本机 Java 对象放入 ScriptEngine 绑定中以便于访问。

scriptEngine.put("myApi", myApiInstance);

这里 "myApiInstance" 有一个非静态方法 "foo()"。

现在在 JS 中我有一个函数:

someJsFunction(func) { func.call(...) }

但是调用

someJsFunction(myApiInstance.foo)

结果为 "TypeError: func.call is not a function"。

另一方面,"myApiInstance.foo()" 按预期工作。 这看起来像是 ScripEngine 的细节,因为 "call()" 方法应该在任何函数中可用。 是的,"typeof myApiInstance.foo" returns "function"

Java 方法和函数接口对象(lambda 对象)被视为脚本函数,因此可以像往常一样从 JavaScript 调用。正如您在此类对象上提到的 "typeof" returns 是的。您可以直接从脚本中调用它们。但是这些不是真正的 JS 函数对象,因为它们的原型不是 Function.prototype。也就是说,如果您想使用 Function.prototype.call 或 .apply 调用这些,[假设您正在分派给任何已传递的可调用对象],您可以执行以下操作:

import javax.script.*;

public class Main {
  public static void main(String[] args) throws Exception {
    ScriptEngineManager m = new ScriptEngineManager();
    ScriptEngine e = m.getEngineByName("nashorn");

    // get the java static method to call
    e.eval("var getProp = java.lang.System.getProperty");
    // direct call
    e.eval("print(getProp('java.home'))");

    // call using Function.prototype.call
    e.eval("print(Function.prototype.call.call(getProp, null, 'java.home'))");

    // a java object
    e.eval("var out = java.lang.System.out");
    // get an instance method
    e.eval("var p = out.println");
    // call java instance method using Function.prototype.call
    e.eval("Function.prototype.call.call(p, out, 'hello world')");
  }
}