You could use any of the following options:

1) Write a Java object with bean-style getters and getters - which can be used by scripts 'as is'. This is the most direct and simple way. Users can use "new" to create objects and use "obj.prop" to access/write to bean-properties and call public Java methods. If there are 'event' callback methods accepting interfaces, scripts can pass a script implementation of interfaces or just pass a script function if the interface is a SAM interface.

2) You could implement jdk.nashorn.api.scripting.JSObject interface in your class and expose the objects of the same. You could it make your object behave like function by implementing call and newObject methods. And implement getMember and putMember methods to expose properties to scripts.

3) You could have a script object whose properties are bound to a Java object. Example:

    var obj = {};
    Object.bindProperties(obj, myJavaObject);
    print(obj.foo); // calls myJavaObject.getFoo() bean getter

The above 'binds' bean-style properties of your Java object to the script object "obj". The script object could have usual proto and so on. This flexible option allows your script object to behave as normal script object -- but some properties being implemented in Java.

All these are documented in

https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes
https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions

-Sundar

On Monday 13 January 2014 03:19 PM, buddhi mihara wrote:
hi,
i am writing a server side js language just like node.jswhat is the proper way 
to expose custom object just like NativeMath or custom constructor function 
just like Native Date to the script environment
thank you                                       

Reply via email to