|
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 |
|
26 package jdk.nashorn.internal.runtime.linker; |
|
27 |
|
28 import java.lang.invoke.MethodHandle; |
|
29 import java.lang.invoke.MethodHandles; |
|
30 import java.lang.invoke.MethodType; |
|
31 import jdk.internal.dynalink.beans.BeansLinker; |
|
32 import jdk.internal.dynalink.linker.ConversionComparator.Comparison; |
|
33 import jdk.internal.dynalink.linker.GuardedInvocation; |
|
34 import jdk.internal.dynalink.linker.GuardingDynamicLinker; |
|
35 import jdk.internal.dynalink.linker.LinkRequest; |
|
36 import jdk.internal.dynalink.linker.LinkerServices; |
|
37 import jdk.internal.dynalink.support.Lookup; |
|
38 import jdk.nashorn.internal.runtime.ConsString; |
|
39 |
|
40 /** |
|
41 * This linker delegates to a {@code BeansLinker} but passes it a special linker services object that has a modified |
|
42 * {@code asType} method that will ensure that we never pass internal engine objects that should not be externally |
|
43 * observable (currently only ConsString) to Java APIs, but rather that we flatten it into a String. We can't just add |
|
44 * this functionality as custom converters via {@code GuaardingTypeConverterFactory}, since they are not consulted when |
|
45 * the target method handle parameter signature is {@code Object}. |
|
46 */ |
|
47 public class NashornBeansLinker implements GuardingDynamicLinker { |
|
48 private static final MethodHandle EXPORT_ARGUMENT = new Lookup(MethodHandles.lookup()).findOwnStatic("exportArgument", Object.class, Object.class); |
|
49 |
|
50 private final BeansLinker beansLinker = new BeansLinker(); |
|
51 |
|
52 @Override |
|
53 public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception { |
|
54 return getGuardedInvocation(beansLinker, linkRequest, linkerServices); |
|
55 } |
|
56 |
|
57 /** |
|
58 * Delegates to the specified linker but injects its linker services wrapper so that it will apply all special |
|
59 * conversions that this class does. |
|
60 * @param delegateLinker the linker to which the actual work is delegated to. |
|
61 * @param linkRequest the delegated link request |
|
62 * @param linkerServices the original link services that will be augmented with special conversions |
|
63 * @return the guarded invocation from the delegate, possibly augmented with special conversions |
|
64 * @throws Exception if the delegate throws an exception |
|
65 */ |
|
66 public static GuardedInvocation getGuardedInvocation(final GuardingDynamicLinker delegateLinker, final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception { |
|
67 return delegateLinker.getGuardedInvocation(linkRequest, new NashornBeansLinkerServices(linkerServices)); |
|
68 } |
|
69 |
|
70 static Object exportArgument(final Object arg) { |
|
71 return arg instanceof ConsString ? arg.toString() : arg; |
|
72 } |
|
73 |
|
74 private static class NashornBeansLinkerServices implements LinkerServices { |
|
75 private final LinkerServices linkerServices; |
|
76 |
|
77 NashornBeansLinkerServices(final LinkerServices linkerServices) { |
|
78 this.linkerServices = linkerServices; |
|
79 } |
|
80 |
|
81 @Override |
|
82 public MethodHandle asType(final MethodHandle handle, final MethodType fromType) { |
|
83 final MethodHandle typed = linkerServices.asType(handle, fromType); |
|
84 |
|
85 final MethodType handleType = handle.type(); |
|
86 final int paramCount = handleType.parameterCount(); |
|
87 assert fromType.parameterCount() == handleType.parameterCount(); |
|
88 |
|
89 MethodHandle[] filters = null; |
|
90 for(int i = 0; i < paramCount; ++i) { |
|
91 if(shouldConvert(handleType.parameterType(i), fromType.parameterType(i))) { |
|
92 if(filters == null) { |
|
93 filters = new MethodHandle[paramCount]; |
|
94 } |
|
95 filters[i] = EXPORT_ARGUMENT; |
|
96 } |
|
97 } |
|
98 |
|
99 return filters != null ? MethodHandles.filterArguments(typed, 0, filters) : typed; |
|
100 } |
|
101 |
|
102 @Override |
|
103 public MethodHandle asTypeLosslessReturn(final MethodHandle handle, final MethodType fromType) { |
|
104 return Implementation.asTypeLosslessReturn(this, handle, fromType); |
|
105 } |
|
106 |
|
107 private static boolean shouldConvert(final Class<?> handleType, final Class<?> fromType) { |
|
108 return handleType == Object.class && fromType == Object.class; |
|
109 } |
|
110 |
|
111 @Override |
|
112 public MethodHandle getTypeConverter(final Class<?> sourceType, final Class<?> targetType) { |
|
113 return linkerServices.getTypeConverter(sourceType, targetType); |
|
114 } |
|
115 |
|
116 @Override |
|
117 public boolean canConvert(final Class<?> from, final Class<?> to) { |
|
118 return linkerServices.canConvert(from, to); |
|
119 } |
|
120 |
|
121 @Override |
|
122 public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest) throws Exception { |
|
123 return linkerServices.getGuardedInvocation(linkRequest); |
|
124 } |
|
125 |
|
126 @Override |
|
127 public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) { |
|
128 return linkerServices.compareConversion(sourceType, targetType1, targetType2); |
|
129 } |
|
130 } |
|
131 } |