author | hannesw |
Wed, 21 Mar 2018 16:55:34 +0100 | |
changeset 49275 | c639a6b33c5c |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.runtime; |
|
27 |
||
28 |
import java.text.MessageFormat; |
|
29 |
import java.util.Locale; |
|
30 |
import java.util.ResourceBundle; |
|
22373 | 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 | 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 | 35 |
/** |
36 |
* Helper class to throw various standard "ECMA error" exceptions such as Error, ReferenceError, TypeError etc. |
|
37 |
*/ |
|
38 |
public final class ECMAErrors { |
|
39 |
private static final String MESSAGES_RESOURCE = "jdk.nashorn.internal.runtime.resources.Messages"; |
|
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 | 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 | 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 | 53 |
private ECMAErrors() { |
54 |
} |
|
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 | 58 |
} |
59 |
||
16188 | 60 |
/** |
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 | 63 |
* |
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 | 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 | 70 |
} |
71 |
||
16147 | 72 |
/** |
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 | 75 |
* |
76 |
* @param global global scope object |
|
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 | 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 | 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 | 84 |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
22373
diff
changeset
|
85 |
final Global globalObj = global; |
16147 | 86 |
final String msg = e.getMessage(); |
87 |
||
88 |
// translate to ECMAScript Error object using error type |
|
89 |
switch (errorType) { |
|
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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 109 |
|
16188 | 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 | 112 |
* |
113 |
* @param msgId resource tag for error message |
|
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 | 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 | 120 |
} |
16147 | 121 |
|
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 | 124 |
* |
125 |
* @param global global scope object |
|
126 |
* @param msgId resource tag for error message |
|
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 | 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 | 133 |
} |
134 |
||
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 | 137 |
* |
16188 | 138 |
* @param cause native Java {@code Throwable} that is the cause of error |
139 |
* @param msgId resource tag for error message |
|
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 | 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 | 146 |
} |
147 |
||
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 | 150 |
* |
16147 | 151 |
* @param global global scope object |
152 |
* @param cause native Java {@code Throwable} that is the cause of error |
|
153 |
* @param msgId resource tag for error message |
|
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 | 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 | 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 | 161 |
} |
162 |
||
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 | 165 |
* |
16188 | 166 |
* @param msgId resource tag for error message |
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 | 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 | 173 |
} |
174 |
||
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 | 177 |
* |
16147 | 178 |
* @param global global scope object |
179 |
* @param msgId resource tag for error message |
|
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 | 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 | 186 |
} |
187 |
||
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 | 190 |
* |
16188 | 191 |
* @param cause native Java {@code Throwable} that is the cause of error |
192 |
* @param msgId resource tag for error message |
|
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 | 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 | 199 |
} |
200 |
||
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 | 203 |
* |
16147 | 204 |
* @param global global scope object |
205 |
* @param cause native Java {@code Throwable} that is the cause of error |
|
206 |
* @param msgId resource tag for error message |
|
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 | 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 | 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 | 214 |
} |
215 |
||
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 | 218 |
* |
16188 | 219 |
* @param msgId resource tag for error message |
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 | 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 | 226 |
} |
227 |
||
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 | 230 |
* |
16147 | 231 |
* @param global global scope object |
232 |
* @param msgId resource tag for error message |
|
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 | 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 | 239 |
} |
240 |
||
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 | 243 |
* |
16188 | 244 |
* @param cause native Java {@code Throwable} that is the cause of error |
245 |
* @param msgId resource tag for error message |
|
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 | 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 | 252 |
} |
253 |
||
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 | 256 |
* |
16147 | 257 |
* @param global global scope object |
258 |
* @param cause native Java {@code Throwable} that is the cause of error |
|
259 |
* @param msgId resource tag for error message |
|
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 | 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 | 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 | 267 |
} |
268 |
||
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 | 271 |
* |
16188 | 272 |
* @param msgId resource tag for error message |
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 | 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 | 279 |
} |
280 |
||
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 | 283 |
* |
16147 | 284 |
* @param global global scope object |
285 |
* @param msgId resource tag for error message |
|
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 | 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 | 292 |
} |
293 |
||
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 | 296 |
* |
16188 | 297 |
* @param cause native Java {@code Throwable} that is the cause of error |
298 |
* @param msgId resource tag for error message |
|
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 | 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 | 305 |
} |
306 |
||
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 | 309 |
* |
16147 | 310 |
* @param global global scope object |
311 |
* @param cause native Java {@code Throwable} that is the cause of error |
|
312 |
* @param msgId resource tag for error message |
|
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 | 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 | 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 | 320 |
} |
321 |
||
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 | 324 |
* |
16188 | 325 |
* @param msgId resource tag for error message |
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 | 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 | 332 |
} |
333 |
||
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 | 336 |
* |
16147 | 337 |
* @param global global scope object |
338 |
* @param msgId resource tag for error message |
|
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 | 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 | 345 |
} |
346 |
||
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 | 349 |
* |
16188 | 350 |
* @param cause native Java {@code Throwable} that is the cause of error |
351 |
* @param msgId resource tag for error message |
|
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 | 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 | 358 |
} |
359 |
||
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 | 362 |
* |
16147 | 363 |
* @param global global scope object |
364 |
* @param cause native Java {@code Throwable} that is the cause of error |
|
365 |
* @param msgId resource tag for error message |
|
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 | 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 | 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 | 373 |
} |
374 |
||
375 |
/** |
|
376 |
* Get the exception message by placing the args in the resource defined |
|
377 |
* by the resource tag. This is visible to, e.g. the {@link jdk.nashorn.internal.parser.Parser} |
|
378 |
* can use it to generate compile time messages with the correct locale |
|
379 |
* |
|
380 |
* @param msgId the resource tag (message id) |
|
381 |
* @param args arguments to error string |
|
382 |
* |
|
383 |
* @return the filled out error string |
|
384 |
*/ |
|
385 |
public static String getMessage(final String msgId, final String... args) { |
|
386 |
try { |
|
387 |
return new MessageFormat(MESSAGES_BUNDLE.getString(msgId)).format(args); |
|
388 |
} catch (final java.util.MissingResourceException e) { |
|
389 |
throw new RuntimeException("no message resource found for message id: "+ msgId); |
|
390 |
} |
|
391 |
} |
|
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 | 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 | 412 |
} |