Skip to content

Commit

Permalink
Added Unmarshal::unboundString
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Mar 2, 2024
1 parent a14346e commit f0b6243
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
```

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.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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 10 additions & 2 deletions src/main/java/overrun/marshal/Configurations.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import overrun.marshal.gen.Sized;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand Down Expand Up @@ -79,7 +78,7 @@ private Configurations() {
* @param logger the logger
*/
public static void setApiLogger(Consumer<String> logger) {
apiLogger = Objects.requireNonNullElseGet(logger, () -> System.err::println);
apiLogger = logger != null ? logger : System.err::println;
}

/**
Expand Down Expand Up @@ -122,5 +121,14 @@ public T get() {
}
return value;
}

/**
* Sets the value
*
* @param t the value
*/
public void set(T t) {
value = t;
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/overrun/marshal/Downcall.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -671,7 +671,7 @@ private static <T> 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,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/overrun/marshal/MemoryStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/overrun/marshal/Unmarshal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/overrun/marshal/test/DowncallInheritTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()));
}
}
3 changes: 2 additions & 1 deletion src/test/java/overrun/marshal/test/ID3.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package overrun.marshal.test;

import overrun.marshal.DirectAccess;
import overrun.marshal.Downcall;
import overrun.marshal.gen.Entrypoint;

Expand All @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/overrun/marshal/test/IDowncall.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ default int testDefault() {

int testReturnInt();

@SizedSeg(12)
String testReturnString();

@SizedSeg(40)
@StrCharset("UTF-16")
String testReturnUTF16String();

Expand Down

0 comments on commit f0b6243

Please sign in to comment.