That comment is meant for String and int keys. Obviously, JSObjects can not be exactly same as native ScriptObjects. For eg. there is no notion of proto for arbitrary JSObjects and so on. Property (string key) access/set, indexed access/set and delete work like script objects.

-Sundar

On Saturday 08 November 2014 05:04 AM, Serguei Mourachov wrote:
Sundar

In case of native Nashorn object, runtime converts objects, when they are used as "keys", to strings (according to JavaScript spec).
I think the same approach should be used for JSObject.
From the api doc for JSObject: " Nashorn will treat objects of such classes just like nashorn script objects."

In my case, inability to use objects as "keys" makes it impossible to implement iterable JSObject, because in that case Symbol.iterator object is used to access the iterator function.

SM


On 11/7/2014 3:07 AM, A. Sundararajan wrote:
By design, JSObject properties are either Strings or integers. When you use

   jsobj.foo = 33

or

   jsobj["foo"] = 33

JSObject.setMember(String, Object) method will be called for the same by Nashorn's linker. If you use

   jobj[1] = 33;

then JSObject.setSlot(int, Object) method will be called

If you use anything else as property (say a script object as in your example), that would be ignored.

Hope this explains,
-Sundar

Serguei Mourachov wrote:
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 NashornScriptEngineFactory();
        ScriptEngine engine = factory.getScriptEngine();

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

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

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

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

            @Override
            public void removeMember(String name) {
                map.remove(name);
            }

            @Override
            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])");
        engine.eval("jsobj[key]=6;print(jsobj[key])");

    }

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

SM





Reply via email to