src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ECMAErrors.java
author hannesw
Wed, 21 Mar 2018 16:55:34 +0100
changeset 49275 c639a6b33c5c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199869: Missing copyright headers in nashorn source code Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import java.text.MessageFormat;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import java.util.Locale;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import java.util.ResourceBundle;
22373
ca044992c73f 8030182: scopeCall with -1 as line number
sundar
parents: 19897
diff changeset
    31
import jdk.nashorn.internal.codegen.CompilerConstants;
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
    32
import jdk.nashorn.internal.objects.Global;
16275
d5d430071b22 8009379: Remove $ from generated class names
jlaskey
parents: 16256
diff changeset
    33
import jdk.nashorn.internal.scripts.JS;
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    34
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
 * Helper class to throw various standard "ECMA error" exceptions such as Error, ReferenceError, TypeError etc.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
public final class ECMAErrors {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
    private static final String MESSAGES_RESOURCE = "jdk.nashorn.internal.runtime.resources.Messages";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
16154
de44634fa4ec 8005782: get rid of javadoc errors, warnings in nashorn build
sundar
parents: 16151
diff changeset
    41
    private static final ResourceBundle MESSAGES_BUNDLE;
de44634fa4ec 8005782: get rid of javadoc errors, warnings in nashorn build
sundar
parents: 16151
diff changeset
    42
    static {
19459
79e75274df99 8022707: Revisit all doPrivileged blocks
sundar
parents: 16275
diff changeset
    43
        MESSAGES_BUNDLE = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault());
16154
de44634fa4ec 8005782: get rid of javadoc errors, warnings in nashorn build
sundar
parents: 16151
diff changeset
    44
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    46
    /** We assume that compiler generates script classes into the known package. */
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    47
    private static final String scriptPackage;
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    48
    static {
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24727
diff changeset
    49
        final String name = JS.class.getName();
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    50
        scriptPackage = name.substring(0, name.lastIndexOf('.'));
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    51
    }
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    52
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
    private ECMAErrors() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    56
    private static ECMAException error(final Object thrown, final Throwable cause) {
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    57
        return new ECMAException(thrown, cause);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
    60
     /**
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
    61
     * Error dispatch mechanism.
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    62
     * Create a {@link ParserException} as the correct JavaScript error
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
    63
     *
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
    64
     * @param e {@code ParserException} for error dispatcher
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    65
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    66
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
    67
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    68
    public static ECMAException asEcmaException(final ParserException e) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
    69
        return asEcmaException(Context.getGlobal(), e);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
    70
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
    71
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
     * Error dispatch mechanism.
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    74
     * Create a {@link ParserException} as the correct JavaScript error
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
     * @param global global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
     * @param e {@code ParserException} for error dispatcher
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    78
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    79
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
    81
    public static ECMAException asEcmaException(final Global global, final ParserException e) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
        final JSErrorType errorType = e.getErrorType();
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    83
        assert errorType != null : "error type for " + e + " was null";
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
    85
        final Global globalObj    = global;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
        final String       msg    = e.getMessage();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
        // translate to ECMAScript Error object using error type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
        switch (errorType) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
        case ERROR:
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    91
            return error(globalObj.newError(msg), e);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
        case EVAL_ERROR:
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    93
            return error(globalObj.newEvalError(msg), e);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
        case RANGE_ERROR:
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    95
            return error(globalObj.newRangeError(msg), e);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
        case REFERENCE_ERROR:
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    97
            return error(globalObj.newReferenceError(msg), e);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
        case SYNTAX_ERROR:
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
    99
            return error(globalObj.newSyntaxError(msg), e);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
        case TYPE_ERROR:
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   101
            return error(globalObj.newTypeError(msg), e);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
        case URI_ERROR:
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   103
            return error(globalObj.newURIError(msg), e);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
        default:
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   105
            // should not happen - perhaps unknown error type?
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   106
            throw new AssertionError(e.getMessage());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
        }
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   108
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   110
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   111
     * Create a syntax error (ECMA 15.11.6.4)
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   112
     *
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   113
     * @param msgId   resource tag for error message
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   114
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   115
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   116
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   117
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   118
    public static ECMAException syntaxError(final String msgId, final String... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   119
        return syntaxError(Context.getGlobal(), msgId, args);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   120
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   123
     * Create a syntax error (ECMA 15.11.6.4)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
     * @param global  global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
     * @param msgId   resource tag for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   128
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   129
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   131
    public static ECMAException syntaxError(final Global global, final String msgId, final String... args) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   132
        return syntaxError(global, null, msgId, args);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   136
     * Create a syntax error (ECMA 15.11.6.4)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
     *
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   138
     * @param cause   native Java {@code Throwable} that is the cause of error
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   139
     * @param msgId   resource tag for error message
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   140
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   141
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   142
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   143
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   144
    public static ECMAException syntaxError(final Throwable cause, final String msgId, final String... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   145
        return syntaxError(Context.getGlobal(), cause, msgId, args);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   146
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   147
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   148
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   149
     * Create a syntax error (ECMA 15.11.6.4)
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   150
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
     * @param global  global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
     * @param cause   native Java {@code Throwable} that is the cause of error
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
     * @param msgId   resource tag for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   155
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   156
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   158
    public static ECMAException syntaxError(final Global global, final Throwable cause, final String msgId, final String... args) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
        final String msg = getMessage("syntax.error." + msgId, args);
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   160
        return error(global.newSyntaxError(msg), cause);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   164
     * Create a type error (ECMA 15.11.6.5)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
     *
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   166
     * @param msgId   resource tag for error message
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   167
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   168
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   169
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   170
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   171
    public static ECMAException typeError(final String msgId, final String... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   172
        return typeError(Context.getGlobal(), msgId, args);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   173
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   174
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   175
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   176
     * Create a type error (ECMA 15.11.6.5)
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   177
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
     * @param global  global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
     * @param msgId   resource tag for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   181
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   182
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   184
    public static ECMAException typeError(final Global global, final String msgId, final String... args) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   185
        return typeError(global, null, msgId, args);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   189
     * Create a type error (ECMA 15.11.6.5)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
     *
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   191
     * @param cause   native Java {@code Throwable} that is the cause of error
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   192
     * @param msgId   resource tag for error message
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   193
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   194
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   195
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   196
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   197
    public static ECMAException typeError(final Throwable cause, final String msgId, final String... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   198
        return typeError(Context.getGlobal(), cause, msgId, args);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   199
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   200
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   201
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   202
     * Create a type error (ECMA 15.11.6.5)
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   203
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
     * @param global  global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
     * @param cause   native Java {@code Throwable} that is the cause of error
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
     * @param msgId   resource tag for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   208
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   209
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   211
    public static ECMAException typeError(final Global global, final Throwable cause, final String msgId, final String... args) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
        final String msg = getMessage("type.error." + msgId, args);
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   213
        return error(global.newTypeError(msg), cause);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   217
     * Create a range error (ECMA 15.11.6.2)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
     *
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   219
     * @param msgId   resource tag for error message
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   220
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   221
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   222
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   223
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   224
    public static ECMAException rangeError(final String msgId, final String... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   225
        return rangeError(Context.getGlobal(), msgId, args);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   226
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   227
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   228
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   229
     * Create a range error (ECMA 15.11.6.2)
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   230
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
     * @param global  global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
     * @param msgId   resource tag for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   234
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   235
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   237
    public static ECMAException rangeError(final Global global, final String msgId, final String... args) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   238
        return rangeError(global, null, msgId, args);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   242
     * Create a range error (ECMA 15.11.6.2)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
     *
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   244
     * @param cause   native Java {@code Throwable} that is the cause of error
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   245
     * @param msgId   resource tag for error message
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   246
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   247
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   248
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   249
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   250
    public static ECMAException rangeError(final Throwable cause, final String msgId, final String... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   251
        return rangeError(Context.getGlobal(), cause, msgId, args);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   252
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   253
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   254
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   255
     * Create a range error (ECMA 15.11.6.2)
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   256
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
     * @param global  global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
     * @param cause   native Java {@code Throwable} that is the cause of error
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
     * @param msgId   resource tag for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   261
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   262
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   264
    public static ECMAException rangeError(final Global global, final Throwable cause, final String msgId, final String... args) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
        final String msg = getMessage("range.error." + msgId, args);
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   266
        return error(global.newRangeError(msg), cause);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   270
     * Create a reference error (ECMA 15.11.6.3)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
     *
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   272
     * @param msgId   resource tag for error message
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   273
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   274
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   275
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   276
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   277
    public static ECMAException referenceError(final String msgId, final String... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   278
        return referenceError(Context.getGlobal(), msgId, args);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   279
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   280
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   281
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   282
     * Create a reference error (ECMA 15.11.6.3)
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   283
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
     * @param global  global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
     * @param msgId   resource tag for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   287
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   288
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   290
    public static ECMAException referenceError(final Global global, final String msgId, final String... args) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   291
        return referenceError(global, null, msgId, args);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   295
     * Create a reference error (ECMA 15.11.6.3)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
     *
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   297
     * @param cause   native Java {@code Throwable} that is the cause of error
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   298
     * @param msgId   resource tag for error message
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   299
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   300
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   301
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   302
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   303
    public static ECMAException referenceError(final Throwable cause, final String msgId, final String... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   304
        return referenceError(Context.getGlobal(), cause, msgId, args);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   305
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   306
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   307
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   308
     * Create a reference error (ECMA 15.11.6.3)
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   309
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
     * @param global  global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
     * @param cause   native Java {@code Throwable} that is the cause of error
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
     * @param msgId   resource tag for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   314
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   315
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   316
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   317
    public static ECMAException referenceError(final Global global, final Throwable cause, final String msgId, final String... args) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
        final String msg = getMessage("reference.error." + msgId, args);
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   319
        return error(global.newReferenceError(msg), cause);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   320
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   322
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   323
     * Create a URI error (ECMA 15.11.6.6)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
     *
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   325
     * @param msgId   resource tag for error message
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   326
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   327
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   328
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   329
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   330
    public static ECMAException uriError(final String msgId, final String... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   331
        return uriError(Context.getGlobal(), msgId, args);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   332
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   333
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   334
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   335
     * Create a URI error (ECMA 15.11.6.6)
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   336
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   337
     * @param global  global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
     * @param msgId   resource tag for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   340
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   341
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   342
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   343
    public static ECMAException uriError(final Global global, final String msgId, final String... args) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   344
        return uriError(global, null, msgId, args);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   345
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   348
     * Create a URI error (ECMA 15.11.6.6)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   349
     *
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   350
     * @param cause   native Java {@code Throwable} that is the cause of error
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   351
     * @param msgId   resource tag for error message
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   352
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   353
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   354
     * @return the resulting {@link ECMAException}
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   355
     */
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   356
    public static ECMAException uriError(final Throwable cause, final String msgId, final String... args) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   357
        return uriError(Context.getGlobal(), cause, msgId, args);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   358
    }
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   359
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   360
    /**
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   361
     * Create a URI error (ECMA 15.11.6.6)
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16154
diff changeset
   362
     *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
     * @param global  global scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
     * @param cause   native Java {@code Throwable} that is the cause of error
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
     * @param msgId   resource tag for error message
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
     * @param args    arguments to resource
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   367
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   368
     * @return the resulting {@link ECMAException}
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
     */
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   370
    public static ECMAException uriError(final Global global, final Throwable cause, final String msgId, final String... args) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
        final String msg = getMessage("uri.error." + msgId, args);
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 22373
diff changeset
   372
        return error(global.newURIError(msg), cause);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
     * Get the exception message by placing the args in the resource defined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
     * by the resource tag. This is visible to, e.g. the {@link jdk.nashorn.internal.parser.Parser}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
     * can use it to generate compile time messages with the correct locale
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   379
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
     * @param msgId the resource tag (message id)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   381
     * @param args  arguments to error string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   382
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
     * @return the filled out error string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
    public static String getMessage(final String msgId, final String... args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   386
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
            return new MessageFormat(MESSAGES_BUNDLE.getString(msgId)).format(args);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
        } catch (final java.util.MissingResourceException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   389
            throw new RuntimeException("no message resource found for message id: "+ msgId);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   393
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   394
    /**
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   395
     * Check if a stack trace element is in JavaScript
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   396
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   397
     * @param frame frame
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   398
     *
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   399
     * @return true if frame is in the script
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   400
     */
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   401
    public static boolean isScriptFrame(final StackTraceElement frame) {
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   402
        final String className = frame.getClassName();
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   403
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   404
        // Look for script package in class name (into which compiler puts generated code)
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   405
        if (className.startsWith(scriptPackage) && !CompilerConstants.isInternalMethodName(frame.getMethodName())) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   406
            final String source = frame.getFileName();
25422
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 24778
diff changeset
   407
            // Make sure that it is not some Java code that Nashorn has in that package!
199a23bee487 8049524: Global object initialization via javax.script API should be minimal
sundar
parents: 24778
diff changeset
   408
            return source != null && !source.endsWith(".java");
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   409
        }
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   410
        return false;
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16188
diff changeset
   411
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   412
}