Discussion:
Is there a way to reference the engine from a linker?
Paulo Lopes
2018-03-15 19:02:40 UTC
Permalink
Hi,

I'm writing a guarded linker to allow custom casting from JSObject to Vertx
custom types. So far so good, the basic tests seem to work but I think I'm
writing too much boiler plate code as I need in many times to have an
intermediate conversion from JSObject to Map or List.

So I know that in the engine I could call Java.asJSONCompatible() but I
don't see how I could get a reference to the engine in the linker.

Could anyone see a way or let me know if there is a open API to do this?

Thanks!
Paulo
Sundararajan Athijegannathan
2018-03-16 01:28:15 UTC
Permalink
Hi,

Dynalink and its pluggable linkers are independent of Nashorn. If you
want to access the current engine, you've to arrange to pass it.

Thanks,
-Sundar
Post by Paulo Lopes
Hi,
I'm writing a guarded linker to allow custom casting from JSObject to Vertx
custom types. So far so good, the basic tests seem to work but I think I'm
writing too much boiler plate code as I need in many times to have an
intermediate conversion from JSObject to Map or List.
So I know that in the engine I could call Java.asJSONCompatible() but I
don't see how I could get a reference to the engine in the linker.
Could anyone see a way or let me know if there is a open API to do this?
Thanks!
Paulo
Attila Szegedi
2018-04-04 17:20:50 UTC
Permalink
Sorry for an awfully late response, but hope it might still help: Java.asJSONCompatible delegates on the Java side to jdk.nashorn.api.scripting.wrapAsJSONCompatible. It’s part of Nashorn’s supported public API, so it should be fine for you to call it.

I’m actually wondering how useful is that API method - you need to pass a “homeGlobal” parameter, and honestly I’m not really sure how you’d obtain that. Maybe Sundar has an idea.

Attila.
Post by Paulo Lopes
Hi,
I'm writing a guarded linker to allow custom casting from JSObject to Vertx
custom types. So far so good, the basic tests seem to work but I think I'm
writing too much boiler plate code as I need in many times to have an
intermediate conversion from JSObject to Map or List.
So I know that in the engine I could call Java.asJSONCompatible() but I
don't see how I could get a reference to the engine in the linker.
Could anyone see a way or let me know if there is a open API to do this?
Thanks!
Paulo
Paulo Lopes
2018-04-04 17:28:35 UTC
Permalink
Hi,

I've worked around it by using:


ScriptUtils.convert(obj, Map.class)


When ever I needed to get a script object as a map or replacing the class with List.class when I need a list...

Is that a safe way to do it?


  Original Message  
From: ***@gmail.com
Sent: April 4, 2018 7:21 PM
To: ***@redhat.com
Cc: nashorn-***@openjdk.java.net
Subject: Re: Is there a way to reference the engine from a linker?

Sorry for an awfully late response, but hope it might still help: Java.asJSONCompatible delegates on the Java side to jdk.nashorn.api.scripting.wrapAsJSONCompatible. It’s part of Nashorn’s supported public API, so it should be fine for you to call it.

I’m actually wondering how useful is that API method - you need to pass a “homeGlobal” parameter, and honestly I’m not really sure how you’d obtain that. Maybe Sundar has an idea.

Attila.
Post by Paulo Lopes
Hi,
I'm writing a guarded linker to allow custom casting from JSObject to Vertx
custom types. So far so good, the basic tests seem to work but I think I'm
writing too much boiler plate code as I need in many times to have an
intermediate conversion from JSObject to Map or List.
So I know that in the engine I could call Java.asJSONCompatible() but I
don't see how I could get a reference to the engine in the linker.
Could anyone see a way or let me know if there is a open API to do this?
Thanks!
Pa
Attila Szegedi
2018-04-04 17:50:21 UTC
Permalink
Oh, good one. Yes, I think that’ll do. It engages Nashorn’s built-in linker chain to find an appropriate conversion.

Attila.
Post by Paulo Lopes
Hi,
ScriptUtils.convert(obj, Map.class)
When ever I needed to get a script object as a map or replacing the class with List.class when I need a list...
Is that a safe way to do it?
Original Message
Sent: April 4, 2018 7:21 PM
Subject: Re: Is there a way to reference the engine from a linker?
Sorry for an awfully late response, but hope it might still help: Java.asJSONCompatible delegates on the Java side to jdk.nashorn.api.scripting.wrapAsJSONCompatible. It’s part of Nashorn’s supported public API, so it should be fine for you to call it.
I’m actually wondering how useful is that API method - you need to pass a “homeGlobal” parameter, and honestly I’m not really sure how you’d obtain that. Maybe Sundar has an idea.
Attila.
Post by Paulo Lopes
Hi,
I'm writing a guarded linker to allow custom casting from JSObject to Vertx
custom types. So far so good, the basic tests seem to work but I think I'm
writing too much boiler plate code as I need in many times to have an
intermediate conversion from JSObject to Map or List.
So I know that in the engine I could call Java.asJSONCompatible() but I
don't see how I could get a reference to the engine in the linker.
Could anyone see a way or let me know if there is a open API to do this?
Thanks!
Paulo
Loading...