Adding js functionality to existing ScriptObjectMirror

A. Sundararajan sundararajan.athijegannathan at
Mon Mar 23 14:26:01 UTC 2015

You could implement additional function in Java as given below..

import javax.script.*;
import jdk.nashorn.api.scripting.*;

public class Main {
   public static void main(String[] a) throws Exception {
     ScriptEngineManager m = new ScriptEngineManager();
     ScriptEngine e = m.getEngineByName("nashorn");
     ScriptObjectMirror obj  = (ScriptObjectMirror)
               e.eval("var obj = ({ foo: 33 }); obj");

     // implement a function object in Java using AbstractJavaObject
     obj.setMember("func", new AbstractJSObject() {
         // hook for actual call of "func"
         public Object call(Object thiz, Object... args) {
             return args[0].toString().toUpperCase();

         // yes, I am a function!
         public boolean isFunction() { return true; }

     // call it from script
     // call it from java
     System.out.println(obj.callMember("func", "hello"));


On Monday 23 March 2015 06:36 PM, BURRIS Thomas wrote:
> Hi ... I have a cache of ScriptObjectMirrors (SOM) that wrap some compiled javascript and -- at runtime -- I need to inject an additional function into the SOMs. Something like this
>       ScriptObjectMirror som = SOMPool.get();
>       som.setMember("someFn", <WHAT-HERE?>);
>       // so that I am now enabled to do this:
>       som.callMember("someFn", "arg1", "arg2");
> Possible? I've tried a bunch of approaches, none have worked.
> thanks!
> This email and any attachments are intended solely for the use of the individual or entity to whom it is addressed and may be confidential and/or privileged.
> If you are not one of the named recipients or have received this email in error,
> (i) you should not read, disclose, or copy it,
> (ii) please notify sender of your receipt by reply email and delete this email and all attachments,
> (iii) Dassault Systemes does not accept or assume any liability or responsibility for any use of or reliance on this email.
> For other languages, go to

More information about the nashorn-dev mailing list