It's easy to make objects available to Rhino from Java using ScriptableObject.putProperty
, but is there a way to expose a top-level function? That is, can I make it so scripts in Rhino can call foo()
and have that call out to Java code?
Exposing a function from Java to Rhino
Asked Answered
Umm... yes, you can. Perhaps I'm not understanding your question correctly (what do you mean by "top-level function"?) but if you've exposed java object to Rhino, you can call its methods.
Sorry if I wasn't clear. While I can expose an object, which allows me to call foo.bar(), I'd ideally like to just call bar(). So, by 'top-level function' I mean a function that's a property of the Rhino Scope that's currently executing, if that makes more sense. –
Horripilate
Oh, I see. You can define a FunctionObject: mozilla.org/rhino/apidocs/org/mozilla/javascript/… and associate it with global (or your current) scope via putProperty() –
Coextensive
© 2022 - 2024 — McMap. All rights reserved.