author | sundar |
Fri, 26 Jul 2013 20:10:47 +0530 | |
changeset 19105 | a01ba9d17eab |
parent 16534 | 6993b3541b28 |
child 23372 | 09707b3e5fb0 |
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 |
||
16277 | 28 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 29 |
|
30 |
import java.lang.invoke.MethodHandle; |
|
31 |
import java.lang.invoke.MethodType; |
|
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16207
diff
changeset
|
32 |
import jdk.internal.dynalink.CallSiteDescriptor; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16207
diff
changeset
|
33 |
import jdk.internal.dynalink.linker.GuardedInvocation; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16207
diff
changeset
|
34 |
import jdk.internal.dynalink.linker.LinkRequest; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16207
diff
changeset
|
35 |
import jdk.internal.dynalink.support.CallSiteDescriptorFactory; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16207
diff
changeset
|
36 |
import jdk.internal.dynalink.support.Guards; |
19105
a01ba9d17eab
8021571: @fork tests should use VM options passed from project.properties
sundar
parents:
16534
diff
changeset
|
37 |
import jdk.nashorn.internal.lookup.Lookup; |
16147 | 38 |
import jdk.nashorn.internal.runtime.ScriptObject; |
39 |
||
40 |
/** |
|
41 |
* Implements lookup of methods to link for dynamic operations on JavaScript primitive values (booleans, strings, and |
|
42 |
* numbers). This class is only public so it can be accessed by classes in the {@code jdk.nashorn.internal.objects} |
|
43 |
* package. |
|
44 |
*/ |
|
19105
a01ba9d17eab
8021571: @fork tests should use VM options passed from project.properties
sundar
parents:
16534
diff
changeset
|
45 |
public final class PrimitiveLookup { |
16147 | 46 |
|
47 |
private PrimitiveLookup() { |
|
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value. |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
52 |
* @param request the link request for the dynamic call site. |
16147 | 53 |
* @param receiverClass the class of the receiver value (e.g., {@link java.lang.Boolean}, {@link java.lang.String} etc.) |
54 |
* @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive |
|
55 |
* value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a |
|
56 |
* primitive base value. This instance will be used to delegate actual lookup. |
|
57 |
* @param wrapFilter A method handle that takes a primitive value of type specified in the {@code receiverClass} and |
|
58 |
* creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the |
|
59 |
* method - it will be combined into the returned invocation as an argument filter on the receiver. |
|
60 |
* @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive |
|
61 |
* type {@code receiverClass}. |
|
62 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
63 |
public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Class<?> receiverClass, |
16147 | 64 |
final ScriptObject wrappedReceiver, final MethodHandle wrapFilter) { |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
65 |
return lookupPrimitive(request, Guards.getInstanceOfGuard(receiverClass), wrappedReceiver, wrapFilter); |
16147 | 66 |
} |
67 |
||
68 |
/** |
|
69 |
* Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value. |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
70 |
* @param request the link request for the dynamic call site. |
16147 | 71 |
* @param guard an explicit guard that will be used for the returned guarded invocation. |
72 |
* @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive |
|
73 |
* value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a |
|
74 |
* primitive base value. This instance will be used to delegate actual lookup. |
|
75 |
* @param wrapFilter A method handle that takes a primitive value of type guarded by the {@code guard} and |
|
76 |
* creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the |
|
77 |
* method - it will be combined into the returned invocation as an argument filter on the receiver. |
|
78 |
* @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive |
|
79 |
* type (that is implied by both {@code guard} and {@code wrappedReceiver}). |
|
80 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
81 |
public static GuardedInvocation lookupPrimitive(final LinkRequest request, final MethodHandle guard, |
16147 | 82 |
final ScriptObject wrappedReceiver, final MethodHandle wrapFilter) { |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
83 |
final CallSiteDescriptor desc = request.getCallSiteDescriptor(); |
16147 | 84 |
final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0); |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
85 |
if ("setProp".equals(operator) || "setElem".equals(operator)) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
86 |
MethodType type = desc.getMethodType(); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
87 |
MethodHandle method = MH.asType(Lookup.EMPTY_SETTER, MH.type(void.class, Object.class, type.parameterType(1))); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
88 |
if (type.parameterCount() == 3) { |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
89 |
method = MH.dropArguments(method, 2, type.parameterType(2)); |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
90 |
} |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
91 |
return new GuardedInvocation(method, guard); |
16147 | 92 |
} |
93 |
||
94 |
if(desc.getNameTokenCount() > 2) { |
|
95 |
final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); |
|
96 |
if(wrappedReceiver.findProperty(name, true) == null) { |
|
97 |
// Give up early, give chance to BeanLinker and NashornBottomLinker to deal with it. |
|
98 |
return null; |
|
99 |
} |
|
100 |
} |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16186
diff
changeset
|
101 |
final GuardedInvocation link = wrappedReceiver.lookup(desc, request); |
16147 | 102 |
if (link != null) { |
103 |
MethodHandle method = link.getInvocation(); |
|
16186
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16151
diff
changeset
|
104 |
final Class<?> receiverType = method.type().parameterType(0); |
16207
ed4aec2d599c
8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents:
16195
diff
changeset
|
105 |
if (receiverType != Object.class) { |
16147 | 106 |
final MethodType wrapType = wrapFilter.type(); |
16186
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16151
diff
changeset
|
107 |
assert receiverType.isAssignableFrom(wrapType.returnType()); |
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16151
diff
changeset
|
108 |
method = MH.filterArguments(method, 0, MH.asType(wrapFilter, wrapType.changeReturnType(receiverType))); |
16147 | 109 |
} |
110 |
return new GuardedInvocation(method, guard, link.getSwitchPoint()); |
|
111 |
} |
|
112 |
return null; |
|
113 |
} |
|
114 |
} |