添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How do I add custom global functions to GraalVM's JavaScript 'this' object without using Nashorn compatibility mode via the Object.bindProperties() method (see this: Defining a default/global Java object to Nashorn script engine? ) ? I could not find a way using the Context methods and reflection of the Context object does not show a similar bindProperties() method I can use.

Example:

I have these methods:

class GlobalMethods
public void a() {...}
public void b() {...}
public void c() {...}

and I want a(), b() and c() to be callable in JavaScript without scope/qualification:

context.eval("js", "a()");

Thanks in advance!

For js functions, you can define a file, say globals.js, where globals are initialized and added to the JavaScript global object :

globalThis.someFunction = function() {...}

and set the option js.commonjs-global-properties to load this file at context creation time :

Context context = Context.newBuilder("js")
                         .option("js.commonjs-global-properties", "./globals.js")
                         .build();

In order to bind java options to JS, you need to enable polyglot.js.allowHostAccess via Bindings :

Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("polyglot.js.allowHostAccess", true);
bindings.put("polyglot.js.allowHostClassLookup", (Predicate<String>) s -> true);
bindings.put("javaObj", new Object());

or with Context :

Context context = Context.newBuilder("js")
                         .allowHostAccess(true)
                         .build();
context.getBindings("js").putMember("a", GlobalMethods.a);

Or you can use the @HostAccess.Export decorator in the class definition :

  @HostAccess.Export
  public void a() {...}

Though I'm not sure you can access a static method like GlobalMethods.a() without referencing the classname at least one time, maybe passing the function output directly is sufficient to suit your needs, or passing the class definition, followed by an eval globalThis.a = GlobalMethods.a (not tested).

Thanks, but the methods I'm trying to expose globally are Java methods, not JavaScript. Is there a way to do that without using a globals.js? – Lester Apr 24, 2022 at 15:06 Sorry if I misunderstood.. (doing context.eval("js", "a()"); means eval that javascript function called a(), I did not see the public void keywords).. Still you can create some bindings, but not sure it will allow you to get a(), b() and c() to be callable in JavaScript without scope/qualification. – EricLavault Apr 24, 2022 at 16:57 Hi Eric, I've tried your global.js method and while it does work (I call the Java methods via the JS methods defined in global.js), it does not support overloading (eg globalThis.a cannot have multiple definitions for each overloaded version of GlobalMethods.a()), so it won't work for me. I'd love to use Graal's new Context API but it seems I'll be stuck in Nashorn compatibility mode. – Lester Apr 25, 2022 at 14:34

Eric's answer that uses a global.js works if the methods are not overloaded. In addition, the complete setup for the Context object looks like this with a few additional required options:

context = Context.newBuilder("js")
          .allowHostAccess(true)
          .allowExperimentalOptions(true)
          .allowAllAccess(true)
          .allowIO(true)                                            // required
          .option("js.nashorn-compat", "false")                     // Must be false
          .option("js.commonjs-require", "true")                    // required
          .option("js.commonjs-require-cwd", "/path/to/globals.js") // required
          .option("js.commonjs-global-properties", "./globals.js")
          .build();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.