title: Sharing JavaScript arrays and objects between multiple nashorn ScriptEngines
body: <p>I am having a problem with nashorn, and I don't quite get how native constructors work (Object, Array, etc).</p>
  
  <p>My issue involves several ScriptEngines through the lifecycle of an application, and some of them can create functions.</p>
  
  <p>I try to use those functions in new ScriptEngines, the problem is that I cannot check if an object is of a given type (array instanceof Array), because that Array wasn't generated by this instance's Array constructor.</p>
  
  <p>Here is a test to replicate it:</p>
  
  <pre><code>def "Just testing"() {                                                                                                                                      
  when:                                                                                                                                                     
    def manager = new ScriptEngineManager()                                                                                                                 
    def engine1 = manager.getEngineByName("nashorn")                                                                                                        
    def engine2 = manager.getEngineByName("nashorn")                                                                                                                
  
    def arrImpl = engine1.eval("[]")                                                                                                                        
  
    engine2.context.setAttribute("arr", arrImpl, ScriptContext.ENGINE_SCOPE)                                                                                
  
    def val = engine2.eval("arr instanceof Array")                                                                                                          
  
  then:                                                                                                                                                     
    val == true                                                                                                                                             
  
  }
  </code></pre>
  
  <p>I read this article <a href="https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes" rel="nofollow">https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes</a> and tried to solve it like this, but still having no luck</p>
  
  <pre><code>def "Just testing"() {                                                                                                                                      
  when:                                                                                                                                                     
    def manager = new ScriptEngineManager()                                                                                                                 
    def engine1 = manager.getEngineByName("nashorn")                                                                                                        
    def engine2 = manager.getEngineByName("nashorn")                                                                                                        
  
    def context = new SimpleScriptContext()                                                                                                                 
    def bindings = engine1.getContext().getBindings(ScriptContext.ENGINE_SCOPE)                                                                             
    context.setBindings(bindings, ScriptContext.ENGINE_SCOPE)                                                                                                              
  
    def arrImpl = engine1.eval("[]")                                                                                                                        
  
  
    context.setAttribute("arr", arrImpl, ScriptContext.ENGINE_SCOPE)                                                                                        
    def val = engine2.eval("arr instanceof Array", context)                                                                                                 
  
  then:                                                                                                                                                     
    val == true                                                                                                                                             
  
  }
  </code></pre>
  
  <p>Do you have any idea of how to make it work?</p>