Skip to content

Commit

Permalink
Downcall: generate method handle for each method
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Feb 1, 2024
1 parent 3b5fa70 commit 72c9244
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ Import as a Gradle dependency:

```groovy
dependencies {
implementation("io.github.over-run:marshal:0.1.0-alpha.14-jdk22")
implementation("io.github.over-run:marshal:0.1.0-alpha.15-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.14-jdk22
projVersion=0.1.0-alpha.15-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
16 changes: 7 additions & 9 deletions src/main/java/overrun/marshal/Downcall.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.lang.invoke.MethodType;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -420,7 +421,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());
final List<String> addedHandleList = new ArrayList<>(methodList.size());

classBuilder.withFlags(ACC_FINAL | ACC_SUPER);

Expand All @@ -432,15 +432,16 @@ private static <T> T loadBytecode(Class<?> targetClass, SymbolLookup lookup, Map
classBuilder.withField("$LINKER", CD_Linker, ACC_PRIVATE | ACC_FINAL | ACC_STATIC);

// method handles
final AtomicInteger handleCount = new AtomicInteger();
methodList.forEach(method -> {
final String entrypoint = getMethodEntrypoint(method);
final String handleName = STR."$mh_\{entrypoint}";
final String handleName = STR."$mh\{handleCount.getAndIncrement()}";
final var parameters = List.of(method.getParameters());

final DowncallMethodData methodData = new DowncallMethodData(
entrypoint,
handleName,
STR."$load$\{method.getName()}",
STR."$load\{handleName}",
STR."""
\{method.getReturnType().getCanonicalName()} \
\{method.getDeclaringClass().getCanonicalName()}.\{method.getName()}\
Expand All @@ -453,12 +454,9 @@ private static <T> T loadBytecode(Class<?> targetClass, SymbolLookup lookup, Map
);
methodDataMap.put(method, methodData);

if (!addedHandleList.contains(handleName)) {
handleDataMap.put(method, methodData);
addedHandleList.add(handleName);
classBuilder.withField(handleName, CD_MethodHandle,
ACC_PRIVATE | ACC_FINAL);
}
handleDataMap.put(method, methodData);
classBuilder.withField(handleName, CD_MethodHandle,
ACC_PRIVATE | ACC_FINAL);
});

// constructor
Expand Down

0 comments on commit 72c9244

Please sign in to comment.