src/java.scripting/share/classes/javax/script/SimpleScriptContext.java
author hannesw
Mon, 23 Apr 2018 18:14:35 +0200
changeset 49858 56923ee4f07e
parent 47216 71c04702a3d5
permissions -rw-r--r--
8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient Reviewed-by: sundar, jlaskey
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18567
35b36d452864 8019320: Fix doclint issues in javax.script
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.script;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Simple implementation of ScriptContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Mike Grogan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class SimpleScriptContext  implements ScriptContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * This is the writer to be used to output from scripts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * By default, a <code>PrintWriter</code> based on <code>System.out</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * is used. Accessor methods getWriter, setWriter are used to manage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * @see java.lang.System#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * @see java.io.PrintWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected Writer writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * This is the writer to be used to output errors from scripts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * By default, a <code>PrintWriter</code> based on <code>System.err</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * used. Accessor methods getErrorWriter, setErrorWriter are used to manage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @see java.lang.System#err
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @see java.io.PrintWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected Writer errorWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * This is the reader to be used for input from scripts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * By default, a <code>InputStreamReader</code> based on <code>System.in</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * is used and default charset is used by this reader. Accessor methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * getReader, setReader are used to manage this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @see java.lang.System#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @see java.io.InputStreamReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected Reader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * This is the engine scope bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * By default, a <code>SimpleBindings</code> is used. Accessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * methods setBindings, getBindings are used to manage this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @see SimpleBindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected Bindings engineScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * This is the global scope bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * By default, a null value (which means no global scope) is used. Accessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * methods setBindings, getBindings are used to manage this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected Bindings globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
18567
35b36d452864 8019320: Fix doclint issues in javax.script
darcy
parents: 5506
diff changeset
    85
    /**
35b36d452864 8019320: Fix doclint issues in javax.script
darcy
parents: 5506
diff changeset
    86
     * Create a {@code SimpleScriptContext}.
35b36d452864 8019320: Fix doclint issues in javax.script
darcy
parents: 5506
diff changeset
    87
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public SimpleScriptContext() {
49858
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
    89
        this(new InputStreamReader(System.in),
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
    90
             new PrintWriter(System.out , true),
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
    91
             new PrintWriter(System.err, true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        engineScope = new SimpleBindings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        globalScope = null;
49858
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
    94
    }
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
    95
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
    96
    /**
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
    97
     * Package-private constructor to avoid needless creation of reader and writers.
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
    98
     * It is the caller's responsability to initialize the engine scope.
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
    99
     *
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
   100
     * @param reader the reader
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
   101
     * @param writer the writer
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
   102
     * @param errorWriter the error writer
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
   103
     */
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
   104
    SimpleScriptContext(Reader reader, Writer writer, Writer errorWriter) {
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
   105
        this.reader = reader;
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
   106
        this.writer = writer;
56923ee4f07e 8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
hannesw
parents: 47216
diff changeset
   107
        this.errorWriter = errorWriter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Sets a <code>Bindings</code> of attributes for the given scope.  If the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <code>engineScope</code> field.  If the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param bindings The <code>Bindings</code> of attributes to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param scope The value of the scope in which the attributes are set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @throws IllegalArgumentException if scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * the specified <code>Bindings</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void setBindings(Bindings bindings, int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if (bindings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    throw new NullPointerException("Engine scope cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                engineScope = bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                globalScope = bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                throw new IllegalArgumentException("Invalid scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Retrieves the value of the attribute with the given name in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * the scope occurring earliest in the search order.  The order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * is determined by the numeric value of the scope parameter (lowest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * scope values first.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   149
     * @param name The name of the attribute to retrieve.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return The value of the attribute in the lowest scope for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * which an attribute with the given name is defined.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * null if no attribute with the name exists in any scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws IllegalArgumentException if the name is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public Object getAttribute(String name) {
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   157
        checkName(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (engineScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return getAttribute(name, ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } else if (globalScope != null && globalScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return getAttribute(name, GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Gets the value of an attribute in a given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param name The name of the attribute to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param scope The scope in which to retrieve the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return The value of the attribute. Returns <code>null</code> is the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * does not exist in the given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *         if the name is empty or if the value of scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public Object getAttribute(String name, int scope) {
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   180
        checkName(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                return engineScope.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (globalScope != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    return globalScope.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Remove an attribute in a given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param name The name of the attribute to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param scope The scope in which to remove the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return The removed value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *         if the name is empty or if the scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public Object removeAttribute(String name, int scope) {
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   209
        checkName(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (getBindings(ENGINE_SCOPE) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    return getBindings(ENGINE_SCOPE).remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                if (getBindings(GLOBAL_SCOPE) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    return getBindings(GLOBAL_SCOPE).remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
41554
fc6a35a87a52 8071678: javax.script.ScriptContext setAttribute method should clarify behavior when GLOBAL_SCOPE is used and global scope object is null
sundar
parents: 30647
diff changeset
   230
     * Sets the value of an attribute in a given scope. If the scope is <code>GLOBAL_SCOPE</code>
fc6a35a87a52 8071678: javax.script.ScriptContext setAttribute method should clarify behavior when GLOBAL_SCOPE is used and global scope object is null
sundar
parents: 30647
diff changeset
   231
     * and no Bindings is set for <code>GLOBAL_SCOPE</code>, then setAttribute call is a no-op.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param name The name of the attribute to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param value The value of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param scope The scope in which to set the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *         if the name is empty or if the scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public void setAttribute(String name, Object value, int scope) {
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   242
        checkName(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                engineScope.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (globalScope != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    globalScope.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public Writer getWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public Reader getReader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public void setReader(Reader reader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        this.reader = reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public void setWriter(Writer writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        this.writer = writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public Writer getErrorWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return errorWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public void setErrorWriter(Writer writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        this.errorWriter = writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Get the lowest scope in which an attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param name Name of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return The lowest scope.  Returns -1 if no attribute with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * name is defined in any scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @throws NullPointerException if name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @throws IllegalArgumentException if name is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public int getAttributesScope(String name) {
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   300
        checkName(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (engineScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return ENGINE_SCOPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } else if (globalScope != null && globalScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return GLOBAL_SCOPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Returns the value of the <code>engineScope</code> field if specified scope is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <code>ENGINE_SCOPE</code>.  Returns the value of the <code>globalScope</code> field if the specified scope is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <code>GLOBAL_SCOPE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param scope The specified scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @return The value of either the  <code>engineScope</code> or <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @throws IllegalArgumentException if the value of scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public Bindings getBindings(int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (scope == ENGINE_SCOPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return engineScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } else if (scope == GLOBAL_SCOPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public List<Integer> getScopes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return scopes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   334
    private void checkName(String name) {
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   335
        Objects.requireNonNull(name);
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   336
        if (name.isEmpty()) {
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   337
            throw new IllegalArgumentException("name cannot be empty");
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   338
        }
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   339
    }
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   340
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    private static List<Integer> scopes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        scopes = new ArrayList<Integer>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        scopes.add(ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        scopes.add(GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        scopes = Collections.unmodifiableList(scopes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
}