add a own ScriptFunction to the nashorn global

A. Sundararajan sundararajan.athijegannathan at oracle.com
Thu Dec 5 02:42:18 PST 2013


jdk.nashorn.internal classes are ... "internal" implementation classes. 
In fact, if you are running with security manager enabled, you'll get 
access exception when attempting to access classes from 
jdk.nashorn.internal.* packages.

For the specific problem you've - namely to expose your own script 
functions from java code, there are two solutions:

*) Implement any @FunctionalInterface interface in JDK (or your own 
@FunctionalInterface)  and pass/put object of the same in a 
javax.script.Bindings or even global scope. Script can access these as 
though these are functions.

*) Implement jdk.nashorn.api.scripting.JSObject in your class and 
implement "call" method on it. Again, nashorn's flexible dynalink based 
linker will treat your JSObject impl. as though it is a function. This 
can also be used to implement "constructor" (newObject method) in Java 
code and so on.

Hope this helps,

Sundar

On Thursday 05 December 2013 11:13 AM, buddhi mihara wrote:
> hi, i need to add a script function like NativeBoolean written in nashorn jdk.nashorn.internal.objects; package , to javascript context.let say my function is ContextifyImpl.it work just fine when i added that ContextifyImpl class to jdk.nashorn.internal.objects package.i configure nasgen tool to generate $Constructor and $Prototype  and when i added my ContextifyImpl class to my own package let say jdk.nashorn.internal.Dilan it giving me error as follow.
> Exception in thread "main" java.lang.IllegalAccessError: tried to access method jdk.nashorn.internal.objects.ScriptFunctionImpl.<init>(Ljava/lang/String;Ljava/lang/invoke/MethodHandle;Ljdk/nashorn/internal/runtime/PropertyMap;[Ljava/lang/invoke/MethodHandle;)V from class jdk.nashorn.internal.Dilan.ContextifyImpl$Constructor	at jdk.nashorn.internal.Dilan.ContextifyImpl$Constructor.<init>(Unknown Source)	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)	at java.lang.reflect.Constructor.newInstance(Constructor.java:414)	at java.lang.Class.newInstance(Class.java:432)	at jdk.nashorn.internal.Dilan.contextify.initConstructors(contextify.java:70)	at jdk.nashorn.internal.Dilan.contextify.makeContextImpl(contextify.java:54)	at jdk.nashorn.internal.Dilan.contextify.makeContext(contextify.java:110)	at jdk.nashorn.internal.scripts.Script$buddhi.runScript(/home/buddhi/Desktop/buddhi.js:4)	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:518)	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:204)	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:365)	at jdk.nashorn.tools.Shell.apply(Shell.java:373)	at jdk.nashorn.tools.Shell.runScripts(Shell.java:302)	at jdk.nashorn.tools.Shell.run(Shell.java:166)	at jdk.nashorn.tools.Shell.main(Shell.java:130)	at jdk.nashorn.tools.Shell.main(Shell.java:109) i need to send a javascript function to javascript exection environment which is written inside my own package.in my projecr consider nashorn is an dependancy.for sake of maintainability i need to place all my code in my own package not in  nashorn packages.how could i do that thank.
> if u can please send me the proper way to configure javascript function using nashorn
> thanks and best regards 		 	   		




More information about the nashorn-dev mailing list