nashorn/test/src/jdk/nashorn/internal/runtime/ContextTest.java
author hannesw
Fri, 06 Jun 2014 16:51:53 +0200
changeset 24879 d316854e4249
parent 24206 40c6d45af73f
child 27361 86c4ddb4797b
permissions -rw-r--r--
8046215: Running uncompilable scripts throws NullPointerException Reviewed-by: sundar, jlaskey
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23375
diff changeset
    28
import static jdk.nashorn.internal.runtime.Source.sourceFor;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static org.testng.Assert.assertEquals;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import static org.testng.Assert.assertTrue;
24879
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    31
import static org.testng.Assert.fail;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import java.util.Map;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 19884
diff changeset
    34
import jdk.nashorn.internal.objects.Global;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import jdk.nashorn.internal.runtime.options.Options;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import org.testng.annotations.Test;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
 * Basic Context API tests.
16263
0679aaa72927 8008789: Enable java access and nashorn runtime tests for jtreg
sundar
parents: 16234
diff changeset
    40
 *
0679aaa72927 8008789: Enable java access and nashorn runtime tests for jtreg
sundar
parents: 16234
diff changeset
    41
 * @test
0679aaa72927 8008789: Enable java access and nashorn runtime tests for jtreg
sundar
parents: 16234
diff changeset
    42
 * @run testng jdk.nashorn.internal.runtime.ContextTest
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
public class ContextTest {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
    // basic context eval test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
    public void evalTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
        final Options options = new Options("");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
        final ErrorManager errors = new ErrorManager();
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16183
diff changeset
    50
        final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 19884
diff changeset
    51
        final Global oldGlobal = Context.getGlobal();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
        Context.setGlobal(cx.createGlobal());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
            String code = "22 + 10";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
            assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
            code = "obj = { js: 'nashorn' }; obj.js";
16183
b9578bc4cd68 8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents: 16151
diff changeset
    58
            assertEquals(eval(cx, "<evalTest2>", code), "nashorn");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
        } finally {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
            Context.setGlobal(oldGlobal);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
24879
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    64
    // Make sure trying to compile an invalid script returns null - see JDK-8046215.
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    65
    @Test
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    66
    public void compileErrorTest() {
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    67
        final Options options = new Options("");
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    68
        final ErrorManager errors = new ErrorManager();
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    69
        final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    70
        final Global oldGlobal = Context.getGlobal();
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    71
        Context.setGlobal(cx.createGlobal());
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    72
        try {
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    73
            final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal());
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    74
            if (script != null) {
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    75
                fail("Invalid script compiled without errors");
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    76
            }
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    77
            if (errors.getNumberOfErrors() != 1) {
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    78
                fail("Wrong number of errors: " + errors.getNumberOfErrors());
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    79
            }
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    80
        } finally {
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    81
            Context.setGlobal(oldGlobal);
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    82
        }
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    83
    }
d316854e4249 8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents: 24206
diff changeset
    84
16183
b9578bc4cd68 8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents: 16151
diff changeset
    85
    // basic check for JS reflection access - java.util.Map-like access on ScriptObject
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
    public void reflectionTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
        final Options options = new Options("");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
        final ErrorManager errors = new ErrorManager();
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16183
diff changeset
    90
        final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 16263
diff changeset
    91
        final boolean strict = cx.getEnv()._strict;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 19884
diff changeset
    92
        final Global oldGlobal = Context.getGlobal();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
        Context.setGlobal(cx.createGlobal());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
            final String code = "var obj = { x: 344, y: 42 }";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
            eval(cx, "<reflectionTest>", code);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
            final Object obj = cx.getGlobal().get("obj");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
16183
b9578bc4cd68 8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents: 16151
diff changeset
   101
            assertTrue(obj instanceof ScriptObject);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
16183
b9578bc4cd68 8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents: 16151
diff changeset
   103
            final ScriptObject sobj = (ScriptObject)obj;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
            int count = 0;
16183
b9578bc4cd68 8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents: 16151
diff changeset
   105
            for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
                final Object key = ex.getKey();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
                if (key.equals("x")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
                    assertTrue(ex.getValue() instanceof Number);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
                    assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
                    count++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
                } else if (key.equals("y")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
                    assertTrue(ex.getValue() instanceof Number);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
                    assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
                    count++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
            }
16183
b9578bc4cd68 8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents: 16151
diff changeset
   119
            assertEquals(count, 2);
b9578bc4cd68 8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents: 16151
diff changeset
   120
            assertEquals(sobj.size(), 2);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
            // add property
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 16263
diff changeset
   123
            sobj.put("zee", "hello", strict);
16183
b9578bc4cd68 8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents: 16151
diff changeset
   124
            assertEquals(sobj.get("zee"), "hello");
b9578bc4cd68 8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents: 16151
diff changeset
   125
            assertEquals(sobj.size(), 3);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
        } finally {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
            Context.setGlobal(oldGlobal);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
    private Object eval(final Context cx, final String name, final String code) {
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23375
diff changeset
   133
        final Source source = sourceFor(name, code);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
        final ScriptObject global = Context.getGlobal();
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16221
diff changeset
   135
        final ScriptFunction func = cx.compileScript(source, global);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
        return func != null ? ScriptRuntime.apply(func, global) : null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
}