Skip to content

Commit

Permalink
Update Upcall; add DirectAccess, DowncallOption
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Feb 23, 2024
1 parent 6a4b73d commit 2939ac9
Show file tree
Hide file tree
Showing 23 changed files with 627 additions and 430 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ jobs:
- name: Grant execute permission for gradlew
if: ${{ runner.os != 'Windows' }}
run: chmod +x gradlew
- name: Build with Gradle
uses: gradle/gradle-build-action@v2
with:
arguments: build --no-daemon
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Execute Gradle build
run: ./gradlew build
- name: Upload build reports
if: ${{ runner.os == 'Linux' && matrix.java == '22-ea' }}
uses: actions/upload-artifact@v4
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Marshal allows you to conveniently create native library bindings with [FFM API](https://openjdk.org/jeps/454).

See [wiki](https://github.com/Over-Run/marshal/wiki) for more information.
~~See [wiki](https://github.com/Over-Run/marshal/wiki) for more information.~~

This library requires JDK 22 or newer.

Expand Down Expand Up @@ -101,3 +101,7 @@ dependencies {

and add this VM argument to enable native access: `--enable-native-access=io.github.overrun.marshal`
or this if you don't use modules: `--enable-native-access=ALL-UNNAMED`

## Additions

- [OverrunGL](https://github.com/Over-Run/overrungl), which is using Marshal
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ allprojects {
dependencies {
// add your dependencies
compileOnly("org.jetbrains:annotations:24.1.0")
testImplementation(platform("org.junit:junit-bom:5.10.1"))
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

Expand Down
3 changes: 2 additions & 1 deletion demo/src/test/java/overrun/marshal/demo/CrossModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import overrun.marshal.Downcall;
import overrun.marshal.DowncallOption;

import java.lang.foreign.*;
import java.lang.invoke.MethodHandles;
Expand Down Expand Up @@ -55,6 +56,6 @@ public interface I {

@Test
void testCrossModule() {
Assertions.assertEquals(1, Downcall.load(MethodHandles.lookup(), I.class, LOOKUP).get());
Assertions.assertEquals(1, Downcall.<I>load(MethodHandles.lookup(), LOOKUP, DowncallOption.targetClass(I.class)).get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrun.marshal.demo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import overrun.marshal.DirectAccess;
import overrun.marshal.Downcall;
import overrun.marshal.DowncallOption;

import java.lang.foreign.*;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Optional;

/**
* Test cross module
*
* @author squid233
* @since 0.1.0
*/
public final class CrossModuleWithDirectAccessTest {
private static final Linker LINKER = Linker.nativeLinker();
private static final MemorySegment s_get;

static {
try {
s_get = LINKER.upcallStub(MethodHandles.lookup().findStatic(CrossModuleWithDirectAccessTest.class, "get", MethodType.methodType(int.class)), FunctionDescriptor.of(ValueLayout.JAVA_INT), Arena.ofAuto());
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

private static final SymbolLookup LOOKUP = name -> "get".equals(name) ? Optional.of(s_get) : Optional.empty();

private static int get() {
return 1;
}

public interface I extends DirectAccess {
int get();
}

@Test
void testCrossModule() {
Assertions.assertEquals(1, Downcall.<I>load(MethodHandles.lookup(), LOOKUP, DowncallOption.targetClass(I.class)).get());
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
exports overrun.marshal.gen;
exports overrun.marshal.struct;

opens overrun.marshal.internal;

requires static org.jetbrains.annotations;
}
55 changes: 55 additions & 0 deletions src/main/java/overrun/marshal/DirectAccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrun.marshal;

import org.jetbrains.annotations.Unmodifiable;

import java.lang.foreign.FunctionDescriptor;
import java.lang.invoke.MethodHandle;
import java.util.Map;

/**
* This interface provides access to function descriptors and method handles
* for each function that is loaded by {@link Downcall}.
*
* @author squid233
* @see Downcall
* @since 0.1.0
*/
public interface DirectAccess {
/**
* {@return an unmodifiable map of the function descriptors for each method}
*/
@Unmodifiable
Map<String, FunctionDescriptor> functionDescriptors();

/**
* {@return an unmodifiable map of the method handles for each method}
*/
@Unmodifiable
Map<String, MethodHandle> methodHandles();

/**
* Gets the method handle with the given entrypoint name.
*
* @param entrypoint the entrypoint name
* @return the method handle
*/
default MethodHandle methodHandle(String entrypoint) {
return methodHandles().get(entrypoint);
}
}
Loading

0 comments on commit 2939ac9

Please sign in to comment.