views:

232

answers:

1

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?

+3  A: 

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.

ChssPly76
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.
Thom
Oh, I see. You can define a FunctionObject: https://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/FunctionObject.html and associate it with global (or your current) scope via putProperty()
ChssPly76
Superb, that looks perfect. Thanks!
Thom