Java calling code:
import jdk.nashorn.api.scripting.*; .... myCustomHashMap dataStore = new myCustomHashMap(); ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine engine = sem.getEngineByName("nashorn"); engine.put("dataStore",dataStore); engine.eval(new java.io.FileReader("test.js")); ((Invocable)engine).invokeFunction("jsTestFunc", "testStr" );
Javascript:
function jsTestFunc (testParam) { dataStore.a = [1,2,3]; dataStore.b = {First:"John",Last:"Doe",age:37}; }
Goal:
I need to JSONify the dataStore after the script execution with no dependence on the script for assistance dataStore.a -> jdk.nashorn.internal.objects.NativeArray dataStore.b -> jdk.nashorn.internal.scripts.JO4
For each Map value, I've tried and failed with:
- Casting to ScriptObject or ScriptObjectMirror
- Casting to Map or List
- Accessing JO4/NativeArray methods directly
- ScriptUtils.wrap() / ScriptUtils.unwrap()
I've tried overriding the HashMap.put()
method, but it appears not to be converted to a ScriptObjectMirror
on assignments, only on explicit function calls:
dataStore.x = [1,2,3] ; -> jdk.nashorn.internal.objects.NativeArray javaHost.javaFunc( [1,2,3] ); -> ScriptObjectMirror
I really need to use myCustomHashMap (it timestamps changes and maintains a change list, etc), so I can't radically alter this arrangement. What can I do to get this data back out?