nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngine.java
author sundar
Thu, 11 Jul 2013 16:34:55 +0530
changeset 18865 8844964e5fc5
parent 18864 c701b823ed9e
child 19085 066c9e5afd79
permissions -rw-r--r--
8020325: static property does not work on accessible, public classes Reviewed-by: attila, hannesw, lagergren
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 jdk.nashorn.internal.runtime.ECMAErrors.referenceError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import java.io.IOException;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import java.io.InputStream;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import java.io.InputStreamReader;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.io.Reader;
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
    35
import java.lang.reflect.Method;
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
    36
import java.lang.reflect.Modifier;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import java.net.URL;
18337
effcb00fcf58 8008915: URLReader constructor should allow specifying encoding
sundar
parents: 18335
diff changeset
    38
import java.nio.charset.Charset;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
import java.security.AccessController;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
import java.security.PrivilegedAction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
import java.security.PrivilegedActionException;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
import java.security.PrivilegedExceptionAction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
import javax.script.AbstractScriptEngine;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
import javax.script.Bindings;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
import javax.script.Compilable;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
import javax.script.CompiledScript;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
import javax.script.Invocable;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
import javax.script.ScriptContext;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
import javax.script.ScriptEngine;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
import javax.script.ScriptEngineFactory;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
import javax.script.ScriptException;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
import jdk.nashorn.internal.runtime.Context;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
import jdk.nashorn.internal.runtime.ErrorManager;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
import jdk.nashorn.internal.runtime.GlobalObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
import jdk.nashorn.internal.runtime.Property;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
import jdk.nashorn.internal.runtime.ScriptFunction;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
import jdk.nashorn.internal.runtime.ScriptObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
import jdk.nashorn.internal.runtime.ScriptRuntime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
import jdk.nashorn.internal.runtime.Source;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
import jdk.nashorn.internal.runtime.options.Options;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
 * JSR-223 compliant script engine for Nashorn. Instances are not created directly, but rather returned through
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
 * {@link NashornScriptEngineFactory#getScriptEngine()}. Note that this engine implements the {@link Compilable} and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
 * {@link Invocable} interfaces, allowing for efficient precompilation and repeated execution of scripts.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
 * @see NashornScriptEngineFactory
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
public final class NashornScriptEngine extends AbstractScriptEngine implements Compilable, Invocable {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
    private final ScriptEngineFactory factory;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
    private final Context             nashornContext;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
    private final ScriptObject        global;
18615
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
    75
    // initialized bit late to be made 'final'. Property object for "context"
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
    76
    // property of global object
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
    77
    private Property                  contextProperty;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
    // default options passed to Nashorn Options object
18335
1b5fdae617cf 8016550: nashorn.option.no.syntax.extensions has the wrong default
sundar
parents: 17523
diff changeset
    80
    private static final String[] DEFAULT_OPTIONS = new String[] { "-scripting", "-doe" };
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
16197
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16189
diff changeset
    82
    NashornScriptEngine(final NashornScriptEngineFactory factory, final ClassLoader appLoader) {
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16189
diff changeset
    83
        this(factory, DEFAULT_OPTIONS, appLoader);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
16197
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16189
diff changeset
    86
    NashornScriptEngine(final NashornScriptEngineFactory factory, final String[] args, final ClassLoader appLoader) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
        this.factory = factory;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
        final Options options = new Options("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
        options.process(args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
        // throw ParseException on first error from script
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
    92
        final ErrorManager errMgr = new Context.ThrowErrorManager();
16180
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
    93
        // create new Nashorn Context
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
        this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
            public Context run() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
                try {
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
    98
                    return new Context(options, errMgr, appLoader);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
                } catch (final RuntimeException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
                    if (Context.DEBUG) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
                        e.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
                    throw e;
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
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
        // create new global object
17523
cb4a7c901e0d 8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents: 17233
diff changeset
   109
        this.global = createNashornGlobal();
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   110
        // set the default engine scope for the default context
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   111
        context.setBindings(new ScriptObjectMirror(global, global), ScriptContext.ENGINE_SCOPE);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
        // evaluate engine initial script
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
        try {
16180
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   115
            evalEngineScript();
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   116
        } catch (final ScriptException e) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
            if (Context.DEBUG) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
                e.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
            throw new RuntimeException(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
    public Object eval(final Reader reader, final ScriptContext ctxt) throws ScriptException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
        try {
16243
f5fdf89e1583 8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents: 16232
diff changeset
   127
            if (reader instanceof URLReader) {
f5fdf89e1583 8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents: 16232
diff changeset
   128
                final URL url = ((URLReader)reader).getURL();
18337
effcb00fcf58 8008915: URLReader constructor should allow specifying encoding
sundar
parents: 18335
diff changeset
   129
                final Charset cs = ((URLReader)reader).getCharset();
effcb00fcf58 8008915: URLReader constructor should allow specifying encoding
sundar
parents: 18335
diff changeset
   130
                return evalImpl(compileImpl(new Source(url.toString(), url, cs), ctxt), ctxt);
16243
f5fdf89e1583 8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents: 16232
diff changeset
   131
            }
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16243
diff changeset
   132
            return evalImpl(Source.readFully(reader), ctxt);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
        } catch (final IOException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
            throw new ScriptException(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
    public Object eval(final String script, final ScriptContext ctxt) throws ScriptException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
        return evalImpl(script.toCharArray(), ctxt);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
    public ScriptEngineFactory getFactory() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
        return factory;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
    public Bindings createBindings() {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   150
        final ScriptObject newGlobal = createNashornGlobal();
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   151
        return new ScriptObjectMirror(newGlobal, newGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
    // Compilable methods
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
    public CompiledScript compile(final Reader reader) throws ScriptException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
            return asCompiledScript(compileImpl(Source.readFully(reader), context));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
        } catch (final IOException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
            throw new ScriptException(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
    public CompiledScript compile(final String str) throws ScriptException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
        return asCompiledScript(compileImpl(str.toCharArray(), context));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
    // Invocable methods
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
    public Object invokeFunction(final String name, final Object... args)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
            throws ScriptException, NoSuchMethodException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
        return invokeImpl(null, name, args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
    public Object invokeMethod(final Object self, final String name, final Object... args)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
            throws ScriptException, NoSuchMethodException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
        if (self == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
            throw new IllegalArgumentException("script object can not be null");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
        return invokeImpl(self, name, args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
    private <T> T getInterfaceInner(final Object self, final Class<T> clazz) {
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   188
        if (clazz == null || !clazz.isInterface()) {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   189
            throw new IllegalArgumentException("interface Class expected");
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   190
        }
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   191
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   192
        // perform security access check as early as possible
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   193
        final SecurityManager sm = System.getSecurityManager();
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   194
        if (sm != null) {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   195
            if (! Modifier.isPublic(clazz.getModifiers())) {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   196
                throw new SecurityException("attempt to implement non-public interfce: " + clazz);
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   197
            }
18865
8844964e5fc5 8020325: static property does not work on accessible, public classes
sundar
parents: 18864
diff changeset
   198
            Context.checkPackageAccess(clazz.getName());
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   199
        }
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   200
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   201
        final ScriptObject realSelf;
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   202
        final ScriptObject ctxtGlobal = getNashornGlobalFrom(context);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
        if(self == null) {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   204
            realSelf = ctxtGlobal;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
        } else if (!(self instanceof ScriptObject)) {
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   206
            realSelf = (ScriptObject)ScriptObjectMirror.unwrap(self, ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
        } else {
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   208
            realSelf = (ScriptObject)self;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
        }
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   210
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
        try {
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   212
            final ScriptObject oldGlobal = getNashornGlobal();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
            try {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   214
                if(oldGlobal != ctxtGlobal) {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   215
                    setNashornGlobal(ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
                }
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   217
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   218
                if (! isInterfaceImplemented(clazz, realSelf)) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   219
                    return null;
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   220
                }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
                return clazz.cast(JavaAdapterFactory.getConstructor(realSelf.getClass(), clazz).invoke(realSelf));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
            } finally {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   223
                if(oldGlobal != ctxtGlobal) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
                    setNashornGlobal(oldGlobal);
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
        } catch(final RuntimeException|Error e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
            throw e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
        } catch(final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
            throw new RuntimeException(t);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
        }
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
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
    public <T> T getInterface(final Class<T> clazz) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
        return getInterfaceInner(null, clazz);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
    public <T> T getInterface(final Object self, final Class<T> clazz) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
        if (self == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
            throw new IllegalArgumentException("script object can not be null");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
        return getInterfaceInner(self, clazz);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
    // These are called from the "engine.js" script
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
     * This hook is used to search js global variables exposed from Java code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
     * @param self 'this' passed from the script
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
     * @param ctxt current ScriptContext in which name is searched
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
     * @param name name of the variable searched
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
     * @return the value of the named variable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
    public Object __noSuchProperty__(final Object self, final ScriptContext ctxt, final String name) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
        final int scope = ctxt.getAttributesScope(name);
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   259
        final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
        if (scope != -1) {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   261
            return ScriptObjectMirror.unwrap(ctxt.getAttribute(name, scope), ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
        if (self == UNDEFINED) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
            // scope access and so throw ReferenceError
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16243
diff changeset
   266
            throw referenceError(ctxtGlobal, "not.defined", name);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
        return UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   272
    private ScriptObject getNashornGlobalFrom(final ScriptContext ctxt) {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   273
        final Bindings bindings = ctxt.getBindings(ScriptContext.ENGINE_SCOPE);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   274
        if (bindings instanceof ScriptObjectMirror) {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   275
             ScriptObject sobj = ((ScriptObjectMirror)bindings).getScriptObject();
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   276
             if (sobj instanceof GlobalObject) {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   277
                 return sobj;
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   278
             }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   281
        // didn't find global object from context given - return the engine-wide global
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   282
        return global;
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   283
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   285
    private ScriptObject createNashornGlobal() {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   286
        final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   287
            @Override
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   288
            public ScriptObject run() {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   289
                try {
16211
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
   290
                    return nashornContext.newGlobal();
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   291
                } catch (final RuntimeException e) {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   292
                    if (Context.DEBUG) {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   293
                        e.printStackTrace();
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   294
                    }
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   295
                    throw e;
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   296
                }
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   297
            }
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   298
        });
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   299
16211
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
   300
        nashornContext.initGlobal(newGlobal);
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
   301
18615
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
   302
        final int NON_ENUMERABLE_CONSTANT = Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE | Property.NOT_WRITABLE;
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   303
        // current ScriptContext exposed as "context"
18615
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
   304
        // "context" is non-writable from script - but script engine still
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
   305
        // needs to set it and so save the context Property object
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
   306
        contextProperty = newGlobal.addOwnProperty("context", NON_ENUMERABLE_CONSTANT, UNDEFINED);
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   307
        // current ScriptEngine instance exposed as "engine". We added @SuppressWarnings("LeakingThisInConstructor") as
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   308
        // NetBeans identifies this assignment as such a leak - this is a false positive as we're setting this property
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   309
        // in the Global of a Context we just created - both the Context and the Global were just created and can not be
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   310
        // seen from another thread outside of this constructor.
18615
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
   311
        newGlobal.addOwnProperty("engine", NON_ENUMERABLE_CONSTANT, this);
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   312
        // global script arguments with undefined value
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   313
        newGlobal.addOwnProperty("arguments", Property.NOT_ENUMERABLE, UNDEFINED);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   314
        // file name default is null
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   315
        newGlobal.addOwnProperty(ScriptEngine.FILENAME, Property.NOT_ENUMERABLE, null);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   316
        return newGlobal;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
    private void evalEngineScript() throws ScriptException {
16205
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents: 16201
diff changeset
   320
        evalSupportScript("resources/engine.js", NashornException.ENGINE_SCRIPT_SOURCE_NAME);
16172
25c8da53438f 8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents: 16151
diff changeset
   321
    }
25c8da53438f 8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents: 16151
diff changeset
   322
16205
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents: 16201
diff changeset
   323
    private void evalSupportScript(final String script, final String name) throws ScriptException {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
        try {
16180
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   325
            final InputStream is = AccessController.doPrivileged(
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   326
                    new PrivilegedExceptionAction<InputStream>() {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16197
diff changeset
   327
                        @Override
16180
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   328
                        public InputStream run() throws Exception {
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   329
                            final URL url = NashornScriptEngine.class.getResource(script);
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   330
                            return url.openStream();
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   331
                        }
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   332
                    });
16205
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents: 16201
diff changeset
   333
            put(ScriptEngine.FILENAME, name);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
            try (final InputStreamReader isr = new InputStreamReader(is)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   335
                eval(isr);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   336
            }
16180
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   337
        } catch (final PrivilegedActionException | IOException e) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
            throw new ScriptException(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
        } finally {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   340
            put(ScriptEngine.FILENAME, null);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   341
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   342
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   343
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   344
    // scripts should see "context" and "engine" as variables
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   345
    private void setContextVariables(final ScriptContext ctxt) {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   346
        final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt);
18615
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
   347
        // set "context" global variable via contextProperty - because this
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
   348
        // property is non-writable
3f6e6adcbc1a 8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents: 18337
diff changeset
   349
        contextProperty.setObjectValue(ctxtGlobal, ctxtGlobal, ctxt, false);
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   350
        Object args = ScriptObjectMirror.unwrap(ctxt.getAttribute("arguments"), ctxtGlobal);
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16180
diff changeset
   351
        if (args == null || args == UNDEFINED) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   352
            args = ScriptRuntime.EMPTY_ARRAY;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   353
        }
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16180
diff changeset
   354
        // if no arguments passed, expose it
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   355
        args = ((GlobalObject)ctxtGlobal).wrapAsObject(args);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   356
        ctxtGlobal.set("arguments", args, false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   357
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
    private Object invokeImpl(final Object selfObject, final String name, final Object... args) throws ScriptException, NoSuchMethodException {
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   360
        final ScriptObject oldGlobal     = getNashornGlobal();
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   361
        final ScriptObject ctxtGlobal    = getNashornGlobalFrom(context);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   362
        final boolean globalChanged = (oldGlobal != ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16230
diff changeset
   364
        Object self = globalChanged? ScriptObjectMirror.wrap(selfObject, oldGlobal) : selfObject;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
            if (globalChanged) {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   368
                setNashornGlobal(ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   370
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
            ScriptObject sobj;
16182
06e8c712f6a3 8006584: improve variable handling in NashornScriptEngine
sundar
parents: 16180
diff changeset
   372
            Object       value = null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   374
            self = ScriptObjectMirror.unwrap(self, ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
            // FIXME: should convert when self is not ScriptObject
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
            if (self instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
                sobj = (ScriptObject)self;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   379
                value = sobj.get(name);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
            } else if (self == null) {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   381
                self  = ctxtGlobal;
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   382
                sobj  = ctxtGlobal;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
                value = sobj.get(name);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   386
            if (value instanceof ScriptFunction) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
                final Object res;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
                try {
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16230
diff changeset
   389
                    final Object[] modArgs = globalChanged? ScriptObjectMirror.wrapArray(args, oldGlobal) : args;
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16230
diff changeset
   390
                    res = ScriptRuntime.checkAndApply((ScriptFunction)value, self, ScriptObjectMirror.unwrapArray(modArgs, ctxtGlobal));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
                } catch (final Exception e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
                    throwAsScriptException(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   393
                    throw new AssertionError("should not reach here");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   394
                }
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   395
                return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(res, ctxtGlobal));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   397
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   398
            throw new NoSuchMethodException(name);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
        } finally {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
            if (globalChanged) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
                setNashornGlobal(oldGlobal);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   404
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   405
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   406
    private Object evalImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
        return evalImpl(compileImpl(buf, ctxt), ctxt);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   408
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   409
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   410
    private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt) throws ScriptException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   411
        if (script == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   412
            return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
        }
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   414
        final ScriptObject oldGlobal = getNashornGlobal();
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   415
        final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   416
        final boolean globalChanged = (oldGlobal != ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   417
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   418
            if (globalChanged) {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   419
                setNashornGlobal(ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   420
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   421
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   422
            setContextVariables(ctxt);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16528
diff changeset
   423
            return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   424
        } catch (final Exception e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   425
            throwAsScriptException(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   426
            throw new AssertionError("should not reach here");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   427
        } finally {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
            if (globalChanged) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   429
                setNashornGlobal(oldGlobal);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   430
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   431
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   432
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   433
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
    private static void throwAsScriptException(final Exception e) throws ScriptException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
        if (e instanceof ScriptException) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
            throw (ScriptException)e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
        } else if (e instanceof NashornException) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   438
            final NashornException ne = (NashornException)e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
            final ScriptException se = new ScriptException(
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
                ne.getMessage(), ne.getFileName(),
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   441
                ne.getLineNumber(), ne.getColumnNumber());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
            se.initCause(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
            throw se;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   444
        } else if (e instanceof RuntimeException) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
            throw (RuntimeException)e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   446
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
            // wrap any other exception as ScriptException
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
            throw new ScriptException(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   450
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
    private CompiledScript asCompiledScript(final ScriptFunction script) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
        return new CompiledScript() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
            public Object eval(final ScriptContext ctxt) throws ScriptException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
                return evalImpl(script, ctxt);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
            public ScriptEngine getEngine() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
                return NashornScriptEngine.this;
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
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   465
    private ScriptFunction compileImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException {
16243
f5fdf89e1583 8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents: 16232
diff changeset
   466
        final Object val = ctxt.getAttribute(ScriptEngine.FILENAME);
f5fdf89e1583 8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents: 16232
diff changeset
   467
        final String fileName = (val != null) ? val.toString() : "<eval>";
f5fdf89e1583 8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents: 16232
diff changeset
   468
        return compileImpl(new Source(fileName, buf), ctxt);
f5fdf89e1583 8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents: 16232
diff changeset
   469
    }
f5fdf89e1583 8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents: 16232
diff changeset
   470
f5fdf89e1583 8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents: 16232
diff changeset
   471
    private ScriptFunction compileImpl(final Source source, final ScriptContext ctxt) throws ScriptException {
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   472
        final ScriptObject oldGlobal = getNashornGlobal();
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   473
        final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   474
        final boolean globalChanged = (oldGlobal != ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   475
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
            if (globalChanged) {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   477
                setNashornGlobal(ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
16230
c38c724d82e7 8008103: Source object should maintain URL of the script source as a private field
sundar
parents: 16211
diff changeset
   480
            return nashornContext.compileScript(source, ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   481
        } catch (final Exception e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   482
            throwAsScriptException(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
            throw new AssertionError("should not reach here");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
        } finally {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   485
            if (globalChanged) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
                setNashornGlobal(oldGlobal);
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
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   491
    private static boolean isInterfaceImplemented(final Class<?> iface, final ScriptObject sobj) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   492
        for (final Method method : iface.getMethods()) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   493
            // ignore methods of java.lang.Object class
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   494
            if (method.getDeclaringClass() == Object.class) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   495
                continue;
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   496
            }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   497
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   498
            Object obj = sobj.get(method.getName());
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   499
            if (! (obj instanceof ScriptFunction)) {
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   500
                return false;
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   501
            }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   502
        }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   503
        return true;
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   504
    }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   505
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   506
    // don't make this public!!
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   507
    static ScriptObject getNashornGlobal() {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   508
        return Context.getGlobal();
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   509
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16185
diff changeset
   510
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   511
    static void setNashornGlobal(final ScriptObject newGlobal) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   512
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   513
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   514
            public Void run() {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   515
               Context.setGlobal(newGlobal);
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   516
               return null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   517
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   518
        });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   519
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   520
}