How could I inject new buil-iin constructors / objects to nashorn?
Sundararajan Athijegannathan
sundararajan.athijegannathan at oracle.com
Fri Sep 25 10:31:04 UTC 2015
Hi,
You may want to look at JSObject
(https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/JSObject.html,
https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/AbstractJSObject.html)
Simple example here ->
https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-jsobject
If you want to do it in script itself (perhaps pre-loaded script for
your model), you can use JSAdapter.
Simple example here ->
https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-JSAdapterconstructor
Hope this helps,
-Sundar
On 9/24/2015 9:03 PM, Lev Serebryakov wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA512
>
>
> I'm writing some framework in Java, and want to use JavaScript as
> glue/DSL language for it (think about GNU Radio with C++ core classes
> and Pythjon glue, for example).
>
> This framework contains meta-model for its classes and objects,
> implemented as Java annotations. It helps to generate different
> boilerplate code automatically and hide some tedious technical details
> from framework's users.
>
> I want to export this meta-model to JavaScript (Nashorn) in most
> efficient and JavaScript-idiomatic way. That is, I need built-in-like
> constructors and objects, which is not exact reflection of Java
> classes and objects, but reflection of my meta-model. Also, I want to
> do it in runtime, not compile-time, as new classes (modules) for my
> framework could be added by user and I don't want to force any
> pre-processors and additional build steps. Of course, I could generate
> code in runtime with ASM (it is used in framework anyway).
>
> What is proper way to do this? All documentation and examples shows
> how to use Java classes and objects from JavaScript executed by
> Nashorn, but it is direct mapping. Is here any good way to wrap my
> meta-model for Nashorn to make it looks as JavaScript-native as
> JavaScript's Number or String or Array?
>
> Other feature I want to have is to provide users way to implement new
> modules (classes) for this framework, but without all these
> annotations. Again, most samples shows how to make "Anonymous" class
> in JavaScript (like "var i = new new java.util.Iterator({ ...});")
> which looks not very JavaScript-friendly...
>
> Sorry, if this list is not suitable for such questions, but I cannot
> find any other Nashorn-related mailing list and can not google answer
> to my question on Internet, as all posts and articles about Nashorn
> are very superficial.
>
> - --
> // Black Lion AKA Lev Serebryakov
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2
>
> iQJ8BAEBCgBmBQJWBBe9XxSAAAAAAC4AKGlzc3Vlci1mcHJAbm90YXRpb25zLm9w
> ZW5wZ3AuZmlmdGhob3JzZW1hbi5uZXRGOTZEMUNBMEI1RjQzMThCNjc0QjMzMEFF
> QUIwM0M1OEJGREM0NzhGAAoJEOqwPFi/3EePcSUQANP8ljdnnE7Lug+sn5S38cee
> cKhBdQWqod/VzM3Ft90innbw+4HHuSTJpOw3LcxaRzskcRC7WSEvG7uJpRhmIWow
> hOx521kCwmrprRee93eaJFJ+Szue3YBoIvcfHIJRglaljsiSt4DkdhjEJLkE+ct/
> Dsit8KCy37UAR/sc82rhNM3Oa/hPNMvKi+C9LL5Eo/a5tffV7FLbGKcCoTHBp1eH
> lq2HpnWVwCRLoL1YVuPmnrL/BDkkogr1YvCeM/TfWnM6EPnyDQq/Lbxckj2WPRJe
> MUmtoCZ9rmlOCADn+8ag+yXnaeEG6+yj12K0HJJf9PP3PE/xXUe6nQmkQysVVJqj
> hG2RwkOQxGsLCFiw9rlmHMems/axOCOd2u3GE7R51W9+Cc+G2Fb9vtnSf7kkYlUq
> VNJiU8Tu2LnfaSN7gMmWBKMLUZXoxzueEbqO2P8katqwKusJB1RizF8Ev7pdvFgt
> DbaKQW4JQIxNf8lG9Xj20rULTBazftd8rjsmUGt1hNHtjmd5oGKtso59epWR6bKY
> c45keo1L3PVSnlXn9c2E/j2iwD2LrnIyYE4OzdkjvfQK2kvwE9Lj5Pm1BwZ04I3g
> hicRnhaeey2oeafqeZMBN2VBqZjKmo2Ch38nwVTPU2PcbKc3UH6o6mIVRzTtDd7i
> cin1TvpoN+ILR6r7uTLq
> =wLD2
> -----END PGP SIGNATURE-----
More information about the nashorn-dev
mailing list