java - Reading Nashorn JO4 and NativeArray -
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 jsonify datastore after script execution no dependence on script assistance datastore.a -> jdk.nashorn.internal.objects.nativearray datastore.b -> jdk.nashorn.internal.scripts.jo4
for each map value, i've tried , failed with:
casting scriptobject or scriptobjectmirror casting map or list accessing jo4/nativearray methods directly scriptutils.wrap() / scriptutils.unwrap()i've tried overriding hashmap.put()
method, appears not converted scriptobjectmirror
on assignments, on explicit function calls:
datastore.x = [1,2,3] ; -> jdk.nashorn.internal.objects.nativearray javahost.javafunc( [1,2,3] ); -> scriptobjectmirror
i need utilize mycustomhashmap (it timestamps changes , maintains alter list, etc), can't radically alter arrangement. can info out?
this bug.
with jdk8u40 onwards, script objects converted scriptobjectmirror whenever script objects passed java layer - object type params or assigned object[] element. such wrapped mirror instances automatically unwrapped when execution crosses script boundary. i.e., java method returns object type value happens scriptobjectmirror object, script caller see scriptobject instance (mirror gets unwrapped automatically)
https://wiki.openjdk.java.net/display/nashorn/nashorn+jsr223+engine+notes
with jdk8u40 access release
java:
public class myobject extends hashmap<string, object> { @override public object put(string key, object value) { system.out.println("key: " + key + " value: " + value + " class: " + value.getclass()); homecoming super.put(key, value); } }
javascript:
var myobject = java.type("my.app.myobject"); var test = new myobject; test.object = {test : "object"}; test.array = [1,2,3];
console:
key: object value: [object object] class: class jdk.nashorn.api.scripting.scriptobjectmirror key: array value: [object array] class: class jdk.nashorn.api.scripting.scriptobjectmirror
java javascript nashorn
No comments:
Post a Comment