7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 /*
26 * @test
27 * @enablePreview
28 * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAdaptVarHandles
29 * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
30 * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAdaptVarHandles
31 * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
32 */
33
34 import java.lang.foreign.*;
35
36 import org.testng.annotations.*;
37 import static org.testng.Assert.*;
38
39 import java.lang.invoke.MethodHandle;
40 import java.lang.invoke.MethodHandles;
41 import java.lang.invoke.MethodType;
42 import java.lang.invoke.VarHandle;
43 import java.util.List;
44
45 public class TestAdaptVarHandles {
46
47 static MethodHandle S2I;
66 O2I = MethodHandles.explicitCastArguments(S2I, MethodType.methodType(int.class, Object.class));
67 I2O = MethodHandles.explicitCastArguments(I2S, MethodType.methodType(Object.class, int.class));
68 S2L = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToLong", MethodType.methodType(long.class, String.class));
69 S2L_EX = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToLongException", MethodType.methodType(long.class, String.class));
70 BASE_ADDR = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "baseAddress", MethodType.methodType(MemorySegment.class, MemorySegment.class));
71 SUM_OFFSETS = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "sumOffsets", MethodType.methodType(long.class, long.class, long.class));
72 VOID_FILTER = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "void_filter", MethodType.methodType(void.class, String.class));
73
74 MethodHandle s2i_ex = MethodHandles.throwException(int.class, Throwable.class);
75 s2i_ex = MethodHandles.insertArguments(s2i_ex, 0, new Throwable());
76 S2I_EX = MethodHandles.dropArguments(s2i_ex, 0, String.class);
77
78 MethodHandle i2s_ex = MethodHandles.throwException(String.class, Throwable.class);
79 i2s_ex = MethodHandles.insertArguments(i2s_ex, 0, new Throwable());
80 I2S_EX = MethodHandles.dropArguments(i2s_ex, 0, int.class);
81 } catch (Throwable ex) {
82 throw new ExceptionInInitializerError();
83 }
84 }
85
86 static final VarHandle intHandleIndexed = ValueLayout.JAVA_INT.arrayElementVarHandle();
87
88 static final VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
89
90 static final VarHandle floatHandle = ValueLayout.JAVA_FLOAT.varHandle();
91
92 @Test
93 public void testFilterValue() throws Throwable {
94 ValueLayout layout = ValueLayout.JAVA_INT;
95 Arena scope = Arena.ofAuto();
96 MemorySegment segment = scope.allocate(layout);
97 VarHandle intHandle = layout.varHandle();
98 VarHandle i2SHandle = MethodHandles.filterValue(intHandle, S2I, I2S);
99 i2SHandle.set(segment, "1");
100 String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
101 assertEquals(oldValue, "1");
102 String value = (String)i2SHandle.get(segment);
103 assertEquals(value, "43");
104 boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
105 assertTrue(swapped);
106 oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
107 assertEquals(oldValue, "12");
108 value = (String)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
109 assertEquals(value, "42");
110 }
111
112 @Test
113 public void testFilterValueComposite() throws Throwable {
114 ValueLayout layout = ValueLayout.JAVA_INT;
115 Arena scope = Arena.ofAuto();
116 MemorySegment segment = scope.allocate(layout);
117 VarHandle intHandle = layout.varHandle();
118 MethodHandle CTX_S2I = MethodHandles.dropArguments(S2I, 0, String.class, String.class);
119 VarHandle i2SHandle = MethodHandles.filterValue(intHandle, CTX_S2I, CTX_I2S);
120 i2SHandle = MethodHandles.insertCoordinates(i2SHandle, 1, "a", "b");
121 i2SHandle.set(segment, "1");
122 String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
123 assertEquals(oldValue, "ab1");
124 String value = (String)i2SHandle.get(segment);
125 assertEquals(value, "ab43");
126 boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
127 assertTrue(swapped);
128 oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
129 assertEquals(oldValue, "ab12");
130 value = (String)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
131 assertEquals(value, "ab42");
132 }
133
134 @Test
135 public void testFilterValueLoose() throws Throwable {
136 ValueLayout layout = ValueLayout.JAVA_INT;
137 Arena scope = Arena.ofAuto();
138 MemorySegment segment = scope.allocate(layout);
139 VarHandle intHandle = layout.varHandle();
140 VarHandle i2SHandle = MethodHandles.filterValue(intHandle, O2I, I2O);
141 i2SHandle.set(segment, "1");
142 String oldValue = (String)i2SHandle.getAndAdd(segment, "42");
143 assertEquals(oldValue, "1");
144 String value = (String)i2SHandle.get(segment);
145 assertEquals(value, "43");
146 boolean swapped = (boolean)i2SHandle.compareAndSet(segment, "43", "12");
147 assertTrue(swapped);
148 oldValue = (String)i2SHandle.compareAndExchange(segment, "12", "42");
149 assertEquals(oldValue, "12");
150 value = (String)(Object)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment);
151 assertEquals(value, "42");
152 }
153
154 @Test(expectedExceptions = IllegalArgumentException.class)
155 public void testBadFilterCarrier() {
156 MethodHandles.filterValue(floatHandle, S2I, I2S);
157 }
158
159 @Test(expectedExceptions = IllegalArgumentException.class)
160 public void testBadFilterUnboxArity() {
161 VarHandle floatHandle = ValueLayout.JAVA_INT.varHandle();
162 MethodHandles.filterValue(floatHandle, S2I.bindTo(""), I2S);
163 }
164
165 @Test(expectedExceptions = IllegalArgumentException.class)
166 public void testBadFilterBoxArity() {
167 VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
168 MethodHandles.filterValue(intHandle, S2I, I2S.bindTo(42));
169 }
170
177 }
178
179 @Test(expectedExceptions = IllegalArgumentException.class)
180 public void testBadFilterBoxException() {
181 VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
182 MethodHandles.filterValue(intHandle, I2S, S2L_EX);
183 }
184
185 @Test(expectedExceptions = IllegalArgumentException.class)
186 public void testBadFilterUnboxException() {
187 VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
188 MethodHandles.filterValue(intHandle, S2L_EX, I2S);
189 }
190
191 @Test(expectedExceptions = IllegalStateException.class)
192 public void testBadFilterBoxHandleException() {
193 VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
194 VarHandle vh = MethodHandles.filterValue(intHandle, S2I, I2S_EX);
195 try (Arena arena = Arena.ofConfined()) {
196 MemorySegment seg = arena.allocate(ValueLayout.JAVA_INT);
197 vh.set(seg, "42");
198 String x = (String) vh.get(seg); // should throw
199 }
200 }
201
202 @Test(expectedExceptions = IllegalStateException.class)
203 public void testBadFilterUnboxHandleException() {
204 VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
205 VarHandle vh = MethodHandles.filterValue(intHandle, S2I_EX, I2S);
206 try (Arena arena = Arena.ofConfined()) {
207 MemorySegment seg = arena.allocate(ValueLayout.JAVA_INT);
208 vh.set(seg, "42"); // should throw
209 }
210 }
211
212 @Test
213 public void testFilterCoordinates() throws Throwable {
214 ValueLayout layout = ValueLayout.JAVA_INT;
215 Arena scope = Arena.ofAuto();
216 MemorySegment segment = scope.allocate(layout);
217 VarHandle intHandle_longIndex = MethodHandles.filterCoordinates(intHandleIndexed, 0, BASE_ADDR, S2L);
218 intHandle_longIndex.set(segment, "0", 1);
219 int oldValue = (int)intHandle_longIndex.getAndAdd(segment, "0", 42);
220 assertEquals(oldValue, 1);
221 int value = (int)intHandle_longIndex.get(segment, "0");
222 assertEquals(value, 43);
223 boolean swapped = (boolean)intHandle_longIndex.compareAndSet(segment, "0", 43, 12);
224 assertTrue(swapped);
225 oldValue = (int)intHandle_longIndex.compareAndExchange(segment, "0", 12, 42);
226 assertEquals(oldValue, 12);
227 value = (int)intHandle_longIndex.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, "0");
228 assertEquals(value, 42);
|
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 /*
26 * @test
27 * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAdaptVarHandles
28 * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
29 * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAdaptVarHandles
30 * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAdaptVarHandles
31 */
32
33 import java.lang.foreign.*;
34
35 import org.testng.annotations.*;
36 import static org.testng.Assert.*;
37
38 import java.lang.invoke.MethodHandle;
39 import java.lang.invoke.MethodHandles;
40 import java.lang.invoke.MethodType;
41 import java.lang.invoke.VarHandle;
42 import java.util.List;
43
44 public class TestAdaptVarHandles {
45
46 static MethodHandle S2I;
65 O2I = MethodHandles.explicitCastArguments(S2I, MethodType.methodType(int.class, Object.class));
66 I2O = MethodHandles.explicitCastArguments(I2S, MethodType.methodType(Object.class, int.class));
67 S2L = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToLong", MethodType.methodType(long.class, String.class));
68 S2L_EX = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "stringToLongException", MethodType.methodType(long.class, String.class));
69 BASE_ADDR = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "baseAddress", MethodType.methodType(MemorySegment.class, MemorySegment.class));
70 SUM_OFFSETS = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "sumOffsets", MethodType.methodType(long.class, long.class, long.class));
71 VOID_FILTER = MethodHandles.lookup().findStatic(TestAdaptVarHandles.class, "void_filter", MethodType.methodType(void.class, String.class));
72
73 MethodHandle s2i_ex = MethodHandles.throwException(int.class, Throwable.class);
74 s2i_ex = MethodHandles.insertArguments(s2i_ex, 0, new Throwable());
75 S2I_EX = MethodHandles.dropArguments(s2i_ex, 0, String.class);
76
77 MethodHandle i2s_ex = MethodHandles.throwException(String.class, Throwable.class);
78 i2s_ex = MethodHandles.insertArguments(i2s_ex, 0, new Throwable());
79 I2S_EX = MethodHandles.dropArguments(i2s_ex, 0, int.class);
80 } catch (Throwable ex) {
81 throw new ExceptionInInitializerError();
82 }
83 }
84
85 static final VarHandle intHandleIndexed = MethodHandles.collectCoordinates(ValueLayout.JAVA_INT.varHandle(),
86 1, MethodHandles.insertArguments(ValueLayout.JAVA_INT.scaleHandle(), 0, 0L));
87
88 static final VarHandle intHandle = MethodHandles.insertCoordinates(ValueLayout.JAVA_INT.varHandle(), 1, 0L);
89
90 static final VarHandle floatHandle = MethodHandles.insertCoordinates(ValueLayout.JAVA_FLOAT.varHandle(), 1, 0L);
91
92 @Test
93 public void testFilterValue() throws Throwable {
94 ValueLayout layout = ValueLayout.JAVA_INT;
95 Arena scope = Arena.ofAuto();
96 MemorySegment segment = scope.allocate(layout);
97 VarHandle intHandle = layout.varHandle();
98 VarHandle i2SHandle = MethodHandles.filterValue(intHandle, S2I, I2S);
99 i2SHandle.set(segment, 0L, "1");
100 String oldValue = (String)i2SHandle.getAndAdd(segment, 0L, "42");
101 assertEquals(oldValue, "1");
102 String value = (String)i2SHandle.get(segment, 0L);
103 assertEquals(value, "43");
104 boolean swapped = (boolean)i2SHandle.compareAndSet(segment, 0L, "43", "12");
105 assertTrue(swapped);
106 oldValue = (String)i2SHandle.compareAndExchange(segment, 0L, "12", "42");
107 assertEquals(oldValue, "12");
108 value = (String)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, 0L);
109 assertEquals(value, "42");
110 }
111
112 @Test
113 public void testFilterValueComposite() throws Throwable {
114 ValueLayout layout = ValueLayout.JAVA_INT;
115 Arena scope = Arena.ofAuto();
116 MemorySegment segment = scope.allocate(layout);
117 VarHandle intHandle = layout.varHandle();
118 MethodHandle CTX_S2I = MethodHandles.dropArguments(S2I, 0, String.class, String.class);
119 VarHandle i2SHandle = MethodHandles.filterValue(intHandle, CTX_S2I, CTX_I2S);
120 i2SHandle = MethodHandles.insertCoordinates(i2SHandle, 2, "a", "b");
121 i2SHandle.set(segment, 0L, "1");
122 String oldValue = (String)i2SHandle.getAndAdd(segment, 0L, "42");
123 assertEquals(oldValue, "ab1");
124 String value = (String)i2SHandle.get(segment, 0L);
125 assertEquals(value, "ab43");
126 boolean swapped = (boolean)i2SHandle.compareAndSet(segment, 0L, "43", "12");
127 assertTrue(swapped);
128 oldValue = (String)i2SHandle.compareAndExchange(segment, 0L, "12", "42");
129 assertEquals(oldValue, "ab12");
130 value = (String)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, 0L);
131 assertEquals(value, "ab42");
132 }
133
134 @Test
135 public void testFilterValueLoose() throws Throwable {
136 ValueLayout layout = ValueLayout.JAVA_INT;
137 Arena scope = Arena.ofAuto();
138 MemorySegment segment = scope.allocate(layout);
139 VarHandle intHandle = layout.varHandle();
140 VarHandle i2SHandle = MethodHandles.filterValue(intHandle, O2I, I2O);
141 i2SHandle.set(segment, 0L, "1");
142 String oldValue = (String)i2SHandle.getAndAdd(segment, 0L, "42");
143 assertEquals(oldValue, "1");
144 String value = (String)i2SHandle.get(segment, 0L);
145 assertEquals(value, "43");
146 boolean swapped = (boolean)i2SHandle.compareAndSet(segment, 0L, "43", "12");
147 assertTrue(swapped);
148 oldValue = (String)i2SHandle.compareAndExchange(segment, 0L, "12", "42");
149 assertEquals(oldValue, "12");
150 value = (String)(Object)i2SHandle.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, 0L);
151 assertEquals(value, "42");
152 }
153
154 @Test(expectedExceptions = IllegalArgumentException.class)
155 public void testBadFilterCarrier() {
156 MethodHandles.filterValue(floatHandle, S2I, I2S);
157 }
158
159 @Test(expectedExceptions = IllegalArgumentException.class)
160 public void testBadFilterUnboxArity() {
161 VarHandle floatHandle = ValueLayout.JAVA_INT.varHandle();
162 MethodHandles.filterValue(floatHandle, S2I.bindTo(""), I2S);
163 }
164
165 @Test(expectedExceptions = IllegalArgumentException.class)
166 public void testBadFilterBoxArity() {
167 VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
168 MethodHandles.filterValue(intHandle, S2I, I2S.bindTo(42));
169 }
170
177 }
178
179 @Test(expectedExceptions = IllegalArgumentException.class)
180 public void testBadFilterBoxException() {
181 VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
182 MethodHandles.filterValue(intHandle, I2S, S2L_EX);
183 }
184
185 @Test(expectedExceptions = IllegalArgumentException.class)
186 public void testBadFilterUnboxException() {
187 VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
188 MethodHandles.filterValue(intHandle, S2L_EX, I2S);
189 }
190
191 @Test(expectedExceptions = IllegalStateException.class)
192 public void testBadFilterBoxHandleException() {
193 VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
194 VarHandle vh = MethodHandles.filterValue(intHandle, S2I, I2S_EX);
195 try (Arena arena = Arena.ofConfined()) {
196 MemorySegment seg = arena.allocate(ValueLayout.JAVA_INT);
197 vh.set(seg, 0L, "42");
198 String x = (String) vh.get(seg, 0L); // should throw
199 }
200 }
201
202 @Test(expectedExceptions = IllegalStateException.class)
203 public void testBadFilterUnboxHandleException() {
204 VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();
205 VarHandle vh = MethodHandles.filterValue(intHandle, S2I_EX, I2S);
206 try (Arena arena = Arena.ofConfined()) {
207 MemorySegment seg = arena.allocate(ValueLayout.JAVA_INT);
208 vh.set(seg, 0L, "42"); // should throw
209 }
210 }
211
212 @Test
213 public void testFilterCoordinates() throws Throwable {
214 ValueLayout layout = ValueLayout.JAVA_INT;
215 Arena scope = Arena.ofAuto();
216 MemorySegment segment = scope.allocate(layout);
217 VarHandle intHandle_longIndex = MethodHandles.filterCoordinates(intHandleIndexed, 0, BASE_ADDR, S2L);
218 intHandle_longIndex.set(segment, "0", 1);
219 int oldValue = (int)intHandle_longIndex.getAndAdd(segment, "0", 42);
220 assertEquals(oldValue, 1);
221 int value = (int)intHandle_longIndex.get(segment, "0");
222 assertEquals(value, 43);
223 boolean swapped = (boolean)intHandle_longIndex.compareAndSet(segment, "0", 43, 12);
224 assertTrue(swapped);
225 oldValue = (int)intHandle_longIndex.compareAndExchange(segment, "0", 12, 42);
226 assertEquals(oldValue, 12);
227 value = (int)intHandle_longIndex.toMethodHandle(VarHandle.AccessMode.GET).invokeExact(segment, "0");
228 assertEquals(value, 42);
|