aboutsummaryrefslogtreecommitdiff
path: root/test/src/jdk/nashorn/api/scripting/test/ScriptObjectMirrorTest.java
blob: 7656dc6b8677ba671c7154bd7512d88829bc91ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.nashorn.api.scripting.test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import javax.script.Bindings;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import jdk.nashorn.api.scripting.AbstractJSObject;
import jdk.nashorn.api.scripting.JSObject;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.testng.annotations.Test;

/**
 * Tests to check jdk.nashorn.api.scripting.ScriptObjectMirror API.
 */
@SuppressWarnings("javadoc")
public class ScriptObjectMirrorTest {

    @SuppressWarnings("unchecked")
    @Test
    public void reflectionTest() throws ScriptException {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        e.eval("var obj = { x: 344, y: 'nashorn' }");

        int count = 0;
        Map<Object, Object> map = (Map<Object, Object>) e.get("obj");
        assertFalse(map.isEmpty());
        assertTrue(map.keySet().contains("x"));
        assertTrue(map.containsKey("x"));
        assertTrue(map.values().contains("nashorn"));
        assertTrue(map.containsValue("nashorn"));
        for (final Map.Entry<?, ?> ex : map.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(344 == ((Number) ex.getValue()).doubleValue());
                count++;
            } else if (key.equals("y")) {
                assertEquals(ex.getValue(), "nashorn");
                count++;
            }
        }
        assertEquals(2, count);
        assertEquals(2, map.size());

        // add property
        map.put("z", "hello");
        assertEquals(e.eval("obj.z"), "hello");
        assertEquals(map.get("z"), "hello");
        assertTrue(map.keySet().contains("z"));
        assertTrue(map.containsKey("z"));
        assertTrue(map.values().contains("hello"));
        assertTrue(map.containsValue("hello"));
        assertEquals(map.size(), 3);

        final Map<Object, Object> newMap = new HashMap<>();
        newMap.put("foo", 23.0);
        newMap.put("bar", true);
        map.putAll(newMap);

        assertEquals(e.eval("obj.foo"), 23.0);
        assertEquals(e.eval("obj.bar"), true);

        // remove using map method
        map.remove("foo");
        assertEquals(e.eval("typeof obj.foo"), "undefined");

        count = 0;
        e.eval("var arr = [ true, 'hello' ]");
        map = (Map<Object, Object>) e.get("arr");
        assertFalse(map.isEmpty());
        assertTrue(map.containsKey("length"));
        assertTrue(map.containsValue("hello"));
        for (final Map.Entry<?, ?> ex : map.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("0")) {
                assertEquals(ex.getValue(), Boolean.TRUE);
                count++;
            } else if (key.equals("1")) {
                assertEquals(ex.getValue(), "hello");
                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(map.size(), 2);

        // add element
        map.put("2", "world");
        assertEquals(map.get("2"), "world");
        assertEquals(map.size(), 3);

        // remove all
        map.clear();
        assertTrue(map.isEmpty());
        assertEquals(e.eval("typeof arr[0]"), "undefined");
        assertEquals(e.eval("typeof arr[1]"), "undefined");
        assertEquals(e.eval("typeof arr[2]"), "undefined");
    }

    @Test
    public void jsobjectTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        try {
            e.eval("var obj = { '1': 'world', func: function() { return this.bar; }, bar: 'hello' }");
            final ScriptObjectMirror obj = (ScriptObjectMirror) e.get("obj");

            // try basic get on existing properties
            if (!obj.getMember("bar").equals("hello")) {
                fail("obj.bar != 'hello'");
            }

            if (!obj.getSlot(1).equals("world")) {
                fail("obj[1] != 'world'");
            }

            if (!obj.callMember("func", new Object[0]).equals("hello")) {
                fail("obj.func() != 'hello'");
            }

            // try setting properties
            obj.setMember("bar", "new-bar");
            obj.setSlot(1, "new-element-1");
            if (!obj.getMember("bar").equals("new-bar")) {
                fail("obj.bar != 'new-bar'");
            }

            if (!obj.getSlot(1).equals("new-element-1")) {
                fail("obj[1] != 'new-element-1'");
            }

            // try adding properties
            obj.setMember("prop", "prop-value");
            obj.setSlot(12, "element-12");
            if (!obj.getMember("prop").equals("prop-value")) {
                fail("obj.prop != 'prop-value'");
            }

            if (!obj.getSlot(12).equals("element-12")) {
                fail("obj[12] != 'element-12'");
            }

            // delete properties
            obj.removeMember("prop");
            if ("prop-value".equals(obj.getMember("prop"))) {
                fail("obj.prop is not deleted!");
            }

            // Simple eval tests
            assertEquals(obj.eval("typeof Object"), "function");
            assertEquals(obj.eval("'nashorn'.substring(3)"), "horn");
        } catch (final Exception exp) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
    }

    @Test
    public void scriptObjectMirrorToStringTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        try {
            final Object obj = e.eval("new TypeError('wrong type')");
            assertEquals(obj.toString(), "TypeError: wrong type", "toString returns wrong value");
        } catch (final Throwable t) {
            t.printStackTrace();
            fail(t.getMessage());
        }

        try {
            final Object obj = e.eval("function func() { print('hello'); }");
            assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value");
        } catch (final Throwable t) {
            t.printStackTrace();
            fail(t.getMessage());
        }
    }

    @Test
    public void mirrorNewObjectGlobalFunctionTest() throws ScriptException {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        final ScriptEngine e2 = m.getEngineByName("nashorn");

        e.eval("function func() {}");
        e2.put("foo", e.get("func"));
        final ScriptObjectMirror e2global = (ScriptObjectMirror)e2.eval("this");
        final Object newObj = ((ScriptObjectMirror)e2global.getMember("foo")).newObject();
        assertTrue(newObj instanceof ScriptObjectMirror);
    }

    @Test
    public void mirrorNewObjectInstanceFunctionTest() throws ScriptException {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        final ScriptEngine e2 = m.getEngineByName("nashorn");

        e.eval("function func() {}");
        e2.put("func", e.get("func"));
        final ScriptObjectMirror e2obj = (ScriptObjectMirror)e2.eval("({ foo: func })");
        final Object newObj = ((ScriptObjectMirror)e2obj.getMember("foo")).newObject();
        assertTrue(newObj instanceof ScriptObjectMirror);
    }

    @Test
    public void indexPropertiesExternalBufferTest() throws ScriptException {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        final ScriptObjectMirror obj = (ScriptObjectMirror)e.eval("var obj = {}; obj");
        final ByteBuffer buf = ByteBuffer.allocate(5);
        int i;
        for (i = 0; i < 5; i++) {
            buf.put(i, (byte)(i+10));
        }
        obj.setIndexedPropertiesToExternalArrayData(buf);

        for (i = 0; i < 5; i++) {
            assertEquals((byte)(i+10), ((Number)e.eval("obj[" + i + "]")).byteValue());
        }

        e.eval("for (i = 0; i < 5; i++) obj[i] = 0");
        for (i = 0; i < 5; i++) {
            assertEquals((byte)0, ((Number)e.eval("obj[" + i + "]")).byteValue());
            assertEquals((byte)0, buf.get(i));
        }
    }

    @Test
    public void conversionTest() throws ScriptException {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        final ScriptObjectMirror arr = (ScriptObjectMirror)e.eval("[33, 45, 23]");
        final int[] intArr = arr.to(int[].class);
        assertEquals(intArr[0], 33);
        assertEquals(intArr[1], 45);
        assertEquals(intArr[2], 23);

        final List<?> list = arr.to(List.class);
        assertEquals(list.get(0), 33);
        assertEquals(list.get(1), 45);
        assertEquals(list.get(2), 23);

        ScriptObjectMirror obj = (ScriptObjectMirror)e.eval(
            "({ valueOf: function() { return 42 } })");
        assertEquals(Double.valueOf(42.0), obj.to(Double.class));

        obj = (ScriptObjectMirror)e.eval(
            "({ toString: function() { return 'foo' } })");
        assertEquals("foo", obj.to(String.class));
    }

    // @bug 8044000: Access to undefined property yields "null" instead of "undefined"
    @Test
    public void mapScriptObjectMirrorCallsiteTest() throws ScriptException {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine engine = m.getEngineByName("nashorn");
        final String TEST_SCRIPT = "typeof obj.foo";

        final Bindings global = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
        engine.eval("var obj = java.util.Collections.emptyMap()");
        // this will drive callsite "obj.foo" of TEST_SCRIPT
        // to use "obj instanceof Map" as it's guard
        engine.eval(TEST_SCRIPT, global);
        // redefine 'obj' to be a script object
        engine.eval("obj = {}");

        final Bindings newGlobal = engine.createBindings();
        // transfer 'obj' from default global to new global
        // new global will get a ScriptObjectMirror wrapping 'obj'
        newGlobal.put("obj", global.get("obj"));

        // Every ScriptObjectMirror is a Map! If callsite "obj.foo"
        // does not see the new 'obj' is a ScriptObjectMirror, it'll
        // continue to use Map's get("obj.foo") instead of ScriptObjectMirror's
        // getMember("obj.foo") - thereby getting null instead of undefined
        assertEquals("undefined", engine.eval(TEST_SCRIPT, newGlobal));
    }

    public interface MirrorCheckExample {
        Object test1(Object arg);
        Object test2(Object arg);
        boolean compare(Object o1, Object o2);
    }

    // @bug 8053910: ScriptObjectMirror causing havoc with Invocation interface
    @Test
    public void checkMirrorToObject() throws Exception {
        final ScriptEngineManager engineManager = new ScriptEngineManager();
        final ScriptEngine engine = engineManager.getEngineByName("nashorn");
        final Invocable invocable = (Invocable)engine;

        engine.eval("function test1(arg) { return { arg: arg }; }");
        engine.eval("function test2(arg) { return arg; }");
        engine.eval("function compare(arg1, arg2) { return arg1 == arg2; }");

        final Map<String, Object> map = new HashMap<>();
        map.put("option", true);

        final MirrorCheckExample example = invocable.getInterface(MirrorCheckExample.class);

        final Object value1 = invocable.invokeFunction("test1", map);
        final Object value2 = example.test1(map);
        final Object value3 = invocable.invokeFunction("test2", value2);
        final Object value4 = example.test2(value2);

        // check that Object type argument receives a ScriptObjectMirror
        // when ScriptObject is passed
        assertEquals(ScriptObjectMirror.class, value1.getClass());
        assertEquals(ScriptObjectMirror.class, value2.getClass());
        assertEquals(ScriptObjectMirror.class, value3.getClass());
        assertEquals(ScriptObjectMirror.class, value4.getClass());
        assertTrue((boolean)invocable.invokeFunction("compare", value1, value1));
        assertTrue(example.compare(value1, value1));
        assertTrue((boolean)invocable.invokeFunction("compare", value3, value4));
        assertTrue(example.compare(value3, value4));
    }

    // @bug 8053910: ScriptObjectMirror causing havoc with Invocation interface
    @Test
    public void mirrorUnwrapInterfaceMethod() throws Exception {
        final ScriptEngineManager engineManager = new ScriptEngineManager();
        final ScriptEngine engine = engineManager.getEngineByName("nashorn");
        final Invocable invocable = (Invocable)engine;
        engine.eval("function apply(obj) { " +
            " return obj instanceof Packages.jdk.nashorn.api.scripting.ScriptObjectMirror; " +
            "}");
        @SuppressWarnings("unchecked")
        final Function<Object,Object> func = invocable.getInterface(Function.class);
        assertFalse((boolean)func.apply(engine.eval("({ x: 2 })")));
    }

    // @bug 8055687: Wrong "this" passed to JSObject.eval call
    @Test
    public void checkThisForJSObjectEval() throws Exception {
        final ScriptEngineManager engineManager = new ScriptEngineManager();
        final ScriptEngine e = engineManager.getEngineByName("nashorn");
        final JSObject jsobj = (JSObject)e.eval("({foo: 23, bar: 'hello' })");
        assertEquals(((Number)jsobj.eval("this.foo")).intValue(), 23);
        assertEquals(jsobj.eval("this.bar"), "hello");
        assertEquals(jsobj.eval("String(this)"), "[object Object]");
        final Object global = e.eval("this");
        assertFalse(global.equals(jsobj.eval("this")));
    }

    @Test
    public void topLevelAnonFuncStatement() throws Exception {
        final ScriptEngineManager engineManager = new ScriptEngineManager();
        final ScriptEngine e = engineManager.getEngineByName("nashorn");
        final JSObject func = (JSObject)e.eval("function(x) { return x + ' world' }");
        assertTrue(func.isFunction());
        assertEquals(func.call(e.eval("this"), "hello"), "hello world");
    }

    // @bug 8170565: JSObject call() is passed undefined for the argument 'thiz'
    @Test
    public void jsObjectThisTest() throws Exception {
        final ScriptEngineManager engineManager = new ScriptEngineManager();
        final ScriptEngine e = engineManager.getEngineByName("nashorn");
        e.put("func", new AbstractJSObject() {
            @Override
            public boolean isFunction() { return true; }

            @Override
            public Object call(Object thiz, Object...args) {
                return thiz;
            }
        });

        assertTrue((boolean)e.eval("func() === this"));

        // check that there is no blind undefined->Global translation!
        assertTrue((boolean)e.eval("typeof(Function.prototype.call.call(func, undefined)) == 'undefined'"));

        // make sure that strict functions don't get translated this for scope calls!
        e.put("sfunc", new AbstractJSObject() {
            @Override
            public boolean isFunction() { return true; }

            @Override
            public boolean isStrictFunction() { return true; }

            @Override
            public Object call(Object thiz, Object...args) {
                return thiz;
            }
        });

        assertTrue((boolean)e.eval("typeof sfunc() == 'undefined'"));
    }
}