package jdk.internal.vm.compiler.libgraal;
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
import jdk.vm.ci.hotspot.HotSpotSpeculationLog;
import jdk.vm.ci.services.Services;
public class LibGraal {
public static boolean isAvailable() {
return inLibGraal() || isolate != 0L;
}
public static boolean isSupported() {
return true;
}
public static boolean inLibGraal() {
return Services.IS_IN_NATIVE_IMAGE;
}
public static void registerNativeMethods(HotSpotJVMCIRuntime runtime, Class<?> clazz) {
if (clazz.isPrimitive()) {
throw new IllegalArgumentException();
}
if (inLibGraal() || !isAvailable()) {
throw new IllegalStateException();
}
runtime.registerNativeMethods(clazz);
}
public static long translate(HotSpotJVMCIRuntime runtime, Object obj) {
if (!isAvailable()) {
throw new IllegalStateException();
}
if (!inLibGraal() && LibGraalScope.currentScope.get() == null) {
throw new IllegalStateException("Not within a " + LibGraalScope.class.getName());
}
return runtime.translate(obj);
}
public static <T> T unhand(HotSpotJVMCIRuntime runtime, Class<T> type, long handle) {
if (!isAvailable()) {
throw new IllegalStateException();
}
if (!inLibGraal() && LibGraalScope.currentScope.get() == null) {
throw new IllegalStateException("Not within a " + LibGraalScope.class.getName());
}
return runtime.unhand(type, handle);
}
private static long initializeLibgraal() {
try {
Services.initializeJVMCI();
HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime();
long[] nativeInterface = runtime.registerNativeMethods(LibGraal.class);
return nativeInterface[1];
} catch (UnsupportedOperationException e) {
return 0L;
}
}
static final long isolate = Services.IS_BUILDING_NATIVE_IMAGE ? 0L : initializeLibgraal();
static boolean isCurrentThreadAttached(HotSpotJVMCIRuntime runtime) {
return runtime.isCurrentThreadAttached();
}