nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java
author sundar
Thu, 22 Aug 2013 22:32:16 +0530
changeset 19623 53207b2e3a7d
parent 19620 3f0c79b63846
child 19624 976b3bfbd657
permissions -rw-r--r--
8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected. Reviewed-by: jlaskey, lagergren, hannesw
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.api.scripting;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import static org.testng.Assert.assertEquals;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16221
diff changeset
    29
import static org.testng.Assert.assertFalse;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import static org.testng.Assert.assertNotNull;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import static org.testng.Assert.assertTrue;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import static org.testng.Assert.fail;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.io.StringReader;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import java.io.StringWriter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import java.lang.reflect.Method;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import java.util.HashMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
import java.util.Map;
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
    39
import java.util.Objects;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
import java.util.concurrent.Callable;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
import javax.script.Bindings;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
import javax.script.Compilable;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
import javax.script.CompiledScript;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
import javax.script.Invocable;
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
    45
import javax.script.ScriptContext;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
import javax.script.ScriptEngine;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
import javax.script.ScriptEngineFactory;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
import javax.script.ScriptEngineManager;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
import javax.script.ScriptException;
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
    50
import javax.script.SimpleBindings;
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
    51
import javax.script.SimpleScriptContext;
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
    52
import org.testng.Assert;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
import org.testng.annotations.Test;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
 * Tests for JSR-223 script engine for Nashorn.
16260
3318a14ac7cb 8008729: Make sure that we can run basic jsr223 tests using jtreg
sundar
parents: 16241
diff changeset
    57
 *
3318a14ac7cb 8008729: Make sure that we can run basic jsr223 tests using jtreg
sundar
parents: 16241
diff changeset
    58
 * @test
17248
60ef2a001ec2 8013295: ScriptEngineTest.java fails with compilation error when running under jtreg
sundar
parents: 17239
diff changeset
    59
 * @build jdk.nashorn.api.scripting.Window jdk.nashorn.api.scripting.WindowEventHandler jdk.nashorn.api.scripting.VariableArityTestInterface jdk.nashorn.api.scripting.ScriptEngineTest
16260
3318a14ac7cb 8008729: Make sure that we can run basic jsr223 tests using jtreg
sundar
parents: 16241
diff changeset
    60
 * @run testng jdk.nashorn.api.scripting.ScriptEngineTest
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
public class ScriptEngineTest {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
    private void log(String msg) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
        org.testng.Reporter.log(msg, true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
    public void argumentsTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
        String[] args = new String[] { "hello", "world" };
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
            e.put("arguments", args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
            Object arg0 = e.eval("arguments[0]");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
            Object arg1 = e.eval("arguments[1]");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
            assertEquals(args[0], arg0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
            assertEquals(args[1], arg1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
    public void argumentsWithTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
        String[] args = new String[] { "hello", "world" };
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
            e.put("arguments", args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
            Object arg0 = e.eval("var imports = new JavaImporter(java.io); " +
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
                    " with(imports) { arguments[0] }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
            Object arg1 = e.eval("var imports = new JavaImporter(java.util, java.io); " +
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
                    " with(imports) { arguments[1] }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
            assertEquals(args[0], arg0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
            assertEquals(args[1], arg1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
    public void argumentsEmptyTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
        try {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   112
            assertEquals(e.eval("arguments instanceof Array"), true);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   113
            assertEquals(e.eval("arguments.length == 0"), true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
    public void factoryTests() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
        assertNotNull(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
        final ScriptEngineFactory fac = e.getFactory();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   128
        assertEquals(fac.getLanguageName(), "ECMAScript");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   129
        assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   130
        assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   131
        assertEquals(fac.getEngineName(), "Oracle Nashorn");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   132
        assertEquals(fac.getOutputStatement("context"), "print(context)");
16241
e23ed61e75a2 8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents: 16234
diff changeset
   133
        assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   134
        assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
        boolean seenJS = false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
        for (String ext : fac.getExtensions()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
            if (ext.equals("js")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
                seenJS = true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   143
        assertEquals(seenJS, true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
        String str = fac.getMethodCallSyntax("obj", "foo", "x");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   145
        assertEquals(str, "obj.foo(x)");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
        boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
        for (String name : fac.getNames()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
            switch (name) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
                case "nashorn": seenNashorn = true; break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
                case "javascript": seenJavaScript = true; break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
                case "ECMAScript": seenECMAScript = true; break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   156
        assertTrue(seenNashorn);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   157
        assertTrue(seenJavaScript);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   158
        assertTrue(seenECMAScript);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
        boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
        for (String mime : fac.getMimeTypes()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
            switch (mime) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
                case "application/javascript": seenAppJS = true; break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
                case "application/ecmascript": seenAppECMA = true; break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
                case "text/javascript": seenTextJS = true; break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
                case "text/ecmascript": seenTextECMA = true; break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   170
        assertTrue(seenAppJS);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   171
        assertTrue(seenAppECMA);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   172
        assertTrue(seenTextJS);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   173
        assertTrue(seenTextECMA);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
    public void evalTests() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
        e.put(ScriptEngine.FILENAME, "myfile.js");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
            e.eval("print('hello')");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
        } catch (final ScriptException se) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
            fail(se.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
            e.eval("print('hello)");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
            fail("script exception expected");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
        } catch (final ScriptException se) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   191
            assertEquals(se.getLineNumber(), 1);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   192
            assertEquals(se.getColumnNumber(), 13);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   193
            assertEquals(se.getFileName(), "myfile.js");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
            // se.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
            Object obj = e.eval("34 + 41");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
            assertTrue(34.0 + 41.0 == ((Number)obj).doubleValue());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
            obj = e.eval("x = 5");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
            assertTrue(5.0 == ((Number)obj).doubleValue());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
        } catch (final ScriptException se) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
            se.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
            fail(se.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
    public void compileTests() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
        CompiledScript script = null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
            script = ((Compilable)e).compile("print('hello')");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
        } catch (final ScriptException se) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
            fail(se.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
            script.eval();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
        } catch (final ScriptException | NullPointerException se) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
            se.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
            fail(se.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
        // try to compile from a Reader
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
            script = ((Compilable)e).compile(new StringReader("print('world')"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
        } catch (final ScriptException se) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
            fail(se.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
            script.eval();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
        } catch (final ScriptException | NullPointerException se) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
            se.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
            fail(se.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
    public void createBindingsTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
        Bindings b = e.createBindings();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
        b.put("foo", 42.0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
        Object res = null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
            res = e.eval("foo == 42.0", b);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
        } catch (final ScriptException | NullPointerException se) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
            se.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
            fail(se.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   256
        assertEquals(res, Boolean.TRUE);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
    public void getInterfaceTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
        final Invocable inv = (Invocable)e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
        // try to get interface from global functions
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   266
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
            e.eval("function run() { print('run'); };");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
            final Runnable runnable = inv.getInterface(Runnable.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
            runnable.run();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
        // try interface on specific script object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
            e.eval("var obj = { run: function() { print('run from obj'); } };");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
            Object obj = e.get("obj");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
            final Runnable runnable = inv.getInterface(obj, Runnable.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
            runnable.run();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   287
    public interface Foo {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   288
        public void bar();
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   289
    }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   290
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   291
    public interface Foo2 extends Foo {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   292
        public void bar2();
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   293
    }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   294
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   295
    @Test
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   296
    public void getInterfaceMissingTest() {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   297
        final ScriptEngineManager manager = new ScriptEngineManager();
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   298
        final ScriptEngine engine = manager.getEngineByName("nashorn");
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   299
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   300
        // don't define any function.
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   301
        try {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   302
            engine.eval("");
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   303
        } catch (final Exception exp) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   304
            exp.printStackTrace();
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   305
            fail(exp.getMessage());
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   306
        }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   307
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   308
        Runnable runnable = ((Invocable)engine).getInterface(Runnable.class);
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   309
        if (runnable != null) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   310
            fail("runnable is not null!");
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   311
        }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   312
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   313
        // now define "run"
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   314
        try {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   315
            engine.eval("function run() { print('this is run function'); }");
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   316
        } catch (final Exception exp) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   317
            exp.printStackTrace();
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   318
            fail(exp.getMessage());
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   319
        }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   320
        runnable = ((Invocable)engine).getInterface(Runnable.class);
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   321
        // should not return null now!
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   322
        runnable.run();
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   323
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   324
        // define only one method of "Foo2"
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   325
        try {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   326
            engine.eval("function bar() { print('bar function'); }");
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   327
        } catch (final Exception exp) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   328
            exp.printStackTrace();
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   329
            fail(exp.getMessage());
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   330
        }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   331
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   332
        Foo2 foo2 = ((Invocable)engine).getInterface(Foo2.class);
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   333
        if (foo2 != null) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   334
            throw new RuntimeException("foo2 is not null!");
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   335
        }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   336
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   337
        // now define other method of "Foo2"
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   338
        try {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   339
            engine.eval("function bar2() { print('bar2 function'); }");
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   340
        } catch (final Exception exp) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   341
            exp.printStackTrace();
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   342
            fail(exp.getMessage());
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   343
        }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   344
        foo2 = ((Invocable)engine).getInterface(Foo2.class);
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   345
        foo2.bar();
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   346
        foo2.bar2();
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   347
    }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16522
diff changeset
   348
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   349
    @Test
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   350
    /**
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   351
     * Try passing non-interface Class object for interface implementation.
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   352
     */
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   353
    public void getNonInterfaceGetInterfaceTest() {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   354
        final ScriptEngineManager manager = new ScriptEngineManager();
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   355
        final ScriptEngine engine = manager.getEngineByName("nashorn");
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   356
        try {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   357
            log(Objects.toString(((Invocable)engine).getInterface(Object.class)));
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   358
            fail("Should have thrown IllegalArgumentException");
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   359
        } catch (final Exception exp) {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   360
            if (! (exp instanceof IllegalArgumentException)) {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   361
                fail("IllegalArgumentException expected, got " + exp);
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   362
            }
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   363
        }
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   364
    }
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   365
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 17751
diff changeset
   366
    @Test
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   367
    /**
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   368
     * Check that we can get interface out of a script object even after
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   369
     * switching to use different ScriptContext.
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   370
     */
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   371
    public void getInterfaceDifferentContext() {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   372
       ScriptEngineManager m = new ScriptEngineManager();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   373
       ScriptEngine e = m.getEngineByName("nashorn");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   374
       try {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   375
           Object obj = e.eval("({ run: function() { } })");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   376
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   377
           // change script context
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   378
           ScriptContext ctxt = new SimpleScriptContext();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   379
           ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   380
           e.setContext(ctxt);
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   381
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   382
           Runnable r = ((Invocable)e).getInterface(obj, Runnable.class);
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   383
           r.run();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   384
       }catch (final Exception exp) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   385
            exp.printStackTrace();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   386
            fail(exp.getMessage());
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   387
       }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   388
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   389
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   390
    @Test
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   391
    /**
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   392
     * Check that getInterface on non-script object 'thiz' results in IllegalArgumentException.
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   393
     */
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   394
    public void getInterfaceNonScriptObjectThizTest() {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   395
        final ScriptEngineManager m = new ScriptEngineManager();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   396
        final ScriptEngine e = m.getEngineByName("nashorn");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   397
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   398
        try {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   399
            ((Invocable)e).getInterface(new Object(), Runnable.class);
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   400
            fail("should have thrown IllegalArgumentException");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   401
        } catch (final Exception exp) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   402
            if (! (exp instanceof IllegalArgumentException)) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   403
                exp.printStackTrace();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   404
                fail(exp.getMessage());
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   405
            }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   406
        }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   407
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   408
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   409
    @Test
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   410
    /**
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   411
     * Check that getInterface on null 'thiz' results in IllegalArgumentException.
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   412
     */
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   413
    public void getInterfaceNullThizTest() {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   414
        final ScriptEngineManager m = new ScriptEngineManager();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   415
        final ScriptEngine e = m.getEngineByName("nashorn");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   416
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   417
        try {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   418
            ((Invocable)e).getInterface(null, Runnable.class);
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   419
            fail("should have thrown IllegalArgumentException");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   420
        } catch (final Exception exp) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   421
            if (! (exp instanceof IllegalArgumentException)) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   422
                exp.printStackTrace();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   423
                fail(exp.getMessage());
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   424
            }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   425
        }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   426
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   427
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   428
    @Test
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   429
    /**
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   430
     * Check that calling getInterface on mirror created by another engine results in IllegalArgumentException.
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   431
     */
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   432
    public void getInterfaceMixEnginesTest() {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   433
        final ScriptEngineManager m = new ScriptEngineManager();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   434
        final ScriptEngine engine1 = m.getEngineByName("nashorn");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   435
        final ScriptEngine engine2 = m.getEngineByName("nashorn");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   436
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   437
        try {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   438
            Object obj = engine1.eval("({ run: function() {} })");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   439
            // pass object from engine1 to engine2 as 'thiz' for getInterface
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   440
            ((Invocable)engine2).getInterface(obj, Runnable.class);
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   441
            fail("should have thrown IllegalArgumentException");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   442
        } catch (final Exception exp) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   443
            if (! (exp instanceof IllegalArgumentException)) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   444
                exp.printStackTrace();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   445
                fail(exp.getMessage());
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   446
            }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   447
        }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   448
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   449
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   450
    @Test
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
    public void accessGlobalTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
            e.eval("var x = 'hello'");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   457
            assertEquals(e.get("x"), "hello");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
        } catch (final ScriptException exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   462
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   463
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   464
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   465
    public void exposeGlobalTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   466
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   467
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   468
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   469
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   470
            e.put("y", "foo");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   471
            e.eval("print(y)");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   472
        } catch (final ScriptException exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   473
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   474
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   475
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
    public void putGlobalFunctionTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   480
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   481
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   482
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
        e.put("callable", new Callable<String>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   485
            public String call() throws Exception {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
                return "callable was called";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   487
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   488
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   489
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   490
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   491
            e.eval("print(callable.call())");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   492
        } catch (final ScriptException exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   493
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   494
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   495
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   496
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   497
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   498
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   499
    public void windowAlertTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   500
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   501
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   502
        final Window window = new Window();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   503
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   504
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   505
            e.put("window", window);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   506
            e.eval("print(window.alert)");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   507
            e.eval("window.alert('calling window.alert...')");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   508
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   509
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   510
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   511
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   512
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   513
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   514
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   515
    public void windowLocationTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   516
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   517
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   518
        final Window window = new Window();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   519
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   520
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   521
            e.put("window", window);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   522
            e.eval("print(window.location)");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   523
            final Object locationValue = e.eval("window.getLocation()");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   524
            assertEquals(locationValue, "http://localhost:8080/window");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   525
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   526
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   527
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   528
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   529
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   530
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   531
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   532
    public void windowItemTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   533
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   534
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   535
        final Window window = new Window();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   536
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   537
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   538
            e.put("window", window);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   539
            final String item1 = (String)e.eval("window.item(65535)");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   540
            assertEquals(item1, "ffff");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   541
            final String item2 = (String)e.eval("window.item(255)");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   542
            assertEquals(item2, "ff");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   543
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   544
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   545
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   546
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   547
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   548
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   549
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   550
    public void windowEventTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   551
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   552
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   553
        final Window window = new Window();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   554
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   555
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   556
            e.put("window", window);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   557
            e.eval("window.onload = function() { print('window load event fired'); return true }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   558
            assertTrue((Boolean)e.eval("window.onload.loaded()"));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   559
            final WindowEventHandler handler = window.getOnload();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   560
            assertNotNull(handler);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   561
            assertTrue(handler.loaded());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   562
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   563
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   564
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   565
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   566
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   567
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   568
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   569
    public void throwTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   570
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   571
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   572
        e.put(ScriptEngine.FILENAME, "throwtest.js");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   573
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   574
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   575
            e.eval("throw 'foo'");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   576
        } catch (final ScriptException exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   577
            log(exp.getMessage());
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   578
            assertEquals(exp.getMessage(), "foo in throwtest.js at line number 1 at column number 0");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   579
            assertEquals(exp.getFileName(), "throwtest.js");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   580
            assertEquals(exp.getLineNumber(), 1);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   581
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   582
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   583
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   584
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   585
    public void setTimeoutTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   586
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   587
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   588
        final Window window = new Window();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   589
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   590
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   591
            final Class<?> setTimeoutParamTypes[] = { Window.class, String.class, int.class };
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   592
            final Method setTimeout = Window.class.getDeclaredMethod("setTimeout", setTimeoutParamTypes);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   593
            assertNotNull(setTimeout);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   594
            e.put("window", window);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   595
            e.eval("window.setTimeout('foo()', 100)");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   596
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   597
            // try to make setTimeout global
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   598
            e.put("setTimeout", setTimeout);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   599
            // TODO: java.lang.ClassCastException: required class
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   600
            // java.lang.Integer but encountered class java.lang.Double
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   601
            // e.eval("setTimeout('foo2()', 200)");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   602
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   603
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   604
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   605
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   606
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   607
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   608
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   609
    public void setWriterTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   610
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   611
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   612
        final StringWriter sw = new StringWriter();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   613
        e.getContext().setWriter(sw);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   614
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   615
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   616
            e.eval("print('hello world')");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   617
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   618
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   619
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   620
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   621
        // dos2unix - fix line endings if running on windows
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   622
        assertEquals(sw.toString().replaceAll("\r", ""), "hello world\n");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   623
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   624
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   625
    @SuppressWarnings("unchecked")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   626
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   627
    public void reflectionTest() throws ScriptException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   628
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   629
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   630
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   631
        e.eval("var obj = { x: 344, y: 'nashorn' }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   632
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   633
        int count = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   634
        Map<Object, Object> map = (Map<Object, Object>)e.get("obj");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   635
        assertFalse(map.isEmpty());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   636
        assertTrue(map.keySet().contains("x"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   637
        assertTrue(map.containsKey("x"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   638
        assertTrue(map.values().contains("nashorn"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   639
        assertTrue(map.containsValue("nashorn"));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   640
        for (final Map.Entry<?, ?> ex : map.entrySet()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   641
            final Object key = ex.getKey();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   642
            if (key.equals("x")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   643
                assertTrue(344 == ((Number)ex.getValue()).doubleValue());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   644
                count++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   645
            } else if (key.equals("y")) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   646
                assertEquals(ex.getValue(), "nashorn");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   647
                count++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   648
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   649
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   650
        assertEquals(2, count);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   651
        assertEquals(2, map.size());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   652
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   653
        // add property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   654
        map.put("z", "hello");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   655
        assertEquals(e.eval("obj.z"), "hello");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   656
        assertEquals(map.get("z"), "hello");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   657
        assertTrue(map.keySet().contains("z"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   658
        assertTrue(map.containsKey("z"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   659
        assertTrue(map.values().contains("hello"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   660
        assertTrue(map.containsValue("hello"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   661
        assertEquals(map.size(), 3);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   662
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   663
        final Map<Object, Object> newMap = new HashMap<>();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   664
        newMap.put("foo", 23.0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   665
        newMap.put("bar", true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   666
        map.putAll(newMap);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   667
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   668
        assertEquals(e.eval("obj.foo"), 23.0);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   669
        assertEquals(e.eval("obj.bar"), true);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   670
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   671
        // remove using map method
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   672
        map.remove("foo");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   673
        assertEquals(e.eval("typeof obj.foo"), "undefined");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   674
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   675
        count = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   676
        e.eval("var arr = [ true, 'hello' ]");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   677
        map = (Map<Object, Object>)e.get("arr");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   678
        assertFalse(map.isEmpty());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   679
        assertTrue(map.containsKey("length"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   680
        assertTrue(map.containsValue("hello"));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   681
        for (final Map.Entry<?, ?> ex : map.entrySet()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   682
            final Object key = ex.getKey();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   683
            if (key.equals("0")) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   684
                assertEquals(ex.getValue(), Boolean.TRUE);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   685
                count++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   686
            } else if (key.equals("1")) {
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   687
                assertEquals(ex.getValue(), "hello");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   688
                count++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   689
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   690
        }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   691
        assertEquals(count, 2);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   692
        assertEquals(map.size(), 2);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   693
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   694
        // add element
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   695
        map.put("2", "world");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   696
        assertEquals(map.get("2"), "world");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   697
        assertEquals(map.size(), 3);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   698
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   699
        // remove all
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   700
        map.clear();
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   701
        assertTrue(map.isEmpty());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   702
        assertEquals(e.eval("typeof arr[0]"), "undefined");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   703
        assertEquals(e.eval("typeof arr[1]"), "undefined");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   704
        assertEquals(e.eval("typeof arr[2]"), "undefined");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   705
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   706
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   707
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   708
    public void redefineEchoTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   709
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   710
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   711
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   712
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   713
            e.eval("var echo = {}; if (typeof echo !== 'object') { throw 'echo is a '+typeof echo; }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   714
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   715
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   716
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   717
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   718
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   719
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   720
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   721
    public void invokeMethodTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   722
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   723
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   724
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   725
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   726
            e.eval("var Example = function() { this.hello = function() { return 'Hello World!'; };}; myExample = new Example();");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   727
            final Object obj = e.get("myExample");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   728
            final Object res = ((Invocable)e).invokeMethod(obj, "hello");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
   729
            assertEquals(res, "Hello World!");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   730
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   731
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   732
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   733
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   734
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   735
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   736
    @Test
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   737
    /**
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   738
     * Check that we can call invokeMethod on an object that we got by evaluating
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   739
     * script with different Context set.
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   740
     */
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   741
    public void invokeMethodDifferentContextTest() {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   742
       ScriptEngineManager m = new ScriptEngineManager();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   743
       ScriptEngine e = m.getEngineByName("nashorn");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   744
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   745
       try {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   746
           // define an object with method on it
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   747
           Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   748
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   749
           final ScriptContext ctxt = new SimpleScriptContext();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   750
           ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   751
           e.setContext(ctxt);
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   752
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   753
           // invoke 'func' on obj - but with current script context changed
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   754
           final Object res = ((Invocable)e).invokeMethod(obj, "hello");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   755
           assertEquals(res, "Hello World!");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   756
       } catch (final Exception exp) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   757
           exp.printStackTrace();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   758
           fail(exp.getMessage());
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   759
       }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   760
    }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   761
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   762
    @Test
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   763
    /**
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   764
     * Check that invokeMethod throws NPE on null method name.
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   765
     */
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   766
    public void invokeMethodNullNameTest() {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   767
        final ScriptEngineManager m = new ScriptEngineManager();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   768
        final ScriptEngine e = m.getEngineByName("nashorn");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   769
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   770
        try {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   771
            final Object obj = e.eval("({})");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   772
            final Object res = ((Invocable)e).invokeMethod(obj, null);
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   773
            fail("should have thrown NPE");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   774
        } catch (final Exception exp) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   775
            if (! (exp instanceof NullPointerException)) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   776
                exp.printStackTrace();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   777
                fail(exp.getMessage());
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   778
            }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   779
        }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   780
    }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   781
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   782
    @Test
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   783
    /**
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   784
     * Check that invokeMethod throws NoSuchMethodException on missing method.
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   785
     */
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   786
    public void invokeMethodMissingTest() {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   787
        final ScriptEngineManager m = new ScriptEngineManager();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   788
        final ScriptEngine e = m.getEngineByName("nashorn");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   789
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   790
        try {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   791
            final Object obj = e.eval("({})");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   792
            final Object res = ((Invocable)e).invokeMethod(obj, "nonExistentMethod");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   793
            fail("should have thrown NoSuchMethodException");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   794
        } catch (final Exception exp) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   795
            if (! (exp instanceof NoSuchMethodException)) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   796
                exp.printStackTrace();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   797
                fail(exp.getMessage());
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   798
            }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   799
        }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   800
    }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   801
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   802
    @Test
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   803
    /**
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   804
     * Check that calling method on non-script object 'thiz' results in IllegalArgumentException.
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   805
     */
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   806
    public void invokeMethodNonScriptObjectThizTest() {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   807
        final ScriptEngineManager m = new ScriptEngineManager();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   808
        final ScriptEngine e = m.getEngineByName("nashorn");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   809
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   810
        try {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   811
            ((Invocable)e).invokeMethod(new Object(), "toString");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   812
            fail("should have thrown IllegalArgumentException");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   813
        } catch (final Exception exp) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   814
            if (! (exp instanceof IllegalArgumentException)) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   815
                exp.printStackTrace();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   816
                fail(exp.getMessage());
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   817
            }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   818
        }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   819
    }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   820
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   821
    @Test
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   822
    /**
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   823
     * Check that calling method on null 'thiz' results in IllegalArgumentException.
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   824
     */
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   825
    public void invokeMethodNullThizTest() {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   826
        final ScriptEngineManager m = new ScriptEngineManager();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   827
        final ScriptEngine e = m.getEngineByName("nashorn");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   828
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   829
        try {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   830
            ((Invocable)e).invokeMethod(null, "toString");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   831
            fail("should have thrown IllegalArgumentException");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   832
        } catch (final Exception exp) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   833
            if (! (exp instanceof IllegalArgumentException)) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   834
                exp.printStackTrace();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   835
                fail(exp.getMessage());
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   836
            }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   837
        }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   838
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   839
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   840
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   841
    @Test
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   842
    /**
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   843
     * Check that calling method on mirror created by another engine results in IllegalArgumentException.
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   844
     */
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   845
    public void invokeMethodMixEnginesTest() {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   846
        final ScriptEngineManager m = new ScriptEngineManager();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   847
        final ScriptEngine engine1 = m.getEngineByName("nashorn");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   848
        final ScriptEngine engine2 = m.getEngineByName("nashorn");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   849
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   850
        try {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   851
            Object obj = engine1.eval("({ run: function() {} })");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   852
            // pass object from engine1 to engine2 as 'thiz' for invokeMethod
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   853
            ((Invocable)engine2).invokeMethod(obj, "run");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   854
            fail("should have thrown IllegalArgumentException");
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   855
        } catch (final Exception exp) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   856
            if (! (exp instanceof IllegalArgumentException)) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   857
                exp.printStackTrace();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   858
                fail(exp.getMessage());
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   859
            }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   860
        }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   861
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   862
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   863
    @Test
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   864
    public void noEnumerablePropertiesTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   865
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   866
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   867
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   868
            e.eval("for (i in this) { throw 'found property: ' + i }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   869
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   870
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   871
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   872
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   873
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   874
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   875
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   876
    public void noRefErrorForGlobalThisAccessTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   877
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   878
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   879
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   880
            e.eval("this.foo");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   881
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   882
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   883
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   884
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   885
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   886
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   887
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   888
    public void refErrorForUndeclaredAccessTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   889
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   890
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   891
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   892
            e.eval("try { print(foo); throw 'no ref error' } catch (e) { if (!(e instanceof ReferenceError)) throw e; }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   893
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   894
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   895
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   896
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   897
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   898
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   899
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   900
    public void typeErrorForGlobalThisCallTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   901
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   902
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   903
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   904
            e.eval("try { this.foo() } catch(e) { if (! (e instanceof TypeError)) throw 'no type error' }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   905
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   906
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   907
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   908
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   909
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   910
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   911
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   912
    public void refErrorForUndeclaredCallTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   913
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   914
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   915
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   916
            e.eval("try { foo() } catch(e) { if (! (e instanceof ReferenceError)) throw 'no ref error' }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   917
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   918
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   919
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   920
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   921
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   922
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   923
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   924
    public void jsobjectTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   925
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   926
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   927
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   928
            e.eval("var obj = { '1': 'world', func: function() { return this.bar; }, bar: 'hello' }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   929
            JSObject obj = (JSObject) e.get("obj");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   930
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   931
            // try basic get on existing properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   932
            if (! obj.getMember("bar").equals("hello")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   933
                fail("obj.bar != 'hello'");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   934
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   935
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   936
            if (! obj.getSlot(1).equals("world")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   937
                fail("obj[1] != 'world'");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   938
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   939
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   940
            if (! obj.call("func", new Object[0]).equals("hello")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   941
                fail("obj.call('func') != 'hello'");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   942
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   943
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   944
            // try setting properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   945
            obj.setMember("bar", "new-bar");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   946
            obj.setSlot(1, "new-element-1");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   947
            if (! obj.getMember("bar").equals("new-bar")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   948
                fail("obj.bar != 'new-bar'");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   949
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   950
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   951
            if (! obj.getSlot(1).equals("new-element-1")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   952
                fail("obj[1] != 'new-element-1'");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   953
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   954
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   955
            // try adding properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   956
            obj.setMember("prop", "prop-value");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   957
            obj.setSlot(12, "element-12");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   958
            if (! obj.getMember("prop").equals("prop-value")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   959
                fail("obj.prop != 'prop-value'");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   960
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   961
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   962
            if (! obj.getSlot(12).equals("element-12")) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   963
                fail("obj[12] != 'element-12'");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   964
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   965
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   966
            // delete properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   967
            obj.removeMember("prop");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   968
            if ("prop-value".equals(obj.getMember("prop"))) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   969
                fail("obj.prop is not deleted!");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   970
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   971
16241
e23ed61e75a2 8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents: 16234
diff changeset
   972
            // Simple eval tests
e23ed61e75a2 8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents: 16234
diff changeset
   973
            assertEquals(obj.eval("typeof Object"), "function");
e23ed61e75a2 8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents: 16234
diff changeset
   974
            assertEquals(obj.eval("'nashorn'.substring(3)"), "horn");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   975
        } catch (final Exception exp) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   976
            exp.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   977
            fail(exp.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   978
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   979
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   980
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   981
    @Test
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   982
    /**
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   983
     * check that null function name results in NPE.
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   984
     */
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   985
    public void invokeFunctionNullNameTest() {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   986
        final ScriptEngineManager m = new ScriptEngineManager();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   987
        final ScriptEngine e = m.getEngineByName("nashorn");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   988
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   989
        try {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   990
            final Object res = ((Invocable)e).invokeFunction(null);
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   991
            fail("should have thrown NPE");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   992
        } catch (final Exception exp) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   993
            if (! (exp instanceof NullPointerException)) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   994
                exp.printStackTrace();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   995
                fail(exp.getMessage());
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   996
            }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   997
        }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   998
    }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
   999
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1000
    @Test
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1001
    /**
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1002
     * Check that attempt to call missing function results in NoSuchMethodException.
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1003
     */
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1004
    public void invokeFunctionMissingTest() {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1005
        final ScriptEngineManager m = new ScriptEngineManager();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1006
        final ScriptEngine e = m.getEngineByName("nashorn");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1007
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1008
        try {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1009
            final Object res = ((Invocable)e).invokeFunction("NonExistentFunc");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1010
            fail("should have thrown NoSuchMethodException");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1011
        } catch (final Exception exp) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1012
            if (! (exp instanceof NoSuchMethodException)) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1013
                exp.printStackTrace();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1014
                fail(exp.getMessage());
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1015
            }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1016
        }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1017
    }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1018
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1019
    @Test
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1020
    /**
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1021
     * Check that invokeFunction calls functions only from current context's Bindings.
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1022
     */
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1023
    public void invokeFunctionDifferentContextTest() {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1024
        ScriptEngineManager m = new ScriptEngineManager();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1025
        ScriptEngine e = m.getEngineByName("nashorn");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1026
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1027
        try {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1028
            // define an object with method on it
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1029
            Object obj = e.eval("function hello() { return 'Hello World!'; }");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1030
            final ScriptContext ctxt = new SimpleScriptContext();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1031
            ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1032
            // change engine's current context
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1033
            e.setContext(ctxt);
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1034
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1035
            ((Invocable)e).invokeFunction("hello"); // no 'hello' in new context!
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1036
            fail("should have thrown NoSuchMethodException");
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1037
        } catch (final Exception exp) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1038
            if (! (exp instanceof NoSuchMethodException)) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1039
                exp.printStackTrace();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1040
                fail(exp.getMessage());
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1041
            }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1042
        }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1043
    }
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1044
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 18874
diff changeset
  1045
    @Test
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1046
    public void invokeFunctionExceptionTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1047
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1048
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1049
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1050
            e.eval("function func() { throw new TypeError(); }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1051
        } catch (final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1052
            t.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1053
            fail(t.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1054
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1055
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1056
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1057
            ((Invocable)e).invokeFunction("func");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1058
            fail("should have thrown exception");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1059
        } catch (final ScriptException se) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1060
            // ECMA TypeError property wrapped as a ScriptException
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1061
            log("got " + se + " as expected");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1062
        } catch (final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1063
            t.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1064
            fail(t.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1065
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1066
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1067
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1068
    @Test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1069
    public void invokeMethodExceptionTest() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1070
        final ScriptEngineManager m = new ScriptEngineManager();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1071
        final ScriptEngine e = m.getEngineByName("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1072
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1073
            e.eval("var sobj = {}; sobj.foo = function func() { throw new TypeError(); }");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1074
        } catch (final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1075
            t.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1076
            fail(t.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1077
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1078
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1079
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1080
            final Object sobj = e.get("sobj");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1081
            ((Invocable)e).invokeMethod(sobj, "foo");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1082
            fail("should have thrown exception");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1083
        } catch (final ScriptException se) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1084
            // ECMA TypeError property wrapped as a ScriptException
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1085
            log("got " + se + " as expected");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1086
        } catch (final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1087
            t.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1088
            fail(t.getMessage());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1089
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1090
    }
16177
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1091
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1092
    @Test
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1093
    public void scriptObjectMirrorToStringTest() {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1094
        final ScriptEngineManager m = new ScriptEngineManager();
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1095
        final ScriptEngine e = m.getEngineByName("nashorn");
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1096
        try {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1097
            Object obj = e.eval("new TypeError('wrong type')");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1098
            assertEquals(obj.toString(), "TypeError: wrong type", "toString returns wrong value");
16177
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1099
        } catch (final Throwable t) {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1100
            t.printStackTrace();
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1101
            fail(t.getMessage());
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1102
        }
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1103
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1104
        try {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1105
            Object obj = e.eval("function func() { print('hello'); }");
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1106
            assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value");
16177
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1107
        } catch (final Throwable t) {
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1108
            t.printStackTrace();
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1109
            fail(t.getMessage());
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1110
        }
e6464f96bdb2 8006412: Improve toString method of ScriptObjectMirror class
sundar
parents: 16151
diff changeset
  1111
    }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1112
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1113
    @Test
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1114
    public void engineScopeTest() {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1115
        final ScriptEngineManager m = new ScriptEngineManager();
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1116
        final ScriptEngine e = m.getEngineByName("nashorn");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1117
        Bindings engineScope = e.getBindings(ScriptContext.ENGINE_SCOPE);
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1118
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1119
        // check few ECMA standard built-in global properties
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1120
        assertNotNull(engineScope.get("Object"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1121
        assertNotNull(engineScope.get("TypeError"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1122
        assertNotNull(engineScope.get("eval"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1123
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1124
        // can access via ScriptEngine.get as well
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1125
        assertNotNull(e.get("Object"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1126
        assertNotNull(e.get("TypeError"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1127
        assertNotNull(e.get("eval"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1128
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1129
        // Access by either way should return same object
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1130
        assertEquals(engineScope.get("Array"), e.get("Array"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1131
        assertEquals(engineScope.get("EvalError"), e.get("EvalError"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1132
        assertEquals(engineScope.get("undefined"), e.get("undefined"));
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1133
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1134
        // try exposing a new variable from scope
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1135
        engineScope.put("myVar", "foo");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1136
        try {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1137
            assertEquals(e.eval("myVar"), "foo");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1138
        } catch (final ScriptException se) {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1139
            se.printStackTrace();
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1140
            fail(se.getMessage());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1141
        }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1142
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1143
        // update "myVar" in script an check the value from scope
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1144
        try {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1145
            e.eval("myVar = 'nashorn';");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1146
        } catch (final ScriptException se) {
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1147
            se.printStackTrace();
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1148
            fail(se.getMessage());
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1149
        }
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1150
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1151
        // now check modified value from scope and engine
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1152
        assertEquals(engineScope.get("myVar"), "nashorn");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1153
        assertEquals(e.get("myVar"), "nashorn");
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16177
diff changeset
  1154
    }
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1155
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1156
    @Test
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1157
    public void multiGlobalTest() {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1158
        final ScriptEngineManager m = new ScriptEngineManager();
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1159
        final ScriptEngine e = m.getEngineByName("nashorn");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1160
        final Bindings b = e.createBindings();
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1161
        final ScriptContext newCtxt = new SimpleScriptContext();
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1162
        newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1163
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1164
        try {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1165
            Object obj1 = e.eval("Object");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1166
            Object obj2 = e.eval("Object", newCtxt);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1167
            Assert.assertNotEquals(obj1, obj2);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1168
            Assert.assertNotNull(obj1);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1169
            Assert.assertNotNull(obj2);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1170
            Assert.assertEquals(obj1.toString(), obj2.toString());
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1171
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1172
            e.eval("x = 'hello'");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1173
            e.eval("x = 'world'", newCtxt);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1174
            Object x1 = e.getContext().getAttribute("x");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1175
            Object x2 = newCtxt.getAttribute("x");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1176
            Assert.assertNotEquals(x1, x2);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1177
            Assert.assertEquals(x1, "hello");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1178
            Assert.assertEquals(x2, "world");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1179
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1180
            x1 = e.eval("x");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1181
            x2 = e.eval("x", newCtxt);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1182
            Assert.assertNotEquals(x1, x2);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1183
            Assert.assertEquals(x1, "hello");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1184
            Assert.assertEquals(x2, "world");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1185
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1186
            final ScriptContext origCtxt = e.getContext();
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1187
            e.setContext(newCtxt);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1188
            e.eval("y = new Object()");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1189
            e.eval("y = new Object()", origCtxt);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1190
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1191
            Object y1 = origCtxt.getAttribute("y");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1192
            Object y2 = newCtxt.getAttribute("y");
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1193
            Assert.assertNotEquals(y1, y2);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1194
            Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt));
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1195
            Assert.assertEquals("[object Object]", y1.toString());
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1196
            Assert.assertEquals("[object Object]", y2.toString());
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1197
        } catch (final ScriptException se) {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1198
            se.printStackTrace();
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1199
            fail(se.getMessage());
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1200
        }
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16182
diff changeset
  1201
    }
17239
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1202
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1203
    @Test
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1204
    /**
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1205
     * Tests whether invocation of a JavaScript method through a variable arity Java method will pass the vararg array.
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1206
     * Both non-vararg and vararg JavaScript methods are tested.
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1207
     * @throws ScriptException
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1208
     */
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1209
    public void variableArityInterfaceTest() throws ScriptException {
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1210
        final ScriptEngineManager m = new ScriptEngineManager();
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1211
        final ScriptEngine e = m.getEngineByName("nashorn");
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1212
        e.eval(
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1213
            "function test1(i, strings) {" +
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1214
            "    return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)" +
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1215
            "}" +
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1216
            "function test2() {" +
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1217
            "    return 'arguments[0] == ' + arguments[0] + ', arguments[1] instanceof java.lang.String[] == ' + (arguments[1] instanceof Java.type('java.lang.String[]')) + ', arguments[1] == ' + java.util.Arrays.toString(arguments[1])" +
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1218
            "}"
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1219
        );
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1220
        final VariableArityTestInterface itf = ((Invocable)e).getInterface(VariableArityTestInterface.class);
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1221
        Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]");
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1222
        Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]");
6dd68632cdcd 8011065: Problems when script implements an interface with variadic methods
attila
parents: 16528
diff changeset
  1223
    }
19107
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1224
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1225
    @Test
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1226
    // check that print function prints arg followed by newline char
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1227
    public void printTest() {
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1228
        final ScriptEngineManager m = new ScriptEngineManager();
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1229
        final ScriptEngine e = m.getEngineByName("nashorn");
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1230
        final StringWriter sw = new StringWriter();
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1231
        e.getContext().setWriter(sw);
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1232
        try {
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1233
            e.eval("print('hello')");
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1234
        } catch (final Throwable t) {
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1235
            t.printStackTrace();
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1236
            fail(t.getMessage());
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1237
        }
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1238
19464
1576facafb62 8022615: [nightly] Two nashorn print tests fail in nightly builds on Windows
sundar
parents: 19107
diff changeset
  1239
        // dos2unix - fix line endings if running on windows
1576facafb62 8022615: [nightly] Two nashorn print tests fail in nightly builds on Windows
sundar
parents: 19107
diff changeset
  1240
        assertEquals(sw.toString().replaceAll("\r", ""), "hello\n");
19107
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1241
    }
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1242
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1243
    @Test
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1244
    // check that print prints all arguments (more than one)
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1245
    public void printManyTest() {
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1246
        final ScriptEngineManager m = new ScriptEngineManager();
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1247
        final ScriptEngine e = m.getEngineByName("nashorn");
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1248
        final StringWriter sw = new StringWriter();
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1249
        e.getContext().setWriter(sw);
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1250
        try {
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1251
            e.eval("print(34, true, 'hello')");
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1252
        } catch (final Throwable t) {
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1253
            t.printStackTrace();
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1254
            fail(t.getMessage());
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1255
        }
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1256
19464
1576facafb62 8022615: [nightly] Two nashorn print tests fail in nightly builds on Windows
sundar
parents: 19107
diff changeset
  1257
        // dos2unix - fix line endings if running on windows
1576facafb62 8022615: [nightly] Two nashorn print tests fail in nightly builds on Windows
sundar
parents: 19107
diff changeset
  1258
        assertEquals(sw.toString().replaceAll("\r", ""), "34 true hello\n");
19107
721939a7971c 8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents: 19101
diff changeset
  1259
    }
19620
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1260
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1261
    @Test
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1262
    public void mirrorNewObjectGlobalFunctionTest() throws ScriptException {
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1263
        final ScriptEngineManager m = new ScriptEngineManager();
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1264
        final ScriptEngine e = m.getEngineByName("nashorn");
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1265
        final ScriptEngine e2 = m.getEngineByName("nashorn");
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1266
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1267
        e.eval("function func() {}");
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1268
        e2.put("foo", e.get("func"));
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1269
        final Object e2global = e2.eval("this");
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1270
        final Object newObj = ((ScriptObjectMirror)e2global).newObject("foo");
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1271
        assertTrue(newObj instanceof ScriptObjectMirror);
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1272
    }
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1273
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1274
    @Test
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1275
    public void mirrorNewObjectInstanceFunctionTest() throws ScriptException {
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1276
        final ScriptEngineManager m = new ScriptEngineManager();
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1277
        final ScriptEngine e = m.getEngineByName("nashorn");
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1278
        final ScriptEngine e2 = m.getEngineByName("nashorn");
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1279
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1280
        e.eval("function func() {}");
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1281
        e2.put("func", e.get("func"));
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1282
        final Object e2obj = e2.eval("({ foo: func })");
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1283
        final Object newObj = ((ScriptObjectMirror)e2obj).newObject("foo");
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1284
        assertTrue(newObj instanceof ScriptObjectMirror);
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19464
diff changeset
  1285
    }
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1286
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1287
    @Test
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1288
    public void userEngineScopeBindingsTest() throws ScriptException {
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1289
        final ScriptEngineManager m = new ScriptEngineManager();
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1290
        final ScriptEngine e = m.getEngineByName("nashorn");
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1291
        e.eval("function func() {}");
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1292
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1293
        final ScriptContext newContext = new SimpleScriptContext();
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1294
        newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1295
        // we are using a new bindings - so it should have 'func' defined
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1296
        Object value = e.eval("typeof func", newContext);
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1297
        assertTrue(value.equals("undefined"));
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1298
    }
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1299
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1300
    @Test
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1301
    public void userEngineScopeBindingsNoLeakTest() throws ScriptException {
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1302
        final ScriptEngineManager m = new ScriptEngineManager();
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1303
        final ScriptEngine e = m.getEngineByName("nashorn");
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1304
        final ScriptContext newContext = new SimpleScriptContext();
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1305
        newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1306
        e.eval("function foo() {}", newContext);
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1307
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1308
        // in the default context's ENGINE_SCOPE, 'foo' shouldn't exist
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1309
        assertTrue(e.eval("typeof foo").equals("undefined"));
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1310
    }
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1311
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1312
    @Test
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1313
    public void userEngineScopeBindingsRetentionTest() throws ScriptException {
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1314
        final ScriptEngineManager m = new ScriptEngineManager();
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1315
        final ScriptEngine e = m.getEngineByName("nashorn");
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1316
        final ScriptContext newContext = new SimpleScriptContext();
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1317
        newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1318
        e.eval("function foo() {}", newContext);
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1319
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1320
        // definition retained with user's ENGINE_SCOPE Binding
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1321
        assertTrue(e.eval("typeof foo", newContext).equals("function"));
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1322
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1323
        final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1324
        // but not in another ENGINE_SCOPE binding
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1325
        newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1326
        assertTrue(e.eval("typeof foo", newContext).equals("undefined"));
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1327
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1328
        // restore ENGINE_SCOPE and check again
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1329
        newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE);
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1330
        assertTrue(e.eval("typeof foo", newContext).equals("function"));
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
  1331
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1332
}