Skip to content

Commit

Permalink
Generate a correct memory layout and accessors for fields containing …
Browse files Browse the repository at this point in the history
…a `GList`/`GSList` (fixes #173)
  • Loading branch information
jwharm committed Dec 28, 2024
1 parent d2b3d6c commit 7addd90
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
4 changes: 2 additions & 2 deletions generator/src/main/java/io/github/jwharm/javagi/JavaGI.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ public static void generate(String namespace,
for (var rt : ns.registeredTypes().values()) {

// Do not generate record types named "...Private" (except for
// GPrivate)
if (rt.skipJava())
// GPrivate) or types that are custom implemented in java-gi
if (rt.skipJava() || rt.customJava())
continue;

typeSpec = switch(rt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ PartialStatement marshalNativeToJava(Type type, String identifier, boolean upcal
PartialStatement elementConstructor = getElementConstructor(type, 0);
PartialStatement elementDestructor = getElementDestructor(type, 0);

if ("runs".equals(v.attr("name"))) {
System.out.println("runs:");
System.out.println(type.toString());
System.out.println(((Type) type.anyTypes().getFirst()).lookup());
System.out.println(elementConstructor.format());
}

// Get parent node (parameter, return value, ...)
Node parent = type.parent();
while (parent instanceof AnyType)
Expand All @@ -330,6 +337,7 @@ PartialStatement marshalNativeToJava(Type type, String identifier, boolean upcal
case InstanceParameter i -> i.transferOwnership();
case ReturnValue r -> r.transferOwnership();
case Property p -> p.transferOwnership();
case Field _ -> NONE;
default -> throw new IllegalStateException();
};

Expand Down
13 changes: 5 additions & 8 deletions generator/src/main/java/io/github/jwharm/javagi/gir/Alias.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@

package io.github.jwharm.javagi.gir;

import com.squareup.javapoet.TypeName;
import io.github.jwharm.javagi.util.PartialStatement;

import static io.github.jwharm.javagi.util.CollectionUtils.*;

import java.lang.foreign.MemorySegment;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -67,12 +65,11 @@ public RegisteredType lookup() {
return null;
}

@Override
public PartialStatement constructorName() {
RegisteredType target = lookup();
return target == null ? null : target.constructorName();
}

/**
* An Alias must be destroyed with the appropriate free-func from the
* target type. So {@link RegisteredType#destructorName} is overridden
* to refer to the target type.
*/
@Override
public PartialStatement destructorName() {
RegisteredType target = lookup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ default String getTypeFunc() {
default boolean skipJava() {
return attrBool("java-gi-skip", false);
}

default boolean customJava() {
return attrBool("java-gi-custom", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public GirElement patch(GirElement element, String namespace) {
if (element instanceof Record r
&& List.of("ByteArray", "HashTable", "List", "SList")
.contains(r.name()))
return r.withAttribute("java-gi-skip", "1");
return r.withAttribute("java-gi-custom", "1");

return element;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.jwharm.javagi.test.gtk;

import org.gnome.glib.SList;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Label;
import org.gnome.pango.Context;
import org.gnome.pango.Layout;
import org.gnome.pango.LayoutLine;
import org.gnome.pango.LayoutRun;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Test reading an {@link SList} from a field.
*/
public class PangoLayoutLineTest {

@Test
public void testPangoLayoutLine() {
Gtk.init();

String TEST_STR = "test";
int TEST_LENGTH = TEST_STR.length();

Label label = new Label();
Context context = label.getPangoContext();
Layout layout = new Layout(context);
layout.setText(TEST_STR, TEST_LENGTH);
SList<LayoutLine> lines = layout.getLinesReadonly();
LayoutLine line = lines.getFirst();
SList<LayoutRun> runs = line.readRuns();
LayoutRun run = runs.getFirst();
assertEquals(TEST_LENGTH, run.readItem().readNumChars());
}
}

0 comments on commit 7addd90

Please sign in to comment.