author | hannesw |
Tue, 12 Dec 2017 15:38:18 +0100 | |
changeset 48247 | fa5a47cad0c9 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
24719 | 1 |
/* |
2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
|
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 |
package jdk.nashorn.internal.runtime; |
|
26 |
||
24776
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
27 |
import java.io.NotSerializableException; |
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
28 |
import java.io.ObjectInputStream; |
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
29 |
import java.io.ObjectOutputStream; |
24719 | 30 |
import jdk.nashorn.internal.codegen.types.Type; |
31 |
||
32 |
/** |
|
33 |
* This exception is thrown from an optimistic operation, e.g. an integer add, |
|
34 |
* that was to optimistic for what really took place. Typically things like |
|
35 |
* trying to get an array element that we want to be an int, and it was a double, |
|
36 |
* and an int add that actually overflows and needs a double for the representation |
|
37 |
*/ |
|
38 |
||
39 |
@SuppressWarnings("serial") |
|
40 |
public final class UnwarrantedOptimismException extends RuntimeException { |
|
41 |
/** Denotes an invalid program point */ |
|
42 |
public static final int INVALID_PROGRAM_POINT = -1; |
|
43 |
||
44 |
/** The value for the first ordinary program point */ |
|
24751 | 45 |
public static final int FIRST_PROGRAM_POINT = 1; |
24719 | 46 |
|
47 |
private Object returnValue; |
|
48 |
private final int programPoint; |
|
49 |
private final Type returnType; |
|
50 |
||
51 |
/** |
|
34732 | 52 |
* Constructor without explicit return type. The return type is determined statically from the class of |
53 |
* the return value, and only canonical internal number representations are recognized. Use |
|
54 |
* {@link #createNarrowest} if you want to handle float and long values as numbers instead of objects. |
|
55 |
* |
|
24719 | 56 |
* @param returnValue actual return value from the too narrow operation |
57 |
* @param programPoint program point where unwarranted optimism was detected |
|
58 |
*/ |
|
59 |
public UnwarrantedOptimismException(final Object returnValue, final int programPoint) { |
|
60 |
this(returnValue, programPoint, getReturnType(returnValue)); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
34732 | 64 |
* Check if a program point is valid. |
24719 | 65 |
* @param programPoint the program point |
66 |
* @return true if valid |
|
67 |
*/ |
|
68 |
public static boolean isValid(final int programPoint) { |
|
69 |
assert programPoint >= INVALID_PROGRAM_POINT; |
|
70 |
return programPoint != INVALID_PROGRAM_POINT; |
|
71 |
} |
|
72 |
||
73 |
private static Type getReturnType(final Object v) { |
|
74 |
if (v instanceof Double) { |
|
75 |
return Type.NUMBER; |
|
76 |
} |
|
77 |
assert !(v instanceof Integer) : v + " is an int"; // Can't have an unwarranted optimism exception with int |
|
78 |
return Type.OBJECT; |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Constructor with explicit return value type. |
|
83 |
* @param returnValue actual return value from the too narrow operation |
|
84 |
* @param programPoint program point where unwarranted optimism was detected |
|
85 |
* @param returnType type of the returned value. Used to disambiguate the return type. E.g. an {@code ObjectArrayData} |
|
86 |
* might return a {@link Double} for a particular element getter, but still throw this exception even if the call |
|
87 |
* site can accept a double, since the array's type is actually {@code Type#OBJECT}. In this case, it must |
|
88 |
* explicitly use this constructor to indicate its values are to be considered {@code Type#OBJECT} and not |
|
89 |
* {@code Type#NUMBER}. |
|
90 |
*/ |
|
91 |
public UnwarrantedOptimismException(final Object returnValue, final int programPoint, final Type returnType) { |
|
92 |
super("", null, false, Context.DEBUG); |
|
24751 | 93 |
assert returnType != Type.OBJECT || returnValue == null || !Type.typeFor(returnValue.getClass()).isNumeric(); |
94 |
assert returnType != Type.INT; |
|
24719 | 95 |
this.returnValue = returnValue; |
96 |
this.programPoint = programPoint; |
|
97 |
this.returnType = returnType; |
|
98 |
} |
|
99 |
||
100 |
/** |
|
34732 | 101 |
* Create an {@code UnwarrantedOptimismException} with the given return value and program point, narrowing |
102 |
* the type to {@code number} if the value is a float or a long that can be represented as double. |
|
103 |
* |
|
104 |
* @param returnValue the return value |
|
105 |
* @param programPoint the program point |
|
106 |
* @return the exception |
|
107 |
*/ |
|
108 |
public static UnwarrantedOptimismException createNarrowest(final Object returnValue, final int programPoint) { |
|
109 |
if (returnValue instanceof Float |
|
110 |
|| (returnValue instanceof Long && JSType.isRepresentableAsDouble((Long) returnValue))) { |
|
111 |
return new UnwarrantedOptimismException(((Number) returnValue).doubleValue(), programPoint, Type.NUMBER); |
|
112 |
} |
|
113 |
return new UnwarrantedOptimismException(returnValue, programPoint); |
|
114 |
} |
|
115 |
||
116 |
/** |
|
24719 | 117 |
* Get the return value. This is a destructive readout, after the method is invoked the return value is null'd out. |
118 |
* @return return value |
|
119 |
*/ |
|
120 |
public Object getReturnValueDestructive() { |
|
121 |
final Object retval = returnValue; |
|
122 |
returnValue = null; |
|
123 |
return retval; |
|
124 |
} |
|
125 |
||
126 |
Object getReturnValueNonDestructive() { |
|
127 |
return returnValue; |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* Get the return type |
|
132 |
* @return return type |
|
133 |
*/ |
|
134 |
public Type getReturnType() { |
|
135 |
return returnType; |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Does this exception refer to an invalid program point? This might be OK if |
|
140 |
* we throw it, e.g. from a parameter guard |
|
141 |
* @return true if invalid program point specified |
|
142 |
*/ |
|
143 |
public boolean hasInvalidProgramPoint() { |
|
144 |
return programPoint == INVALID_PROGRAM_POINT; |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Get the program point |
|
149 |
* @return the program point |
|
150 |
*/ |
|
151 |
public int getProgramPoint() { |
|
152 |
return programPoint; |
|
153 |
} |
|
154 |
||
155 |
/** |
|
48247
fa5a47cad0c9
8069338: Implement sharedScopeCall for optimistic types
hannesw
parents:
47216
diff
changeset
|
156 |
* Return a new {@code UnwarrantedOptimismException} with the same return value and the |
fa5a47cad0c9
8069338: Implement sharedScopeCall for optimistic types
hannesw
parents:
47216
diff
changeset
|
157 |
* new program point. |
fa5a47cad0c9
8069338: Implement sharedScopeCall for optimistic types
hannesw
parents:
47216
diff
changeset
|
158 |
* |
fa5a47cad0c9
8069338: Implement sharedScopeCall for optimistic types
hannesw
parents:
47216
diff
changeset
|
159 |
* @param newProgramPoint new new program point |
fa5a47cad0c9
8069338: Implement sharedScopeCall for optimistic types
hannesw
parents:
47216
diff
changeset
|
160 |
* @return the new exception instance |
24719 | 161 |
*/ |
48247
fa5a47cad0c9
8069338: Implement sharedScopeCall for optimistic types
hannesw
parents:
47216
diff
changeset
|
162 |
public UnwarrantedOptimismException replaceProgramPoint(final int newProgramPoint) { |
fa5a47cad0c9
8069338: Implement sharedScopeCall for optimistic types
hannesw
parents:
47216
diff
changeset
|
163 |
assert isValid(newProgramPoint); |
fa5a47cad0c9
8069338: Implement sharedScopeCall for optimistic types
hannesw
parents:
47216
diff
changeset
|
164 |
return new UnwarrantedOptimismException(returnValue, newProgramPoint, returnType); |
24719 | 165 |
} |
166 |
||
167 |
@Override |
|
168 |
public String getMessage() { |
|
169 |
return "UNWARRANTED OPTIMISM: [returnValue=" + |
|
170 |
returnValue + |
|
171 |
" (class=" + |
|
172 |
(returnValue == null ? "null" : returnValue.getClass().getSimpleName()) + |
|
173 |
(hasInvalidProgramPoint() ? |
|
174 |
" <invalid program point>" : |
|
175 |
(" @ program point #" + programPoint)) + |
|
176 |
")]"; |
|
177 |
} |
|
178 |
||
24776
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
179 |
|
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
180 |
private void writeObject(final ObjectOutputStream out) throws NotSerializableException { |
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
181 |
throw new NotSerializableException(getClass().getName()); |
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
182 |
} |
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
183 |
|
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
184 |
private void readObject(final ObjectInputStream in) throws NotSerializableException { |
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
185 |
throw new NotSerializableException(getClass().getName()); |
966226aeee50
8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents:
24751
diff
changeset
|
186 |
} |
24719 | 187 |
} |