Skip to content

Commit

Permalink
Struct
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Sep 15, 2024
1 parent c629665 commit a3ed1da
Show file tree
Hide file tree
Showing 15 changed files with 771 additions and 218 deletions.
1 change: 1 addition & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
uses: actions/setup-java@v4
with:
java-version: |
23-ea
${{ matrix.java }}
distribution: 'temurin'
- name: Grant execute permission for gradlew
Expand Down
25 changes: 25 additions & 0 deletions demo/src/main/kotlin/io/github/overrun/marshalgen/test/Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ package io.github.overrun.marshalgen.test
import io.github.overrun.marshalgen.*

fun main() {
val MyStruct = struct("overrungl.gen.MyStruct", cType = "mystruct") {
int("Int")
address("Address")
void_pointer("VoidPointer", javadoc { +"A `void*` member." })
c_int("CInt")
string("String")
const_char_pointer("ConstCharPointer", javadoc { +"A `const char*` member." })
size_t("SizeT")
}
val MyStruct2 = struct("overrungl.gen.MyStruct2", javadoc = javadoc { +"Javadoc" }) {
MyStruct("MyStruct")
}
StructRegistration.generate("overrungl.gen.MyStructRegistration")

downcall("overrungl.gen.MyDowncall", javadoc = javadoc {
+"Paragraph 1"
+"Paragraph 2"
Expand Down Expand Up @@ -72,6 +86,8 @@ fun main() {
"Parameter1" param "The first parameter"
"Parameter2" param "The second parameter"
returns("The returned value")
see("#SkippedFunction()")
see("#ReturnAddress()")
})
string("StringFunction", string * "Parameter1")
void("DefaultFunction1") {
Expand All @@ -88,6 +104,13 @@ fun main() {
""".trimIndent()
)
}
void("DefaultFunction3", string * "Parameter1") {
default(
"""
System.out.println("default operation 3");
""".trimIndent()
)
}
const_char_pointer("NativeType", const_char_pointer * "Parameter1")
handle("ReturnMethodHandle")
string("StringCharset", (string * "Parameter1") { charset("UTF-16") }) {
Expand All @@ -104,11 +127,13 @@ fun main() {
int * "Parameter1",
(int * "Parameter2") { default("42") },
int * "Parameter3",
void_pointer * "Parameter4",
javadoc = javadoc {
+"Default parameters"
"Parameter1" param "The first parameter"
"Parameter2" param "The second parameter"
"Parameter3" param "The third parameter"
"Parameter4" param "The fourth parameter"
})
string("TestDefaultOverload", (int * "Parameter1") { default("42") }, string * "Parameter2")
string("TestReturnOverload")
Expand Down
18 changes: 18 additions & 0 deletions generated/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ java {
languageVersion.set(JavaLanguageVersion.of(23))
}
}

tasks.withType<JavaCompile> {
options.compilerArgs.add("--enable-preview")
}

tasks.withType<Javadoc> {
options {
if (this is CoreJavadocOptions) {
// TODO
addBooleanOption("-enable-preview", true)
addStringOption("source", "23")
if (this is StandardJavadocDocletOptions) {
links("https://over-run.github.io/memstack/", "https://over-run.github.io/marshal/")
}
}
jFlags("-Dstdout.encoding=UTF-8", "-Dstderr.encoding=UTF-8")
}
}
26 changes: 19 additions & 7 deletions generated/src/main/java/overrungl/gen/MyDowncall.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ static void StaticMethod() {
/// @param Parameter1 The first parameter
/// @param Parameter2 The second parameter
/// @return The returned value
/// @see #SkippedFunction()
/// @see #ReturnAddress()
int WithParameter(int Parameter1, double Parameter2);

String StringFunction(String Parameter1);
Expand All @@ -76,17 +78,25 @@ default void DefaultFunction2() {
System.out.println("default operation");
}

default void DefaultFunction3(String Parameter1) {
System.out.println("default operation 3");
}

void DefaultFunction3(MemorySegment Parameter1);

@CType("const char*")
String NativeType(@CType("const char*") String Parameter1);

MemorySegment NativeType(MemorySegment Parameter1);
@CType("const char*")
MemorySegment NativeType(@CType("const char*") MemorySegment Parameter1);

MethodHandle ReturnMethodHandle();

@StrCharset("UTF-16")
String StringCharset(@StrCharset("UTF-16") String Parameter1);

MemorySegment StringCharset(MemorySegment Parameter1);
@StrCharset("UTF-16")
MemorySegment StringCharset(@StrCharset("UTF-16") MemorySegment Parameter1);

@Critical(allowHeapAccess = true)
void CriticalFunction();
Expand All @@ -100,14 +110,16 @@ default void DefaultFunction2() {
/// @param Parameter1 The first parameter
/// @param Parameter2 The second parameter
/// @param Parameter3 The third parameter
void DefaultParamFunction(int Parameter1, int Parameter2, int Parameter3);
/// @param Parameter4 The fourth parameter
void DefaultParamFunction(int Parameter1, int Parameter2, int Parameter3, @CType("void*") @CanonicalType("void*") MemorySegment Parameter4);

/// Default parameters
/// @param Parameter1 The first parameter
/// @param Parameter3 The third parameter
/// @param Parameter4 The fourth parameter
@Skip
default void DefaultParamFunction(int Parameter1, int Parameter3) {
this.DefaultParamFunction(Parameter1, 42, Parameter3);
default void DefaultParamFunction(int Parameter1, int Parameter3, @CType("void*") @CanonicalType("void*") MemorySegment Parameter4) {
this.DefaultParamFunction(Parameter1, 42, Parameter3, Parameter4);
}

String TestDefaultOverload(int Parameter1, String Parameter2);
Expand All @@ -125,7 +137,7 @@ default String TestDefaultOverload(String Parameter2) {

int[] TestIntArray(int[] Parameter1, @Ref int[] Parameter2);

MemorySegment TestIntArray(MemorySegment Parameter1, MemorySegment Parameter2);
MemorySegment TestIntArray(MemorySegment Parameter1, @Ref MemorySegment Parameter2);

MemorySegment[] TestAddressArray(MemorySegment[] Parameter1);

Expand All @@ -140,6 +152,6 @@ default String TestDefaultOverload(String Parameter2) {

void TestAllocator3(Arena Parameter1);

void CanonicalLayouts(@CanonicalType("bool") boolean p0, @CanonicalType("char") byte p1, @CanonicalType("short") short p2, @CanonicalType("int") int p3, @CanonicalType("float") float p4, @CanonicalType("long") long p5, @CanonicalType("long long") long p6, @CanonicalType("double") double p7, @CanonicalType("size_t") long p8, @CanonicalType("wchar_t") int p9);
void CanonicalLayouts(@CType("bool") @CanonicalType("bool") boolean p0, @CType("char") @CanonicalType("char") byte p1, @CType("short") @CanonicalType("short") short p2, @CType("int") @CanonicalType("int") int p3, @CType("float") @CanonicalType("float") float p4, @CType("long") @CanonicalType("long") long p5, @CType("long long") @CanonicalType("long long") long p6, @CType("double") @CanonicalType("double") double p7, @CType("size_t") @CanonicalType("size_t") long p8, @CType("wchar_t") @CanonicalType("wchar_t") int p9);

}
142 changes: 142 additions & 0 deletions generated/src/main/java/overrungl/gen/MyStruct.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// This file is auto-generated. DO NOT EDIT!
package overrungl.gen;
import java.lang.foreign.Linker;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandles;
import overrun.marshal.LayoutBuilder;
import overrun.marshal.Unmarshal;
import overrun.marshal.gen.CType;
import overrun.marshal.gen.CanonicalType;
import overrun.marshal.struct.Struct;
import overrun.marshal.struct.StructAllocator;

/// ## Members
///
/// ### Int
///
/// [Getter](#Int()) - [Setter](#Int(int))
///
/// ### Address
///
/// [Getter](#Address()) - [Setter](#Address(java.lang.foreign.MemorySegment))
///
/// ### VoidPointer
///
/// [Getter](#VoidPointer()) - [Setter](#VoidPointer(java.lang.foreign.MemorySegment))
///
/// A `void*` member.
///
/// ### CInt
///
/// [Getter](#CInt()) - [Setter](#CInt(int))
///
/// ### String
///
/// [Getter](#String()) - [Setter](#String(java.lang.foreign.MemorySegment))
///
/// ### ConstCharPointer
///
/// [Getter](#ConstCharPointer()) - [Setter](#ConstCharPointer(java.lang.foreign.MemorySegment))
///
/// A `const char*` member.
///
/// ### SizeT
///
/// [Getter](#SizeT()) - [Setter](#SizeT(long))
///
/// ## Layout
///
/// ```
/// typedef struct mystruct {
/// int Int;
/// MemorySegment Address;
/// void* VoidPointer;
/// int CInt;
/// String String;
/// const char* ConstCharPointer;
/// size_t SizeT;
/// } MyStruct;
/// ```
///
public interface MyStruct extends Struct<MyStruct> {
/// The struct allocator.
StructAllocator<MyStruct> OF = new StructAllocator<>(MethodHandles.lookup(), LayoutBuilder.struct()
.add(ValueLayout.JAVA_INT, "Int")
.add(ValueLayout.ADDRESS, "Address")
.add(ValueLayout.ADDRESS, "VoidPointer")
.add(ValueLayout.JAVA_INT, "CInt")
.add(Unmarshal.STR_LAYOUT, "String")
.add(Unmarshal.STR_LAYOUT, "ConstCharPointer")
.add(Linker.nativeLinker().canonicalLayouts().get("size_t"), "SizeT")
.build());

@Override
MyStruct slice(long index, long count);

@Override
MyStruct slice(long index);

/// {@return `Int`}
int Int();

/// Sets `Int` with the given value.
/// @param Int the value
/// @return `this`
MyStruct Int(int Int);

/// {@return `Address`}
MemorySegment Address();

/// Sets `Address` with the given value.
/// @param Address the value
/// @return `this`
MyStruct Address(MemorySegment Address);

/// {@return `VoidPointer`}
@CType("void*")
MemorySegment VoidPointer();

/// Sets `VoidPointer` with the given value.
/// @param VoidPointer the value
/// @return `this`
MyStruct VoidPointer(@CType("void*") MemorySegment VoidPointer);

/// {@return `CInt`}
@CType("int")
@CanonicalType("int")
int CInt();

/// Sets `CInt` with the given value.
/// @param CInt the value
/// @return `this`
MyStruct CInt(@CType("int") @CanonicalType("int") int CInt);

/// {@return `String`}
MemorySegment String();

/// Sets `String` with the given value.
/// @param String the value
/// @return `this`
MyStruct String(MemorySegment String);

/// {@return `ConstCharPointer`}
@CType("const char*")
MemorySegment ConstCharPointer();

/// Sets `ConstCharPointer` with the given value.
/// @param ConstCharPointer the value
/// @return `this`
MyStruct ConstCharPointer(@CType("const char*") MemorySegment ConstCharPointer);

/// {@return `SizeT`}
@CType("size_t")
@CanonicalType("size_t")
long SizeT();

/// Sets `SizeT` with the given value.
/// @param SizeT the value
/// @return `this`
MyStruct SizeT(@CType("size_t") @CanonicalType("size_t") long SizeT);

}
48 changes: 48 additions & 0 deletions generated/src/main/java/overrungl/gen/MyStruct2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// This file is auto-generated. DO NOT EDIT!
package overrungl.gen;
import java.lang.foreign.MemorySegment;
import java.lang.invoke.MethodHandles;
import overrun.marshal.LayoutBuilder;
import overrun.marshal.gen.CType;
import overrun.marshal.struct.Struct;
import overrun.marshal.struct.StructAllocator;
import overrungl.gen.MyStruct;

/// Javadoc
///
/// ## Members
///
/// ### MyStruct
///
/// [Getter](#MyStruct()) - [Setter](#MyStruct(java.lang.foreign.MemorySegment))
///
/// ## Layout
///
/// ```
/// typedef struct {
/// mystruct MyStruct;
/// } MyStruct2;
/// ```
///
public interface MyStruct2 extends Struct<MyStruct2> {
/// The struct allocator.
StructAllocator<MyStruct2> OF = new StructAllocator<>(MethodHandles.lookup(), LayoutBuilder.struct()
.add(MyStruct.OF.layout(), "MyStruct")
.build());

@Override
MyStruct2 slice(long index, long count);

@Override
MyStruct2 slice(long index);

/// {@return `MyStruct`}
@CType("mystruct")
MemorySegment MyStruct();

/// Sets `MyStruct` with the given value.
/// @param MyStruct the value
/// @return `this`
MyStruct2 MyStruct(@CType("mystruct") MemorySegment MyStruct);

}
10 changes: 10 additions & 0 deletions generated/src/main/java/overrungl/gen/MyStructRegistration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file is auto-generated. DO NOT EDIT!
package overrungl.gen;
import static overrun.marshal.gen.processor.ProcessorTypes.registerStruct;
final class MyStructRegistration {
private MyStructRegistration() { }
static void registerAll() {
registerStruct(MyStruct.class, MyStruct.OF);
registerStruct(MyStruct2.class, MyStruct2.OF);
}
}
Loading

0 comments on commit a3ed1da

Please sign in to comment.