Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug for methods returning MethodHandle #17

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Import as a Gradle dependency:

```groovy
dependencies {
implementation("io.github.over-run:marshal:0.1.0-alpha.30-jdk23")
implementation("io.github.over-run:marshal:0.1.0-alpha.31-jdk23")
}
```

Expand Down
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.30-jdk23
projVersion=0.1.0-alpha.31-jdk23
projDesc=Marshaler of native libraries
# Uncomment them if you want to publish to maven repository.
projUrl=https://github.com/Over-Run/marshal
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/overrun/marshal/Downcall.java
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,16 @@ private static DowncallData generateData(

// function descriptor
final FunctionDescriptor get = descriptorMap.get(entrypoint);
final FunctionDescriptor descriptor = get != null ?
get :
DescriptorTransformer.getInstance().process(new DescriptorTransformer.Context(method,
methodData.descriptorSkipFirstParameter(),
methodData.parameters()));

final FunctionDescriptor descriptor;
if (method.getReturnType() == MethodHandle.class) {
descriptor = get;
} else {
descriptor = get != null ?
get :
DescriptorTransformer.getInstance().process(new DescriptorTransformer.Context(method,
methodData.descriptorSkipFirstParameter(),
methodData.parameters()));
}
descriptorMap1.put(entrypoint, descriptor);

final Optional<MemorySegment> optional = lookup.find(entrypoint);
Expand All @@ -664,7 +668,9 @@ private static DowncallData generateData(
throw new NoSuchElementException("Symbol not found: " + entrypoint + " (" + descriptor + "): " + methodData.signatureString());
}
}
map.putIfAbsent(entrypoint, handle);
if (!map.containsKey(entrypoint) || map.get(entrypoint) == null) {
map.put(entrypoint, handle);
}
}
return new DowncallData(Collections.unmodifiableMap(descriptorMap1),
Collections.unmodifiableMap(map),
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/overrun/marshal/test/downcall/IDowncall.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@
* @since 0.1.0
*/
public interface IDowncall {
Map<String, FunctionDescriptor> MAP = Map.of("testDefault", FunctionDescriptor.of(ValueLayout.JAVA_INT));
Map<String, FunctionDescriptor> MAP = Map.of(
"testDefault", FunctionDescriptor.of(ValueLayout.JAVA_INT),
"testReturnInt", FunctionDescriptor.of(ValueLayout.JAVA_INT)
);

static IDowncall getInstance(boolean testDefaultNull) {
ProcessorTypes.registerStruct(Vector3.class, Vector3.OF);
ProcessorTypes.registerUpcall(SimpleUpcall.class, stub -> i -> SimpleUpcall.invoke(stub, i));
return Downcall.load(MethodHandles.lookup(), DowncallProvider.lookup(testDefaultNull), DowncallOption.descriptors(MAP));
}

@Entrypoint("testReturnInt")
MethodHandle mh_testReturnInt();

void test();

@Entrypoint("test")
Expand Down