2021-06-02 10:53:06 +00:00
|
|
|
/*
|
2023-10-12 19:50:08 +00:00
|
|
|
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
|
2023-08-07 10:58:11 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2021-06-02 10:53:06 +00:00
|
|
|
*
|
2023-08-07 10:58:11 +00:00
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
2021-06-02 10:53:06 +00:00
|
|
|
*
|
2023-08-07 10:58:11 +00:00
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
2021-06-02 10:53:06 +00:00
|
|
|
*
|
2023-08-07 10:58:11 +00:00
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
2021-06-02 10:53:06 +00:00
|
|
|
*
|
2023-08-07 10:58:11 +00:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2021-06-02 10:53:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
2021-11-24 11:51:16 +00:00
|
|
|
* @library ../
|
2021-06-02 10:53:06 +00:00
|
|
|
* @run testng/othervm
|
|
|
|
* --enable-native-access=ALL-UNNAMED
|
|
|
|
* TestVirtualCalls
|
|
|
|
*/
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
import java.lang.foreign.Linker;
|
|
|
|
import java.lang.foreign.FunctionDescriptor;
|
2021-06-02 10:53:06 +00:00
|
|
|
|
2022-12-05 13:49:53 +00:00
|
|
|
import java.lang.foreign.MemorySegment;
|
2021-06-02 10:53:06 +00:00
|
|
|
import java.lang.invoke.MethodHandle;
|
|
|
|
|
|
|
|
import org.testng.annotations.*;
|
|
|
|
|
|
|
|
import static org.testng.Assert.assertEquals;
|
|
|
|
|
2021-11-24 11:51:16 +00:00
|
|
|
public class TestVirtualCalls extends NativeTestHelper {
|
2021-06-02 10:53:06 +00:00
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
static final Linker abi = Linker.nativeLinker();
|
2021-06-02 10:53:06 +00:00
|
|
|
|
|
|
|
static final MethodHandle func;
|
2022-12-05 13:49:53 +00:00
|
|
|
static final MemorySegment funcA;
|
|
|
|
static final MemorySegment funcB;
|
|
|
|
static final MemorySegment funcC;
|
2021-06-02 10:53:06 +00:00
|
|
|
|
|
|
|
static {
|
|
|
|
func = abi.downcallHandle(
|
2021-11-24 11:51:16 +00:00
|
|
|
FunctionDescriptor.of(C_INT));
|
2021-06-02 10:53:06 +00:00
|
|
|
|
2021-06-04 12:53:17 +00:00
|
|
|
System.loadLibrary("Virtual");
|
2022-05-12 16:17:45 +00:00
|
|
|
funcA = findNativeOrThrow("funcA");
|
|
|
|
funcB = findNativeOrThrow("funcB");
|
|
|
|
funcC = findNativeOrThrow("funcC");
|
2021-06-02 10:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testVirtualCalls() throws Throwable {
|
2021-11-24 11:51:16 +00:00
|
|
|
assertEquals((int) func.invokeExact(funcA), 1);
|
|
|
|
assertEquals((int) func.invokeExact(funcB), 2);
|
|
|
|
assertEquals((int) func.invokeExact(funcC), 3);
|
2021-06-02 10:53:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 12:53:17 +00:00
|
|
|
@Test(expectedExceptions = NullPointerException.class)
|
|
|
|
public void testNullTarget() throws Throwable {
|
2022-12-05 13:49:53 +00:00
|
|
|
int x = (int) func.invokeExact((MemorySegment)null);
|
2021-06-04 12:53:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
}
|