Exposing a function from Java to Rhino
Asked Answered
H

1

7

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?

Horripilate answered 9/10, 2009 at 20:4 Comment(0)
C
6

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.

Coextensive answered 9/10, 2009 at 20:26 Comment(2)
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.