aboutsummaryrefslogtreecommitdiff
path: root/test/src/jdk/nashorn/internal/runtime/LexicalBindingTest.java
blob: 2cd0bf01ee10ea89ab44ad35cf7d9657a13e0307 (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
/*
 * Copyright (c) 2014, 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.internal.runtime;

import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import org.testng.annotations.Test;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;

import static org.testng.Assert.assertEquals;

/**
 * Top-level lexical binding tests.
 *
 * @test
 * @run testng jdk.nashorn.internal.runtime.LexicalBindingTest
 */
@SuppressWarnings("javadoc")
public class LexicalBindingTest {

    final static String LANGUAGE_ES6 = "--language=es6";
    final static int NUMBER_OF_CONTEXTS = 20;
    final static int MEGAMORPHIC_LOOP_COUNT = 20;

    /**
     * Test access to global var-declared variables for shared script classes with multiple globals.
     */
    @Test
    public static void megamorphicVarTest() throws ScriptException, InterruptedException {
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        final ScriptEngine e = factory.getScriptEngine();
        final ScriptContext[] contexts = new ScriptContext[NUMBER_OF_CONTEXTS];
        final String sharedScript = "foo";


        for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) {
            final ScriptContext context = contexts[i] = new SimpleScriptContext();
            final Bindings b = e.createBindings();
            context.setBindings(b, ScriptContext.ENGINE_SCOPE);
            assertEquals(e.eval("var foo = '" + i + "';", context), null);
        }

        for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) {
            final ScriptContext context = contexts[i];
            assertEquals(e.eval(sharedScript, context), String.valueOf(i));
        }
    }

    /**
     * Test access to global lexically declared variables for shared script classes with multiple globals.
     */
    @Test
    public static void megamorphicMultiGlobalLetTest() throws ScriptException, InterruptedException {
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6);
        final ScriptContext[] contexts = new ScriptContext[NUMBER_OF_CONTEXTS];
        final String sharedScript = "foo";


        for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) {
            final ScriptContext context = contexts[i] = new SimpleScriptContext();
            final Bindings b = e.createBindings();
            context.setBindings(b, ScriptContext.ENGINE_SCOPE);
            assertEquals(e.eval("let foo = '" + i + "';", context), null);
        }

        for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) {
            final ScriptContext context = contexts[i];
            assertEquals(e.eval(sharedScript, context), String.valueOf(i));
        }
    }


    /**
     * Test access to global lexically declared variables for shared script classes with single global.
     */
    @Test
    public static void megamorphicSingleGlobalLetTest() throws ScriptException, InterruptedException {
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6);
        final String sharedGetterScript = "foo";
        final String sharedSetterScript = "foo = 1";

        for (int i = 0; i < MEGAMORPHIC_LOOP_COUNT; i++) {
            assertEquals(e.eval(sharedSetterScript), 1);
            assertEquals(e.eval(sharedGetterScript), 1);
            assertEquals(e.eval("delete foo; a" + i + " = 1; foo = " + i + ";"), i);
            assertEquals(e.eval(sharedGetterScript), i);
        }

        assertEquals(e.eval("let foo = 'foo';"), null);
        assertEquals(e.eval(sharedGetterScript), "foo");
        assertEquals(e.eval(sharedSetterScript), 1);
        assertEquals(e.eval(sharedGetterScript), 1);
        assertEquals(e.eval("this.foo"), MEGAMORPHIC_LOOP_COUNT - 1);
    }

    /**
     * Test access to global lexically declared variables for shared script classes with single global.
     */
    @Test
    public static void megamorphicInheritedGlobalLetTest() throws ScriptException, InterruptedException {
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6);
        final String sharedGetterScript = "foo";
        final String sharedSetterScript = "foo = 1";

        for (int i = 0; i < MEGAMORPHIC_LOOP_COUNT; i++) {
            assertEquals(e.eval(sharedSetterScript), 1);
            assertEquals(e.eval(sharedGetterScript), 1);
            assertEquals(e.eval("delete foo; a" + i + " = 1; Object.prototype.foo = " + i + ";"), i);
            assertEquals(e.eval(sharedGetterScript), i);
        }

        assertEquals(e.eval("let foo = 'foo';"), null);
        assertEquals(e.eval(sharedGetterScript), "foo");
        assertEquals(e.eval(sharedSetterScript), 1);
        assertEquals(e.eval(sharedGetterScript), 1);
        assertEquals(e.eval("this.foo"), MEGAMORPHIC_LOOP_COUNT - 1);
    }

    /**
     * Test multi-threaded access to global lexically declared variables for shared script classes with multiple globals.
     */
    @Test
    public static void multiThreadedLetTest() throws ScriptException, InterruptedException {
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6);
        final Bindings b = e.createBindings();
        final ScriptContext origContext = e.getContext();
        final ScriptContext newCtxt = new SimpleScriptContext();
        newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
        final String sharedScript = "foo";

        assertEquals(e.eval("let foo = 'original context';", origContext), null);
        assertEquals(e.eval("let foo = 'new context';", newCtxt), null);

        final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
        final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        assertEquals(e.eval("foo = 'newer context';", newCtxt), "newer context");
        final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
        final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));

        t3.start();
        t4.start();
        t3.join();
        t4.join();

        assertEquals(e.eval(sharedScript), "original context");
        assertEquals(e.eval(sharedScript, newCtxt), "newer context");
    }

    private static class ScriptRunner implements Runnable {

        final ScriptEngine engine;
        final ScriptContext context;
        final String source;
        final Object expected;
        final int iterations;

        ScriptRunner(final ScriptEngine engine, final ScriptContext context, final String source, final Object expected, final int iterations) {
            this.engine = engine;
            this.context = context;
            this.source = source;
            this.expected = expected;
            this.iterations = iterations;
        }

        @Override
        public void run() {
            try {
                for (int i = 0; i < iterations; i++) {
                    assertEquals(engine.eval(source, context), expected);
                }
            } catch (final ScriptException se) {
                throw new RuntimeException(se);
            }
        }
    }
}