generated from Over-Run/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Upcall; add DirectAccess, DowncallOption
- Loading branch information
Showing
23 changed files
with
627 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
demo/src/test/java/overrun/marshal/demo/CrossModuleWithDirectAccessTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.