Skip to content

Instantly share code, notes, and snippets.

@bowbahdoe
Created July 17, 2023 15:46
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bowbahdoe/de6abbbb4abae0fa498b94ddd15d72e3 to your computer and use it in GitHub Desktop.
Continuation Proxy Classes
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public final class Continuation {
final Object impl;
static final Class<?> IMPL_CLASS;
static final MethodHandle NEW;
static final MethodHandle YIELD;
static final MethodHandle RUN;
static final MethodHandle IS_DONE;
static {
try {
IMPL_CLASS = Class.forName("jdk.internal.vm.Continuation");
var lookup = MethodHandles.privateLookupIn(
IMPL_CLASS,
MethodHandles.lookup()
);
NEW = lookup.findConstructor(
IMPL_CLASS,
MethodType.methodType(void.class, ContinuationScope.IMPL_CLASS, Runnable.class)
);
YIELD = lookup.findStatic(
IMPL_CLASS,
"yield",
MethodType.methodType(boolean.class, ContinuationScope.IMPL_CLASS)
);
RUN = lookup.findVirtual(
IMPL_CLASS,
"run",
MethodType.methodType(void.class)
);
IS_DONE = lookup.findVirtual(
IMPL_CLASS,
"isDone",
MethodType.methodType(boolean.class)
);
} catch (Throwable t) {
throw new ExceptionInInitializerError(t);
}
}
public Continuation(ContinuationScope scope, Runnable runnable) {
try {
this.impl = NEW.invoke(scope.impl, runnable);
}
catch (Throwable t) {
throw new RuntimeException(t);
}
}
public static void yield(ContinuationScope scope) {
try {
YIELD.invoke(scope.impl);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
public void run() {
try {
RUN.invoke(impl);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
public boolean isDone() {
try {
return (boolean) IS_DONE.invoke(impl);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
}
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public final class ContinuationScope {
final Object impl;
static final Class<?> IMPL_CLASS;
static final MethodHandle NEW;
static {
try {
IMPL_CLASS = Class.forName("jdk.internal.vm.ContinuationScope");
var lookup = MethodHandles.privateLookupIn(
IMPL_CLASS,
MethodHandles.lookup()
);
NEW = lookup.findConstructor(
IMPL_CLASS,
MethodType.methodType(void.class, String.class)
);
} catch (Throwable t) {
throw new ExceptionInInitializerError(t);
}
}
public ContinuationScope(String name) {
try {
this.impl = NEW.invoke(name);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
@Override
public String toString() {
return impl.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment