java - Is there a language-independent way to add a function to JSR223 scripting bindings? -
the jsr223 bindings
class allows expose arbitrary java objects scripting languages. have objects. define function quit()
can called scripting environment turns quitobject.run()
in java. jsr223 doesn't define concept of function object. there language-independent way following in javascript, namely take runnable() , create function in scripting environment?
static private object asfunction(scriptengine engine, runnable r) throws scriptexception { final bindings bindings = engine.createbindings(); bindings.put("r", r); return engine.eval( "(function (r) { var f = function() { r.run(); }; return f;})(r)", bindings); } runnable quitobject = /* get/create runnable here */ bindings bindings = engine.createbindings(); bindings.put("quit", asfunction(engine, quitobject));
with builtin javascript support jsr223 creates sun.org.mozilla.javascript.internal.interpretedfunction
want. won't work in jython or whatever, , i'd make language-independent.
i don't want script users have type quitobject.run()
that's clumsy, , don't want parse script input find quit()
buried within other code.
if @ javascript engine source code you'll find how oracle/sun implemented 2 functions (print, , println) magically (or not magically) present when fire engine.
those function 'scripted' , more or less did.
what : load , evaluate bootstrap.[language_extension]
before evaluating other input in new context.
you create such scripts each language intend support.
Comments
Post a Comment