Skip to content

Commit

Permalink
Downcall: fix cross-module access
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Feb 1, 2024
1 parent 72c9244 commit 2b048d0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import overrun.marshal.gen.*;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SegmentAllocator;
import java.lang.foreign.ValueLayout;

/**
* GLFW constants and functions
Expand Down Expand Up @@ -95,6 +94,6 @@ Import as a Gradle dependency:

```groovy
dependencies {
implementation("io.github.over-run:marshal:0.1.0-alpha.15-jdk22")
implementation("io.github.over-run:marshal:0.1.0-alpha.16-jdk22")
}
```
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ projGroupId=io.github.over-run
projArtifactId=marshal
# The project name should only contain lowercase letters, numbers and hyphen.
projName=marshal
projVersion=0.1.0-alpha.15-jdk22
projVersion=0.1.0-alpha.16-jdk22
projDesc=Marshaler of native libraries
# Uncomment them if you want to publish to maven repository.
projUrl=https://github.com/Over-Run/marshal
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/overrun/marshal/Downcall.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
* <h2>Loading native library</h2>
* You can load native libraries with {@link #load(Class, SymbolLookup)}.
* This method generates a hidden class that loads method handle with the given symbol lookup.
* <p>
* The generated class implements the target class.
* The target class <strong>MUST</strong> be {@code public}
* as the generated class is defined in package {@code overrun.marshal}.
* <h2>Methods</h2>
* The loader finds method from the target class and its superclasses.
* <p>
Expand Down Expand Up @@ -397,7 +401,7 @@ private static Method findUpcallWrapper(Class<?> aClass) {
@SuppressWarnings("unchecked")
private static <T> T loadBytecode(Class<?> targetClass, SymbolLookup lookup, Map<String, FunctionDescriptor> descriptorMap) {
final ClassFile cf = of();
final ClassDesc cd_thisClass = ClassDesc.of(targetClass.getPackageName(), DEFAULT_NAME);
final ClassDesc cd_thisClass = ClassDesc.of(Downcall.class.getPackageName(), DEFAULT_NAME);
final byte[] bytes = cf.build(cd_thisClass, classBuilder -> {
final List<Method> methodList = Arrays.stream(targetClass.getMethods())
.filter(method ->
Expand All @@ -420,7 +424,6 @@ private static <T> T loadBytecode(Class<?> targetClass, SymbolLookup lookup, Map
});

final Map<Method, DowncallMethodData> methodDataMap = LinkedHashMap.newLinkedHashMap(methodList.size());
final Map<Method, DowncallMethodData> handleDataMap = LinkedHashMap.newLinkedHashMap(methodList.size());

classBuilder.withFlags(ACC_FINAL | ACC_SUPER);

Expand Down Expand Up @@ -454,7 +457,6 @@ private static <T> T loadBytecode(Class<?> targetClass, SymbolLookup lookup, Map
);
methodDataMap.put(method, methodData);

handleDataMap.put(method, methodData);
classBuilder.withField(handleName, CD_MethodHandle,
ACC_PRIVATE | ACC_FINAL);
});
Expand All @@ -468,7 +470,7 @@ private static <T> T loadBytecode(Class<?> targetClass, SymbolLookup lookup, Map
.invokespecial(CD_Object, INIT_NAME, MTD_void);

// method handles
handleDataMap.values().forEach(methodData -> {
methodDataMap.values().forEach(methodData -> {
// initialize field
codeBuilder.aload(codeBuilder.receiverSlot())
.aload(codeBuilder.parameterSlot(0))
Expand Down Expand Up @@ -866,7 +868,7 @@ private static <T> T loadBytecode(Class<?> targetClass, SymbolLookup lookup, Map
});

// handle loader
handleDataMap.forEach((method, methodData) -> classBuilder.withMethod(
methodDataMap.forEach((method, methodData) -> classBuilder.withMethod(
methodData.loaderName(),
MTD_MethodHandle_SymbolLookup,
ACC_PRIVATE | ACC_STATIC,
Expand Down Expand Up @@ -1046,7 +1048,7 @@ private static <T> T loadBytecode(Class<?> targetClass, SymbolLookup lookup, Map
});

try {
final MethodHandles.Lookup hiddenClass = MethodHandles.privateLookupIn(targetClass, MethodHandles.lookup())
final MethodHandles.Lookup hiddenClass = MethodHandles.lookup()
.defineHiddenClassWithClassData(bytes, Map.copyOf(descriptorMap), true, MethodHandles.Lookup.ClassOption.STRONG);
return (T) hiddenClass.findConstructor(hiddenClass.lookupClass(), MethodType.methodType(void.class, SymbolLookup.class))
.invoke(lookup);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/overrun/marshal/test/DescriptorMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static boolean acceptLong(long d) {
return d == 84L;
}

interface Interface {
public interface Interface {
static Interface getInstance(ValueLayout returnLayout, ValueLayout acceptLayout) {
return Downcall.load(lookup(returnLayout, acceptLayout), Map.of(
"testReturn", FunctionDescriptor.of(returnLayout),
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/overrun/marshal/test/IndirectInterfaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
* @since 0.1.0
*/
public final class IndirectInterfaceTest {
interface I1 {
public interface I1 {
default int fun1() {
return 1;
}
}

interface I2 extends I1 {
public interface I2 extends I1 {
}

I2 INSTANCE = Downcall.load(I2.class, SymbolLookup.loaderLookup());
Expand Down

0 comments on commit 2b048d0

Please sign in to comment.