Skip to content

Commit

Permalink
Use condy directly such that a ConstantsRegistry class isn't generate…
Browse files Browse the repository at this point in the history
…d as a side effect.
  • Loading branch information
broneill committed Dec 2, 2024
1 parent 932134b commit 34ed180
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/main/java/org/cojen/tupl/core/DirectPageOpsSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ static String kindStr() {
}
}

static final Throwable failure;
static volatile Object result;

static {
Throwable ex = null;
try {
select();
} catch (Throwable e) {
ex = e;
result = e;
}
failure = ex;
}

private static void select() throws Throwable {
Expand Down Expand Up @@ -128,14 +126,18 @@ private static void select() throws Throwable {
return;
}

result = unsafe;

ClassMaker cm = ClassMaker.beginExplicit
("org.cojen.tupl.core.DirectPageOps", MethodHandles.lookup());

cm.addField(unsafe.getClass(), "U").private_().static_().final_();
cm.addField(long.class, "O").private_().static_().final_();

MethodMaker mm = cm.addClinit();
mm.field("U").setExact(unsafe);
var unsafeVar = mm.var(DirectPageOpsSelector.class)
.condy("U").invoke(Object.class, "U").cast(unsafe.getClass());
mm.field("U").set(unsafeVar);
mm.field("O").set(mm.field("U").invoke("arrayBaseOffset", byte[].class));

mm = cm.addMethod(int.class, "kind").static_();
Expand Down Expand Up @@ -223,4 +225,11 @@ private static Throwable combine(Throwable existing, Throwable additional) {
existing.addSuppressed(additional);
return existing;
}

// Condy method which is called by the generated code.
static Object U(MethodHandles.Lookup caller, String name, Class type) {
Object unsafe = result;
result = null;
return unsafe;
}
}

0 comments on commit 34ed180

Please sign in to comment.