From f0b624302ca735c4e2fea0b256d4cb3471e58d18 Mon Sep 17 00:00:00 2001 From: squid233 <60126026+squid233@users.noreply.github.com> Date: Sat, 2 Mar 2024 19:09:47 +0800 Subject: [PATCH] Added Unmarshal::unboundString --- README.md | 2 +- gradle.properties | 2 +- src/main/java/module-info.java | 2 +- .../java/overrun/marshal/Configurations.java | 12 ++++++++-- src/main/java/overrun/marshal/Downcall.java | 6 ++--- .../java/overrun/marshal/MemoryStack.java | 2 +- src/main/java/overrun/marshal/Unmarshal.java | 23 ++++++++++++++++++- .../internal/{ => data}/DowncallData.java | 2 +- .../marshal/test/DowncallInheritTest.java | 5 ++-- src/test/java/overrun/marshal/test/ID3.java | 3 ++- .../java/overrun/marshal/test/IDowncall.java | 2 -- 11 files changed, 45 insertions(+), 16 deletions(-) rename src/main/java/overrun/marshal/internal/{ => data}/DowncallData.java (96%) diff --git a/README.md b/README.md index e70acea..2d86d81 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ Import as a Gradle dependency: ```groovy dependencies { - implementation("io.github.over-run:marshal:0.1.0-alpha.21-jdk22") + implementation("io.github.over-run:marshal:0.1.0-alpha.22-jdk22") } ``` diff --git a/gradle.properties b/gradle.properties index 0dad2fe..a2aa8e6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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.21-jdk22 +projVersion=0.1.0-alpha.22-jdk22 projDesc=Marshaler of native libraries # Uncomment them if you want to publish to maven repository. projUrl=https://github.com/Over-Run/marshal diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index dfbcb36..4d35c6f 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -25,7 +25,7 @@ exports overrun.marshal.gen; exports overrun.marshal.struct; - opens overrun.marshal.internal; + opens overrun.marshal.internal.data; requires static org.jetbrains.annotations; } diff --git a/src/main/java/overrun/marshal/Configurations.java b/src/main/java/overrun/marshal/Configurations.java index 3e97588..711f99f 100644 --- a/src/main/java/overrun/marshal/Configurations.java +++ b/src/main/java/overrun/marshal/Configurations.java @@ -18,7 +18,6 @@ import overrun.marshal.gen.Sized; -import java.util.Objects; import java.util.function.Consumer; import java.util.function.Supplier; @@ -79,7 +78,7 @@ private Configurations() { * @param logger the logger */ public static void setApiLogger(Consumer logger) { - apiLogger = Objects.requireNonNullElseGet(logger, () -> System.err::println); + apiLogger = logger != null ? logger : System.err::println; } /** @@ -122,5 +121,14 @@ public T get() { } return value; } + + /** + * Sets the value + * + * @param t the value + */ + public void set(T t) { + value = t; + } } } diff --git a/src/main/java/overrun/marshal/Downcall.java b/src/main/java/overrun/marshal/Downcall.java index 050776b..9ffbab2 100644 --- a/src/main/java/overrun/marshal/Downcall.java +++ b/src/main/java/overrun/marshal/Downcall.java @@ -18,7 +18,7 @@ import overrun.marshal.gen.Type; import overrun.marshal.gen.*; -import overrun.marshal.internal.DowncallData; +import overrun.marshal.internal.data.DowncallData; import overrun.marshal.internal.DowncallOptions; import overrun.marshal.struct.ByValue; import overrun.marshal.struct.Struct; @@ -125,7 +125,7 @@ public final class Downcall { private static final ClassDesc CD_CEnum = ClassDesc.of("overrun.marshal.CEnum"); private static final ClassDesc CD_Charset = ClassDesc.of("java.nio.charset.Charset"); private static final ClassDesc CD_Checks = ClassDesc.of("overrun.marshal.Checks"); - private static final ClassDesc CD_DowncallData = ClassDesc.of("overrun.marshal.internal.DowncallData"); + private static final ClassDesc CD_DowncallData = ClassDesc.of("overrun.marshal.internal.data.DowncallData"); private static final ClassDesc CD_IllegalStateException = ClassDesc.of("java.lang.IllegalStateException"); private static final ClassDesc CD_Marshal = ClassDesc.of("overrun.marshal.Marshal"); private static final ClassDesc CD_MemorySegment = ClassDesc.of("java.lang.foreign.MemorySegment"); @@ -671,7 +671,7 @@ private static T loadBytecode(MethodHandles.Lookup caller, SymbolLookup look } else if (returnType == String.class) { final boolean hasCharset = getCharset(blockCodeBuilder, method); blockCodeBuilder.invokestatic(CD_Unmarshal, - "unmarshalAsString", + "unboundString", hasCharset ? MTD_String_MemorySegment_Charset : MTD_String_MemorySegment); } else if (Struct.class.isAssignableFrom(returnType)) { blockCodeBuilder.ifThenElse(Opcode.IFNONNULL, diff --git a/src/main/java/overrun/marshal/MemoryStack.java b/src/main/java/overrun/marshal/MemoryStack.java index 710d68e..5b470f0 100644 --- a/src/main/java/overrun/marshal/MemoryStack.java +++ b/src/main/java/overrun/marshal/MemoryStack.java @@ -117,7 +117,7 @@ public static MemoryStack create(MemorySegment segment) { private void frameOverflow() { if (DEBUG) { - Configurations.apiLog("[WARNING] Out of frame stack space (" + frames.length + ") in thread: " + Thread.currentThread()); + Configurations.apiLog(STR."[WARNING] Out of frame stack space (\{frames.length}) in thread: \{Thread.currentThread()}"); } frames = Arrays.copyOf(frames, frames.length * 3 / 2); } diff --git a/src/main/java/overrun/marshal/Unmarshal.java b/src/main/java/overrun/marshal/Unmarshal.java index f28a396..39d558d 100644 --- a/src/main/java/overrun/marshal/Unmarshal.java +++ b/src/main/java/overrun/marshal/Unmarshal.java @@ -56,7 +56,7 @@ private Unmarshal() { * @param segment the native segment */ public static boolean isNullPointer(@Nullable MemorySegment segment) { - return segment == null || segment.address() == 0L; + return segment == null || segment == MemorySegment.NULL || segment.address() == 0L; } /** @@ -150,6 +150,27 @@ public static boolean unmarshalAsBoolean(double v) { return isNullPointer(segment) ? null : segment.getString(0L, charset); } + /** + * Unmarshal the given segment as a string. + * + * @param segment the segment, which size is unknown + * @return the string + */ + public static @Nullable String unboundString(MemorySegment segment) { + return isNullPointer(segment) ? null : segment.reinterpret(STR_SIZE).getString(0L); + } + + /** + * Unmarshal the given segment as a string. + * + * @param segment the segment, which size is unknown + * @param charset the charset + * @return the string + */ + public static @Nullable String unboundString(MemorySegment segment, Charset charset) { + return isNullPointer(segment) ? null : segment.reinterpret(STR_SIZE).getString(0L, charset); + } + /** * Unmarshal the given segment as an array. * diff --git a/src/main/java/overrun/marshal/internal/DowncallData.java b/src/main/java/overrun/marshal/internal/data/DowncallData.java similarity index 96% rename from src/main/java/overrun/marshal/internal/DowncallData.java rename to src/main/java/overrun/marshal/internal/data/DowncallData.java index 351c053..9202355 100644 --- a/src/main/java/overrun/marshal/internal/DowncallData.java +++ b/src/main/java/overrun/marshal/internal/data/DowncallData.java @@ -14,7 +14,7 @@ * copies or substantial portions of the Software. */ -package overrun.marshal.internal; +package overrun.marshal.internal.data; import java.lang.foreign.FunctionDescriptor; import java.lang.foreign.SymbolLookup; diff --git a/src/test/java/overrun/marshal/test/DowncallInheritTest.java b/src/test/java/overrun/marshal/test/DowncallInheritTest.java index b4908fa..b6fb109 100644 --- a/src/test/java/overrun/marshal/test/DowncallInheritTest.java +++ b/src/test/java/overrun/marshal/test/DowncallInheritTest.java @@ -18,8 +18,7 @@ import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; /** * test inherit downcall @@ -39,5 +38,7 @@ void testInheritDowncall() { assertEquals(1, d.get1()); assertEquals(1, d.get2()); assertEquals(3, d.get3()); + + assertEquals(3, assertDoesNotThrow(() -> (int) d.methodHandle("get3").invokeExact())); } } diff --git a/src/test/java/overrun/marshal/test/ID3.java b/src/test/java/overrun/marshal/test/ID3.java index bf987c9..8581968 100644 --- a/src/test/java/overrun/marshal/test/ID3.java +++ b/src/test/java/overrun/marshal/test/ID3.java @@ -16,6 +16,7 @@ package overrun.marshal.test; +import overrun.marshal.DirectAccess; import overrun.marshal.Downcall; import overrun.marshal.gen.Entrypoint; @@ -29,7 +30,7 @@ * @author squid233 * @since 0.1.0 */ -public interface ID3 extends ID1, ID2 { +public interface ID3 extends ID1, ID2, DirectAccess { final class Provider { private static final Linker LINKER = Linker.nativeLinker(); private static final Arena ARENA = Arena.ofAuto(); diff --git a/src/test/java/overrun/marshal/test/IDowncall.java b/src/test/java/overrun/marshal/test/IDowncall.java index 09139a5..28d5715 100644 --- a/src/test/java/overrun/marshal/test/IDowncall.java +++ b/src/test/java/overrun/marshal/test/IDowncall.java @@ -78,10 +78,8 @@ default int testDefault() { int testReturnInt(); - @SizedSeg(12) String testReturnString(); - @SizedSeg(40) @StrCharset("UTF-16") String testReturnUTF16String();