author | attila |
Wed, 20 Dec 2017 17:36:50 +0100 | |
changeset 48411 | 4ff5c5206427 |
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.linker; |
|
27 |
||
22669 | 28 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 29 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
30 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
31 |
||
32 |
import java.lang.invoke.MethodHandle; |
|
33335
a46c85103868
8139756: Eliminate GuardedTypeConversion, DynamicLinker.getCurrentLinkRequest and its associated permission
attila
parents:
32049
diff
changeset
|
33 |
import java.lang.invoke.MethodHandles; |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
34 |
import java.lang.invoke.MethodType; |
22669 | 35 |
import java.util.HashMap; |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
36 |
import java.util.Map; |
33335
a46c85103868
8139756: Eliminate GuardedTypeConversion, DynamicLinker.getCurrentLinkRequest and its associated permission
attila
parents:
32049
diff
changeset
|
37 |
import java.util.function.Supplier; |
34447
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
38 |
import jdk.dynalink.CallSiteDescriptor; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
39 |
import jdk.dynalink.NamedOperation; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
40 |
import jdk.dynalink.Operation; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
41 |
import jdk.dynalink.beans.BeansLinker; |
48411 | 42 |
import jdk.dynalink.beans.StaticClass; |
34447
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
43 |
import jdk.dynalink.linker.GuardedInvocation; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
44 |
import jdk.dynalink.linker.GuardingDynamicLinker; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
45 |
import jdk.dynalink.linker.GuardingTypeConverterFactory; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
46 |
import jdk.dynalink.linker.LinkRequest; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
47 |
import jdk.dynalink.linker.LinkerServices; |
48411 | 48 |
import jdk.dynalink.linker.support.Guards; |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
49 |
import jdk.dynalink.linker.support.Lookup; |
24719 | 50 |
import jdk.nashorn.internal.codegen.types.Type; |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
51 |
import jdk.nashorn.internal.runtime.ECMAException; |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
52 |
import jdk.nashorn.internal.runtime.JSType; |
16147 | 53 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
24719 | 54 |
import jdk.nashorn.internal.runtime.UnwarrantedOptimismException; |
16147 | 55 |
|
56 |
/** |
|
57 |
* Nashorn bottom linker; used as a last-resort catch-all linker for all linking requests that fall through all other |
|
58 |
* linkers (see how {@link Bootstrap} class configures the dynamic linker in its static initializer). It will throw |
|
59 |
* appropriate ECMAScript errors for attempts to invoke operations on {@code null}, link no-op property getters and |
|
60 |
* setters for Java objects that couldn't be linked by any other linker, and throw appropriate ECMAScript errors for |
|
61 |
* attempts to invoke arbitrary Java objects as functions or constructors. |
|
62 |
*/ |
|
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
63 |
final class NashornBottomLinker implements GuardingDynamicLinker, GuardingTypeConverterFactory { |
16147 | 64 |
|
65 |
@Override |
|
66 |
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) |
|
67 |
throws Exception { |
|
68 |
final Object self = linkRequest.getReceiver(); |
|
69 |
||
70 |
if (self == null) { |
|
71 |
return linkNull(linkRequest); |
|
72 |
} |
|
73 |
||
74 |
// None of the objects that can be linked by NashornLinker should ever reach here. Basically, anything below |
|
75 |
// this point is a generic Java bean. Therefore, reaching here with a ScriptObject is a Nashorn bug. |
|
76 |
assert isExpectedObject(self) : "Couldn't link " + linkRequest.getCallSiteDescriptor() + " for " + self.getClass().getName(); |
|
77 |
||
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
78 |
return linkBean(linkRequest); |
16147 | 79 |
} |
80 |
||
81 |
private static final MethodHandle EMPTY_PROP_GETTER = |
|
82 |
MH.dropArguments(MH.constant(Object.class, UNDEFINED), 0, Object.class); |
|
83 |
private static final MethodHandle EMPTY_ELEM_GETTER = |
|
84 |
MH.dropArguments(EMPTY_PROP_GETTER, 0, Object.class); |
|
85 |
private static final MethodHandle EMPTY_PROP_SETTER = |
|
86 |
MH.asType(EMPTY_ELEM_GETTER, EMPTY_ELEM_GETTER.type().changeReturnType(void.class)); |
|
87 |
private static final MethodHandle EMPTY_ELEM_SETTER = |
|
88 |
MH.dropArguments(EMPTY_PROP_SETTER, 0, Object.class); |
|
89 |
||
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
90 |
private static final MethodHandle THROW_STRICT_PROPERTY_SETTER; |
48411 | 91 |
private static final MethodHandle THROW_STRICT_PROPERTY_REMOVER; |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
92 |
private static final MethodHandle THROW_OPTIMISTIC_UNDEFINED; |
48411 | 93 |
private static final MethodHandle MISSING_PROPERTY_REMOVER; |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
94 |
|
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
95 |
static { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
96 |
final Lookup lookup = new Lookup(MethodHandles.lookup()); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
97 |
THROW_STRICT_PROPERTY_SETTER = lookup.findOwnStatic("throwStrictPropertySetter", void.class, Object.class, Object.class); |
48411 | 98 |
THROW_STRICT_PROPERTY_REMOVER = lookup.findOwnStatic("throwStrictPropertyRemover", boolean.class, Object.class, Object.class); |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
99 |
THROW_OPTIMISTIC_UNDEFINED = lookup.findOwnStatic("throwOptimisticUndefined", Object.class, int.class); |
48411 | 100 |
MISSING_PROPERTY_REMOVER = lookup.findOwnStatic("missingPropertyRemover", boolean.class, Object.class, Object.class); |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
101 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
102 |
|
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
103 |
private static GuardedInvocation linkBean(final LinkRequest linkRequest) throws Exception { |
34455
cc9f05d3caf0
8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
34447
diff
changeset
|
104 |
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor(); |
16147 | 105 |
final Object self = linkRequest.getReceiver(); |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
106 |
switch (NashornCallSiteDescriptor.getStandardOperation(desc)) { |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
107 |
case NEW: |
30394
72a59e4dffea
8079470: Misleading error message when explicit signature constructor is called with wrong arguments
sundar
parents:
28437
diff
changeset
|
108 |
if(BeansLinker.isDynamicConstructor(self)) { |
72a59e4dffea
8079470: Misleading error message when explicit signature constructor is called with wrong arguments
sundar
parents:
28437
diff
changeset
|
109 |
throw typeError("no.constructor.matches.args", ScriptRuntime.safeToString(self)); |
72a59e4dffea
8079470: Misleading error message when explicit signature constructor is called with wrong arguments
sundar
parents:
28437
diff
changeset
|
110 |
} |
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
111 |
if(BeansLinker.isDynamicMethod(self)) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
112 |
throw typeError("method.not.constructor", ScriptRuntime.safeToString(self)); |
16147 | 113 |
} |
34455
cc9f05d3caf0
8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
34447
diff
changeset
|
114 |
throw typeError("not.a.function", NashornCallSiteDescriptor.getFunctionErrorMessage(desc, self)); |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
115 |
case CALL: |
25252
e8bfc909db53
8043232: Index selection of overloaded java new constructors
sundar
parents:
24778
diff
changeset
|
116 |
if(BeansLinker.isDynamicConstructor(self)) { |
e8bfc909db53
8043232: Index selection of overloaded java new constructors
sundar
parents:
24778
diff
changeset
|
117 |
throw typeError("constructor.requires.new", ScriptRuntime.safeToString(self)); |
e8bfc909db53
8043232: Index selection of overloaded java new constructors
sundar
parents:
24778
diff
changeset
|
118 |
} |
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
119 |
if(BeansLinker.isDynamicMethod(self)) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
120 |
throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self)); |
16147 | 121 |
} |
34455
cc9f05d3caf0
8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
34447
diff
changeset
|
122 |
throw typeError("not.a.function", NashornCallSiteDescriptor.getFunctionErrorMessage(desc, self)); |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
123 |
default: |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
124 |
// Everything else is supposed to have been already handled by Bootstrap.beansLinker |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
125 |
// delegating to linkNoSuchBeanMember |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
126 |
throw new AssertionError("unknown call type " + desc); |
26649
1d7a917a35e2
8058545: With strict mode, bean property assignment of a non-existent property should result in TypeError
sundar
parents:
25865
diff
changeset
|
127 |
} |
16147 | 128 |
} |
129 |
||
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
130 |
static MethodHandle linkMissingBeanMember(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
131 |
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor(); |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
132 |
final String operand = NashornCallSiteDescriptor.getOperand(desc); |
48411 | 133 |
final boolean strict = NashornCallSiteDescriptor.isStrict(desc); |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
134 |
switch (NashornCallSiteDescriptor.getStandardOperation(desc)) { |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
135 |
case GET: |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
136 |
if (NashornCallSiteDescriptor.isOptimistic(desc)) { |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
137 |
return adaptThrower(MethodHandles.insertArguments(THROW_OPTIMISTIC_UNDEFINED, 0, NashornCallSiteDescriptor.getProgramPoint(desc)), desc); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
138 |
} else if (operand != null) { |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
139 |
return getInvocation(EMPTY_PROP_GETTER, linkerServices, desc); |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
140 |
} |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
141 |
return getInvocation(EMPTY_ELEM_GETTER, linkerServices, desc); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
142 |
case SET: |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
143 |
if (strict) { |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
144 |
return adaptThrower(bindOperand(THROW_STRICT_PROPERTY_SETTER, operand), desc); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
145 |
} else if (operand != null) { |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
146 |
return getInvocation(EMPTY_PROP_SETTER, linkerServices, desc); |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
147 |
} |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
148 |
return getInvocation(EMPTY_ELEM_SETTER, linkerServices, desc); |
48411 | 149 |
case REMOVE: |
150 |
if (strict) { |
|
151 |
return adaptThrower(bindOperand(THROW_STRICT_PROPERTY_REMOVER, operand), desc); |
|
152 |
} |
|
153 |
return getInvocation(bindOperand(MISSING_PROPERTY_REMOVER, operand), linkerServices, desc); |
|
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
154 |
default: |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
155 |
throw new AssertionError("unknown call type " + desc); |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
156 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
157 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
158 |
|
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
159 |
private static MethodHandle bindOperand(final MethodHandle handle, final String operand) { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
160 |
return operand == null ? handle : MethodHandles.insertArguments(handle, 1, operand); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
161 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
162 |
|
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
163 |
private static MethodHandle adaptThrower(final MethodHandle handle, final CallSiteDescriptor desc) { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
164 |
final MethodType targetType = desc.getMethodType(); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
165 |
final int paramCount = handle.type().parameterCount(); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
166 |
return MethodHandles |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
167 |
.dropArguments(handle, paramCount, targetType.parameterList().subList(paramCount, targetType.parameterCount())) |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
168 |
.asType(targetType); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
169 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
170 |
|
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
171 |
@SuppressWarnings("unused") |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
172 |
private static void throwStrictPropertySetter(final Object self, final Object name) { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
173 |
throw createTypeError(self, name, "cant.set.property"); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
174 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
175 |
|
48411 | 176 |
@SuppressWarnings("unused") |
177 |
private static boolean throwStrictPropertyRemover(final Object self, final Object name) { |
|
178 |
if (isNonConfigurableProperty(self, name)) { |
|
179 |
throw createTypeError(self, name, "cant.delete.property"); |
|
180 |
} |
|
181 |
return true; |
|
182 |
} |
|
183 |
||
184 |
@SuppressWarnings("unused") |
|
185 |
private static boolean missingPropertyRemover(final Object self, final Object name) { |
|
186 |
return !isNonConfigurableProperty(self, name); |
|
187 |
} |
|
188 |
||
189 |
// Corresponds to ECMAScript 5.1 8.12.7 [[Delete]] point 3 check for "isConfigurable" (but negated) |
|
190 |
private static boolean isNonConfigurableProperty(final Object self, final Object name) { |
|
191 |
if (self instanceof StaticClass) { |
|
192 |
final Class<?> clazz = ((StaticClass)self).getRepresentedClass(); |
|
193 |
return BeansLinker.getReadableStaticPropertyNames(clazz).contains(name) || |
|
194 |
BeansLinker.getWritableStaticPropertyNames(clazz).contains(name) || |
|
195 |
BeansLinker.getStaticMethodNames(clazz).contains(name); |
|
196 |
} |
|
197 |
final Class<?> clazz = self.getClass(); |
|
198 |
return BeansLinker.getReadableInstancePropertyNames(clazz).contains(name) || |
|
199 |
BeansLinker.getWritableInstancePropertyNames(clazz).contains(name) || |
|
200 |
BeansLinker.getInstanceMethodNames(clazz).contains(name); |
|
201 |
} |
|
202 |
||
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
203 |
private static ECMAException createTypeError(final Object self, final Object name, final String msg) { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
204 |
return typeError(msg, String.valueOf(name), ScriptRuntime.safeToString(self)); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
205 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
206 |
|
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
207 |
@SuppressWarnings("unused") |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
208 |
private static Object throwOptimisticUndefined(final int programPoint) { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
209 |
throw new UnwarrantedOptimismException(UNDEFINED, programPoint, Type.OBJECT); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
210 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
211 |
|
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
212 |
@Override |
33335
a46c85103868
8139756: Eliminate GuardedTypeConversion, DynamicLinker.getCurrentLinkRequest and its associated permission
attila
parents:
32049
diff
changeset
|
213 |
public GuardedInvocation convertToType(final Class<?> sourceType, final Class<?> targetType, final Supplier<MethodHandles.Lookup> lookupSupplier) throws Exception { |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
214 |
final GuardedInvocation gi = convertToTypeNoCast(sourceType, targetType); |
33335
a46c85103868
8139756: Eliminate GuardedTypeConversion, DynamicLinker.getCurrentLinkRequest and its associated permission
attila
parents:
32049
diff
changeset
|
215 |
return gi == null ? null : gi.asType(MH.type(targetType, sourceType)); |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
216 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
217 |
|
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
218 |
/** |
34732 | 219 |
* Main part of the implementation of {@link GuardingTypeConverterFactory#convertToType} that doesn't |
220 |
* care about adapting the method signature; that's done by the invoking method. Returns conversion |
|
221 |
* from Object to String/number/boolean (JS primitive types). |
|
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
222 |
* @param sourceType the source type |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
223 |
* @param targetType the target type |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
224 |
* @return a guarded invocation that converts from the source type to the target type. |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
225 |
* @throws Exception if something goes wrong |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
226 |
*/ |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
227 |
private static GuardedInvocation convertToTypeNoCast(final Class<?> sourceType, final Class<?> targetType) throws Exception { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
228 |
final MethodHandle mh = CONVERTERS.get(targetType); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
229 |
if (mh != null) { |
24719 | 230 |
return new GuardedInvocation(mh); |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
231 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
232 |
|
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
233 |
return null; |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
234 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
235 |
|
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
236 |
private static MethodHandle getInvocation(final MethodHandle handle, final LinkerServices linkerServices, final CallSiteDescriptor desc) { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
237 |
return linkerServices.asTypeLosslessReturn(handle, desc.getMethodType()); |
16147 | 238 |
} |
239 |
||
240 |
// Used solely in an assertion to figure out if the object we get here is something we in fact expect. Objects |
|
241 |
// linked by NashornLinker should never reach here. |
|
242 |
private static boolean isExpectedObject(final Object obj) { |
|
243 |
return !(NashornLinker.canLinkTypeStatic(obj.getClass())); |
|
244 |
} |
|
245 |
||
246 |
private static GuardedInvocation linkNull(final LinkRequest linkRequest) { |
|
34455
cc9f05d3caf0
8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
34447
diff
changeset
|
247 |
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor(); |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
248 |
switch (NashornCallSiteDescriptor.getStandardOperation(desc)) { |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
249 |
case NEW: |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
250 |
case CALL: |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
251 |
throw typeError("not.a.function", "null"); |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
252 |
case GET: |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
253 |
throw typeError(NashornCallSiteDescriptor.isMethodFirstOperation(desc) ? "no.such.function" : "cant.get.property", getArgument(linkRequest), "null"); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
254 |
case SET: |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
255 |
throw typeError("cant.set.property", getArgument(linkRequest), "null"); |
48411 | 256 |
case REMOVE: |
257 |
throw typeError("cant.delete.property", getArgument(linkRequest), "null"); |
|
16147 | 258 |
default: |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
259 |
throw new AssertionError("unknown call type " + desc); |
16147 | 260 |
} |
261 |
} |
|
262 |
||
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
263 |
private static final Map<Class<?>, MethodHandle> CONVERTERS = new HashMap<>(); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
264 |
static { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
265 |
CONVERTERS.put(boolean.class, JSType.TO_BOOLEAN.methodHandle()); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
266 |
CONVERTERS.put(double.class, JSType.TO_NUMBER.methodHandle()); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
267 |
CONVERTERS.put(int.class, JSType.TO_INTEGER.methodHandle()); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
268 |
CONVERTERS.put(long.class, JSType.TO_LONG.methodHandle()); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
269 |
CONVERTERS.put(String.class, JSType.TO_STRING.methodHandle()); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
270 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
271 |
|
16147 | 272 |
private static String getArgument(final LinkRequest linkRequest) { |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
273 |
final Operation op = linkRequest.getCallSiteDescriptor().getOperation(); |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
274 |
if (op instanceof NamedOperation) { |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
275 |
return ((NamedOperation)op).getName().toString(); |
16147 | 276 |
} |
277 |
return ScriptRuntime.safeToString(linkRequest.getArguments()[1]); |
|
278 |
} |
|
279 |
} |