< prev index next >

test/jdk/java/foreign/TestUpcallStructScope.java

Print this page

 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  * @requires jdk.foreign.linker != "UNSUPPORTED"
29  *
30  * @run testng/othervm/native
31  *   --enable-native-access=ALL-UNNAMED
32  *   -Djdk.internal.foreign.DowncallLinker.USE_SPEC=false
33  *   TestUpcallStructScope
34  * @run testng/othervm/native
35  *   --enable-native-access=ALL-UNNAMED
36  *   -Djdk.internal.foreign.DowncallLinker.USE_SPEC=true
37  *   TestUpcallStructScope
38  */
39 
40 import java.lang.foreign.*;
41 
42 import org.testng.annotations.Test;
43 
44 import java.lang.invoke.MethodHandle;
45 import java.lang.invoke.MethodHandles;
46 import java.lang.invoke.MethodType;
47 import java.util.concurrent.atomic.AtomicReference;
48 import java.util.function.Consumer;
49 
50 import static org.testng.Assert.assertFalse;
51 
52 public class TestUpcallStructScope extends NativeTestHelper {
53     static final MethodHandle MH_do_upcall;
54     static final Linker LINKER = Linker.nativeLinker();
55     static final MethodHandle MH_Consumer_accept;
56 
57     // struct S_PDI { void* p0; double p1; int p2; };
58     static final MemoryLayout S_PDI_LAYOUT = MemoryLayout.structLayout(
59         C_POINTER.withName("p0"),
60         C_DOUBLE.withName("p1"),
61         C_INT.withName("p2"),
62         MemoryLayout.paddingLayout(4)
63     );
64 
65     static {
66         System.loadLibrary("TestUpcallStructScope");
67         MH_do_upcall = LINKER.downcallHandle(
68                 findNativeOrThrow("do_upcall"),
69                 FunctionDescriptor.ofVoid(C_POINTER, S_PDI_LAYOUT)
70         );
71 
72         try {
73             MH_Consumer_accept = MethodHandles.publicLookup().findVirtual(Consumer.class, "accept",
74                     MethodType.methodType(void.class, Object.class));
75         } catch (NoSuchMethodException | IllegalAccessException e) {
76             throw new RuntimeException(e);
77         }
78     }
79 
80     private static MethodHandle methodHandle (Consumer<MemorySegment> callback) {
81         return MH_Consumer_accept.bindTo(callback).asType(MethodType.methodType(void.class, MemorySegment.class));
82     }
83 
84     @Test

 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  *
28  * @run testng/othervm/native
29  *   --enable-native-access=ALL-UNNAMED
30  *   -Djdk.internal.foreign.DowncallLinker.USE_SPEC=false
31  *   TestUpcallStructScope
32  * @run testng/othervm/native
33  *   --enable-native-access=ALL-UNNAMED
34  *   -Djdk.internal.foreign.DowncallLinker.USE_SPEC=true
35  *   TestUpcallStructScope
36  */
37 
38 import java.lang.foreign.*;
39 
40 import org.testng.annotations.Test;
41 
42 import java.lang.invoke.MethodHandle;
43 import java.lang.invoke.MethodHandles;
44 import java.lang.invoke.MethodType;
45 import java.util.concurrent.atomic.AtomicReference;
46 import java.util.function.Consumer;
47 
48 import static org.testng.Assert.assertFalse;
49 
50 public class TestUpcallStructScope extends NativeTestHelper {
51     static final MethodHandle MH_do_upcall;

52     static final MethodHandle MH_Consumer_accept;
53 








54     static {
55         System.loadLibrary("TestUpcallStructScope");
56         MH_do_upcall = LINKER.downcallHandle(
57                 findNativeOrThrow("do_upcall"),
58                 FunctionDescriptor.ofVoid(C_POINTER, S_PDI_LAYOUT)
59         );
60 
61         try {
62             MH_Consumer_accept = MethodHandles.publicLookup().findVirtual(Consumer.class, "accept",
63                     MethodType.methodType(void.class, Object.class));
64         } catch (NoSuchMethodException | IllegalAccessException e) {
65             throw new RuntimeException(e);
66         }
67     }
68 
69     private static MethodHandle methodHandle (Consumer<MemorySegment> callback) {
70         return MH_Consumer_accept.bindTo(callback).asType(MethodType.methodType(void.class, MemorySegment.class));
71     }
72 
73     @Test
< prev index next >