Rhino to Nashorn - Adding properties/bean available to scope
<p>In Rhino, I have a Scriptable bean like below </p>

<pre><code> /**
 * 
 */
package test.rhino;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.mozilla.javascript.FunctionObject;
import org.mozilla.javascript.Scriptable;

/**
 * @author 
 * 
 */
public class SomeBean implements Scriptable, Map&lt;String,Object&gt; {

    /**
     * The current values for this object.
     */
    private HashMap&lt;String, Object&gt; values = new HashMap&lt;&gt;();

    /**
     * 
     */
    public SomeBean() {
        System.out.println("SomeBean();");
    }

    /*
     * @see org.mozilla.javascript.Scriptable#getClassName()
     */
    @Override
    public String getClassName() {
        return "SomeBean";
    }

    /*
     * @see org.mozilla.javascript.Scriptable#get(java.lang.String,
     * org.mozilla.javascript.Scriptable)
     */
    @Override
    public Object get(String name, Scriptable start) {

        System.out.println("Get is called.");
        System.out.println("Called for this" + name + " and returned :" + values.get(name));

        return values.get(name);
    }

    /*
     * @see org.mozilla.javascript.Scriptable#put(java.lang.String,
     * org.mozilla.javascript.Scriptable, java.lang.Object)
     */
    @Override
    public void put(String name, Scriptable start, Object value) {
        System.out.println("Put is called. Input name: " + name + "\n Input values: " + value);

        values.put(name, value);

    }

    @Override
    public Object get(int index, Scriptable start) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean has(String name, Scriptable start) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean has(int index, Scriptable start) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void put(int index, Scriptable start, Object value) {
        // TODO Auto-generated method stub

    }

    @Override
    public void delete(String name) {
        // TODO Auto-generated method stub

    }

    @Override
    public void delete(int index) {
        // TODO Auto-generated method stub

    }

    @Override
    public Scriptable getPrototype() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setPrototype(Scriptable prototype) {
        // TODO Auto-generated method stub

    }

    @Override
    public Scriptable getParentScope() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setParentScope(Scriptable parent) {
        // TODO Auto-generated method stub

    }

    @Override
    public Object[] getIds() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object getDefaultValue(Class&lt;?&gt; hint) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean hasInstance(Scriptable instance) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean containsKey(Object key) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean containsValue(Object value) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Object get(Object key) {
        System.out.println("MAP -- Get is called.");
        // TODO Auto-generated method stub
        return values.get(key);
    }

    @Override
    public Object put(String key, Object value) {
        values.put(key, value);
        System.out.println("MAP -- Put is called.");
        // TODO Auto-generated method stub
        return value;
    }

    @Override
    public Object remove(Object key) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void putAll(Map&lt;? extends String, ? extends Object&gt; m) {
        // TODO Auto-generated method stub

    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

    @Override
    public Set&lt;String&gt; keySet() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Collection&lt;Object&gt; values() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Set&lt;java.util.Map.Entry&lt;String, Object&gt;&gt; entrySet() {
        // TODO Auto-generated method stub
        return null;
    }

    private static Scriptable globalPrototype;

    public static void finishInit(Scriptable scope, FunctionObject constructor, Scriptable prototype) {
        System.out.println("finishInit is called.");
        globalPrototype = prototype;
    }

}
</code></pre>

<p>I can retrieve this bean as a Scriptable object and pass it to the eval() function of a compiled script like below.</p>

<pre><code>String src5 = "(function(){return Name;})();\n";
Script sc = getCompiledScript(src5);
SomeBean sb = new SomeBean();
sb.put("Name", "Matt Murdock");
Scriptable sp = (Scriptable) sb;
result = script.exec(context, sp);
</code></pre>

<p>The result print Matt Murdock. We can refer to properties directly here since SomeBean instance is passed as scope to the script execution.</p>

<p>I am unable to find an equivalent way in Nashorn. I can use the source to get a CompiledScript instance but i cannot add SomeBean to the binding without giving it a key and i cannot call its members ( Name ) directly in my JavaScript function. The below code throws error which is obvious since we cannot access the properties without the prefix key. </p>

<pre><code>NSomeOtherBean nsob = new NSomeOtherBean(); // extends AbstractJSObject
nsob.setMember("Name", "Bruce Wayne");

Bindings binding = engine.createBindings();
binding.put("nsob", nsob);
engine.setBindings(binding, ScriptContext.GLOBAL_SCOPE);
result = engine.eval("(function(){return Name;})();\n",binding);
</code></pre>

<p>Is there a way in Nashorn to add properties of NSomeOtherBean instance to the scope and access them without using a prefix? like how we can do it in Rhino. </p>