From 2f2a0cce922f09bc618478496ed44d0166b92fa1 Mon Sep 17 00:00:00 2001 From: squid233 <60126026+squid233@users.noreply.github.com> Date: Mon, 7 Oct 2024 12:19:30 +0800 Subject: [PATCH] Add DirectAccessData --- src/main/java/module-info.java | 2 - .../java/overrun/marshal/DirectAccess.java | 38 +---------------- ...owncallData.java => DirectAccessData.java} | 16 +++---- src/main/java/overrun/marshal/Downcall.java | 42 ++++++------------- .../overrun/marshal/internal/Constants.java | 18 ++++---- .../overrun/marshal/test/GlobalVarTest.java | 2 +- .../test/downcall/DowncallInheritTest.java | 2 +- 7 files changed, 32 insertions(+), 88 deletions(-) rename src/main/java/overrun/marshal/{internal/data/DowncallData.java => DirectAccessData.java} (66%) diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index deedd12..0d264c0 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -26,8 +26,6 @@ exports overrun.marshal.gen.processor; exports overrun.marshal.struct; - opens overrun.marshal.internal.data; - requires static org.jetbrains.annotations; requires io.github.overrun.memstack; } diff --git a/src/main/java/overrun/marshal/DirectAccess.java b/src/main/java/overrun/marshal/DirectAccess.java index 370ba5c..2353631 100644 --- a/src/main/java/overrun/marshal/DirectAccess.java +++ b/src/main/java/overrun/marshal/DirectAccess.java @@ -16,14 +16,6 @@ package overrun.marshal; -import org.jetbrains.annotations.Unmodifiable; -import overrun.marshal.gen.Skip; - -import java.lang.foreign.FunctionDescriptor; -import java.lang.foreign.SymbolLookup; -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}. @@ -34,33 +26,7 @@ */ public interface DirectAccess { /** - * {@return an unmodifiable map of the function descriptors for each method} - */ - @Skip - @Unmodifiable - Map functionDescriptors(); - - /** - * {@return an unmodifiable map of the method handles for each method} - */ - @Skip - @Unmodifiable - Map methodHandles(); - - /** - * Gets the method handle with the given entrypoint name. - * - * @param entrypoint the entrypoint name - * @return the method handle - */ - @Skip - default MethodHandle methodHandle(String entrypoint) { - return methodHandles().get(entrypoint); - } - - /** - * {@return the symbol lookup of this library} + * {@return the data of this access} */ - @Skip - SymbolLookup symbolLookup(); + DirectAccessData directAccessData(); } diff --git a/src/main/java/overrun/marshal/internal/data/DowncallData.java b/src/main/java/overrun/marshal/DirectAccessData.java similarity index 66% rename from src/main/java/overrun/marshal/internal/data/DowncallData.java rename to src/main/java/overrun/marshal/DirectAccessData.java index 9202355..b552c5e 100644 --- a/src/main/java/overrun/marshal/internal/data/DowncallData.java +++ b/src/main/java/overrun/marshal/DirectAccessData.java @@ -14,7 +14,7 @@ * copies or substantial portions of the Software. */ -package overrun.marshal.internal.data; +package overrun.marshal; import java.lang.foreign.FunctionDescriptor; import java.lang.foreign.SymbolLookup; @@ -22,17 +22,17 @@ import java.util.Map; /** - * Downcall class data + * The data for {@link DirectAccess}. * - * @param descriptorMap descriptorMap - * @param handleMap handleMap - * @param symbolLookup symbolLookup + * @param functionDescriptors an unmodifiable map of the function descriptors for each method + * @param methodHandles an unmodifiable map of the method handles for each method + * @param symbolLookup the symbol lookup of this library * @author squid233 * @since 0.1.0 */ -public record DowncallData( - Map descriptorMap, - Map handleMap, +public record DirectAccessData( + Map functionDescriptors, + Map methodHandles, SymbolLookup symbolLookup ) { } diff --git a/src/main/java/overrun/marshal/Downcall.java b/src/main/java/overrun/marshal/Downcall.java index fbf7a93..f0d1068 100644 --- a/src/main/java/overrun/marshal/Downcall.java +++ b/src/main/java/overrun/marshal/Downcall.java @@ -20,7 +20,6 @@ import overrun.marshal.gen.processor.*; import overrun.marshal.internal.DowncallOptions; import overrun.marshal.internal.StringCharset; -import overrun.marshal.internal.data.DowncallData; import overrun.marshal.struct.ByValue; import java.lang.classfile.ClassFile; @@ -208,7 +207,7 @@ private static String getMethodEntrypoint(Method method) { entrypoint.value(); } - private static Map.Entry buildBytecode(MethodHandles.Lookup caller, SymbolLookup lookup, DowncallOption... options) { + private static Map.Entry buildBytecode(MethodHandles.Lookup caller, SymbolLookup lookup, DowncallOption... options) { Class _targetClass = null, targetClass; Map _descriptorMap = null, descriptorMap; UnaryOperator _transform = null, transform; @@ -281,7 +280,7 @@ private static Map.Entry buildBytecode(MethodHandles.Looku } //endregion - final DowncallData downcallData = generateData(methodDataMap, lookup, descriptorMap, transform); + final var downcallData = generateData(methodDataMap, lookup, descriptorMap, transform); return Map.entry(cf.build(cd_thisClass, classBuilder -> { classBuilder.withFlags(ACC_FINAL | ACC_SUPER); @@ -296,7 +295,7 @@ private static Map.Entry buildBytecode(MethodHandles.Looku //region method handles for (String entrypoint : methodEntrypointSet) { - if (downcallData.handleMap().get(entrypoint) != null) { + if (downcallData.methodHandles().get(entrypoint) != null) { classBuilder.withField(entrypoint, CD_MethodHandle, ACC_PRIVATE | ACC_FINAL | ACC_STATIC); @@ -342,7 +341,7 @@ private static Map.Entry buildBytecode(MethodHandles.Looku // returns MethodHandle if (returnType == MethodHandle.class) { if (method.isDefault() && - downcallData.handleMap().get(entrypoint) == null) { + downcallData.methodHandles().get(entrypoint) == null) { // invoke super interface invokeSuperMethod(codeBuilder, parameters); codeBuilder.invokespecial(cd_targetClass, @@ -358,7 +357,7 @@ private static Map.Entry buildBytecode(MethodHandles.Looku } if (method.isDefault() && - downcallData.handleMap().get(entrypoint) == null) { + downcallData.methodHandles().get(entrypoint) == null) { // invoke super interface invokeSuperMethod(codeBuilder, parameters); codeBuilder.invokespecial(cd_targetClass, @@ -470,26 +469,11 @@ private static Map.Entry buildBytecode(MethodHandles.Looku //region DirectAccess final boolean hasDirectAccess = DirectAccess.class.isAssignableFrom(targetClass); if (hasDirectAccess) { - classBuilder.withMethodBody("functionDescriptors", - MTD_Map, + classBuilder.withMethodBody("directAccessData", + MTD_DirectAccessData, ACC_PUBLIC, codeBuilder -> codeBuilder - .ldc(DCD_classData_DowncallData) - .invokevirtual(CD_DowncallData, "descriptorMap", MTD_Map) - .areturn()); - classBuilder.withMethodBody("methodHandles", - MTD_Map, - ACC_PUBLIC, - codeBuilder -> codeBuilder - .ldc(DCD_classData_DowncallData) - .invokevirtual(CD_DowncallData, "handleMap", MTD_Map) - .areturn()); - classBuilder.withMethodBody("symbolLookup", - MTD_SymbolLookup, - ACC_PUBLIC, - codeBuilder -> codeBuilder - .ldc(DCD_classData_DowncallData) - .invokevirtual(CD_DowncallData, "symbolLookup", MTD_SymbolLookup) + .ldc(DCD_classData_DirectAccessData) .areturn()); } //endregion @@ -498,13 +482,13 @@ private static Map.Entry buildBytecode(MethodHandles.Looku classBuilder.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, codeBuilder -> { final int handleMapSlot = codeBuilder.allocateLocal(TypeKind.ReferenceType); - codeBuilder.ldc(DCD_classData_DowncallData) - .invokevirtual(CD_DowncallData, "handleMap", MTD_Map) + codeBuilder.ldc(DCD_classData_DirectAccessData) + .invokevirtual(CD_DirectAccessData, "methodHandles", MTD_Map) .astore(handleMapSlot); // method handles for (String entrypoint : methodEntrypointSet) { - if (downcallData.handleMap().get(entrypoint) != null) { + if (downcallData.methodHandles().get(entrypoint) != null) { codeBuilder .aload(handleMapSlot) .ldc(entrypoint) @@ -622,7 +606,7 @@ private static boolean isValidReturnType(Class aClass) { return aClass == MethodHandle.class || ProcessorTypes.isRegisteredExactly(aClass); } - private static DowncallData generateData( + private static DirectAccessData generateData( Map methodDataMap, SymbolLookup lookup, Map descriptorMap, @@ -676,7 +660,7 @@ private static DowncallData generateData( } } } - return new DowncallData(Collections.unmodifiableMap(descriptorMap1), + return new DirectAccessData(Collections.unmodifiableMap(descriptorMap1), Collections.unmodifiableMap(map), lookup); } diff --git a/src/main/java/overrun/marshal/internal/Constants.java b/src/main/java/overrun/marshal/internal/Constants.java index b73cf14..d80f344 100644 --- a/src/main/java/overrun/marshal/internal/Constants.java +++ b/src/main/java/overrun/marshal/internal/Constants.java @@ -42,9 +42,9 @@ public final class Constants { */ public static final ClassDesc CD_Checks = ClassDesc.of("overrun.marshal.Checks"); /** - * CD_DowncallData + * CD_DirectAccessData */ - public static final ClassDesc CD_DowncallData = ClassDesc.of("overrun.marshal.internal.data.DowncallData"); + public static final ClassDesc CD_DirectAccessData = ClassDesc.of("overrun.marshal.DirectAccessData"); /** * CD_IllegalStateException */ @@ -105,10 +105,6 @@ public final class Constants { * CD_StructLayout */ public static final ClassDesc CD_StructLayout = ClassDesc.of("java.lang.foreign.StructLayout"); - /** - * CD_SymbolLookup - */ - public static final ClassDesc CD_SymbolLookup = ClassDesc.of("java.lang.foreign.SymbolLookup"); /** * CD_Unmarshal */ @@ -214,6 +210,10 @@ public final class Constants { * MTD_Charset_String */ public static final MethodTypeDesc MTD_Charset_String = MethodTypeDesc.of(CD_Charset, CD_String); + /** + * MTD_DirectAccessData + */ + public static final MethodTypeDesc MTD_DirectAccessData = MethodTypeDesc.of(CD_DirectAccessData); /** * MTD_long */ @@ -366,10 +366,6 @@ public final class Constants { * MTD_StructLayout */ public static final MethodTypeDesc MTD_StructLayout = MethodTypeDesc.of(CD_StructLayout); - /** - * MTD_SymbolLookup - */ - public static final MethodTypeDesc MTD_SymbolLookup = MethodTypeDesc.of(CD_SymbolLookup); /** * MTD_Upcall_MemorySegment */ @@ -438,7 +434,7 @@ public final class Constants { /** * DCD_classData_DowncallData */ - public static final DynamicConstantDesc DCD_classData_DowncallData = DynamicConstantDesc.ofNamed(BSM_CLASS_DATA, DEFAULT_NAME, CD_DowncallData); + public static final DynamicConstantDesc DCD_classData_DirectAccessData = DynamicConstantDesc.ofNamed(BSM_CLASS_DATA, DEFAULT_NAME, CD_DirectAccessData); /** * DCD_classData_StructLayout */ diff --git a/src/test/java/overrun/marshal/test/GlobalVarTest.java b/src/test/java/overrun/marshal/test/GlobalVarTest.java index e740018..37ea453 100644 --- a/src/test/java/overrun/marshal/test/GlobalVarTest.java +++ b/src/test/java/overrun/marshal/test/GlobalVarTest.java @@ -44,7 +44,7 @@ public final class GlobalVarTest { interface I extends DirectAccess { I INSTANCE = Downcall.load(MethodHandles.lookup(), LOOKUP); - MemorySegment myGlobalVar = INSTANCE.symbolLookup().findOrThrow("globalVar"); + MemorySegment myGlobalVar = INSTANCE.directAccessData().symbolLookup().findOrThrow("globalVar"); } @Test diff --git a/src/test/java/overrun/marshal/test/downcall/DowncallInheritTest.java b/src/test/java/overrun/marshal/test/downcall/DowncallInheritTest.java index 960aae5..7901985 100644 --- a/src/test/java/overrun/marshal/test/downcall/DowncallInheritTest.java +++ b/src/test/java/overrun/marshal/test/downcall/DowncallInheritTest.java @@ -39,6 +39,6 @@ void testInheritDowncall() { assertEquals(1, d.get2()); assertEquals(3, d.get3()); - assertEquals(3, assertDoesNotThrow(() -> (int) d.methodHandle("get3").invokeExact())); + assertEquals(3, assertDoesNotThrow(() -> (int) d.directAccessData().methodHandles().get("get3").invokeExact())); } }