Native Nashorn Object vs JSObject
A. Sundararajan
sundararajan.athijegannathan at oracle.com
Fri Nov 7 11:07:00 UTC 2014
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
>
>
More information about the nashorn-dev
mailing list