For security reasons I'd like to pass to JS functions in Nashorn native Java types.
If I create an engine and I run the following:
NashornScriptEngineFactory nashornScriptEngineFactory = new NashornScriptEngineFactory();
ScriptEngine engine = nashornScriptEngineFactory.getScriptEngine();
engine.eval(script);
Invocable invocable = (Invocable) engine;
JSObject objectWork = (JSObject) engine.get("objectWork");
objectWork.call(null,"eee");
with script being
function objectWork(arg){
print ("arg is "+ typeof arg);
print ("arg.getClass()"+ arg.getClass());
for (var k in arg){
print(k);
}
}
The output is
arg is string
arg.getClass()class java.lang.String
I'm not so happy about the 2nd and I don't know if getClass is exposing any method of the java.lang.Class object. Is there a way to pass something that wraps the Java String in a native one extending Nashorn own JSObject?
JSObjectbut I'm not sure it's a good idea. This class is underjdk.*packages.