nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/scripting/NashornScriptEngine.java
author sundar
Mon, 05 Jan 2015 16:02:56 +0530
changeset 28317 0aeeae75e696
parent 27099 eceb216332cb
child 28785 a503c972d4bd
permissions -rw-r--r--
8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files Reviewed-by: attila, 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
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23375
diff changeset
    28
import static jdk.nashorn.internal.runtime.Source.sourceFor;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import java.io.IOException;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import java.io.Reader;
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 22379
diff changeset
    32
import java.lang.invoke.MethodHandles;
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
    33
import java.lang.reflect.Method;
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
    34
import java.lang.reflect.Modifier;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    35
import java.security.AccessControlContext;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import java.security.AccessController;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    37
import java.security.Permissions;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
import java.security.PrivilegedAction;
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    39
import java.security.ProtectionDomain;
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
    40
import java.text.MessageFormat;
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
    41
import java.util.Locale;
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
    42
import java.util.ResourceBundle;
16147
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;
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
    52
import javax.script.SimpleBindings;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
    53
import jdk.nashorn.internal.objects.Global;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
import jdk.nashorn.internal.runtime.Context;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
import jdk.nashorn.internal.runtime.ErrorManager;
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
28317
0aeeae75e696 8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents: 27099
diff changeset
    68
 *
0aeeae75e696 8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents: 27099
diff changeset
    69
 * @since 1.8u40
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
 */
28317
0aeeae75e696 8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents: 27099
diff changeset
    71
@jdk.Exported
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
public final class NashornScriptEngine extends AbstractScriptEngine implements Compilable, Invocable {
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
    73
    /**
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
    74
     * Key used to associate Nashorn global object mirror with arbitrary Bindings instance.
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
    75
     */
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
    76
    public static final String NASHORN_GLOBAL = "nashorn.global";
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
    77
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
    78
    // commonly used access control context objects
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    79
    private static AccessControlContext createPermAccCtxt(final String permName) {
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    80
        final Permissions perms = new Permissions();
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    81
        perms.add(new RuntimePermission(permName));
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    82
        return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    83
    }
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    84
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    85
    private static final AccessControlContext CREATE_CONTEXT_ACC_CTXT = createPermAccCtxt(Context.NASHORN_CREATE_CONTEXT);
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
    86
    private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT  = createPermAccCtxt(Context.NASHORN_CREATE_GLOBAL);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
    88
    // the factory that created this engine
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
    private final ScriptEngineFactory factory;
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
    90
    // underlying nashorn Context - 1:1 with engine instance
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
    private final Context             nashornContext;
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
    92
    // do we want to share single Nashorn global instance across ENGINE_SCOPEs?
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
    93
    private final boolean             _global_per_engine;
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
    94
    // This is the initial default Nashorn global object.
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
    95
    // This is used as "shared" global if above option is true.
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
    96
    private final Global              global;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
    98
    // Nashorn script engine error message management
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
    99
    private static final String MESSAGES_RESOURCE = "jdk.nashorn.api.scripting.resources.Messages";
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   100
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   101
    private static final ResourceBundle MESSAGES_BUNDLE;
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   102
    static {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
   103
        MESSAGES_BUNDLE = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault());
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   104
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   105
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   106
    // helper to get Nashorn script engine error message
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   107
    private static String getMessage(final String msgId, final String... args) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   108
        try {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   109
            return new MessageFormat(MESSAGES_BUNDLE.getString(msgId)).format(args);
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   110
        } catch (final java.util.MissingResourceException e) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   111
            throw new RuntimeException("no message resource found for message id: "+ msgId);
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   112
        }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   113
    }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   114
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   115
    NashornScriptEngine(final NashornScriptEngineFactory factory, final String[] args, final ClassLoader appLoader, final ClassFilter classFilter) {
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   116
        assert args != null : "null argument array";
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
        this.factory = factory;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
        final Options options = new Options("nashorn");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
        options.process(args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
        // throw ParseException on first error from script
16185
893aabe8c800 8006635: Reduce access levels as much as possible
sundar
parents: 16182
diff changeset
   122
        final ErrorManager errMgr = new Context.ThrowErrorManager();
16180
374e36bd1357 8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents: 16172
diff changeset
   123
        // create new Nashorn Context
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
        this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
            public Context run() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
                try {
26071
430e39f6731f 8050078: Nashorn ClassFilter Support
sundar
parents: 26068
diff changeset
   128
                    return new Context(options, errMgr, appLoader, classFilter);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
                } catch (final RuntimeException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
                    if (Context.DEBUG) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
                        e.printStackTrace();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
                    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
                    throw e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
            }
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
   136
        }, CREATE_CONTEXT_ACC_CTXT);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   138
        // cache this option that is used often
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   139
        this._global_per_engine = nashornContext.getEnv()._global_per_engine;
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   140
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
        // create new global object
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   142
        this.global = createNashornGlobal(context);
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   143
        // set the default ENGINE_SCOPE object for the default context
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   144
        context.setBindings(new ScriptObjectMirror(global, global), ScriptContext.ENGINE_SCOPE);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
    public Object eval(final Reader reader, final ScriptContext ctxt) throws ScriptException {
20210
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   149
        return evalImpl(makeSource(reader, ctxt), ctxt);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
    public Object eval(final String script, final ScriptContext ctxt) throws ScriptException {
20210
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   154
        return evalImpl(makeSource(script, ctxt), ctxt);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
    public ScriptEngineFactory getFactory() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
        return factory;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
    public Bindings createBindings() {
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   164
        if (_global_per_engine) {
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   165
            // just create normal SimpleBindings.
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   166
            // We use same 'global' for all Bindings.
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   167
            return new SimpleBindings();
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   168
        }
19630
99f53f31008e 8023550: -d option was broken for any dir but '.'. Fixed Java warnings.
lagergren
parents: 19624
diff changeset
   169
        return createGlobalMirror(null);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
    // Compilable methods
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
    public CompiledScript compile(final Reader reader) throws ScriptException {
20210
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   176
        return asCompiledScript(makeSource(reader, context));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
    public CompiledScript compile(final String str) throws ScriptException {
20210
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   181
        return asCompiledScript(makeSource(str, context));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
    // Invocable methods
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
    public Object invokeFunction(final String name, final Object... args)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
            throws ScriptException, NoSuchMethodException {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
        return invokeImpl(null, name, args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
    @Override
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   193
    public Object invokeMethod(final Object thiz, final String name, final Object... args)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
            throws ScriptException, NoSuchMethodException {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   195
        if (thiz == null) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   196
            throw new IllegalArgumentException(getMessage("thiz.cannot.be.null"));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
        }
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   198
        return invokeImpl(thiz, name, args);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   201
    @Override
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   202
    public <T> T getInterface(final Class<T> clazz) {
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   203
        return getInterfaceInner(null, clazz);
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   204
    }
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   205
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   206
    @Override
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   207
    public <T> T getInterface(final Object thiz, final Class<T> clazz) {
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   208
        if (thiz == null) {
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   209
            throw new IllegalArgumentException(getMessage("thiz.cannot.be.null"));
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   210
        }
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   211
        return getInterfaceInner(thiz, clazz);
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   212
    }
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   213
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   214
    // Implementation only below this point
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   215
20210
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   216
    private static Source makeSource(final Reader reader, final ScriptContext ctxt) throws ScriptException {
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   217
        try {
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23375
diff changeset
   218
            return sourceFor(getScriptName(ctxt), reader);
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   219
        } catch (final IOException e) {
20933
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 20567
diff changeset
   220
            throw new ScriptException(e);
20210
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   221
        }
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   222
    }
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   223
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   224
    private static Source makeSource(final String src, final ScriptContext ctxt) {
24206
40c6d45af73f 8040078: Avoid repeated reading of source for cached loads
hannesw
parents: 23375
diff changeset
   225
        return sourceFor(getScriptName(ctxt), src);
20210
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   226
    }
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   227
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   228
    private static String getScriptName(final ScriptContext ctxt) {
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   229
        final Object val = ctxt.getAttribute(ScriptEngine.FILENAME);
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   230
        return (val != null) ? val.toString() : "<eval>";
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   231
    }
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   232
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   233
    private <T> T getInterfaceInner(final Object thiz, final Class<T> clazz) {
27099
eceb216332cb 8050977: Java8 Javascript Nashorn exception: no current Global instance for nashorn
sundar
parents: 26071
diff changeset
   234
        assert !(thiz instanceof ScriptObject) : "raw ScriptObject not expected here";
eceb216332cb 8050977: Java8 Javascript Nashorn exception: no current Global instance for nashorn
sundar
parents: 26071
diff changeset
   235
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   236
        if (clazz == null || !clazz.isInterface()) {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   237
            throw new IllegalArgumentException(getMessage("interface.class.expected"));
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   238
        }
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   239
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   240
        // perform security access check as early as possible
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   241
        final SecurityManager sm = System.getSecurityManager();
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   242
        if (sm != null) {
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   243
            if (! Modifier.isPublic(clazz.getModifiers())) {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   244
                throw new SecurityException(getMessage("implementing.non.public.interface", clazz.getName()));
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   245
            }
20567
5621fc356049 8025771: Enhance Nashorn Contexts
sundar
parents: 20210
diff changeset
   246
            Context.checkPackageAccess(clazz);
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   247
        }
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   248
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   249
        ScriptObject realSelf = null;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   250
        Global realGlobal = null;
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   251
        if(thiz == null) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   252
            // making interface out of global functions
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   253
            realSelf = realGlobal = getNashornGlobalFrom(context);
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   254
        } else if (thiz instanceof ScriptObjectMirror) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   255
            final ScriptObjectMirror mirror = (ScriptObjectMirror)thiz;
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   256
            realSelf = mirror.getScriptObject();
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   257
            realGlobal = mirror.getHomeGlobal();
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19630
diff changeset
   258
            if (! isOfContext(realGlobal, nashornContext)) {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   259
                throw new IllegalArgumentException(getMessage("script.object.from.another.engine"));
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   260
            }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   261
        }
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   262
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   263
        if (realSelf == null) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   264
            throw new IllegalArgumentException(getMessage("interface.on.non.script.object"));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
        }
18864
c701b823ed9e 8020276: interface checks in Invocable.getInterface implementation
sundar
parents: 18615
diff changeset
   266
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
        try {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   268
            final Global oldGlobal = Context.getGlobal();
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   269
            final boolean globalChanged = (oldGlobal != realGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
            try {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   271
                if (globalChanged) {
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   272
                    Context.setGlobal(realGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
                }
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   274
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   275
                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
   276
                    return null;
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   277
                }
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 22379
diff changeset
   278
                return clazz.cast(JavaAdapterFactory.getConstructor(realSelf.getClass(), clazz,
75563515567f 8032681: Issues with Nashorn
attila
parents: 22379
diff changeset
   279
                        MethodHandles.publicLookup()).invoke(realSelf));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
            } finally {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   281
                if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18865
diff changeset
   282
                    Context.setGlobal(oldGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
        } catch(final RuntimeException|Error e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
            throw e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
        } catch(final Throwable t) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
            throw new RuntimeException(t);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   292
    // Retrieve nashorn Global object for a given ScriptContext object
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   293
    private Global getNashornGlobalFrom(final ScriptContext ctxt) {
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   294
        if (_global_per_engine) {
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   295
            // shared single global object for all ENGINE_SCOPE Bindings
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   296
            return global;
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   297
        }
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   298
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   299
        final Bindings bindings = ctxt.getBindings(ScriptContext.ENGINE_SCOPE);
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   300
        // is this Nashorn's own Bindings implementation?
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   301
        if (bindings instanceof ScriptObjectMirror) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   302
            final Global glob = globalFromMirror((ScriptObjectMirror)bindings);
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   303
            if (glob != null) {
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   304
                return glob;
19620
3f0c79b63846 8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction
sundar
parents: 19459
diff changeset
   305
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   308
        // Arbitrary user Bindings implementation. Look for NASHORN_GLOBAL in it!
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24727
diff changeset
   309
        final Object scope = bindings.get(NASHORN_GLOBAL);
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   310
        if (scope instanceof ScriptObjectMirror) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   311
            final Global glob = globalFromMirror((ScriptObjectMirror)scope);
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   312
            if (glob != null) {
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   313
                return glob;
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   314
            }
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   315
        }
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   316
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   317
        // We didn't find associated nashorn global mirror in the Bindings given!
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   318
        // Create new global instance mirror and associate with the Bindings.
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   319
        final ScriptObjectMirror mirror = createGlobalMirror(ctxt);
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   320
        bindings.put(NASHORN_GLOBAL, mirror);
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   321
        return mirror.getHomeGlobal();
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   322
    }
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   323
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   324
    // Retrieve nashorn Global object from a given ScriptObjectMirror
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   325
    private Global globalFromMirror(final ScriptObjectMirror mirror) {
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24727
diff changeset
   326
        final ScriptObject sobj = mirror.getScriptObject();
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   327
        if (sobj instanceof Global && isOfContext((Global)sobj, nashornContext)) {
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   328
            return (Global)sobj;
19623
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   329
        }
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   330
53207b2e3a7d 8023560: Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected.
sundar
parents: 19620
diff changeset
   331
        return null;
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   332
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   333
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   334
    // Create a new ScriptObjectMirror wrapping a newly created Nashorn Global object
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   335
    private ScriptObjectMirror createGlobalMirror(final ScriptContext ctxt) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   336
        final Global newGlobal = createNashornGlobal(ctxt);
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   337
        return new ScriptObjectMirror(newGlobal, newGlobal);
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   338
    }
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   339
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   340
    // Create a new Nashorn Global object
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   341
    private Global createNashornGlobal(final ScriptContext ctxt) {
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   342
        final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   343
            @Override
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   344
            public Global run() {
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   345
                try {
16211
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
   346
                    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
   347
                } 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
   348
                    if (Context.DEBUG) {
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   349
                        e.printStackTrace();
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   350
                    }
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   351
                    throw e;
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   352
                }
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   353
            }
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 19101
diff changeset
   354
        }, CREATE_GLOBAL_ACC_CTXT);
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   355
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 24779
diff changeset
   356
        nashornContext.initGlobal(newGlobal, this);
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 24779
diff changeset
   357
        newGlobal.setScriptContext(ctxt);
16211
41e031a45186 8006191: `cmd` -> exec("cmd") in script mode
jlaskey
parents: 16210
diff changeset
   358
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   359
        return newGlobal;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   361
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   362
    private Object invokeImpl(final Object selfObject, final String name, final Object... args) throws ScriptException, NoSuchMethodException {
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   363
        name.getClass(); // null check
27099
eceb216332cb 8050977: Java8 Javascript Nashorn exception: no current Global instance for nashorn
sundar
parents: 26071
diff changeset
   364
        assert !(selfObject instanceof ScriptObject) : "raw ScriptObject not expected here";
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   366
        Global invokeGlobal = null;
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   367
        ScriptObjectMirror selfMirror = null;
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   368
        if (selfObject instanceof ScriptObjectMirror) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   369
            selfMirror = (ScriptObjectMirror)selfObject;
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19630
diff changeset
   370
            if (! isOfContext(selfMirror.getHomeGlobal(), nashornContext)) {
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   371
                throw new IllegalArgumentException(getMessage("script.object.from.another.engine"));
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   372
            }
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 20933
diff changeset
   373
            invokeGlobal = selfMirror.getHomeGlobal();
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   374
        } else if (selfObject == null) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   375
            // selfObject is null => global function call
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   376
            final Global ctxtGlobal = getNashornGlobalFrom(context);
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 20933
diff changeset
   377
            invokeGlobal = ctxtGlobal;
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   378
            selfMirror = (ScriptObjectMirror)ScriptObjectMirror.wrap(ctxtGlobal, ctxtGlobal);
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   379
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   381
        if (selfMirror != null) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   382
            try {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 19884
diff changeset
   383
                return ScriptObjectMirror.translateUndefined(selfMirror.callMember(name, args));
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   384
            } catch (final Exception e) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   385
                final Throwable cause = e.getCause();
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   386
                if (cause instanceof NoSuchMethodException) {
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   387
                    throw (NoSuchMethodException)cause;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
                }
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 20933
diff changeset
   389
                throwAsScriptException(e, invokeGlobal);
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   390
                throw new AssertionError("should not reach here");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
        }
19099
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   393
30230d3febb8 8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents: 19085
diff changeset
   394
        // Non-script object passed as selfObject
19101
3e6fe94f02a8 8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents: 19099
diff changeset
   395
        throw new IllegalArgumentException(getMessage("interface.on.non.script.object"));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   397
20210
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   398
    private Object evalImpl(final Source src, final ScriptContext ctxt) throws ScriptException {
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   399
        return evalImpl(compileImpl(src, ctxt), ctxt);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
    private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt) throws ScriptException {
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   403
        return evalImpl(script, ctxt, getNashornGlobalFrom(ctxt));
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   404
    }
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   405
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25422
diff changeset
   406
    private static Object evalImpl(final Context.MultiGlobalCompiledScript mgcs, final ScriptContext ctxt, final Global ctxtGlobal) throws ScriptException {
24282
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   407
        final Global oldGlobal = Context.getGlobal();
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   408
        final boolean globalChanged = (oldGlobal != ctxtGlobal);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   409
        try {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   410
            if (globalChanged) {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   411
                Context.setGlobal(ctxtGlobal);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   412
            }
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   413
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   414
            final ScriptFunction script = mgcs.getFunction(ctxtGlobal);
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 24779
diff changeset
   415
            ctxtGlobal.setScriptContext(ctxt);
24282
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   416
            return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal));
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   417
        } catch (final Exception e) {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   418
            throwAsScriptException(e, ctxtGlobal);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   419
            throw new AssertionError("should not reach here");
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   420
        } finally {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   421
            if (globalChanged) {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   422
                Context.setGlobal(oldGlobal);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   423
            }
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   424
        }
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   425
    }
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   426
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25422
diff changeset
   427
    private static Object evalImpl(final ScriptFunction script, final ScriptContext ctxt, final Global ctxtGlobal) throws ScriptException {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
        if (script == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   429
            return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   430
        }
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   431
        final Global oldGlobal = Context.getGlobal();
16189
38eaf01bf6d6 8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents: 16188
diff changeset
   432
        final boolean globalChanged = (oldGlobal != ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   433
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18865
diff changeset
   435
                Context.setGlobal(ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 24779
diff changeset
   438
            ctxtGlobal.setScriptContext(ctxt);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16528
diff changeset
   439
            return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
        } catch (final Exception e) {
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 20933
diff changeset
   441
            throwAsScriptException(e, ctxtGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
            throw new AssertionError("should not reach here");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
        } finally {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   444
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18865
diff changeset
   445
                Context.setGlobal(oldGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   446
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   450
    private static void throwAsScriptException(final Exception e, final Global global) throws ScriptException {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
        if (e instanceof ScriptException) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
            throw (ScriptException)e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
        } else if (e instanceof NashornException) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
            final NashornException ne = (NashornException)e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
            final ScriptException se = new ScriptException(
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
                ne.getMessage(), ne.getFileName(),
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
                ne.getLineNumber(), ne.getColumnNumber());
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 20933
diff changeset
   458
            ne.initEcmaError(global);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
            se.initCause(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
            throw se;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
        } else if (e instanceof RuntimeException) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   462
            throw (RuntimeException)e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   463
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   464
            // wrap any other exception as ScriptException
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   465
            throw new ScriptException(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   466
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   467
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   468
20210
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   469
    private CompiledScript asCompiledScript(final Source source) throws ScriptException {
24282
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   470
        final Context.MultiGlobalCompiledScript mgcs;
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   471
        final ScriptFunction func;
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   472
        final Global oldGlobal = Context.getGlobal();
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   473
        final Global newGlobal = getNashornGlobalFrom(context);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   474
        final boolean globalChanged = (oldGlobal != newGlobal);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   475
        try {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   476
            if (globalChanged) {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   477
                Context.setGlobal(newGlobal);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   478
            }
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   479
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   480
            mgcs = nashornContext.compileScript(source);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   481
            func = mgcs.getFunction(newGlobal);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   482
        } catch (final Exception e) {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   483
            throwAsScriptException(e, newGlobal);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   484
            throw new AssertionError("should not reach here");
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   485
        } finally {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   486
            if (globalChanged) {
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   487
                Context.setGlobal(oldGlobal);
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   488
            }
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   489
        }
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   490
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   491
        return new CompiledScript() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   492
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   493
            public Object eval(final ScriptContext ctxt) throws ScriptException {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   494
                final Global globalObject = getNashornGlobalFrom(ctxt);
24282
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   495
                // Are we running the script in the same global in which it was compiled?
20933
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 20567
diff changeset
   496
                if (func.getScope() == globalObject) {
89748612fd1d 8026250: Logging nullpointer bugfix and javadoc warnings
lagergren
parents: 20567
diff changeset
   497
                    return evalImpl(func, ctxt, globalObject);
20210
80d652da38ac 8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents: 19889
diff changeset
   498
                }
24282
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   499
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   500
                // different global
2e3bd98c5664 8041697: CompiledScript slower when eval with binding
sundar
parents: 24206
diff changeset
   501
                return evalImpl(mgcs, ctxt, globalObject);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   502
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   503
            @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   504
            public ScriptEngine getEngine() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   505
                return NashornScriptEngine.this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   506
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   507
        };
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   508
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   509
16243
f5fdf89e1583 8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents: 16232
diff changeset
   510
    private ScriptFunction compileImpl(final Source source, final ScriptContext ctxt) throws ScriptException {
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   511
        return compileImpl(source, getNashornGlobalFrom(ctxt));
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   512
    }
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   513
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   514
    private ScriptFunction compileImpl(final Source source, final Global newGlobal) throws ScriptException {
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   515
        final Global oldGlobal = Context.getGlobal();
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   516
        final boolean globalChanged = (oldGlobal != newGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   517
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   518
            if (globalChanged) {
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   519
                Context.setGlobal(newGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   520
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   521
19624
976b3bfbd657 8023631: engine.js init script should be loaded into every global instance created by engines
sundar
parents: 19623
diff changeset
   522
            return nashornContext.compileScript(source, newGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   523
        } catch (final Exception e) {
22370
c150d042ffff 8029364: NashornException to expose thrown object
sundar
parents: 20933
diff changeset
   524
            throwAsScriptException(e, newGlobal);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   525
            throw new AssertionError("should not reach here");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   526
        } finally {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   527
            if (globalChanged) {
19085
066c9e5afd79 8020731: Revisit checkPermission calls in Context class
sundar
parents: 18865
diff changeset
   528
                Context.setGlobal(oldGlobal);
16147
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
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   532
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   533
    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
   534
        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
   535
            // 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
   536
            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
   537
                continue;
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   538
            }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   539
22379
5181d08e3440 8031359: Invocable.getInterface() works incorrectly if interface has default methods
sundar
parents: 22370
diff changeset
   540
            // skip check for default methods - non-abstract, interface methods
5181d08e3440 8031359: Invocable.getInterface() works incorrectly if interface has default methods
sundar
parents: 22370
diff changeset
   541
            if (! Modifier.isAbstract(method.getModifiers())) {
5181d08e3440 8031359: Invocable.getInterface() works incorrectly if interface has default methods
sundar
parents: 22370
diff changeset
   542
                continue;
5181d08e3440 8031359: Invocable.getInterface() works incorrectly if interface has default methods
sundar
parents: 22370
diff changeset
   543
            }
5181d08e3440 8031359: Invocable.getInterface() works incorrectly if interface has default methods
sundar
parents: 22370
diff changeset
   544
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24727
diff changeset
   545
            final Object obj = sobj.get(method.getName());
16528
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   546
            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
   547
                return false;
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   548
            }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   549
        }
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   550
        return true;
8d20ffabe47e 8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents: 16527
diff changeset
   551
    }
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19630
diff changeset
   552
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   553
    private static boolean isOfContext(final Global global, final Context context) {
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22669
diff changeset
   554
        return global.isOfContext(context);
19884
1bacbaa1bfc7 8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents: 19630
diff changeset
   555
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   556
}