Native Nashorn Object vs JSObject

Serguei Mourachov smourachov at
Thu Nov 6 18:19:27 UTC 2014

On 11/6/2014 8:22 AM, A. Sundararajan wrote:
> Will you please post full source of your JSObject? (just enough to 
> reproduce issue you're talking about).
> -Sundar
> On Wednesday 05 November 2014 05:14 AM, Serguei Mourachov wrote:
>> It looks like some operations that are available for native Nashorn 
>> objects, are not implemented for JSObject.
>> For example, following script works and prints '6': engine.eval("var 
>> obj={};var key={}; obj[key]=6;print(obj[key])");
>> In case when 'obj' is an implementation of  JSObject, the script runs 
>> without any error, printing 'null'.
>> SM
here is the sample code:

     public static void main(String[] args) throws Exception {
         NashornScriptEngineFactory factory = new 
         ScriptEngine engine = factory.getScriptEngine();

         Bindings b = engine.getBindings(ScriptContext.ENGINE_SCOPE);

         AbstractJSObject jsobj = new AbstractJSObject(){
             Map<String, Object> map = new HashMap<>();

             public void setMember(String name, Object value) {
                 map.put(name, value);

             public Object getMember(String name) {
                 return map.get(name);

             public void removeMember(String name) {

             public boolean hasMember(String name) {
                 return map.containsKey(name);
         b.put("jsobj", jsobj);
         engine.eval("var obj={}; var key={}; obj[key]=6;print(obj[key])");


if you replace var key={} with var key='foo' the code works as expected


More information about the nashorn-dev mailing list